抬起脊柱视图

发布于 2024-12-17 01:55:13 字数 1406 浏览 3 评论 0原文

我尝试遵循“废弃你的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟织青萝梦 2024-12-24 01:55:13

在 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' 的模式匹配,因为它没有意识到右侧模式意味着 < code>f 必须是 List'。我怀疑这可能是因为模式匹配是从左到右完成的,因为如果我翻转 Typed' 字段的顺序,那么 List a' 就会匹配在Nil'之前,它编译得很好。

The error I get when compiling this on GHC 6.12.1 is:

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

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 that f must be List'. I suspect this may be because pattern matching is done left-to-right, because if I flip the order of the fields of Typed', so that List a' gets matched before Nil', it compiles just fine.

情域 2024-12-24 01:55:13

我们必须翻转 (:->) 的两个字段,即类型必须是
第一个,带注释的术语必须是第二个。这是因为模式
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文