1 Jun 2006 02:22
Tips for converting Prolog to typeclasses?
Greg Buchholz <haskell <at> sleepingsquirrel.org>
2006-06-01 00:22:33 GMT
2006-06-01 00:22:33 GMT
Lately, in my quest to get a better understanding of the typeclass
system, I've been writing my typeclass instance declarations in Prolog
first, then when I've debugged them, I port them over back over to
Haskell. The porting process involves a lot trial and error on my part
trying to decide when to use functional dependencies and which compiler
extension to enable ( -fallow-undecidable-instances,
-fallow-overlapping-instances, etc.). Which might be okay, but I still
can produce things that won't compile, and I don't necessarily know if
I'm making a fundamental mistake in a program, or if there's something
trivial that I'm not doing quite right.
For example, there was a question on haskell-cafe last week about
creating an "apply" function. My first solution (
http://www.haskell.org//pipermail/haskell-cafe/2006-May/015905.html )
was to use type classes and nested tuples for the collection of
arguments. This works fine. But then I wanted to try to get closer to
what the original poster wanted, namely to use regular homogenous lists
to store the arguments. So I thought I could reuse the class definition
and just provide new instances for a list type, instead of the nested
tuple type. Here's the class definition...
> class Apply a b c | a b -> c where
> apply :: a -> b -> c
...So I wrote the following Prolog snippets which seemed like they might
properly describe the situation I was looking for...
:- op(1000,xfy,=>). % use => instead of -> for arrow type
app(A=>B,[A],C) :- app(B,[A],C).
app(C,[A],C).
(Continue reading)
RSS Feed