caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* typing question
@ 2007-04-24 18:14 micha
  2007-04-24 22:22 ` [Caml-list] " Philippe Wang
  0 siblings, 1 reply; 7+ messages in thread
From: micha @ 2007-04-24 18:14 UTC (permalink / raw)
  To: caml-list

Hi,

why is the type of register1  'a -> string but the type of register2
_'a -> string?


cheers
 Michael



let symbol_id = ref 0;;
let register1 fkt =
    let name = "symbol-" ^ (string_of_int !symbol_id) in
    incr symbol_id;
    Callback.register name fkt;
    name
;;

let register2 =
    let symbol_id = ref 1 in
    fun fkt -> let name = "symbol-" ^ (string_of_int !symbol_id) in
    incr symbol_id;
    Callback.register name fkt;
    name
;;


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

* Re: [Caml-list] typing question
  2007-04-24 18:14 typing question micha
@ 2007-04-24 22:22 ` Philippe Wang
  0 siblings, 0 replies; 7+ messages in thread
From: Philippe Wang @ 2007-04-24 22:22 UTC (permalink / raw)
  To: micha, ocaml ml

micha wrote:
> Hi,
>
> why is the type of register1  'a -> string but the type of register2
> _'a -> string?
>
>
> cheers
>  Michael
>
>
>
> let symbol_id = ref 0;;
> let register1 fkt =
>     let name = "symbol-" ^ (string_of_int !symbol_id) in
>     incr symbol_id;
>     Callback.register name fkt;
>     name
> ;;
>
> let register2 =
>     let symbol_id = ref 1 in
>     fun fkt -> let name = "symbol-" ^ (string_of_int !symbol_id) in
>     incr symbol_id;
>     Callback.register name fkt;
>     name
> ;;
>   

It is because register2 is detected as an "expansive" expression.

"Expansive expressions" are those that we can't generalize without 
taking the risk to allow type errors at execution, so its type variable 
remains unknown and
can't be "forall 'a. 'a".

Take this simple example :

let x = ref []
So x : '_a ref, because if x were 'a ref, then you could write
x := [343] ; x := ["hello"]
and there would be a type error at runtime.

Thus, even if an expression such as id2 in :
let id x = x ;;
let id2 = id id ;;
does exactly the same computing as id, it can't be 'a -> 'a because id2 
is an "application" and ocaml doesn't generalize applications...

Good luck,

--
 Philippe Wang



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

* Re: Typing question
  1999-10-25 15:07 Typing question Sylvain
  1999-10-26  8:02 ` Sven LUTHER
  1999-10-26 18:16 ` skaller
@ 1999-10-28 18:15 ` Jerome Vouillon
  2 siblings, 0 replies; 7+ messages in thread
From: Jerome Vouillon @ 1999-10-28 18:15 UTC (permalink / raw)
  To: Sylvain, caml-list


Hello,

On Mon, Oct 25, 1999 at 05:07:58PM +0200, Sylvain wrote:
> I tried to compile this file "foobar.ml" issuing 
> 
> ocamlc -c -i foobar.ml > foobar.mli
> ocamlc -c -i foobar.mli
> ocamlc -c foobar.ml
> 
> But at the third stage of the process, it answers me that the
> implementation doesn't match the interface ...

The type of a class in not always printed correctly.
In your case, the type of the first class should be :
    class nameTable :
      'a ->
      object
        constraint 'a = #answerToIsNewName
        val fatherContext : 'a
        val table : (string, nameInfo) Hashtbl.t
        method addName : string -> nameType -> unit
        method delName : string -> unit
        method isNewName : string -> bool
        method newName : nameType -> string
      end

-- Jérôme




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

* Re: Typing question
  1999-10-25 15:07 Typing question Sylvain
  1999-10-26  8:02 ` Sven LUTHER
@ 1999-10-26 18:16 ` skaller
  1999-10-28 18:15 ` Jerome Vouillon
  2 siblings, 0 replies; 7+ messages in thread
From: skaller @ 1999-10-26 18:16 UTC (permalink / raw)
  To: Sylvain; +Cc: caml-list

Sylvain wrote:
> 
> Hello,
> 
> I tried to compile this file "foobar.ml" issuing
> 
> ocamlc -c -i foobar.ml > foobar.mli
> ocamlc -c -i foobar.mli

            ^^
            Did you really mean this?

> ocamlc -c foobar.ml
> 
> But at the third stage of the process, it answers me that the
> implementation doesn't match the interface ...
                               Sylvain.Baro@lip6.fr

-- 
John Skaller, mailto:skaller@maxtal.com.au
1/10 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
downloads: http://www.triode.net.au/~skaller




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

* Re: Typing question
  1999-10-26  8:02 ` Sven LUTHER
@ 1999-10-26 10:35   ` Sylvain
  0 siblings, 0 replies; 7+ messages in thread
From: Sylvain @ 1999-10-26 10:35 UTC (permalink / raw)
  To: luther; +Cc: caml-list

Hello,

yes, but the mli file is compiled correctly, but it seems that the cmi
file is no more compatible with the ml file...


> 
> Some time ago i was told that the -i flag not always produce a syntax-correct mli file.
> 
> Friendly,
> 
> Sven LUTHER
> 

-- 

					Sylvain Baro
					Sylvain.Baro@lip6.fr




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

* Re: Typing question
  1999-10-25 15:07 Typing question Sylvain
@ 1999-10-26  8:02 ` Sven LUTHER
  1999-10-26 10:35   ` Sylvain
  1999-10-26 18:16 ` skaller
  1999-10-28 18:15 ` Jerome Vouillon
  2 siblings, 1 reply; 7+ messages in thread
From: Sven LUTHER @ 1999-10-26  8:02 UTC (permalink / raw)
  To: Sylvain, caml-list

On Mon, Oct 25, 1999 at 05:07:58PM +0200, Sylvain wrote:
> Hello,
> 
> I tried to compile this file "foobar.ml" issuing 
> 
> ocamlc -c -i foobar.ml > foobar.mli
> ocamlc -c -i foobar.mli
> ocamlc -c foobar.ml

Some time ago i was told that the -i flag not always produce a syntax-correct mli file.

Friendly,

Sven LUTHER




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

* Typing question
@ 1999-10-25 15:07 Sylvain
  1999-10-26  8:02 ` Sven LUTHER
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Sylvain @ 1999-10-25 15:07 UTC (permalink / raw)
  To: caml-list

Hello,

I tried to compile this file "foobar.ml" issuing 

ocamlc -c -i foobar.ml > foobar.mli
ocamlc -c -i foobar.mli
ocamlc -c foobar.ml

But at the third stage of the process, it answers me that the
implementation doesn't match the interface ...

I joined the file "as is" in the following, because it is a short one.

-- begin --
(* Name server *)

open Hashtbl


exception EmptyContextAllocation
exception NameAlreadyUsed of string


class virtual answerToIsNewName =
  object 
  method virtual isNewName : string -> bool
end

type nameType =
    ConstrName
  | TypeName
  | PredName
  | VarName
  | MuName


type nameInfo =
    { kind : nameType ; num : int}


(* one table because each  number identificator is unique *)
let (numTable : (int,string) t) =
  create 111


let global_name_cpt = ref 0
let gen_new_global () = 
  incr global_name_cpt ; !global_name_cpt




(* one nameTable should be created for each scope *)
class nameTable (fatherContext : #answerToIsNewName) =
  object(self)
  val table  =
    (create 17 : (string,nameInfo) t)

  val fatherContext  = ((fatherContext) : #answerToIsNewName)

 method newName kindOf  =
    ((let newnum = gen_new_global ()
    in
    let newname = string_of_int newnum (* Temporaire *)
    in
    let register () = 
      add numTable newnum newname ;
      add table newname {kind = kindOf ; num = newnum}
    in
    try
      find table newname ; 
      self#newName kindOf
    with Not_found ->
      (register () ; newname)) : string)
          


  method isNewName n = 
    try 
      find table n ; false
    with Not_found -> fatherContext#isNewName n


  method addName name kindOf =
    let newnum = gen_new_global ()
    in
    let register () = 
      add numTable newnum name ;
      add table name {kind = kindOf ; num = newnum}
    in
    try
      find table name ; 
      raise (NameAlreadyUsed name)
    with Not_found ->
      register ()
   
  method delName name =
    remove table name

end


class emptyContextClass =
  object
  method newName (x : nameType) = ((raise EmptyContextAllocation) :
string)
  method isNewName (s : string) = true
  method addName (n:string) (k:nameType) = 
    ((raise EmptyContextAllocation) : unit)

end

let emptyContext = new emptyContextClass


-- end --

Thank you.

-- 

					Sylvain Baro
					Sylvain.Baro@lip6.fr




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

end of thread, other threads:[~2007-04-24 22:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-24 18:14 typing question micha
2007-04-24 22:22 ` [Caml-list] " Philippe Wang
  -- strict thread matches above, loose matches on Subject: below --
1999-10-25 15:07 Typing question Sylvain
1999-10-26  8:02 ` Sven LUTHER
1999-10-26 10:35   ` Sylvain
1999-10-26 18:16 ` skaller
1999-10-28 18:15 ` Jerome Vouillon

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