From: "Sébastien Furic" <sebastien.furic@lmsintl.com>
To: <caml-list@inria.fr>
Subject: [Caml-list] Questions about default instances
Date: Thu, 4 Aug 2011 18:22:31 +0200 [thread overview]
Message-ID: <4E3AC747.2010405@lmsintl.com> (raw)
Hello,
What is the usual way in OCaml to define mutually recursive classes
that share default instances? In order to illustrate my problem, let's
suppose we would like to define a hierarchy of classes defining booleans
/à la/ Smalltalk:
-8<-------------------------------------------------------------
class virtual object_ = object
method virtual to_string: string
end
class virtual boolean = object
inherit object_
method virtual not_: boolean
method virtual or_: boolean Lazy.t -> boolean
method virtual and_: boolean Lazy.t -> boolean
method virtual if_: 'a . 'a Lazy.t -> 'a Lazy.t -> 'a
end
class false_class = object (self)
inherit boolean
method to_string = "false"
method not_ = new true_class
method or_ chk = Lazy.force chk
method and_ _ = (self :> boolean)
method if_ _ chk = Lazy.force chk
end
and true_class = object (self)
inherit boolean
method to_string = "true"
method not_ = new false_class
method or_ _ = (self :> boolean)
method and_ chk = Lazy.force chk
method if_ chk _ = Lazy.force chk
end
(* Default instances (that I would have preferred to inject
directly in definitions above) *)
let false_ = new false_class
and true_ = new true_class
-8<-------------------------------------------------------------
Methods corresponding to message not_ in both false_class and
true_class should ideally return a instance of, respectively, true_class
and false_class that would have been created once and for all (instead
of a fresh instance each time they are invoked). How does one achieve
this in OCaml the functional way? (i.e., without resorting to
references, but also, ideally, without resorting to lazy values)
The use of immediate objects is IMO a better choice to implement
booleans (because I don't want nor need to let users subclass
false_class and true_class). Here is an attempt:
-8<-------------------------------------------------------------
(* Using the same definition of boolean above *)
let rec false_ = object (self)
inherit boolean
method to_string = "false"
method not_ = true_
method or_ chk = Lazy.force chk
method and_ _ = self
method if_ _ chk = Lazy.force chk
end
and true_ = object (self)
inherit boolean
method to_string = "true"
method not_ = false_
method or_ _ = self
method and_ chk = Lazy.force chk
method if_ chk _ = Lazy.force chk
end
-8<-------------------------------------------------------------
I get “Error: This kind of expression is not allowed as right-hand
side of `let rec'”. I wonder why OCaml does not accept the definition of
recursive values like above (notice that references to false_ and true_
are “protected” by method definitions). Wouldn't it be safe to extend
recursive definitions with this pattern?
Cheers,
Sébastien.
next reply other threads:[~2011-08-04 16:22 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-04 16:22 Sébastien Furic [this message]
2011-08-08 7:14 ` Jacques Garrigue
2011-08-16 8:42 ` Sébastien Furic
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4E3AC747.2010405@lmsintl.com \
--to=sebastien.furic@lmsintl.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox