Here’s what I’ve got:
{-# LANGUAGE MultiParamTypeClasses
, FlexibleInstances #-}
class ListResultMult r a where
lstM :: a -> [a] -> r
listM :: ListResultMult r a => a -> r
listM a = lstM a []
instance ListResultMult r a => ListResultMult (a -> r) a where
lstM a as x = lstM x $ a:as
instance ListResultMult [a] a where
lstM a as = reverse $ a:as
Here’s how it works:
> listM 'a' 'b' 'c' :: String
"abc"
> putStrLn $ listM 'a' 'b' 'c'
abc
> listM (1::Int) (2::Int) :: [Int]
[1,2]
Here’s how it fails
> sum $ listM 1 2
No instance for (ListResultMult (a2 -> [a0]) a1) ...
> listM 1 :: [Int]
No instance for (ListResultMult [Int] a0) ...
Contrast with printf:
instance Show a => ListResultMult (IO ()) a where
lstM a as = print . reverse $ a:as
> listM "foo" "bar" -- boo
No instance for (ListResult t0 [Char]) ...
> printf "%s %s" "foo" "bar"
foo bar
> listM "foo" "bar" :: IO () -- yay
["foo","bar"]
Type unsafety:
> :t listM 2 "foo"
Some weird type is actually inferred
So here’s what I want to do:
- Type Safety. I thought when I defined
ListResultMult r a => ListResultMult (a -> r) aandListResultMult [a] a, it would only permit you to build up homogeneous lists, and notice a type error when you don’t. Why didn’t it? - Defaulting. I have no clue what weirdness is going on with
listM 1 :: [Int]. What’s up?
Type functions seem like just the ticket for this problem. Here’s a sample file:
Here are your examples in ghci: