Slide 1

Slide 1 text

∃ PROLOG | PROLOG ∈ SCALA

Slide 2

Slide 2 text

About me: @folone like types* *same reasons why @propensive does in his “Batshit crazy algebra with types” talk

Slide 3

Slide 3 text

def validate[F[_], G, H, V <: HList, I <: HList, M <: HList, A <: HList, R](g: G)(v: V)(implicit hlG: FnHListerAux[G, A => R], zip: ZipApplyAux[V, I, M], mapped: MappedAux[A, F, M], unH: FnUnHListerAux[I => F[R], H], folder: LeftFolderAux[M, F[A => R], applier.type, F[HNil => R]], appl: Applicative[F]) = unH((in: I) => folder(zip(v, in), hlG(g).point[F]).map(_(HNil)))

Slide 4

Slide 4 text

8+ implicits FnHipsterAux

Slide 5

Slide 5 text

LOGIC PROGRAMMING

Slide 6

Slide 6 text

PROgrammation en LOGique, 1972, 5GL Lingua franca

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

BURAN

Slide 9

Slide 9 text

IBM Watson http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/services-catalog.html

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Main principles It's all about formulating the question. Facts (ground terms) Rules (to generate more facts) RELATIONS > FUNCTIONS WHAT > HOW

Slide 12

Slide 12 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 13

Slide 13 text

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

Slide 14

Slide 14 text

NO.

Slide 15

Slide 15 text

PACKAGE MANAGER

Slide 16

Slide 16 text

Facts depends(scala, java). depends(java, gcc). depends(ghc, clang). depends(erlang, prolog). depends(erlang, clang). depends(prolog, gcc).

Slide 17

Slide 17 text

?- depends(scala, prolog). no ?- depends(java, gcc). yes ?- depends(prolog, X). X = gcc. ?- depends(erlang, X). X = prolog ; X = clang. Questions

Slide 18

Slide 18 text

Questions ?- depends(What, OnWhat). What = scala, OnWhat = java; What = java, OnWhat = gcc; What = ghc, OnWhat = clang; What = erlang, OnWhat = prolog; What = erlang, OnWhat = clang; What = prolog, OnWhat = gcc.

Slide 19

Slide 19 text

common_dep(Pack1, Pack2) :- 
 depends(Pack1, Topic), 
 depends(Pack2, Topic), 
 Pack1 \== Pack2. ?- common_dep(java, What).
 What = prolog. Rules

Slide 20

Slide 20 text

Facts provides(scala, scalac). provides(scala, scala). provides(erlang, erl). provides(java, javac). provides(prolog, swipl). provides(ghc, ghci).

Slide 21

Slide 21 text

deps_needed(Package, What) :- depends(Package, What). deps_needed(Package, What) :- depends(Package, X), depends(X, What).

Slide 22

Slide 22 text

?- deps_needed(scala, What). What = java ; What = gcc. Questions

Slide 23

Slide 23 text

deps_to_run(Command, What) :- provides(What, Command). deps_to_run(Command, What) :- provides(X, Command), deps_needed(X, What). Rules

Slide 24

Slide 24 text

?- deps_to_run(erl, What). What = erlang ; What = prolog ; What = clang ; What = gcc. Questions

Slide 25

Slide 25 text

install(Dep) :- installed(Dep). install(Dep) :- assert(installed(Dep)), write('Installing '), write_term(Dep, []), write('\n'), installed(Dep).

Slide 26

Slide 26 text

run(Command) :- findall(Dep, deps_to_run(Command, Dep), L), reverse(L, Deps), forall(member(Dep, Deps), install(Dep)), write('Running '), write_term(Command, []), write('\n'), !.

Slide 27

Slide 27 text

?- run(ghci). Installing clang Installing ghc Running ghci true. ?- run(erl). Installing gcc Installing prolog Installing erlang Running erl true.

Slide 28

Slide 28 text

github.com/larsyencken/marelle

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Scala is a logic programming language...

Slide 31

Slide 31 text

In type system. programming language...

Slide 32

Slide 32 text

In type system. programming in Scala is... TYPELEVEL

Slide 33

Slide 33 text

logic programming in Scala programming in Scala is...

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

The more interesting your types get, the less fun it is to write them down. – Benjamin Pierce —

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

DEMO github.com/folone/2014-scalaio

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Promoting Functions to Type Families in Haskell HMonoid, HFunctor, HApplicative, HMonad Related Fun with type functions The art of Prolog Shapeless

Slide 44

Slide 44 text

@milessabin @xeno_by @travisbrown @larsr_h @killnicole Thanks

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

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