caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Polymorphic variants and inheritance
@ 2012-10-25 11:54 Ivan Gotovchits
  2012-10-25 11:57 ` Didier Cassirame
  0 siblings, 1 reply; 4+ messages in thread
From: Ivan Gotovchits @ 2012-10-25 11:54 UTC (permalink / raw)
  To: caml-list


Here a simple example that illustrates the problem:

   type odds = [`One | `Three ]
   type evens = [`Two | `Four ]
   type numbers = [ odds | evens ]

   class type number = object
     method category: numbers
   end

   class type odd = object
     inherit number
     method category: odds
   end


   class type even = object 
     inherit number
     method category: evens
   end


 
It fails to compile, stating the following error:
       The method category has type odds but is expected to have type numbers
       Type odds = [ `One | `Three ] is not compatible with type
         numbers = [ `Four | `One | `Three | `Two ] 
       The first variant type does not allow tag(s) `Four, `Two

Just by theory, odds are covariant to numbers, so this kind of subtyping
can be done. Is it possible to persuade compiler?  

-- 
         (__) 
         (oo) 
   /------\/ 
  / |    ||   
 *  /\---/\ 
    ~~   ~~   
...."Have you mooed today?"...

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

* Re: [Caml-list] Polymorphic variants and inheritance
  2012-10-25 11:54 [Caml-list] Polymorphic variants and inheritance Ivan Gotovchits
@ 2012-10-25 11:57 ` Didier Cassirame
  2012-10-25 12:14   ` Didier Cassirame
  0 siblings, 1 reply; 4+ messages in thread
From: Didier Cassirame @ 2012-10-25 11:57 UTC (permalink / raw)
  To: Ivan Gotovchits; +Cc: caml-list

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

Hi Ivan,

You may use parameterized classes like this :

type odds = [`One | `Three ]
type evens = [`Two | `Four ]
type numbers = [ odds | evens ]

class type ['a] number = object
  constraint 'a = [< numbers]
  method category: 'a
end

class type ['a] odd = object
  inherit ['a] number

  constraint 'a = odds

  method category: 'a
end


class type ['a] even = object
  inherit ['a] number
  constraint 'a = evens
  method category: 'a
end

This passes the compilation.

didier


2012/10/25 Ivan Gotovchits <ivg@ieee.org>

>
> Here a simple example that illustrates the problem:
>
>    type odds = [`One | `Three ]
>    type evens = [`Two | `Four ]
>    type numbers = [ odds | evens ]
>
>    class type number = object
>      method category: numbers
>    end
>
>    class type odd = object
>      inherit number
>      method category: odds
>    end
>
>
>    class type even = object
>      inherit number
>      method category: evens
>    end
>
>
>
> It fails to compile, stating the following error:
>        The method category has type odds but is expected to have type
> numbers
>        Type odds = [ `One | `Three ] is not compatible with type
>          numbers = [ `Four | `One | `Three | `Two ]
>        The first variant type does not allow tag(s) `Four, `Two
>
> Just by theory, odds are covariant to numbers, so this kind of subtyping
> can be done. Is it possible to persuade compiler?
>
> --
>          (__)
>          (oo)
>    /------\/
>   / |    ||
>  *  /\---/\
>     ~~   ~~
> ...."Have you mooed today?"...
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

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

* Re: [Caml-list] Polymorphic variants and inheritance
  2012-10-25 11:57 ` Didier Cassirame
@ 2012-10-25 12:14   ` Didier Cassirame
  2012-10-26  3:37     ` Ivan Gotovchits
  0 siblings, 1 reply; 4+ messages in thread
From: Didier Cassirame @ 2012-10-25 12:14 UTC (permalink / raw)
  To: Ivan Gotovchits; +Cc: caml-list

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

type odds = [`One | `Three ]
type evens = [`Two | `Four ]
type numbers = [ odds | evens ]

class type ['a] number = object
  constraint 'a = [< numbers]
  method category: 'a
end

class type odd = object
  inherit [odds] number
  method category: odds
end

class type even = object
  inherit [evens] number
  method category: evens
end

It's probably even better like this :)

Cheers,

didier


2012/10/25 Didier Cassirame <didier.cassirame@gmail.com>

> Hi Ivan,
>
> You may use parameterized classes like this :
>
> type odds = [`One | `Three ]
> type evens = [`Two | `Four ]
> type numbers = [ odds | evens ]
>
> class type ['a] number = object
>   constraint 'a = [< numbers]
>   method category: 'a
> end
>
> class type ['a] odd = object
>   inherit ['a] number
>
>   constraint 'a = odds
>
>   method category: 'a
> end
>
>
> class type ['a] even = object
>   inherit ['a] number
>   constraint 'a = evens
>   method category: 'a
> end
>
> This passes the compilation.
>
> didier
>
>
> 2012/10/25 Ivan Gotovchits <ivg@ieee.org>
>
>>
>> Here a simple example that illustrates the problem:
>>
>>    type odds = [`One | `Three ]
>>    type evens = [`Two | `Four ]
>>    type numbers = [ odds | evens ]
>>
>>    class type number = object
>>      method category: numbers
>>    end
>>
>>    class type odd = object
>>      inherit number
>>      method category: odds
>>    end
>>
>>
>>    class type even = object
>>      inherit number
>>      method category: evens
>>    end
>>
>>
>>
>> It fails to compile, stating the following error:
>>        The method category has type odds but is expected to have type
>> numbers
>>        Type odds = [ `One | `Three ] is not compatible with type
>>          numbers = [ `Four | `One | `Three | `Two ]
>>        The first variant type does not allow tag(s) `Four, `Two
>>
>> Just by theory, odds are covariant to numbers, so this kind of subtyping
>> can be done. Is it possible to persuade compiler?
>>
>> --
>>          (__)
>>          (oo)
>>    /------\/
>>   / |    ||
>>  *  /\---/\
>>     ~~   ~~
>> ...."Have you mooed today?"...
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>
>
>

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

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

* Re: [Caml-list] Polymorphic variants and inheritance
  2012-10-25 12:14   ` Didier Cassirame
@ 2012-10-26  3:37     ` Ivan Gotovchits
  0 siblings, 0 replies; 4+ messages in thread
From: Ivan Gotovchits @ 2012-10-26  3:37 UTC (permalink / raw)
  To: Didier Cassirame; +Cc: caml-list

Didier Cassirame <didier.cassirame@gmail.com> writes:

> type odds = [`One | `Three ]
> type evens = [`Two | `Four ]
> type numbers = [ odds | evens ]
>
> class type ['a] number = object
>   constraint 'a = [< numbers]
>   method category: 'a
> end
>
> class type odd = object
>   inherit [odds] number
>   method category: odds
> end
>
> class type even = object
>   inherit [evens] number 
>   method category: evens
> end
>
> It's probably even better like this :)
>
It's an excellent solution, Didier! Thanks alot =)

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

end of thread, other threads:[~2012-10-26  3:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-25 11:54 [Caml-list] Polymorphic variants and inheritance Ivan Gotovchits
2012-10-25 11:57 ` Didier Cassirame
2012-10-25 12:14   ` Didier Cassirame
2012-10-26  3:37     ` Ivan Gotovchits

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