我试着遵循"Scrap your boilerpolate" Revolutions纸上的程序。不幸的是,我发现提升脊椎视图部分中的程序不能在我的GHC中编译,有人能指出我哪里错了吗?
{-# LANGUAGE FlexibleContexts, MultiParamTypeClasses,
FlexibleInstances, UndecidableInstances, ScopedTypeVariables,
NoMonomorphismRestriction, DeriveTraversable, DeriveFoldable,
DeriveFunctor, GADTs, KindSignatures, TypeOperators,
TemplateHaskell, BangPatterns
#-}
{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-name-shadowing
-fwarn-monomorphism-restriction -fwarn-hi-shadowing
#-}
module LiftedSpine where
import Test.HUnit
-- Lifted Type View
newtype Id x = InID x
newtype Char' x = InChar' Char
newtype Int' x = InInt' Int
data List' a x = Nil' | Cons' (a x) (List' a x)
data Pair' a b x = InPair' (a x ) (b x)
data Tree' a x = Empty' | Node' (Tree' a x ) (a x) (Tree' a x)
data Type' :: ( * -> * ) -> * where
Id :: Type' Id
Char' :: Type' Char'
Int' :: Type' Int'
List' :: Type' a -> Type' (List' a)
Pair' :: Type' a -> Type' b -> Type' (Pair' a b)
Tree' :: Type' a -> Type' (Tree' a)
infixl 1 :->
data Typed' (f :: * -> *) a = (f a) :-> (Type' f)
size :: forall (f :: * -> *) (a :: *) . Typed' f a -> Int
size (Nil' :-> (List' a' )) = 0
size (Cons' x xs :-> List' a' ) =
size (xs :-> List' a') + size (x :-> a' )发布于 2011-11-22 03:34:38
我们必须翻转(:->)的两个字段,即类型必须是第一个,带注释的术语必须是第二个。这是因为GADT上的模式匹配和精化在GHC中是从左到右的。
发布于 2011-11-21 13:02:36
在GHC 6.12.1上编译这段代码时,我得到的错误是:
Couldn't match expected type `f' against inferred type `List' a'
`f' is a rigid type variable bound by
the type signature for `size' at /tmp/Foo.hs:34:15
In the pattern: Nil'
In the pattern: Nil' :-> (List' a')
In the definition of `size': size (Nil' :-> (List' a')) = 0它似乎无法输入检查Nil'的模式匹配,因为它没有意识到右侧的模式意味着f必须为List'。我怀疑这可能是因为模式匹配是从左到右进行的,因为如果我颠倒Typed'字段的顺序,使List a'在Nil'之前得到匹配,它就可以很好地编译。
https://stackoverflow.com/questions/8207288
复制相似问题