From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 8DB677EEBF for ; Fri, 24 Jul 2015 16:44:51 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of garrigue@math.nagoya-u.ac.jp) identity=pra; client-ip=133.6.130.5; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="garrigue@math.nagoya-u.ac.jp"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of garrigue@math.nagoya-u.ac.jp) identity=mailfrom; client-ip=133.6.130.5; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="garrigue@math.nagoya-u.ac.jp"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mailhost.math.nagoya-u.ac.jp) identity=helo; client-ip=133.6.130.5; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="garrigue@math.nagoya-u.ac.jp"; x-sender="postmaster@mailhost.math.nagoya-u.ac.jp"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0CmAACKTrJVlwWCBoVchFLDRgKCDAEBAQEBARIBAQEBAQgWB0+EJAEBBCMEGQMBNQEBDgsYAgIYDgICVwYyiA62dnCEawKMOoRnAQEBAQEBAQMBAQEBAQEBARMBBoEiiiuEVDMHgmkvgRSNNocyjDyBRZN+g2GEMmCCSwEBAQ X-IPAS-Result: A0CmAACKTrJVlwWCBoVchFLDRgKCDAEBAQEBARIBAQEBAQgWB0+EJAEBBCMEGQMBNQEBDgsYAgIYDgICVwYyiA62dnCEawKMOoRnAQEBAQEBAQMBAQEBAQEBARMBBoEiiiuEVDMHgmkvgRSNNocyjDyBRZN+g2GEMmCCSwEBAQ X-IronPort-AV: E=Sophos;i="5.15,538,1432591200"; d="scan'208";a="171526121" Received: from rabbit.math.nagoya-u.ac.jp (HELO mailhost.math.nagoya-u.ac.jp) ([133.6.130.5]) by mail2-smtp-roc.national.inria.fr with ESMTP; 24 Jul 2015 16:44:50 +0200 Received: from mailhost.math.nagoya-u.ac.jp (localhost [127.0.0.1]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTP id 4534E66F7; Fri, 24 Jul 2015 23:44:49 +0900 (JST) Received: from mailhost.math.nagoya-u.ac.jp (rabbit-172 [172.16.254.254]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTP id 00AE524EA; Fri, 24 Jul 2015 23:44:49 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=math.nagoya-u.ac.jp; h= subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; s=alpha; bh=6vI/d3rUP5yst6SktKQkQUWMC7A=; b=ocztJoRE80UGBfJtVZSKLszgR5fM a5Y2nHHUy4Yb54kvrhr5nqpxpTliIyfUbVuqji632DSzqOGdpPMkHkhJ1aNaKby6 uHF3SqEnXPSaA2eer2Glb1A3ZXTHAnjvPX9XaL3L9/lbGtj8Q/M0wnyxVPKjl56Z tS3n/qPXIblRbWE= DomainKey-Signature: a=rsa-sha1; h=Received:Subject:Mime-Version:Content-Type:From:In-Reply-To:Date:Cc:Content-Transfer-Encoding:Message-Id:References:To:X-Mailer; b=comL/DYmfopcI+QLTouQHHXm7dwFhItCLl+d2RTXt2QZoUMuAoVQf7v9oli2iQBBOXaCpe5wgbE9zw48zPktC+zexF867pMWThMYfUYzXjBzhJH52oCDVJ4ZNYs0Tzrl0tANlxgJpBX/qfEkqK20MGyryjt8rPAsO6CpyEE/TGI=; c=nofws; d=math.nagoya-u.ac.jp; q=dns; s=alpha Received: from tet.garrigue.jp (58x158x128x157.ap58.ftth.ucom.ne.jp [58.158.128.157]) by mailhost.math.nagoya-u.ac.jp (Postfix) with ESMTPSA id 61FA18065; Fri, 24 Jul 2015 23:45:31 +0900 (JST) Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2102\)) Content-Type: text/plain; charset=utf-8 From: Jacques Garrigue In-Reply-To: Date: Fri, 24 Jul 2015 23:44:47 +0900 Cc: OCaML List Mailing Content-Transfer-Encoding: quoted-printable Message-Id: <25270BD4-F9E4-45A2-8190-F91B937D8BB1@math.nagoya-u.ac.jp> References: To: immanuel litzroth X-Mailer: Apple Mail (2.2102) Subject: Re: [Caml-list] Question on private type abbreviations On 2015/07/24 23:04, immanuel litzroth wrote: >=20 > I have a question related to private type abbreviations > I'm interfacing C++ and ocaml and I want to make sure that the ranges of = integer types are correct and reflect them in the ocaml interface. >=20 > So I define > type uint8 =3D private int > and=20 > type int8 =3D private int > same for the other sizes/signedness > and the appropriate functions to do range checking (those are external an= d use=20 > std::numeric limits) > external uint8 : int -> uint8 =3D "make_uint8" > ...=20 > this gives typesafety and avoids boxing/unboxing and makes sure that the = user can > only pass values that are range checked at the earliest opportunity. >=20 > Now I wanna check my code > for all the types I wanna use 1 checking function something like this: >=20 > let test_conversions (the_fun : int -> 't) (the_val : int) =3D > try > let the_t =3D the_fun the_val in > Printf.printf "Numbers are %d\n" (the_t : 't :> int) > with > | Invalid_argument str -> Printf.printf "Error: %s" str >=20 > let () =3D test_conversions uint8 1 -> will work > .. > let () =3D test_conversions uint64 (-1) -> will print Error... >=20 > Now this doesn't typecheck because the type var 't in the signature is t= oo general. > what I need to put there is "a type coercible to int" > Is that possible? Is there some way to achieve this? I see no way to do that implicitly. Namely, subtyping is only checked for coercions, so if you don=E2=80=99t wr= ite a coercion for each of your types, this won=E2=80=99t work. This means you need to add another parameter: let test_conversions (coerce : =E2=80=99t -> int) (the_fun : int -> 't) = (the_val : int) =3D try let the_t =3D the_fun the_val in Printf.printf "Numbers are %d\n=E2=80=9D (coerce the_t) with | Invalid_argument str -> Printf.printf "Error: %s=E2=80=9D str let from_uint8 x : uint8 :> int =3D x let from_uint64 x : uint64 :> int =3D x let () =3D test_conversions from_uint8 uint8 1 .. let () =3D test_conversions from_uint64 uint64 (-1)=20 Jacques Garrigue