Skip to content

Commit

Permalink
Correct misplaced docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed Jan 9, 2012
1 parent b78ab6a commit 14d69a3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 34 deletions.
12 changes: 8 additions & 4 deletions src/logic_introduction/core.clj
Expand Up @@ -6,16 +6,18 @@

;; Logic functions

(defn geto [key env value]
(defn geto
"env is an environment such that the expression key is
associated with the expression value"
[key env value]
(matche [env]
([[[key :- value] . _]])
([[_ . ?rest]] (geto key ?rest value))))

(defn typedo [context exp result-type]
(defn typedo
"context is an environment such that expression exp executed in
environment context results in type result-type"
[context exp result-type]
(conde
((geto exp context result-type))
((matche [context exp result-type]
Expand All @@ -28,8 +30,9 @@

;; Frontend functions

(defmacro deftyped [name type arg-vec & body]
(defmacro deftyped
"Defines a function with a strict type"
[name type arg-vec & body]
(list* `defn
(with-meta name
(assoc (meta name)
Expand Down Expand Up @@ -93,8 +96,9 @@
(get-return t)))


(defn type-check-form [form]
(defn type-check-form
"Type checks a form"
[form]
(let [call (-> form
params-to-sym
call-to-lambda)
Expand Down
15 changes: 10 additions & 5 deletions src/logic_introduction/decl_model.clj
Expand Up @@ -28,8 +28,9 @@
(lfirst [_] a)
(lrest [_] d))

(defn lcons [a d]
(defn lcons
"Constructs a sequence a with an improper tail d if d is a logic variable."
[a d]
(if (or (coll? d) (nil? d))
(cons a (seq d))
(LCons. a d )))
Expand Down Expand Up @@ -255,9 +256,10 @@

;; Polymorphic type

(defn env-assoc [exp env type]
(defn env-assoc
"env is an environment such that the expression key is
associated with the type value"
[exp env type]
(cond-one
((undo-if-false [exp env type]
(caro env [exp :- type])))
Expand All @@ -281,8 +283,9 @@

;; Numbers

(defn s [n]
(defn s
"Returns n's succeeding natural number"
[n]
(lcons n []))

(def zero 0)
Expand All @@ -293,8 +296,9 @@
(def five (s four))
(def six (s five))

(defn natural-number [x]
(defn natural-number
"A relation where x is a natural number"
[x]
(println "top")
(let-logic-variable [n]
(cond-one
Expand All @@ -308,9 +312,10 @@
(set-or-equals (s n) x)))
(natural-number n)))))

(defn <=o [x y]
(defn <=o
"x and y are natural numbers, such that x is less than or
equal to y"
[x y]
(let-logic-variable [?x ?y]
(cond-one
((undo-if-false [x y]
Expand Down
27 changes: 18 additions & 9 deletions src/logic_introduction/fetch.clj
Expand Up @@ -5,43 +5,50 @@

;; http://www.daddymodern.com/useful-prolog/

(defn url-to-process [url]
(defn url-to-process
"Output: url"
[url]
(== url "http://api.worldbank.org/countries/USA/indicators/AG.AGR.TRAC.NO?per_page=10&date=2005:2011&format=json"))

(defn slurpo [url datastream]
(defn slurpo
"Input: url
Output: datastream"
[url datastream]
(project [url]
(== datastream (slurp url))))

(defn read-jsono [input output]
(defn read-jsono
"Input: input
Output: output"
[input output]
(project [input]
(== output (read-json input))))


(defn fetch-data [fetched-data]
(defn fetch-data
"Output: fetched-data"
[fetched-data]
(fresh [url datastream]
(url-to-process url)
(slurpo url datastream)
(read-jsono datastream fetched-data)))

(defn process-data-header [header]
(defn process-data-header
"Input: header"
[header]
(matche [header]
([?ignore])))

(defn json-object-has-value [json-object name value]
(defn json-object-has-value
"Input: json-object, name
Output: value"
[json-object name value]
(project [json-object name]
(== [name value] (find json-object name))))

(defn process-json-object [json-object]
(defn process-json-object
"Input: json-object"
[json-object]
(fresh [date-value indicator-value temp]
(json-object-has-value json-object :date date-value)
(json-object-has-value json-object :value indicator-value)
Expand All @@ -51,16 +58,18 @@
(== temp (println "Value: " indicator-value))
(== temp (println)))))

(defn process-data-contents [json]
(defn process-data-contents
"Input: json"
[json]
(matche [json]
([[]])
([[?json-object . ?rest]]
(process-json-object ?json-object)
(process-data-contents ?rest))))

(defn process-data [json]
(defn process-data
"Input: json"
[json]
(matche [json]
([[?header ?contents]]
(process-data-header ?header)
Expand Down
36 changes: 24 additions & 12 deletions src/logic_introduction/numbers.clj
Expand Up @@ -4,8 +4,9 @@

;; From "Art of Prolog", Chapter 3

(defn s [x y]
(defn s
"x and y are natural numbers, such that y is the successor of x"
[x y]
(conso x [] y))

(def zero 0)
Expand All @@ -16,17 +17,19 @@
(def five (first (run 1 [q] (s four q))))
(def six (first (run 1 [q] (s five q))))

(defn natural-number [x]
(defn natural-number
"A relation where x is a natural number"
[x]
(conde
[(== zero x)]
[(fresh [p]
(s p x)
(natural-number p))]))

(defn <=o [x y]
(defn <=o
"x and y are natural numbers, such that x is less than or
equal to y"
[x y]
(conde
[(== x zero)
(natural-number y)]
Expand All @@ -35,8 +38,9 @@
(s yp y)
(<=o xp yp))]))

(defn <o [x y]
(defn <o
"x and y are natural numbers, such that x is less than y"
[x y]
(conde
[(== x zero)
(natural-number y)
Expand All @@ -46,9 +50,10 @@
(s yp y)
(<=o xp yp))]))

(defn plus [x y z]
(defn plus
"x, y, and z are natural numbers such that z is the sum of
x and y"
[x y z]
(conde
[(fresh [a]
(== [zero a a] [x y z])
Expand All @@ -58,19 +63,21 @@
(s zp z)
(plus xp y zp))]))

(defn times [x y z]
(defn times
"x, y, and z are natural numbers such that z is the product
of x and y"
[x y z]
(conde
[(== [zero zero] [x z])]
[(fresh [xp xy]
(s xp x)
(times xp y xy)
(plus xy y z))]))

(defn exp [n x y]
(defn exp
"n, x, and y are natural numbers such that y equals x raised
to the power n"
[n x y]
(conde
[(fresh [np]
(== [zero zero] [x y])
Expand All @@ -83,33 +90,37 @@
(exp np x z)
(times z x y))]))

(defn factorial [n f]
(defn factorial
"f equals n factorial"
[n f]
(conde
[(== [zero one] [n f])]
[(fresh [np f1]
(s np n)
(factorial np f1)
(times n f1 f))]))

(defn minimum [n1 n2 min]
(defn minimum
"The minimum of the natural numbers n1 and n2 is min"
[n1 n2 min]
(conde
[(== n1 min)
(<=o n1 n2)]
[(== n2 min)
(<=o n2 n1)]))

(defn modo [x y z]
(defn modo
"z is the remainder of the integer division of x by y"
[x y z]
(fresh [q qy]
(<o z y)
(times y q qy)
(plus qy z x)))

(defn ackermann [x y a]
(defn ackermann
"a is the value of Ackermann's function for the natural
numbers x and y"
[x y a]
(conde
[(s y a)
(== zero x)]
Expand All @@ -123,8 +134,9 @@
(ackermann x yp val1)
(ackermann xp val1 a))]))

(defn gcd [x y z]
(defn gcd
"z is the greatest common divisor of the natural numbers x and y"
[x y z]
(conde
[(modo x y z)
(gcd y z z)]
Expand Down
12 changes: 8 additions & 4 deletions src/logic_introduction/polymorphism.clj
Expand Up @@ -3,23 +3,26 @@
(:use [logic-introduction.facts])
(:use [clojure.core.logic]))

(defn env-assoc [exp env type]
(defn env-assoc
"env is an environment such that the expression key is
associated with the type value"
[exp env type]
(matche [env]
([[[exp :- type] . _]])
([[_ . ?rest]] (env-assoc exp ?rest type))))

(defn same-or-subtype [parent child-or-same]
(defn same-or-subtype
"parent is a simple type such that type child-or-same is a subtype
or the same as parent"
[parent child-or-same]
(conde
((== parent child-or-same))
((derived-ancestor parent child-or-same))))

(defn polymorphic-type [parent child-or-same]
(defn polymorphic-type
"parent is a (simple or compound) type such that (simple or compound) type child
is equal or subtype of parent"
[parent child-or-same]
(matche [parent child-or-same]
([parent child-or-same]
(same-or-subtype parent child-or-same))
Expand All @@ -28,9 +31,10 @@
(same-or-subtype ?p ?c)
(polymorphic-type ?ps ?cs))))

(defn expression-check [context exp result-type]
(defn expression-check
"context is an environment such that expression exp executed in
environment context results in type result-type"
[context exp result-type]
(conde
((fresh [poly-res-type]
(polymorphic-type result-type poly-res-type)
Expand Down

0 comments on commit 14d69a3

Please sign in to comment.