From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id D65E4BC57 for ; Sat, 1 May 2010 18:51:18 +0200 (CEST) X-IronPort-AV: E=Sophos;i="4.52,310,1270418400"; d="scan'208";a="50172983" Received: from mail-fx0-f52.google.com ([209.85.161.52]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/RC4-MD5; 01 May 2010 18:51:18 +0200 Received: by fxm19 with SMTP id 19so1009164fxm.39 for ; Sat, 01 May 2010 09:51:18 -0700 (PDT) MIME-Version: 1.0 Received: by 10.239.177.77 with SMTP id u13mr1141257hbf.96.1272732678337; Sat, 01 May 2010 09:51:18 -0700 (PDT) Received: by 10.239.131.144 with HTTP; Sat, 1 May 2010 09:51:18 -0700 (PDT) In-Reply-To: References: Date: Sat, 1 May 2010 18:51:18 +0200 Message-ID: Subject: Re: [Caml-list] Subtyping structurally-equivalent records, or something like it? From: =?ISO-8859-1?Q?St=E9phane_Lescuyer?= To: Anthony Tavener Cc: caml-list@yquem.inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; subtyping:01 acceleration:01 annotations:01 mult:01 multiplying:01 explicitely:01 annotations:01 constructors:01 runtime:01 acceleration:01 structurally:01 ocaml:01 subtypes:01 subtypes:01 structurally:01 Hi Anthony, I think that maybe using phantom types could do the trick : consider defining empty types for all the different "kinds" of similar constructs that you have, and then define the kinematic record with a phantom parameter type. type position type acceleration type force type 'a kinematic =3D {lin : Vec.t; ang: Vec.t} By adding some extra typing annotations, you can then constraint your functions to accept (or produce) only a given kind of construct, say for example a position kinematic : let pos_0 : position kinematic =3D {lin =3D Vec.origin; ang =3D Vec.origin = } let double_force (v : force kinematic) : force kinematic =3D {lin =3D Vec.mult 2. v.lin; ang =3D v.ang } double_force pos_0 -> does not type check You can write generic functions as add, norm, products, etc : they are just polymorphic with respect to the phantom type parameter. By the way you ensure that you are not multiplying apples and carrots : let plus (v : 'a kinematic) (v' : 'a kinematic) : 'a kinematic =3D ... Of course, the overhead is that you have to explicitely write some type annotations, especially for constructors, but the runtime overhead is exactly 0. And also, one limitation is that you can't use different projection names for the different cosntructs, since it is really always the same record type that you are using. I hope this helps, St=E9phane L. On Sat, May 1, 2010 at 6:04 PM, Anthony Tavener wrote: > I have this: > > =A0 type kinematic =3D { lin: Vec.t; ang: Vec.t } > > Which I've been using to represent a medley of physical attributes (force= , > momentum, velocity, etc.). > > As the physics code becomes increasingly substantial I'm running into > possible human-error, like passing a momentum where a force is expected, = or > a mass instead of inverse-mass (mass is also a record though different, b= ut > inv-mass has the same structure as mass). So I'd like to make distinct > types, such as: > > =A0 type position =3D { r: Vec.t; theta: Vec.t } > =A0 type acceleration =3D { a: Vec.t; alpha: Vec.t } > =A0 type force =3D { f: Vec.t; tau: Vec.t } > > They are structurally equivalent, and ideally I'd like to be able to trea= t > these as 'kinematic' too, since that is how I would express the arithmeti= c > and common functions on these types (add, mul, etc). > > > I'm sure I've seen posts on this before but I can't find them now (though > what I remember are questions about having distinct 'float' types, such a= s > for degrees vs radians, rather than records). > > I know OCaml doesn't have subtypes for records, which is effectively what > I'm looking for. Though this case is a bit more specialized that that... = all > the subtypes and base type are structurally equivalent. Code for one of > these types would technically work on any... but is there a way to inform > the type system of my intentions? > > > I hope someone has a better option than those I've considered, or that I > have a grave misunderstanding somewhere and one of these options is actua= lly > good: > > 1. Objects. Subtyping makes these a natural fit, but in this case I don't > need anything else which objects provide, and a certainly don't need the > poor performance or method-calling mixed in with my computational code > (aesthetically... yucky, to me). Again, each type is structurally > equivalent. Just some functions want certain types. > > 2. Using distinct records for each type, but no 'kinematic' base type, so > all common operations are duplicated for each new type. No performance hi= t. > But the redundant code is horrible -- makes extensions a pain, and a > potential bug-source. > > 2b. Same as above, but putting the common code in a functor which we appl= y > on all the different types. I think this will add some overhead, since th= e > signature of the types (below) would demand accessor functions for the > record fields, in order to uniformly get the fields from the different ty= pes > (again, even though they are structurally equivalent) -- these calls > probably wouldn't get optimized out. But maybe there is a better way to d= o > this? > > =A0 module type KINEMATIC =3D sig > =A0=A0=A0 type t > =A0=A0=A0 val lin : t -> Vec.t > =A0=A0=A0 val ang : t -> Vec.t > =A0 end > > 3. Making all the other types different aliases of 'kinematic'; then usin= g > explicit type declarations in function parameters and coercion to > 'kinematic' when needed. This makes some ugly code, and the added-typesaf= ety > is almost illusory. This is kind-of like 'typesafe' C code doing typecast= ing > gymnastics. > > 4. Adapter functions: 'kinematic_of_force: force -> kinematic', etc. as a > way to use the common set of 'kinematic' functions. This is clunky and co= mes > with a big performance hit unless these functions became like > type-coercions. If there is a way this can be done with zero runtime cost= , > I'd accept the clunkiness. :) > > Any thoughts? > > > I've been using OCaml for a few years now, but this is my first post. I f= eel > many of you are familiar online personae through reading archives, blogs, > and websites. Thank-you for all the help I've absorbed through those vari= ous > channels. And thanks to those making the language I favor for most tasks! > > Briefly introducing myself: I've been a professional video-game developer > for 15 years, most recently specializing in AI. I quit my last job almost > two years ago to travel and program (95% in OCaml!), and am developing a > game now. I've seen indications over the years of other game developers > taking the plunge and then parting ways with OCaml, surely back to C++. I > see OCaml as viable and certainly more pleasurable, even with avoiding > mutation. But within a pressure-cooker environment (working for $$ from > someone else) people fall back on what they are most familiar with... als= o > you can't go too rogue while still being part of a team. :) > > -Anthony Tavener > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > --=20 I'm the kind of guy that until it happens, I won't worry about it. - R.H. RoY05, MVP06