macros.scm 22 KB
Newer Older
1
(export <next> <or> <and> <not> <bag-of> <cond> <if> 
2
        <with-guile-log> <eval> <if-some>
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
3 4
        <cc> <fail> <let> <let*> <var> </.> <ask> <ret> <when>
        <define> <cut> <pp> <pp-dyn> <set-cut>
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
5
        bag-of/3 set-of/3 <run> <recur> <letrec>
6 7
        <lambda> <with-fail> <with-cut> <peek-fail>
        <match> <=> <r=> <==> *r* <funcall>
8
        <and!> <and!!> <succeds> 
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
9
        <format> <tail-code> <code> <ret> <return>
10
        <def> <<define>> <with-fail> <dynwind> parse<>
11 12
        let<> <or-i> <stall> <continue> <take>
        <state-ref> <state-set!> <lv*> <clear>
13
        <and-i> and-interleave interleave tr
14
        <letg> <set!> define-guarded *gp-var-tr*)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
15

16
(re-export define-guile-log guile-log-macro? log-code-macro log-code-macro?)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
17

18 19 20 21 22

(define *cc* (gp-make-fluid))
(gp-fluid-set! *cc* #f)

(define (<stall> p cc)
23 24
  (set! *cc* (cons p cc))
  'stalled)
25

26
(define (<take> n) (<continue> n))
27 28
(define <continue>
  (case-lambda 
29 30 31 32 33 34
    (()  (let ((*cc* (pk (gp-fluid-ref *cc*))))
           (if (and *cc* (car *cc*))
               ((cdr *cc*) (car *cc*))
               #f)))
    ((n) (let ((*cc* (gp-fluid-ref *cc*)))
           (if (and *cc* (integer? n) (not (car *cc*)))
35
             ((cdr *cc*) n)
36
             'cannot-continue-and-take-n)))))
37

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
38 39
(define-syntax <eval> 
  (syntax-rules ()
40 41
    ((_ (v ...) code fini cc)
     (let ((v (u-var!)) ...)
42 43 44 45
       (let ((fi fini)
             (c  cc))       
         (parse<> (fi last fi c)
           code))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
46
   
47
(define *gp-var-tr* (make-fluid 'v))
48
                                      
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
(define tr
  (case-lambda 
    ((x) (tr (fluid-ref *gp-var-tr*) x))
    ((pre x)               
     (define a '())
     (define n 0)
     (let loop ((x (u-scm x)))
       (match x
         ((x . l)
          (cons (loop x) (loop l)))
         ((? gp-var? x)
          (let* ((m (gp-var-number (gp-lookup x)))
                 (r (assoc m a)))
            (if r
                (cdr r)
                (let ((k (string->symbol 
                          (format #f "~a~a" pre n))))
                  (set! a (cons (cons m k) a))
                  (set! n (+ n 1))
                  k))))
         (else
          x))))))
71
  
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
72 73 74
(define-syntax <run>
  (syntax-rules (*)
    ((_ (v) code ...)
75 76
     (let ((ret '())
           (fr  (gp-newframe)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
77 78
       (<eval> (v)
         (<and> code ...)
79
         (lambda x 
80
           (gp-unwind fr)
81
           (reverse ret))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
82
         (lambda (p)
83
           (set! ret (cons (tr (u-scm v)) ret))
84
           (u-abort p)))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
85 86

    ((_ (v ...) code ...)
87
     (let ((ret '()) (fr (gp-newframe)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
88 89
       (<eval> (v ...)
               (<and> code ...)
90
               (lambda x 
91 92 93 94
                 (let ((r ret))
                   (set! ret '())
                   (gp-unwind fr)
                   (reverse r)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
95
               (lambda (p)
96
                 (set! ret (cons (tr (u-scm (list v ...))) ret))
97 98
                 (u-abort p)))))
               
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
99 100 101 102 103
    
    ((_ * . l)  (<run> . l))

    ((_ m (v) code ...)
     (let ((n    m)
104
           (fr   (gp-newframe))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
105 106 107
           (ret '()))
       (<eval> (v)
         (<and> code ...)
108
         (lambda x 
109 110 111 112
           (let ((r ret))
             (set! ret '())
             (gp-unwind fr)
             (reverse r)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
113
         (lambda (p)
114 115 116 117 118 119 120
           (if (= n 0)
               (let ((r (reverse ret)))
                 (set! ret '())
                 (gp-set! *cc* (cons #f (lambda (mm) 
                                          (set! n mm)
                                          (u-abort p))))
                 r)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
121
               (begin
122
                 (set! ret (cons (tr (u-scm v)) ret))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
123 124
                 (set! n (- n 1))
                 (if (= n 0)
125 126 127 128 129 130 131
                     (let ((r (reverse ret)))
                       (set! ret '())
                       (gp-set! *cc* (cons #f (lambda (mm) 
                                                (set! n mm)
                                                (u-abort p))))
                       r)
                     (u-abort p))))))))
132
                 
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
133 134

    ((_ m (v ...) code ...)
135
     (let ((n m) (ret '()) (fr (gp-newframe)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
136 137
       (<eval> (v ...)
               (<and> code ...)
138
               (lambda  x 
139 140 141 142
                 (let ((r ret))
                   (set! ret '())
                   (gp-unwind fr)
                   (reverse r)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
143 144 145
               (lambda (p)
                 (if (> n 0)
                     (begin
146
                       (set! ret (cons (tr (u-scm (list v ...))) ret))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
147 148
                       (set! n (- n 1))
                       (if (= n 0)
149 150 151
                           (let ((r (reverse ret)))
                             (set! ret '())
                             (gp-set! *cc* 
152 153
                                      (cons #f (lambda (mm) 
                                                 (set! n mm)
154 155
                                                 (u-abort p))))
                             r)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
156
                           (u-abort p)))
157 158
                     (reverse ret))))))))
               
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  
                       

(define-syntax prompt=>
 (syntax-rules ()
   ((_ (Pr W) Code ...)
    (let ((Pr (u-prompt W)))
      Code ...))))

(define-syntax mk-syms
  (lambda (x)
    (syntax-case x ()
      ((q . l)
       ;(pk (syntax->datum (syntax l)))
       (syntax (mk-syms* . l))))))
       
(define-syntax mk-syms*
  (lambda (x)
    (syntax-case x ()
      ((q 1  (a as ...) rs  l)
       (syntax (mk-syms 10 (as ...) rs l)))
      ((q 10 (a as ...) rs  l)
       (with-syntax ((sym (datum->syntax (syntax q) (gensym "arg"))))
            (syntax (mk-syms 10 (as ...) (sym . rs)  l))))
      ((q 11 (a) rs (f b ...))
       (syntax (f rs b ...)))
      ((q 11 (a as ...) rs  l)
       (with-syntax ((sym (datum->syntax (syntax q) (gensym "pr"))))
            (syntax (mk-syms 11 (as ...) (sym . rs)  l))))
      ((q 2 (a as ...) rs  l)
       (with-syntax ((sym-p (datum->syntax (syntax q) (gensym "pr"))))
            (syntax (mk-syms 2 (as ...) (sym-p . rs)  l))))
      ((q 4 (a as ...) rs  l)
       (with-syntax ((sym-p1 (datum->syntax (syntax q) (gensym "pr" )))
                     (sym-w2 (datum->syntax (syntax q) (gensym "tag")))
                     (sym-p2 (datum->syntax (syntax q) (gensym "pr" ))))
            (syntax (mk-syms 4 (as ...) ((sym-p1 sym-w2 sym-p2) . rs)  l))))
      ((q 2 () rs (f a ...))
       (with-syntax ((sym (datum->syntax (syntax q) (gensym "w"))))
                    (syntax (f w rs a ...))))
      ((q 4 () rs (f a ...))
       (with-syntax ((sym (datum->syntax (syntax q) (gensym "w"))))
                    (syntax (f w rs a ...))))
      ((q n () rs (f a ...))
       (syntax (f rs a ...))))))

(define-syntax %semi-turn%
  (syntax-rules ()
    ((_ w (pr2 ...) (cut fi pr cc) (ee)    (e ...))
     (umatch #:tag w #:name '<or> () 
             ((prompt=> (pr2 w) (parse<> (cut w pr2 cc)  e)))
             ...
             ((parse<> (cut fi pr cc) ee))))
     
    ((_ w prs meta (x . l) (y ...)) 
     (%semi-turn% w prs meta l (y ... x)))))


(define-guile-log  <or>
  (syntax-rules ()
219
    ((_ meta   ) (parse<> meta <fail>))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233
    ((_ meta e1) (parse<> meta e1))
    ((_ meta e1 es ...) 
     (mk-syms 2 (es ...) () (%semi-turn% meta (e1 es ...) ())))))

(define-guile-log <and>
  (lambda (x)
    (syntax-case x (last)
      ((_ w)
       #'(parse<> w <cc>)) 

      ((_ meta       e1       ) 
       #'(parse<> meta e1))

      ((_ (cut fi pr cc) (a b ...) e2 ...)
234
       (log-code-macro? (syntax a))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
       #'(a (cut fi pr (parse<> (cut fi pr cc) (<and> e2 ...))) b ...))

      ((_ (cut fi pr cc) e1 e2 ...)  
       #'(let ((ccc (lambda (pr2) (<and> (cut last pr2 cc) e2 ...))))
           (parse<> (cut fi pr ccc) e1))))))

;;This is inspired from kanren
;; (<and!>  a ... ) gives exactly one answer from (and a ....)
;; (<and!!> a ... ) is the same as (and (and! a) ...)
;; this can be modeled by a cut in a less elegant way.

(define-guile-log <and!>
  (syntax-rules ()
    ((_ w) (parse<> w <cc>))
    ((_ (cut fi pr cc) a ...) 
     (let ((ccc (lambda (Pr) (cc pr))))
       (parse<> (cut fi pr ccc) (<and> a ...))))))


(define-guile-log <and!!>
  (syntax-rules ()
256 257 258 259 260
    ((_ meta) 
     (parse<> meta <cc>))

    ((_ meta a ...) 
     (parse<> meta (<and> (<and!> a) ...)))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
261 262 263 264

;; this will try to make a success and if so reset the state and continue it's a 
;; companion to <not>.

265
(define-guile-log <succeds>
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  (syntax-rules ()
    ((_ (cut fi pr cc) g)
     (let* ((P   (gp-newframe))
            (ccc (lambda (Pr) (gp-unwind P) (cc pr))))        
       (parse<> (cut fi pr ccc) g)))))



(define-syntax <%fkn%>
  (syntax-rules (last)
    ((_ (cut last pr cc) f a ...)  (f pr cc a ... ))
    ((_ (cut w    pr cc) f a ...)  (u-call  w f pr cc a ... ))))


(define-guile-log <with-fail>
  (syntax-rules ()
    ((_ (cut fi p cc) pp code ...)
283 284 285 286 287 288 289 290 291 292 293 294
     (let ((p pp))
       (parse<> (cut fi p cc) (<and> code ...))))))

(define-guile-log <with-cut>
  (syntax-rules ()
    ((_ (cut fi p cc) cutt code ...)
     (let ((c cutt))
       (parse<> (c fi p cc) (<and> code ...))))))

(define-guile-log <peek-fail>
  (syntax-rules ()
    ((_ (cut fi p cc) pp code ...)
295
     (let ((pp p))
296
       (parse<> (cut fi p cc) (<and> code ...))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
297

298

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
299 300 301 302 303 304 305 306 307 308
(define-guile-log <if>
  (syntax-rules ()
    ((_ meta p a)
     (parse<> meta (<and> (<and!> p) a)))
    ((_ (cut fi p cc) pred a b)     
     (umatch #:tag w #:name '<if> ()
             ((prompt=> (pr w)
                (parse<> (cut w pr cc)
                  (<and> (<and!> pred) (<with-fail> p a)))))
             ((parse<> (cut fi p cc) b))))))
309 310 311 312 313 314 315

(define-guile-log <if-some>
  (syntax-rules ()
    ((_ meta p a)
     (<and> p a))
    ((_ meta p a b)
     (parse<> meta (<or> (<and> p a) (<and> (<not> p) b))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
316 317 318
                         

(define-guile-log <cond>
319 320
  (syntax-rules (else)
    ((_ meta (else b)) 
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
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 389
     (parse<> meta b))
    ((_ meta (a b))  
     (parse<> meta (<and> a b)))
    ((_ meta (a b) ab ...)     
     (parse<> meta (<if> a b (<cond> ab ...))))))
        
(define-guile-log <var>
  (syntax-rules ()
    ((_ meta (v ...) code ...) 
     (let ((v (u-var!)) ...) (parse<> meta (<and> code ...))))))


(define-syntax <%let%>
  (syntax-rules ()
    ((_ meta (v ...) code ...) (let (v  ...) 
                                 (parse<> meta (<and> code ...))))))

(define-syntax <%let*%>
  (syntax-rules ()
    ((_ meta (v ...) code ...) (let* (v  ...) 
                                 (parse<> meta (<and> code ...))))))

(define-syntax <%cc%>
  (syntax-rules (last parse<>)
    ((_ (cut w    p (parse<> . l))) (parse<> . l))
    ((_ (cut last p cc))   (cc p)           )
    ((_ (cut w    p cc))   (u-call w cc p)  )))

(define-syntax <%cut%>
  (syntax-rules ()
    ((_ (cut w p cc)           )   
     (parse<> (cut last cut cc) <cc>))
    ((_ (cut w p cc) code ...  )   
     (parse<> (cut last cut cc) (<and> code ... )))))

(define-syntax <%not%>
  (syntax-rules ()
    ((_ (cut w p cc) code)
     (umatch #:tag q-not #:name '<not> ()
             ((prompt=> (pr q-not) 
                        (parse<> (pr q-not pr (lambda (x) (u-abort p))) code)))
             ((parse<> (cut w p cc) <cc>))))))
                        

(define-syntax <%fail%>
  (syntax-rules ()
    ((_ _         p)  (u-abort p))
    ((_ (_ _ p _)  )  (u-abort p))))

(define-guile-log <set-cut>
  (syntax-rules ()
    ((_ (cut w p (_ (_ _ _ cc) <cc>))) (parse<> (p w p cc) <cc>))))

(log-code-macro '<set-cut>)

(define-guile-log <when>
  (syntax-rules ()
    ((_ wc p)
     (if p 
         (parse<> wc <cc>) 
         (parse<> wc <fail>)))
    
    ((_ wc p code ...)
     (if p 
         (parse<> wc (<and> code ...))
         (parse<> wc <fail>)))))

(log-code-macro '<when>)

390 391 392 393
(define (pp x)
  (pretty-print x)
  x)

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
(define-guile-log <pp>
  (syntax-rules ()
    ((_ wc x) (begin (pp (u-scm x))
                  (parse<> wc <cc>)))))
(log-code-macro '<pp>)

(define-guile-log <pp-dyn>
  (syntax-rules ()
    ((_ wc a b)   
     (u-dynwind
      (lambda () (pp (u-scm a)))
      (lambda () (parse<> wc <cc>))
      (lambda () (pp (u-scm b)))))))

(define-guile-log <format>
  (syntax-rules ()
    ((_ wc s str a ...)
     (begin 
       (format s str (u-scm a) ...)
       (parse<> wc <cc>)))))
(log-code-macro '<format>)

(define-guile-log <tail-code>
  (syntax-rules ()
    ((_ (_ _ p cc) (pp ccc) code ...)
     (let ((pp p) (ccc cc)) code ...))))

(define-guile-log <code>
  (syntax-rules ()
    ((_ wc code ...)
     (begin code ... (parse<> wc <cc>)))))
(log-code-macro '<code>)

(define-guile-log <return>
  (syntax-rules ()
    ((_ wc code ...)
     (begin code ...))))

(log-code-macro '<code>)

(define-syntax parse<>
  (lambda (x)
    (syntax-case x ()
      ((_ . l)
       ;(pp `(parse<> ,@(syntax->datum (syntax l))))
       (syntax (parse2<> . l))))))

(define-syntax parse2<>
  (syntax-rules (     <not> if
                      <ret> <fail> <cc> <pp> 
                      <let> <let*> <cut>
                      <repl-vars> last)                       
    ((_ meta (<not>      X   ))   (<%not%>  meta   X   ))    
    ((_ meta (<ret> code)    ) (u-scm code))
    ((_ meta  <fail>         ) (<%fail%> meta))
    ((_ meta (<fail> Pr)     ) (<%fail%> meta Pr))
    ((_ meta  <cc>           ) (<%cc%> meta))
    ((_ meta (if p . l)      ) (<if> meta (<when> p) . l))
    ((_ meta  <cut>          ) (<%cut%>  meta      ))
    ((_ meta (<cut>   l ...) ) (<%cut%>  meta l ...))
    ((_ meta (<let>   l ...) ) (<%let%>  meta l ...))
    ((_ meta (<let*>  l ...) ) (<%let*%> meta l ...))
    ;;Be nice to function calls
    ((_ meta (f ...)         ) (dispatch meta (f ...)))))

(define-syntax dispatch
  (lambda (x)
    (syntax-case x ()
      ((_ w (n . l))       
       ;(pk (syntax->datum (syntax (n w . l))))
464
       (if (guile-log-macro? (syntax n))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
465 466 467 468 469 470 471 472
           (syntax (n w . l))
           (syntax (<%fkn%> w n . l)))))))

(define-syntax <with-prolog-environment>
  (syntax-rules (last)
    ((_ (      p cc) code) (parse<> (p   last p cc) code))
    ((_ (cut w p cc) code) (parse<> (cut w    p cc) code))))

473 474 475 476
(define-syntax <with-guile-log>
  (syntax-rules (last)
    ((_ (    p cc) code ...) (parse<> (p   last p cc) (<and> code ...)))
    ((_ (cut p cc) code ...) (parse<> (cut last p cc) (<and> code ...)))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
477 478 479 480 481

(define <next> (lambda x x))

(define-syntax <ask>
  (syntax-rules ()
482
    ((_ code ...)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
483 484
     (umatch #:tag <next> #:name '<ask> ()
             ((prompt=> (pr <next>)
485 486
                        (parse<> (pr <next> pr (lambda x #t)) 
                          (<and> code ...))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
             (#f)))))

(define-syntax <define>
  (syntax-rules ()
    ((_ (name a ...) code ...)
     (define (name <Cut> <CC> a ...)
       (<with-prolog-environment> 
           (<Cut> last <Cut> <CC>) 
         (<and> code ...))))))

(define *depth* (make-fluid))

(define-guile-log <match>
  (syntax-rules ()
    ((_ wc as ...)
     (match0 (+) wc as ...)
     #;(mk-syms 1 v () (find-last0 v wc (as ...))))))

(define-syntax match0
  (syntax-rules ()
    ((_ (m) wc #:mode m0 . l)
     (match0 (m0) wc . l))
    ((_ meta wc v . l)
     (mk-syms 1 v () (find-last0 v wc meta l)))))

(define-syntax <lambda>
  (syntax-rules ()
    ((_ (a ...) code ...)
     (lambda (<Cut> <CC> a ... )
       (<with-prolog-environment> (<Cut> last <Cut> <CC>) 
         (<and> code ...))))
    ((_ a code ...)
     (lambda (<Cut> <CC> . a)
       (<with-prolog-environment> (<Cut> last <Cut> <CC>) 
         (<and> code ...))))))


(define-guile-log <recur>
  (syntax-rules ()
    ((_ (cut q p cc) n ((w v) ...) code ...)
     (letrec ((n (lambda (pp cccc w ...)
                   (parse<> (pp q pp cccc) 
                     (<and> code ...)))))
       (n p cc v ...)))))

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
532
(define-guile-log <letrec>
533 534 535 536 537
  (syntax-rules ()
    ((_ (cut q p cc) ((v . lam) ...) code ...)
     (letrec ((v . lam) ...)
       (parse<> (cut q p cc)
         (<and> code ...))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 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 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

(define-syntax find-last0
  (syntax-rules ()
    ((_ args v wc me as)
     (mk-syms 11 as () (find-last args v () as wc me)))))

(define-syntax find-last
  (lambda (x)
    (syntax-case x ()
      ((_ . l) 
       ;(pk `(find-last ,@(syntax->datum #'l)))
       (syntax (find-last* . l))))))

(define-syntax find-last*
  (lambda (x)
    (syntax-case x ()      
      ((_  pr       args       v ()   ((a)        ...) wc me)
       (syntax (find-last** me pr args v  ((a) ...)  ()   wc)))

      ((_  pr       args       v ()   ((a as ...) ...) wc me)
       (syntax (find-last* pr       args    v ((a) ...) 
                           ((  as ...) ...) wc me)))
      
      ((_  pr        args      v ((m ...) ...) ((e       ) ...) wc me)
       (syntax (find-last** me pr args v ((m ...) ...) () 
                            ((e) ...) () wc)))

      ((_ pr args v ((b ...  ) ...) ((a as ...) ... ) wc me)
       (syntax (find-last* pr args v ((b ... a) ...) 
                           ((as   ...) ... ) wc me))))))

(define-syntax find-last**
  (syntax-rules ()
    ((_ (m) (pr ...) args v ((a)) ((b) ...) (cut w p cc))
     (umatch #:mode m #:tag <next> #:name '<match> 
             ()
             ((prompt=> (pr <next>)
                        (<with-prolog-environment> (cut <next> pr cc) b)))
             ...
             ((<with-prolog-environment> (cut w p cc) a))))

    ((_ m pr args v (a aa ...) (b ...) wc)
     (find-last** m pr args v (aa ...) (b ... a) wc))


    ((_ (m) (pr ...) args v ((as ...)) ((aas ...)  ...) ((a)) ((b) ...)  
        (cut w p cc))
     (umatch #:mode m #:tag <next> #:name '<match>
             v
             (aas ... (prompt=> (pr <next>)
                        (<with-prolog-environment> (cut <next> pr cc) b)))
             ...
             
             (as ...  (<with-prolog-environment> (cut w p cc) a))))

    ((_ m pr args v ((as ...) (aas ...) ...) ((aass ...)  ...) ((a) (aa) ...) 
        ((b) ...)  wc)
     (find-last** m pr args v ((aas ...) ...) ((aass ...) ... (as ...)) 
                  ((aa) ...) ((b) ... (a)) wc))))



          
(define-syntax let<>0
  (syntax-rules ()
    ((_ wc (m pat val) code)
     (umatch #:mode m #:name 'let<>/<=>/<==> (val)
             (pat (parse<> wc code  )) 
             (_   (parse<> wc <fail>))))))

(define-guile-log let<>
  (syntax-rules ()
    ((_ w (a b ...) code ...)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
611
     (let<>0 w a (let<> w (b ...) code ...)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    ((_ w () code ...)
     (parse<> w (<and> code ...)))))

(define-syntax letify
  (lambda (x)
    (syntax-case x ()
      ((_ w ((m f) pat val) code ...)
       #`(let<>0 w (m #,(tr-pat #'pat) (f val)) code ...))

      ((_ w (m pat val) code ...)
       #`(let<>0 w (m #,(tr-pat #'pat) val) code ...)))))

(define (tr-pat x)
  (syntax-case x (quote unquote)
    ((quote   _) x)
    ((unquote _) x)
    ((x . l)     #`(#,(tr-pat #'x) #,@(tr-pat #'l)))
    (()          #'())
    ( x          #'(unquote x))))
        

;;TODO, this will unify with a cyclic check!
;;Not possible to use a pure raw form here
(define-guile-log <=>
  (syntax-rules ()
    ((_ wc X Y)
     (<=>q wc (u-unify! +) X Y))))
(log-code-macro '<=>)

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
641 642 643 644 645 646
(define-guile-log <r=>
  (syntax-rules ()
    ((_ wc X Y)
     (<=>q wc (gp-unify-raw! ++) X Y))))
(log-code-macro '<=>)

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
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
(define-guile-log <==>
  (syntax-rules ()
    ((_ wc X Y)
     (<=>q wc (gp-m-unify! -) X Y))))
(log-code-macro '<==>)


(define-guile-log <=>q
  (syntax-rules (unquote quote _)

    ((_ wc q _   x)
     (parse<> wc <cc>))

    ((_ wc q x   _)
     (parse<> wc <cc>))

    ((_ wc q ()  ()) 
     (parse<> wc <cc>))
    
    ((_ wc q ()  X)
     (<=>q wc q '() X))

    ((_ wc q X   ())
     (<=>q wc q '() X))

    ((_ wc q (X) (Y))
     (<=>q wc q X Y))

    ;;unquote logic so that we can evaluate forms in the unification
    ((_ wc (unify m) (unquote X)  (unquote Y))
     (<when> wc (unify X Y) <cc>))

    ((_ wc (unify m) (unquote X)  (quote   Y))
     (<when> wc (unify X (quote Y)) <cc>))

    ((_ wc (u m)     (unquote X)  (Y . L)    )   
     (letify wc (m (Y . L) X) <cc>))

    ((_ wc (unify m) (unquote X)  Y          )
     (<when> wc (unify X Y) <cc>))

    ((_ wc q Y            (unquote X))   
     (<=>q wc q (unquote X) Y))

    ;; quote logic to protect quoted forms from destructioning
    ((_ wc (unify m) (quote X)  (quote Y))
     (<when> wc (unify (quote X) (quote Y)) <cc>))

    ((_ wc (u m)     (quote X)  (Y . L)    ) 
     (letify wc (m (Y . L) (quote X)) <cc>))

    ((_ wc (unify m) (quote X)  Y          )  
     (<when> wc (unify (quote X) Y) <cc>))

    ((_ wc q Y         (quote X))  
     (<=>q wc q (quote X) Y))


    ((_ wc q     (X . Lx) (Y . Ly))
     (parse<> wc (<and> (<=>q q X Y) (<=>q q Lx Ly))))

    ((_ wc (u m)     X        (Y . Ly))
     (letify wc (m (Y . Ly) X) <cc>))

    ((_ wc (u m)     (X . Lx)    Y)
     (letify wc (m (X . Lx) Y) <cc>))

    ((_ wc (unify m) X Y)
     (<when> wc (unify X Y) <cc>))))

(define-guile-log </.>
  (syntax-rules ()
    ((_ code ...) (<lambda> () code ...))))
 


723
(define (<funcall> P CC F . L) (apply (u-scm F) `(,P ,CC ,@L)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

(define-syntax <def>
  (syntax-rules ()
    ((_ (f #:mode mode a ...) m ... (me ... codee))
     (<define> (f a ...)
         (<match> #:mode mode (a ...)
           m ... 
           (me ... (<cut> codee)))))

    ((_ (f a ...) m ... (me ... codee))
     (<define> (f a ...)
         (<match> (a ...)
           m ... 
           (me ... (<cut> codee)))))))

(define-syntax <<define>>
  (syntax-rules ()
    ((_ (#:mode mode f a ...) (m ... code) ...)
     (<define> (f a ...)
         (<match> #:mode mode (a ...)
            (m  ... (<cut> code)) ...)))

    ((_ (f a ...) (m ... code) ...)
     (<define> (f a ...)
         (<match> (a ...)
           (m  ... (<cut> code)) ...)))))

751 752 753 754 755 756 757 758 759 760 761

(define-guile-log <dynwind>
  (syntax-rules ()
    ((_ w redo undo)
     (u-dynwind
      redo
      (lambda () (parse<> w <cc>))
      undo))))
(log-code-macro '<dynwind>)


Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
762 763
(include-from-path "logic/guile-log/interleave.scm")  			    

764
 
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
;; This is code that allow to store a state

(define (<state-ref>)
  (let ((ret (cons (gp-store-state)
                   (begin
                     (gp-swap-to-b)
                     (gp-store-state)))))
  (gp-swap-to-a)
  ret))

(define (<state-set!> state)
  (gp-restore-state (car state))
  (gp-swap-to-b)
  (gp-restore-state (cdr state))
  (gp-swap-to-a)
  (if #f #f))
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795

(define-guile-log <lv*>
  (syntax-rules ()
    ((_ meta (((v ...) (f ...)) c ...) code ...)
     (parse<> meta 
       (<and> (f ... v ...)
              (<lv*>  (c ...) code ...))))
    ((_ meta () code ...)
     (parse<> meta (<and> code ...)))))

(define (<clear>)
  (gp-swap-to-b)
  (gp-clear)
  (gp-swap-to-a)
  (gp-clear))