caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* 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

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

On Sun, 23 Mar 2008, Jacques Le Normand wrote:

> 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

Basically ".." means "any number of methods of any kind", which needs to 
be represented by a type variable.

You can do something like this, which is probably not very useful:

# type 'a foo = < bar: int; .. > as 'a;;
type 'a foo = 'a constraint 'a = < bar : int; .. >


A more idiomatic way is to declare the following type:

type foo = < bar: int >

And when you need an object to behave as having type foo, you use ":>" to 
hide the extra methods.

# type foo = < bar: int >;;
type foo = < bar : int >
# 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;;
This expression has type < bar : int; hello : unit -> unit >
but is here used with type foo
Only the first object type has a method hello
# print_bar (x :> foo);;
123- : unit = ()



Martin

--
http://wink.com/profile/mjambon
http://martin.jambon.free.fr


^ 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-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

* 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

* Type error
@ 2005-04-02  1:29 Jon Harrop
  0 siblings, 0 replies; 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

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 --
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
  -- strict thread matches above, loose matches on Subject: below --
2008-04-01  2:27 Jacques Le Normand
2006-10-15  3:29 Type error Denis Bueno
2005-04-02  1:29 Jon Harrop

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