SlideShare uma empresa Scribd logo
1 de 46
Relational Logic Programming for Clojure
CORE.LOGIC
http://www.slideshare.net/normanrichards/corelogic-introduction
git clone https://github.com/orb/logicdemo.git
Why core.logic?
http://xkcd.com/292/
core.logic in the real world
ThreatGRID uses core.logic to
process observations of malware
execution looking for behavioral
indicators of compromise.
Observations are simple factual statements that map into
core.logic relations and SQL tables. The mapping allows
the same data model to be used for core.logic programs in
memory, as well as persistance in a database and later
analysis and querying via SQL.
A persistent core.logic database. pldb provides an in-
memory persistent fact database making it easier to use
core.logic in multi-threaded environments like web
applications.
github.com/threatgrid/observations
github.com/threatgrid/pldb
run*
core.logic
(run* [q])
This is the simplest possible
core.logic program. q is the
query. run* says “give me all the
results”.
run* returns a seq of all possible
results. The symbol _0
represents a fresh (unbound)
logic variable.
(_0)
unification
core.logic
(run* [q]
(== q :hello-world))
The most fundamental operation
on a logic variable is to unify it.
unification is ==.
There is only one value of q that
satisfies the relation. (:hello-world)
unification
core.logic
(run* [q]
(== q [:hello :world]))
Logic variables can also be
unified over sequences.
There is still only one value of q
that satisfies the relation.
([:hello :world])
unification
core.logic
(run* [q]
(== q [:hello :world])
(== q [:hello :world]))
A logic variable can be unified
with the same value multiple
times.
([:hello :world])
unification
core.logic
(run* [q]
(== q :hello)
(== q :world))
A logic variable cannot unify with
two different values at the same
time.
There are no values of q that
satisfy the relation. ()
conde
core.logic
(run* [q]
(conde
[(== q :hello)]
[(== q :world)]))
You can introduce alternative
values with conde. Every conde
line that succeeds produces
possible alternative values.
There are 2 values of q that
satisfy the relation. (:hello :world)
Disunification
core.logic
(run* [q]
(conde
[(== q :hello)]
[(== q :world)])
(!= q :hello))
!= introduces a constraint that
two values never unify.
There are 2 values of q that
satisfy the conde goal, but !=
eliminates one of them. (:world)
FRESH
core.logic
(run* [q]
(fresh [x y]
(== x :something)
(== y :something-else)))
fresh introduces new logic
variables.
x and y are bound, but the query
remains unbound.
(_0)
FRESH
core.logic
(run* [q]
(fresh [x y]
(== x :something)
(== x :something-else)))
The query fails since no value of
q can make x unify with two
different values.
()
FRESH
core.logic
(run* [q]
(fresh [x y]
(== q [x :and y])
(== x :something)
(== :something-else y)))
Order does not matter for
unification.
([:something :and :something-else])
membero
core.logiccore.logic
(run* [q]
(fresh [smurf]
(membero smurf
[:papa :brainy :lazy :handy])
(== q [smurf smurf])))
membero is relation that
succeeds when the first
argument is a member of the
second argument. It can
succeed multiple times.
q produces each success ([:papa :papa]
[:brainy :brainy]
[:lazy :lazy]
[:handy :handy])
membero
core.logiccore.logic
(run* [q]
(fresh [smurf1 smurf2]
(membero smurf1
[:papa :brainy :lazy :handy])
(membero smurf2
[:papa :brainy :lazy :handy])
(== q [smurf1 smurf2])))
Both membero relations succeed
multiple times. q is unified with
each pair.
([:papa :papa]
[:papa :brainy]
[:brainy :papa]
...
[:handy :lazy]
[:handy :handy])
distincto
core.logiccore.logic
(run* [q]
(fresh [smurf1 smurf2 smurf3]
(membero smurf1
[:papa :brainy :lazy :handy])
(membero smurf2
[:papa :brainy :lazy :handy])
(membero smurf3
[:papa :brainy :lazy :handy])
(distincto [smurf1 smurf2 smurf3])
(== q [smurf1 smurf2 smurf3])))
distincto ensures that no two
items in the relation unify with
each other. smurf1 will never
unify with smurf2, and neither will
unify with smurf3.
([:papa :brainy :lazy]
[:papa :brainy :handy]
[:brainy :papa :lazy]
[:brainy :papa :handy]
...
[:handy :lazy :brainy])
everyg
core.logiccore.logic
(run* [q]
(fresh [smurf1 smurf2 smurf3]
(== q [smurf1 smurf2 smurf3])
(everyg #(membero % [:papa :brainy :lazy :handy])
q)
(distincto q)))
everyg ensures that every
element in a collection satisfies a
goal. It is not a proper relation, in
that it requires the collection to
already be a seq.
([:papa :brainy :lazy]
[:papa :brainy :handy]
[:brainy :papa :lazy]
[:brainy :papa :handy]
...
[:handy :lazy :brainy])
lvar
core.logiccore.logic
(run* [q]
(== q [(lvar) (lvar) (lvar)])
(everyg #(membero % [:papa :brainy :lazy :handy])
q)
(distincto q))
lvar creates a new a logic
variable. Since we don’t need to
refer to the items individually, we
can just say that the
([:papa :brainy :lazy]
[:papa :brainy :handy]
[:brainy :papa :lazy]
[:brainy :papa :handy]
...
[:handy :lazy :brainy])
Map coloring
core.logiccore.logic
http://pragprog.com/book/btlang/seven-languages-in-seven-weeks
(run 1 [q]
(fresh [tn ms al ga fl]
(everyg #(membero % [:red :blue :green])
[tn ms al ga fl])
(!= ms tn) (!= ms al) (!= al tn)
(!= al ga) (!= al fl) (!= ga fl) (!= ga tn)
 
(== q {:tennesse tn
:mississipi ms
:alabama al
:georgia ga
:florida fl})))
({:tennesse :blue,
:mississipi :red,
:alabama :green,
:georgia :red,
:florida :blue})
rock paper scissors
core.logiccore.logic
(defn beatso [player1 player2]
(conde
[(== player1 :rock) (== player2 :scissors)]
[(== player1 :scissors) (== player2 :paper)]
[(== player1 :paper) (== player2 :rock)]))
beatso is a custom relation
between two terms. It succeeds
when the first players move
beats the second players move
rock paper scissors
core.logiccore.logic
(run* [q]
(beatso :rock :paper))
beatso fails because :rock does
not beat paper. No value of q
makes this succeed.
()
rock paper scissors
core.logiccore.logic
(run* [q]
(beatso :paper :rock))
beatso succeeds because :paper
beats rock. q remains fresh
because no questions were
asked of it.
(_0)
rock paper scissors
core.logiccore.logic
(run* [q]
(beatso :rock q))
beatso can answer in either
direction.
(:scissors)
(run* [q]
(beatso q :scissors))
(:rock)
rock paper scissors
core.logiccore.logic
(run* [q]
(fresh [x y]
(beatso x y)
(== q [x y])))
This query asks for all the pairs
where x beats y.
([:rock :scissors]
[:scissors :paper]
[:paper :rock])
FACTS and RELATIONS
core.logiccore.logic
rpsls is a relation of one term.
Five facts are asserted about the
relation.
(defrel rpsls gesture)
(fact rpsls :rock)
(fact rpsls :paper)
(fact rpsls :scissors)
(fact rpsls :lizard)
(fact rpsls :spock)
FACTS and RELATIONS
core.logiccore.logic
(run* [q]
(rpsls q))
defrel relations answer queries in
the same way as the other
relations we’ve seen.
(:rock :paper :scissors :lizard :spock)
FACTS and RELATIONS
core.logiccore.logic
beats is a relation of two terms,
indicating the first gesture beats
the second one.
(defrel beats gesture1 gesture2)
(fact beats :scissors :paper)
(fact beats :paper :rock)
(fact beats :rock :lizard)
(fact beats :lizard :spock)
(fact beats :spock :scissors)
(fact beats :scissors :lizard)
(fact beats :lizard :paper)
(fact beats :paper :spock)
(fact beats :spock :rock)
(fact beats :rock :scissors)
FACTS and RELATIONS
core.logiccore.logic
(run* [q]
(fresh [x y]
(beats :spock x)
(beats x y)
(beats y :spock)
(== q [:spock x y :spock])))
We can ask questions like: give
me a 4-chain of dominated
moves starting and ending
with :spock. There are three
solutions.
([:spock :scissors :lizard :spock]
[:spock :scissors :paper :spock]
[:spock :rock :lizard :spock])
FACTS and RELATIONS
core.logiccore.logic
(defn win-chaino [x]
(fresh [a d]
(rpsls a)
(conso a d x)
(conde
[(emptyo d)]
[(fresh [b]
(beats a b)
(firsto d b))
(win-chaino d)])))
A winning chain is a single rpsls
move either by itself or followed
by a winning chain whose first
move is beaten by the original
move.
conso, emptyo and firsto are
relations over cons lists.
FACTS and RELATIONS
core.logiccore.logic
(count
(run* [q]
(== q (concat [:spock]
(repeatedly 10 lvar)
[:lizard]))
(win-chaino q)))
How many winning chains are
there from :spock to :lizard with
10 steps?
385
USEless logic puzzle
core.logiccore.logic
‣ petey pig did not hand out the popcorn
‣ pippin pig does not live in the wood house
‣ the pig that lives in the straw house handed out popcorn
‣ Petunia pig handed out apples
‣ The pig who handed out chocolate does not live in the brick house.
Three little pigs, who each
lived in a different type of
house, handed out treats for
Halloween. Use the clues to
figure out which pig lived in
each house, and what type of
treat each pig handed out.
http://holidays.hobbyloco.com/halloween/logic1.html
USEless logic puzzle
core.logiccore.logic
(defn pigso [q]
(fresh [h1 h2 h3 t1 t2 t3]
(== q [[:petey h1 t1]
[:pippin h2 t2]
[:petunia h3 t3]])
(permuteo [t1 t2 t3]
[:chocolate :popcorn :apple])
(permuteo [h1 h2 h3]
[:wood :straw :brick])
 ... ))
pigso starts by defining the
solution space.
permuteo succeeds when the
first list is permutation of the
second.
USEless logic puzzle
core.logiccore.logic
(fresh [notpopcorn _]
(membero notpopcorn [:chocolate :apple])
(membero [:petey _ notpopcorn] q))
(fresh [notwood _]
(membero notwood [:straw :brick])
(membero [:pippin notwood _] q))
(fresh [_]
(membero [_ :straw :popcorn] q))
(fresh [_]
(membero [:petunia _ :apple] q))
(fresh [notbrick _]
(membero notbrick [:straw :wood])
(membero [_ notbrick :chocolate] q))
The clues translate cleanly to
goals constraining the solution
space.
membero has a solution when
the first item is a member of the
second.
FACTS and RELATIONS
core.logiccore.logic
(run* [q]
(pigso q))
pigso finds the only solution.
([[:petey :wood :chocolate]
[:pippin :straw :popcorn]
[:petunia :brick :apple]])
FINITE DOMAINS
core.logiccore.logic
fd/interval declares a finite
integer interval and fd/in
contrains logic variables to a
domain.
(defn two-plus-two-is-four [q]
(fresh [t w o f u r TWO FOUR]
(fd/in t w o f u r (fd/interval 0 9))
(fd/distinct [t w o f u r])
(fd/in TWO (fd/interval 100 999))
(fd/in FOUR (fd/interval 1000 9999))
 
...
(== q [TWO TWO FOUR])))
T W O
+ T W O
-------
F O U R
http://www.amazon.com/Crypt-arithmetic-Puzzles-in-PROLOG-ebook/dp/B006X9LY8O
FINITE DOMAINS
core.logiccore.logic
fd/eq translates simple math to
constraints over finite domain
logic variables.
(fd/eq (= TWO
(+ (* 100 t)
(* 10 w)
o)))
(fd/eq (= FOUR
(+ (* 1000 f)
(* 100 o)
(* 10 u)
r)))
 
(fd/eq (= (+ TWO TWO) FOUR))
T W O
+ T W O
-------
F O U R
FINITE DOMAINS
core.logiccore.logic
There are 7 unique solutions to
the problem.
(run* [q]
(two-plus-two-is-four q))
T W O
+ T W O
-------
F O U R
([734 734 1468]
[765 765 1530]
[836 836 1672]
[846 846 1692]
[867 867 1734]
[928 928 1856]
[938 938 1876])
sudoku made easier
core.logiccore.logic
After setting up the logic
variables and initializing state,
the solution simply requires every
row, column and square on the
board to have distinct values.
(defn solve [puzzle]
(let [sd-num (fd/domain 1 2 3 4 5 6 7 8 9)
board (repeatedly 81 lvar)
rows (into [] (map vec (partition 9 board)))
cols (apply map vector rows)
squares (for [x (range 0 9 3)
y (range 0 9 3)]
(get-square rows x y))]
 
(run* [q]
(== q board)
(everyg #(fd/in % sd-num) board)
(init-board board puzzle)
(everyg fd/distinct rows)
(everyg fd/distinct cols)
(everyg fd/distinct squares))))
https://gist.github.com/swannodette/3217582
sudoku made easier
core.logiccore.logic
matche is pattern matching
syntax for conde. To unify the
initial logic variables with a
board, we require either the
board have a 0 or that the logic
variable unifies with the value of
the board.
(defn init-board [vars puzzle]
(matche [vars puzzle]
([[] []]
succeed)
([[_ . vs] [0 . ps]]
(init-board vs ps))
([[num . vs] [num . ps]]
(init-board vs ps))))
sudoku
core.logiccore.logic
(def puzzle1
[0 0 0 0 0 9 0 6 0
0 3 8 0 0 5 0 0 4
0 2 0 0 6 0 0 7 0
0 0 0 0 0 0 3 9 0
0 0 0 9 2 6 0 0 0
0 9 7 0 0 0 0 0 0
0 4 0 0 7 0 0 3 0
5 0 0 4 0 0 2 1 0
0 7 0 8 0 0 0 0 0])
(partition 9 (first (solve puzzle1)))
((7 1 4 2 8 9 5 6 3)
(6 3 8 7 1 5 9 2 4)
(9 2 5 3 6 4 1 7 8)
(8 6 1 5 4 7 3 9 2)
(4 5 3 9 2 6 7 8 1)
(2 9 7 1 3 8 4 5 6)
(1 4 9 6 7 2 8 3 5)
(5 8 6 4 9 3 2 1 7)
(3 7 2 8 5 1 6 4 9))
Norman richards
orb@nostacktrace.com
@maximoburrito
core.logic introduction
core.logic introduction
core.logic introduction
core.logic introduction
core.logic introduction

Mais conteúdo relacionado

Mais procurados

Oι μαθητές του Xριστού
Oι μαθητές του XριστούOι μαθητές του Xριστού
Oι μαθητές του Xριστούavramaki
 
Ευκτική όλων των χρόνων στα βαρύτονα ρήματα
Ευκτική όλων των χρόνων στα βαρύτονα ρήματαΕυκτική όλων των χρόνων στα βαρύτονα ρήματα
Ευκτική όλων των χρόνων στα βαρύτονα ρήματαGeorgia Dimitropoulou
 
13. Προσέγγιση της γιορτής της Μεταμόρφωσης
13. Προσέγγιση της γιορτής της Μεταμόρφωσης13. Προσέγγιση της γιορτής της Μεταμόρφωσης
13. Προσέγγιση της γιορτής της ΜεταμόρφωσηςΠΕ 01 ΜΠΑΛΤΟΣ ΙΩΑΝΝΗΣ
 
πίστη και επιστήμη B ΛΥΚΕΙΟΥ
πίστη και επιστήμη B ΛΥΚΕΙΟΥπίστη και επιστήμη B ΛΥΚΕΙΟΥ
πίστη και επιστήμη B ΛΥΚΕΙΟΥmariastou
 
Ειρεσιώνη
ΕιρεσιώνηΕιρεσιώνη
Ειρεσιώνηmavroedi
 
η αγία αικατερίνη
η αγία αικατερίνηη αγία αικατερίνη
η αγία αικατερίνηatavar
 
1.7α ΤΟ ΤΕΛΟΣ ΤΗΣ ΖΩΗΣ
1.7α ΤΟ ΤΕΛΟΣ ΤΗΣ ΖΩΗΣ1.7α ΤΟ ΤΕΛΟΣ ΤΗΣ ΖΩΗΣ
1.7α ΤΟ ΤΕΛΟΣ ΤΗΣ ΖΩΗΣΕλενη Ζαχου
 
Μαθαίνοντας τα Ηφαίστεια
Μαθαίνοντας τα ΗφαίστειαΜαθαίνοντας τα Ηφαίστεια
Μαθαίνοντας τα ΗφαίστειαChristos Gamvroudis
 
Rapsodia z 139-291 odyssia
Rapsodia z 139-291 odyssiaRapsodia z 139-291 odyssia
Rapsodia z 139-291 odyssiaaxrysan
 
Πλάτων - Πολιτεία (Γ΄Λυκείου) - Μεταφράσεις
Πλάτων - Πολιτεία (Γ΄Λυκείου) - ΜεταφράσειςΠλάτων - Πολιτεία (Γ΄Λυκείου) - Μεταφράσεις
Πλάτων - Πολιτεία (Γ΄Λυκείου) - ΜεταφράσειςOlga Paizi
 
Διαφορές και ομοιότητες καθολικής και ορθοδοξης βάπτισης
Διαφορές και ομοιότητες καθολικής και ορθοδοξης βάπτισηςΔιαφορές και ομοιότητες καθολικής και ορθοδοξης βάπτισης
Διαφορές και ομοιότητες καθολικής και ορθοδοξης βάπτισηςΘεοδώρα Φραγκάκη
 
γνωμικά παροιμίες-αινίγματα για τον ήλιο
γνωμικά  παροιμίες-αινίγματα για τον ήλιογνωμικά  παροιμίες-αινίγματα για τον ήλιο
γνωμικά παροιμίες-αινίγματα για τον ήλιοelenadamo
 
Θεία Ευχαριστία - Divine Liturgy
Θεία Ευχαριστία - Divine LiturgyΘεία Ευχαριστία - Divine Liturgy
Θεία Ευχαριστία - Divine LiturgyAgios Nikolaos Siatistas
 
δ.ε.10 Κατακόμβες
 δ.ε.10 Κατακόμβες δ.ε.10 Κατακόμβες
δ.ε.10 Κατακόμβεςdesphan
 

Mais procurados (19)

Oι μαθητές του Xριστού
Oι μαθητές του XριστούOι μαθητές του Xριστού
Oι μαθητές του Xριστού
 
Ευκτική όλων των χρόνων στα βαρύτονα ρήματα
Ευκτική όλων των χρόνων στα βαρύτονα ρήματαΕυκτική όλων των χρόνων στα βαρύτονα ρήματα
Ευκτική όλων των χρόνων στα βαρύτονα ρήματα
 
O akathistos ymnos
O akathistos ymnosO akathistos ymnos
O akathistos ymnos
 
ΤΑ ΦΑΝΤΑΣΜΑΤΑ (ΜΑΡΙΑ ΙΟΡΔΑΝΙΔΟΥ)
ΤΑ ΦΑΝΤΑΣΜΑΤΑ (ΜΑΡΙΑ ΙΟΡΔΑΝΙΔΟΥ)ΤΑ ΦΑΝΤΑΣΜΑΤΑ (ΜΑΡΙΑ ΙΟΡΔΑΝΙΔΟΥ)
ΤΑ ΦΑΝΤΑΣΜΑΤΑ (ΜΑΡΙΑ ΙΟΡΔΑΝΙΔΟΥ)
 
13. Προσέγγιση της γιορτής της Μεταμόρφωσης
13. Προσέγγιση της γιορτής της Μεταμόρφωσης13. Προσέγγιση της γιορτής της Μεταμόρφωσης
13. Προσέγγιση της γιορτής της Μεταμόρφωσης
 
πίστη και επιστήμη B ΛΥΚΕΙΟΥ
πίστη και επιστήμη B ΛΥΚΕΙΟΥπίστη και επιστήμη B ΛΥΚΕΙΟΥ
πίστη και επιστήμη B ΛΥΚΕΙΟΥ
 
Ειρεσιώνη
ΕιρεσιώνηΕιρεσιώνη
Ειρεσιώνη
 
Σύγχρονες παιδαγωγικές αντιλήψεις (Project)
Σύγχρονες παιδαγωγικές αντιλήψεις (Project)Σύγχρονες παιδαγωγικές αντιλήψεις (Project)
Σύγχρονες παιδαγωγικές αντιλήψεις (Project)
 
Βυζαντινή Ναοδομία
Βυζαντινή ΝαοδομίαΒυζαντινή Ναοδομία
Βυζαντινή Ναοδομία
 
η αγία αικατερίνη
η αγία αικατερίνηη αγία αικατερίνη
η αγία αικατερίνη
 
1.7α ΤΟ ΤΕΛΟΣ ΤΗΣ ΖΩΗΣ
1.7α ΤΟ ΤΕΛΟΣ ΤΗΣ ΖΩΗΣ1.7α ΤΟ ΤΕΛΟΣ ΤΗΣ ΖΩΗΣ
1.7α ΤΟ ΤΕΛΟΣ ΤΗΣ ΖΩΗΣ
 
Μαθαίνοντας τα Ηφαίστεια
Μαθαίνοντας τα ΗφαίστειαΜαθαίνοντας τα Ηφαίστεια
Μαθαίνοντας τα Ηφαίστεια
 
Rapsodia z 139-291 odyssia
Rapsodia z 139-291 odyssiaRapsodia z 139-291 odyssia
Rapsodia z 139-291 odyssia
 
Πάσχα
ΠάσχαΠάσχα
Πάσχα
 
Πλάτων - Πολιτεία (Γ΄Λυκείου) - Μεταφράσεις
Πλάτων - Πολιτεία (Γ΄Λυκείου) - ΜεταφράσειςΠλάτων - Πολιτεία (Γ΄Λυκείου) - Μεταφράσεις
Πλάτων - Πολιτεία (Γ΄Λυκείου) - Μεταφράσεις
 
Διαφορές και ομοιότητες καθολικής και ορθοδοξης βάπτισης
Διαφορές και ομοιότητες καθολικής και ορθοδοξης βάπτισηςΔιαφορές και ομοιότητες καθολικής και ορθοδοξης βάπτισης
Διαφορές και ομοιότητες καθολικής και ορθοδοξης βάπτισης
 
γνωμικά παροιμίες-αινίγματα για τον ήλιο
γνωμικά  παροιμίες-αινίγματα για τον ήλιογνωμικά  παροιμίες-αινίγματα για τον ήλιο
γνωμικά παροιμίες-αινίγματα για τον ήλιο
 
Θεία Ευχαριστία - Divine Liturgy
Θεία Ευχαριστία - Divine LiturgyΘεία Ευχαριστία - Divine Liturgy
Θεία Ευχαριστία - Divine Liturgy
 
δ.ε.10 Κατακόμβες
 δ.ε.10 Κατακόμβες δ.ε.10 Κατακόμβες
δ.ε.10 Κατακόμβες
 

Semelhante a core.logic introduction

Logic programming a ruby perspective
Logic programming a ruby perspectiveLogic programming a ruby perspective
Logic programming a ruby perspectiveNorman Richards
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойSigma Software
 
Functional programming with clojure
Functional programming with clojureFunctional programming with clojure
Functional programming with clojureLucy Fang
 
Learn basics of Clojure/script and Reagent
Learn basics of Clojure/script and ReagentLearn basics of Clojure/script and Reagent
Learn basics of Clojure/script and ReagentMaty Fedak
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)jaxLondonConference
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - StockholmJan Kronquist
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryDatabricks
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryDatabricks
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data ManagementAlbert Bifet
 
Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Davide Cerbo
 
Functional Concepts for OOP Developers
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developersbrweber2
 
A gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojureA gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojurePaul Lam
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Introthnetos
 
Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)tarcieri
 
Spark with Elasticsearch - umd version 2014
Spark with Elasticsearch - umd version 2014Spark with Elasticsearch - umd version 2014
Spark with Elasticsearch - umd version 2014Holden Karau
 

Semelhante a core.logic introduction (20)

Logic programming a ruby perspective
Logic programming a ruby perspectiveLogic programming a ruby perspective
Logic programming a ruby perspective
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
 
Hw09 Hadoop + Clojure
Hw09   Hadoop + ClojureHw09   Hadoop + Clojure
Hw09 Hadoop + Clojure
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Functional programming with clojure
Functional programming with clojureFunctional programming with clojure
Functional programming with clojure
 
Learn basics of Clojure/script and Reagent
Learn basics of Clojure/script and ReagentLearn basics of Clojure/script and Reagent
Learn basics of Clojure/script and Reagent
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)The Curious Clojurist - Neal Ford (Thoughtworks)
The Curious Clojurist - Neal Ford (Thoughtworks)
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
 
Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)
 
Functional Concepts for OOP Developers
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developers
 
A gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojureA gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojure
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)
 
TeraSort
TeraSortTeraSort
TeraSort
 
Spark with Elasticsearch - umd version 2014
Spark with Elasticsearch - umd version 2014Spark with Elasticsearch - umd version 2014
Spark with Elasticsearch - umd version 2014
 

Mais de Norman Richards

An Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScriptAn Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScriptNorman Richards
 
Lisp 1.5 - Running history
Lisp 1.5 - Running historyLisp 1.5 - Running history
Lisp 1.5 - Running historyNorman Richards
 
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 unificationNorman Richards
 
Deconstructing the Functional Web with Clojure
Deconstructing the Functional Web with ClojureDeconstructing the Functional Web with Clojure
Deconstructing the Functional Web with ClojureNorman Richards
 
The Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScriptThe Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScriptNorman Richards
 

Mais de Norman Richards (7)

An Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScriptAn Adventure in Serverless ClojureScript
An Adventure in Serverless ClojureScript
 
Lisp 1.5 - Running history
Lisp 1.5 - Running historyLisp 1.5 - Running history
Lisp 1.5 - Running history
 
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
 
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
 

Último

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 2024The Digital Insurer
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
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 Processorsdebabhi2
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
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 FresherRemote DBA Services
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
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, ...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 

Último (20)

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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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, ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

core.logic introduction

  • 1. Relational Logic Programming for Clojure CORE.LOGIC http://www.slideshare.net/normanrichards/corelogic-introduction git clone https://github.com/orb/logicdemo.git
  • 3. core.logic in the real world ThreatGRID uses core.logic to process observations of malware execution looking for behavioral indicators of compromise. Observations are simple factual statements that map into core.logic relations and SQL tables. The mapping allows the same data model to be used for core.logic programs in memory, as well as persistance in a database and later analysis and querying via SQL. A persistent core.logic database. pldb provides an in- memory persistent fact database making it easier to use core.logic in multi-threaded environments like web applications. github.com/threatgrid/observations github.com/threatgrid/pldb
  • 4. run* core.logic (run* [q]) This is the simplest possible core.logic program. q is the query. run* says “give me all the results”. run* returns a seq of all possible results. The symbol _0 represents a fresh (unbound) logic variable. (_0)
  • 5. unification core.logic (run* [q] (== q :hello-world)) The most fundamental operation on a logic variable is to unify it. unification is ==. There is only one value of q that satisfies the relation. (:hello-world)
  • 6. unification core.logic (run* [q] (== q [:hello :world])) Logic variables can also be unified over sequences. There is still only one value of q that satisfies the relation. ([:hello :world])
  • 7. unification core.logic (run* [q] (== q [:hello :world]) (== q [:hello :world])) A logic variable can be unified with the same value multiple times. ([:hello :world])
  • 8. unification core.logic (run* [q] (== q :hello) (== q :world)) A logic variable cannot unify with two different values at the same time. There are no values of q that satisfy the relation. ()
  • 9. conde core.logic (run* [q] (conde [(== q :hello)] [(== q :world)])) You can introduce alternative values with conde. Every conde line that succeeds produces possible alternative values. There are 2 values of q that satisfy the relation. (:hello :world)
  • 10. Disunification core.logic (run* [q] (conde [(== q :hello)] [(== q :world)]) (!= q :hello)) != introduces a constraint that two values never unify. There are 2 values of q that satisfy the conde goal, but != eliminates one of them. (:world)
  • 11. FRESH core.logic (run* [q] (fresh [x y] (== x :something) (== y :something-else))) fresh introduces new logic variables. x and y are bound, but the query remains unbound. (_0)
  • 12. FRESH core.logic (run* [q] (fresh [x y] (== x :something) (== x :something-else))) The query fails since no value of q can make x unify with two different values. ()
  • 13. FRESH core.logic (run* [q] (fresh [x y] (== q [x :and y]) (== x :something) (== :something-else y))) Order does not matter for unification. ([:something :and :something-else])
  • 14. membero core.logiccore.logic (run* [q] (fresh [smurf] (membero smurf [:papa :brainy :lazy :handy]) (== q [smurf smurf]))) membero is relation that succeeds when the first argument is a member of the second argument. It can succeed multiple times. q produces each success ([:papa :papa] [:brainy :brainy] [:lazy :lazy] [:handy :handy])
  • 15. membero core.logiccore.logic (run* [q] (fresh [smurf1 smurf2] (membero smurf1 [:papa :brainy :lazy :handy]) (membero smurf2 [:papa :brainy :lazy :handy]) (== q [smurf1 smurf2]))) Both membero relations succeed multiple times. q is unified with each pair. ([:papa :papa] [:papa :brainy] [:brainy :papa] ... [:handy :lazy] [:handy :handy])
  • 16. distincto core.logiccore.logic (run* [q] (fresh [smurf1 smurf2 smurf3] (membero smurf1 [:papa :brainy :lazy :handy]) (membero smurf2 [:papa :brainy :lazy :handy]) (membero smurf3 [:papa :brainy :lazy :handy]) (distincto [smurf1 smurf2 smurf3]) (== q [smurf1 smurf2 smurf3]))) distincto ensures that no two items in the relation unify with each other. smurf1 will never unify with smurf2, and neither will unify with smurf3. ([:papa :brainy :lazy] [:papa :brainy :handy] [:brainy :papa :lazy] [:brainy :papa :handy] ... [:handy :lazy :brainy])
  • 17. everyg core.logiccore.logic (run* [q] (fresh [smurf1 smurf2 smurf3] (== q [smurf1 smurf2 smurf3]) (everyg #(membero % [:papa :brainy :lazy :handy]) q) (distincto q))) everyg ensures that every element in a collection satisfies a goal. It is not a proper relation, in that it requires the collection to already be a seq. ([:papa :brainy :lazy] [:papa :brainy :handy] [:brainy :papa :lazy] [:brainy :papa :handy] ... [:handy :lazy :brainy])
  • 18. lvar core.logiccore.logic (run* [q] (== q [(lvar) (lvar) (lvar)]) (everyg #(membero % [:papa :brainy :lazy :handy]) q) (distincto q)) lvar creates a new a logic variable. Since we don’t need to refer to the items individually, we can just say that the ([:papa :brainy :lazy] [:papa :brainy :handy] [:brainy :papa :lazy] [:brainy :papa :handy] ... [:handy :lazy :brainy])
  • 19. Map coloring core.logiccore.logic http://pragprog.com/book/btlang/seven-languages-in-seven-weeks (run 1 [q] (fresh [tn ms al ga fl] (everyg #(membero % [:red :blue :green]) [tn ms al ga fl]) (!= ms tn) (!= ms al) (!= al tn) (!= al ga) (!= al fl) (!= ga fl) (!= ga tn)   (== q {:tennesse tn :mississipi ms :alabama al :georgia ga :florida fl}))) ({:tennesse :blue, :mississipi :red, :alabama :green, :georgia :red, :florida :blue})
  • 20. rock paper scissors core.logiccore.logic (defn beatso [player1 player2] (conde [(== player1 :rock) (== player2 :scissors)] [(== player1 :scissors) (== player2 :paper)] [(== player1 :paper) (== player2 :rock)])) beatso is a custom relation between two terms. It succeeds when the first players move beats the second players move
  • 21. rock paper scissors core.logiccore.logic (run* [q] (beatso :rock :paper)) beatso fails because :rock does not beat paper. No value of q makes this succeed. ()
  • 22. rock paper scissors core.logiccore.logic (run* [q] (beatso :paper :rock)) beatso succeeds because :paper beats rock. q remains fresh because no questions were asked of it. (_0)
  • 23. rock paper scissors core.logiccore.logic (run* [q] (beatso :rock q)) beatso can answer in either direction. (:scissors) (run* [q] (beatso q :scissors)) (:rock)
  • 24. rock paper scissors core.logiccore.logic (run* [q] (fresh [x y] (beatso x y) (== q [x y]))) This query asks for all the pairs where x beats y. ([:rock :scissors] [:scissors :paper] [:paper :rock])
  • 25. FACTS and RELATIONS core.logiccore.logic rpsls is a relation of one term. Five facts are asserted about the relation. (defrel rpsls gesture) (fact rpsls :rock) (fact rpsls :paper) (fact rpsls :scissors) (fact rpsls :lizard) (fact rpsls :spock)
  • 26. FACTS and RELATIONS core.logiccore.logic (run* [q] (rpsls q)) defrel relations answer queries in the same way as the other relations we’ve seen. (:rock :paper :scissors :lizard :spock)
  • 27. FACTS and RELATIONS core.logiccore.logic beats is a relation of two terms, indicating the first gesture beats the second one. (defrel beats gesture1 gesture2) (fact beats :scissors :paper) (fact beats :paper :rock) (fact beats :rock :lizard) (fact beats :lizard :spock) (fact beats :spock :scissors) (fact beats :scissors :lizard) (fact beats :lizard :paper) (fact beats :paper :spock) (fact beats :spock :rock) (fact beats :rock :scissors)
  • 28. FACTS and RELATIONS core.logiccore.logic (run* [q] (fresh [x y] (beats :spock x) (beats x y) (beats y :spock) (== q [:spock x y :spock]))) We can ask questions like: give me a 4-chain of dominated moves starting and ending with :spock. There are three solutions. ([:spock :scissors :lizard :spock] [:spock :scissors :paper :spock] [:spock :rock :lizard :spock])
  • 29. FACTS and RELATIONS core.logiccore.logic (defn win-chaino [x] (fresh [a d] (rpsls a) (conso a d x) (conde [(emptyo d)] [(fresh [b] (beats a b) (firsto d b)) (win-chaino d)]))) A winning chain is a single rpsls move either by itself or followed by a winning chain whose first move is beaten by the original move. conso, emptyo and firsto are relations over cons lists.
  • 30. FACTS and RELATIONS core.logiccore.logic (count (run* [q] (== q (concat [:spock] (repeatedly 10 lvar) [:lizard])) (win-chaino q))) How many winning chains are there from :spock to :lizard with 10 steps? 385
  • 31. USEless logic puzzle core.logiccore.logic ‣ petey pig did not hand out the popcorn ‣ pippin pig does not live in the wood house ‣ the pig that lives in the straw house handed out popcorn ‣ Petunia pig handed out apples ‣ The pig who handed out chocolate does not live in the brick house. Three little pigs, who each lived in a different type of house, handed out treats for Halloween. Use the clues to figure out which pig lived in each house, and what type of treat each pig handed out. http://holidays.hobbyloco.com/halloween/logic1.html
  • 32. USEless logic puzzle core.logiccore.logic (defn pigso [q] (fresh [h1 h2 h3 t1 t2 t3] (== q [[:petey h1 t1] [:pippin h2 t2] [:petunia h3 t3]]) (permuteo [t1 t2 t3] [:chocolate :popcorn :apple]) (permuteo [h1 h2 h3] [:wood :straw :brick])  ... )) pigso starts by defining the solution space. permuteo succeeds when the first list is permutation of the second.
  • 33. USEless logic puzzle core.logiccore.logic (fresh [notpopcorn _] (membero notpopcorn [:chocolate :apple]) (membero [:petey _ notpopcorn] q)) (fresh [notwood _] (membero notwood [:straw :brick]) (membero [:pippin notwood _] q)) (fresh [_] (membero [_ :straw :popcorn] q)) (fresh [_] (membero [:petunia _ :apple] q)) (fresh [notbrick _] (membero notbrick [:straw :wood]) (membero [_ notbrick :chocolate] q)) The clues translate cleanly to goals constraining the solution space. membero has a solution when the first item is a member of the second.
  • 34. FACTS and RELATIONS core.logiccore.logic (run* [q] (pigso q)) pigso finds the only solution. ([[:petey :wood :chocolate] [:pippin :straw :popcorn] [:petunia :brick :apple]])
  • 35. FINITE DOMAINS core.logiccore.logic fd/interval declares a finite integer interval and fd/in contrains logic variables to a domain. (defn two-plus-two-is-four [q] (fresh [t w o f u r TWO FOUR] (fd/in t w o f u r (fd/interval 0 9)) (fd/distinct [t w o f u r]) (fd/in TWO (fd/interval 100 999)) (fd/in FOUR (fd/interval 1000 9999))   ... (== q [TWO TWO FOUR]))) T W O + T W O ------- F O U R http://www.amazon.com/Crypt-arithmetic-Puzzles-in-PROLOG-ebook/dp/B006X9LY8O
  • 36. FINITE DOMAINS core.logiccore.logic fd/eq translates simple math to constraints over finite domain logic variables. (fd/eq (= TWO (+ (* 100 t) (* 10 w) o))) (fd/eq (= FOUR (+ (* 1000 f) (* 100 o) (* 10 u) r)))   (fd/eq (= (+ TWO TWO) FOUR)) T W O + T W O ------- F O U R
  • 37. FINITE DOMAINS core.logiccore.logic There are 7 unique solutions to the problem. (run* [q] (two-plus-two-is-four q)) T W O + T W O ------- F O U R ([734 734 1468] [765 765 1530] [836 836 1672] [846 846 1692] [867 867 1734] [928 928 1856] [938 938 1876])
  • 38. sudoku made easier core.logiccore.logic After setting up the logic variables and initializing state, the solution simply requires every row, column and square on the board to have distinct values. (defn solve [puzzle] (let [sd-num (fd/domain 1 2 3 4 5 6 7 8 9) board (repeatedly 81 lvar) rows (into [] (map vec (partition 9 board))) cols (apply map vector rows) squares (for [x (range 0 9 3) y (range 0 9 3)] (get-square rows x y))]   (run* [q] (== q board) (everyg #(fd/in % sd-num) board) (init-board board puzzle) (everyg fd/distinct rows) (everyg fd/distinct cols) (everyg fd/distinct squares)))) https://gist.github.com/swannodette/3217582
  • 39. sudoku made easier core.logiccore.logic matche is pattern matching syntax for conde. To unify the initial logic variables with a board, we require either the board have a 0 or that the logic variable unifies with the value of the board. (defn init-board [vars puzzle] (matche [vars puzzle] ([[] []] succeed) ([[_ . vs] [0 . ps]] (init-board vs ps)) ([[num . vs] [num . ps]] (init-board vs ps))))
  • 40. sudoku core.logiccore.logic (def puzzle1 [0 0 0 0 0 9 0 6 0 0 3 8 0 0 5 0 0 4 0 2 0 0 6 0 0 7 0 0 0 0 0 0 0 3 9 0 0 0 0 9 2 6 0 0 0 0 9 7 0 0 0 0 0 0 0 4 0 0 7 0 0 3 0 5 0 0 4 0 0 2 1 0 0 7 0 8 0 0 0 0 0]) (partition 9 (first (solve puzzle1))) ((7 1 4 2 8 9 5 6 3) (6 3 8 7 1 5 9 2 4) (9 2 5 3 6 4 1 7 8) (8 6 1 5 4 7 3 9 2) (4 5 3 9 2 6 7 8 1) (2 9 7 1 3 8 4 5 6) (1 4 9 6 7 2 8 3 5) (5 8 6 4 9 3 2 1 7) (3 7 2 8 5 1 6 4 9))