minikanren.scm 17.4 KB
Newer Older
1 2
(define-module (logic guile-log minikanren)
  #:use-module (logic guile-log)
3
  #:use-module (logic guile-log vlist)
4
  #:use-module ((logic guile-log umatch)
5
		#:select (gp-attvar-raw? set-attribute-cstor! gp-lookup
6
					 gp-newframe-choice
7
					 gp-make-var
8
					 gp-attvar?))
9 10 11
  #:use-module (logic guile-log run)
  #:use-module (logic guile-log iinterleave)
  #:use-module (logic guile-log guile-prolog attribute)
12
  #:re-export (init-machines new-machine new-pot-machine delay)
13
  #:export
14 15 16
  (conde fresh freshs freshx == =/= symbolo numbero booleano
	 run run* absento condo conde conds condes alls
	 pko pkdyno similar conda condas condu condus condx condxn
17
	 project var true define-delay counter count-up once
18 19 20 21
	 validate))

(define-inlinable (var  x) (<lambda> () (when (attvar? x))))
(define-inlinable (true)   (<lambda> () <cc>))
22 23 24 25

(define touch (make-fluid (make-hash-table)))
(define (init) (fluid-set! touch (make-hash-table)))

26
(define-syntax-rule (once x) (<lambda> () (<once-ii> (x))))
27 28
(define-syntax-rule (project x) (<lookup> x))

29

30 31
;; consa and condu is subtle with no reordering of goals 
;; we could use the guile-log framework but we are not that lucky
32 33 34 35 36 37
(<define-guile-log-rule> (<and*> x ...)
  (<and-ii> (<and> (x) ((reschedule-1*))) ...))

(<define-guile-log-rule> (<and+*> x ...)
  (<and+> (<and> (x) (reschedule #f)) ...))

38
(define-syntax-rule (conda (x xx ...) ...)
39
  (<lambda> ()
40 41 42 43 44
    (<cond> (((once x)) (<and*> xx ...)) ...)))

(define-syntax-rule (condas (x xx ...) ...)
  (<lambda> ()
    (<cond> (((once x)) (<and+*> xx ...)) ...)))
45 46 47 48 49 50 51


(define-syntax condu
  (syntax-rules ()
    ((condu)   
     (<lambda> () <fail>))
    ((condu x) 
52
     (once (conda x)))
53
    ((condu . l)
54 55 56 57 58 59 60 61 62 63 64 65 66
     (once (conda . l)))))

(define-syntax condus
  (syntax-rules ()
    ((condus)   
     (<lambda> () <fail>))
    ((condus x) 
     (once (condas x)))
    ((condus . l)
     (once (condas . l)))))


(define-syntax condo
67 68 69 70
  (syntax-rules ()
    ((conde)
     (<lambda> () <fail>))
    ((conde (x ...))
71
     (<lambda> () (<and*> x ...)))
72 73
    ((conde (x ...) (xx ...) ...)
     (<lambda> ()
74 75 76 77 78
       (<or> (<and> (<and*> x ...)
		    (<and> ((reschedule-1**))
			   (<and*> xx ...))
		    ...))))))

79

80 81 82 83 84
(define-syntax conde
  (syntax-rules ()
    ((conde)
     (<lambda> () <fail>))
    ((conde (x ...))
85 86 87
     (<lambda> () (<and*> x ...)))
    ((conde (x ...) (xx ...) ...)
     (<lambda> ()
88
       (<or-ii> (<and*> x ...)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	   (<and> ((reschedule-1**))
		  (<and*> xx ...))
		...)))))

(define-guile-log <and-rev*>
  (lambda (x)
    (syntax-case x ()
      ((<and-rev*> w x ...)
       (with-syntax (((y ...) (reverse #'(x ...))))
	 #'(<and*> w y ...))))))


(define-syntax -attvar?
  (syntax-rules ()
    ((_ (#t a ...)) (and (attvar? (<lookup> a)) ...))
    ((_ x)          (attvar? (<lookup> x)))))

(define-guile-log ax  
  (syntax-rules ()
    ((_ w (h t) (p ...))
     (<and> w (p) ...))
    ((_ w (h t) (p ...) x)
     (<and> w (p) ... (x)))
    ((_ w (h t) (p ...) x ...)
    (<and> w (p) ... 
       (if (-attvar? h)
	   (if (-attvar? t)
	       (<and*> x ...)
	       (<and-rev*> x ...))
	   (if (-attvar? t)
	       (<and*> x ...)
	       (<and*> x ...)))))))
		 
	     
(define-syntax condx
  (syntax-rules ()
    ((conde)
     (<lambda> () <fail>))
    ((conde p (q x ...))
     (<lambda> () 
      (ax p q x ...)))
    ((conde p (q x ...) (qq xx ...) ...)
     (<lambda> ()
132
      (<or-ii> (ax p q x ...)
133 134 135 136 137 138 139 140 141 142 143 144 145
	       (<and> ((reschedule-1**))
		      (ax p qq xx ...))
	       ...)))))

(define-syntax condxn
  (syntax-rules ()
    ((conde)
     (<lambda> () <fail>))
    ((conde p (q x ...))
     (<lambda> () 
      (ax p q x ...)))
    ((conde p (q x ...) (qq xx ...) ...)
     (<lambda> ()
146
      (<or-ii> (ax p q x ...)
147 148 149 150 151 152 153 154 155 156 157 158 159
	       (<and> ((reschedule-1**))
		      (ax p qq xx ...))
	       ...)))))
		     

(define-syntax condes
  (syntax-rules ()
    ((condes)
     (<lambda> () <fail>))
    ((condes (x ...))
     (<lambda> () (<and+*> x ...)))
    ((condes (x ...) (xx ...) ...)
     (<lambda> ()
160
	       (<or-ii> 
161 162 163 164 165 166 167 168 169 170 171 172
		(<and+> (<and> (x) (reschedule #f)) ...)
		(<and> ((reschedule-1**))
		       (<and+> (<and> (xx) (reschedule #f))
			       ...))
		...)))))

(define-syntax conds
  (syntax-rules ()
    ((conde)
     (<lambda> () <fail>))
    ((conde (x ...))
     (<lambda> () (<and+> (<and> (x) (reschedule #f)) ...)))
173 174
    ((conde (x ...) (xx ...) ...)
     (<lambda> ()
175 176 177 178 179 180
	       (<or> (<and+> (<and> (x) (reschedule #f)) ...)
		     (<and> ((reschedule-1**))
			    (<and+> (<and> (xx) (reschedule #f))
				    ...))
		     ...)))))

181

182
(define-syntax-rule (fresh (v ...) x ...)
183
  (<lambda> ()
184
    (<var> (v ...) (<and*> x ...))))
185 186

(define-syntax-rule (freshs (v ...) x ...)
187
  (<lambda> ()
188 189 190 191 192
    (<var> (v ...) (<and+*> x ...))))

(define-syntax-rule (freshx (v ...) p q x ...)
  (<lambda> ()
    (<var> (v ...) (ax p q x ...))))
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

(define-syntax-rule (==  x y) (<lambda> () (<=> ,x ,y)))

(define (pko x)
  (<lambda> () (<pp> x)))

(define (pkdyno x y)
  (<lambda> () (<pp-dyn> x y)))

(define-inlinable (ground-<type>? pred)
  (lambda (u s)
    (pred (gp-lookup u s))))

(define ground-symbol?
  (ground-<type>? symbol?))

(define ground-number?
  (ground-<type>? number?))

212 213 214
(define ground-boolean?
  (ground-<type>? boolean?))

215
(<define> (abs->abs test u)
216 217
  (<let> ((u (<lookup> u)))
  (<var> (w ww) 
218
    (<if> (<get-attr> u isAbsent w)
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      (<let*> ((w (<lookup> w)))
	(<recur> lp ((w w))
	  (if (pair? w)
	      (if (test (car w))
		  (<and> (=/=* u (car w)) (lp (cdr w)))
		  (lp (cdr w)))
	      (<del-attr> u isAbsent))))
      <cc>)
    (<if> (<get-attr> u isNot ww)
	  (<let> ((ww (<lookup> ww)))
	    (<recur> lp ((ww ww) (r '()))
	      (if (pair? ww)
		  (<let> ((k (car ww)))
		    (if (or (and (test (car k)) (eq? (<lookup> (cdr k)) u))
			    (and (test (cdr k)) (eq? (<lookup> (car k)) u)))
			(lp (cdr ww) (cons k r))
			(if (or (eq? (<lookup> (car k)) u) (eq? (<lookup> 
								 (cdr k)) u))
			    (lp (cdr ww) r)
			    (lp (cdr ww) (cons k r)))))
		  (if (pair? r)
		      (<put-attr> u isNot r)
		      (<del-attr> u isNot)))))
	  <cc>))))

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

(define (symbolo u)
  (<lambda> ()
    (abs->abs symbol? u)
    (<let> ((u (<lookup> u)))
      (if (attvar? u)
	  (if (gp-attvar-raw? u S)
	      (<var> (w)
	        (<if> (<get-attr> u isSymbol w)
		      <cc>
		      (<if> (<or> (<get-attr> u isNumber  w)
				  (<get-attr> u isBoolean w))
			    <fail>
			    (<put-attr> u isSymbol #t))))
	      (<put-attr> u isSymbol #t))
259 260
	  (when (ground-symbol? u S))))
    ((execute-delay u))))
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

(define (booleano u)
  (<lambda> ()
    (abs->abs boolean? u)
    (<let> ((u (<lookup> u)))
      (if (attvar? u)
	  (if (gp-attvar-raw? u S)
	      (<var> (w)
	        (<if> (<get-attr> u isBoolean w)
		      <cc>
		      (<if> (<or> (<get-attr> u isNumber w)
				  (<get-attr> u isSymbol w))
			    <fail>			    
			    (<put-attr> u isBoolean #t))))
	      (<put-attr> u isSymbol #t))
276 277
	  (when (ground-boolean? u S))))
    ((execute-delay u))))
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

(define (numbero u)
  (<lambda> ()
    (<let> ((u (<lookup> u)))
      (abs->abs number? u)
      (if (attvar? u)
	  (if (gp-attvar-raw? u S)
	      (<var> (w)
		(<if> (<get-attr> u isSymbol w)
		      <fail>
		      (<if> (<or> (<get-attr> u isNumber  w)
				  (<get-attr> u isBoolean w))
			    <cc>
			    (<put-attr> u isNumber #t))))
	      (<put-attr> u isNumber #t))
293 294
	  (when (ground-number? u S))))
    ((execute-delay u))))
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
(define (potential-var x)
  (lambda (s . p?)
    (define n 0)
    (<wrap-s>
     (<lambda> ()
        (<recur> lp ((x x))
	  (<<match>> (#:mode -) (x)
	    ((x . l) (<and> (lp x) (lp l)))
	    (x       
	     (<let> ((x (<lookup> x)))
		(if (attvar? x)
		    (<code> (set! n (+ n 1)))
		    <cc>))))))
     s)
    n))

(define (potential x)
  (lambda (s . p?)
    (define n 0)
    (define (p y) 
      (if (pair? p?)
	  (begin
	    (<wrap-s> (<lambda> () (<pp> x)) s)
	    (pk 'min y))
	  y))

    (<wrap-s> (<lambda> (x) 
		 (<recur> lp ((x x))
		   (<<match>> (#:mode -) (x)
		     ((x . l) (<and> (lp x) (lp l)))
		     (x       (<code> (set! n (+ n 1)))))))
	      s
	      x)
    (p (ash n 0))))

(define (potential-c x)
  (let ((a (potential-s x))
	(b (potential   x)))
    (lambda (s . l)
      (+ (apply a s l) (apply b s l)))))

(define (potential-s x)  
  (lambda (s . p?)
    (define (p x) 
      (if (pair? p?) (pk 'min x) x))

    (p
     (if (pair? s)
	 (if (vhash? (cdr s))
	     (vlist-length (cdr s))
	     (length s))
	 0))))

(define (potential-ss x)
  (lambda (s . l)
    (define h (make-hash-table))
    (define i 0)
    (let ((ss (if (pair? s) (cdr s) '())))
      (let lp ((l (if (null? ss) '() (if (pair? ss) ss (vhash->assoc ss)))))
	(if (pair? l)
	    (let ((v (caar l)))
	      (let ((r (hashq-ref h v #f)))
		(if r 
		    (lp (cdr l))
		    (begin
		      (hashq-set! h v #t)
		      (let lp2 ((ll (gp-lookup (cdar l) s)))
			(cond
			 ((pair? ll)
			  (let ((r (hashq-ref h ll #f)))
			    (if r
				#t
				(begin
				  (hashq-set! h ll #t)
				  (lp2 (gp-lookup (car ll) s))
				  (lp2 (gp-lookup (cdr ll) s))))))

			 ((gp-attvar? ll s)
			  (let ((r (hashq-ref h ll #f)))
			    (if r
				#t
				(begin
				  (set! i (+ i 1))
				  (hashq-set! h ll #t)))))
			 
			 (else
			  #t)))
		      (lp (cdr l)))))))))
    i))




389
(define-syntax-rule (run n (vs ...) c ...)
390 391 392 393
  (begin
    (<clear>)
    (with-fluids ((*gp-var-tr* '_.)
		  (*init-tr*   init))
394
      (<run> n (vs ...) 
395 396 397
	(<and>
	 (<logical++>)
	 (init-machines)
398
	 ;(init-pot-machines (potential-c (list vs ...)))
399
	 (init-schedule-data)
400
	 (<and> (<and> (c) ((reschedule-1*))) ...)
401
	 (reschedule #t))))))
402

403
(define-syntax-rule (run* (vs ...) c ...)
404 405 406 407
  (begin
    (<clear>)
    (with-fluids ((*gp-var-tr* '_.)
		  (*init-tr*   init))
408
      (<run> * (vs ...) 
409
	(<and>
410
	 (<logical++>)
411
	 ;(init-pot-machines (potential-c (list vs ...)))
412
	 (init-schedule-data)
413
	 (init-machines)
414
	 (<and>  (<and> (c) ((reschedule-1*))) ...)
415
	 (reschedule #t))))))
416
    
417 418 419 420 421 422 423 424
(define-syntax-rule (<lambda-is> (x y z) . code)
  (<lambda> (x y z)
    (if z
	(<and> . code)
	<fail>)))

(define isAbsent 
  (<lambda-is> (val newval z)
425 426 427 428 429 430 431
  (<let> ((val    (<lookup> val))
	  (newval (<lookup> newval)))
     (<recur> lp ((val val))
	(if (pair? val)
	    (<and>
	     (abse (car val) newval)
	     (lp (cdr val)))
432
	    <cc>)))))
433 434 435 436 437 438 439 440 441 442 443 444 445

(<define> (pisAbsent h t v)
  (<var> (w)
    (<get-attr> v isAbsent w)
    (<=> h ,`(,@(map (lambda (x) `(absento (,x ,v))) (<lookup> w)) . ,t))))

(set-attribute-cstor! isAbsent pisAbsent)

(<define> (abse u v)
  (<let> ((u (<lookup> u)))
    (<recur> lp ((v v))
      (<<match>> (#:mode -) (v)
        ((? attvar? v)
446 447
	 (<let> ((v       (<lookup> v))
		 (simple? #f))
448
	    (if (gp-attvar-raw? v S)
449
		(<var> (w ww)
450 451
		  (<cond>
		   ((<get-attr> v isSymbol w) 
452
		    (<code> (set! simple? #t))
453 454 455 456
		    (if (symbol? u)
			(=/=* v u)
			<cc>))

457 458
		   ((<get-attr> v isBoolean w)
		    (<code> (set! simple? #t)) 
459 460 461 462 463
		    (if (boolean? u)
			(=/=* v u)
			<cc>))
		
		   ((<get-attr> v isNumber w) 
464
		    (<code> (set! simple? #t))
465
		    (if (number? u)
466
			(=/=* v u) 
467
			<cc>))
468 469 470

		   (else <cc>))
		  
471
		   
472 473 474 475 476 477 478 479 480 481
		  (<if> (<get-attr> v isAbsent ww)
			(<let> ((ww (<lookup> ww)))
			  (if (member u ww)
			      <cc>
			      (if simple?
				  <cc>
				  (<put-attr> v isAbsent (cons u ww)))))
			(if simple?
			    <cc>
			    (<put-attr> v isAbsent (list u)))))
482 483 484 485
		(<put-attr> v isAbsent (list u)))))

	((x . y)
	 (<and> (lp x) (lp y)))
486 487 488

	(x (<not> (<==> x u)))))))

489 490 491 492 493 494

(define-inlinable (absento u v)
  (<lambda> ()
    (abse u v)))
      

495

496
(define-syntax-rule (mk-1 isSymbol symbol? symbolo)
497
(define isSymbol
498
  (<lambda-is> (x y z)
499 500 501 502 503 504 505 506 507 508
    (<let> ((y (<lookup> y)))
      (cond
        ((symbol? (<lookup> y))
	 <cc>)
	((gp-attvar-raw? y S)
	 ((symbolo y))))))))

(mk-1 isSymbol  symbol?  symbolo)
(mk-1 isNumber  number?  numbero)
(mk-1 isBoolean boolean? booleano)
509 510 511 512 513 514

(<define> (pisSymbol a tail x)
  (<=> a (,`(sym ,x) . tail)))

(set-attribute-cstor! isSymbol pisSymbol)

515 516 517 518
(<define> (pisNumber a tail x)
  (<=> a (,`(num ,x) . tail)))
    
(set-attribute-cstor! isNumber pisNumber)
519 520 521 522 523 524

(<define> (pisBoolean a tail x)
  (<=> a (,`(bool ,x) . tail)))

(set-attribute-cstor! isBoolean pisBoolean)

525

526 527 528 529 530 531 532 533 534 535
(define isNot 
  (<lambda-is> (val y z)
    (<let> ((y   (<lookup> y))
	    (val (<lookup> val)))
       (<recur> lp ((val val))
	 (if (pair? val)
	     (<and>
	      (=/=* (caar val) (cdar val))
	      (lp (cdr val)))
	     <cc>)))))
536 537 538 539 540 541 542 543 544 545 546 547

(<define> (pisNot a tail x)
  (<var> (r)
    (<get-attr> x isNot r)
    (<recur> lp ((r (<lookup> r)) (a a))
      (if (pair? r)
	  (<let*> ((d (<lookup> (car r)))
		   (q (hashq-ref (fluid-ref touch) d #f)))
	     (if q
		 (lp (cdr r) a)
		 (<var> (t)
		   (<code> (hashq-set! (fluid-ref touch) d #t))
548
		   (<=> a ,`((=/= ,(car d) ,(cdr d)) . ,t))
549 550 551 552 553
		   (lp (cdr r) t))))
	  (<=> a tail)))))

(set-attribute-cstor! isNot pisNot)

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
(<define> (add-test var test other true)
  (<let> ((pred (lambda (x) #t))
	  (code (<lambda> (v)
		   (<if> (<get-attr> var isNot v)
			 (<let> ((head (car test))
				 (tail (cdr test))
				 (v    (<lookup> v)))
			   (<recur> lp ((vv v))
			     (if (pair? vv)
				 (<if> 
				  (<and!> 
				   (<or>
				    (<and> (<==> ,(caar vv) head) 
					   (<==> ,(cdar vv) tail))
				    (<and> (<==> ,(cdar vv) head) 
					   (<==> ,(caar vv) tail))))
				  (<code> (true))
				  (lp (cdr vv)))
				 (<put-attr> var isNot (cons test v)))))
			 (<put-attr> var isNot (list test)))
		   (<cc> #f))))

    (<var> (v)
      (if (and (gp-attvar-raw? var S) (not (gp-attvar? var S)))
	  (<var> (q)
	    (<cond>
	     ((<get-attr> var isSymbol  #t) (<code> (set! pred symbol?)))
	     ((<get-attr> var isNumber  #t) (<code> (set! pred number?)))
	     ((<get-attr> var isBoolean #t) (<code> (set! pred boolean?)))
	     (else <cc>))
	    (if (not (pred v))
		(<cc> #t)
		(code v)))
	  (code v)))))    

589 590

(<define> (=/=* uu vv)
591 592 593 594 595
  (<let*> ((fr   (<newframe>))
	   (test (cons (<lookup> uu) (<lookup> vv)))
	   (true #f)
	   (ftrue (lambda () (set! true #t)))
	   (var  #f))
596 597 598 599 600 601 602

    (<recur> lp ((u uu) (v vv))
      (<let> ((u (<lookup> u))
	      (v (<lookup> v)))
       (if (attvar? u)
	   (<and>
	    (<code> (set! var #t))
603 604 605 606
	    (<values> (t) (add-test u test v ftrue))
	    (if t 
		(<code> (set! true #t))
		<cc>))
607 608 609 610 611
	   <cc>)

       (if (attvar? v)
	   (<and>
	    (<code> (set! var #t))
612 613 614 615
	    (<values> (t) (add-test v test u ftrue))
	    (if t 
		(<code> (set! true #t))
		<cc>))
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
	   <cc>)

       (if (not var)
	   (<<match>> (#:mode -) (u v)
	     ((x . xl) (y . yl)
	      (<and>
	       (lp x y) 
	       (if true
		   <cc>
		   (lp xl yl))))
	     (a b
	      (if (equal? (<lookup> u) (<lookup> v))
		  <cc>
		  (<code> (set! true #t)))))
	   <cc>)))
    (if true
632
	(<code> (<unwind-tail> fr))
633 634 635 636 637
	(when var))))
    
(define-syntax-rule (=/= x y) 
  (<lambda> () (=/=* x y)))

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
(define-syntax-rule (delayable ass)
  (and (and-map (lambda (x) (<var?> x)) ass)
       (let lp ((as ass))
	 (if (pair? as)
	     (let ((q (car as)))
	       (if (memq q (cdr as))
		   #f
		   (lp (cdr as))))
	     #t))))

(define-syntax-rule (define-u (head ...) code ...)
  (<define> (head ... p)
    (cond
     (p code ...)
     (else <fail>))))

(define-u (isDelay l item)
  (<var> (ll)
    (<cond>
     ((<get-attr> item isDelay ll)
      (<recur> l1 ((l l) (new '()))
        (<<match>> (#:mode -) (l)
	  (((and x (as-l _ f . args-l)) . l)
	   (<recur> l2 ((ll ll))
	     (<<match>> (#:mode -) (ll)
	       (((as-ll _ ,f . args-ll) . _)
		(<and>
		 (<=> args-l args-ll)
		 (l1 l new)))
	       ((_ . ll)
		(l2 ll))
	       (()
		(l1 l (cons x new))))))
	  (()
	   (if (pair? new)
	       (<put-attr> item isDelay (append new (<lookup> ll)))
	       <cc>)))))
     (else
      (<recur> lp ((l l))
	(<<match>> (#:mode -) (l)
	  (((as _ f . args) . l)
	   (<and>
	    (<recur> lpp ((as (<lookup> as)))
	     (if (pair? as)
		 (if (delayable (list (car as)))
		     (<var> (q)
		       (<if> (<get-attr> (car as) isDelay q)
			     (<recur> lp2 ((q (<lookup> q)) (new '()))
			       (<<match>> (#:mode -) (q)
                                 (((,as _ ,f . _) . q)
				  (lp2 q new))
				 ((x . q)
				  (lp2 q (cons x new)))
				 (()
				  (<and>
				   (if (null? new)
				       (<del-attr> (car as) isDelay)
				       (<put-attr> (car as) isDelay new))
				   (lpp (cdr as))))))
			     (lpp (cdr as))))
		     (lpp (cdr as)))
		 <cc>))				   
	    ((apply f args))
	    (lp l)))	  
	  (()
	   <cc>)))))))

(define-syntax-rule (define-delay (f args ...) (u ...) code ...)
  (define (f args ...)
    (let ((quode (begin code ...)))
      (<lambda> ()
        (<let> ((as (list (<lookup> u) ...)))
          (if (delayable as)
	      (<let> ((a (list as 'f f args ...)))
		(<var> (q)
		  (<if> (<get-attr> u isDelay q)
			(<put-attr> u isDelay (cons a (<lookup> q)))
			(<put-attr> u isDelay (cons a '())))
		  ...))
	      (quode)))))))

(define (execute-delay v)
  (<lambda> ()
    (<var> (l)
     (<if> (<get-attr> v isDelay l)
	   (<recur> lp ((l l))
	     (<<match>> (#:mode -) (l)
	       (((_ _ f . args) . l)
		(<and>
		 ((apply f args))
		 (lp l)))
	       (()
		<cc>)))
	   <cc>))))
732

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
(<define> (pDelay a tail x)
  (<var> (r)
    (<get-attr> x isDelay r)
    (<recur> lp ((r r) (a a))
      (<<match>> (#:mode -) (r)
	(((and d (as s _ . args)) . l)
	 (<and>
	  (<let*> ((d (<lookup> d))
		   (q (hashq-ref (fluid-ref touch) d #f)))
	     (if q
		 (lp l a)
		 (<var> (t)
		   (<code> (hashq-set! (fluid-ref touch) d #t))
		   (<=> a ,`((,s . ,args) . ,t))
		   (lp l t))))))
	(()
	 (<=> a tail))))))

(set-attribute-cstor! isDelay pDelay)

(define *counter* (gp-make-var 0))       	    
(define (count-up)
  (<lambda> () (<set> *counter* (+ 1 (<lookup> *counter*)))))
(define-syntax-rule (counter) (<lookup> *counter*))
(define (validate nr)
  (<lambda> ()
    (when (eq? (counter) nr))))