[mercury-users] Higher-order semideterminism in discriminated unions

api at gmx.de api at gmx.de
Tue Aug 26 20:36:02 AEST 2003


When working with higher-order predicates that get a function as an
argument and have to store in a data structure defined by a
discriminated union, everything works only when the declared determinism
of the function argument is det. But as soon as it is changed to semidet
or worse, the compiled program crashes with a segmentation fault at
runtime. Also the mercury debugger produces invalid results.

For example
:- module tst_hol_semidet.
:- interface.
:- import_module io.

:- pred main(io__state::di, io__state::uo) is det.

:- type storage(Y).
:- pred do(func(string)=Y, storage(Y)) .
:- mode do(in(func(in)=out is semidet), out) is det.

:- func use(storage(Y), string) = Y.
:- mode use(in, in) = out is semidet.

:- implementation.
:- import_module string, require.

:- type storage(Y) ---> store(func(string)=Y).

do(F, store(F)).

use(store(F), A) = F(A).

main -->
        (func(A::in) = ((A ++ "1")::out) is semidet
        :- A \= "No"
    { Res1 = use(Storage, "Yes") ->
      R1 = Res1
      error("Dunno Yes")

    { Res2 = use(Storage, "No") ->
      R2 = Res2
      error("Dunno No")
    {error("did not work")}.

Contrary to the det version, this program crashes with
*** Mercury runtime: caught segmentation violation ***
This may have been caused by a stack overflow, due to unbounded
exiting from signal handler

>From the manuals I quote the "higher-order mode" section from which I
understand that Determinism=semidet should be legal.

"In order to call a higher-order term, the compiler must know its
higher-order inst. This can cause problems when higher-order terms are
placed into a polymorphic collection type and then extracted, since the
declared mode for the extraction will typically be `out' and the
higher-order inst information will be lost. To partially alleviate this
problem, and to make higher-order functional programming easier, if the
term to be called has a function type, but no higher-order inst
information, we assume that it has the default higher-order function
inst `func(in, ..., in) = out is Determinism'."

Does anyone know what to do then? I have tried to use instantiatedness
information for the storage but without success.


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