Problem with constructing instances for a class Pair
i want the operations on pairs to work for lists applying to each pair in the list. i prefer to achieve this with overloading, using classes and instances i cannot get from the code for the ordinary case: class P a where outl :: a b c -> b ... data P1 p q = P1 p q deriving (Show, Eq, Ord) -- a pair like constructor instance P P1 where outl (P1 p q) = p to code for the mapped case: instance P [P1] where .... gives -- - Kind error: `P1' is not applied to enough type arguments -- - In the instance declaration for `P [P1]' no other combination of parameters i can think of works for an instance of class P for lists. what do i not see? any help appreciated! andrew frank the remainder is the code i have tried: -} module PairTest where class P a where outl :: a b c -> b outr :: a b c -> c make :: b -> c -> a b c data P1 p q = P1 p q deriving (Show, Eq, Ord) type P2 = (,) instance P P2 where outl = fst outr = snd make = (,) instance P P1 where outl (P1 p q) = p outr (P1 p q) = q make = P1 p1list :: [P1 Int Bool] p1list = [P1 3 True, P1 4 False] qlist = fmap outr p1list -- or map --------------can this be done as overloading of outl, outr for lists? outlPl = fmap outl plist = outlPl p1list --instance P [P1] where -- outl = fmap outl -- - Kind error: `P1' is not applied to enough type arguments -- - In the instance declaration for `P [P1]' --instance P [P1 p q] where -- outl = fmap outl -- Kind error: Expecting kind `* -> * -> *', but `[P1 p q]' has kind `*' -- In the instance declaration for `P [P1 p q]' class P3 a b c where outl3 :: a b c -> b --instance P3 [P1 p q] -- - Kind error: Expecting kind `* -> * -> *', but `[P1 p q]' has kind `*' -- - In the instance declaration for `P3 [P1 p q]' Andrew U. Frank Professor, Head of Department Geoinformation and Cartography E127 phone: +43 1 588 01 12710 TU Vienna secr. +43 1 588 01 12700 Gusshausstrasse 27-29 fax +43 1 588 01 12799 A-1040 Vienna Austria cellular phone +43 676 41925 72 http://www.geoinfo.tuwien.ac.at/persons/frank/frank.html skype:AndrewUFrank
Hello Frank, Saturday, December 24, 2005, 12:02:28 AM, you wrote: F> class P a where F> outl :: a b c -> b F> outr :: a b c -> c F> make :: b -> c -> a b c reusing my answer to David Roundy :))) are you seen in Hugs manual chapter about multi-parameter type classes? they use the following class Collects e ce | ce -> e where empty :: ce insert :: e -> ce -> ce member :: e -> ce -> Bool instance Eq e => Collects e [e] where ... instance Eq e => Collects e (e -> Bool) where ... instance Collects Char BitSet where ... instance (Hashable e, Collects a ce) => Collects e (Array Int ce) where ... may be you can solve your problem by using {-# OPTIONS_GHC -fglasgow-exts #-} class P b c abc | abc -> b, abc -> b where outl :: abc -> b outr :: abc -> c make :: b -> c -> abc instance P b c (P1 b c) where outl (P1 p q) = p outr (P1 p q) = q make = P1 instance P b c (P2 b c) where outl = fst outr = snd make = (,) data P1 p q = P1 p q deriving (Show, Eq, Ord) type P2 = (,) instance (P b c abc) => P [b] [c] [abc] where outl = undefined outr = undefined make = zipWith make although it is a bit too verbose... and have problems with defining outl/outr F> Andrew U. Frank F> Professor, Head of Department i helped Professor? wow! :) -- Best regards, Bulat mailto:bulatz@HotPOP.com
participants (2)
-
Bulat Ziganshin -
Frank