SlideShare uma empresa Scribd logo
1 de 42
Baixar para ler offline
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
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}
}
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)@

Mais conteúdo relacionado

Mais procurados

ぐだ生 Java入門第一回(equals hash code_tostring)
ぐだ生 Java入門第一回(equals hash code_tostring)ぐだ生 Java入門第一回(equals hash code_tostring)
ぐだ生 Java入門第一回(equals hash code_tostring)
Makoto Yamazaki
 
The Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unificationThe Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unification
Norman Richards
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
agnonchik
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introduction
elliando dias
 

Mais procurados (20)

01. haskell introduction
01. haskell introduction01. haskell introduction
01. haskell introduction
 
Logic programming a ruby perspective
Logic programming a ruby perspectiveLogic programming a ruby perspective
Logic programming a ruby perspective
 
R for Pythonistas (PyData NYC 2017)
R for Pythonistas (PyData NYC 2017)R for Pythonistas (PyData NYC 2017)
R for Pythonistas (PyData NYC 2017)
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
ぐだ生 Java入門第一回(equals hash code_tostring)
ぐだ生 Java入門第一回(equals hash code_tostring)ぐだ生 Java入門第一回(equals hash code_tostring)
ぐだ生 Java入門第一回(equals hash code_tostring)
 
Clojure: The Art of Abstraction
Clojure: The Art of AbstractionClojure: The Art of Abstraction
Clojure: The Art of Abstraction
 
The Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unificationThe Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unification
 
Hello, Guava !
Hello, Guava !Hello, Guava !
Hello, Guava !
 
Introducing: A Complete Algebra of Data
Introducing: A Complete Algebra of DataIntroducing: A Complete Algebra of Data
Introducing: A Complete Algebra of Data
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13Introduction to Haskell: 2011-04-13
Introduction to Haskell: 2011-04-13
 
Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch? Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch?
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Context
ContextContext
Context
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introduction
 
An Introduction to RxJava
An Introduction to RxJavaAn Introduction to RxJava
An Introduction to RxJava
 

Destaque

Destaque (20)

Slides
SlidesSlides
Slides
 
Csp scala wixmeetup2016
Csp scala wixmeetup2016Csp scala wixmeetup2016
Csp scala wixmeetup2016
 
Exploring the Scala ecosystem
Exploring the Scala ecosystemExploring the Scala ecosystem
Exploring the Scala ecosystem
 
42.type: Literal-based Singleton types
42.type: Literal-based Singleton types42.type: Literal-based Singleton types
42.type: Literal-based Singleton types
 
Introduction to Scala Macros
Introduction to Scala MacrosIntroduction to Scala Macros
Introduction to Scala Macros
 
Few simple-type-tricks in scala
Few simple-type-tricks in scalaFew simple-type-tricks in scala
Few simple-type-tricks in scala
 
Shapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaShapeless- Generic programming for Scala
Shapeless- Generic programming for Scala
 
Scala Type Classes: Basics and More
Scala Type Classes:  Basics and MoreScala Type Classes:  Basics and More
Scala Type Classes: Basics and More
 
Effective way to code in Scala
Effective way to code in ScalaEffective way to code in Scala
Effective way to code in Scala
 
Industrial use of formal methods
Industrial use of formal methodsIndustrial use of formal methods
Industrial use of formal methods
 
Compilers Are Databases
Compilers Are DatabasesCompilers Are Databases
Compilers Are Databases
 
Practical type mining in Scala
Practical type mining in ScalaPractical type mining in Scala
Practical type mining in Scala
 
The art of decomposing monoliths
The art of decomposing monolithsThe art of decomposing monoliths
The art of decomposing monoliths
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
Macro in Scala
Macro in ScalaMacro in Scala
Macro in Scala
 
Scala Frameworks for Web Application 2016
Scala Frameworks for Web Application 2016Scala Frameworks for Web Application 2016
Scala Frameworks for Web Application 2016
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
ゆるふわScalaコップ本読書会 第7章
ゆるふわScalaコップ本読書会 第7章ゆるふわScalaコップ本読書会 第7章
ゆるふわScalaコップ本読書会 第7章
 
Demystifying Scala Type System
Demystifying Scala Type SystemDemystifying Scala Type System
Demystifying Scala Type System
 

Semelhante a There's a Prolog in your Scala!

Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議
dico_leque
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
g3_nittala
 
An Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsAn Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional Paradigms
Miles Sabin
 

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

Intro to threp
Intro to threpIntro to threp
Intro to threp
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Multinomial Logistic Regression with Apache Spark
Multinomial Logistic Regression with Apache SparkMultinomial Logistic Regression with Apache Spark
Multinomial Logistic Regression with Apache Spark
 
Alpine Spark Implementation - Technical
Alpine Spark Implementation - TechnicalAlpine Spark Implementation - Technical
Alpine Spark Implementation - Technical
 
Apache Spark & Streaming
Apache Spark & StreamingApache Spark & Streaming
Apache Spark & Streaming
 
Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
Eta
EtaEta
Eta
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
 
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
 
AlgorithmAnalysis2.ppt
AlgorithmAnalysis2.pptAlgorithmAnalysis2.ppt
AlgorithmAnalysis2.ppt
 
Scala
ScalaScala
Scala
 
DeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic ProgrammingDeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic Programming
 
Introducción a hadoop
Introducción a hadoopIntroducción a hadoop
Introducción a hadoop
 
An Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsAn Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional Paradigms
 
Beauty and the beast - Haskell on JVM
Beauty and the beast  - Haskell on JVMBeauty and the beast  - Haskell on JVM
Beauty and the beast - Haskell on JVM
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

There's a Prolog in your Scala!