1

私は論文「Scrapyourboiloperpolate」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' )
4

2 に答える 2

1

GHC6.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'

于 2011-11-21T05:02:36.093 に答える
1

(:->)の2つのフィールドを反転する必要があります。つまり、タイプが最初で、注釈付きの用語が2番目である必要があります。これは、GADTのパターンマッチングとリファインメントがGHCでは暗黙的に左から右に行われるためです。

于 2011-11-21T19:34:38.393 に答える