[mercury-users] How did Mercury do away with the cut?

Richard A. O'Keefe ok at atlas.otago.ac.nz
Fri Jul 6 14:33:40 AEST 2001


Fergus Henderson wrote:

	3. If-then-else.
	
		In Prolog, cut is often (mis-)used as a way of writing if-then-else.
		Prolog has an if-then-else construct which can be used instead,
		but some Prolog programmers prefer to use cut, for reasons that
		I have difficulty understanding and definitely don't agree with.
		In Mercury, you have to use if-then-else in this situation,
		so in a sense, Mercury enforces good style.
	
Historically, cut existed in Prolog long before the if-then-else did.
Some noted Prolog programmers formed their Prolog style at a time when
you _had_ to use cut because there wasn't any if-then-else to use instead.
Having used both styles, I must say that I find the if-then-else version
much clumsier, but I quite perceive its advantages.  However, it is
perfectly possible to automatically transform a wide range of cut-containing
predicates into if-then-else code, so there's no reason in principle why
Mercury couldn't allow a cut-like notation with if-then-else semantics.
(Whether that's a good idea is another matter.)

		Note that Mercury's if-then-else is also much better behaved than
		Prolog's.  Prolog's if-then-else always commits to the first solution
		of the condition, which gives it many of the same problems as cut.
		Mercury's if-then-else is quite capable of returning multiple
		solutions for the condition, on backtracking, if the condition
		happens to be non-deterministic.
	
Many Prolog implementations provide something called a "soft cut".
Basically,
    p(...) :- ~1~, 'soft !', ~2~.
    p(...) :- ~3~.
is equivalent to Mercury's
    p(...) :- if ~1~ then ~2~ else ~3~.
Allowing this in arbitrary contexts is quite tricky; I was able to
implement a soft-cut if-then-else in C Prolog in just a few lines of
C, but never found a use for it.

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list