From 81c8f01c12f734306da2cf8b58dd5bd35128bb69 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Fri, 19 Jul 2019 19:53:15 -0400 Subject: [PATCH] Make API more flexible --- src/sicp_logic/core.clj | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/sicp_logic/core.clj b/src/sicp_logic/core.clj index 902db1c..75bd330 100644 --- a/src/sicp_logic/core.clj +++ b/src/sicp_logic/core.clj @@ -1,6 +1,6 @@ (ns sicp-logic.core (:require [sicp-logic.binding :refer [instantiate]] - [sicp-logic.db :refer [add-assertion add-rule]] + [sicp-logic.db :as db] [sicp-logic.evaluator :refer [qeval]])) (defn contract-question-mark [v] @@ -36,10 +36,17 @@ (defn assert! [db assertion] "Adds a new assertion to the database." - (add-assertion db assertion)) + (db/add-assertion db assertion)) + +(defn add-rule! [db rule] + "Adds a new rule to the database." + (db/add-rule db (query-syntax-process rule))) (defmacro defrule [db conclusion body] - "Adds a new rule to the database." - (let [processed-conclusion (query-syntax-process conclusion) - processed-body (query-syntax-process body)] - `(add-rule ~db (quote [~processed-conclusion ~processed-body])))) + "Convenience macro to add a new rule to the database. + + Usage: + (defrule [grandparent ?x ?y] + (and [parent ?x ?z] + [parent ?z ?y]))" + `(add-rule! ~db (quote [conclusion body])))