[mercury-users] 're-creating' higher order insts?

Ian MacLarty maclarty at csse.unimelb.edu.au
Mon Oct 8 17:43:22 AEST 2007

On Mon, Oct 08, 2007 at 05:16:14PM +1000, Mark Brown wrote:
> On 08-Oct-2007, Ondrej Bojar <bojar at csse.unimelb.edu.au> wrote:
> > Mark Brown wrote:
> >> This is on the right track.  You want something like
> >> :- pred assert_function_is_inst_foo(..., func_type::in, 
> >> func_type::out(foo))
> >> 	is {semi,}det.
> >
> > Exactly. It's just my limited English (or possibly any natural language 
> > skills) that I needed that many words. I should have written Mercury 
> > instead. :-)
> >
> >> 1) You can replace the static checking with dynamic checking.  Your
> >> application will have a fixed set of insts that it is interested in, so
> >> you can create a type to describe every possible inst.  Then construct a
> >> value of this type each time you put a function in the ADT, and make sure
> >> you keep track of which value goes with with function (e.g., by packaging
> >> them up in the same term).  You can pass this information in the ... part
> >> of the assert_function_is_inst_foo predicate, above, which can throw an
> >> exception or fail if it is anything other than "foo".
> >
> > I understand this technique, but I don't see how to do the final assert. Or 
> > is
> > this definition of the assert_function_is_inst_foo above valid?
> >
> > assert_function_is_inst_foo(..., Func, Func).
> >
> > I'd expect to need some magical promise_I_know_the_inst_better_than_you 
> > call.
> Yes, I skimmed over that bit.  Ultimately there will be a call to
> something like "unsafe_promise_function_is_inst_foo", which has a
> definition equivalent to
> 	unsafe_promise_function_is_inst_foo(Func, Func)
> but which is written (trivially) using the foreign language interface.
> Going via foreign code gets around the compiler checking and so allows the
> more specific inst to be given for the result value.

Another way to store predicates in polymorphic data structures is to
define a typeclass that has a method with the signature you want.  Then
define a separate type for each predicate you want to store in the ADT.
Then use an existential type like the following to store the values in
the ADT:

:- type some_pred ---> some [T] some_pred(T) => my_pred_tc(T).

mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au

More information about the users mailing list