caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Type error
@ 2005-04-02  1:29 Jon Harrop
  2005-04-02  7:50 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 6+ messages in thread
From: Jon Harrop @ 2005-04-02  1:29 UTC (permalink / raw)
  To: caml-list


I've got a type error:

This type of expression, <height:int; width:int; _..> -> unit, contains type 
variables that cannot be generalized

which appears with the form:

let f =
  let t = ref None in
  fun data -> ...

but not in the unnested form:

let t = ref None

let f =
  fun data -> ...

Is that supposed to happen? I thought those two forms were exactly equivalent 
(except for polluting the outer namespace in the latter case).

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


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

* Re: [Caml-list] Type error
  2005-04-02  1:29 Type error Jon Harrop
@ 2005-04-02  7:50 ` Jacques Garrigue
  0 siblings, 0 replies; 6+ messages in thread
From: Jacques Garrigue @ 2005-04-02  7:50 UTC (permalink / raw)
  To: jon; +Cc: caml-list

From: Jon Harrop <jon@ffconsultancy.com>

> I've got a type error:
> 
> This type of expression, <height:int; width:int; _..> -> unit, contains type 
> variables that cannot be generalized
> 
> which appears with the form:
> 
> let f =
>   let t = ref None in
>   fun data -> ...
> 
> but not in the unnested form:
> 
> let t = ref None
> 
> let f =
>   fun data -> ...
> 
> Is that supposed to happen? I thought those two forms were exactly
> equivalent.  

They are not.
The criterion to decide whether to generalize type variables in a
definition is whether it is syntactically a "value" or not.
Since [ref None] is not a value, your first definition ends up not
being a value, and cannot be generalized.
The second one is ok, because probably t is later constrained to have
a specific type (not containing type variables) and f is now a "value"
and can be properly generalized.
Note also that this error message does not occur if you provide an
interface for your module. But you may in place have a message about a
type not being included in another, if you really expected the
function to be polymorphic.

Jacques Garrigue


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

* type error
@ 2008-04-01  2:27 Jacques Le Normand
  0 siblings, 0 replies; 6+ messages in thread
From: Jacques Le Normand @ 2008-04-01  2:27 UTC (permalink / raw)
  To: caml-list caml-list

[-- Attachment #1: Type: text/plain, Size: 320 bytes --]

hello caml-list
thanks for all the help so far; it's been very educational
there's a type error I can't get my head around:


class a =
object
end

and b =
object
  inherit a
  method d (e : b) = (e :> a)
end


gives the error:

The abbreviation b expands to type < d : b -> a > but is used with type <  >

why is this?

[-- Attachment #2: Type: text/html, Size: 438 bytes --]

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

* Re: type error
  2008-03-23 22:19 ` [Caml-list] " Martin Jambon
@ 2008-03-24  8:53   ` Remi Vanicat
  0 siblings, 0 replies; 6+ messages in thread
From: Remi Vanicat @ 2008-03-24  8:53 UTC (permalink / raw)
  To: caml-list

Martin Jambon <martin.jambon@ens-lyon.org> writes:

> A more idiomatic way is to declare the following type:
>
> type foo = < bar: int >

An even more idiomatic way to do it is by using type class:

class type foo = object method bar : int end;;

and then you can use #foo that will be the type you wanted, Martin's
example become:

# class type foo = object method bar : int end;;
class type foo = object method bar : int end
# let print_bar (x : #foo) = print_int x # bar;;
val print_bar : #foo -> unit = <fun>
# let x =
    (object
     method bar = 123
       method hello () = print_endline "hello"
       end);;
val x : < bar : int; hello : unit -> unit > = <obj>
# print_bar x;;
123- : unit = ()



-- 
Rémi Vanicat


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

* type error
@ 2008-03-23 22:01 Jacques Le Normand
  2008-03-23 22:19 ` [Caml-list] " Martin Jambon
  0 siblings, 1 reply; 6+ messages in thread
From: Jacques Le Normand @ 2008-03-23 22:01 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 232 bytes --]

Hello caml-list,
I don't quite understand what the error is here:

 type foo = <bar:int;..>

error:

A type variable is unbound in this type declaration.
In definition < bar : int; .. > the variable 'a is unbound


cheers
--Jacques

[-- Attachment #2: Type: text/html, Size: 298 bytes --]

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

* Type error
@ 2006-10-15  3:29 Denis Bueno
  0 siblings, 0 replies; 6+ messages in thread
From: Denis Bueno @ 2006-10-15  3:29 UTC (permalink / raw)
  To: OCaml Mailing List

I'm writing a simple (stupid) de-functorised Set library. Its skeleton is:

,----
| type 'a t
|   (** The type of sets. *)
|
| val empty: 'a t
|   (** The empty set. *)
|
| val mem: 'a -> 'a t -> bool
|   (** [mem x s] tests whether [x] belongs to the set [s]. *)
|
| val add: 'a -> 'a t -> 'a t
|   (** [add x s] returns a set containing all elements of [s],
|       plus [x]. If [x] was already in [s], [s] is returned unchanged. *)
`----

In my implementation, sets are lists.

One of the things I'd like to do is write an of_array to create an
array from a set. However, it is often the case that I have an array
of objects from which I'd like to pull out one field & make a set of
those fields. So, my of_array looks like this:

Interface:
,----
| val of_array: ?getter : ('b -> 'a) -> 'b array -> 'a t
|   (** [of_array xs] returns a set containing the elements in [xs]. *)
`----

Implementation:
,----
| let of_array ?(getter = fun x -> x) xs =
|   Array.fold_right (fun x set -> add (getter x) set) xs empty;;
`----

I get a type error when trying to compile this. I don't know how to
fix it, or even if it's possible without wrecking my nice of_array
signature. The type error:

,----
| The implementation pSet.ml does not match the interface pSet.cmi:
| Values do not match:
|   val of_array : ?getter:('a -> 'a) -> 'a array -> 'a list
| is not included in
|   val of_array : ?getter:('a -> 'b) -> 'a array -> 'b t
`----

I understand why this shouldn't be compilable, but, is it possible to
do something similarly elegant (without creating a separate function)?

-Denis


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

end of thread, other threads:[~2008-04-01  2:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-02  1:29 Type error Jon Harrop
2005-04-02  7:50 ` [Caml-list] " Jacques Garrigue
2006-10-15  3:29 Denis Bueno
2008-03-23 22:01 type error Jacques Le Normand
2008-03-23 22:19 ` [Caml-list] " Martin Jambon
2008-03-24  8:53   ` Remi Vanicat
2008-04-01  2:27 Jacques Le Normand

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