From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id 2B7727EE09 for ; Fri, 26 Oct 2012 21:58:47 +0200 (CEST) Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of gabriel.scherer@gmail.com) identity=pra; client-ip=209.85.223.182; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail1-smtp-roc.national.inria.fr: domain of gabriel.scherer@gmail.com designates 209.85.223.182 as permitted sender) identity=mailfrom; client-ip=209.85.223.182; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ie0-f182.google.com) identity=helo; client-ip=209.85.223.182; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="postmaster@mail-ie0-f182.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AloCAMHqilDRVd+2m2dsb2JhbABEwk4IIwEBAQEBCAkLCRQngh4BAQEEEgITGQEbHQEDDAYFCw0uIQEBEQEFARwGExoBB4dRAQMPC50wYgkDjDCCdoR5ChknDVmIdQEFDIp+ZwEThloDlB6BVYEXiheDLxYphBOBWQkX X-IronPort-AV: E=Sophos;i="4.80,656,1344204000"; d="scan'208";a="179127452" Received: from mail-ie0-f182.google.com ([209.85.223.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 26 Oct 2012 21:58:46 +0200 Received: by mail-ie0-f182.google.com with SMTP id k10so7599352iea.27 for ; Fri, 26 Oct 2012 12:58:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=WhlQbsOmlqOmyJ0A6+E2yxG3M4PQaj/PgJQdelebYa4=; b=PqvYGQZKib2eRD/UgznpSXmVsHJa3L+gp3Lo65C/VIguSDEG53BmEAGfyyUkeqFxFK knwB6o5kMne04eD1VGbpcUx1zl/8ZyCysp/dX+q7z4s8xpclsoiYvTvGHKzIYuYM8DIh kWom6MPZOw6ZIImehoCQTZWwjONBOSRxexNOHWR26TptF8569W9vyBny/NIHT7y1rEOq vh3gh2tVCgQs/lSKdH36BHMxOV3Dsy1tkr5QdIRXZLrgZ0tHFsxcx7DFLQjjlR1FEsN6 fZHV/okCXbn8nesN8AuNlW2piIYvV5z4d9IaID8nYbMKaMEOjr4ukLpmR3FHpokWgano NFDw== Received: by 10.50.179.97 with SMTP id df1mr3329136igc.2.1351281525132; Fri, 26 Oct 2012 12:58:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.50.65.9 with HTTP; Fri, 26 Oct 2012 12:58:04 -0700 (PDT) In-Reply-To: <1351280342705@names.co.uk> References: <1351280342705@names.co.uk> From: Gabriel Scherer Date: Fri, 26 Oct 2012 21:58:04 +0200 Message-ID: To: "\"Mark\"" Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [Caml-list] accessing the type of a polymorphic parameter If you can formulate your problems with ('a ops), the module defining an abstract type t can provide a (t ops) from inside the abstraction boundary. Handling composite data types is also rather easy, just provide for example an ('a ops -> 'a list ops) function. The runtime type representation idea of Alain can also be extended to deal with private types, but you need either to pre-plan the required operation in advance and provide them from inside the abstraction boundary, or expose the runtime type representation of the abstract type which lessen encapsulation (but you can hardly do worse than using Obj.magic in this case). This is present in "ocaml-ty", a prototype work on runtime type representation by Pierre Chambart and Gr=E9goire Henry which has been presented during the last OCaml Users and Developers Meeting back in September. It's experimental work and a moving target, and comes with a "modification of the language" part that isn't really the concern here, but you could think of looking at their work (and providing them feedback!) if you really insist on making heavy use of ad-hoc polymorphism. https://gitorious.org/ocaml-ty On Fri, Oct 26, 2012 at 9:39 PM, "Mark" wrote: > Superb, thanks David, Lilian, Gabriel. This may be just about sufficient > for my purposes. I'll have a play. > > As well as predefined OCaml primitive datatypes, what would perfect for me > would be the ability to also do newly created abstract datatypes, and > composite datatypes like lists of strings. Are either of these possible > with your solutions? > > Mark. > > on 26/10/12 7:29 PM, David Allsopp wrote: > >> ... >> For this explicit example, you could write: >> >> let foo x =3D >> match Obj.tag (Obj.repr x) with >> x when x =3D Obj.int_tag -> ... >> | x when x =3D Obj.string_tag -> ... >> >> but this probably isn't what you're after. The type of foo will also be = 'a >> -> '_b so you won't get any help from the type system in the calling of > foo. >> ... > > > on 26/10/12 7:50 PM, Lilian Jean BESSON wro= te: > >> ... >> In my code "surcharge.ml", the main functions are "typage" : 'a -> >> string, and "dump" : a' -> string. >> Examples: >> #9> Surcharge.typage;; >> - : 'a -> string =3D >> #10> Surcharge.typage 4 (* ok *);; >> - : string =3D "int" >> #11> Surcharge.typage 8.7 (* ok *);; >> - : string =3D "float" >> #12> Surcharge.typage "ok";; (* ok *) >> - : string =3D "string" >> #13> Surcharge.typage 'k' (* not ok *);; >> - : string =3D "int" >> #14> Surcharge.typage [4] (* not ok *);; >> - : string =3D "array" >> #15> Surcharge.typage [|4|];; >> - : string =3D "array" >> #16> Surcharge.typage cos (* not ok *);; >> - : string =3D "unknown" >> But this function "typage" is very limited : no difference between arrays >> and strings etc... >> ... > > > on 26/10/12 8:24 PM, Gabriel Scherer wrote: > >> ... >> Long answer: I presume that your implementation works for any type >> providing some fixed set of operations, but that you wish those >> operations to be implemented differently for each type. You can define >> a type ('a ops) describing these operations on the type 'a (as a >> record of functions for example), and use the polymorphic type ('a -> >> 'a ops -> foo) instead of ('a -> foo) to write your function. >> Depending on your specific needs there may be slightly different >> approaches (using a algebraic representation of runtime types rather >> than a typeclass-like dictionary), a very general way being described >> in this blog post by Alain Frisch : >> http://www.lexifi.com/blog/dynamic-types >> It could help to have more details on your needs to suggest a better >> solution. >> >> Wrong answer: yes, using Obj.magic you can access the runtime >> representation of values. You can't distinguish between boolean and >> integers and the empty list, but worrying about that is for the >> coward, let's shoot yourself in the foot -- starting by reading the >> documentation on the representation of OCaml values, chapter >> "interacting with C" in the manual. >> ...