Video is available here: http://skillsmatter.com/podcast/scala/theres-a-prolog-in-your-scala
Visualizations from slides 33-34 are available here:
slide 33 (scala -> prolog): https://db.tt/pjVwnQuj
slide 34 (prolog -> scala): https://db.tt/R5NSJF5g
document from slide 7: http://www.foia.cia.gov/search-results?search_api_views_fulltext=SW+90-10029X&field_collection=
paper from slide 9: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.3972
DEMO repository: https://github.com/folone/scalaeXchange
Copyrights:
The photo of Battersea Power station: http://www.flickr.com/photos/stephenwalford/8420082569/
The photo of hanoi towers: http://www.flickr.com/photos/46268742@N00/8455144986/
The photo of Buran is taken from the following article: http://englishrussia.com/2006/09/14/buran-the-first-russian-shuttle/
Photo of Spock, played by Zachary Quinto, is presumably copyrighted by Paramount Pictures, Skydance Productions, and Bad Robot.
10. What > How
Relations > Functions
Facts
Rules (to generate more facts)
Main principles
It's all about formulating the question.
Expressed with
11. Predicates > Functions
Map inputs to outputs
Run
Return some output
Define constraints
Match
Only return yes or no*
*If "yes", they may add bindings to variables.
log(2, 16, 4). log(2, 16) = 4
12. — How many Prolog
programmers does it take
to change a lightbulb?
13. — How many Prolog
programmers does it take
to change a lightbulb?
NO.
17. Questions
?- talk_about(Who, What).
Who = dpp, What = webdev ;
Who = milessabin, What = typelevel ;
Who = milessabin, What = macros ;
Who = larsr_h, What = typelevel ;
Who = xeno_by, What = macros ;
Who = sirthias, What = webdev.
31. Prolog
gcd(X, X, X).
gcd(X, Y, Out) :-
X < Y
Z is Y - X
gcd(X, Z, Out).
gcd(X, Y, Out) :-
Y < X,
gcd(Y, X, Out).
32. trait GCD[X <: Nat, Y <: Nat] { type Out <: Nat }
object GCD
def gcd[N<:Nat](x:Nat,y:Nat)(implicit gcd:Aux[x.N,y.N,N],wn:Witness.Aux[N]):N =
wn.value
type Aux[X <: Nat, Y <: Nat, Z <: Nat] = GCD[X, Y] { type Out = Z }
Scala
{
implicit def gcd0[X <: Nat]:
Aux[X, X, X] =
new GCD[X, X] { type Out = X }
implicit def gcd1[X <: Nat, Y <: Nat, Z <: Nat,
Out0 <: Nat]
(implicit ev0 : LT[X, Y],
ev1 : Diff.Aux[Y, X, Z],
ev2 : Aux[X, Z, Out0]):
Aux[X, Y, Out0] =
new GCD[X, Y] { type Out = Out0 }
implicit def gcd2[X <: Nat, Y <: Nat, Out0 <: Nat]
(implicit ev0 : LT[Y, X],
ev1 : Aux[Y, X, Out0]):
Aux[X, Y, Out0] =
new GCD[X, Y] { type Out = Out0}
}
33.
34.
35. Facts — implicit vals.
Rules — implicit defs taking implicit
vals as parameters.
Implication is the other way around.
36. Gotchas:
• Scala does not perform the DFS. In case of
multiple implicits, it does not compile. Prioritizing
implicits via inheritance.
• Diverging implicit expansion -Xlog-implicits.
• Extremely hard to debug (pen and paper style).
• Peculiar exceptions (SO in compiler, Method too
large, etc.)
37. — So now I need to redefine all the
goodness from prolog stdlib?! Lists,
naturals, etc.
Shapeless: stdlib for logic programming
in scala.