The Mercury Frequently Asked Questions list

Copyright (C) 1995 University of Melbourne.

Permission is granted to make and distribute verbatim copies of this FAQ list provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this FAQ list under the conditions for verbatim copying, provided also that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this FAQ list into another language, under the above conditions for modified versions.

Common programming errors

  1. What does the error message "undefined symbol `'.'/2'" mean?

    You need to explicitly import the `list' module

    :- import_module list.
    
    if your code uses lists.

  2. Why doesn't `X <= 3' work?

    In Mercury, less-than-or-equal to is written as `=<' not as `<=', which is used for reverse implication.

  3. I defined a type like this: `:- type number ---> int ; float.' Why doesn't this work?

    You tried to define a type that is an undiscriminated union of two types, which is not allowed by the Mercury type system. The declaration above defines an enumerated type with two constants, "int" and "float". This is not what you want, but it is legal Mercury, which is why you don't get an error message on the type declaration itself.

  4. I get a "scope error" in an if-then-else. I checked and both branches bind the same variables. What is the error?

    This error is generally happens if you attempt bind non-local variables in the condition of the if-then-else. For example, the following code attempts to bind `Value' in the call to `map__search', but `Value' occurs outside of the if-then-else -- in particular, it occurs in the head of the clause.

    :- pred map__search(map(K, V), K, V).
    :- mode map__search(in, in, out) is semidet.
    
    :- pred lookup(map(string, int), string, int).
    :- mode lookup(in, in, out) is det.
    
    lookup(Map, Key, Value) :-
            (if map__search(Map, Key, Value) then
                    true
            else
                    Value = -1
            ).
    
    Binding non-local variables in the condition of an if-then-else is forbidden since it would be unsound; it would lead to inconsistent results. For example, `(X = 1 -> Y = 1 ; Y = 2), X = 2' would fail, but `X = 2, (X = 1 -> Y = 1 ; Y = 2)' would succeed -- breaking one of the fundamental laws of logic, `(P, Q) <=> (Q, P)'. Mode analysis therefore rejects such programs. (In certain rare circumstances, the compiler may report this as a "mode error" rather than a "scope error".) The way to fix such errors is to avoid binding non-local variables in the condition, and instead bind them in the then part of the if-then-else. So in the above example, you should introduce a new local variable, which we will call `Value1':
    lookup(Map, Key, Value) :-
            (if some [Value1]
                    map__search(Map, Key, Value1)
            then
                    Value = Value1
            ;
                    Value = -1
            ).
    
    The explicit existential quantifier is optional; if you prefer a slightly more succinct style you can write this as
    lookup(Map, Key, Value) :-
            ( map__search(Map, Key, Value1) ->
                    Value = Value1
            ;
                    Value = -1
            ).
    

Problems caused by unimplemented Mercury features

  1. How can I avoid getting a compile-time error when I try to fill in a partially instantiated data structure?

    At the moment, you can create a partially instantiated data structure, but you can't fill in the holes. The reason is that the code that does the filling in must temporarily alias two variables together, and the current mode checker does not allow this. This limitation will go away in the future.

  2. I'm getting an error from the C compiler:
    foo.c:45: redeclaration of `mercury_const_3'
    foo.c:37: `mercury_const_3' previously declared here
    
    Is this a bug?

    Yes, this is a known bug in the Mercury compiler (sorry!). The work-around is to compile with the `--no-static-ground-terms' option.

What to do when all else fails

  1. I'm getting an error message that I don't understand. What can I do?

    Try compiling with the `-E' (`--verbose-errors') option. This option causes the compiler to give even more verbose descriptions than usual.

  2. I followed the instructions in the user's guide, but it still didn't work. What do I do next?

    Send email to mercury-bugs@cs.mu.oz.au, and we'll try to solve your problem.