func
func copied to clipboard
support functional predicate definition syntax
Ciao (with :=) and GRIPS (with <-) and Mercury (with =) provide special syntax for defining predicates that behave like functions.
In both GRIPS and Mercury, function definition implies determinism. I think Ciao makes a mistake here by allowing nondeterministic predicates to be defined with functional notation.
Using functional definitions should imply determinism. Mercury's = should be enough syntax. I've never seen =/2 as a top level functor. So, assuming --/1 performs decrement, our definition should be
fact(0) = 1
fact(N) = N * fact(--N)
which desugars into
fact(0, F) :-
!,
F = 1.
fact(N, F) :-
succ(Nminus, N),
fact(Nminus, F0),
F is F0*N.
There's no protection against fact(-1) looping forever, but most functional languages don't protect against that either.