Re: if then else




"bart demoen" <bmd@xxxxxxxxxxxxxx> wrote in message
news:pan.2008.02.23.19.55.38.462175@xxxxxxxxxxxxxxxxx
On Sat, 23 Feb 2008 10:57:38 -0500, Neng-Fa Zhou wrote:

With matching clauses, one always has to be aware about the catch 22: when
the argument is free, it fails. It is similar to using ==, but it is
hidden syntactically. The same is true in Guarded Horn Clauses (from which
your matching clauses inherited), and CHR: it is a pitfall, and the
language hides it.

Yes, Matching Clause was inspired by GHC. One correction: In GHC, a subgoal
is suspended not failed if it is not sufficiently instantiated. The
semantics of Matching Clause is simpler. For a subgoal C and a matching
clause H:-G : B, if there exists a substitution @ such that H@ is identical
to C and G@ succeeds then C can be reduced to B@ using the clause. So, for a
subgoal p(X) and a matching clause

p([H|T]):-true : Body.

the clause cannot be applied to the subgoal if X is free because no
substitution exists that can make p([H|T])@ identical to p(X).

Try to rewrite the following
matching clauses into readable if-then-else if you don't agree.

p([]):-true : true.
p([a,b,c|X]):-true : do_abc,p(X).
p([a,b,d|X]):-true : do_abd,p(X).
p([a,b|X]):-true : do_ab,p(X).

I will not take the challenge - I just want to say: if someone produces
such code for me in a serious project, I would consider firing him/her.

I am lucky that I don't work for you:-) This is a simplified version of the
peephole optimizer of the B-Prolog compiler. I guess you have a better way
to encode it.

Cheers,
Neng-Fa


.