<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/07/dc704013788c4589829696f7dfb16ae4"
  from="Yaron M. Minsky &lt;yminsky@c...&gt;"
  author="Yaron M. Minsky"
  date="2003-07-01T10:37:10"
  subject="Re: [Caml-list] syntax of private constructors in CVS version"
  prev="2003/07/663279d54087691e1e8d563df218286c"
  next="2003/07/1fed8977474ed12631a858ab780ab9fc"
  prev-in-thread="2003/07/a6c7433918c84295b55fdd0b2150eca7"
  next-in-thread="2003/07/7f9e7fcd6f9f523709199fb503e1b759"
  prev-thread="2003/06/a03edde7de13ace25d84ede87d813c3f"
  next-thread="2003/06/5a327328e9738765806d584d52354b45"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/06/59218369fd3fa7dddfd9df9c7869c6bc"
  from="Shaddin Doghmi &lt;shaddin@m...&gt;"
  author="Shaddin Doghmi"
  date="2003-06-27T20:50:41"
  subject="[Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/06/b25986b3483edf8a66caeff6d4dfa914"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-06-27T21:29:12"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/06/6bf322f4a3555c3fcf5db70b3fe39eba"
  from="Shaddin Doghmi &lt;shaddin@m...&gt;"
  author="Shaddin Doghmi"
  date="2003-06-27T23:49:09"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/06/a59fcd293310f2643da245c0f5ae1f05"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-06-28T02:24:57"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/06/655ccd72cd43d8d9384c5f32806301cd"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2003-06-30T18:39:07"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/07/a6c7433918c84295b55fdd0b2150eca7"
  from="Hendrik Tews &lt;tews@t...&gt;"
  author="Hendrik Tews"
  date="2003-07-01T07:43:30"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
</msg>
<msg 
  url="2003/07/dc704013788c4589829696f7dfb16ae4"
  from="Yaron M. Minsky &lt;yminsky@c...&gt;"
  author="Yaron M. Minsky"
  date="2003-07-01T10:37:10"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
</msg>
<msg 
  url="2003/07/7f9e7fcd6f9f523709199fb503e1b759"
  from="Yaron M. Minsky &lt;yminsky@c...&gt;"
  author="Yaron M. Minsky"
  date="2003-07-01T10:51:32"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/07/d22a306d1dca8af537a49f31e0884038"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2003-07-01T17:05:49"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/07/7ea9688cc25a7ef880125869a2170e96"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-07-01T17:20:44"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
</msg>
</msg>
</msg>
<msg 
  url="2003/07/66caba7829a123e6bb39d1d9c4b9bb00"
  from="John Max Skaller &lt;skaller@o...&gt;"
  author="John Max Skaller"
  date="2003-07-03T11:46:27"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/07/1eccca803cd78c2ed7f87d84c07cacd3"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2003-07-07T22:31:28"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/07/91fc472a8ed631257e1e6ea2685aaa2f"
  from="John Max Skaller &lt;skaller@o...&gt;"
  author="John Max Skaller"
  date="2003-07-13T09:11:47"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/07/e1d3789e4095909694f1b11298762891"
  from="Laurent Vibert &lt;lvibert@i...&gt;"
  author="Laurent Vibert"
  date="2003-07-15T08:02:52"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/07/32a40dd0400e6e67ba0420eff5aa16c3"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2003-07-15T08:22:34"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
</msg>
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2003/06/a0187e5d430351691489ab943636ce50"
  from="Chris Hecker &lt;checker@d...&gt;"
  author="Chris Hecker"
  date="2003-06-30T19:02:27"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
<msg 
  url="2003/07/1fed8977474ed12631a858ab780ab9fc"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2003-07-01T10:47:36"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
</msg>
<msg 
  url="2003/06/811ca16749dd2b66fc09665a2a8f413b"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-06-30T21:36:57"
  subject="Re: [Caml-list] syntax of private constructors in CVS version">
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
A couple of thoughts:

      * First, this seem

On Mon, 2003-06-30 at 14:39, Pierre Weis wrote:
&gt; Hi Brian &amp; all,
&gt; 
&gt; [Sketching the semantics of private types, this message is long.] 
&gt; 
&gt; &gt; Private only works with new record types and sum types that you declared. 
&gt; &gt; Think about it. What would it mean to declare some set of polymorphic variant 
&gt; &gt; tags as "private"? 
&gt; 
&gt; You're absolutely right on your comments and explanation. However, I
&gt; think we also need to explain the ideas beneath private types and
&gt; their intended uses.
&gt; 
&gt; As you should already know, usual sum and product types in Caml
&gt; correspond to the mathematical notion of free algebraic data
&gt; structures. This is fairly useful and allows the modelization of a lot
&gt; of common data structures in practical programming
&gt; situations. However, besides the free algebra structures,
&gt; mathematicians use a lot of non-free algebras (so-called structures
&gt; defined via generators and relations). Private types aim at giving a
&gt; way to modelize those non-free structures. Equivalenetly, you can
&gt; think of private types as providing a way to implement types equipped
&gt; with invariants, quotient structures (i.e. sets of equivalence classes
&gt; for some equivalence relation on a free algebra), or data types with
&gt; canonical normal forms.
&gt; 
&gt; As an example, consider a simple modelization of natural integers in
&gt; unary notation:
&gt; 
&gt; type nat =
&gt;    | Zero
&gt;    | Succ of nat;;
&gt; 
&gt; The nat type is a regular sum type; a value of the nat type is either
&gt; the constant constructor Zero (modelizing the integer 0) or
&gt; constructed with any application of the constructor Succ to a nat
&gt; value (Succ n modelizing the successor of n or n + 1). Hence, the nat
&gt; type is adequate in the sense that it exactly modelizes the unary
&gt; representation of positive integers. Nothing new here, and we can
&gt; easily write a test to 0 as:
&gt; 
&gt; let is_zero = function
&gt;   | Zero -&gt; true
&gt;   | _ -&gt; false;;
&gt; 
&gt; Now, let's go one step further and imagine we modelize integers
&gt; (either positive or negative). Even if a complex (adequate)
&gt; modelization of those numbers using a regular Caml data type is not
&gt; very complex, let's take, for the sake of explanation, a simple
&gt; approach by generalizing the nat type; we simply add an extra Pred
&gt; constructor to modelize negative integers: Pred n is the predecessor
&gt; of n or n - 1:
&gt; 
&gt; type integer =
&gt;    | Zero
&gt;    | Succ of integer
&gt;    | Pred of integer;;
&gt; 
&gt; Now, we can modelize all integers in unary notation but the integer
&gt; type is no more adequate, in the sense that we have a problem of
&gt; unicity of representation: a given number can be modelized by
&gt; (infinitely) many distinct values of the integer data type. For
&gt; instance, the terms Succ (Pred Zero) and Zero all represent the number
&gt; 0. You may think that this is not a big problem but in fact it is very
&gt; ennoying, because when writing a function over values of type integer,
&gt; you must now pattern match a lot of patterns instead of the only one
&gt; that canonically represents a given integer. For instance
&gt; 
&gt; let is_zero = function
&gt;   | Zero -&gt; true
&gt;   | _ -&gt; false;;
&gt; 
&gt; is no more the expected predicate. We must write something much more
&gt; complex, for instance
&gt; 
&gt; let rec is_zero = function
&gt;   | Zero -&gt; true
&gt;   | Succ (Pred n) -&gt; is_zero n
&gt;   | Pred (Succ n) -&gt; is_zero n
&gt;   | n -&gt; false;;
&gt; 
&gt; (BTW: is this code now correct ?)
&gt; 
&gt; A private type will solve this problem. We define a module Integer
&gt; whose interface defines the integer type as a private type equipped
&gt; with functions that construct values in the different summands of the
&gt; type:
&gt; 
&gt; type integer = private
&gt;    | Zero
&gt;    | Succ of integer
&gt;    | Pred of integer;;
&gt; 
&gt; value zero : unit -&gt; integer
&gt; value succ : integer -&gt; integer
&gt; value pred : integer -&gt; integer
&gt; 
&gt; In the implementation of the module, we keep the definition of the
&gt; type as regular (say ``public'', if you want) and implement the
&gt; constructing functions such that they ensure the unicity of
&gt; representation of integers:
&gt; 
&gt; let zero () = Zero;;
&gt; 
&gt; let succ = function
&gt;   | Pred n -&gt; n
&gt;   | n -&gt; Succ n;;
&gt; 
&gt; let pred = function
&gt;   | Succ n -&gt; n
&gt;   | n -&gt; Succ n;;
&gt; 
&gt; It is now easy to prove that all the integers built with applications
&gt; of functions pred and succ have a unique representation (or
&gt; equivalently that no tree of type integer obtained by composing those
&gt; functions can have an occurrence of the redexes (Pred (Succ ...)) or
&gt; (Succ (Pred ...))).
&gt; 
&gt; Now, since all the clients of the Integer module cannot construct
&gt; values of type integer by directly applying the constructors and must
&gt; use the constructing functions instead, we know for sure that the
&gt; unicity property holds. Hence, the naive (and natural) version of
&gt; is_zero is correct again.
&gt; 
&gt; In conclusion: private types serve to modelize types with arbitrary
&gt; algebraic invariants (no miracle here: you must program those
&gt; invariants in the definition of the constructing functions).
&gt; 
&gt; In contrast with abstract data types, the private types still maintain
&gt; the elegant pattern matching facility of ML outside the module that
&gt; defines the type.
&gt; 
&gt; Hope this helps.
&gt; 
&gt; Pierre Weis
&gt; 
&gt; INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/
&gt; 
&gt; 
&gt; -------------------
&gt; To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
&gt; Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
&gt; Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
-- 
|--------/            Yaron M. Minsky              \--------|
|--------\ http://www.cs.cornell.edu/home/yminsky/ /--------|

Open PGP --- KeyID B1FFD916 (new key as of Dec 4th)
Fingerprint: 5BF6 83E1 0CE3 1043 95D8 F8D5 9F12 B3A9 B1FF D916



-------------------
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

</contents>

</message>

