Answer the question
In order to leave comments, you need to log in
How to declare a type class with two parameters so that the compiler understands the type of the first from the type of the second?
There is a type class declared like this:
newtype Fix f = Fix { unFix :: f (Fix f) }
class Functor f => Fixable t f where
toFix :: t -> Fix f
fromFix :: Fix f -> t
-- Сам тип Fix
instance Functor f => Fixable (Fix f) f where
toFix = id
fromFix = id
-- И список
data ListF a b = Nil | Cons a b deriving (Eq, Functor)
instance Fixable [a] (ListF a) where
toFix = apo go
where go (x:xs) = Cons x (Right xs)
go _ = Nil
fromFix = cata go
where go (Cons x xs) = x:xs
go _ = []
main :: IO ()
main = putStrLn $ someFunc [\a b -> a ++ b, \a b -> a ++ b ++ a] "str1" "str2"
where someFunc cs = para go cs
String -> String -> String
someFunc :: Fixable t (ListF (String -> String -> String)) -> (String -> String -> String)
someFunc :: [String -> String -> String] -> (String -> String -> String)
Answer the question
In order to leave comments, you need to log in
There are two ways - classical via FunctionalDependencies and neoclassical via TypeFamilies .
Functional dependencies are made specifically for classes with multiple parameters:
{-# LANGUAGE FunctionalDependencies #-}
class Collects e ce | ce -> e where
empty :: ce
insert :: e -> ce -> ce
member :: e -> ce -> Bool
toList :: ce -> [e]
usage :: Collects e ce => ce -> Maybe e
{-# LANGUAGE TypeFamilies #-}
class Collects ce where
type Elem ce
empty :: ce
insert :: Elem ce -> ce -> ce
member :: Elem ce -> ce -> Bool
toList :: ce -> [Elem ce]
usage :: Collects ce => ce -> Maybe (Elem ce)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question