Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed recur.el bug in cond expansion, started kanren implementation i…
…n loel.el
- Loading branch information
1 parent
28275fb
commit bccfb64
Showing
13 changed files
with
356 additions
and
97 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
(provide 'loel) | ||
(require 'cl) | ||
(require 'recur) | ||
|
||
(defvar *var-tag-table* | ||
(make-hash-table :weakness 'key) | ||
"var-tag-table is a weak table which helps identifty loel variables.") | ||
|
||
(defun* new-var-tag (&optional (prefix "")) | ||
(let ((sym (gensym prefix))) | ||
(puthash sym t *var-tag-table*) | ||
sym)) | ||
|
||
(defun tag-in-table? (tag) | ||
(gethash tag *var-tag-table* nil)) | ||
|
||
(defun f-var (sym) | ||
(vector '--var (new-var-tag) sym)) | ||
|
||
(defun var? (o) | ||
(and (vectorp o) | ||
(= (length o) 3) | ||
(eq (elt o 0) '--var) | ||
(tag-in-table? (elt o 1)))) | ||
|
||
(defun var-sym (var) | ||
(elt var 2)) | ||
|
||
(defvar *symbol-counter* 0) | ||
(defun new-symbol () | ||
(prog1 (internf "_%d" *symbol-counter*) | ||
(setq *symbol-counter* (+ 1 *symbol-counter*)))) | ||
|
||
(defmacro* var (&optional (symbol (new-symbol))) | ||
(if (not (symbolp symbol)) | ||
(error "Var must be initialized with a symbol.") | ||
`(f-var ',symbol))) | ||
|
||
(defun rhs (assoc) (cdr assoc)) | ||
(defun lhs (assoc) (car assoc)) | ||
|
||
(recur-defun* | ||
walk-step | ||
(var sub) | ||
(cond | ||
((empty? sub) nil) | ||
(t | ||
(if (eq (var-sym var) | ||
(var-sym (car (car sub)))) | ||
(car sub) | ||
(recur var (cdr sub)))))) | ||
|
||
(recur-defun* walk (var sub) | ||
(cond | ||
((var? var) | ||
(let ((a (walk-step var sub))) | ||
(cond | ||
(a (recur (rhs a) sub)) | ||
(t var)))) | ||
(t var))) | ||
|
||
|
||
(defmacro substitution (&rest pairs) | ||
`(list ,@(loop for pair in (bunch-list pairs) collect | ||
`(cons ,(car pair) ,(cadr pair))))) | ||
|
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
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(require 'monads) | ||
(require 'utils) | ||
|
||
(defun get-options (options) (cdr options)) | ||
(defun options? (mb-options) | ||
(and (listp mb-options) | ||
(eq (car mb-options) 'Options))) | ||
(defun list->options (lst) | ||
(cons 'Options lst)) | ||
|
||
|
||
(defun options-bind (v f) | ||
(let ((options (get-options v))) | ||
(list->options (mapcat (comp #'get-options f) options)))) | ||
|
||
(defvar options-monad | ||
(tbl! | ||
:m-bind | ||
#'options-bind | ||
:m-return | ||
(lex-lambda (v) | ||
(list->options (list v)))) | ||
"Options monad - just window dressing on the list monad.") | ||
|
||
|
||
(defun fpush (x stack) (cons x stack)) | ||
(defun fdrop (stack) (cdr stack)) | ||
|
||
(defun mfpush (mitems mstack) | ||
(funcall (m-lift-into2 #'fpush options-monad) mitems mstack)) | ||
(defun mfdrop (mstack) | ||
(funcall (m-lift-into1 #'fdrop options-monad) mstack)) | ||
|
||
(mfdrop (mfpush '(Options a b c) '(Options () (a) (a a)))) | ||
|
||
(domonad options-monad | ||
[x '(Options 1 2 3) | ||
y '(Options 4 5 6)] | ||
(list x y)) | ||
|
||
(get-options '(Options a b c)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1 @@ | ||
(setf fact-db (cl-make-hash-table :test 'equal)) | ||
|
||
(defmacro fact (name args &rest terms) | ||
`(setf (tbl fact-db '(,name ,@args)) | ||
,(if terms `(quote ,terms) t))) | ||
|
||
(defun capitilized-symbol? (sym) | ||
(let* ((str-version (format "%s" sym)) | ||
(first-char (substring str-version 0 1))) | ||
(string= first-char (upcase first-char)))) | ||
|
||
(defun prolog-constant? (sym) | ||
(or (numberp sym) | ||
(not (capitilized-symbol? sym)) | ||
(stringp sym))) | ||
|
||
(defun constant-form? (form) | ||
(and-over #'prolog-constant? (cdr form))) | ||
(defmacro query (&rest form) | ||
(cond | ||
((constant-form? form) `($ ',form in fact-db 'equal)) | ||
(t 'too-dumb-for-this-query))) | ||
|
||
(macroexpand '(fact father (vincent) (child-of X vincent) (married vincent (mother-of Y X)))) | ||
|
||
(fact human (vincent)) | ||
(fact human (shelley)) | ||
(fact mother (shelley weather)) | ||
(fact father (vincent weather)) | ||
(fact human (X) (mother Y X) (father Z X) (human Y) (human Z)) | ||
(keyshash fact-db) | ||
(query human vincent) | ||
(macroexpand '(query human vincent)) | ||
(in '(human vincent) (keyshash fact-db) 'equal) | ||
(equal '(human vincent) '(human vincent)) | ||
|
||
(fact parent (vincent weather)) | ||
(fact parent (shelley weather)) | ||
(fact parent (shelley flint)) | ||
(fact parent (vincent flint)) | ||
(fact parent (debbie sophie)) | ||
(fact parent (brian sophie)) | ||
|
||
|
||
(query parent X Y) | ||
; find possible values of X, these are any facts or rules in the data base for which (parent X Y) could possibly be true. | ||
; find possible values of Y, these are any facts or rules for which (parent x Y) is true, where x [= X | ||
|
||
(parent |
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
Oops, something went wrong.