1:- module(w, []). 2 3% :- use_module(engine). 4:- use_module(util('swap-args')). 5% :- expects_dialect(pac). 6term_expansion --> pac:expand_pac. 7% term_expansion --> expand_pac. 8:- use_module(pac(op)). 9:- style_check(-singleton). 10 11% ?- w:swap_args_of(f, 1, 2,` f([], [])`, X), smash(X). 12% ?- w:swap_args_of(f, 1, 2,` f([a,b], [[]])`, X), smash(X). 13% ?- w:swap_args_of(f, 1, 2,`f(a, g(b))`, X), smash(X). 14% ?- w:swap_args_of(f, 1, 2,`f([], [])`, X), smash(X). 15% ?- w:swap_args_of(f, 2, 3,`f(a, b, c)`, X), smash(X). 16% ?- w:swap_args_of(f, 2, 3,`f(a, b, c) + x`, X), smash(X). 17% ?- w:swap_args_of(f, 2, 3,` f(a, b, c, d`, X), smash(X). 18 19 20% ?- w:easy_arith(1 + 0, X). 21%@ X = 1 . 22%@ X = 1 . 23% ?- w:easy_arith(pred(0), X). 24%@ X = pred(0). 25%@ X = 0 . 26% ?- w:easy_arith(2 + 2, X). 27% ?- w:easy_arith(2 * 2, X). 28%@ X = 4 . 29% ?- w:easy_arith(2 + 2 * 3, X). 30%@ X = 8 . 31% ?- w:easy_arith((1+ 1) + ((2 + 1) + 3), X). 32%@ X = 8 . 33%@ X = 8 . 34%@ X = 8 35% ?- trace, w:easy_arith(add(2,1), X). 36%@ X = 5 . 37 38 39 40int_mul(predec(0),0):-! . 41int_mul(predec(N),A1):-succ(N0,N),!,int_mul(N0,A1) . 42int_mul(X+Y,A1):-!,(int_mul(X,A2),int_mul(Y,A3)),int_mul(add(A2,A3),A1) . 43int_mul(add(N,0),A1):-!,int_mul(N,A1) . 44int_mul(add(A,B),A1):-!,(int_mul(predec(B),A2),int_mul(add(A,A2),A3)),succ(A3,A1) . 45int_mul(A*B,A1):-!,(int_mul(A,A2),int_mul(B,A3)),int_mul(mul(A2,A3),A1) . 46int_mul(mul(A1,0),0):-! . 47int_mul(mul(A,N),A1):-!,((int_mul(predec(N),A2),int_mul(mul(A,A2),A3)),int_mul(A,A4)),int_mul(A3+A4,A1) . 48int_mul(N,N):-! . 49 50 51% ?- listing(w:easy_arith). 52%@ easy_arith(predec(0), 0). 53%@ easy_arith(predec(A), C) :- 54%@ succ(B, A), 55%@ easy_arith(B, C). 56%@ easy_arith(A+B, E) :- 57%@ easy_arith(A, C), 58%@ easy_arith(B, D), 59%@ easy_arith(add(C, D), E). 60%@ easy_arith(add(A, 0), B) :- 61%@ easy_arith(A, B). 62%@ easy_arith(add(B, A), E) :- 63%@ easy_arith(predec(A), C), 64%@ easy_arith(add(B, C), D), 65%@ succ(D, E). 66%@ easy_arith(A*B, E) :- 67%@ easy_arith(A, C), 68%@ easy_arith(B, D), 69%@ easy_arith(mul(C, D), E). 70%@ easy_arith(mul(_, 0), 0). 71%@ easy_arith(mul(B, A), F) :- 72%@ easy_arith(predec(A), C), 73%@ easy_arith(mul(B, C), D), 74%@ easy_arith(B, E), 75%@ easy_arith(D+E, F). 76%@ easy_arith(A, A). 77%@ 78%@ true.