I was going to argue this point but then it occurred to me that writing a whole bunch of functions like:<br><br>isAFoo :: FooBarBaz -> FooBarBaz -> Bool<br>isAFoo x = typeChecker (Foo undefined) x<br><br>isn't really in any way better than:<br>
<br>isAFoo :: FooBarBaz -> FooBarBaz -> Bool<br>
isAFoo (Foo _) = True<br>isAFoo _ = False<br><br>Although it did give me a chance to play around with Data and Typeable a bit.<br><br clear="all">-R. Kyle Murphy<br>--<br>Curiosity was framed, Ignorance killed the cat.<br>
<br><br><div class="gmail_quote">On Tue, Aug 3, 2010 at 15:25, Brent Yorgey <span dir="ltr"><<a href="mailto:byorgey@seas.upenn.edu">byorgey@seas.upenn.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On Tue, Aug 03, 2010 at 04:19:58PM +0300, Alex Rozenshteyn wrote:<br>
> I have never used Data.Typeable, but maybe it could be made relevant here?<br>
<br>
</div>Not really. Data.Typeable lets you pass (representations of) types<br>
around at runtime, and thus do things like type-safe casts. So it's<br>
useful for things like serialization, extracting things out of<br>
existential wrappers, and so on. It isn't really relevant in this<br>
situation, as Matt already has all the type information he could want.<br>
<font color="#888888"><br>
-Brent<br>
</font><div><div></div><div class="h5"><br>
><br>
> On Tue, Aug 3, 2010 at 4:18 PM, Brent Yorgey <<a href="mailto:byorgey@seas.upenn.edu">byorgey@seas.upenn.edu</a>> wrote:<br>
><br>
> > On Tue, Aug 03, 2010 at 09:51:45PM +1000, Matt Andrew wrote:<br>
> > > Hi all,<br>
> > ><br>
> > > I am in the process of writing a Scheme interpreter/compiler in Haskell<br>
> > as my first serious project after learning the basics of Haskell. The goal<br>
> > is to really get a feel for Haskell. I am trying to accomplish this as much<br>
> > as I can on my own, but am referring to Jonathan Tang's 'Write Yourself a<br>
> > Scheme in 48 hours' whenever I get really stuck.<br>
> > ><br>
> > > I have a question regarding a pattern that I have found within my code<br>
> > for which I cannot seem to find an abstraction.<br>
> > ><br>
> > > I am implementing some of the primitive Scheme type-checker functions<br>
> > with the following code:<br>
> > ><br>
> > > numberP :: SchemeVal -> SchemeVal<br>
> > > numberP (Number _) = Bool True<br>
> > > numberP _ = Bool False<br>
> > ><br>
> > > boolP :: SchemeVal -> SchemeVal<br>
> > > boolP (Bool _) = Bool True<br>
> > > boolP _ = Bool False<br>
> > ><br>
> > > symbolP :: SchemeVal -> SchemeVal<br>
> > > symbolP (Atom _) = Bool True<br>
> > > symbolP _ = Bool False<br>
> > ><br>
> > > This is a pattern that I could easily provide an abstraction for with a<br>
> > Lisp macro, but I'm having trouble discovering if/how it's possible to do so<br>
> > elegantly in Haskell. The closest (but obviously incorrect) code to what I'm<br>
> > trying to accomplish would be:<br>
> ><br>
> > It isn't really possible to abstract this any further in Haskell.<br>
> > Constructors are rather magical functions, but they are still<br>
> > functions, and like other functions cannot be compared for equality<br>
> > directly. Pattern-matching them is the only sort of equality<br>
> > comparison you get.<br>
> ><br>
> > With that said, your intuition to use Lisp macros is a good one.<br>
> > Haskell has a similar metaprogramming facility called Template<br>
> > Haskell, which could easily be used to automatically generate these<br>
> > sorts of functions. Of course, it's a little more complicated than<br>
> > Lisp macros since Haskell syntax is so much more complex than Lisp's<br>
> > -- but given that, on the whole it's not so bad. I wouldn't use TH to<br>
> > generate just the three functions you showed -- but I would certainly<br>
> > consider it for ten.<br>
> ><br>
> > -Brent<br>
> > _______________________________________________<br>
> > Beginners mailing list<br>
> > <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
> > <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
> ><br>
><br>
><br>
><br>
> --<br>
> () ascii ribbon campaign - against html e-mail<br>
> /\ <a href="http://www.asciiribbon.org" target="_blank">www.asciiribbon.org</a> - against proprietary attachments<br>
><br>
> Alex R<br>
<br>
> _______________________________________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br>