抬起脊柱视图
我尝试遵循“废弃你的boilerpolate”革命论文的计划。 不幸的是,我发现提升脊柱视图部分中的程序无法在我的 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' )
I tried to follow the program of the paper "Scrap your boilerpolate" Revolutions.
Unfortunately, I found the program in the section lifted spine view does not compile in my GHC,
could anybody point out where I am wrong?
{-# 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' )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 GHC 6.12.1 上编译此代码时出现的错误是:
它似乎无法类型检查
Nil'
的模式匹配,因为它没有意识到右侧模式意味着 < code>f 必须是List'
。我怀疑这可能是因为模式匹配是从左到右完成的,因为如果我翻转Typed'
字段的顺序,那么List a'
就会匹配在Nil'
之前,它编译得很好。The error I get when compiling this on GHC 6.12.1 is:
It seems like it fails to type check the pattern match of
Nil'
because it hasn't realized that the right hand side pattern means thatf
must beList'
. I suspect this may be because pattern matching is done left-to-right, because if I flip the order of the fields ofTyped'
, so thatList a'
gets matched beforeNil'
, it compiles just fine.我们必须翻转 (:->) 的两个字段,即类型必须是
第一个,带注释的术语必须是第二个。这是因为模式
GADT 的匹配和细化在 GHC 中隐式从左到右进行。
we have to flip the two fields of (:->), i.e., the type has to be
first and the annotated term has to be second. This is because pattern
matching on GADTs and refinement is implicitly left-to-right in GHC.