Replies: 4 comments 32 replies
-
Putting goals into lists is not a good choice since this cannot be covered/fully typed with meta-predicate declarations. |
Beta Was this translation helpful? Give feedback.
-
cond_t([], false).
cond_t([Goals,Effects|Conds], Truth) :-
if_(Goals,
( Effects, Truth=true
; cond_t(Conds, Truth) % ** allows for all possible true goals
),
cond_t(Conds, Truth)
). Edit: @UWN 's teaching me a thing or two about Prolog API 😁 |
Beta Was this translation helpful? Give feedback.
-
I think you can already do it using cond(Goals, Effects, Default) :-
foldl(\If^Then^Else^if_(If,Then,Else)^true, Goals, Effects, Default, Cond_0), Cond_0. And then query it like this: ?- cond([X=1,X=2], [Y=a,Y=b], Y=c).
X = 2, Y = b
; X = 1, Y = a
; Y = c, dif:dif(X,1), dif:dif(X,2). |
Beta Was this translation helpful? Give feedback.
-
I'm honestly astonished this works. Too good to be true? :- use_module(library(reif)).
:- op(1100, xfy, ::).
:- op(1050, xfy, ??).
??(A,B) :- if_(A, B, false).
??(A,B,T) :-
if_(A,
( B -> T=true
; T=false
),
T=false
).
::(A,B) :- (A;B).
?- ( X=1
?? Y=a
:: X=2
?? Y=b
:: X=3
?? Y=c
:: X=4,Y=d
).
%@ X = 1, Y = a
%@ ; X = 2, Y = b
%@ ; X = 3, Y = c
%@ ; X = 4, Y = d.
?- ( X=1, Y=2
?? Z=a
:: X=3, Y=5
?? Z=b
:: X=6, Y=7
?? Z=c
:: format("hey", []), nl, X=9, Y=11, Z=d
).
%@ X = 1, Y = 2, Z = a
%@ ; X = 3, Y = 5, Z = b
%@ ; X = 6, Y = 7, Z = c
%@ ; hey
%@ X = 9, Y = 11, Z = d.
?- X=1 ?? Y=2.
%@ X = 1, Y = 2
%@ ; false.
?- X=1 ?? Y=2 :: Y=3.
%@ X = 1, Y = 2
%@ ; Y = 3.
?- ( X=1
?? false, Z=a
:: X=2
?? Z=b
:: X=3
?? Z=c
:: format("hey", []), nl
).
%@ X = 2, Z = b
%@ ; X = 3, Z = c
%@ ; hey
%@ true.
?- ( X=1
?? Z=a
:: X=2
?? Z=b
).
%@ X = 1, Z = a
%@ ; X = 2, Z = b
%@ ; false.
|
Beta Was this translation helpful? Give feedback.
-
In lisp,
cond
is used the wayif/elseif
is used in other languages.The following modification to
reif
, an addition ofcond_t/2
would allow for similar syntax:Such as:
otherwise I just end up finding myself doing nested
if_
s, which is a bummer.Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions