From 714bf2cab6f1e7bd91d332c13682645d89cf340c Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 20 Jul 2019 16:45:28 -0700 Subject: [PATCH] Try to handle expressions in branch combinator. If the expression isn't 'true' or 'false' atoms then we assume it's a comparison expression and try to check its truth value. If this fails then it will try both branches, to allow for e.g. compilation. THis almost works, but there's a choice point or something that gets hit before it tries the false path, ?- joy(` [32 >] [++] [--] ifte`, Si, So). Si = [_2076|_2078], So = [_2076+1|_2078] ; wtf? + Si = [_2076|_2078], So = [[+], 1, _2076|_2078] ; Si = [_2076|_2078], So = [_2076-1|_2078] ; wtf? - Si = [_2076|_2078], So = [[-], 1, _2076|_2078] ; wtf? branch Si = [_2076|_2078], So = [[branch], [++], [--], _2076>32, _2076|_2078] ; wtf? swap Si = [_2076|_2078], So = [[swap, branch], [--], [++], _2076>32, _2076|_2078] ; wtf? first Si = [_2076|_2078], So = [[first, [++], [--], swap, branch], [_2076>32|_2078], _2076|_2078] etc... --- thun/thun.pl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/thun/thun.pl b/thun/thun.pl index bd98254..b235003 100644 --- a/thun/thun.pl +++ b/thun/thun.pl @@ -192,8 +192,15 @@ combo(dipd, [P, X, Y|S], S, Ei, Eo) :- append(P, [Y, X|Ei], Eo). combo(dupdip, [P, X|S], [X|S], Ei, Eo) :- append(P, [X|Ei], Eo). -combo(branch, [T, _, true|S], S, Ei, Eo) :- append(T, Ei, Eo). -combo(branch, [_, F, false|S], S, Ei, Eo) :- append(F, Ei, Eo). +combo(branch, [T, _, true|S], S, Ei, Eo) :- !, append(T, Ei, Eo). +combo(branch, [_, F, false|S], S, Ei, Eo) :- !, append(F, Ei, Eo). +combo(branch, [T, F, Expr|S], S, Ei, Eo) :- + catch( + (Expr -> append(T, Ei, Eo) ; append(F, Ei, Eo)), + _, + try_both_branches(T, F, Ei, Eo) % in case of error. + ). + combo(loop, [_, false|S], S, E, E ). combo(loop, [B, true|S], S, Ei, Eo) :- append(B, [B, loop|Ei], Eo). @@ -213,6 +220,10 @@ combo(genrec, [R1, R0, Then, If|S], append(R0, [Quoted|R1], Else). +try_both_branches(T, _, Ei, Eo) :- append(T, Ei, Eo). +try_both_branches(_, F, Ei, Eo) :- append(F, Ei, Eo). + + /* Compiler */