vm-goal-model.scm 16.9 KB
Newer Older
1 2 3
(<define> (set_extended x)
  (<code> (set! unify_operators (combine_ops (<scm> x)))))

4 5 6 7 8
(<define> (gen x) (<=> x ,(gensym "disj")))
(<define> (gen-rec x) (<=> x ,(gensym "Rec")))

(<define> (get_consts x) (<=> x ,(get-consts)))

9
(compile-prolog-string "
10

11 12 13 14 15 16 17 18 19 20 21
reverse_op(<,>).
reverse_op(>,<).
reverse_op(=<,>=).
reverse_op(>=,=<).
reverse_op(@<,@>).
reverse_op(@>,@<).
reverse_op(@=<,@>=).
reverse_op(@>=,@=<).
reverse_op(=:=,=:=).
reverse_op(=\\=,=\\=).

22 23 24 25 26 27 28
rev(X,Y) :-
  rev(X,[],Y).

rev([],X,X).
rev([A|X],Y,Z) :-
  YY = [A|Y],
  rev(X,YY,Z).
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
zero(V)    :- get_A(V,A),A=[[0|_]].

print([]).
print([X|L]) :- write(X),nl,print(L).

wrap(Code,[L,LL]) :-
  catch(Code,E,
     (  
        tfc(E),
        (
           E==#t -> L=[[[cc]|LL],LL];
           E==c  -> L=[[[cut],[fail]|LL],LL];
           L=[[[fail]|LL],LL]
        )
     )).

46
-trace.
47
-extended(',',m_and,;,m_or,\\+,m_not).
48
compile_goal(Code,Iout):- !,
49 50 51 52 53 54 55 56 57 58 59 60 61
  compile_goal(Code,Iout,StackSize,Narg,Consts,#t).

compile_goal(Code,Iout,StackSize,Narg,Constants,Pretty) :- !,
  (
    Code = (F(|A) :- Goal) -> length(A,Narg) ;
    Code = (F     :- Goal) -> Narg = 0       ;
    throw(compiles_only_clauses(Code))
  ),
  make_vhash(HV),
  make_vhash(HC),
  init_vars,init_e,
  (var(Constants) -> init_const ; true),
  b_setval(pretty,#t),
62
  make_state(0,[[0,_,_]],[],[[0,_]],0,0,0,0,0,[HC,HV],[],V),
63
  wrap(compile_goal(Code,#t,V,[L,[]]),[L,[]]),!,
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
64
  print(L),nl,!,
65 66
  get_M(V,StackSize),
  handle_all(L,LL),
67 68 69
  %(var(Constants)-> get_consts(Constants);true),
  %print(LL),nl,!,
  (true -> Iout=LL ; (b_setval(pretty,#f),mmtr(LL,Iout))).
70 71 72 73 74 75 76 77

compile_goal(X,Tail,V,L) :- var_p(X),!,
  compile_goal(call(X),Tail,V,L).


compile_goal(pr(X),Tail,V,[L,L]) :- !,
  write(pr(X)),nl.

78 79 80 81
compile_goal(set_p(Q),Tail,V,[L,LL]) :- !,
  get_P(V,P),
  L=[[set_p,Q,P]|LL].

82
compile_goal(!,Tail,V,[L,LL]) :- !,
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
83
  check_tail(Tail),  
84 85
  (Tail==#t -> L=[[cut],[cc]|LL] ; L=[[cut]|LL]).

86
compile_goal(softie(A),Tail,V,[L,LLL]) :- !,
87
  check_tail(Tail),
88
  tail(Tail,V,LL,LLL),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
89 90
  get_P(V,P),
  (Tail==#t -> L=[[softie,A],[cc,P]|LL] ; L=[[softie,A]|LL]).
91

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
92
compile_goal(begin_att,Tail,V,[L,LLL]) :- !,
93
   check_tail(Tail),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
94
   tail(Tail,V,LL,LLL),
95 96 97 98 99 100 101 102 103 104
   (
     Tail==#t -> L=LLL ;
     (
       get_A(V,A),
       A=[[Ai  | _] | _],
       set_A(V,[[AAi,AAx,AAt] | A]),
       AAi is Ai + 1,
       var_p(AAx),
       add_var(AAx,V,Tagx),
       tr('pre-unify',Pre),
105 106
       get_P(V,P),
       L = [[Pre,AAt,Tagx,P]|LL]
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
     )
   ).

compile_goal(end_att,Tail,V,[L,LL]) :- !,
   check_tail(Tail),
   get_AF(V, A, F),
   [[Ai,Ax,At]|AA]=A,
   set_A(V, AA),
   (Ai==0  -> throw(missmatching_begin_end_pair) ; true),
   var_p(Ax),
   add_var(Ax,V,Tag),
   (Tail==#t -> tr('post-unify-tail',Post) ; tr('post-unify',Post)),      
   (
     Tail==#t ->  LLL=LL ; 
     (
       At==#t ->
         (
           tr('post-unicall',PostCall),
           get_C(V,[C|_]),
           LLL=[[PostCall,C,F]|LL]
         ) ;
         LLL=LL
     )
   ),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
131
   get_P(V,P),
132 133
   (
      At   == #t  ->  L = [[Post,Tag]|LLL] ; 
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
134
      Tail == #t  ->  L = [[cc,P]|LLL]       ; 
135 136 137
                      L = LLL
   ).  

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
138
compile_goal(pop(N),Tail,V,[L,LLL]) :- !,
139 140 141
  check_tail(Tail),
  M is -N,
  push_v(M,V),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
142 143
  tail(Tail,V,LL,LLL),
  L=[[pop,N]|LL].
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

compile_goal(with_cut(C,CS),Tail,V,[L,LL]) :- !,
  var_p(C),
  var_p(CS),
  add_var(C ,V,TagC ),
  add_var(CS,V,TagCS),
  L=[[cutter,TagC,TagCS]|LL].

compile_goal((Args <= Goal),Tail,V,L) :- !,
  (listp(Args) -> true ; throw(not_proper_head(Args <= goal))),
  reverse(Args,AArgs),
  mg(cc(|Args),AArgs,Impr,0,N),
  NN is N + 3,
  link_l(L,L1,L2,L3),
  compile_goal(Goal,label(G,NN),V,L1),
  L2=[[[label,G]|U],U],
  set_S(V,0),
  push_v(NN,V),
  compile_goal((begin_att,Impr,pop(3),end_att),Tail,V,L3).

164 165
compile_goal((F(|ASrgs) :- Goal),Tail,V,L) :- !,
  rev(ASrgs,Args),
166 167
  (listp(Args) -> true ; throw(not_proper_head(F(|Args)))),
  reverse(Args,AArgs),
168 169
  mg(F(|Args),AArgs,Impr,0,N),  
  push_v(N,V),
170
  get_A(V,[[0|_]|_]) ->
171 172 173
     wrap(compile_goal((move_args(0,N),begin_att,Impr,end_att,Goal),
                       Tail,V,L),L);
     wrap(compile_goal((move_args(0,N),begin_att,Impr,Goal),Tail,V,L),L).
174 175 176 177 178

compile_goal((F :- Goal),Tail,V,L) :- !,
  push_v(4,V),
  wrap(compile_goal((pop(4),Goal),Tail,V,L),L).

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
179 180 181
compile_goal(newtag_F(F),Tail,V,[L,LL]) :- !,
  check_tail(Tail),
  tail(Tail,V,L,LL),
182
  gen(F).
183 184 185 186

compile_goal(extended_off,Tail,V,[L,L]) :- !,
    set_extended(#f).

187 188
compile_goal(move_args(N,N),Tail,V,[L,LL]) :- !,
    L=[['handle-spcc',N]|LL].
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
189

190 191 192 193
compile_goal(move_args(K,N),Tail,V,[L,LL]) :- !,
    L=[push_at(K)|L1],
    KK is K + 1,
    compile_goal(move_args(KK,N),Tail,V,[L1,LL]).
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
compile_goal(extended_on(|L),Tail,V,[L,L]) :- !,
  set_extended(L).

compile_goal(extended_on,Tail,V,[L,L]) :- !,
  set_extended([]).
compile_goal(collect_F(F),Tail,V,[L,L]) :-
  get_F(V,F).

compile_goal(Op(\"recur\",Args),Tail,V,[L,LL]) :-    
   Args = F(|U),
   Op=\"op2*\",!,
   get_line(U,X,Xin,N),
   reverse(X,XX),
   mg(recur(|U),XX,Impr,0,N),
   add_recur(F,A,N),
   push_args_args(#f,Xin,V,L,L1,_,_),
   touch_A(V),
   touch_Q(10,V),
213 214
   %gen_rec(Sym)
   %set_F(V,Sym)
215 216 217 218 219 220 221
   L1=[[label,A]|L2],
   compile_goal((begin_att,Impr,end_att),Tail,V,[L2,LL]).

compile_goal((X,Y),Tail,V,L) :- !,
  collect_conj((X,Y),Gs,[]),
  compile_conj(Gs,Tail,V,L).

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
222
compile_goal(m_or(fail,false),#t,V,[[[fail,P],[cc,P] |L], L]) :-
223 224 225
   get_P(V,P).
compile_goal(m_or(fail,false),#f,V,[[[fail,P]      |L], L]) :-
   get_P(V,P).
226 227
compile_goal(true,_,_,_) :- throw(#t).
compile_goal(';'(|X),Tail,V,[L,LL]) :- !,
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
228
   write(disjTail(Tail)),nl,
229 230
   collect_disj(X,XX,[]),
   (
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
231
     XX=[]  -> L=[[flse]|LL]                ;
232 233
     XX=[Z] -> compile_goal(Z,Tail,V,[L,LL]) ; 
     (
234
       get_AB2ESM(V,Aq,B2,E,S,M),
235
       get_F(V,F), 
236
       Q=[_,_],
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
237
       compile_disjunction(XX,#t,Aq,Ae,Out,Lab,LabA,Tail,S,U,B2,V,LM,Q,QQ),!,
238
       (zero(V) -> Tp is 0 ; Tp is 1),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
239
       pushl_Q(V,QQ),
240
       (
241
         varx(Q) ->
242
            (
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
243 244 245 246 247 248
              var_p(Var1),
              var_p(Var2),
              add_var_f(Var1,V,F,LabA1),
              add_var_f(Var2,V,F,LabA2),
              LabA = [LabA1,LabA2],
              L   = [['newframe-light',LabA,Lab,Q] | LX]
249 250
            ); 
            (
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
251 252 253 254 255 256
              var_p(Var1),
              var_p(Var2),
              add_var_f(Var1,V,F,LabA1),
              add_var_f(Var2,V,F,LabA2),
              LabA = [LabA1,LabA2],
              L   = [[newframe,LabA,Lab,Tp] | LX]   
257 258 259 260 261
            )
       ),
       LM = [LX,  [[label,Out] | LL]],
       get_EBH(V,Ed,B,H),
       add_missing_variables(H,U,Ed,Ed,EEd),!,
262 263 264
       BB2 is B2 \\/ EEd,
       EE is  E  \\/ EEd,
       set_B2E(V,BB2,EE)
265 266 267
     )
  ).

268
compile_goal(verbatim_call(X),Tail,V,[L,LL]) :- !,
269 270 271 272 273 274 275 276 277 278 279 280
   ( \\+var_p(X),
     ( 
       F(|Args)=X -> 
        (
          (var_p(F) ; isApply(Args)) -> fail ; true
        ) ; 
       true
     )
   ),
   !, 
   compile_goal(X,Tail,V,[L,LL]).

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
compile_goal(call(X),Tail,V,L) :- !,
   new_var(VS,V,TagS1),
   new_var(VP,V,TagP1),
   compile_goal(call_(X,A,Al,[C0|_],Pre,Post,LP),Tail,V,L),
   new_var(VS,V,TagS2),
   new_var(VP,V,TagP2),
   Ad=[A0,Cut_p],
   (
    A == #t ->
    ( 
     Cut_p == #t ->
     (
        Pre  = ['store-ps'   , A0, TagP1, TagS1    ],
        Post = [['goto-inst' , Out                 ],
                [label       , A0                  ],
                ['fail-psc'  , TagP1, TagS1, C0    ],
                [label       , Out                 ],
                ['post-sc'   , TagS1, C0           ]|LP]
     ) ; 
     (
       Pre  = [true],
       Post = LP
303
     )
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    ) ;
    (
     Cut_p == #t ->
     (
        Pre  = ['store-p', A0, TagP1],
        Post = [['goto-inst' , Out          ],
                [label       , A0           ],
                ['fail-pc  ' ,TagP1, C0     ],
                [label       , Out          ],
                ['post-c'    , C0    ]|LP]
     ) ; 
     (
       Pre  = [true],
       Post = LP
     )
    )
  ).

compile_goal(call_(X,A0,Al,C0,Pre,Post,LP),Tail,V,[L,LL]) :- !,
   L=[Pre|LX],
324
   get_C(V,C),
325 326 327
   get_AA(V,A),
   AA = [A0|A],
   set_AA(V,AA),
328 329
   C  = [C0|_],
   CC = [Al|C],
330
   set_C(V,CC),
331
   compile_goal(X,#f,V,[LX,LLX]),
332
   set_AA(V,A),
333
   LLX = Post,
334
   set_C(V,C),
335 336
   (Tail=#t -> LP=[[cc]|LL] ; LP=LL).

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
compile_goal((X =.. Y),Tail,V, L, LL) :- !,
  (var_p(X);constant(X)) ->
  (
     compile_arg(X,Branch,HC,HV,L,LLX),
     compile_imprint(Y,Branch,HC,HV,LLX,LLY),
     tr(=..,Op),
     LLY=[[Op]|LL]
  ).
  

compile_goal(set(V,X),Tail,V,[L,LL]) :- !,
  touch_Q(11,V),
  tr(set,Set),
  (var_p(X) -> true ; throw(no_var_in_set)),
  add_var(X,V,Tag),
  (isFirst(Tag) -> true ; force_variable(Tag)),
  push_args(X,V,L,LX),
  push_v(-1,V),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
355
  get_P(V,P),
356 357
  (
    Tail=#t ->
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
358
       LX=[[Set,Tag],[cc,P]|LL];
359 360 361 362
       LX=[[Set,Tag]     |LL]  
  ).

compile_goal(once(G),Tail,V,[L,LL]) :- !,
363 364 365 366 367 368 369 370 371
  var_p(VP),
  var_p(VS),
  new_var(VP,V,TagP1),
  new_var(VS,V,TagS1),
  compile_goal(call_(G,A,Al,[C0|_],Pre,Post,LP),#f,V,[LX,LLX]),
  new_var(VP,V,TagP2),
  new_var(VS,V,TagS2),
  get_C(V,C),    
  Al=[A0,Cut_p],
372
  (
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    A==#t ->
    (
      Cut_p==#t ->
      (
        Pre  = ['store-ps'  , A0,TagP1,TagS1   ],
        Post = [['goto-inst' , Out              ],
                [label       , A0               ],
                [fail-psc    , TagP2,TagS2,C0   ],
                [label,Out                      ],
                ['softie-psc', TagP2,TagS2,C0   ]|LP]
      ) ;
      (
        Pre  = ['store-ps',TagP1,TagS1],
        Post = [['softie-ps',TagP2,TagS2]|LP]
      )
   ) ;
   (
     Cut_p==#t ->
      (
        Pre  = ['store-p',A0,TagP1],
        Post = [['goto-inst',Out],
                [label      ,A0],
                [fail-pc    ,TagP2,C0],
                [label,Out],
                ['softie-pc',TagP2,C0]|LP]
      ) ;
      (
        Pre  = [true],
        Post = LP
      )
   )
404 405 406 407 408 409 410 411
  ).
  
compile_goal((X->Y),Tail,V,L) :- !,
  compile_goal((once(X),Y),Tail,V,L).

compile_goal(\\+\\+\\+X,Tail,V,[L,LL]) :- !,
   compile_goal(\\+X,Tail,V,[L,LL]).

412 413
compile_goal(\\+\\+X,Tail,V,[L,LLL]) :- !,
   tail(Tail,V,LL,LLL),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
414
   L=LX,
415
   get_QAESBB2(V,Q,AA,E,S,B,B2),
416
   set_QAE(V,[],[[0|_]],0),
417 418 419 420 421 422 423
   var_p(VP),   
   var_p(VS),
   var_p(VT),
   get_F(V,F), 
   add_var_f(VP,V,F,TagP1),
   add_var_f(VS,V,F,TagS1),
   add_var_f(VT,V,F,TagT1),
424
   compile_goal(call_(X,A,Ad,[C0|_],Pre,Post,LP),#f,V,[LX,LLX]),
425 426 427
   add_var_f(VP,V,F,TagP2),
   add_var_f(VS,V,F,TagS2),
   add_var_f(VT,V,F,TagT2),
428
   set_QAESBB2(V,Q,AA,E,S,B,B2),
429 430 431 432 433 434 435 436 437 438 439
   (A=[[0|_]|_] -> Tp is 0 ; Tp is 1),
   Ad=[A0,Cut_p],
   ( 
    A==#t ->
    (
     Cut_p == #t ->
     (
       Tp == 0 ->
       (
         Pre  = ['newframe-ps', A0, TagP1, TagS1],
         Post = [['goto-inst' , Out],
440
                 [label       , A0],
441 442 443 444 445 446 447 448 449 450 451
                 ['fail-psc'  , TagP2, TagS2, C0],
                 [label       , Out],
                 ['unwind-psc', TagP2, TagS2, C0]|LP]
       ) ;
       (
         Pre  = ['newframe-pst', A0, TagP1, TagS1, TagT1],
         Post = [['goto-inst'  , Out],
                 [label        , A0],
                 ['fail-psc'   , TagP2, TagS2, C0],
                 [label        , Out],
                 ['unwind-psct', TagP2, TagS2, TagT2, C0]|LP]
452
       )
453 454 455 456 457 458 459 460
     ) ;
     (
       Tp == 0 ->
       (
         Pre =  ['newframe-ps', TagP1, TagS1],
         Post = [['unwind-ps' , TagP2, TagS2]|LP]
       ) ;
       (
461 462
         Pre =  ['newframe-pst', A0, TagP1, TagS1, TagT1],
         Post = [['unwind-pst' , A0, TagP2, TagS2, TagT2]|LP]
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
       )
     )
    );
    (
     Cut_p == #t ->
     (
         Pre  = ['store-p'  , A0, TagP1],
         Post = [['goto-inst', Out      ],
                 [label      , A0       ],
                 ['fail-pc'  , TagP2, C ],
                 [label      , Out      ],
                 ['restore-c', C0       ]|LP]
     ) ;
     (
         Pre =  [true],
         Post = LP
     )
480
    )
481 482 483
 ).

compile_goal(\\+X,Tail,V,L) :- !,
484
  check_tail(Tail),
485 486 487 488
  new_var(VS,V,TagS1),
  new_var(VP,V,TagP1),
  new_var(VT,V,TagT1),
  get_QAESB(V,Q,A,E,S,B),
489
  
490 491
  ifc(
  (
492 493
     set_QAE(V,[],[[0|_]],0),
     compile_goal(call_(X,A,Ad,[C0|_],Pre,Post,LP),#f,V,L)
494 495 496 497 498 499
  ),Er,
  (   
      tfc(Er),
      (Er==#t -> throw(#f) ; throw(#t))
  ),
  (
500 501 502
    new_var(VS,V,TagS2),
    new_var(VP,V,TagP2),
    new_var(VT,V,TagT2),
503
    get_A(V,A1),
504
    set_QAESB(V,Q,A,E,S,B),
505
    (A=[[0|_]|_] -> Tp is 0 ; Tp is 1),
506
    Ad=[A0,Cut_p],
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 532 533 534 535 536 537 538 539 540 541
    ( 
     A==#t ->
     (
       Cut_p == #t ->
       (
         Tp == 0 ->
           (
              Pre  = ['newframe-psc',Al,TagP1,TagS1], 
              Post = [['fail-psc',TagP2,TagS2,C0],
                      [label,Al],
                      ['unwind-psc',TagP2,TagS2,C0]|LP]
          );
          (
              Pre  = ['newframe-psct',Al,TagP1,TagS1, TagT1], 
              Post = [['fail-psc',TagP2,TagS2,C0],
                      [label,Al],
                      ['unwind-psct',TagP2,TagS2,TagT2,C0]|LP]
          )
      ) ;
      (
        Tp == 0 ->
          (
            Pre  = ['newframe-ps',TagP1,TagS1],
            Post = [['fail-p',TagP2],
                    [label,Al],
                    ['unwind-ps',TagP2,TagS2]|LP]
          );
          (
            Pre  = ['newframe-pst',TagP1,TagS1,TagT1],
            Post = [['fail-p',TagP2],
                    [label,Al],
                    ['unwind-pst',TagP2,TagS2,TagT2]|LP]
          )
      )
542
     );
543 544 545 546 547 548 549 550 551 552 553 554 555 556
     (
       Cut_p == #t ->
       (
          Pre  = ['store-p',Al,TagP1], 
          Post = [['fail-pc',TagP2,C0],
                  [label,Al],
                  ['restore-pc',TagP2,C0]|LP]      
      ) ;
      (
         Pre  = ['store-p',Al,TagP1],
         Post = [['fail-p',TagP2],
                 [label,Al],
                 ['restore-p',TagP2]|LP]
      )
557
     )
558
    )
559
  )).
560 561 562


compile_goal((m_and(Op,m_or(<,>,=<,>=,=:=,=\\=,@<,@=<,@>,@>=)))(X,Y),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
563 564
              Tail,V,[L,LLL]) :- !,
  write(compTail(Tail)),nl,
565
  check_tail(Tail),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
566
  tail(Tail,V,LL,LLL),
567
  get_P(V,P),
568 569 570 571 572 573 574 575 576 577
  ifc(compile_scm(X,V,L,LX),EX,
     (
       ifc(compile_scm(Y,V,L,LY),EY,
          (
             call(Op(EX,EY)) -> throw(#t) ; throw(#f)
          ),
          (
              push_v(-1,V),
              reverse_op(Op,Or),
              binop1L(Or,O),
578

579
              tr(O,OO),
580
              LY=[[OO,P,EX]|LLL]
581 582 583 584 585 586 587
         ))
     ),
     ifc(compile_scm(Y,V,LX,LY),EY,
       (
         push_v(-1,V),
         binop1L(Op,O),
         tr(O,OO),
588
         LX=[[OO,P,EY]|LLL]
589 590 591 592 593
       ),
       (
         push_v(-2,V),
         tr(Op,OOp),
         binop(OOp,O),
594
         LY=[[Op,P]|LLL]
595 596 597 598 599 600 601
       ))).

compile_goal(X is Y,Tail,V,L) :- !,
    (zero(V) -> 
      compile_goal((begin_att,iss(X,Y),end_att),Tail,V,L);
      compile_goal(iss(X,Y),Tail,V,L)).

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
602
compile_goal(iss(X,Y),Tail,V,[L,LLL]) :- !,
603
  check_tail(Tail),
604
  tail(Tail,V,LL,LLL),
605 606 607 608 609 610 611 612 613 614 615
  (
    instruction(Y) ->
    (
      instruction(X) ->
      (
        X==Y -> throw(#t) ; throw(#f)
      );
      (
        tr('unify-instruction-2',Unify),
        var_p(X),
        add_var(X,V,Tag),
616
        L=[[Unify,Tag,Y,#t]|LL]
617
      )
618
    );    
619 620 621 622 623 624 625 626
    ifc(compile_scm(Y,V,L,LX),EY,compile_goal(X is EY, Tail, V, [L,LL]),
    (
      var_p(X)   ->   
       ( 
         add_var(X,V,Tag),
         (isFirst(Tag) -> true ; touch_Q(12,V)),
         push_v(-1,V),
         tr(unify,Unify),
627
         LX=[[unify,Tag,#t]|LL]
628 629 630 631 632 633 634 635
       );
       (
         (
          constant(X) ->
             tr('equal-constant',Equal) ; 
          tr('equal-instruction',Equal)
         ),
         push_v(-1,V),
636
         LX=[[Equal,X]|LL]
637 638 639 640 641 642 643 644 645 646 647 648
       )
    ))).
   
compile_goal(unify_with_occurs_check(X,Y),Tail,V,L) :- !,
  touch_Q(13,V),
  (
    X==Y    -> throw(#t) ;
    zero(V) -> 
      compile_goal((begin_att,uni_0(X,Y),end_att),Tail,V,L);
      compile_goal(uni_0(X,Y),Tail,V,L)
  ).

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
649
compile_goal(uni_0(X,Y),Tail,V,[L,LLL]) :- !,
650
  check_tail(Tail),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
651 652
  compile_unify(X,Y,V,[L,LLL],0),
  tail(Tail,V,LL,LLL).
653 654 655 656 657 658 659 660 661 662 663


compile_goal(X = Y,Tail,V,L) :- !,
  touch_Q(14,V),
  (
    X==Y    -> throw(#t) ;
    zero(V) -> 
      compile_goal((begin_att,uni_x(X,Y),end_att),Tail,V,L);
      compile_goal(uni_x(X,Y),Tail,V,L)
  ).

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
664
compile_goal(uni_x(X,Y),Tail,V,[L,LLL]) :- !,
665
  check_tail(Tail),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
666 667
  compile_unify(X,Y,V,[L,LL],#t),
  tail(Tail,V,LL,LLL).
668 669 670 671 672

compile_goal(X == Y,Tail,V,L) :- !,
  (
    X==Y    -> throw(#t) ;
    zero(V) -> 
673
      compile_goal((begin_att,unim(X,Y),end_att),Tail,V,L);
674 675 676
      compile_goal(uni(X,Y),Tail,V,L)
  ).

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
677
compile_goal(uni(X,Y),Tail,V,[L,LLL]) :- !,
678
  check_tail(Tail),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
679 680
  compile_unify(X,Y,V,[L,LL],#t),
  tail(Tail,V,LL,LLL).
681

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
682
compile_goal(unim(X,Y),Tail,V,[L,LLL]) :- !,
683
  check_tail(Tail),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
684 685
  compile_unify(X,Y,V,[L,LL],#f),
  tail(Tail,V,LL,LLL).
686

Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
687
compile_goal(imprint(X,M),Tail,V,[L,LLL]) :- !,
688
  check_tail(Tail),
Stefan Israelsson Tampe's avatar
Stefan Israelsson Tampe committed
689 690
  compile_imprint(X,V,L,LL,M),
  tail(Tail,V,LL,LLL).
691

692
compile_goal(m_and(X,F(|AArgs)),Tail,V,L) :- !,
693
   (
694
    (var_p(F) ; isApply(AArgs)) -> compile_goal(call(X),Tail,V,L) ;
695 696
    (rev(AArgs,Args), caller(F,Args,Tail,V,L)),
    set_P(V,\"procedure\")
697 698 699 700 701 702 703 704 705 706 707 708 709
   ).

compile_goal(F,Tail,V,L) :-
  constant(F) -> (!,caller(F,[],Tail,V,L)).

compile_goal(X,_,_,_) :-
  throw(failed_compile_goal(X)).
      
isApply(X) :- var_p(X),!.
isApply([X|L]) :- isApply(L).

ncons(X,N) :-
  ncons(X,0,N).
710

711 712 713 714 715
")


(set! (@@ (logic guile-log guile-prolog vm vm-var) compile_goal)
  compile_goal)