Version française
Home     About     Download     Resources     Contact us    
Browse thread
Quantifier in the type
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Florent Monnier <fmonnier@l...>
Subject: Re: [Caml-list] Quantifier in the type
> I put 'a in the interface:
> val fu : 'a -> 'a
> and int in the implementation:
> let fu x = x + 1

this interface doesn't reflect the implementation, ocaml will reject it in any 
way

> So I have universal quantification: for any type 'a function fu can
> consume the argument.

No it is not universal, imagine all the types that a user can create, you 
cannot handle them all (except maybe with the Obj module, but it's another 
question)

Anyway if you handle different types, you still have to join them in a single 
type :

type generic =
  | Int of int
  | Float of float
  | Int64 of int64
  ... and so on

>                       So my implementation doesn't comply with that
> universal quatification. And the message I get is following:
>
> Values do not match: val fu : int -> int is not included in val fu : 'a
> -> 'a

Yes it doesn't match at all, the type of the implementation is int, nothing 
else.

> So the declaration of value in mli file is called simply a 'value'. Is
> it intentional?

It is just the word in the English language to designate this thing.

> I thought that value and it's declaration are separate notions?

a value is handle by a identifier and has a type
  let ident = some_value in
here ident and some_value are of some type

> My reading of " val fu : 'a -> 'a " is:
>  some partiular value vvv that belongs to set of values that satisfy
> "forall 'a : (vvv can be used with ('a -> 'a) type)"

here are some examples of functions with type ('a -> 'a) :

let id v = (v)
let return_sleep v = Unix.sleep 3; (v)
let feedback_kind v =
  let r = Obj.repr v in
  if Obj.is_int r then print_endline "value similar to int";
  (v)
;;

# let (duplicate : 'a -> 'a) = function v ->
    Obj.obj(Obj.dup(Obj.repr v)) ;;
val duplicate : 'a -> 'a = <fun>
# let str = "AAA" ;;
val str : string = "AAA"
# let str2 = duplicate str ;;
val str2 : string = "AAA"
# str.[0] <- 'B' ;;
- : unit = ()
# str, str2 ;;
- : string * string = ("BAA", "AAA")

________________________________


# let reminder = ref [] ;;
val reminder : '_a list ref = {contents = []}

# let remind_through v =
    if List.exists (fun this -> Obj.magic v = this) !reminder
    then failwith "allready processed" 
    else begin
      reminder := Obj.magic v :: !reminder;
      (v)
    end 
  ;;              
val remind_through : 'a -> 'a = <fun>

# let a, b, c, d =
  "Hello",
  4096,
  4.0 *. atan 1.0,
  None;;
val a : string = "Hello"
val b : int = 4096
val c : float = 3.14159265358979312
val d : 'a option = None

# remind_through a ;;
- : string = "Hello"
# remind_through b ;;
- : int = 4096
# remind_through c ;;
- : float = 3.14159265358979312
# remind_through d ;;
- : 'a option = None

# remind_through a ;;
Exception: Failure "allready processed".



> But if I write
> let (bar : 'a -> 'a ) = (fun x -> x + 1)
> I create a value that belongs to set "exists 'a : (vvv can be used with
> ('a -> 'a) type)"
> So it's the other quantifier.

int can be used for a function which takes 'a, but this opposite is not true:
a function that implements with ints can not take 'a parameters.

> I think that the quantifier has to be part of type, since a type is set
> of values (and the quantifier plays important role when describing some
> of such sets).
> So my question is: since we see the same string " 'a -> 'a " that refers
> to different types in different contexts, what are the rules? How is
> type definition in OCaml translated to a type?

type definition in OCaml are translated to actual types by inference.
http://en.wiktionary.org/wiki/inference

(fun x -> x + 1) will be easily infered to (int -> int)

The only way to do a generic function to make addition on generic types as 
float, int, or a number in a string, is like the example below.
But those kind of things are HIGHLY discouraged !
Please consider each Obj.magic as a hammer hit,
you will understand that programming with a hammer is discouraged :-)

# let gen_add v =
    let r = Obj.repr v in
    let tag = Obj.tag r in
    if tag = Obj.int_tag then
      (Obj.magic((Obj.magic v) + 1))
    else if tag = Obj.double_tag then
      (Obj.magic((Obj.magic v) +. 1.0))
    else
    try if tag = Obj.string_tag then
      (Obj.magic(Scanf.sscanf (Obj.magic v) "%f"
                   (fun v -> string_of_float(v +. 1.0))))
    else
      (v)
    with Scanf.Scan_failure _ -> (v) 
  ;;
val gen_add : 'a -> 'a = <fun>

# gen_add 3 ;;
- : int = 4
# gen_add 3.0 ;;
- : float = 4.
# gen_add "3" ;;
- : string = "4."

-- 
Florent