[m-users.] Functions vs Predicates?

Qqwy/Wiebe-Marten qqwy at gmx.com
Thu Oct 29 06:19:12 AEDT 2020

Dear members of the Mercury users mailing list,

I am a programmer with quite a bit of background in Haskell and
Elixir/Erlang (as well as some other languages) and some beginner
experience in Prolog.

As I like statically-typed languages very much, I was excited to
discover Mercury and have now read through the various beginner
tutorials and a big part of the users guide and language reference.

There is one thing that is puzzling me, however: Mercury has both
`pred`icates and `func`tions.
At first I thought that `func`s were "syntactic sugar" around a
predicate which always flows in a single direction
(which therefore would need less manual type annotations as the compiler
does not need to infer as much).

However, later on I read that `func`s can also be used backward.
This puzzles me, because now I am confused as to when I should use a
`pred` and when a `func` in my own code.
What confuses me further is that there are e.g. functions like
`univ_to_term` and `term_to_univ`
which seem like perfect candidates to have been written as a single
reversible `term_univ(Term, Univ)` predicate instead.

When is it more idiomatic to use a function, and when to use a predicate?

Thank you,

~Marten / Qqwy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://lists.mercurylang.org/archives/users/attachments/20201028/d58ae27b/attachment.sig>

More information about the users mailing list