SlideShare a Scribd company logo
The Logical Burrito Episode I:
The Road to Unification
an exploration of pattern matching, term rewriting and unification
(wrapped up in a warm flour tortilla)
Norman Richards
orb@nostacktrace.com
@MaximoBurrito
Goals
• Explore unification as groundwork for understanding logic
programming in general and core.logic (mini-kanren based) in
specific
• Rather than focus on unification, this is a survey of some loosely
related (in my mind) topics - pattern matching and and term
rewriting
• Will demonstrate these ideas in other languages: ML for pattern
matching, Mathematica for term rewriting, and Prolog for unification
Pattern Matching
as a dispatch mechanism (or simple conditional)
a way to express the selection criteria for which code to execute
• In Clojure, dispatch is on arity: (defn foo ([x] …) ([x y] …))
• you can destructure but you can’t ask questions about the
types or value
• OO dispatch - based on type of first arg (.getArea shape)
• Multiple dispatch - based on type of all args (method overriding is
compile time) multimethods
Dispatch
Multimethods
(defmulti fact identity)
(defmethod fact 0 [_]
1)
(defmethod fact :default [n]
(* n (fact (dec n))))
Multimethods
(defmulti fizzbuzz identity)
(defmethod fizzbuzz 1 [n] (str n))
(defmethod fizzbuzz 2 [n] (str n))
(defmethod fizzbuzz 3 [n] "fizz")
;; ... this isn't going to work
Multimethods
;; we can make arbitrary dispatch rules
(defmulti fizzbuzz
(fn [n]
[(zero? (mod n 3))
(zero? (mod n 5))]))
 
;; as long as we can match the defaults
(defmethod fizzbuzz [true true] [n] "fizzbuzz")
(defmethod fizzbuzz [true false] [n] "fizz")
(defmethod fizzbuzz [false true] [n] "buzz")
(defmethod fizzbuzz [false false] [n] (str n))
Multimethods
(defmulti beats vector)
;; or if we have a single default value
(defmethod beats :default [m1 m2] false)
(defmethod beats [:paper :rock] [m1 m2] true)
(defmethod beats [:rock :scissors] [m1 m2] true)
(defmethod beats [:scissors :paper] [m1 m2] true)
Multimethods
• Multimethods are great for what they do, but they aren't a
replacement for pattern maching
• Clojure doesn't have great pattern matching, so we'll consider ML
Pattern Matching (SML)
(* SML matching at the function level*)
fun fac 0 = 1
| fac n = n * fac (n - 1);
Pattern Matching (SML)
val div_3 = div_by 3;
val div_5 = div_by 5;
 
(* No guard statements, but we can match in the
body of the function on derived values *)
fun fizzbuzz n =
case (div_3 n, div_5 n) of
(true, true) => "FizzBuzz"
| (true, false) => "Fizz"
| (false,true) => "Buzz"
| _ => Int.toString n (* underscore matches anything
*)
Pattern Matching (SML)
datatype suit = Clubs | Diamonds | Hearts | Spades
datatype rank = Jack | Queen | King | Ace | Num of int
type card = suit * rank
datatype color = Red | Black
 
(* non-default don't cares exceed what multimethods can do *)
fun card_color (Diamonds, _) = Red
| card_color (Hearts , _) = Red
| card_color _ = Black
 
(* note deep destructuring *)
fun card_value (_, Num(n)) = n
| card_value (_, Ace) = 11
| card_value _ = 10
core.match
(defn card_color [[suit value]]
(match [suit value]
[:diamonds _] :red
[:hearts _] :red
[_ _] :black)) 
 
(defn card_value [card]
(match card
[_ (n :guard number?)] n
[_ :ace] 11
:else 10))
 
Limitations of core.match
• No defnm to use at language level
• Syntax can be ugly
• Can't match non-linear patterns: [n n]
(define eval-exp
(lambda (expr env)
(pmatch expr
[,x (guard (symbol? x)) ;; variable
(lookup x env)]
[(quote ,datum) datum]
[(list . ,expr*)
;; (map (lambda (expr) (eval-exp expr env)) expr*)
(eval-exp* expr* env)]
[(lambda (,x) ,body) ;; abstraction
`(closure ,x ,body ,env)]
[(,e1 ,e2) ;; application
(let ((proc (eval-exp e1 env))
(val (eval-exp e2 env)))
(pmatch proc
[(closure ,x ,body ,envˆ)
;; evaluate body in an extended environment
(eval-exp body `((,x . ,val) . ,envˆ))]
[,else (error 'eval-exp "e1 does not evaluate to a procedure")]))])))
A scheme example (pmatch)
Term rewriting
Term rewriting is a branch of theoretical computer
science which combines elements of logic, universal
algebra, automated theorem proving and functional
programming.  [...] This constitutes a Turing-complete
computational model which is very close to functional
programming.
- Term Rewriting and All That
Mathematica
The innermost kernel of the Mathematica language is essentially
nothing else than a higher-order, conditional rewrite language,
efficiently and professionally implemented.
- Mathematica as a Rewrite Language (Bruno Buchberger)
Rewriting in Mathematica
In[1]:= sum[m_, 0] := m;
sum[m_, s[n_]] := s[sum[m, n]];
In[3]:= sum[s[s[0]], s[s[s[s[0]]]]] 
Out[3]= s[s[s[s[s[s[0]]]]]]
Rewriting in Mathematica
In[4]:= map[f_, {}] := {};
map[f_, {x_, xs___}] := Prepend[map[f, {xs}], f[x]];
 
In[6]:= map[something, {1, 2, 3, 4, 5}]
 
Out[6]= {something[1], something[2],
something[3], something[4], something[5]}
Rewriting in Mathematica
In[7]:= something[n_] := dont_care /; OddQ[n]
 
In[8]:= map[something, {1, 2, 3, 4, 5}]
 
Out[8]= {dont_care,
something[2],
dont_care,
something[4],
dont_care}
Rewriting in Clojure (termito)
(defrules s-rule
[(+ ?x 0) ?x]
[(+ ?x (s ?y)) (s (+ ?x ?y))])
(simplify '(+ (s (s 0)) (s (s (s (s 0))))) s-rule)
;; (s (s (s (s (s (s 0))))))
Rewriting in Clojure (termito)
(defnc numbers [x] (number? x))
(defnc commutative? [op] (or (= op '*) (= op '+)))
(defnc associative? [op] (or (= op '*) (= op '+)))
(defrules zero-rules
[(* 0 ?x) 0])
(defrules identity-rules
[(* 1 ?x) ?x]
[(+ 0 ?x) ?x])
(defrules associative-rules
[(?op ?x (?op ?y ?z))
:when [?op ~associative? #{?x ?y} ~numberc]
(?op (?op ?x ?y) ?z)])
Unification
Unification is a basic operation on terms.  Two terms
unify if substitutions can be made for any variables in
the terms so that the terms are made identical.  If no
such substitution exists, then the terms do not unify.
- Clause and Effect
Unification with Prolog
?- 42 = 42.
true.
?- six_times_nine = 42.
false.
?- enough = enough.
true.
Constants unify with themselves
Unification with Prolog
?- City = austin.
City = austin.
?- 7 = Number.
Number = 7.
uninstantiated logic variables unify with constants.
Unification with Prolog
?- X = Y.
X = Y.
?- A=B, C=D, C=B.
A = B, B = C, C = D.
uninstantiated variables unify with each other and co-refer
Unification with Prolog
?- A=B, C=D, C=B, A=pi.
A = B, B = C, C = D, D = pi.
?- A=B, C=D, C=B, A=pi, D=tau.
false.
uninstantiated variables unify with each other and co-refer
Unification with Prolog
?- foo(1) = x.
false.
?- foo(1) = foo(2).
false.
?- foo(X) = bar(X).
false.
?- foo(X) = foo(Y).
X = Y.
complex terms unify if they have the same head and all their parts unify
Unification with Prolog
?- foo(X,Y)=foo(2,3).
X = 2,
Y = 3.
?- foo(X,5) =
foo(7,Y).
X = 7,
Y = 5.
?- foo(X,5) =
foo(9,X).
false.
Unification with Prolog
?- foo(A,B,B) = foo(B, B, A).
A = B.
?- foo(bar(Z)) = foo(X), X= bar(baz).
Z = baz,
X = bar(baz).
?- f(X, a(b,c)) = f(d, a(Z,c)).
X = d,
Z = b.
Unification with Clojure
• core.logic unifier has more features, more actively maintained
• core.unify has the prettier API, less baggage
core.unify vs core.logic
• core.logic
• (unify [1 '?x])  => 1
• (unifier [1 '?x]) ==> {?x 1}
• core.unify
• (unify 1 '?x) ==> {?x 1}
• (unifier 1 '?x) ==> 1
core.logic unification
(unifier '[dog dog])
;; {}
(unifier '[dog cat])
;; nil
(unifier '[?animal dog])
;; {?animal dog}
 
(unifier '[?foo ?bar])
;; {?foo ?bar}
core.logic unification
(unifier '[{:name bob :age 42}
{:name ?X :age ?Y}])
;; {?X bob, ?Y 42}
 
(unifier '[{:name bob :age 42}
{:name ?X}])
;; nil
core.logic unification
(unifier {:when {'?num numberc}}
'[[?num ?animal] [:magical :unicorn]])
;; nil
 
(unifier {:when {'?num numberc}}
'[[?num ?animal] [42 :unicorn]])
;; {?animal :unicorn, ?num 42}
core.logic unification
(unifier {:as '{?a 3}}
'[?a ?b])
;; {?b 3, ?a 3}
 
(unifier {:as '{?a 3 ?b 4}}
'[?a ?b])
;; nil
(unifier {:as (unifier '[?a ?b])}
'[(?c 5) (?a ?b)])
;; {?b 5, ?a 5, ?c 5}
(defn unifier+ [& pairs]
(loop [u {} pairs pairs]
(if (seq pairs)
(when-let [u' (unifier {:as u} (first pairs))]
(recur (merge u u')
(rest pairs)))
u)))
(unifier+ '[?x 1]
'[?y 2]
'[?z [?x ?y]])
;; {?z [1 2], ?y 2, ?x 1}
core.logic unification
(defn q [data query]
(filter #(unifier+ (conj query %)) data))
(def things [{:name "thing 1" :color :red}
{:name "thing 2" :color :blue}
{:name "thing 3" :color :red}])
 
(q things '[{:name ?name :color :red}])
;; ({:color :red, :name "thing 1"}
{:color :red, :name "thing 3"})
core.logic unification
Why?
• Logic programming
• Machine Learning / Natural language processing
• Type inferencing / type checking
• Theorem proving / Equation solving
Until the next logical burrito,
(run* [your-location]
(conde
[(== your-location :home)]
[succeed])
(!= your-location :here))
Norman Richards
orb@nostacktrace.com
@MaximoBurrito

More Related Content

What's hot

CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodecamp Romania
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Science
Mike Anderson
 
Introduction to Gremlin
Introduction to GremlinIntroduction to Gremlin
Introduction to GremlinMax De Marzi
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
osfameron
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GPars
Paul King
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
Ruslan Shevchenko
 
Python dictionary : past, present, future
Python dictionary: past, present, futurePython dictionary: past, present, future
Python dictionary : past, present, future
delimitry
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)
Gagan Agrawal
 
Scala vs java 8
Scala vs java 8Scala vs java 8
Scala vs java 8
François Sarradin
 
concurrency gpars
concurrency gparsconcurrency gpars
concurrency gparsPaul King
 
Hammurabi
HammurabiHammurabi
Hammurabi
Mario Fusco
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
intelliyole
 
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 Introductionelliando dias
 
Let the type system be your friend
Let the type system be your friendLet the type system be your friend
Let the type system be your friend
The Software House
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
Susan Potter
 

What's hot (19)

CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical Groovy
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Science
 
Introduction to Gremlin
Introduction to GremlinIntroduction to Gremlin
Introduction to Gremlin
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GPars
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Python dictionary : past, present, future
Python dictionary: past, present, futurePython dictionary: past, present, future
Python dictionary : past, present, future
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)GPars (Groovy Parallel Systems)
GPars (Groovy Parallel Systems)
 
Scala vs java 8
Scala vs java 8Scala vs java 8
Scala vs java 8
 
concurrency gpars
concurrency gparsconcurrency gpars
concurrency gpars
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
 
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
 
Let the type system be your friend
Let the type system be your friendLet the type system be your friend
Let the type system be your friend
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
 

Similar to The Logical Burrito - pattern matching, term rewriting and unification

Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Bryan O'Sullivan
 
Implementing pattern-matching in JavaScript (full version)
Implementing pattern-matching in JavaScript (full version)Implementing pattern-matching in JavaScript (full version)
Implementing pattern-matching in JavaScript (full version)
François-Guillaume Ribreau
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
Koen Handekyn
 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
Eelco Visser
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In RubyRoss Lawley
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
JavaDayUA
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
Werner Hofstra
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
Prashant Kalkar
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
Debasish Ghosh
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
rik0
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
PyCon Italia
 
ScalaBlitz
ScalaBlitzScalaBlitz
Introduction to R
Introduction to RIntroduction to R
Introduction to Ragnonchik
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
Software Guru
 

Similar to The Logical Burrito - pattern matching, term rewriting and unification (20)

Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Implementing pattern-matching in JavaScript (full version)
Implementing pattern-matching in JavaScript (full version)Implementing pattern-matching in JavaScript (full version)
Implementing pattern-matching in JavaScript (full version)
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Scala
ScalaScala
Scala
 
Lec3
Lec3Lec3
Lec3
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
ScalaBlitz
ScalaBlitzScalaBlitz
ScalaBlitz
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 

More from Norman Richards

An Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScriptAn Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScript
Norman Richards
 
Logic programming a ruby perspective
Logic programming a ruby perspectiveLogic programming a ruby perspective
Logic programming a ruby perspective
Norman Richards
 
Lisp 1.5 - Running history
Lisp 1.5 - Running historyLisp 1.5 - Running history
Lisp 1.5 - Running history
Norman Richards
 
Deconstructing the Functional Web with Clojure
Deconstructing the Functional Web with ClojureDeconstructing the Functional Web with Clojure
Deconstructing the Functional Web with Clojure
Norman Richards
 
Immutant
ImmutantImmutant
Immutant
Norman Richards
 
Vert.X mini-talk
Vert.X mini-talkVert.X mini-talk
Vert.X mini-talk
Norman Richards
 
The Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScriptThe Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScript
Norman Richards
 

More from Norman Richards (7)

An Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScriptAn Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScript
 
Logic programming a ruby perspective
Logic programming a ruby perspectiveLogic programming a ruby perspective
Logic programming a ruby perspective
 
Lisp 1.5 - Running history
Lisp 1.5 - Running historyLisp 1.5 - Running history
Lisp 1.5 - Running history
 
Deconstructing the Functional Web with Clojure
Deconstructing the Functional Web with ClojureDeconstructing the Functional Web with Clojure
Deconstructing the Functional Web with Clojure
 
Immutant
ImmutantImmutant
Immutant
 
Vert.X mini-talk
Vert.X mini-talkVert.X mini-talk
Vert.X mini-talk
 
The Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScriptThe Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScript
 

Recently uploaded

みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 

Recently uploaded (20)

みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 

The Logical Burrito - pattern matching, term rewriting and unification

  • 1. The Logical Burrito Episode I: The Road to Unification an exploration of pattern matching, term rewriting and unification (wrapped up in a warm flour tortilla) Norman Richards orb@nostacktrace.com @MaximoBurrito
  • 2. Goals • Explore unification as groundwork for understanding logic programming in general and core.logic (mini-kanren based) in specific • Rather than focus on unification, this is a survey of some loosely related (in my mind) topics - pattern matching and and term rewriting • Will demonstrate these ideas in other languages: ML for pattern matching, Mathematica for term rewriting, and Prolog for unification
  • 3. Pattern Matching as a dispatch mechanism (or simple conditional) a way to express the selection criteria for which code to execute
  • 4. • In Clojure, dispatch is on arity: (defn foo ([x] …) ([x y] …)) • you can destructure but you can’t ask questions about the types or value • OO dispatch - based on type of first arg (.getArea shape) • Multiple dispatch - based on type of all args (method overriding is compile time) multimethods Dispatch
  • 5. Multimethods (defmulti fact identity) (defmethod fact 0 [_] 1) (defmethod fact :default [n] (* n (fact (dec n))))
  • 6. Multimethods (defmulti fizzbuzz identity) (defmethod fizzbuzz 1 [n] (str n)) (defmethod fizzbuzz 2 [n] (str n)) (defmethod fizzbuzz 3 [n] "fizz") ;; ... this isn't going to work
  • 7. Multimethods ;; we can make arbitrary dispatch rules (defmulti fizzbuzz (fn [n] [(zero? (mod n 3)) (zero? (mod n 5))]))   ;; as long as we can match the defaults (defmethod fizzbuzz [true true] [n] "fizzbuzz") (defmethod fizzbuzz [true false] [n] "fizz") (defmethod fizzbuzz [false true] [n] "buzz") (defmethod fizzbuzz [false false] [n] (str n))
  • 8. Multimethods (defmulti beats vector) ;; or if we have a single default value (defmethod beats :default [m1 m2] false) (defmethod beats [:paper :rock] [m1 m2] true) (defmethod beats [:rock :scissors] [m1 m2] true) (defmethod beats [:scissors :paper] [m1 m2] true)
  • 9. Multimethods • Multimethods are great for what they do, but they aren't a replacement for pattern maching • Clojure doesn't have great pattern matching, so we'll consider ML
  • 10. Pattern Matching (SML) (* SML matching at the function level*) fun fac 0 = 1 | fac n = n * fac (n - 1);
  • 11. Pattern Matching (SML) val div_3 = div_by 3; val div_5 = div_by 5;   (* No guard statements, but we can match in the body of the function on derived values *) fun fizzbuzz n = case (div_3 n, div_5 n) of (true, true) => "FizzBuzz" | (true, false) => "Fizz" | (false,true) => "Buzz" | _ => Int.toString n (* underscore matches anything *)
  • 12. Pattern Matching (SML) datatype suit = Clubs | Diamonds | Hearts | Spades datatype rank = Jack | Queen | King | Ace | Num of int type card = suit * rank datatype color = Red | Black   (* non-default don't cares exceed what multimethods can do *) fun card_color (Diamonds, _) = Red | card_color (Hearts , _) = Red | card_color _ = Black   (* note deep destructuring *) fun card_value (_, Num(n)) = n | card_value (_, Ace) = 11 | card_value _ = 10
  • 13. core.match (defn card_color [[suit value]] (match [suit value] [:diamonds _] :red [:hearts _] :red [_ _] :black))    (defn card_value [card] (match card [_ (n :guard number?)] n [_ :ace] 11 :else 10))  
  • 14. Limitations of core.match • No defnm to use at language level • Syntax can be ugly • Can't match non-linear patterns: [n n]
  • 15. (define eval-exp (lambda (expr env) (pmatch expr [,x (guard (symbol? x)) ;; variable (lookup x env)] [(quote ,datum) datum] [(list . ,expr*) ;; (map (lambda (expr) (eval-exp expr env)) expr*) (eval-exp* expr* env)] [(lambda (,x) ,body) ;; abstraction `(closure ,x ,body ,env)] [(,e1 ,e2) ;; application (let ((proc (eval-exp e1 env)) (val (eval-exp e2 env))) (pmatch proc [(closure ,x ,body ,envˆ) ;; evaluate body in an extended environment (eval-exp body `((,x . ,val) . ,envˆ))] [,else (error 'eval-exp "e1 does not evaluate to a procedure")]))]))) A scheme example (pmatch)
  • 16. Term rewriting Term rewriting is a branch of theoretical computer science which combines elements of logic, universal algebra, automated theorem proving and functional programming.  [...] This constitutes a Turing-complete computational model which is very close to functional programming. - Term Rewriting and All That
  • 17. Mathematica The innermost kernel of the Mathematica language is essentially nothing else than a higher-order, conditional rewrite language, efficiently and professionally implemented. - Mathematica as a Rewrite Language (Bruno Buchberger)
  • 18. Rewriting in Mathematica In[1]:= sum[m_, 0] := m; sum[m_, s[n_]] := s[sum[m, n]]; In[3]:= sum[s[s[0]], s[s[s[s[0]]]]]  Out[3]= s[s[s[s[s[s[0]]]]]]
  • 19. Rewriting in Mathematica In[4]:= map[f_, {}] := {}; map[f_, {x_, xs___}] := Prepend[map[f, {xs}], f[x]];   In[6]:= map[something, {1, 2, 3, 4, 5}]   Out[6]= {something[1], something[2], something[3], something[4], something[5]}
  • 20. Rewriting in Mathematica In[7]:= something[n_] := dont_care /; OddQ[n]   In[8]:= map[something, {1, 2, 3, 4, 5}]   Out[8]= {dont_care, something[2], dont_care, something[4], dont_care}
  • 21. Rewriting in Clojure (termito) (defrules s-rule [(+ ?x 0) ?x] [(+ ?x (s ?y)) (s (+ ?x ?y))]) (simplify '(+ (s (s 0)) (s (s (s (s 0))))) s-rule) ;; (s (s (s (s (s (s 0))))))
  • 22. Rewriting in Clojure (termito) (defnc numbers [x] (number? x)) (defnc commutative? [op] (or (= op '*) (= op '+))) (defnc associative? [op] (or (= op '*) (= op '+))) (defrules zero-rules [(* 0 ?x) 0]) (defrules identity-rules [(* 1 ?x) ?x] [(+ 0 ?x) ?x]) (defrules associative-rules [(?op ?x (?op ?y ?z)) :when [?op ~associative? #{?x ?y} ~numberc] (?op (?op ?x ?y) ?z)])
  • 23. Unification Unification is a basic operation on terms.  Two terms unify if substitutions can be made for any variables in the terms so that the terms are made identical.  If no such substitution exists, then the terms do not unify. - Clause and Effect
  • 24. Unification with Prolog ?- 42 = 42. true. ?- six_times_nine = 42. false. ?- enough = enough. true. Constants unify with themselves
  • 25. Unification with Prolog ?- City = austin. City = austin. ?- 7 = Number. Number = 7. uninstantiated logic variables unify with constants.
  • 26. Unification with Prolog ?- X = Y. X = Y. ?- A=B, C=D, C=B. A = B, B = C, C = D. uninstantiated variables unify with each other and co-refer
  • 27. Unification with Prolog ?- A=B, C=D, C=B, A=pi. A = B, B = C, C = D, D = pi. ?- A=B, C=D, C=B, A=pi, D=tau. false. uninstantiated variables unify with each other and co-refer
  • 28. Unification with Prolog ?- foo(1) = x. false. ?- foo(1) = foo(2). false. ?- foo(X) = bar(X). false. ?- foo(X) = foo(Y). X = Y. complex terms unify if they have the same head and all their parts unify
  • 29. Unification with Prolog ?- foo(X,Y)=foo(2,3). X = 2, Y = 3. ?- foo(X,5) = foo(7,Y). X = 7, Y = 5. ?- foo(X,5) = foo(9,X). false.
  • 30. Unification with Prolog ?- foo(A,B,B) = foo(B, B, A). A = B. ?- foo(bar(Z)) = foo(X), X= bar(baz). Z = baz, X = bar(baz). ?- f(X, a(b,c)) = f(d, a(Z,c)). X = d, Z = b.
  • 31. Unification with Clojure • core.logic unifier has more features, more actively maintained • core.unify has the prettier API, less baggage
  • 32. core.unify vs core.logic • core.logic • (unify [1 '?x])  => 1 • (unifier [1 '?x]) ==> {?x 1} • core.unify • (unify 1 '?x) ==> {?x 1} • (unifier 1 '?x) ==> 1
  • 33. core.logic unification (unifier '[dog dog]) ;; {} (unifier '[dog cat]) ;; nil (unifier '[?animal dog]) ;; {?animal dog}   (unifier '[?foo ?bar]) ;; {?foo ?bar}
  • 34. core.logic unification (unifier '[{:name bob :age 42} {:name ?X :age ?Y}]) ;; {?X bob, ?Y 42}   (unifier '[{:name bob :age 42} {:name ?X}]) ;; nil
  • 35. core.logic unification (unifier {:when {'?num numberc}} '[[?num ?animal] [:magical :unicorn]]) ;; nil   (unifier {:when {'?num numberc}} '[[?num ?animal] [42 :unicorn]]) ;; {?animal :unicorn, ?num 42}
  • 36. core.logic unification (unifier {:as '{?a 3}} '[?a ?b]) ;; {?b 3, ?a 3}   (unifier {:as '{?a 3 ?b 4}} '[?a ?b]) ;; nil (unifier {:as (unifier '[?a ?b])} '[(?c 5) (?a ?b)]) ;; {?b 5, ?a 5, ?c 5}
  • 37. (defn unifier+ [& pairs] (loop [u {} pairs pairs] (if (seq pairs) (when-let [u' (unifier {:as u} (first pairs))] (recur (merge u u') (rest pairs))) u))) (unifier+ '[?x 1] '[?y 2] '[?z [?x ?y]]) ;; {?z [1 2], ?y 2, ?x 1} core.logic unification
  • 38. (defn q [data query] (filter #(unifier+ (conj query %)) data)) (def things [{:name "thing 1" :color :red} {:name "thing 2" :color :blue} {:name "thing 3" :color :red}])   (q things '[{:name ?name :color :red}]) ;; ({:color :red, :name "thing 1"} {:color :red, :name "thing 3"}) core.logic unification
  • 39. Why? • Logic programming • Machine Learning / Natural language processing • Type inferencing / type checking • Theorem proving / Equation solving
  • 40.
  • 41. Until the next logical burrito, (run* [your-location] (conde [(== your-location :home)] [succeed]) (!= your-location :here)) Norman Richards orb@nostacktrace.com @MaximoBurrito