I implemented the converters in Haskell as follows:
{-# LANGUAGE RankNTypes #-} import Prelude hiding (foldr) import Data.Foldable type Reducer ba = a -> b -> b type Transducer ab = forall t. Reducer tb -> Reducer ta class Foldable c => Collection c where insert :: a -> ca -> ca empty :: ca reduce :: Collection c => Transducer ab -> ca -> cb reduce f = foldr (f insert) empty mapping :: (a -> b) -> Transducer ab mapping fgx = g (fx)
Now I want to define a generic map function. Therefore, I load the above code into GHCi:
Prelude> :load Transducer [1 of 1] Compiling Main ( Transducer.hs, interpreted ) Ok, modules loaded: Main. *Main> let map = reduce . mapping <interactive>:3:20: Couldn't match type 'Reducer t0 b1 -> Reducer t0 a1' with 'forall t. Reducer tb -> Reducer t a' Expected type: (a1 -> b1) -> Transducer ab Actual type: (a1 -> b1) -> Reducer t0 b1 -> Reducer t0 a1 Relevant bindings include map :: (a1 -> b1) -> ca -> cb (bound at <interactive>:3:5) In the second argument of '(.)', namely 'mapping' In the expression: reduce . mapping *Main> let map f = reduce (mapping f) *Main> :t map map :: Collection c => (a -> b) -> ca -> cb
Therefore, I cannot define map = reduce . mapping map = reduce . mapping . However, I can define map f = reduce (mapping f) .
I believe that this problem is caused by the restriction of monomorphism. I would really like to write map = reduce . mapping map = reduce . mapping instead of map f = reduce (mapping f) . Therefore, I have two questions:
- What causes this problem? Is this a restriction of monomorphism?
- How to fix this problem?
source share