caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Type aliases
@ 2002-11-28 18:01 Martin Jambon
  2002-11-28 18:51 ` Alain Frisch
  2002-11-28 18:55 ` Alessandro Baretta
  0 siblings, 2 replies; 4+ messages in thread
From: Martin Jambon @ 2002-11-28 18:01 UTC (permalink / raw)
  To: caml-list

Hi,

I would like to have the following properties:

1) Define type aliases:

     type year = int
     and month = int

2) Don't hide their representation and derive the primitives from the
   original type:

     # let next_year = current_year + 1 ;;
     val next_year : year = 2003

   (+) and 1 would be silently converted to types using year instead of
   int.


3) Prohibit the mixing of different types that do not derive from each
   other, without an explicit cast:

     let my_mistake = year + month   (* Type error! *)

   where year has type year and month has type month.
   But this could be valid:

     let some_int = (year :> int) * 13 + month


Basically, I would like to minimize the risk of error when I manipulate
simple datatypes like numerical parameters or int/string identifiers.


Thanks for any suggestion!

Martin

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Type aliases
  2002-11-28 18:01 [Caml-list] Type aliases Martin Jambon
@ 2002-11-28 18:51 ` Alain Frisch
  2002-11-29 14:03   ` Martin Jambon
  2002-11-28 18:55 ` Alessandro Baretta
  1 sibling, 1 reply; 4+ messages in thread
From: Alain Frisch @ 2002-11-28 18:51 UTC (permalink / raw)
  To: Martin Jambon; +Cc: caml-list

On Thu, 28 Nov 2002, Martin Jambon wrote:

> 1) Define type aliases:
...
> 2) Don't hide their representation and derive the primitives from the
>    original type:
...
> 3) Prohibit the mixing of different types that do not derive from each
>    other, without an explicit cast:
...
> Basically, I would like to minimize the risk of error when I manipulate
> simple datatypes like numerical parameters or int/string identifiers.

You can get something close to what you want with:

module Ints :
sig
  type 'a integer

  val int: int -> 'a integer
  val get: 'a integer -> int

  val ( + ): 'a integer -> 'a integer -> 'a integer
  val ( * ): 'a integer -> 'a integer -> 'a integer
(* etc... *)
end =
struct
  type 'a integer = int
  let int x = x
  let get x = x
  include Pervasives
end


integer literals must be written like (int 42), which is somewhat
painful, but Camlp4 can help here...

The point is that you cannot mix Apples and Oranges:

# open Ints;;
# let x : [ `Apple ] integer = int 3;;
val x : [ `Apple] Ints.integer = <abstr>
# let y : [ `Orange ] integer = int 4;;
val y : [ `Orange] Ints.integer = <abstr>
# get (x + y);;
This expression has type [ `Orange] Ints.integer but is here used with
type [ `Apple] Ints.integer


Hope this helps.


-- Alain

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Type aliases
  2002-11-28 18:01 [Caml-list] Type aliases Martin Jambon
  2002-11-28 18:51 ` Alain Frisch
@ 2002-11-28 18:55 ` Alessandro Baretta
  1 sibling, 0 replies; 4+ messages in thread
From: Alessandro Baretta @ 2002-11-28 18:55 UTC (permalink / raw)
  To: Martin Jambon, ocaml



Martin Jambon wrote:

> 3) Prohibit the mixing of different types that do not derive from each
>    other, without an explicit cast:
> 
>      let my_mistake = year + month   (* Type error! *)
> 
>    where year has type year and month has type month.
>    But this could be valid:
> 
>      let some_int = (year :> int) * 13 + month
> 

When I want this kind of property I use variant tags to 
encapsulate the datum and document its semantics:

type time_value = [
| `Year of int
| `Month of int
| `Week of int
| `Day of int
| `Hour of int
| `Min of int
| `Sec of int
]


let this_year = `Year(2002)

let time_value_function = function
| `Year (y) -> ...
| `Month(m) -> ...
...

This is a fairly heavy solution in terms of code length, but 
it does guarantee type safety and excellent level of 
readability--a property you'd lose with operator overloading.

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Type aliases
  2002-11-28 18:51 ` Alain Frisch
@ 2002-11-29 14:03   ` Martin Jambon
  0 siblings, 0 replies; 4+ messages in thread
From: Martin Jambon @ 2002-11-29 14:03 UTC (permalink / raw)
  To: Alain Frisch; +Cc: caml-list

> You can get something close to what you want with:
>
> module Ints :
> sig
>   type 'a integer
>
>   val int: int -> 'a integer
>   val get: 'a integer -> int
>
>   val ( + ): 'a integer -> 'a integer -> 'a integer
>   val ( * ): 'a integer -> 'a integer -> 'a integer
> (* etc... *)
> end =
> struct
>   type 'a integer = int
>   let int x = x
>   let get x = x
>   include Pervasives
> end

Great! Thanks a lot.

Finally, I decided to define only the 2 conversion functions, since we
have to make difficult choices like the following:

  Should we define   val ( * ) : 'a integer -> 'b integer -> 'b integer
  or                 val ( * ) : 'a integer -> 'a integer -> 'a integer
  or                 val ( * ) : int -> 'a integer -> 'a integer
  ?

So, this is enough to avoid confusion between keys that have the same
representation (commonly int or string) and is complementary to the use
of labeled arguments.
(and no preprocessing is required to make (1 + 1) valid).


Martin

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2002-11-29 14:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-28 18:01 [Caml-list] Type aliases Martin Jambon
2002-11-28 18:51 ` Alain Frisch
2002-11-29 14:03   ` Martin Jambon
2002-11-28 18:55 ` Alessandro Baretta

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