[Haskell-cafe] Newbie Question on type constructors
Benjamin Franksen
benjamin.franksen at bessy.de
Mon Nov 1 18:11:32 EST 2004
On Monday 01 November 2004 22:34, you wrote:
> On 2004-11-01 at 23:01+0100 Benjamin Franksen wrote:
> > On Monday 01 November 2004 21:51, Jon Fairbairn wrote:
> > > Put the data declaration in a module, export the type, but
> > >
> > > not the constructor you want to hide:
> > > > module Shape (Shape(Square), circle) where
> >
> > Since we were talking about 'what can one do with a constructor as
> > compared with a function', it should be mentioned that this also has a
> > disadvantage: you loose pattern matching outside the module. I.e. no
> > longer can say
> >
> > ...
> > case x of
> > Circle radius -> ...
> > Square edge -> ...
> >
> > BTW, would it be possible (in principle) to allow constructor export
> > 'read-only', so matching against a constructor is allowed but not
> > constructing a value?
>
> I don't see why not. To add something to the debate about
> what Circle /is/, I'd like to mention that the declaration
>
> data Shape = Circle Double | Square Double
>
> is a convenience that declares three new names (Shape,
> Circle and Square), but five entities.
>
> There's Shape: a type, Circle, Square:: Double -> Shape:
> constructor functions, and Circle, Square:: Shape -> (Double
> -> t) -> t -> t: destructor functions.
Interesting point of view! But... could you explain the types of the
destructors? And how one would use them to deconstruct a Shape? Because,
hmmm, isn't it rather *one* destructor with type
destructShape :: Shape -> (Double -> t) -> (Double -> t) -> t
where the second and third arguments explain what to do with a Circle resp. a
Square? So that
case s of
Circle r -> f r
Square l -> g l
is another way to write
destructShape s g f
Ben
More information about the Haskell-Cafe
mailing list