I'd like to be able to define something like instance Eq a => Coll (-> Bool) a where empty = \_ -> False single x = \y -> if x == y then True else False union a b = \x -> a x || b x insert s x = \y -> x == y || s y and the like However, this seems to be impossible. Is this the type lambda restriction that's been discussed recently on the mailing list? - Hal -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume
Hal Daume III <hdaume@ISI.EDU> writes:
I'd like to be able to define something like
instance Eq a => Coll (-> Bool) a where empty = \_ -> False single x = \y -> if x == y then True else False union a b = \x -> a x || b x insert s x = \y -> x == y || s y
I don't know what Coll is, but the following is working: class Collection e ce | ce -> e where empty :: ce insert :: e -> ce -> ce member :: e -> ce -> Bool instance Eq a => Collection a (a -> Bool) where empty = (\x -> False) insert e f = (\x -> if x == e then True else f x) member e f = f e
class Collection e ce | ce -> e where empty :: ce insert :: e -> ce -> ce member :: e -> ce -> Bool
instance Eq a => Collection a (a -> Bool) where empty = (\x -> False) insert e f = (\x -> if x == e then True else f x) member e f = f e
This is way better than my solution... I had never used multi-parameter classes before, so I forgot the functional dependency (right name? the "|ce->e"), and there was obviously no need for my extra constructor. J.A.
Yeah, both options suggested are valid, of course. But I really don't want to have a constructor and I'm using Edison where Coll is defined something like: class Coll c e where empty :: c e insert :: c e -> e -> c e etc., which precludes the fun dep solution. - Hal -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume On Tue, 23 Apr 2002, Jorge Adriano wrote:
class Collection e ce | ce -> e where empty :: ce insert :: e -> ce -> ce member :: e -> ce -> Bool
instance Eq a => Collection a (a -> Bool) where empty = (\x -> False) insert e f = (\x -> if x == e then True else f x) member e f = f e
This is way better than my solution...
I had never used multi-parameter classes before, so I forgot the functional dependency (right name? the "|ce->e"), and there was obviously no need for my extra constructor.
J.A.
On Monday 22 April 2002 23:31, Hal Daume III wrote:
I'd like to be able to define something like
instance Eq a => Coll (-> Bool) a where empty = \_ -> False single x = \y -> if x == y then True else False union a b = \x -> a x || b x insert s x = \y -> x == y || s y
and the like
However, this seems to be impossible. Is this the type lambda restriction that's been discussed recently on the mailing list?
- Hal
Hi Hal, I'd do it like this, hope it helps. ---------------------------------- module Test where newtype BinClass a = BC (a->Bool) class Coll c a where empty :: c a single :: a->c a union :: c a->c a->c a insert :: c a->a->c a instance Eq a => Coll BinClass a where empty = BC(\_->True) single x = BC(\y -> if x == y then True else False) union (BC a) (BC b) = BC(\x -> a x || b x) insert (BC s) x = BC(\y -> x == y || s y)
Hal Daume III wrote:
I'd like to be able to define something like
single x = \y -> if x == y then True else False
Just a note on style: it always hurts me to see something like if term then True else False -- this is just the same as 'term'. So you could say single x = \y -> x==y which is in turn just the same as single x = (x==) which is, amazingly, nothing more than single = (==) -- one can debatte whether this is still better style than the first variant, but it's surely interesting to realize. All the best, Christian Sievers
Yeah, I realized that right after I sent the email (I was composing "on the fly" and not copy-and-pasting). I think the main reason I wrote it as an explicit lambda expression rather than just single = (==) was because I wanted it to parallel the other definitions. IMO, the preferred way to write it would have been "single x = \y -> x == y; I think "single = (==)" is much more difficult to understand at first glance. Doesn't really matter though; presumably the compiler would fix all of this :). -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume On Tue, 23 Apr 2002, Christian Sievers wrote:
Hal Daume III wrote:
I'd like to be able to define something like
single x = \y -> if x == y then True else False
Just a note on style: it always hurts me to see something like
if term then True else False
-- this is just the same as 'term'.
So you could say
single x = \y -> x==y
which is in turn just the same as
single x = (x==)
which is, amazingly, nothing more than
single = (==)
-- one can debatte whether this is still better style than the first variant, but it's surely interesting to realize.
All the best, Christian Sievers
participants (4)
-
Christian Sievers -
Hal Daume III -
Jorge Adriano -
Pixel