[mercury-users] RE:
Ralph Becket
rbeck at microsoft.com
Wed Sep 5 23:19:20 AEST 2001
> From: Peter Ross [mailto:peter.ross at miscrit.be]
> Sent: 05 September 2001 13:27
>
> :- module place.
>
> :- interface.
>
> % A place has a name.
> :- typeclass place(P) where [
> func name(P) = string
> ].
>
> % A place contains some objects of type T.
> :- typeclass place(P, T) <= place(P) where [
> % Get one of the T's stored in P.
> pred get(P::in, T::out) is nondet
> ].
>
> :- type existential_place
> ---> some [P, T] (p(P, T) => place(P, T)).
>
> :- type env == int.
>
> :- implementation.
>
> % Given an arbitary place determine it's environment.
> :- pred f(existential_place::in, (func(U) = env)::in, env::out) is
nondet.
>
> f(p(Place, _), F, Int) :-
> get(Place, T),
> Int = F(T).
I think the problem is in `get(Place, T)' - the type of T is
not constrained so the compiler can't work out which get/2 to
call (it needs to know the types of both Place and T); you
could use functional dependencies here.
You need to code it up slightly differently (disclaimer: this
is after a good pub lunch).
:- typeclass token(T) where [...].
:- typeclass place(P) where [
func name(P) = string,
some [T] pred get_token(P, T) => token(T),
mode get_token(in, out) is nondet
].
:- type env == int.
:- some [T] (pred env(P, ((func T) = T3), T3) => (foo:token(T)))
<= (foo:place(P)).
:- mode env(in, func(in) = out is det, out) is nondet.
env(P, F, E) :-
get_token(P, T),
E = F(T).
NOTE! Getting the type of env/3 right required resorting to
asking the compiler to do it for me. Neither of the following
worked:
:- pred env(P, (func(T) = int <= token(T)), env) <= place(P).
This looks correct to me, but unfortunately the type system
doesn't allow type class constraints on types (I'll ask DJ if
this is necessary or just an omission when he gets here next
week).
:- pred env(P, func(T) = int, env) <= (place(P), token(T)).
This looked less likely and also failed to work.
:- some [T] (pred env(P, ((func T) = T3), T3) => (foo:token(T)))
<= (foo:place(P)).
I don't understand why T has to be existentially quantified
here. The func argument should work for any T that is a token;
this looks like the func argument is dictating what T should be.
Bug or comprehension failure on my part?
- Ralph
--------------------------------------------------------------------------
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