Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phantom types #3113

Closed
vicuna opened this issue Aug 26, 2004 · 0 comments
Closed

Phantom types #3113

vicuna opened this issue Aug 26, 2004 · 0 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Aug 26, 2004

Original bug ID: 3113
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Hi!

I posted to the mailing list about my attempts to learn how to use phantom
types. The only response I have received (from brogoff) seemed to confirm my
fears I had found a bug in the OCaml type checker. I was using 3.08.0 but
just upgraded to 3.08.1 and the problem still appears. This may well be my
misunderstanding, however. The following:

module PhantomInt : sig
type 'p t

val add : 'p t -> 'p t -> 'p t

val add_even_even : [< Even ] t -> [< Even ] t -> [< Even ] t val add_even_odd : [< Even ] t -> [< Odd ] t -> [< Odd ] t
val add_odd_even : [ Odd ] t -> [ Even ] t -> [ Odd ] t val add_odd_odd : [ Odd ] t -> [ Odd ] t -> [ Even ] t
val neg : 'a t -> 'a t
val make_even : int -> [ Even ] t val make_odd : int -> [ Odd ] t
end = struct
type 'p t = int

let add = (+)
let add_even_even = add
let add_even_odd = add
let add_odd_even = add
let add_odd_odd = add
let neg n = -n
let make_even n = if n mod 2 = 0 then n else failwith "not even"
let make_odd n = if n mod 2 <> 0 then n else failwith "not odd"
end;;

let i = PhantomInt.make_even 2;;
let j = PhantomInt.make_odd 3;;
let k = PhantomInt.add_even_odd i j;;
PhantomInt.add_even_even i k;;
PhantomInt.neg i;;

<<<<<

works as expected, but changing the type to be non-abstract (i.e. putting
"type 'p t = int" in the signature) changes the behaviour and the phantom
type checking ceases to work. Perhaps this is the intended behaviour though,
I'm not sure.

Cheers,
Jon.

@vicuna vicuna closed this as completed Aug 27, 2004
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant