Skip to content

Commit

Permalink
Moved common functions to nutils, renamed continuation macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
itfrombit committed Jan 17, 2010
1 parent 41ae3fc commit 2c8e9b3
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 189 deletions.
8 changes: 4 additions & 4 deletions atn.nu
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
(set g-atn-cur nil)

(macro defnode (name *arcs)
`(cc-function ,name (pos regs) (choose ,@*arcs)))
`(=function ,name (pos regs) (choose ,@*arcs)))

(macro down (sub next *cmds)
`(cc-bind (g-atn-cur pos regs) (,sub pos (cons nil regs))
`(=bind (g-atn-cur pos regs) (,sub pos (cons nil regs))
(,next pos ,(compile-cmds *cmds))))

(macro cat (cat next *cmds)
Expand All @@ -35,7 +35,7 @@

(macro up (expr)
`(let ((g-atn-cur (nth pos $sent)))
(cc-values ,expr pos (cdr regs))))
(=values ,expr pos (cdr regs))))

(macro getr (key *regs)
(if (null? *regs) ;; regs is optional
Expand All @@ -62,7 +62,7 @@
`(progn
(set $sent ,sent)
(set $paths nil)
(cc-bind (parse __pos __regs) (,node 0 '(nil))
(=bind (parse __pos __regs) (,node 0 '(nil))
(if (== __pos ($sent length))
(then (progn ,@*body (fail)))
(else (fail))))))
Expand Down
12 changes: 6 additions & 6 deletions callcc.nu
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

(set g-cc-cont (do (x) x))

(macro cc-do (params *body)
(macro =do (params *body)
`(do (g-cc-cont ,@params) ,@body))

(macro cc-function (name params *body)
(let ((__f ((+ "cc-" name) symbolValue)))
(macro =function (name params *body)
(let ((__f ((+ "=" name) symbolValue)))
(set __paramlist nil)
(set __m (list __f 'g-cc-cont))
(set __m (append __m (params map: (do (p) (list 'quasiquote-eval p)))))
Expand All @@ -17,11 +17,11 @@
,__m)
(function ,__f (g-cc-cont ,@params) ,@*body))))

(macro cc-bind (params expr *body)
(macro =bind (params expr *body)
`(let ((g-cc-cont (do ,params ,@*body))) ,expr))

(macro cc-values (*vals)
(macro =values (*vals)
`(g-cc-cont ,@*vals))

(macro cc-apply (fn *params)
(macro =apply (fn *params)
`(apply ,fn g-cc-cont ,@*params))
5 changes: 0 additions & 5 deletions choose.nu
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@
`(cb (do (,var) ,@*body) ,choices))

(function cb (fn choices)
;(puts "cb: fn: #{fn}")
;(puts "cb: choices: #{choices}")

(if (not (null? choices))
(then
;(puts "cb: then")
(if (cdr choices)
(push (do () (cb fn (cdr choices)))
g-choose-paths))
((fn (car choices))))
(else
;(puts "cb: else")
(fail))))

(function choose-clear-paths ()
Expand Down
61 changes: 25 additions & 36 deletions dft.nu
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(load "Nutils:cl_utils")
(load "callcc.nu")

;; A test generator example:
Expand All @@ -10,61 +11,49 @@

(set g-dft-saved nil)

(cc-function dft-node (tree)
(=function dft-node (tree)
(cond ((null? tree)
(restart))
((atom tree)
(cc-values tree))
(=values tree))
(else
(push (do () (dft-node (cdr tree))) g-dft-saved)
(dft-node (car tree)))))

(cc-function restart ()
(=function restart ()
(if (not (null? g-dft-saved))
(then
(set f (pop g-dft-saved))
(f))
(else
(cc-values "done"))))
(=values "done"))))

(cc-function dft2 (tree)
(=function dft2 (tree)
(set g-dft-saved nil)
(cc-bind (node) (dft-node tree)
(cond ((== node "done") (cc-values nil))
(else (puts node)
(restart)))))
(=bind (node) (dft-node tree)
(cond ((== node "done") (=values nil))
(else (puts node)
(restart)))))


(set t1 '(10 (20 (30 40)) (50 60 (70 80) 90)))
(set t2 '(1 (2 (3 6 7) 4 5)))

;(dft t2)
(puts "(dft-node t2): #{(dft-node t2)}")
(puts " (restart): #{(restart)}")
(puts " g-dft-saved: #{g-dft-saved}")

;(puts "restart macro:")
;(puts restart)
;(puts "restart function:")
;(puts cc-restart)
;(puts "dft-node macro")
;(puts dft-node)
;(puts "dft-node function")
;(puts cc-dft-node)
(puts "(dft2 t2): #{(dft2 t2)}")

(puts (dft-node t2))

(puts (restart))

(puts g-dft-saved)

(dft2 t2)

;; Generate all combinations of the tree nodes
(puts "Generating all combinations of the tree nodes:")
(puts
(cc-bind (node1) (dft-node t1)
(if (== node1 'done)
(then 'done)
(else
(cc-bind (node2) (dft-node t2)
(list node1 node2))))))
(=bind (node1) (dft-node t1)
(if (== node1 'done)
(then 'done)
(else
(=bind (node2) (dft-node t2)
(list node1 node2))))))

(30 times: (do (x) (puts (restart))))

Expand All @@ -73,16 +62,16 @@
;(dft2 t2)

;; A few quick tests...
;(macrox (cc-function add1 (x) (cc-values (+ 1 x))))
;(macrox (=function add1 (x) (=values (+ 1 x))))
;
;(cc-function add1 (x) (cc-values (+ 1 x)))
;(=function add1 (x) (=values (+ 1 x)))
;
;(add1 5)

;; Multiple args
;(macrox (cc-function a (b c) (+ b c)))
;(macrox (=function a (b c) (+ b c)))
;
;(cc-function a (b c) (+ b c))
;(=function a (b c) (+ b c))
;
;(a 1 2)

Expand Down
47 changes: 29 additions & 18 deletions parlor_trick.nu
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@
(load "callcc.nu")
(load "choose.nu")

(=function two-numbers ()
(choose-bind n1 '(0 1 2 3 4 5)
(choose-bind n2 '(0 1 2 3 4 5)
(=values n1 n2))))

(=function parlor-trick (sum)
(=bind (n1 n2) (two-numbers)
(if (== (+ n1 n2) sum)
(then
;(choose-clear-paths)
`(the sum of ,n1 ,n2))
(else
(fail)))))


;; Test out choose with a simple example:
(function doto (x)
(choose (+ x 2) (* x 2)))

(puts "(macrox (choose (+ x 2) (* x 2))):")
(puts (macrox
(choose (+ x 2) (* x 2))
))

(function doto (x)
(choose (+ x 2) (* x 2)))
(puts "(choose (+ x 2) (* x 2))")

(puts (doto 3))
(puts "(doto 3): #{(doto 3)}")
(puts "(fail): #{(fail)}")

(cc-function two-numbers ()
(choose-bind n1 '(0 1 2 3 4 5)
(choose-bind n2 '(0 1 2 3 4 5)
(cc-values n1 n2))))

(cc-function parlor-trick (sum)
(cc-bind (n1 n2) (two-numbers)
(if (== (+ n1 n2) sum)
(then
(choose-clear-paths)
`(the sum of ,n1 ,n2))
(else
(fail)))))

(puts (parlor-trick 7))
(choose-clear-paths)
(puts "(parlor-trick 7): #{(parlor-trick 7)}")
(puts "(fail): #{(fail)}")
(puts "(fail): #{(fail)}")
(puts "(fail): #{(fail)}")
(puts "(fail): #{(fail)}")

Loading

0 comments on commit 2c8e9b3

Please sign in to comment.