Say I have a tuple like ('a',(1,("Hello",False)). Just for fun (reading: learning), I would like to create a function that applies some function of the correct form to any such tuple and returns the result. Usage example:
applyFnToTuple ('o',('t','w')) $ \a b c -> [a,b,c] == "otw"
applyFnToTuple ('h','i') $ \a b -> [a,b] == "hi"
applyFnToTuple ("hello",('y','o')) $ \a b c -> a ++ [b,c]
I made most of them as follows:
type family TupleFn ty out where
TupleFn (a,b) output = a -> (TupleFn b output)
TupleFn b output = b -> output
class ApplyFnToTuple a where
applyFnToTuple :: a -> TupleFn a out -> out
instance ApplyFnToTuple b => ApplyFnToTuple (a,b) where
applyFnToTuple (a,b) fn = applyFnToTuple b (fn a)
instance ApplyFnToTuple a where
applyFnToTuple b fn = fn b
The sticking point is the last instance. I fully expect to add {-# OVERLAPPABLE #-}as amore general than (a,b). I am also trying to understand how GHC can resolve the acorrect version of my class TupleFnand know the correct sig type, but I can easily add this to my own lack of understanding. But anyway, the actual GHCI error gives me:
Couldn't match expected type ‘a -> out’
with actual type ‘TupleFn a out’
Relevant bindings include
fn :: TupleFn a out (bound at examples.hs:574:22)
b :: a (bound at examples.hs:574:20)
applyFnToTuple :: a -> TupleFn a out -> out
(bound at examples.hs:574:5)
The function ‘fn’ is applied to one argument,
but its type ‘TupleFn a out’ has none
In the expression: fn b
In an equation for ‘applyFnToTuple’: applyFnToTuple b fn = fn b
Failed, modules loaded: none.
, TupleFn , . , , , , :
instance ApplyFnToTuple Char where
applyFnToTuple b fn = fn b
, .., .
, ?
Thankyou:)
PS: GHC 7.10.1