2011-11-21 38 views
1

我试图按照纸张"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') 

回答

1

我们必须翻转(: - >)的两个字段,即首先必须是 ,注释的词必须是第二个。这是因为在GADT和匹配上匹配的模式 隐含在GHC中从左到右。

1

编制上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'之前获得匹配,它编译得很好。