抬起脊柱视图

时间:2011-11-21 04:30:41

标签: generics haskell gadt

我试图遵循论文"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' )

2 个答案:

答案 0 :(得分: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'之前匹配,它就会编译很好。

答案 1 :(得分:1)

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