caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Florent Monnier <fmonnier@linux-nantes.fr.eu.org>
To: caml-list@yquem.inria.fr
Cc: Dawid Toton <dawid.toton@uj.edu.pl>
Subject: Re: [Caml-list] Quantifier in the type
Date: Wed, 15 Oct 2008 20:18:26 +0200	[thread overview]
Message-ID: <200810152018.26396.fmonnier@linux-nantes.fr.eu.org> (raw)
In-Reply-To: <48F60638.6040101@uj.edu.pl>

> 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


  reply	other threads:[~2008-10-15 18:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-15 15:03 Dawid Toton
2008-10-15 18:18 ` Florent Monnier [this message]
2008-10-16 15:33   ` [Caml-list] " Dawid Toton
2008-10-17  8:11     ` Tiphaine Turpin
2008-10-17 11:40       ` Dawid Toton
2008-10-16 23:06 ` Florent Monnier

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=200810152018.26396.fmonnier@linux-nantes.fr.eu.org \
    --to=fmonnier@linux-nantes.fr.eu.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=dawid.toton@uj.edu.pl \
    /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;
as well as URLs for NNTP newsgroup(s).