run.scm 8.22 KB
Newer Older
1 2 3 4
(define-module (logic guile-log run)
  #:use-module (ice-9 match)
  #:use-module (logic guile-log macros)
  #:use-module (logic guile-log umatch)
5
  #:use-module (logic guile-log guile-prolog copy-term)
6
  #:use-module (logic guile-log interleave)
7
  #:use-module (logic guile-log persistance)
8
  #:export (<stall> <continue> <take> <run> <eval> <ask>
9
		    <cont-ref> <cont-set!>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
		    *gp-var-tr* *kanren-assq*
		    *init-tr*))
(define put_attr #f)
(define *init-tr* (make-fluid (lambda () #f)))
(<define> (build_attribut_representation res tail x)
  (<let> ((x (<lookup> x)))
    (if (gp-attvar-raw? x S)
	(<recur> lp ((res res) 
		     (l (map (lambda (x) 
			       (let ((res
				      (attribute-cstor-repr (car x))))
				 (if res
				     res
				     x)))
			     (gp-att-data x S))))
	   (if (pair? l)
	       (<let> ((xx (car l)))
		 (if (pair? xx)
		   (<var> (t)
		     (<=> (,(vector (list put_attr x (car xx) (cdr xx))) . t)
			  res)
		      (lp t (cdr l)))
		   (<var> (t)
		    ((car l) res t x)
		    (lp t (cdr l)))))
	       (<=> res tail))))))

(define bar build_attribut_representation)
38

39
(define-named-object *cc* (gp-make-var #f))
40

41 42
(define (<stall> s p cc . l)
  (gp-var-set *cc* (cons s (cons (cons p l) cc)) s)
43
  (fluid-set! *current-stack* s)
44 45
  'stalled)

46 47 48 49 50 51 52 53
(define (<cont-ref>)
  (gp-var-ref *cc*))

(define (<cont-set!> cc)
  (let ((s (car cc)))
    (gp-var-set *cc* cc s)
    (fluid-set! *current-stack* s)))

54 55 56
(define (<take> n) (<continue> n))
(define <continue>
  (case-lambda 
57
    (()  (let ((*cc* (gp-var-ref *cc*)))
58
           (if (and *cc* (car *cc*))
59
               (apply (cddr *cc*) (car *cc*) (cadr *cc*))
60 61 62 63 64 65 66 67 68 69 70
               'cannot-continue)))
    ((n) (let ((*cc* (gp-var-ref *cc*)))
           (if (and *cc* (integer? n) (not (car *cc*)))
               ((cdr *cc*) n)
               'cannot-continue-and-take-n)))))

(define (make-empty-s) (fluid-ref *current-stack*))

(define-syntax <eval> 
  (syntax-rules ()
    ((_ (v ...) code fini cc)
71 72 73
     (let* ((s  (fluid-ref *current-stack*))
	    (ss (gp-newframe s)))
       (<eval> ss (v ...) code fini cc)))
74 75

    ((_ s (v ...) code fini cc)
76 77
     (let* ((f (gp-newframe s))
	    (v (gp-var! f)) ...)
78 79
       (let ((fi fini)
             (c  cc))
80
         (<with-guile-log> (f fi c)
81 82 83 84 85 86
           code))))))
   
(define *gp-var-tr* (make-fluid 'v))
                                      
(define tr
  (case-lambda 
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
    ((x s single?) 
     (tr (fluid-ref *gp-var-tr*) x s single?))

    ((pre x s single?)
     (define init ((fluid-ref *init-tr*)))
     (define r ((<lambda> ()
		 (<var> (xx yy) 
		    (copy_term x xx yy) 
		    (<cc> (cons xx yy))))
		s (lambda x #f) 
		(lambda (ss p res)
		  (set! s ss)
		  (set! res (gp->scm res s))
		  (if (null? (cdr res))
		      (car res)
		      (if single?
			  (set! x (cons (car res) (cdr res)))
			  (set! x (cons (car res) (cdr res))))))))
     (define vars (make-hash-table))
     (define (get-var-number v)
       (let ((r (hashq-ref vars v #f)))
	 (if r 
	     (values r #t)
	     (let ((r n))
	       (let ((k (string->symbol 
			 (format #f "~a~a" pre n))))
		 (set! n (+ n 1))
		 (hashq-set! vars v k)
		 (values k #f))))))
116 117

     (define n 0)
118
     (define tail '())
119 120 121 122 123
     (let loop ((x (gp->scm x s)))
       (match x
         ((x . l)
          (cons (loop x) (loop l)))
         (x
124 125 126 127 128 129 130 131 132 133
	  (let ((x (gp-lookup x s)))
	    (cond
	     ((gp-attvar-raw? x s)
	      (get-var-number x))

	     ((gp-var? x s)
	      (get-var-number x))

	     (else
	      x)))))))))
134 135
  
(define *kanren-assq* #f)
136 137 138 139 140 141 142 143 144

(define-syntax-rule (mk<run> <run> <run*>)
  (define-syntax-rule (<run> . l)
    (let ((fr (gp-newframe (fluid-ref *current-stack*))))
      (dynamic-wind
        (lambda () 
          #f)
        (lambda () 
          (let ((r (<run*> . l)))
145
            #;(gp-unwind fr)
146 147
            r))
        (lambda ()
148
          #f #;(gp-unwind fr))))))
149 150 151 152

(mk<run> <run> <run*>)

(define-syntax <run*>
153 154
  (syntax-rules (*)
    ((_ (v) code ...)
155 156
     (let* ((fr1  (gp-newframe (fluid-ref *current-stack*)))
	    (fr   (gp-newframe fr1)))
157
       (with-fluids ((*current-stack* fr))
158 159 160 161 162 163 164
       (if *kanren-assq*
           (gp-logical++))
       (let-with-lr-guard fr wind lg rg ((ret '()))
         (lg fr
           (<eval> (v)
           (<and> code ...)
           (lambda x 
165
             (gp-unwind-tail fr1)
166 167
             (reverse ret))
           (lambda (s p)
168 169 170
	     (let ((res (gp-var! s)))
	       (set! ret (cons (tr (gp->scm v s) s #t) ret))
	       (p)))))))))
171 172

    ((_ (v ...) code ...)
173 174
     (let* ((fr1 (gp-newframe (fluid-ref *current-stack*)))
	    (fr  (gp-newframe fr1)))
175
       (with-fluids ((*current-stack* fr))
176 177 178 179 180 181 182 183 184
       (if *kanren-assq*
           (gp-logical++))
       (let-with-lr-guard fr wind lg rg ((ret '()))
         (lg fr
         (<eval> (v ...)
           (<and> code ...)
           (lambda x 
             (let ((r ret))
               (set! ret '())
185
               (gp-unwind-tail fr1)
186 187
               (reverse r)))
           (lambda (s p)
188
             (set! ret (cons (tr (gp->scm (list v ...) s) s #f) ret))
189
             (p))))))))
190 191 192 193 194
               
    
    ((_ * . l)  (<run> . l))

    ((_ m (v) code ...)
195 196
     (let* ((fr1   (gp-newframe (fluid-ref *current-stack*)))
	    (fr    (gp-newframe fr1)))
197
       (with-fluids ((*current-stack* fr))
198 199 200 201 202 203 204 205 206 207
       (if *kanren-assq*
           (gp-logical++))
       (let-with-lr-guard fr wind lg rg ((n m) (ret '()))
	 (lg fr
         (<eval> (v)
           (<and> code ...)
           (lambda x 
             (let ((r ret))
               (set! n    0)
	       (set! ret '())
208
               (gp-unwind-tail fr1)
209 210 211 212 213 214 215 216 217 218 219
               (reverse r)))
           (lambda (s p)
             (if (= n 0)
                 (let ((r (reverse ret)))
                   (gp-var-set *cc* (cons #f (lambda (mm) 
					       (set! n mm)
					       (set! ret '())
					       (p))) s)
                   r)
                 (begin
                   (set! n (- n 1))
220
		   (set! ret (cons (tr (gp->scm v s) s #t) ret))
221 222 223 224 225 226 227
                   (if (= n 0)
                       (let ((r (reverse ret)))
                         (gp-var-set *cc* (cons #f (lambda (mm) 
						     (set! n mm)
						     (set! ret '())
						     (p))) s)
                         r)
228
                       (p)))))))))))
229 230 231
                 

    ((_ m (v ...) code ...)
232 233 234
     (let* ((fr1   (gp-newframe (fluid-ref *current-stack*)))
	    (fr2   (gp-newframe fr1)))
       (with-fluids ((*current-stack* fr2))
235 236
       (if *kanren-assq*
           (gp-logical++))
237 238
       (let-with-lr-guard fr2 wind lg rg ((n m) (ret '()))
	 (lg fr2
239 240 241 242 243 244
	 (<eval> (v ...)
           (<and> code ...)
           (lambda  x 
             (let ((r ret))
               (set! n    0)
	       (set! ret '())
245
               (gp-unwind-tail fr1)
246 247 248 249 250 251 252 253 254 255 256 257
               (reverse r)))
           (lambda (s p)
             (if (= n 0)
                 (let ((r (reverse ret)))
                   (gp-var-set *cc* (cons #f (lambda (mm) 
					       (set! n mm)
					       (set! ret '())
					       (p)))
			       s)
                   r)
                 (begin
                   (set! n (- n 1))
258
		   (set! ret (cons (tr (list (gp->scm v s) ...) s #f) ret))
259 260 261 262 263 264 265 266
                   (if (= n 0)
                       (let ((r (reverse ret)))
                         (gp-var-set *cc* (cons #f (lambda (mm) 
						     (set! n mm)
						     (set! ret '())
						     (p)))
				     s)
                         r)
267
                       (p)))))))))))))
268

269
(define-syntax <ask*>
270 271 272 273 274 275 276
  (syntax-rules ()
    ((_ code ...)     
     (let ((cc (lambda (s p) #t))
           (p  (lambda ()  #f)))           
       (let ((s  (make-empty-s)))
         (<with-guile-log> (s p cc)
           (<and> code ...)))))))
277

278
(mk<run> <ask> <ask*>)
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

(<define> (build_attribut_representation res tail x)
  (<let> ((x (<lookup> x)))
    (if (gp-attvar-raw? x S)
	(<recur> lp ((res res) 
		     (l (map (lambda (x) 
			       (let ((res
				      (attribute-cstor-repr (car x))))
				 (if res
				     res
				     x)))
			     (gp-att-data x S))))
		 (if (pair? l)
		     (<let> ((xx (car l)))
		        (if (pair? xx)
			    (<var> (t)
			       (<=> (,(vector 
				       (list put_attr x (car xx) (cdr xx))) 
				     . t)
				    res)
			       (lp t (cdr l)))
			    (<var> (t)
			      ((car l) res t x)
			      (lp t (cdr l)))))
		     (<=> res tail))))))

(set! (@ (logic guile-log macros) build_attribut_representation)
  build_attribut_representation)