caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Q] Four micro-questions on objects and modules.
@ 1999-11-02 13:09 Christian RINDERKNECHT
  1999-11-03 21:40 ` Jerome Vouillon
  0 siblings, 1 reply; 3+ messages in thread
From: Christian RINDERKNECHT @ 1999-11-02 13:09 UTC (permalink / raw)
  To: caml-list; +Cc: Christian RINDERKNECHT

Hello,

I have a few questions on objects and modules.

 1) Assume I compile m.ml made of: class c = object end
                        and m.mli: class c : object end

    Now:

            Objective Caml version 2.02
 
    # #load "m.cmo";;
    # let f = fun (x : #M.c) -> x;;
    val f : (#M.c as 'a) -> 'a = <fun>

    OK. What strikes me is:

    # let g = fun (x : M.c) -> x;;
    val g : M.c -> M.c = <fun>

    What is the difference between f and g? If the type of g is
    equivalent to #M.c -> #M.c, then according to the manual (#-types)
    there is a difference, isn'it?

    Moreover, I tought "M.c" was not syntactically correct when "c"
    is a class (hence the "#" symbol)... 


 2) What is the semantics of the syntactic productions
    (http://caml.inria.fr/ocaml/htmlman/node6.5.html):
 
    typexpr : typexpr # class-path
            | ( typexpr {, typexpr}) # class-path


 3) Would it be possible to allow the "include" feature in class
    types? 


 4) With values one can equivalently write:

    # let (h : unit -> unit) = fun x -> x;;
    val h : unit -> unit = <fun>

    or

    # let h = ((fun x -> x) : unit -> unit);;
    val h : unit -> unit = <fun>

    I prefer the former when the function expression is big and I don't
    want my eyes to read it only in order to find the type annotation
    at the end.

    For the same sake, I think it would be consistent and useful to
    allow class types, module types to qualify directly the module
    names and the class names: 

    # module (M : S) = struct (* lot of stuff here *) end;;
             ^
    Not a Syntax error!

    and

    # class (x : ct) = object (* lot of stuff here *) end;;     
            ^
    Not a Syntax error!

    ...and the same for method and value instances.


Best regards,

-- 

Christian

-----------------------------------------------------------------------
Christian Rinderknecht                     Phone +33 (0)1 60 76 44 43
Institut National des Télécommunications   Fax   +33 (0)1 60 76 47 11
Département Logiciels Réseaux (LOR)        WWW
9, Rue Charles Fourier, F-91011 Évry Cedex



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

* Re: [Q] Four micro-questions on objects and modules.
  1999-11-02 13:09 [Q] Four micro-questions on objects and modules Christian RINDERKNECHT
@ 1999-11-03 21:40 ` Jerome Vouillon
  1999-11-07 10:37   ` Christian RINDERKNECHT
  0 siblings, 1 reply; 3+ messages in thread
From: Jerome Vouillon @ 1999-11-03 21:40 UTC (permalink / raw)
  To: Christian RINDERKNECHT


Hello,

>  1) Assume I compile m.ml made of: class c = object end
>                         and m.mli: class c : object end
> 
>     Now:
> 
>             Objective Caml version 2.02
>  
>     # #load "m.cmo";;
>     # let f = fun (x : #M.c) -> x;;
>     val f : (#M.c as 'a) -> 'a = <fun>
> 
>     OK. What strikes me is:
> 
>     # let g = fun (x : M.c) -> x;;
>     val g : M.c -> M.c = <fun>
> 
>     What is the difference between f and g? If the type of g is
>     equivalent to #M.c -> #M.c, then according to the manual (#-types)
>     there is a difference, isn'it?

If you remove the abbreviations, the type of f is
  (< .. > as 'a) -> 'a
So, this function can be applied to any object and returns an object
of the same type (you should understand the ellipsis as a type
variable that can be instantiated to any row of methods). If it had
type #M.c -> #M.c, that is,
   < .. > -> < .. >
then, it could be apply to any object and would returns an object of
any type. You cannot write a function of this type unless it never
terminates or always raises an exception.
Finally, the type of g is
   < > -> < >
This function can only be applied to an object with no method (you can
actually apply it to any object by using a coercion first), and it
returns an object with no method.

>     Moreover, I tought "M.c" was not syntactically correct when "c"
>     is a class (hence the "#" symbol)... 
>
>  2) What is the semantics of the syntactic productions
>     (http://caml.inria.fr/ocaml/htmlman/node6.5.html):
>  
>     typexpr : typexpr # class-path
>             | ( typexpr {, typexpr}) # class-path

A class defines two type abbreviations: here M.c and #M.c.  The first
one is the type of an object of this class. The second one is more
general, and is the type of any object that has at least the methods
of class M.c, but may have more methods. In particular the type of an
object of any subclass of M.c will be an instance of type #M.c.

>  3) Would it be possible to allow the "include" feature in class
>     types? 

What do you mean ? A class type can inherit from another class type :
   class type c = object method m : int end
   class type d = object inherit c method n : bool end

>  4) With values one can equivalently write:
> 
>     # let (h : unit -> unit) = fun x -> x;;
>     val h : unit -> unit = <fun>
> 
>     or
 
>     # let h = ((fun x -> x) : unit -> unit);;
>     val h : unit -> unit = <fun>
> 
>     I prefer the former when the function expression is big and I don't
>     want my eyes to read it only in order to find the type annotation
>     at the end.
> 
>     For the same sake, I think it would be consistent and useful to
>     allow class types, module types to qualify directly the module
>     names and the class names: 
> 
>     # module (M : S) = struct (* lot of stuff here *) end;;
>              ^
>     Not a Syntax error!
> 
>     and
> 
>     # class (x : ct) = object (* lot of stuff here *) end;;     
>             ^
>     Not a Syntax error!
> 
>     ...and the same for method and value instances.

You can also write
    let h : unit -> unit = fun x -> x;;
(without parentheses)
The same syntax is also accepted for modules and classes.

-- Jérôme




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

* Re: [Q] Four micro-questions on objects and modules.
  1999-11-03 21:40 ` Jerome Vouillon
@ 1999-11-07 10:37   ` Christian RINDERKNECHT
  0 siblings, 0 replies; 3+ messages in thread
From: Christian RINDERKNECHT @ 1999-11-07 10:37 UTC (permalink / raw)
  To: Jerome Vouillon; +Cc: caml-list

Hello,

> >  3) Would it be possible to allow the "include" feature in class
> >     types? 
> 
> What do you mean ? A class type can inherit from another class type :
>    class type c = object method m : int end
>    class type d = object inherit c method n : bool end

Okay. This is exactly what I was asking for. But... now I am a little
bit confused since I was used to think that inheritance was a concept
that applies to classes, and (sub)typing to class types. Could you
explain? (Please note I am new to these topics...)

It may be not related, but the toplevel does not print out the "inherit"
construct after the typing of the classes:

        Objective Caml version 2.02

# class x =                        
    object
      method m = ()
    end
  ;;
class x : object method m : unit end
# class y =
    object
      inherit x  
      method n = ()
    end
  ;;
class y : object method m : unit method n : unit end
                 ^^^^^^^^^^^^^^^
                 No "inherit x" used instead here...


Best regards,

-- 

Christian

-----------------------------------------------------------------------
Christian Rinderknecht                     Phone +33 (0)1 60 76 44 43
Institut National des Télécommunications   Fax   +33 (0)1 60 76 47 11
Département Logiciels Réseaux (LOR)        WWW
9, Rue Charles Fourier, F-91011 Évry Cedex



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

end of thread, other threads:[~1999-11-07 21:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-11-02 13:09 [Q] Four micro-questions on objects and modules Christian RINDERKNECHT
1999-11-03 21:40 ` Jerome Vouillon
1999-11-07 10:37   ` Christian RINDERKNECHT

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