Slide 1

Slide 1 text

There is a Prolog in your Scala! @folone Scala eXchange ‘13

Slide 2

Slide 2 text

One particular approach

Slide 3

Slide 3 text

8+ implicits FnHipsterAux

Slide 4

Slide 4 text

Logic Programming

Slide 5

Slide 5 text

programmation en logique, 1972, 5GL Lingua franca

Slide 6

Slide 6 text

A language that doesn't affect the way you think about programming, is not worth knowing. — Alan J. Perlis

Slide 7

Slide 7 text

Buran

Slide 8

Slide 8 text

IBM Watson

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

What > How Relations > Functions Facts Rules (to generate more facts) Main principles It's all about formulating the question. Expressed with

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

— How many Prolog programmers does it take to change a lightbulb?

Slide 13

Slide 13 text

— How many Prolog programmers does it take to change a lightbulb? NO.

Slide 14

Slide 14 text

Scala eXchange tracks planning https://github.com/folone/scalaeXchange/

Slide 15

Slide 15 text

Facts talk_about(dpp, webdev). talk_about(milessabin, typelevel). talk_about(larsr_h, typelevel). talk_about(xeno_by, macros). talk_about(milessabin, macros). talk_about(sirthias, webdev).

Slide 16

Slide 16 text

Questions ?- talk_about(milessabin, webdev). no ?- talk_about(larsr_h, typelevel). yes ?- talk_about(dpp, X). X = webdev. ?- talk_about(milessabin, X). X = typelevel ; X = macros.

Slide 17

Slide 17 text

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.

Slide 18

Slide 18 text

Rules same_topic(Person1, Person2) :- talk_about(Person1, Topic), talk_about(Person2, Topic), Person1 \== Person2. ?- same_topic(milessabin, Who). Who = larsr_h ; Who = xeno_by.

Slide 19

Slide 19 text

Facts works_in(dpp, industry). works_in(milessabin, industry). works_in(milessabin, academia). works_in(larsr_h, academia). works_in(xeno_by, academia). works_in(sirthias, industry).

Slide 20

Slide 20 text

same_topic(Person1, Person2) :- talk_about(Person1, Topic), talk_about(Person2, Topic), Person1 \== Person2. same_topic(Person1, Person2) :- works_in(Person1, Area), works_in(Person2, Area), Person1 \== Person2.

Slide 21

Slide 21 text

Questions ?- same_topic(dpp, Who). Who = sirthias ; Who = milessabin ; Who = sirthias.

Slide 22

Slide 22 text

Rules exactly_same_topic(Person1, Person2) :- talk_about(Person1, Topic), talk_about(Person2, Topic), works_in(Person1, Area), works_in(Person2, Area), Person1 \== Person2.

Slide 23

Slide 23 text

Questions ?- exactly_same_topic(dpp, Who). Who = sirthias. ?- exactly_same_topic(milessabin, Who). Who = larsr_h ; Who = xeno_by.

Slide 24

Slide 24 text

topic(Topic, Res) :- findall(Person, talk_about(Person, Topic), L1), Res = (Topic, L1). environment(Area, Res) :- findall(Person, works_in(Person, Area), L1), Res = (Area, L1). ?- topic(webdev, List). List = (webdev, [dpp, sirthias]). ?- environment(academia, List). List = (academia, [milessabin, larsr_h, xeno_by]).

Slide 25

Slide 25 text

topics(L) :- findall(Topic, talk_about(_, Topic), L1), list_to_set(L1, L). tracks(L) :- topics(L1), member(Topic, L1), topic(Topic, L). ?- topics(L). L = [webdev, typelevel, macros]. ?- tracks(L). L = (webdev, [dpp, sirthias]) ; L = (typelevel, [milessabin, larsr_h]) ; L = (macros, [xeno_by, milessabin]).

Slide 26

Slide 26 text

Functional is imperative without state. Logic is functional without manual search*. * DFS

Slide 27

Slide 27 text

Scala is a logic programming language...

Slide 28

Slide 28 text

in type system! Scala is a logic programming language...

Slide 29

Slide 29 text

TYPELEVEL programming in Scala is... in type system! programming language...

Slide 30

Slide 30 text

logic programming TYPELEVEL programming in Scala is... in type system! in Scala!

Slide 31

Slide 31 text

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).

Slide 32

Slide 32 text

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} }

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Facts — implicit vals. Rules — implicit defs taking implicit vals as parameters. Implication is the other way around.

Slide 36

Slide 36 text

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.)

Slide 37

Slide 37 text

— So now I need to redefine all the goodness from prolog stdlib?! Lists, naturals, etc. Shapeless: stdlib for logic programming in scala.

Slide 38

Slide 38 text

DEMO https://github.com/folone/scalaeXchange/

Slide 39

Slide 39 text

Functional in the small, OO in the large, logic in the type system!

Slide 40

Slide 40 text

http://mitpress.mit.edu/books/art-prolog shapeless (and all the typelevel.scala libraries) metascala https://www.assembla.com/spaces/metascala/wiki Links

Slide 41

Slide 41 text

@milessabin @xeno_by @travisbrown @larsr_h @killnicole Thanks

Slide 42

Slide 42 text

?- (λx.folonexlambda-calcul.us)@