From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p9GKOYrU018015 for ; Sun, 16 Oct 2011 22:24:37 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjYCAGY8m07VUZiOjmdsb2JhbABChHWVD4xpghcBAQEBCQsJCRIGIYFuAQEEASMEDAkBLwwGCwkCGAICBRYLAgIJAwIBAgFFEwgCh3sCo1mQVIEwhUSBFASTeoUpjC8 X-IronPort-AV: E=Sophos;i="4.69,354,1315173600"; d="scan'208";a="124337523" Received: from mylady.t-com.sk (HELO smtp.t-com.sk) ([213.81.152.142]) by mail1-smtp-roc.national.inria.fr with ESMTP; 16 Oct 2011 22:24:36 +0200 MIME-version: 1.0 Content-transfer-encoding: 8BIT Content-type: text/plain; charset=UTF-8 Received: from [10.0.0.4] ([unknown] [95.103.32.20]) by relay1.stonline.sk (STOnline ESMTP Server) with ESMTPA id <0LT6009X8E0ZH900@relay1.stonline.sk> for caml-list@inria.fr; Sun, 16 Oct 2011 22:24:36 +0200 (CEST) Message-id: <4E9B3D66.6080203@fiit.stuba.sk> Date: Sun, 16 Oct 2011 22:24:06 +0200 From: =?UTF-8?B?TWF0ZWogS2/FocOtaw==?= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.16) Gecko/20111004 Icedove/3.0.11 To: caml-list@inria.fr References: <4E9B27A1.5070309@fiit.stuba.sk> In-reply-to: X-Enigmail-Version: 1.0.1 OpenPGP: id=F248FE18; url=http://www2.fiit.stuba.sk/~kosik/doc/kosik.asc Subject: Re: [Caml-list] Ocaml: typing by name vs. typing by structure On 10/16/2011 09:24 PM, Gabriel Scherer wrote: > If record were structurally typed, there would be an ambiguity as to, > for example, what this function mean: > > let access_foo t = t.foo > > What would the type of `access_foo` be ? { foo : 'a } -> 'a ? { foo : > 'a; bar : 'b } -> 'a ? { foo : 'a } -> 'a seems plasible > Should `access_foo { foo = 1 }` be typable? And `access_foo { foo = 1; > bar = true }` ? > > In this situation, you want record subtyping. This get more > complicated than the relatively simple OCaml records. This may be too troublesome to support but I guess it is plausible language change and it would enable additional Ocaml terseness. Let me clarify. At the moment, it is not possible to do this: type t = {l1:int} * {l2:int};; It is a syntax error. We are forced to do it gradually: type t1 = { l1 : int; } type t2 = { l2 : int; } type t = t1 * t This is somewhat cumbersome. Similarly, you cannot do this type t = A of {l1:int} | B of {l2:float} | C of {l1:int; l2:float} Only this works: type t1 = { l1 : int} type t2 = { l2 : float} type t3 = { l1 : int; l2: float} type t = A of t1 | B of t2 | C of t3 We were forced to introduce three superfluous type-names [t1;t2;t3]. How would you feel if you were forced to do that for every tuple-type? Sometimes tuples are not ideal (when they have lots of members) Records are better in those cases because you can access elements by their label and when you construct records with lots of fields, the labels clarify which field has what meaning. But switching from tuples to records is not smooth. What could be gained in readability (labeled fields) is lost by cluttering the program by auxiliary record type definitions. > In fact, OCaml > has structurally typed structures with named fields : objects > > # let access_foo t = t#foo;; > val access_foo : < foo : 'a; .. > -> 'a = > # access_foo (object method foo = 1 method bar = true end);; > - : int = 1 Interesting. > > Tuples don't have this issue as they don't have an universal accessor > function : there is no way to get the first element of a tuple > whatever its width is. Therefore, all tuples manipulation make the > structure concrete, and structural subtyping comes at no complexity > cost ... if you accept the absence of universal accessors. SML has > them (#1 #2 etc.) and I'm not sure how they handle this. -- Matej Košík