macros.scm 18.5 KB
Newer Older
1 2
(export <next> <or> <and> <not> <bag-of> <cond> <if> 
        <with-guile-log> <eval>
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>
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
8
        <and!> <and!!> <succeeds> 
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
9
        <format> <tail-code> <code> <ret> <return>
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
10
        <def> <<define>> <with-fail>
11
        let<> <or!>)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
12 13 14 15 16



(define-syntax <eval> 
  (syntax-rules ()
17 18 19 20
    ((_ (v ...) code fini cc)
     (let ((v (u-var!)) ...)
       (parse<> (fini last fini cc)
         code)))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
21 22 23 24 25 26 27 28
   
                                        
(define-syntax <run>
  (syntax-rules (*)
    ((_ (v) code ...)
     (let ((ret '()))
       (<eval> (v)
         (<and> code ...)
29 30
         (lambda x 
           (reverse ret))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
31 32
         (lambda (p)
           (set! ret (cons (u-scm v) ret))
33
           (u-abort p)))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
34 35 36 37 38

    ((_ (v ...) code ...)
     (let ((ret '()))
       (<eval> (v ...)
               (<and> code ...)
39
               (lambda x (reverse ret))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
40 41
               (lambda (p)
                 (set! ret (cons (u-scm (list v ...)) ret))
42 43
                 (u-abort p)))))
               
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
44 45 46 47 48 49 50 51
    
    ((_ * . l)  (<run> . l))

    ((_ m (v) code ...)
     (let ((n    m)
           (ret '()))
       (<eval> (v)
         (<and> code ...)
52
         (lambda x (reverse ret))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
53 54 55 56 57 58 59 60
         (lambda (p)
           (if (> n 0)
               (begin
                 (set! ret (cons (u-scm v) ret))
                 (set! n (- n 1))
                 (if (= n 0)
                     (reverse ret)
                     (u-abort p)))
61
               (reverse ret))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
62 63 64 65 66

    ((_ m (v ...) code ...)
     (let ((n m) (ret '()))
       (<eval> (v ...)
               (<and> code ...)
67
               (lambda  x (reverse ret))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
68 69 70 71 72 73 74 75
               (lambda (p)
                 (if (> n 0)
                     (begin
                       (set! ret (cons (u-scm (list v ...)) ret))
                       (set! n (- n 1))
                       (if (= n 0)
                           (reverse ret)
                           (u-abort p)))
76 77
                     (reverse ret))))))))
               
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
78 79 80 81 82 83 84 85 86 87 88 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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
  
                       

(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 ()
    ((_ 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 ...)
       (log-code-macro? (syntax->datum (syntax a)))
       #'(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-syntax and!!
  (syntax-rules ()
    ((_ (cut fi pr cc) ccc a)
     (parse<> (cut fi pr (ccc cc)) a))
    ((_ (cut fi pr cc) ccc a as ...)
     (let ((ccc (lambda (pr) (and!! (cut fi pr cc) ccc as ...))))
       (parse<> (cut fi pr ccc) a)))))

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

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

(define-guile-log <succeeds>
  (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 ...)
209 210 211 212 213 214 215 216 217 218 219 220 221 222
     (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 ...)
     (let (pp p)
       (parse<> (cut fi p cc) (<and> code ...))))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 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

(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))))))
                         

(define-guile-log <cond>
  (syntax-rules ()
    ((_ meta (#t b)) 
     (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>)

(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))))
       (if (guile-log-macro? (syntax->datum (syntax n)))
           (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))))

387 388 389 390
(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
391 392 393 394 395

(define <next> (lambda x x))

(define-syntax <ask>
  (syntax-rules ()
396
    ((_ code ...)
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
397 398
     (umatch #:tag <next> #:name '<ask> ()
             ((prompt=> (pr <next>)
399 400
                        (parse<> (pr <next> pr (lambda x #t)) 
                          (<and> code ...))))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
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
             (#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
446 447 448 449 450
(define-guile-log <letrec>
  ((_ (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
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 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

(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
524
     (let<>0 w a (let<> w (b ...) code ...)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    ((_ 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
554 555 556 557 558 559
(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
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 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
(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 ...))))
 


636
(define (<funcall> P CC F . L) (apply (u-scm F) `(,P ,CC ,@L)))
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
637 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

(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)) ...)))))

(include-from-path "logic/guile-log/interleave.scm")