[Haskell-cafe] Newbie Question on type constructors

Brian Beckman bbeckman at exchange.microsoft.com
Mon Nov 1 15:30:15 EST 2004


Most interesting discussion -- in reading it, I realized that I had a
'hidden agenda' in asking my question (hidden even from myself), and
that is: can I put interesting functionality, like precondition checks &
data validation, in data constructors?  I suspect not, and that's why I
tend to write something like the following:

  data Shape = Circle Float
             | Square Float
    deriving (Eq, Show)

  circle :: Float -> Shape
  circle x = if (x <= 0) then error "Bad radius!" else Circle x

That's fine, but I don't know how to prevent users from calling "Circle"
directly (in some other languages, I could declare the raw constructor
to be "private", and, in Haskell, there may be some way for me to hack
module exports to hide the raw constructor, but I haven't seen a way to
do that).

In any event, it might be useful if I could have some pattern to prevent
circumvention of initialization code. Advice?

-----Original Message-----
From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Ben Rudiak-Gould
Sent: Monday, November 01, 2004 11:33 AM
To: finnw at iname.com
Cc: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] Newbie Question on type constructors

Finn Wilcox wrote:

 >On Mon, 1 Nov 2004, Ben Rudiak-Gould wrote:
 >
 >>In particular, one cannot write an invert :: (a->b) -> Maybe
 >>(b->a) which never returns a wrong answer, except for invert = const
>>Nothing  >  >How about:
 >
 >invert = undefined
 >
 >This never returns an answer at all, so it can't return a wrong one!

Sorry, I should have been clearer: my Nothing return was intended to
mean "I don't know an answer", not "there is no answer". So my const
Nothing is like your undefined, a function which never returns an answer
(and thus never returns a wrong one).

-- Ben

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list