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 4E8117EE09 for ; Fri, 26 Oct 2012 21:23:56 +0200 (CEST) Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of gabriel.scherer@gmail.com) identity=pra; client-ip=209.85.210.182; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail4-smtp-sop.national.inria.fr: domain of gabriel.scherer@gmail.com designates 209.85.210.182 as permitted sender) identity=mailfrom; client-ip=209.85.210.182; receiver=mail4-smtp-sop.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 (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ia0-f182.google.com) identity=helo; client-ip=209.85.210.182; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="postmaster@mail-ia0-f182.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlsCAIviilDRVdK2m2dsb2JhbABEsD6SEAgjAQEBAQEICQsJFCeCHgEBAQQSAhMZARsSCwEDDAYFCw0NISIBEQEFAQoSBhMSCQeFboFjAQMPC500YgkDjDCCdoR3ChknAwpZiHUBBQyLZQGGbQOVc4EXjUYWKYQTgWI X-IronPort-AV: E=Sophos;i="4.80,654,1344204000"; d="scan'208";a="160551771" Received: from mail-ia0-f182.google.com ([209.85.210.182]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 26 Oct 2012 21:23:54 +0200 Received: by mail-ia0-f182.google.com with SMTP id k10so4340798iag.27 for ; Fri, 26 Oct 2012 12:23:53 -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; bh=9ytYrHQHFIm00yJ6dck/sQVpUFmg9x03yecdVEbYmew=; b=v5MKL5or8FGQcfvwMq5znl+nJEx+/PMFmoOKf9K062/xKe4VnoXQoGKOBGXTal8VYk dDoxPw7TOwBnVAOanmQp/ds4iGGyo5W6HOZPmw+qXntawO8PRQBz5ld5qVOkTPJMCI8i iTxAKi+2ttDn1fmerojD840j+tV6ud6v3BrLJXznJv/+ZNyO/vHa6bNqq5KuLVe8mlSR 2KlfCq4GjJ5r5h2pin57oiXO5dumaNIpxdEUIhNmmjj/FmEMRqq4b/XZ9HVIZhsf9LNh FA0YqhKQR/yjyUNatKP0WgsZpJIvnlaCZ1M4LPPGN9wYw6j4LXVmpY7xBQStc1LFhjjC 1Rxw== Received: by 10.50.1.170 with SMTP id 10mr3232925ign.2.1351279433399; Fri, 26 Oct 2012 12:23:53 -0700 (PDT) MIME-Version: 1.0 Received: by 10.50.65.9 with HTTP; Fri, 26 Oct 2012 12:23:13 -0700 (PDT) In-Reply-To: <1351274501735@names.co.uk> References: <1351274501735@names.co.uk> From: Gabriel Scherer Date: Fri, 26 Oct 2012 21:23:13 +0200 Message-ID: To: "\"Mark\"" Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] accessing the type of a polymorphic parameter Short answer: no, it is not possible to do this. 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. On Fri, Oct 26, 2012 at 8:01 PM, "Mark" wrote: > Hello. > > Is it possible in OCaml for a polymorphic function to somehow have > access to the concrete type of an actual parameter used in a call of > the function? > > For example, is there some way of writing a polymorphic function 'foo' > list this: > > let foo x = > match (type_of x) with > Int -> .... > | String -> .... > > where 'type_of' is some function that returns a representation of the > concrete type of its argument. > > I want this because I want to implement an ad-hoc polymorphic > function. I know this is outside the bounds of "normal" OCaml, but is > there some nasty part of the language like Obj.magic that I could use > to achieve this? > > Mark. > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs