On Mon, Sep 25, 2000 at 03:33:54PM -0700, Peter Achten wrote:
> At 07:46 25-9-00 -0300, Prof. José Romildo Malaquias wrote:
> 
> >Hello.
> >
> >Is there any Haskell implementation that supports
> >extensible data types, in which new value constructors
> >can be added to a previously declared data type,
> >like
> >
> >         data Fn = Sum | Pro | Pow
> >         ...
> >         extend data Fn = Sin | Cos | Tan
> >
> >where first the Fn datatype had only three values,
> >(Sum, Pro and Pow) but later it was extended with
> >three new values (Sin, Cos and Tan)?
> >
> >What are the pointers to documentation on such
> >kind of extensions to Haskell?
> 
> In the Clean Object I/O library we encountered a similar challenge and 
> solved it using type constructor classes. The solution can also be used in 
> Haskell. The basic idea is as follows:
> 
> (1) For each type constructor that has to be extensible you introduce a 
> type constructor class;
> (2) For each of the data constructors of such type constructors you 
> introduce a new type constructor (reusing their name is very convenient);
> (3) For each such new type constructor define it as an instance of the type 
> constructor class that was created from its parent type constructor.
> 
> Example:
> 
> (1) From type constructor Fn you create:
> class FnExt where
>      ...   -- Define your class member functions here
> 
> (2) From the data constructors Sum | Pro | Pow you create:
> data Sum = Sum
> data Pro  = Pro
> data Pow  = Pow
> 
> (3) For each newly defined type constructor in (2) you define an instance:
> instance FnExt Sum
> instance FnExt Pro
> instance FnExt Pow
> 
> 
> All of your functions that previously were defined on Fn are now overloaded 
> functions:
> 
> fun :: ... Fn ... -> ... Fn ...
> 
> becomes:
> 
> fun :: (FnExt fn) => ... fn ... -> ... fn ...
> 
> I hope you find this scheme useful. I guess its applicability depends on 
> your particular class member functions and program.

And then how would I define data types based on Fn ? The math expressions
my system has to deal is expressed as something like

        data Expr = Int
                  | App Fn [Expr]

I cannot just define

        data (FnExt fn) => Expr fn = Int
                                   | App fn [Expr fn]

because the second value constructor would not be general enough
and the values of the second form would be all sums, or all products,
and so on.

An existentialy quantified variable would solve this

        data Expr =                              Int
                  | forall fn . (fn :: FnExt) => App fn [Expr]

But I have to think the its consequences to the system.

Anny comments?

Romildo
-- 
Prof. José Romildo Malaquias <[EMAIL PROTECTED]>
Departamento de Computação
Universidade Federal de Ouro Preto
Brasil

Reply via email to