SlideShare a Scribd company logo
There is a Prolog
in your Scala!
@folone Scala eXchange ‘13
One particular
approach
8+ implicits
FnHipsterAux
Logic
Programming
programmation en
logique, 1972, 5GL
Lingua franca
A language that doesn't
affect the way you think
about programming,
is not worth knowing.
— Alan J. Perlis
Buran
IBM Watson
There's a Prolog in your Scala!
What > How
Relations > Functions
Facts
Rules (to generate more facts)
Main principles
It's all about formulating the question.
Expressed with
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
— How many Prolog
programmers does it take
to change a lightbulb?
— How many Prolog
programmers does it take
to change a lightbulb?
NO.
Scala eXchange
tracks planning
https://github.com/folone/scalaeXchange/
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).
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.
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.
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.
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).
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.
Questions
?- same_topic(dpp, Who).
Who = sirthias ;
Who = milessabin ;
Who = sirthias.
Rules
exactly_same_topic(Person1, Person2) :-
  talk_about(Person1, Topic),
  talk_about(Person2, Topic),
  works_in(Person1, Area),
  works_in(Person2, Area),
  Person1 == Person2.
Questions
?- exactly_same_topic(dpp, Who).
Who = sirthias.
?- exactly_same_topic(milessabin, Who).
Who = larsr_h ;
Who = xeno_by.
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]).
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]).
Functional is imperative without state.
Logic is functional without manual search*.
* DFS
Scala is a logic
programming language...
in type system!
Scala is a logic
programming language...
TYPELEVEL
programming in Scala is...
in type system!
programming language...
logic programming
TYPELEVEL
programming in Scala is...
in type system!
in Scala!
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).
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}
}
There's a Prolog in your Scala!
There's a Prolog in your Scala!
Facts — implicit vals.
Rules — implicit defs taking implicit
vals as parameters.
Implication is the other way around.
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.)
— So now I need to redefine all the
goodness from prolog stdlib?! Lists,
naturals, etc.
Shapeless: stdlib for logic programming
in scala.
DEMO
https://github.com/folone/scalaeXchange/
Functional in the small,
OO in the large,
logic in the type system!
http://mitpress.mit.edu/books/art-prolog
shapeless (and all the typelevel.scala libraries)
metascala https://www.assembla.com/spaces/metascala/wiki
Links
@milessabin
@xeno_by
@travisbrown
@larsr_h
@killnicole
Thanks
?-
(λx.folonexlambda-calcul.us)@

More Related Content

PDF
Python - Lecture 4
PDF
Scala - en bedre og mere effektiv Java?
PDF
JS OO and Closures
PDF
Scala - en bedre Java?
PDF
Meet scala
PPS
Making an Object System with Tcl 8.5
PPTX
Optimizing Tcl Bytecode
PDF
core.logic introduction
Python - Lecture 4
Scala - en bedre og mere effektiv Java?
JS OO and Closures
Scala - en bedre Java?
Meet scala
Making an Object System with Tcl 8.5
Optimizing Tcl Bytecode
core.logic introduction

What's hot (19)

PDF
01. haskell introduction
PDF
Logic programming a ruby perspective
PDF
R for Pythonistas (PyData NYC 2017)
PDF
Spark workshop
KEY
ぐだ生 Java入門第一回(equals hash code_tostring)
PDF
Clojure: The Art of Abstraction
PDF
The Logical Burrito - pattern matching, term rewriting and unification
KEY
Hello, Guava !
PDF
Introducing: A Complete Algebra of Data
PDF
Beyond xUnit example-based testing: property-based testing with ScalaCheck
PPT
PPTX
Introduction to Haskell: 2011-04-13
PPTX
Should I Use Scalding or Scoobi or Scrunch?
PDF
Hammurabi
ODP
Introduction to R
PPTX
Context
PDF
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
PDF
From Lisp to Clojure/Incanter and RAn Introduction
PDF
An Introduction to RxJava
01. haskell introduction
Logic programming a ruby perspective
R for Pythonistas (PyData NYC 2017)
Spark workshop
ぐだ生 Java入門第一回(equals hash code_tostring)
Clojure: The Art of Abstraction
The Logical Burrito - pattern matching, term rewriting and unification
Hello, Guava !
Introducing: A Complete Algebra of Data
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Introduction to Haskell: 2011-04-13
Should I Use Scalding or Scoobi or Scrunch?
Hammurabi
Introduction to R
Context
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
From Lisp to Clojure/Incanter and RAn Introduction
An Introduction to RxJava
Ad

Viewers also liked (20)

PDF
PDF
Csp scala wixmeetup2016
PDF
Exploring the Scala ecosystem
PDF
42.type: Literal-based Singleton types
ODP
Introduction to Scala Macros
PDF
Few simple-type-tricks in scala
ODP
Shapeless- Generic programming for Scala
PDF
Scala Type Classes: Basics and More
ODP
Effective way to code in Scala
PDF
Industrial use of formal methods
PPTX
Compilers Are Databases
PPTX
Practical type mining in Scala
PDF
The art of decomposing monoliths
PPTX
Purely Functional Data Structures in Scala
PDF
Macro in Scala
PDF
Scala Frameworks for Web Application 2016
PPTX
Advanced Functional Programming in Scala
PDF
Scala Days NYC 2016
PPTX
ゆるふわScalaコップ本読書会 第7章
PDF
Demystifying Scala Type System
Csp scala wixmeetup2016
Exploring the Scala ecosystem
42.type: Literal-based Singleton types
Introduction to Scala Macros
Few simple-type-tricks in scala
Shapeless- Generic programming for Scala
Scala Type Classes: Basics and More
Effective way to code in Scala
Industrial use of formal methods
Compilers Are Databases
Practical type mining in Scala
The art of decomposing monoliths
Purely Functional Data Structures in Scala
Macro in Scala
Scala Frameworks for Web Application 2016
Advanced Functional Programming in Scala
Scala Days NYC 2016
ゆるふわScalaコップ本読書会 第7章
Demystifying Scala Type System
Ad

Similar to There's a Prolog in your Scala! (20)

PDF
Intro to threp
PDF
Lambda? You Keep Using that Letter
PDF
Alpine Spark Implementation - Technical
PDF
Multinomial Logistic Regression with Apache Spark
PPT
apache spark presentation for distributed processing
PPT
SPARK bigdata pyspark databricks learn spark.ppt
PDF
Apache Spark & Streaming
PDF
Meta-objective Lisp @名古屋 Reject 会議
PPT
Profiling and optimization
PPTX
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
PDF
lec4_annotated.pdf ml csci 567 vatsal sharan
PPTX
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
PPTX
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
ODP
Scala as a Declarative Language
PPTX
ScalaDays 2013 Keynote Speech by Martin Odersky
PDF
Real Time Big Data Management
PPT
AlgorithmAnalysis2.ppt
PPTX
PDF
DeepStochLog: Neural Stochastic Logic Programming
Intro to threp
Lambda? You Keep Using that Letter
Alpine Spark Implementation - Technical
Multinomial Logistic Regression with Apache Spark
apache spark presentation for distributed processing
SPARK bigdata pyspark databricks learn spark.ppt
Apache Spark & Streaming
Meta-objective Lisp @名古屋 Reject 会議
Profiling and optimization
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
lec4_annotated.pdf ml csci 567 vatsal sharan
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
Scala as a Declarative Language
ScalaDays 2013 Keynote Speech by Martin Odersky
Real Time Big Data Management
AlgorithmAnalysis2.ppt
DeepStochLog: Neural Stochastic Logic Programming

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
KodekX | Application Modernization Development
PDF
Encapsulation theory and applications.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Cloud computing and distributed systems.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Big Data Technologies - Introduction.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
The AUB Centre for AI in Media Proposal.docx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
KodekX | Application Modernization Development
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
Digital-Transformation-Roadmap-for-Companies.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
MYSQL Presentation for SQL database connectivity
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
Cloud computing and distributed systems.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Big Data Technologies - Introduction.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf

There's a Prolog in your Scala!