caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Variant parameterized method?
@ 2003-07-22 20:09 Brian Hurt
  2003-07-22 22:06 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Brian Hurt @ 2003-07-22 20:09 UTC (permalink / raw)
  To: Ocaml Mailing List


What I want to do is write a class interface like:

class virtual ['a] foo :
object
	method virtual doit : 'a
	method map : 'b. ('a -> 'b) -> 'b foo
end

The map function returns a new foo (not necessarily a new member of 
whatever derived from foo class the function is actually being called on) 
parameterized on the variant type.  This would be implemented more or less 
like:

class ['a] mapped_foo f_init e_init =
object
	val f = f_init
	val e = e_init
	method doit = f (e#doit)
	method map g =
		let h x = g (f x) in
		mapped_foo g e
end

The first prototype, when I try to compile it, returns with:

File "foo.mli", line 2, characters 6-100:
The abbreviation foo is used with parameters 'a foo
wich are incompatible with constraints 'b foo

[sic on the wich- minor typo there]. It works just fine if I change map to 
return a 'b list, or 'b array, or 'b Stack.t- it seems to be just 'b foo 
that's a problem.  Am I missing something?  If not, what is the suggested 
way for doing this?  I need map as a member function- it needs to behave 
differently on different types, and as I don't have reflextion, I need it 
to be a member function.  I'd prefer to keep it as a class for taste 
reasons.

Help is appreciated.

Brian


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Variant parameterized method?
  2003-07-22 20:09 [Caml-list] Variant parameterized method? Brian Hurt
@ 2003-07-22 22:06 ` Jacques Garrigue
  2003-07-22 22:34   ` Brian Hurt
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2003-07-22 22:06 UTC (permalink / raw)
  To: brian.hurt; +Cc: caml-list

From: Brian Hurt <brian.hurt@qlogic.com>

> What I want to do is write a class interface like:
> 
> class virtual ['a] foo :
> object
> 	method virtual doit : 'a
> 	method map : 'b. ('a -> 'b) -> 'b foo
> end
> 
> The map function returns a new foo (not necessarily a new member of 
> whatever derived from foo class the function is actually being called on) 
> parameterized on the variant type.

This precise type is not admissible in the ocaml type system.
In ocaml recursive types must be regular: only 'a foo may occur in the
expansion of 'a foo.

This problem is discussed in an answer to PR#1730 in the caml bug
database.
This can be solved by introducing an explicit wrapper.

I give two versions of the code: with a polymorphic reference and with
a recursive modules. They are strictly equivalent: one is not safer
than the other (at least currently).

(* polymorphic reference *)
type 'a c0 = C of < map : 'b. ('a -> 'b) -> 'b c0 >

type m = {mutable new_c : 'a. 'a -> 'a c0}
let m = {new_c = fun _ -> failwith "new_c"}

class ['a] c (x : 'a) = object
  method map : 'b. ('a -> 'b) -> 'b c0 = fun f -> m.new_c (f x)
end
let () = m.new_c <- fun x -> C (new c x)

(* recursive module *)
module rec M : sig
  class ['a] c : 'a -> object
    method map : ('a -> 'b) -> 'b M.c_t
  end
  type 'a c_t = C of 'a c
end = struct
  class ['a] c (x : 'a) = object (_ : _ #M.c)
    method map = fun f -> M.C (new M.c (f x))
  end
  type 'a c_t = C of 'a c
end

Not that the fact the result of #map is a ['b c_t] rather than a ['b
c] is not a practical problem: you can just write
  let C x' = x#map f
If you prefer a [(x#map f).c] notation, use a one-field record rather
than a one constructor sum.

If you think (as I do) that all these examples are just too
complicated in practice, there is a simpler way to go:
only define a fold method in your class, and define map itself out of
the class.

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Variant parameterized method?
  2003-07-22 22:06 ` Jacques Garrigue
@ 2003-07-22 22:34   ` Brian Hurt
  0 siblings, 0 replies; 3+ messages in thread
From: Brian Hurt @ 2003-07-22 22:34 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: Ocaml Mailing List

Thanks for your help.

On Wed, 23 Jul 2003, Jacques Garrigue wrote:

> This precise type is not admissible in the ocaml type system.
> In ocaml recursive types must be regular: only 'a foo may occur in the
> expansion of 'a foo.

Ah.  Any hope of getting this fixed?

> 
> This problem is discussed in an answer to PR#1730 in the caml bug
> database.

Unfortunately, I don't speak french.

> This can be solved by introducing an explicit wrapper.

This is one possibility.

> If you think (as I do) that all these examples are just too
> complicated in practice, there is a simpler way to go:
> only define a fold method in your class, and define map itself out of
> the class.

Maybe I'm asking the wrong question.  I need/want map to behave 
differently when called on different subtypes of foo.  In a pseudo 
java/ocaml mix, I want to do:

let map f x =
    if x instanceof 'a foo_map then
        let e = ('a foo_map) in
        (* various code dealing with an already mapped foo *)
    else
        (* various code dealing with an arbitrary unmapped foo *)

The obvious way to do this to me is to make map a member function.  But 
this wouldn't be the first time I was missing something obvious.

Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-07-22 22:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-22 20:09 [Caml-list] Variant parameterized method? Brian Hurt
2003-07-22 22:06 ` Jacques Garrigue
2003-07-22 22:34   ` Brian Hurt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).