caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Question on private type abbreviations
@ 2015-07-24 14:04 immanuel litzroth
  2015-07-24 14:44 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: immanuel litzroth @ 2015-07-24 14:04 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1258 bytes --]

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?
Thanks in advance,
Immanuel

[-- Attachment #2: Type: text/html, Size: 1755 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Question on private type abbreviations
  2015-07-24 14:04 [Caml-list] Question on private type abbreviations immanuel litzroth
@ 2015-07-24 14:44 ` Jacques Garrigue
  2015-07-24 15:34   ` immanuel litzroth
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2015-07-24 14:44 UTC (permalink / raw)
  To: immanuel litzroth; +Cc: OCaML List Mailing

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Question on private type abbreviations
  2015-07-24 14:44 ` Jacques Garrigue
@ 2015-07-24 15:34   ` immanuel litzroth
  0 siblings, 0 replies; 3+ messages in thread
From: immanuel litzroth @ 2015-07-24 15:34 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: OCaML List Mailing

[-- Attachment #1: Type: text/plain, Size: 2684 bytes --]

Yes,
I found that explicitly adding the conversion function works works but then
I got
into some currying problem -- sorry I'm relatively new to ocaml:

let conversion_test = test_conversions (fun (x : uint8) -> (x:>int)) in
    conversion_test uint8 1

I want to just define the conversion test inside the body of the let so I
can reuse the name
for the other types. That doesn't seem to work?
Immanuel

On Fri, Jul 24, 2015 at 4:44 PM, Jacques Garrigue <
garrigue@math.nagoya-u.ac.jp> wrote:

> 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
>
>

[-- Attachment #2: Type: text/html, Size: 3578 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-07-24 15:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-24 14:04 [Caml-list] Question on private type abbreviations immanuel litzroth
2015-07-24 14:44 ` Jacques Garrigue
2015-07-24 15:34   ` immanuel litzroth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).