Questions tagged [mercury]

Mercury is a purely declarative logical/functional language. It features a strong, static, polymorphic type system, as well as a strong mode and determinism systems. The type system is similar to that of Haskell, while the syntax is derived from Prolog's.

The Mercury Project

Mercury is a purely declarative logical/functional programming language. It is a very high-level language that allows programmers to concentrate on the problem rather than the low-level details such as memory management. Unlike other high-level languages Mercury is designed for the construction of large, reliable, efficient software systems by teams of programmers. As a consequence, programming in Mercury has a different feel than programming in most other high-level languages.

Main Features

Mercury has several key features that give it its unique, although familiar (to Prolog programmers) flavour. For more details, consult the introductory documentation.

Purely declarative

Predicates and functions in Mercury do not have non-logical side effects. I/O is performed by library predicates that take an old state of the world and some other parameters, and return a new state of the world and possibly some other results. Haskell uses monads to ensure purity of I/O operations, Mercury does not require the use of Monads.

Strongly typed

Mercury's type system is based on many-sorted logic with parametric polymorphism, very similar to the type systems of modern functional languages such as ML and Haskell. Programmers must declare the types they need using declarations such as

:- type list(T)
    --->    [] 
    ;       [T | list(T)].  

:- type maybe(T)
    --->    yes(T)
    ;       no.

Programmers should declare the type signatures of the predicates they define, for example

:- pred append(list(T), list(T), list(T)).

The compiler infers the types of all variables for which there is no declaration in the program. Type errors are reported at compile time.

Strongly moded

The programmer should declare the mode of each parameter of a predicate. A parameter's mode describes the instantiation state change of a variable that is passed as an argument to a predicate. A predicate may have more than one mode. For example, append is usually used in at least these two modes:

:- mode append(in, in, out).
:- mode append(out, out, in).

in modes are input to the predicate. A variable passed as an in argument is ground before and after the call. out modes are output from the predicate, a variable passed as an out argument is free before the call but ground after the call.

If a predicate has only one mode, the mode information can be given in the predicate declaration.

:- pred factorial(int::in, int::out).

Strong determinism

For each mode of each predicate, the programmer should declare whether the predicate will succeed exactly once (det), at most once (semidet), at least once (multi) or an arbitrary number of times (nondet). These declarations are attached to mode declarations like this:

:- mode append(in, in, out) is det.
:- mode append(out, out, in) is multi.

:- pred factorial(int::in, int::out) is det.

A module system

Programs consist of one or more modules. Each module has an interface section that contains the declarations for the types, functions and predicates exported from the module, and an implementation section that contains the definitions of the exported entities and also definitions for types and predicates that are local to the module.

Higher-order programming

Mercury allows programming with closures, currying, and lambda expressions.

Strong error checking

The type, mode and determinism systems require the programmer to make assertions about their programs. These assertions are checked by the compiler, this can make various common programming errors impossible. For example, a pure Mercury program (a program containing only Mercury code) can never dereference a NULL pointer.

Very efficient

In comparison with existing logic programming languages, strong types, modes, and determinism provide the compiler with the information it needs to generate very efficient code.

Multiple back-ends

Mercury can compile to high-level C, low-level C (using C effectively as a portable assembler). It can also compile to Java, to C# and to Erlang, providing full FFI facilities in all supported targets.

Community

Mercury's home page, "The Mercury Project", is the hub of the community. From there you can find the documentation, access the mailing lists) (as well as their archives) and access the bug tracker. There is also an IRC channel, although it can be a little slow on the response times. (The Mercury community is not large, but it is friendly and helpful when it connects.)

44 questions
46
votes
6 answers

What other ways can state be handled in a pure functional language besides with Monads?

So I started to wrap my head around Monads (used in Haskell). I'm curious what other ways IO or state can be handled in a pure functional language (both in theory or reality). For example, there is a logical language called "mercury" that uses…
James Scourtos
  • 691
  • 6
  • 7
21
votes
4 answers

What is more interesting or powerful: Curry, Mercury or Lambda-Prolog?

I would like to ask you about what formal system could be more interesting to implement from scratch/reverse engineer. I've looked through some existing and open-source projects of logical/declarative programming systems. I've decided to make up…
Bubba88
  • 1,910
  • 20
  • 44
20
votes
1 answer

How do Rust's ownership semantics relate to uniqueness typing as found in Clean and Mercury?

I noticed that in Rust moving is applied to lvalues, and it's statically enforced that moved-from objects are not used. How do these semantics relate to uniqueness typing as found in Clean and Mercury? Are they the same concept? If not, how do they…
user1804599
8
votes
1 answer

What benefit does types bring in logic programming languages like Mercury?

I've starting looking at the Mercury language, which seems very interesting. I'm a new to logic programming, but pretty experienced with functional programming in Scala and Haskell. One thing I've been pondering is why you need types in logic…
Jesper Nordenberg
  • 2,104
  • 11
  • 15
6
votes
3 answers

Does any version of Prolog support higher order abstraction of accumulators?

I was wondering about a Prolog that could include a built-in call like this: accum(generator, filter, accumulator) Calculates all solutions to generator. For each one, if filter can be proved, accumulator is proved. Backtracks to find all solutions…
Mark Green
  • 1,310
  • 12
  • 19
5
votes
1 answer

How to generate lists of fresh variables in Mercury like I can in Prolog?

In SWI Prolog, list(Xs) :- length(Xs, _). is "pure" in that I can pass it a variable with any sort of instantiatedness and it will nondeterministically unify it with all most general unifiers of a particular length. Is it possible to write a pure…
GeoffChurch
  • 395
  • 1
  • 13
5
votes
2 answers

What is a "strongly moded" programming language?

I was looking through the Mercury programming language's about page when I found a part where it said: Mercury is a strongly moded language What does this mean!? I've search all over the internet, and have found no answer!
xilpex
  • 3,097
  • 2
  • 14
  • 45
5
votes
3 answers

IDE or Editor with Support for Mercury

Are there any IDE's or editors that support Mercury besides emacs?
None
  • 3,875
  • 7
  • 43
  • 67
5
votes
1 answer

Mercury installation

I already download Mercury 11.07.1 from http://www.mercury.csse.unimelb.edu.au/download/release.html and also installed cygwin on my Windows 7 PC. However i don't know how to install the mercury. Is there anyone can help me with the installation…
Hello_World
  • 101
  • 3
  • 10
4
votes
1 answer

Mercury: Determinism and pattern matching

I have a semideterministic function. When I re-write it to use pattern matching instead of an if statement, Mercury says it becomes nondeterministic. I'd like to understand why. The original code: :- pred nth(list(T), int, T). :- mode nth(in, …
Evan
  • 2,400
  • 1
  • 18
  • 34
4
votes
3 answers

Mercury: How to declare determinism of a higher-order data type?

When I compile the Mercury code below, I get this error from the compiler: In clause for `main(di, uo)': in argument 1 of call to predicate `test_with_anonymous_functions.assert_equals'/5: mode error: variable `V_15' has instantiatedness `/*…
Evan
  • 2,400
  • 1
  • 18
  • 34
4
votes
1 answer

Are algebraic predicates supported in Mercury?

I'm very new to Mercury and logic programming in general. I haven't found a numeric example like this in the docs or samples... Take the example predicate: :- pred diffThirtyFour(float, float). :- mode diffThirtyFour(in, out) is…
m88
  • 93
  • 1
  • 5
4
votes
1 answer

Need an example of Record Syntax in mercury

I am new to mercury and am trying to wrap my head around Record Syntax, but the Reference Manual is the only place I have encountered it and it leaves me mystified: Term ^ field1(Arg1) ^ field2(Arg2, Arg3) is equivalent to field2(Arg2, Arg3,…
SourceSimian
  • 682
  • 4
  • 18
3
votes
1 answer

Mercury "undefined reference" compilation error when using local module

I have a module exporting nat/1 to test/generate natural numbers: :- module nat. :- interface. :- import_module int. :- pred nat(int). :- mode nat(in) is det. :- mode nat(out) is multi. :- implementation. :- pragma…
GeoffChurch
  • 395
  • 1
  • 13
3
votes
3 answers

ADT properties in Mercury

I wander why Mercury (10.04) can't infer determinism of next snippet: :- pred load_freqs(int::in, io.res(list(float))::out, io::di, io::uo) is det. load_freqs(CPU, ResFreqs, !IO):- open_input(cpu_fn(CPU, "available_frequencies"), ResStream,…
ony
  • 12,457
  • 1
  • 33
  • 41
1
2 3