Skip to content

Instantly share code, notes, and snippets.

@raek
Created May 3, 2011 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raek/954222 to your computer and use it in GitHub Desktop.
Save raek/954222 to your computer and use it in GitHub Desktop.
And now, an explanation of the joke...
(ns dcg-joke
(:refer-clojure :exclude [reify == inc])
(:use (clojure.core.logic minikanren prelude)))
(declare sentence noun-phrase preposition-phrase verb-phrase
pronoun determiner noun preposition auxiliary verb)
(defn sentence [s s1 s6]
(exist [aux np1 vp np2 s2 s3 s4 s5]
(auxiliary aux s1 s2)
(noun-phrase np1 s2 s3)
(verb-phrase vp s3 s4)
(noun-phrase np2 s4 s5)
(conde ((exist [pp]
(preposition-phrase pp s5 s6)
(== s (list 'S aux np1 vp np2 pp))))
((== s5 s6)
(== s (list 'S aux np1 vp np2))))))
(defn noun-phrase [np s1 s4]
(conde ((exist [pn]
(pronoun pn s1 s4)
(== np (list 'NP pn))))
((exist [d n s2]
(determiner d s1 s2)
(noun n s2 s4)
(== np (list 'NP d n))))
((exist [d n pp s2 s3]
(determiner d s1 s2)
(noun n s2 s3)
(preposition-phrase pp s3 s4)
(== np (list 'NP (list 'NP d n) pp))))))
(defn preposition-phrase [pp s1 s3]
(exist [p np s2]
(preposition p s1 s2)
(noun-phrase np s2 s3)
(== pp (list 'PP p np))))
(defn verb-phrase [vp s1 s2]
(exist [v]
(verb v s1 s2)
(== vp (list 'VP v))))
(defne pronoun [pn s1 s2]
([_ ['I . ?x] ?x] (== pn (list 'PN 'I))))
(defne determiner [d s1 s2]
([_ ['the . ?x] ?x] (== d (list 'D 'the)))
([_ ['this . ?x] ?x] (== d (list 'D 'this))))
(defne noun [n s1 s2]
([_ ['dress . ?x] ?x] (== n (list 'N 'dress)))
([_ ['window . ?x] ?x] (== n (list 'N 'window))))
(defne preposition [p s1 s2]
([_ ['in . ?x] ?x] (== p (list 'P 'in))))
(defne auxiliary [aux s1 s2]
([_ ['could . ?x] ?x] (== aux (list 'AUX 'could))))
(defne verb [v s1 s2]
([_ ['try 'on . ?x] ?x] (== v (list 'V 'try 'on))))
(comment
(run 2 [parse-tree]
(sentence parse-tree '(could I try on this dress in the window) ()))
;; =>
((S (AUX could)
(NP (PN I))
(VP (V try on))
(NP (D this)
(N dress))
(PP (P in)
(NP (D the)
(N window))))
(S (AUX could)
(NP (PN I))
(VP (V try on))
(NP (NP (D this)
(N dress))
(PP (P in)
(NP (D the)
(N window))))))
;; Yes please, but we also have a fitting room
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment