Oto kod:Korzystanie z jednowartościowy szeregowych typu 2
{-# 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))
Wynika to z błędów:
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
Jednak nie jestem pewien, jak to naprawić lub zdiagnozować problem. Może to być tak proste, jak dobrze (i dobrze napisany) podpis typu.
Starając się zrozumieć, co się dzieje, piszę wersję non-jednowartościowy (bezużyteczny dla mnie), ale kompiluje:
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)
To czyni mnie rzeczą błąd powyżej jest związane z this question tam, gdzie nie ma miejsca na słownik, ale to po prostu ukłucie w ciemności.
Wygląda na to, że mogłeś zrobić podwójny wpis. Prawdopodobnie powinno to zostać usunięte i odpowiedzi można znaleźć tutaj: http://stackoverflow.com/questions/24744294/pattern-matching-on-rank-2-type – jberryman
@jberryman Nie jest dla mnie oczywiste, że problemy w tych dwóch kwestiach są powiązane (choć niektóre podzestawy kodu są w rzeczywistości takie same). Dlatego wysłałem jako dwa pytania. – crockeea
Użyj zmiennych typu scoped, aby ustalić typ pierwszego argumentu. Spróbuj także wypróbować opcję 'forall' poza zasięgiem globalnym w typie dla' runFun'. – nomen