Created
July 8, 2015 03:22
-
-
Save nberger/4159b7c31f75c5b4d79d to your computer and use it in GitHub Desktop.
recipes with core.logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns recipes.core | |
(:refer-clojure :exclude [==]) | |
(:use [clojure.core.logic] | |
[clojure.core.logic.pldb :as pldb])) | |
(db-rel in-larder i) | |
(db-rel recipe r) | |
(db-rel in-recipe r i) | |
(db-rel compound-ingredient i is) | |
(declare ingredient-in-lardero) | |
(defne any-ingredient-in-lardero [ingredients] | |
([[?i . ?morei]] (conda [(ingredient-in-lardero ?i)] | |
[(emptyo ?morei) fail] | |
[(any-ingredient-in-lardero ?morei)]))) | |
(defne all-ingredients-in-lardero [ingredients] | |
([[?i . ?morei]] | |
(ingredient-in-lardero ?i) | |
(conda [(emptyo ?morei)] | |
[(all-ingredients-in-lardero ?morei)]))) | |
(defn ingredient-in-lardero [i] | |
(conde | |
[(fresh [composition op sub-ingredients] | |
(compound-ingredient i composition) | |
(conso op sub-ingredients composition) | |
(conde | |
[(== :or op) (any-ingredient-in-lardero sub-ingredients)] | |
[(== :and op) (all-ingredients-in-lardero sub-ingredients)]))] | |
[(in-larder i)])) | |
(defn possible-recipe [r] | |
(recipe r) | |
(fresh [ingredients] | |
(in-recipe r ingredients) | |
(all-ingredients-in-lardero ingredients))) | |
(def recipes (db | |
[compound-ingredient :carrots-or-peas [:or :carrots :peas]] | |
[compound-ingredient :onions-or-garlic [:or :onions :garlic]] | |
[compound-ingredient :carrots-and-onions [:and :carrots :onions]] | |
[compound-ingredient :rice-and-peas [:and :rice :peas]] | |
[compound-ingredient :carrots-onions-or-rice-peas [:or :carrots-and-onions :rice-and-peas]] | |
[recipe :risotto-a] | |
[recipe :risotto-b] | |
[in-recipe :risotto-a [:carrots-or-peas :rice :onions-or-garlic]] | |
[in-recipe :risotto-b [:garlic :carrots-onions-or-rice-peas]])) | |
(def larder-1 (db [in-larder :carrots] | |
[in-larder :rice] | |
[in-larder :garlic])) | |
(def larder-2 (db [in-larder :rice] | |
[in-larder :peas] | |
[in-larder :garlic])) | |
(with-dbs [recipes larder-1] | |
(run* [q] | |
(possible-recipe q))) | |
;=> (:risotto-a) | |
(with-dbs [recipes larder-2] | |
(run* [q] | |
(possible-recipe q))) | |
;=> (:risotto-a :risotto-b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment