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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id 94AF97EE09 for ; Fri, 26 Oct 2012 21:39:05 +0200 (CEST) Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of mark@proof-technologies.com) identity=pra; client-ip=85.233.160.25; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="mark@proof-technologies.com"; x-sender="mark@proof-technologies.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail4-smtp-sop.national.inria.fr: domain of mark@proof-technologies.com designates 85.233.160.25 as permitted sender) identity=mailfrom; client-ip=85.233.160.25; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="mark@proof-technologies.com"; x-sender="mark@proof-technologies.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@outgoing-smtp.namesco.net) identity=helo; client-ip=85.233.160.25; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="mark@proof-technologies.com"; x-sender="postmaster@outgoing-smtp.namesco.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AikCAI7milBV6aAZjWdsb2JhbABEhhiFVLZqIwEBAQEJCQsJEgUkgkgEUhsaAiYCIxsEHQ4FGgGHWQMTB6l+iQgNiVSBIIlqaBODNoIRgRMDjgiGFoFVgReET4VIiACBWgkX X-IronPort-AV: E=Sophos;i="4.80,654,1344204000"; d="scan'208";a="160552645" Received: from outgoing-smtp.namesco.net ([85.233.160.25]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 26 Oct 2012 21:39:04 +0200 Received: from [192.168.0.128] (helo=plod.namesco.net) by outgoing-smtp.namesco.net with esmtp (Exim 4.67) (envelope-from ) id 1TRpkE-0003Oy-Vy; Fri, 26 Oct 2012 20:39:03 +0100 Received: from webmail by plod.namesco.net with local (Exim 4.72) (envelope-from ) id 1TRpkE-0006RG-Um; Fri, 26 Oct 2012 20:39:02 +0100 To: CC: From: =?utf-8?q?=22Mark=22?= Reply-To: =?utf-8?q?=22Mark=22?= MIME-Version: 1.0 X-Mailer: Namesco Webmail v3.0 Message-ID: <1351280342705@names.co.uk> Date: Fri, 26 Oct 2012 20:39:02 +0100 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-namesco: 192.168.0.171 X-Spam-Score: -1.4 (-) Subject: Re: [Caml-list] accessing the type of a polymorphic parameter 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 wrote: > ... > 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. > ...