Re: "ocaml_beginners":: comaring "apples" to "oranges" ?
- It's ironic to see a high-level concept like "disjoint union" (or "variant
type") be explained in terms of "simple" terms like pointers and stuff.
Maybe you're a bit too clung on the low-level details of previous
programming languages that don't work like OCaml.
A value of the type
type foo =
| A of a
| B of b
is either of the form (A v), where (v) is a value of type (a), or (B v),
where (v) is a value of type (b). The equality between values of type (foo)
is defined by cases:
(A v = A v') holds when (v = v') (at type a)
(B v = B v') holds when (v = v') (at type b)
note that (A _ = B _), or (B _ = A _), never holds.
That's all you need to know. But as your mind has seemingly be corrupted by
too much C programming to think of programming language data as anything
else than vectors to void*, we can also phrase it that way: a value of a
variant type is compiled down to something like a struct whose first
component (after runtime-specific information) is the constructor (A, B,
etc.), and whose other components are the corresponding parameters ("C of
foo * bar" has two parameters). (But constant constructors with just
parameters are just integers.)
On Mon, Feb 25, 2013 at 2:20 PM, Sergei Steshenko <sergstesh@...>wrote:
> > From: Lukasz Stafiniak <lukstafi@...>
> >To: OCaml Beginners <email@example.com>
> >Sent: Monday, February 25, 2013 3:15 PM
> >Subject: Re: "ocaml_beginners":: comaring "apples" to "oranges" ?
> >On Mon, Feb 25, 2013 at 2:07 PM, Sergei Steshenko sergstesh@...
> >> **
> >> As I stated earlier, variant type is like "C" union.
> >Variant types are disjoint unions (with identifiable origin) while "C"
> >unions are non-disjoint (without identifiable origin). They are "as
> >different as unions can be".
> Well, I am not familiar with this terminology.
> Could you please explain in simple terms like memory locations, pointers,
> Archives up to December 31, 2011 are also downloadable at
> The archives of the very official ocaml list (the seniors' one) can be
> found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames
> etc.Yahoo! Groups Links
[Non-text portions of this message have been removed]
>________________________________First of all, I want both non-homogenous keys and non-homogenous values, i.e. I want top be able in the same hash to have (I know the code won't compile) :
> From: Gabriel Scherer <gabriel.scherer@...>
>Sent: Tuesday, February 26, 2013 5:20 PM
>Subject: Re: "ocaml_beginners":: comaring "apples" to "oranges" ?
>I'm not sure I have fully understood this length and
>unequally-productive discussion, so let me ask a question: what you
>are trying to do is to have a static disciplines for things that are
>not homogeneous hash tables (all keys store values of the same type),
>but heterogeneous hash tables where each key may store values of a
>The closest thing in the type system are object types (extensible
>types that have fields of different types). But different types can
>only be used if the access patterns are *statically* known, that is if
>the keys used (`one`, `two` in your example) are constants in the code
>rather than dynamically decided. (Of course in some cases you may pass
>acessors functions to be applied to the object). Do you think you can
>respect this expressivity restriction, in exchange for better static
>(Putting my theoretician hat on: there has been research on more
>dynamic associative structures. OCaml object types are based on row
>type variables, whose related uses on extensible records have been
>studied, for example, by Didier Rémy in the early nineties, Daan
>Leijen in the 2000's, and recently folks trying to do static typing on
let h = Hashtbl.create 16
Hashtbl.replace h 1 "one"
Hashtbl.replace "two" 2
Hashtbl.replace 3.0 "three"
Hashtbl.replace [1;2;3] "some_list"
Hashtbl.replace "another_list" [1; "two"; 3.0]
Regarding theoretical work and work done by others - I definitely know you professional guys have much deeper understanding and knowledge than I, but in this case I _myself_ need to step on all the required rakes - otherwise I won't learn and won't understand.
That is, reinvention of wheels and stepping on rakes are at the moment my explicit goals.