Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: immanuel litzroth <ilitzroth@gmail.com>
Cc: OCaML List Mailing <caml-list@inria.fr>
Subject: Re: [Caml-list] Question on private type abbreviations
Date: Fri, 24 Jul 2015 23:44:47 +0900	[thread overview]
Message-ID: <25270BD4-F9E4-45A2-8190-F91B937D8BB1@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAJjmLU5LXP3=f_RTpK6Y1GM75EFw1r0LX0qQiKFDnsxFAG0j3A@mail.gmail.com>

On 2015/07/24 23:04, immanuel litzroth wrote:
> 
> 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.
> 
> So I define
> type uint8 = private int
> and 
> type int8 = private int
> same for the other sizes/signedness
> and the appropriate functions to do range checking (those are external and use 
> std::numeric limits)
> external uint8 : int -> uint8 = "make_uint8"
> ... 
> 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.
> 
> Now I wanna check my code
> for all the types I wanna use 1 checking function something like this:
> 
> let test_conversions   (the_fun : int -> 't)  (the_val : int) =
>   try
>     let the_t = the_fun the_val in
>     Printf.printf "Numbers are %d\n" (the_t : 't :> int)
>   with
>   | Invalid_argument str -> Printf.printf "Error: %s" str
> 
> let () = test_conversions uint8 1 -> will work
> ..
> let () = test_conversions uint64  (-1) -> will print Error...
> 
> Now this doesn't typecheck because the type  var 't in the signature is too 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’t write a coercion for
each of your types, this won’t work.
This means you need to add another parameter:

let test_conversions  (coerce : ’t -> int)  (the_fun : int -> 't)  (the_val : int) =
  try
    let the_t = the_fun the_val in
    Printf.printf "Numbers are %d\n” (coerce the_t)
  with
  | Invalid_argument str -> Printf.printf "Error: %s” str

let from_uint8 x : uint8 :> int = x
let from_uint64 x : uint64 :> int = x

let () = test_conversions from_uint8 uint8 1
..
let () = test_conversions from_uint64 uint64  (-1) 

Jacques Garrigue


  reply	other threads:[~2015-07-24 14:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-24 14:04 immanuel litzroth
2015-07-24 14:44 ` Jacques Garrigue [this message]
2015-07-24 15:34   ` immanuel litzroth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=25270BD4-F9E4-45A2-8190-F91B937D8BB1@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=ilitzroth@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox