goal-transformers.scm 35.9 KB
Newer Older
1
(define-module (logic guile-log prolog goal-transformers)
2
  #:use-module (guile-user)
3
  #:use-module (logic guile-log prolog operators)
4
  #:use-module (logic guile-log prolog pre)
Stefan Israelsson Tampe committed
5
  #:use-module (logic guile-log prolog parser)
6
  #:use-module (logic guile-log tools)
7
  #:use-module (logic guile-log iinterleave)
8
  #:use-module (logic guile-log soft-cut)
9
  #:use-module (logic guile-log prolog error)
Stefan Israelsson Tampe committed
10
  #:use-module (logic guile-log prolog directives)
11
  #:use-module (logic guile-log prolog goal)
12
  #:use-module (logic guile-log prolog modules)
13
  #:use-module (logic guile-log prolog goal-functors)
Stefan Israelsson Tampe committed
14
  #:use-module (logic guile-log prolog varstat)
15 16 17 18 19 20 21 22 23
  #:use-module ((logic guile-log prolog var)
		#:renamer (lambda (x)
			    (cond
			     ((eq? x 'arg)
			      '*arg*)
			     ((eq? x 'var)
			      '*var*)
			     (else x))))

24
  #:use-module (logic guile-log functional-database)
25 26
  #:use-module (logic guile-log prolog order)
  #:use-module (logic guile-log ck)
27
  #:use-module (logic guile-log umatch)
28
  #:use-module (logic guile-log prolog variant)
29 30
  #:use-module ((logic guile-log) 
		#:renamer  (lambda (x)
31 32 33 34 35 36
			     (cond
			      ((eq? x '<_>)
			       'GL:_)
			      ((eq? x 'umatch)
			       'GL:umatch)
			      (else x))))
37

Stefan Israelsson Tampe committed
38
  #:use-module (logic guile-log prolog names)
39
  #:use-module (logic guile-log prolog namespace)
40
  #:use-module (ice-9 match)
41
  #:use-module (ice-9 pretty-print)
42
  #:replace (catch throw)
43
  #:export (     unify_with_occurs_check copy_term functor arg
44 45 46
		 var atomic compound nonvar
		 directive
		 procedure_name
47
                  once once_i *once* once-f
48
                  -var -atom 
Stefan Israelsson Tampe committed
49
                  halt
50
                  
51
		  uniq yield_at_change
52

53
		  ^ :- #{,}# #{,,}# -> #{\\+}# op2= == =@= | -i> *->
54
		  #{\\=}# #{\\==}# #{\\=@=}# @< @> @>= @=< is op2<=
55
		  op2+ op2- op1- op1+  #{\\}# op2* op2/ // rem mod div
56
		  ** << >> #{/\\}# #{\\/}# op2< op2> op2>= op2=< =:= #{=\\=}#
57
		  =.. --> ? $ ?-   =>
Stefan Israelsson Tampe committed
58

59
		  gop2+ gop2- gop1+ gop1-
60
		  #{g\\}# gop2* gop2/ g// gop2rem gop2mod
61 62
		  g** g^
		  g<< g>> #{g/\\}# #{g\\/}# gop2< gop2> gop2>= gop2=< g=:= 
63
		  #{g=\\=}#
64

65
		  #{;}#	#{;;}#
Stefan Israelsson Tampe committed
66
		    mk-scheme-biop
67
		    mk-scheme-unop
Stefan Israelsson Tampe committed
68 69 70
		    mk-prolog-biop-tr
		    mk-prolog-biop
		    s a
71
                  )
Stefan Israelsson Tampe committed
72
	 #:re-export(sin cos atan exp log sqrt))
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#|
We could make all variable references through a stack frame e.g.
(m A),(m B)

(lambda (s p cc a b)
  ((ref a 1) s p 
   (lambda (ss pp)
     ((ref a 2) ss pp cc (ref a 3)))
   (ref a 4)))

(define (m code)
  (lambda (b)
    (lambda (s p c cc a)
      (exec a b c code))))

(define (, x y)
  (lambda (b)
    (let ((xx (x b))
	  (yy (y b)))
      (lambda (s p c cc a)
	(xx s p (lambda (ss pp) (yy ss pp cc a)) a)))))

(define (or x y)
  (lambda (b)
    (let ((xx (x b))
	  (yy (y b)))
      (lambda (s p c cc a)
100
	(let ((fr (gp-newframe-choice s)))
101
	  (xx s (lambda ()
102
		  (gp-unwind-tail fr)
103 104 105 106 107 108 109 110
		  (yy s p c cc a)) c cc a))))))

(define (if p x y)
  (lambda (b)
    (let ((pp (p b))
	  (xx (x b))
	  (yy (y b)))
      (lambda (s p c cc a)
111
	(let ((fr (gp-newframe-choice s)))
112
	  (pp s (lambda ()
113
		  (gp-unwind-tail fr)
114 115 116
		  (yy s p c cc a)) 
	      c 
	      (lambda (ss pp)
117
		(gp-prune-tail fr)
118 119 120
		(xx ss p c cc a))))))))
		
|#
121

122 123 124
(define (transfer-o-p from to)
  (set-object-properties! to (object-properties from)))

125
(define do-print #f)
126 127 128 129 130 131 132 133 134 135 136
(define pp
  (case-lambda
   ((s x)
    (when do-print
      (pretty-print `(,s ,(syntax->datum x))))
    x)
   ((x)
    (when do-print
      (pretty-print (syntax->datum x)))
    x)))

137 138 139 140 141 142 143
(define-syntax-rule (define-syntax-rule-pk (f . a) c)
  (define-syntax f
    (lambda (x)
      (syntax-case x ()
	((_ . a) #'c)))))

(define-syntax-rule (define-quote-rule quote (f . x) code)
144 145 146 147 148
  (define-syntax f
    (lambda (y)
      ;(pk (syntax->datum y))
      (syntax-case y (quote)
        ((_ . x) #'code)))))
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  

(<define-guile-log-rule> (<1> nm x) 
  (<and> (nm x)))

(<define-guile-log-rule> (<2> nm x y) 
  (<and> (nm x y)))

(<define-guile-log-rule> (<is2> nm x y) 
  (<and> (nm x ,y)))

(<define-guile-log-rule> (<not-2> nm x y) 
  (<not> (nm x y)))

(<define-guile-log-rule> (<when-2> nm x y)
164
  (<and> (when (nm (<lookup> x) (<lookup> y)))))
165 166

(<define-guile-log-rule> (<when-not-2> nm x y)
167
  (<and> (when (not (nm (<lookup> x) (<lookup> y))))))
168 169 170 171 172

(define-syntax -id-
  (syntax-rules (quote)
    ((_ s 'x)  (ck s 'x))))

Stefan Israelsson Tampe committed
173 174 175 176
(define-syntax -ia-
  (syntax-rules (quote)
    ((_ s 'x)  (ck s 'x))))

177 178 179 180 181 182 183 184
(define-syntax -goal-eval-
  (syntax-rules (quote)
    ((_ s 'x)  (ck s '(goal-eval x)))))

(define-syntax -scm-eval-
  (syntax-rules (quote)
    ((_ s 'x)  (ck s '(scm-eval x)))))

185 186 187 188
(define-syntax -fff-eval-
  (syntax-rules (quote)
    ((_ s 'x)  (ck s '(fff-eval x)))))

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
(define-syntax-rule (-2- nm x y) (nm x y))
(define-syntax-rule (-1- nm x)   (nm x))

(define-syntax delay-ck
  (syntax-rules (quote)
    ((_ s 'f 'w 'x ...)
     (ck s '(f w x ...)))))


(define-syntax-rule (mk-kind nm x1 x2)
  (define-guile-log nm
    (lambda (x)
      ;(pk (syntax->datum x))
      (syntax-case x (quote)
	((_ (a (... ...)) 'x)
	 #'(x2 (a (... ...)) 'x))
	((_ stx x)
	 #'(x1 stx x))
	((_ x)
	 #'(x2 x))))))

Stefan Israelsson Tampe committed
210
(define (-arg- stx x)
211
  #``#,(*arg* stx x))
212

213 214 215 216 217
(mk-kind g  goal   -goal-eval-)
(mk-kind v  *var*  -id-)
(mk-kind a  -arg-  -ia-)
(mk-kind s  scm    -scm-eval-)
(mk-kind ff fff    -fff-eval-)
218 219 220 221 222 223 224 225 226

(define-guile-log goal-ck
  (syntax-rules (quote)
    ((_ w 'f . l) (ck () (delay-ck 'f 'w . l)))))

(define-guile-log scm-ck
  (syntax-rules (quote)
    ((_ 'f . l) (ck () (delay-ck 'f . l)))))

227
(define-syntax-rule (GT x) `(@@ (logic guile-log prolog goal-transformers) x))
228

229 230 231
(define-syntax-rule (meta-mk-prolog-op mk-prolog stx nm-code 
				       (a ...) (tp ...) code)
  (define-syntax-rule (mk-prolog op-type op nm-tr nm-func nm-code tp ...)
232
    (begin      
233 234
      (define-goal-functor (nm-func a ...)
	(goal-ck 'code 'nm-code (tp 'a) ...))
235 236
      (set-object-property! nm-func 'goal-compile-stub 
                            (lambda (a ...)
237 238
                              `(,(GT code) ,(GT nm-code) 
				,a ...)))
239 240
      (set-object-property! nm-func 'goal-compile-types
                            '(tp ...))
241 242
      (define-goal-transformer nm-func (nm-tr stx n m a ...)
	#`(code nm-code #,(tp stx a) ...))
243
      (bind-operator-to-functor nm-func op))))
244

Stefan Israelsson Tampe committed
245 246 247
(define-syntax-rule (meta-mk-prolog-op-tr mk-prolog stx nm-code 
				       (a ...) (tp ...) code)
  (define-syntax-rule (mk-prolog op-type op nm-tr nm-func nm-code (tp ...) tr)
248
    (begin
Stefan Israelsson Tampe committed
249 250 251 252
      (define-goal-functor (nm-func a ...)
	(goal-ck 'code 'nm-code (tp 'a) ...))
      (set-object-property! nm-func 'goal-compile-stub 
                            (lambda (a ...)
253 254
                              `(,(GT code) ,(GT nm-code) 
				,a ...)))
Stefan Israelsson Tampe committed
255 256 257
      (set-object-property! nm-func 'goal-compile-types
                            '(tp ...))
      (define-goal-transformer nm-func . tr)
258
      (bind-operator-to-functor nm-func op))))
Stefan Israelsson Tampe committed
259

260 261
(meta-mk-prolog-op mk-prolog-biop stx nm-code
		   (x y) (tp1 tp2) <2>)
Stefan Israelsson Tampe committed
262 263 264 265

(meta-mk-prolog-op-tr mk-prolog-biop-tr stx nm-code
		   (x y) (tp1 tp2) <2>)

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
(meta-mk-prolog-op mk-prolog-unop stx nm-code
		   (x)   (tp1)     <1>)
(meta-mk-prolog-op mk-prolog-biop-not stx nm-code
		   (x y) (tp1 tp2) <not-2>)
(meta-mk-prolog-op mk-prolog-biop-when stx nm-code
		   (x y) (tp1 tp2) <when-2>)
(meta-mk-prolog-op mk-prolog-biop-when-not stx nm-code
		   (x y) (tp1 tp2) <when-not-2>)
; ---------------------
(define-syntax-rule (meta-mk-prolog-term
		     mk-prolog-term stx 
		     (a ...) (tp ...))
  (define-syntax-rule (mk-prolog-term nm-tr nm-func nm-code tp ...)
    (begin
      (define-goal-functor (nm-func a ...)
	(goal-ck 'nm-code (tp 'a) ...))
282 283
      (set-object-property! nm-func 'goal-compile-stub 
                            (lambda (a ...)
284
                              `(,(GT nm-code) ,a ...)))
285 286
      (set-object-property! nm-func 'goal-compile-types
                            '(tp ...))
287 288
      (define-goal-transformer nm-func (nm-tr stx n m a ...)
	#`(nm-code #,(tp stx a) ...)))))
289
        
290
(meta-mk-prolog-term mk-prolog-term-0 stx ()      ())
291 292 293
(meta-mk-prolog-term mk-prolog-term-1 stx (x)     (tp))
(meta-mk-prolog-term mk-prolog-term-2 stx (x y)   (tp-x tp-y))
(meta-mk-prolog-term mk-prolog-term-3 stx (x y z) (tp-x tp-y tp-z))
294 295 296 297
(meta-mk-prolog-term mk-prolog-term-4 stx (x y z w) 
		     (tp-x tp-y tp-z tp-w))
(meta-mk-prolog-term mk-prolog-term-5 stx (x y z w u) 
		     (tp-x tp-y tp-z tp-w tp-u))
298 299 300 301 302 303 304

; ------------------------
(define-syntax-rule (meta-mk-scheme-op mk-scheme stx nm-code 
				       (a ...) (tp ...) code)

  (define-syntax-rule (mk-scheme op-type op nm-tr nm-func nm-code tp ...)
    (begin
Stefan Israelsson Tampe committed
305
      (define-scm-functor (nm-func s a ...)
306
	(syntax-parameterize ((S (identifier-syntax s)))
Stefan Israelsson Tampe committed
307 308
	  (scm-ck 'code 'nm-code (tp 'a) ...)))
      (define-goal-transformer nm-func (nm-tr stx n m a ...)
309
	#`(code nm-code #,(tp stx a) ...))
310
      (bind-operator-to-functor nm-func op))))
311 312 313 314 315 316

(meta-mk-scheme-op mk-scheme-biop stx nm-code
		   (x y) (tp1 tp2) -2-)
(meta-mk-scheme-op mk-scheme-unop stx nm-code
		   (x) (tp1) -1-)

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
(define-syntax-rule (meta-mk-scheme-op-dual mk-scheme stx nm-code 
				       (a1 a2) (tp1 tp2) code1 code2)

  (define-syntax-rule (mk-scheme op-type op nm-tr nm-func nm-code tp1 tp2)
    (begin
      (define-scm-functor (nm-func s . a)
	(apply (case-lambda 
		 ((a1 a2)		  
		  (syntax-parameterize ((S (identifier-syntax s)))
		     (scm-ck 'code2 'nm-code 
			     (tp1 'a1) (tp2 'a2))))
		 ((a1)
		  (syntax-parameterize ((S (identifier-syntax s)))
		     (scm-ck 'code1 'nm-code 
			     (tp1 'a1)))))
	       a))

      (define-goal-transformer nm-func (nm-tr stx n m . a)
	(apply (case-lambda
		 ((a1 a2)
		  #`(code2 nm-code #,(tp1 stx a1) #,(tp2 stx a2)))
		 ((a1)
		  #`(code1 nm-code #,(tp1 stx a1))))
	       a))

      (bind-operator-to-functor nm-func op))))

(meta-mk-scheme-op-dual mk-scheme-dual stx nm-code
			(x y) (tp1 tp2) -1- -2-)

347 348 349
; ------------------------------
(define (tr-error op) 
  (lambda (stx n m . l)
Stefan Israelsson Tampe committed
350 351
    (warn (format #f "int ~a op ~a should not be translated directly"
		  (get-refstr n m) op))
352
    #'((fk-error 'op))))
353 354

(define (fk-error op)
355 356 357
  (let ((warn-message 
         (format #f "Operator ~a is not evaluable, will fail" op)))
                              
358
   (<lambda> (Cut SCut . X)
359 360 361
    (<let> ((e (get-flag unknown)))
      (cond
        ((eq? e error)
362
         (existence_error procedure op))
363

364
        ((eq? e warning)
365 366 367
         (<code> (warn warn-message))
         <fail>)

368
        ((eq? e fail)
369 370 371 372
         <fail>)

        (else
         (<code> (error "Bug in prolog flag 'unknown' implementation"))))))))
373

374
(<define> (<iss> x y)
375 376
  (<let> ((x (<lookup> x))
	  (y (<lookup> y)))
377
    (if (number? x)
378
        (if (number? y)
379 380 381 382 383 384 385
            (if (inexact? x)
                (if (inexact? y)
                    (when (my-equal? x y))
                    <fail>)
                (if (inexact? y)
                    <fail>
                    (when (eqv? x y))))
386 387
            (<r=> x ,(scm-eval y)))
        (<r=> x ,(scm-eval y)))))
388

389 390
(define-syntax-rule (mk-prolog-abstract tp op fk-name tr-name)
 (begin   
Stefan Israelsson Tampe committed
391
   (define-goal-functor      fk-name         (fk-error op))
392
   (define-goal-transformer  fk-name tr-name (tr-error op))
393
   (set-procedure-property!  fk-name 'name 'fk-name)
394
   (bind-operator-to-functor fk-name op)))
395

396 397
(mk-prolog-abstract 'xfx ":-"  :-     tr-fact)
(mk-prolog-abstract 'xfx "-->" -->    tr-dcg)
398 399 400
(mk-prolog-abstract 'fx "?"    ?      tr-?)
(mk-prolog-abstract 'fx "?-"   ?-     tr-?-)
(mk-prolog-abstract 'fx "$"    $      tr-$)
401

402 403 404 405 406 407 408 409
(<define> (<:> module l)
  (<let> ((module (ref-module (procedure-name (<lookup> module))))
	  (l      (<lookup> l)))
    (if (procedure? l) 
	(namespace-switch module (module-ref module 
					     (procedure-name l)))
	(namespace-switch module l))))

410 411 412
(define-syntax-rule (mk-meta --ifa -ifa)
  (<define-guile-log-rule> (--ifa x (... ...)) 
    (-ifa x (... ...))))
413

414 415 416 417 418
(define -if
  (<case-lambda>
   ((p a)   (p) <cut> (a))
   ((p a b) (<if> (p) (a) (b)))))

419
(mk-meta --if <if>)
420 421 422 423 424 425 426
			
(define --soft-if
  (<case-lambda>
   ((p a)   (p) (a))
   ((p a b) (<soft-if> (p) (a) (b)))))
(mk-meta --soft-if <soft-if>)

427 428 429 430

(define-guile-log -if-ii
  (lambda (x)
    (syntax-case x ()
431
      ((_ w p x)   #'(<and> w (<once-ii> p) x))
432
      ((_ w p x y) #'(<if> w  (<once-ii> p) x y)))))
433

434 435 436
(<define-guile-log-rule> (<if-ii> a b c)
     (<if> (<once-ii> a) b c))

437 438
(mk-meta --if-ii -if-ii)

439
(<define> (-and x y) (<and> (x) (y)))
440
(mk-meta --and <and>)
441

442
(<define> (-and-ii x y) (<and-ii> (x) (y)))
443
(mk-meta --and-ii <and-ii>)
444

445
(<define> (-or x y) (<or> (x) (y)))
446
(mk-meta --or <or>)
447

448
(<define> (-or-ii x y) (<or-ii> (x) (y)))
449
(mk-meta --or-ii <or-ii>)
450

451
(<define> (-not x) (<not> (x)))
452
(mk-meta --not <not>) 
453

454 455 456 457 458 459 460
(mk-prolog-biop     'xfy ":"    tr-ns:          op2:     <:>       a a)
(mk-prolog-biop     'xfy ","    tr-and          #{,}#    --and     g g)
(mk-prolog-biop     'xfy ",,"   tr-and-ii       #{,,}#   --and-ii  g g)
(mk-prolog-biop     'xfy "->"   tr-if-then      ->       --if      g g)
(mk-prolog-biop     'xfy "*->"  tr-if-then-soft *->      --soft-if g g)
(mk-prolog-biop     'xfy "-i>"  tr-if-then-ii   -i>      --if-ii   g g)
(mk-prolog-unop     'fy  "\\+"  tr-negation     #{\\+}#  --not     g  )
461

462 463
(set! (@ (logic guile-log prolog goal-functors) op2:) op2:)

464 465
(<define> (-<r=>  x y) (<r=> x y))		    
(<define> (-<=>   x y) (<=> x y))		    
466
(<define> (-<==>  x y) (<==> x y))
467

468
(mk-prolog-biop-tr  'xfx "="    unify-tr     op2=    <r=>  (v v)
Stefan Israelsson Tampe committed
469 470 471
  ((unify-tr stx n m x y)
   (match x
    ((#:variable v id nn mm)
472
     (if (and #f (first-variable? v id) v (variable-not-included? v y))
Stefan Israelsson Tampe committed
473 474 475
	 (begin
	   (local-variable! v)
	   #`(<code> 
476
	      (set! #,(datum->syntax stx v) `#,(*var* stx y))))
477
	 #`(-<r=> `#,(*var* stx x) `#,(*var* stx y))))
478 479 480
    (_
     (match y
       ((#:variable v id nn mm)
481
	(if (and #f (first-variable? v id) v 
482 483 484 485
		 (variable-not-included? v x))
	    (begin
	      (local-variable! v)
	      #`(<code> (set! #,(datum->syntax stx v) `#,(*var* stx y))))
486
	    #`(-<r=> `#,(*var* stx x) `#,(*var* stx y))))
487
       (_
488
	#`(-<r=> `#,(*var* stx x) `#,(*var* stx y))))))))
489

490 491 492
(mk-prolog-biop     'xfx "=="   ident-tr     ==       -<==>   a a)
(mk-prolog-biop-not 'xfx "\\==" not-ident-tr #{\\==}# -<==>   a a)
(mk-prolog-biop-not 'xfx "\\="  not-unify-tr #{\\=}#  -<r=>   a a)
493 494 495

(mk-prolog-biop     'xfx "=@="    variant-tr      =@=       is-variant?  a a)
(mk-prolog-biop-not 'xfx "\\=@="  not-variant-tr  #{\\=@=}# is-variant?  a a)
496 497


498 499 500 501
(mk-prolog-biop     'xfx "@>"   tr-ogt       @>           term> a a)
(mk-prolog-biop     'xfx "@<"   tr-olt       @<           term< a a)
(mk-prolog-biop-not 'xfx "@>="  tr-0ge       @>=          term< a a)
(mk-prolog-biop-not 'xfx "@=<"  tr-0le       @=<          term> a a)
502

503
(mk-prolog-biop-tr  'xfx "is"   tr-is        is           <iss> (a s)
Stefan Israelsson Tampe committed
504 505 506
 ((tr-is stx n m x y)
  (match x
   ((#:variable v id nn mm)
507
    (if (and #f (first-variable? v id))
Stefan Israelsson Tampe committed
508 509 510 511 512 513
	(begin
	  (local-variable! v)
	  #`(<code> (set! #,(datum->syntax stx v) #,(scm stx y))))
	#`(<iss> #,(-arg- stx x) #,(scm stx y))))
   (_
    #`(<iss> #,(-arg- stx x) #,(scm stx y))))))
514
(define (my-rem x y) (- x (* (quotient x y) y)))
515 516 517 518 519
(define consx
  (case-lambda 
    ((x y) (cons x y))
    ((x)   x)))

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
(define (mk-lam cc)
  (define (lam s p c* . x)
    (apply cc s p x))
  lam)
     
(<define> (<<=> data goal)
  (<<match>> (#:mode -) (data)
    (#((f . l))
      (<and>          
       (<values> x (let ((cc CC))
                     (<and>
                      (<=> f ,(mk-lam cc))
                      (goal-eval goal))))
       (<=> x l)))
    (_
     (<and>
      (<values> x (goal-eval goal))
      (<=> x data)))))

539 540 541 542 543 544 545 546 547 548 549 550 551 552
(<define> (<=>> data goal)
  (<<match>> (#:mode -) (data)
    (#((f . l))
      (<and>          
       (<values> x (let ((cc CC))
                     (<and>
                      (<=> f ,(mk-lam cc))
                      (goal-eval goal))))
       (<=> x l)))
    (_
     (<and>
      (<values> x (goal-eval goal))
      (<=> x data)))))

553
(mk-prolog-biop 'xfx "<="   <=-tr     op2<=       <<=> a a)
554 555 556
(mk-prolog-biop 'xfx "←"   -tr                <<=> a a)
(mk-prolog-biop 'xfx "=>"   =>-tr     op2=>        <=>> a a)
(mk-prolog-biop 'xfx "←"   -tr                <=>> a a)
557

558
(define-syntax-rule (shr x y) (ash x (- y)))
559
(mk-scheme-dual     'yfx "|"    tr-|         |             consx     s s)
560 561
(mk-scheme-dual     'yfx "+"    tr-+         op2+          .+        s s)
(mk-scheme-dual     'yfx "-"    tr--         op2-          .-        s s)
562
(mk-scheme-unop     'fy  "\\"   tr-bitnot    #{\\}#        lognot    s  )
563 564 565
(mk-scheme-biop     'yfx "*"    tr-*         op2*          .*        s s)
(mk-scheme-biop     'yfx "/"    tr-/         op2/          ./        s s)
(mk-scheme-biop     'yfx "//"   tr-i/        //            truncate/ s s)
566 567
(mk-scheme-biop     'yfx "rem"  tr-rem       rem           my-rem    s s)
(mk-scheme-biop     'yfx "mod"  tr-mod       mod           modulo    s s)
568
(mk-scheme-biop     'yfx "div"  tr-div       div           quotient  s s)
569
(mk-scheme-biop     'xfx "**"   tr-pow       **            myexpt    s s)
570
(mk-scheme-biop     'xfx "^"    tr-pow2      ^             myexpt    s s)
571 572 573
(mk-scheme-biop     'yfx "<<"   tr-shr       <<            .ash      s s)
(mk-scheme-biop     'yfx ">>"   tr-shr       >>            shr       s s)
(mk-scheme-biop     'yfx "/\\"  tr-bitand    #{/\\}#       logand    s s)
574
(mk-scheme-biop     'yfx "\\/"  tr-bitor     #{\\/}#       .logior   s s)
575

576 577 578
(define (name-as f g) (set-procedure-property! f 'name (procedure-name g)))


579 580 581 582 583 584
(mk-prolog-biop-when 'xfx "<"    tr-<         op2<           .<        s s)
(mk-prolog-biop-when 'xfx ">"    tr->         op2>           .>        s s)
(mk-prolog-biop-when 'xfx ">="   tr->=        op2>=          my->=     s s)
(mk-prolog-biop-when 'xfx "=<"   tr-=<        op2=<          my-<=     s s)
(mk-prolog-biop-when 'xfx "=:="  tr-equal     =:=            my-equal? s s)
(mk-prolog-biop-when-not 'xfx "=\\="  tr-not-equal  #{=\\=}# my-equal? s s)
585

586 587
(mk-scheme-dual     'yfx "+"    tr-+g         gop2+         +        s s)
(mk-scheme-dual     'yfx "-"    tr--g         gop2-         -        s s)
588
(mk-scheme-unop     'fy  "\\"   tr-bitnotg    #{g\\}#       lognot    s  )
589 590 591
(mk-scheme-biop     'yfx "*"    tr-*g         gop2*         *        s s)
(mk-scheme-biop     'yfx "/"    tr-/g         gop2/         /        s s)
(mk-scheme-biop     'yfx "//"   tr-i/g        g//           truncate/ s s)
592 593
(mk-scheme-biop     'xfx "**"   tr-powg       g**           expt     s s)
(mk-scheme-biop     'xfx "^"    tr-pow2g      g^            expt     s s)
594 595 596 597 598
(mk-scheme-biop     'yfx "<<"   tr-shrg       g<<           ash      s s)
(mk-scheme-biop     'yfx ">>"   tr-shrg       g>>           shr       s s)
(mk-scheme-biop     'yfx "/\\"  tr-bitandg    #{g/\\}#      logand    s s)
(mk-scheme-biop     'yfx "\\/"  tr-bitorg     #{g\\/}#      logior    s s)

599

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
(define-syntax-rule (mkbi f ff) 
  (define-syntax-rule (f x y) 
    (ff (<lookup> x) (<lookup> y))))

(mkbi bi-equal? equal?)
(mkbi bi<       <)
(mkbi bi>       >)
(mkbi bi<=      <=)
(mkbi bi>=      >=)

(mk-prolog-biop-when 'xfx "<"    tr-<g         gop2<           bi<       s s)
(mk-prolog-biop-when 'xfx ">"    tr->g         gop2>           bi>       s s)
(mk-prolog-biop-when 'xfx ">="   tr->=g        gop2>=          bi>=      s s)
(mk-prolog-biop-when 'xfx "=<"   tr-=<g        gop2=<          bi<=      s s)
(mk-prolog-biop-when 'xfx "=:="  tr-equalg     g=:=            bi-equal? s s)
(mk-prolog-biop-when-not 'xfx "=\\="  tr-not-equalg  #{g=\\=}# bi-equal? s s)
616

617 618 619
(define (.logior x y)
  (check-num (logior (is-a-num? x) (is-a-num? y))))

620 621
(define (myexpt x y) (exact->inexact (expt x y)))

622 623 624 625 626
(define (.< x y)
  (check-num (< (is-a-num? x) (is-a-num? y))))
(define (.> x y)
  (check-num (> (is-a-num? x) (is-a-num? y))))

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
(define .+ 
  (case-lambda 
   ((x y)
    (check-num (+ (is-a-num? x) (is-a-num? y))))
   ((x)
    (check-num (+ (is-a-num? x))))))

(define .- 
  (case-lambda 
   ((x y)
    (check-num (- (is-a-num? x) (is-a-num? y))))
   ((x)
    (check-num (- (is-a-num? x))))))

(define .-1 .-)

(define .+1 .+)

645 646 647 648 649 650 651
(define (.* x y)
  (check-num (* (is-a-num? x) (is-a-num? y))))
(define (./ x y)
  (exact->inexact (/ (is-a-num? x) (is-a-num? y))))
(define (.ash x n)
  (check-num (ash (is-a-num? x) (is-a-num? n))))

652 653 654 655

(define e1 1.000000000001)
(define e2 0.999999999999)
(define (my-equal? x y)
656 657 658 659 660 661 662 663 664 665
  (if (and (is-a-num? x) (is-a-num? y))
      (begin
	(if (< x 0) (set! x (- x)))
	(if (< y 0) (set! y (- y)))

	(if (inexact? x)
	    (and (<= y (* e1 x)) (>= y (* e2 x)))
	    (if (inexact? y)
		(and (<= x (* e1 y)) (>= x (* e2 y)))
		(= x y))))
666 667 668 669 670 671 672
      (= x y)))

(define (my-<= x y)
  (if (inexact? x)
      (> y (* e2 x))
      (if (inexact? y)
          (< x (* e1 y))
673
          (<= x y))))
674 675 676 677 678 679

(define (my->= x y)
  (if (inexact? x)
      (< y (* e1 x))
      (if (inexact? y)
          (> x (* e2 y))
680
          (>= x y))))
681

682 683 684 685 686 687 688 689 690 691 692 693
#| Further supported operators scm functions are
abs(x)                     (abs x)
sign(x)                    (if (= x 0) 0 (if (< x 0) -1 1))
float_integer_part(x)      (truncate/ x 1)
float_fractional_part(x)   (call-with-values (lambda () (truncate/ x 1)
                                                (lambda (x y) y)))
float(x)                   (exact->inexact x)
truncate(x)                (truncate x)
round(x)                   (round x)
ceiling(x)                 (ceiling x)
floor(x)                   (floor x)
|#
694

695 696 697 698 699
(define (project@ s x)
  (if (namespace? x)
      (project@ s (namespace-val (gp-lookup x s)))
      x))

700 701 702 703 704 705 706 707
(define (al s x)
  (umatch (#:mode - #:status s) (x)
    ((x . l)
     (cons x (al s l)))
    (()
     '())
    (x x)))

Stefan Israelsson Tampe committed
708
(<define> (sup=.. x y)
709
  (<let*> ((xx (<lookup> x))
710
	   (y  (al S y))
Stefan Israelsson Tampe committed
711 712
	   (x  (project@ S xx))
	   (x  (al S x)))
713
    (cond
714 715 716
     ((null? x)
      (<=> y (())))

Stefan Israelsson Tampe committed
717
     ((pair? x)
718
      (sup=.. (vector (list | (car x) (cdr x))) y))
719

720
     ((or (number? x) (null? x))
721
      (<cut> (<=> (x) y)))
722
          
723
     ((procedure? x)
724 725
      (<cut>
       (sup=.. (vector (list x)) y)))
726
     
727
     ((vector? x)
728
      (<cut>
Stefan Israelsson Tampe committed
729
        (<match> (#:mode - #:name =..1) (x)
730 731
          (#(l) 
           (<cut>
Stefan Israelsson Tampe committed
732
             (<match> (#:mode - #:name =..2) (y)
733 734 735 736 737 738 739 740 741 742 743 744 745 746
              ((f . u)
               (<cut>
                (<let> ((f (<lookup> f)))                   
                  (cond 
                   ((string? f)
                    (<let> ((g (module-ref (current-module) 
                                           (string->symbol f))))
                      (if (procedure? g)
                          (<=> l  ,(cons g u))
                          (existence_error 
                           procedure 
                           (vector `(,divide ,f
                                             ,(length (<scm> u))))))))
                   (else
747
                    (<=> l y))))))
748
              (y
749
               (<cut> 
750
                (<=> l y))))))
751
          (#(a b) (<cut> <fail>))
752 753 754 755 756 757 758 759 760
          (_    (type_error 'compound x)))))

     ((<var?> x)
      (<cut>
       (cond
        ((<var?> y)
         (instantiation_error))
       
        ((list? y)
761 762 763 764
	 (<<match>> (#:mode - #:name =..3) (y)
           ((,| a b)
	     (<=> x ,(cons a b)))

765
	   ((f . l)
766 767 768
             (<cut>
              (<let> ((f (<lookup> f)))
               (cond              
769 770
                ((or (procedure? f) (string? f))
                 (<=> xx ,(vector (cons f l))))
771

772
                ((and (number? f) (null? (<lookup> l)))
773
                 (<=> xx f))
774 775 776 777

		((or (pair? f) (vector? f))
		 (<=> xx ,(vector (cons f l))))

778 779
                ((number? f)
                 (type_error atom f))
780
              
781 782
                ((<var?> f)
                 (instantiation_error))
783 784 785 786 787 788 789 790 791
		
		((eq? f |)
		 (<=> x ,(apply cons* l)))

		((eq? f '()) 
		 (if (null? l)
		     (<=> x ())
		     (<=> x ,(vector y))))
		 
792 793 794 795 796 797 798
                (else
                 (type_error atom f))))))
            (()
             (<cut>
              (type_error list y)))))

        ((pair? y)
799 800 801 802 803 804
	 (<recur> lp ((z y))
	   (if (pair? z)
	       (lp (cdr z))
	       (if (<var?> z)
		   (instantiation_error)
		   (type_error list y)))))
805
       
806 807
        (else
         (type_error list y))))))))
808

Stefan Israelsson Tampe committed
809 810
(<define-guile-log-rule> (mac=.. a b) (sup=.. a b))
(mk-prolog-biop     'xfx "=.." tr=.. =.. mac=.. a a)
811

812
(define-goal-functor (#{;}# x y)
Stefan Israelsson Tampe committed
813
  (<var> (if then)
814
    (<if> (<=> x ,(vector `(,-> ,if ,then)))
Stefan Israelsson Tampe committed
815
	  (<if> (goal-eval if) (goal-eval then) (goal-eval y))
816 817 818 819 820
	  (<if> (<=> x ,(vector `(,*-> ,if ,then)))
		(<soft-if> (goal-eval if) (goal-eval then) (goal-eval y))
		(<if> (<=> x ,(vector `(,-i> ,if ,then)))
		      (<if-ii> (goal-eval if) (goal-eval then) (goal-eval y))
		      (<or> (goal-eval x) (goal-eval y)))))))
821

822
(set-object-property! #{;}# 'goal-compile-stub 
823 824
  (lambda (a b)
    `((@ (logic guile-log) <or>) ,a ,b)))
825
(set-object-property! #{;}# 'goal-compile-types
826 827
                      '(g g))

828
(define-goal-transformer #{;}# (tr-disjunction stx n m x y)
829
  (match x
830
    ((('xfy _ "->" _) i t n m)
Stefan Israelsson Tampe committed
831
     (let lp ((i i))
832
       (match i
Stefan Israelsson Tampe committed
833 834
	((x) (lp x))
	(_
835
	 #`(--if #,(goal stx i) #,(goal stx t) #,(goal stx y))))))
836 837 838 839 840 841 842 843 844 845 846 847 848 849

    ((('xfy _ "-i>" _) i t n m)
     (let lp ((i i))
       (match i
	((x) (lp x))
	(_
	 #`(--if-ii #,(goal stx i) #,(goal stx t) #,(goal stx y))))))
    
    ((('xfy _ "*->" _) i t n m)
     (let lp ((i i))
       (match i
	((x) (lp x))
	(_
	 #`(--soft-if #,(goal stx i) #,(goal stx t) #,(goal stx y))))))
850
    (_
851
     #`(--or #,(goal stx x) #,(goal stx y)))))
852

853
(bind-operator-to-functor #{;}# ";")
854

855 856 857 858 859 860
(define-goal-functor (#{;;}# x y)
  (<var> (if then)
    (<if> (<=> x ,(vector `(,-i> ,if ,then)))
	  (<if>   (<once-ii> (goal-eval if)) (goal-eval then) (goal-eval y))
          (<or-ii> (goal-eval x) (goal-eval y)))))

861 862

(set-object-property! #{;;}# 'goal-compile-stub 
863
  (lambda (a b)
864
    `((@ (logic guile-log iinterleave) <or-ii>) ,a ,b)))
865

866 867
(set-object-property! #{;;}# 'goal-compile-types
                      '(g g))
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
(define-goal-transformer #{;;}# (tr-disjunction-ii stx n m x y)
  (match x
    ((('xfy _ "-i>" _) i t n m)
     (let lp ((i i))
       (match i
	((x) (lp x))

	(((_ _ (or "<" ">" "=<" ">=" "==" "=:=" "=\\=") _) a b n2 m2)
	 #`(--if-ii #,(goal stx i) #,(goal stx t) #,(goal stx y)))
	(_
	 #`(--if-ii #,(goal stx i) #,(goal stx t) #,(goal stx y))))))
    (_
     #`(--or-ii #,(goal stx x) #,(goal stx y)))))

(bind-operator-to-functor #{;;}# ";;")

884 885 886

(<define-guile-log-rule> (<unify_with_occurs_check> x y) (<=> x y))
(mk-prolog-term-2 tr-uoc unify_with_occurs_check <unify_with_occurs_check> 
887
		  v a)
888

889
;; CALL
890
(define call
891 892 893 894 895
  (<case-lambda>
   ((g)
    (<code> (gp-var-set *call-expression* g S))
    (goal-eval g))
   ((g . l)
896
    (<<match>> (#:mode - #:name 'call) (g)
897 898 899 900 901 902 903 904
      (#(u)
       (<and>
	(<code> (gp-var-set *call-expression* g S))
	(goal-eval  (vector (append (<scm> u) l)))))
      (else
       (goal-eval (vector (cons g l))))))))

#|
905 906
(<define-guile-log-rule> (call-mac g) (call-fkn g))
(mk-prolog-term-1 tr-call call call-mac a)
907
|#
908

909
;; CATCH
910 911
(<define> (catch-fkn cut scut g c h)
  (<with-cut> cut scut
912 913 914
    (<code> (gp-var-set *call-expression* g S))
    (<catch> 'prolog #f 
      (<lambda> () (goal-eval g))
915
      (<lambda> (tag next x)
916
        (<or> 
917 918 919
         (<and> (<=> c x) <cut> (goal-eval h))
         (next))))))
        
920
(<define-guile-log-rule> (catch-mac g c h) (catch-fkn CUT SCUT g c h))
921
(mk-prolog-term-3 tr-catch catch catch-mac a a a)
922

923
;; THROW
924
(<define> (throw x) (throw-it (<scm> x)))
925

926
;; COPY_TERM
927
(<define-guile-log-rule> (<copy_term> x l)
928 929 930
   (<=> ,l ,(<cp> x))
   (do-attribute-constructors))

931 932
(mk-prolog-term-2 tr-cp copy_term <copy_term> a a)

933 934 935
(<define> (uniq X goal)
  (<uniq> (</.> (goal-eval goal)) X))

936 937 938
(<define> (yield_at_change test action)
   (<yield-at-change> test
     (<lambda> (start?) (goal-eval action))))
939 940 941 942 943 944 945

(define-syntax-rule (mk-test tr-nm fk-nm -nm x code)
  (begin
     (<define-guile-log-rule> (-nm x) 
       (<and> (if code <cc> <fail>)))
     (mk-prolog-term-1 tr-nm fk-nm -nm a)))

946
(mk-test tr-var      var      -var      x (attvar? x))
947
(mk-test tr-atom     atom     -atom     x (let ((y (<lookup> x)))
948
                                            (or (string? y)
949
                                                (procedure? y)
Stefan Israelsson Tampe committed
950 951
                                                (char? y)
                                                (null? y))))
952 953 954 955 956 957
(mk-test tr-integer  integer*  -integer  x (and
                                            (number? (<lookup> x))
                                            (integer? (<lookup> x))))
(mk-test tr-float    float*    -float    x (and (number? (<lookup> x))
                                                (inexact? (<lookup> x))))

958

959 960 961
(define-syntax-rule (mk-num float float*)
  (<define> (float x)
    (<match> (#:mode - #:name float) (x)
962
     (#((,op2- x))
963 964 965 966 967
      (<cut> (float* x)))
     (x
      (<cut> (float* x))))))

(mk-num integer -integer)
968 969 970 971 972 973 974
(mk-num float+  -float)

(define float
  (case-lambda
    ((x) (exact->inexact x))
    (x   (apply float+ x))))

975
     
976

977
(define (atomic? s x)    
978
  (let ((y (gp-lookup x s)))
979
    (or (symbol? y) (null? y)
980
        (number?  y) (string? y) (char? y) (procedure? y))))
981

982 983 984 985 986 987 988 989
(define (comp? s x)
  (let ((y (gp-lookup x s)))
    (or (vector? y)
        (gp-pair- y s)
        (pair? y))))

(mk-test tr-atomic   atomic    -atomic   x (atomic? S x))
(mk-test tr-compound compound* -compound x (comp? S x))
990
(mk-test tr-nonvar   nonvar    -nonvar   x (not (attvar? x)))
991
(mk-test tr-number   number*   -number   x (number? (<lookup> x)))
992

993
(mk-num number -number)
994

995 996 997 998
(<define> (compound x)
  (<not> (number x))
  (-compound x))

999 1000 1001 1002
(define (gp->list x s)
  (if (gp-pair? x s)
      (cons (gp-car x s) (gp->list (gp-cdr x s) s))
      '()))
1003 1004 1005 1006 1007 1008 1009

(<define> (functor comp nm arity)
  (<match> (#:mode - #:name functor) (comp)
    (#((f . l))
     (<cut>
      (<and>
       (<=> f nm)
1010
       (<=> arity ,(length (gp->list l S))))))
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

    ((a b . l)
     (<cut>
      (<and>
       (<=> nm ",")
       (<=> arity 2))))

    ((a . l)
     (<cut>
      (<and>
       (<=> nm ".")
       (<=> arity 2))))

    (f
     (<cut>
      (<let> ((f (<lookup> f)))
       (cond
        ((or (procedure? f) (number? f) (string? f) (null? f) (char? f))
         (<and> (<=> f nm) (<=> 0 arity)))

        ((<var?> f)
         (<let> ((arity (<lookup> arity)))
           (cond
            ((<var?> arity)
             (instantiation_error))
            ((and (number? arity) (integer? arity))
             (<let> ((nm (<lookup> nm)))                    
               (cond 
                ((<var?> nm)
                (instantiation_error))
                ((or (procedure? nm) (number? nm) (char? nm) (string? nm)
                     (null? nm))
                 (if (>= arity 0)
                     (cond
                      ((= arity 0)
                       (<=> f nm))
                      ((> arity (get-flag max_arity))
                       (representation_error max_arity))
                      ((procedure? nm)
                       (<=> f ,(vector (cons nm 
                                             (let lp ((n arity))
                                               (if (= n 0)
                                                   '()
                                                   (cons (gp-var! S) 
                                                         (lp (- n 1)))))))))
                      (else
                       (type_error atom nm)))
                     (domain_error not_less_than_zero arity)))
                (else
                 (type_error atomic nm)))))
            (else
             (type_error integer arity)))))
               
        (else
         (type_error callable f))))))))
           
           

1069

Stefan Israelsson Tampe committed
1070 1071 1072
(<define> (arg n term a)
  (<let> ((n    (<lookup> n))
          (term (<lookup> term)))
1073
    (<<match>> (#:mode - #:name arg) (n)
1074
       (#((,op2- x))
Stefan Israelsson Tampe committed
1075
        (if (integer? (<lookup> x))
1076
            (domain_error not_less_than_zero n)
1077
            (type_error integer n)))
Stefan Israelsson Tampe committed
1078
       (_ (<cut> <cc>)))
1079

Stefan Israelsson Tampe committed
1080
    (cond
1081 1082 1083
     ((procedure? term)
      <fail>)

1084
     ((<var?> term)
Stefan Israelsson Tampe committed
1085
      (instantiation_error))
1086 1087 1088 1089 1090 1091 1092 1093
	      
     ((<var?> n) <cc>)

     ((not (integer? n))
      (type_error integer n))
     
     ((< n 0)
      (domain_error not_less_than_zero n))
1094

1095 1096 1097
     (else <cc>))
    
    (cond
1098 1099 1100
     ((<var?> n)
      (<let> ((lam (<lambda> (term)
		     (<recur> lp ((i 0) (term term))
1101
		       (<match> (#:mode + #:name arg) (term)
1102
			 ((,a . l)
1103 1104
			  (<=> n i))
			 ((_  . l) 
1105 1106 1107
			  (lp (+ i 1) l))
			 (_ <fail>))))))

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	  (cond 
	   ((and (vector? term) (= (vector-length term) 1))
	    (lam (vector-ref term 0)))
	   (else
	    (<<match>> (#:mode - #:name 'arg2) (term)
	      ((x . l) (lam (list cons x l)))
	       (_ (domain_error "term" term)))))))

       ((and (vector? term)  (= (vector-length term) 1))
	(<recur> lp ((l (vector-ref term 0)) (n n))
	  (<<match>> (#:mode - #:name arg-2) (l)
	    ((x . l)
	     (if (= n 0)
		 (<=> x a)
		 (lp l (- n 1))))
	    (()
	     <fail>))))
1125
       (else
1126
	(<<match>> (#:mode - #:name 'arg3) (term)
1127
	  ((x . l)
1128 1129 1130 1131 1132 1133
	   (cond
	    ((= n 0) (<=> a cons))
	    ((= n 1) (<=> a x))
	    ((= n 2) (<=> a l))
	    (else
	     <fail>)))
1134
	  (()
1135 1136 1137 1138
	   <fail>)
	  (_
	   (domain_error "term" term)))))))
  
1139 1140 1141 1142 1143 1144 1145

(<define> (procedure_name f out)
   (<let> ((f (<lookup> f)))
      (if (procedure? f)
          (<=> out ,(procedure-name f))
          (if (<var?> f)
              (instantiation_error)
1146
              (type_error procedure f)))))
1147 1148

(<define> (fail) <fail>)
1149
(<define> (false) <fail>)
1150
(<define> (true) <cc>)
1151

1152
#;
1153 1154 1155
(define-goal-functor (!)
  (<with-fail> CUT <cc>))

1156 1157
(<define-guile-log-rule> (<!>)
  (<and> <cut>))
1158

1159
(mk-prolog-term-0 tr-! ! <!>)
1160

1161 1162 1163 1164
(define cc  (lambda x #t))
(define p   (lambda x #f))
(define ss  (fluid-ref *current-stack*))

1165 1166
(define *once* (gp-make-var #t))

1167 1168 1169 1170
(<define> (once-f v)
 (<let> ((v (<lookup> v)))
   (if (<var?> v)
       (instantiation_error)
1171 1172 1173 1174
       (<let> ((p P))
	  (<code> (gp-var-set *call-expression* v S))
	  (goal-eval v)
	  (<with-fail> p <cc>)))))
1175 1176 1177 1178 1179 1180 1181 1182 1183

(<define> (once_i v)
 (<let> ((v (<lookup> v)))
   (if (<var?> v)
       (instantiation_error)
       (<let> ((p P))
	  (<code> (gp-var-set *call-expression* v S))
	  (<once-ii> (goal-eval v))
	  (<with-fail> p <cc>)))))
1184
 
1185 1186 1187
(<define-guile-log-rule> (once-mac v) (once-f v))
(mk-prolog-term-1 tr-once once once-mac a)

1188 1189


Stefan Israelsson Tampe committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
(define halt
  (case-lambda 
    ((s p cc)    'halt)
    ((s p cc x)  
     ((<lambda> (x)
        (<let> ((x (<lookup> x)))
           (cond
            ((<var?> x)
             (instantiation_error))
            ((not (integer? x))
             (type_error integer x))
            (else
             (<ret> `(halt ,x))))))
      s p cc x))))
Stefan Israelsson Tampe committed
1204

1205

1206 1207 1208 1209 1210 1211 1212
(name-as gop2- op2-)
(name-as gop2+ op2+)
(name-as #{g\\}# #{\\}#) 
(name-as gop2* op2*)
(name-as gop2/ op2/)
(name-as g//   //)
(name-as g**   **)
1213
(name-as g^    ^)
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
(name-as g<<   <<)
(name-as g>>   >>)
(name-as #{g/\\}# #{/\\}#) 
(name-as #{g\\/}# #{\\/}#) 
(name-as gop2<    op2<)
(name-as gop2>    op2>)
(name-as gop2>=   op2>=)
(name-as gop2=<   op2=<)
(name-as g=:=     =:= )
(name-as #{g=\\=}# #{=\\=}#)
1224 1225 1226 1227 1228 1229 1230 1231

(define first? #t)
(if first?
    (begin
      (set! (@ (logic guile-log prolog names) float)   float)
      (set! (@ (logic guile-log prolog names) number)  number)  
      (set! (@ (logic guile-log prolog names) integer) integer)
      
1232
      (set! (@ (logic guile-log prolog names) prolog-and) #{,}#)      
1233
      (set! (@@ (logic guile-log prolog analyze) #{,}#) #{,}#)      
1234 1235 1236
      (set! (@ (logic guile-log prolog names) prolog-or)  #{;}#)      
      (set! (@ (logic guile-log prolog names) prolog-not) #{\\+}#)
      (set! (@ (logic guile-log prolog names) prolog=..)  =..)
1237 1238
      (set! (@ (logic guile-log prolog names) divide) op2/)
      (set! (@ (logic guile-log prolog names) plus)   op2+)
1239
      (set! (@ (logic guile-log prolog names) unary-minus)  op2-)
1240 1241 1242
      (set! (@ (logic guile-log prolog names) binary-minus) op2-)
      (set! (@ (logic guile-log prolog names) binary-plus)  op2+)
      (set! (@ (logic guile-log prolog names) fact)   :-)
1243 1244
      (set! (@ (logic guile-log prolog names) true)   true)
      (set! (@ (logic guile-log prolog names) fail)   fail)
1245
      (set! (@ (logic guile-log prolog names) false)   false)
1246 1247
      (set! (@ (logic guile-log prolog names) !)      !)
      (set! (@ (logic guile-log prolog names) atom)   atom)
1248
      (set! (@ (logic guile-log prolog names) call)   call)
1249
      (set! (@@ (logic guile-log prolog variant) op2=) op2=)
1250
      (set! (@@ (logic guile-log prolog modules) *once*) *once*)
1251
      (set! (@@ (logic guile-log prolog dynamic) once-f) once-f)
1252
      (set! first? #f)))
1253

1254 1255 1256 1257
(define op1+ op2+)
(define op1- op2-)
(define gop1+ gop2+)
(define gop1- gop2-)
1258