caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Define class and sum type in one time
@ 2011-03-17 10:08 Pierre-Alexandre Voye
  2011-03-17 10:33 ` [Caml-list] " Pierre-Alexandre Voye
  0 siblings, 1 reply; 11+ messages in thread
From: Pierre-Alexandre Voye @ 2011-03-17 10:08 UTC (permalink / raw)
  To: caml-list

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

Hi everyone, i'm trying to write a generic hierarchical finite state
machine, and I have a syntax problem which stop me.
I want to define an object which use, somewhere a sum type.
The compiler answer "Syntax Error"

Here a simple example of my problem :

class machin = object(self)
  val mutable valeur = 0
  method getValeur = valeur
end
and
truc = Machin of (int->int) | Recur of truc;;

And the answer :
Error: Syntax error
 which point  on the "of" after "Machin"

Is there a way to solve this problem ?

In advance, thank you very much

Pierre-Alexandre
-- 
---------------------
Isaac Project - http://www.lisaac.org/

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

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

* [Caml-list] Re: Define class and sum type in one time
  2011-03-17 10:08 [Caml-list] Define class and sum type in one time Pierre-Alexandre Voye
@ 2011-03-17 10:33 ` Pierre-Alexandre Voye
  2011-03-17 10:47   ` Guillaume Yziquel
  0 siblings, 1 reply; 11+ messages in thread
From: Pierre-Alexandre Voye @ 2011-03-17 10:33 UTC (permalink / raw)
  To: caml-list

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

My example would be better with this :

class machin = object(self)
  val mutable valeur = (None : truc option)
  method getValeur = valeur
end
and
truc = Machin of (int->int) | Recur of truc;;

The object need the type defined

2011/3/17 Pierre-Alexandre Voye <ontologiae@gmail.com>

> Hi everyone, i'm trying to write a generic hierarchical finite state
> machine, and I have a syntax problem which stop me.
> I want to define an object which use, somewhere a sum type.
> The compiler answer "Syntax Error"
>
> Here a simple example of my problem :
>
> class machin = object(self)
>   val mutable valeur = 0
>   method getValeur = valeur
> end
> and
> truc = Machin of (int->int) | Recur of truc;;
>
> And the answer :
> Error: Syntax error
>  which point  on the "of" after "Machin"
>
> Is there a way to solve this problem ?
>
> In advance, thank you very much
>
> Pierre-Alexandre
> --
> ---------------------
> Isaac Project - http://www.lisaac.org/
>



-- 
---------------------
Isaac Project - http://www.lisaac.org/

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

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

* Re: [Caml-list] Re: Define class and sum type in one time
  2011-03-17 10:33 ` [Caml-list] " Pierre-Alexandre Voye
@ 2011-03-17 10:47   ` Guillaume Yziquel
  2011-03-17 10:50     ` Pierre-Alexandre Voye
       [not found]     ` <350217430.1223639.1300359057593.JavaMail.root@zmbs3.inria.fr>
  0 siblings, 2 replies; 11+ messages in thread
From: Guillaume Yziquel @ 2011-03-17 10:47 UTC (permalink / raw)
  To: Pierre-Alexandre Voye; +Cc: caml-list

Le Thursday 17 Mar 2011 à 11:33:44 (+0100), Pierre-Alexandre Voye a écrit :
>    My example would be better with this :
>    class machin = object(self)
>    Â  val mutable valeur = (None : truc option)
>    Â  method getValeur = valeur
>    end
>    and
>    truc = Machin of (int->int) | Recur of truc;;
>    The object need the type defined

You cannot declare a class and a type with an 'and'. Either two classes,
or two types.

Simply declare truc before machin.

type truc = Machin of int -> int | Recur of truc

class machin = object

	val mutable valeur : truc option = None

	method getValeur = valeur

end

-- 
     Guillaume Yziquel


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

* Re: [Caml-list] Re: Define class and sum type in one time
  2011-03-17 10:47   ` Guillaume Yziquel
@ 2011-03-17 10:50     ` Pierre-Alexandre Voye
  2011-03-17 11:01       ` David Rajchenbach-Teller
                         ` (2 more replies)
       [not found]     ` <350217430.1223639.1300359057593.JavaMail.root@zmbs3.inria.fr>
  1 sibling, 3 replies; 11+ messages in thread
From: Pierre-Alexandre Voye @ 2011-03-17 10:50 UTC (permalink / raw)
  To: caml-list

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

So, this is impossible  ?   :

class maclasse = object(self)
  val mutable valeur = (None : truc option)
  method getValeur = valeur
end
and
truc = Machin of (maclasse->int) | Recur of truc;;

?
(three attempt to write a correct example of my problem.. All apologies !)

2011/3/17 Guillaume Yziquel <guillaume.yziquel@citycable.ch>

> Le Thursday 17 Mar 2011 à 11:33:44 (+0100), Pierre-Alexandre Voye a écrit :
> >    My example would be better with this :
> >    class machin = object(self)
> >    Â  val mutable valeur = (None : truc option)
> >    Â  method getValeur = valeur
> >    end
> >    and
> >    truc = Machin of (int->int) | Recur of truc;;
> >    The object need the type defined
>
> You cannot declare a class and a type with an 'and'. Either two classes,
> or two types.
>
> Simply declare truc before machin.
>
> type truc = Machin of int -> int | Recur of truc
>
> class machin = object
>
>        val mutable valeur : truc option = None
>
>        method getValeur = valeur
>
> end
>
> --
>      Guillaume Yziquel
>



-- 
---------------------
Isaac Project - http://www.lisaac.org/

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

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

* Re: [Caml-list] Re: Define class and sum type in one time
  2011-03-17 10:50     ` Pierre-Alexandre Voye
@ 2011-03-17 11:01       ` David Rajchenbach-Teller
  2011-03-17 11:14         ` David Rajchenbach-Teller
  2011-03-17 11:05       ` Guillaume Yziquel
  2011-03-18 17:16       ` Gerd Stolpmann
  2 siblings, 1 reply; 11+ messages in thread
From: David Rajchenbach-Teller @ 2011-03-17 11:01 UTC (permalink / raw)
  To: Pierre-Alexandre Voye; +Cc: caml-list

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

You'll probably need something along the lines of:

class maclasse = object(self)
  val mutable valeur = (None : truc option)
  method getValeur = valeur
end
let truc_maker('a) = Machin of 'a -> int | Recur of truc_maker('a)
let truc = truc_maker(maclasse)

On Mar 17, 2011, at 11:50 AM, Pierre-Alexandre Voye wrote:

> So, this is impossible  ?   :
> 
> class maclasse = object(self)
>   val mutable valeur = (None : truc option)
>   method getValeur = valeur
> end
> and 
> truc = Machin of (maclasse->int) | Recur of truc;; 
> 
> ?
> (three attempt to write a correct example of my problem.. All apologies !)
> 
> 2011/3/17 Guillaume Yziquel <guillaume.yziquel@citycable.ch>
> 
> 
> 
> -- 
> ---------------------
> Isaac Project - http://www.lisaac.org/


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

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

* Re: [Caml-list] Re: Define class and sum type in one time
       [not found]     ` <350217430.1223639.1300359057593.JavaMail.root@zmbs3.inria.fr>
@ 2011-03-17 11:02       ` Paolo Herms
  0 siblings, 0 replies; 11+ messages in thread
From: Paolo Herms @ 2011-03-17 11:02 UTC (permalink / raw)
  To: caml-list

Yes, but you can still do this:

type 'maclasse truc = Machin of ('maclasse->int) | Recur of 'maclasse truc

class maclasse  = object(self)
  val mutable valeur = (None : maclasse truc option)
  method getValeur = valeur
end
-- 
Paolo Herms
PhD Student - CEA-LIST Software Safety Lab. / INRIA ProVal Project
Paris, France

On Thursday 17 March 2011 11:50:57 Pierre-Alexandre Voye wrote:
> So, this is impossible  ?   :
> 
> class maclasse = object(self)
>   val mutable valeur = (None : truc option)
>   method getValeur = valeur
> end
> and
> truc = Machin of (maclasse->int) | Recur of truc;;


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

* Re: [Caml-list] Re: Define class and sum type in one time
  2011-03-17 10:50     ` Pierre-Alexandre Voye
  2011-03-17 11:01       ` David Rajchenbach-Teller
@ 2011-03-17 11:05       ` Guillaume Yziquel
  2011-03-17 13:44         ` Gabriel Scherer
       [not found]         ` <1080649473.122478.1300369493351.JavaMail.root@zmbs4.inria.fr>
  2011-03-18 17:16       ` Gerd Stolpmann
  2 siblings, 2 replies; 11+ messages in thread
From: Guillaume Yziquel @ 2011-03-17 11:05 UTC (permalink / raw)
  To: Pierre-Alexandre Voye; +Cc: caml-list

Le Thursday 17 Mar 2011 à 11:50:37 (+0100), Pierre-Alexandre Voye a écrit :
>    So, this is impossible  ?   :
>    class maclasse = object(self)
>    Â  val mutable valeur = (None : truc option)
>    Â  method getValeur = valeur
>    end
>    and
>    truc = Machin of (maclasse->int) | Recur of truc;;
>    ?
>    (three attempt to write a correct example of my problem.. All apologies
>    !)

No, you can. Break the recursivity with a recursive module. However,
it's quite clumsy, so if you do not have a compelling reason to use
object, avoid the useless complexity.

module rec M : sig

	class maclasse : object

		val mutable valeur : M.truc option 

		method getValeur : M.truc option

	end

	type truc = Machin of (maclasse -> int) | Recur of truc

end = struct

	class maclasse = object

		val mutable valeur : M.truc option = None

		method getValeur = valeur

	end

	type truc = Machin of (maclasse -> int) | Recur of truc

end

include M

-- 
     Guillaume Yziquel


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

* Re: [Caml-list] Re: Define class and sum type in one time
  2011-03-17 11:01       ` David Rajchenbach-Teller
@ 2011-03-17 11:14         ` David Rajchenbach-Teller
  0 siblings, 0 replies; 11+ messages in thread
From: David Rajchenbach-Teller @ 2011-03-17 11:14 UTC (permalink / raw)
  To: David Rajchenbach-Teller; +Cc: Pierre-Alexandre Voye, caml-list

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

Arf, sorry, I got mixed with OPA syntax (or revised syntax).

This was actually

let 'a truc_maker = Machin of 'a -> int | Recur of 'a truc_maker
let truc = maclasse truc_maker

On Mar 17, 2011, at 12:01 PM, David Rajchenbach-Teller wrote:

> You'll probably need something along the lines of:
> 
> class maclasse = object(self)
>   val mutable valeur = (None : truc option)
>   method getValeur = valeur
> end
> let truc_maker('a) = Machin of 'a -> int | Recur of truc_maker('a)
> let truc = truc_maker(maclasse)
> 
> On Mar 17, 2011, at 11:50 AM, Pierre-Alexandre Voye wrote:
> 
>> So, this is impossible  ?   :
>> 
>> class maclasse = object(self)
>>   val mutable valeur = (None : truc option)
>>   method getValeur = valeur
>> end
>> and 
>> truc = Machin of (maclasse->int) | Recur of truc;; 
>> 
>> ?
>> (three attempt to write a correct example of my problem.. All apologies !)
>> 
>> 2011/3/17 Guillaume Yziquel <guillaume.yziquel@citycable.ch>
>> 
>> 
>> 
>> -- 
>> ---------------------
>> Isaac Project - http://www.lisaac.org/
> 


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

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

* Re: [Caml-list] Re: Define class and sum type in one time
  2011-03-17 11:05       ` Guillaume Yziquel
@ 2011-03-17 13:44         ` Gabriel Scherer
       [not found]         ` <1080649473.122478.1300369493351.JavaMail.root@zmbs4.inria.fr>
  1 sibling, 0 replies; 11+ messages in thread
From: Gabriel Scherer @ 2011-03-17 13:44 UTC (permalink / raw)
  To: Guillaume Yziquel; +Cc: Pierre-Alexandre Voye, caml-list, caml-webmaster

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

On Thu, Mar 17, 2011 at 12:05 PM, Guillaume Yziquel <
guillaume.yziquel@citycable.ch> wrote:

> No, you can. Break the recursivity with a recursive module. However,
> it's quite clumsy, so if you do not have a compelling reason to use
> object, avoid the useless complexity.
>

I have seen similar questions (how to define mutually recursive
type/exception/classes...) asked many times. It is quite natural and the
answer is not so obvious. Maybe it would be a good candidate for adoption in
the OCaml FAQ?
  http://caml.inria.fr/resources/doc/faq/index.en.html

PS : I remember reading about an extension to OCaml where all compilation
units where implicitly recursive, so that you could write Current_module.foo
and have mutual structure item recursion for free. I thought it was from
Alain Frisch, but a search on Lexifi's blog didn't return anything. Has one
list reader kept the reference to this article?

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

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

* Re: [Caml-list] Re: Define class and sum type in one time
       [not found]         ` <1080649473.122478.1300369493351.JavaMail.root@zmbs4.inria.fr>
@ 2011-03-17 13:54           ` Fabrice Le Fessant
  0 siblings, 0 replies; 11+ messages in thread
From: Fabrice Le Fessant @ 2011-03-17 13:54 UTC (permalink / raw)
  To: caml-list

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

On 03/17/2011 02:44 PM, Gabriel Scherer wrote:

> PS : I remember reading about an extension to OCaml where all
> compilation units where implicitly recursive, so that you could write
> Current_module.foo and have mutual structure item recursion for free. I
> thought it was from Alain Frisch, but a search on Lexifi's blog didn't
> return anything. Has one list reader kept the reference to this article?

On Tue, Dec 28, 2010 at 03:59:32PM +0100, Alain Frisch wrote:

...

I can also mention a local extension we use at LexiFi: a compilation
unit (.ml) for which an explicit signature (.mli) exists is type-checked
as a recursive module with itself. (Currently, we disallow recursive
references to value-like components.)  So in the example above, the
definition of the module type S could be put in Bar.mli:

...

--Fabrice

[-- Attachment #2: fabrice_le_fessant.vcf --]
[-- Type: text/x-vcard, Size: 384 bytes --]

begin:vcard
fn:Fabrice LE FESSANT
n:LE FESSANT;Fabrice
org:INRIA Saclay -- Ile-de-France;Projet OCamlPro
adr;quoted-printable:;;Parc Orsay Universit=C3=A9 ;Orsay CEDEX;;91893;France
email;internet:fabrice.le_fessant@inria.fr
title;quoted-printable:Charg=C3=A9 de Recherche
tel;work:+33 1 74 85 42 14
tel;fax:+33 1 74 85 42 49 
url:http://fabrice.lefessant.net/
version:2.1
end:vcard


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

* Re: [Caml-list] Re: Define class and sum type in one time
  2011-03-17 10:50     ` Pierre-Alexandre Voye
  2011-03-17 11:01       ` David Rajchenbach-Teller
  2011-03-17 11:05       ` Guillaume Yziquel
@ 2011-03-18 17:16       ` Gerd Stolpmann
  2 siblings, 0 replies; 11+ messages in thread
From: Gerd Stolpmann @ 2011-03-18 17:16 UTC (permalink / raw)
  To: Pierre-Alexandre Voye; +Cc: caml-list

Am Donnerstag, den 17.03.2011, 11:50 +0100 schrieb Pierre-Alexandre
Voye:
> So, this is impossible  ?   :
> 
> class maclasse = object(self)
>   val mutable valeur = (None : truc option)
>   method getValeur = valeur
> end
> and 
> truc = Machin of (maclasse->int) | Recur of truc;; 
> 
> ?
> (three attempt to write a correct example of my problem.. All
> apologies !)

This is the closest without introducing a type variable:

type maclasse_t = < getValeur : truc option >
and truc = Machin of maclasse_t->int | Recur of truc

Here, maclasse is just an object type (which is less than a full class
type, as it has only methods but no type information about instance
variables).

In a second step you can define the class:

class maclasse = ( object ... end : maclasse_t )

Generally, it is a good recommendation to define object or class types
before doing the class implementation. Not only allows it more
complicated types but it is also easier to get the typing right, because
you have always maclasse_t at hand to give type hints.

Gerd

> 
> 2011/3/17 Guillaume Yziquel <guillaume.yziquel@citycable.ch>
>         Le Thursday 17 Mar 2011 à 11:33:44 (+0100), Pierre-Alexandre
>         Voye a écrit :
>         >    My example would be better with this :
>         >    class machin = object(self)
>         
>         >    Â  val mutable valeur = (None : truc option)
>         >    Â  method getValeur = valeur
>         >    end
>         >    and
>         >    truc = Machin of (int->int) | Recur of truc;;
>         >    The object need the type defined
>         
>         
>         You cannot declare a class and a type with an 'and'. Either
>         two classes,
>         or two types.
>         
>         Simply declare truc before machin.
>         
>         type truc = Machin of int -> int | Recur of truc
>         
>         class machin = object
>         
>                val mutable valeur : truc option = None
>         
>                method getValeur = valeur
>         
>         end
>         
>         --
>             Guillaume Yziquel
> 
> 
> 
> -- 
> ---------------------
> Isaac Project - http://www.lisaac.org/


-- 
------------------------------------------------------------
Gerd Stolpmann, Bad Nauheimer Str.3, 64289 Darmstadt,Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------



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

end of thread, other threads:[~2011-03-18 17:17 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-17 10:08 [Caml-list] Define class and sum type in one time Pierre-Alexandre Voye
2011-03-17 10:33 ` [Caml-list] " Pierre-Alexandre Voye
2011-03-17 10:47   ` Guillaume Yziquel
2011-03-17 10:50     ` Pierre-Alexandre Voye
2011-03-17 11:01       ` David Rajchenbach-Teller
2011-03-17 11:14         ` David Rajchenbach-Teller
2011-03-17 11:05       ` Guillaume Yziquel
2011-03-17 13:44         ` Gabriel Scherer
     [not found]         ` <1080649473.122478.1300369493351.JavaMail.root@zmbs4.inria.fr>
2011-03-17 13:54           ` Fabrice Le Fessant
2011-03-18 17:16       ` Gerd Stolpmann
     [not found]     ` <350217430.1223639.1300359057593.JavaMail.root@zmbs3.inria.fr>
2011-03-17 11:02       ` Paolo Herms

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