Skip to content

Commit

Permalink
refactored make-term wrt prolog lists, added tests for reading prolog…
Browse files Browse the repository at this point in the history
… lists and term printing
  • Loading branch information
erik sohnel committed Jun 23, 2009
1 parent aeaf791 commit eb7bb29
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
24 changes: 22 additions & 2 deletions hoeck/prolog-tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
(is (every? false? (map get-variable-name non-vars))) "no varname corner-cases"))


(deftest term-construction-deconstruction
(deftest term-construction
;; comparing clojure term creation with what
;; the builtin tuprolog parse constructs
;; compare the string outputs of the two resulting Terms instead
Expand All @@ -27,6 +27,7 @@
(are (= (str (read-term _1)) (str (make-term _2)))
;; atoms
"1" 1
"1.0001" 1.0001
"p" 'p
;; won't work: "single-atom" 'single-atom due: "-" is a prolog operator
"single_atom" 'single_atom
Expand All @@ -53,7 +54,26 @@
'(<- (predicate2 X 1)
(predicate2 (p X) (a 1))
(= X (a Y) (b X))
(b Y))))
(b Y))
;; lists
"[]" []
"[a]" '[a]
"[a,b,c,d]" '[a b c d]
"[a|3]" '(. a 3)
"[p(X,[a,b]),99]" '[(p X [a b]) 99]
"[p(X,[a,b])|99]" '(. (p X [a b]) 99)))

(deftest term-deconstruction
;; testing the make-clojure-term function
(are (= (make-clojure-term (make-term _1)) _1)
;; atoms
1, 1.1, 'ppp, 'pred-a, 'predicate-1, 'Var, 'X
;; predicates
'(p X) '(predicate X Y 1) '(p (p X) Y) '(p (q (r (s X) (t X))))
;; lists
[] [1 2 3] '[(pred X (func Y)) (func Y) 1 2]
;; dotted lists
'(. 1 2)))


;; interface tests:
Expand Down
17 changes: 10 additions & 7 deletions hoeck/prolog.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[clojure.contrib.def :only [defnk]]
[clojure.set :only [difference]]
clojure.contrib.test-is)
(:load "prolog-tests")
(:import (alice.tuprolog Prolog Term Struct Var Library PrimitiveInfo Theory)))

(defn- anonymous-var?
Expand All @@ -30,6 +29,8 @@
(Character/isLetter (first vname))
vname)))))))

(declare make-term)

(defn- fold-right-struct
"ex: (fold-right-struct 'op '(1 2 3)) -> (op 1 (op 2 3))
used to create nested structs. The ',' (and) operator is expanded using this function."
Expand All @@ -51,9 +52,8 @@
[expr]
(cond
(vector? expr)
(if (= (nth expr 1 nil) '.)
(Struct. (make-term (nth expr 0)) (make-term (nth expr 2)))
(Struct. (into-array Term (map make-term expr))))
;; to create lists use [1 2 3] or (. 1 (. 2 (. 3 [])))
(Struct. (into-array Term (map make-term expr)))
(seq? expr)
(if (empty? expr)
(alice.tuprolog.Struct.) ;; empty list
Expand Down Expand Up @@ -224,10 +224,10 @@
alice.tuprolog.Long (.longValue term)
alice.tuprolog.Int (.intValue term))
alice.tuprolog.Struct (let [get-terms (fn [] (map #(make-clojure-term (.getTerm term %)) (range 0 (.getArity term))))]
(cond (.isAtom term) (get-name)
(cond (.isList term) (vec (map make-clojure-term (-> term .listIterator iterator-seq)))
(.isAtom term) (get-name)
(.isClause term) (cons '<- (get-terms))
(.isCompound term) (cons (get-name) (get-terms))
(.isList term) (throwf "todo")))
(.isCompound term) (cons (get-name) (get-terms))))
alice.tuprolog.Var (cond (.isAnonymous term) '_
:else (get-name)))))

Expand Down Expand Up @@ -267,6 +267,9 @@
(defmacro ?- [& query]
`(apply ?-* '~query))


(load "prolog-tests")

;;;;
(comment

Expand Down

0 comments on commit eb7bb29

Please sign in to comment.