Here's the code:
{-# LANGUAGE RankNTypes, FlexibleContexts, ScopedTypeVariables #-}
module Foo where
import Data.Vector.Generic.Mutable as M
import Data.Vector.Generic as V
import Control.Monad.ST
import Control.Monad.Primitive
import Control.Monad
data DimFun v s r =
DimFun {dim::Int, func :: v (PrimState s) r -> s ()}
runFun :: (Vector v r) =>
(forall s . (PrimMonad s) => DimFun (Mutable v) s r) -> v r -> v r
runFun t x = runST $ do
y <- thaw x
evalFun t y
unsafeFreeze y
evalFun :: (PrimMonad s, MVector v r) => DimFun v s r -> v (PrimState s) r -> s ()
evalFun (DimFun dim f) y | dim == M.length y = f y
fm :: (MVector v r, PrimMonad s, Num r, Monad m) => m (DimFun v s r)
fm = error ""
f :: forall v r m . (Vector v r, Num r, Monad m) => m (v r -> v r)
f = liftM runFun $ (fm :: forall s . (PrimMonad s) => m (DimFun (Mutable v) s r))
This results in errors:
Couldn't match type ‘DimFun (Mutable v) s0 r’
with ‘forall (s :: * -> *). PrimMonad s => DimFun (Mutable v) s r’
Expected type: DimFun (Mutable v) s0 r -> v r -> v r
Actual type: (forall (s :: * -> *).
PrimMonad s =>
DimFun (Mutable v) s r)
-> v r -> v r
Relevant bindings include
f :: m (v r -> v r) (bound at Testing/Foo.hs:36:1)
In the first argument of ‘liftM’, namely ‘runFun’
In the expression: liftM runFun
However, I'm not sure how to fix or diagnose the problem. It might be as simple as a well-place (and well-written) type signature.
While trying to figure out what was going on, I write a non-monadic version (useless to me), but it compiles:
gm :: (MVector v r, PrimMonad s, Num r) => DimFun v s r
gm = error ""
g :: forall v r m . (Vector v r, Num r) => v r -> v r
g = runFun (gm :: forall s . (PrimMonad s) => DimFun (Mutable v) s r)
This makes me thing the error above is related to the this question where there is no place for the dictionary to go, but that's really just a stab in the dark.