首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提升的脊椎视图

提升的脊椎视图
EN

Stack Overflow用户
提问于 2011-11-21 12:30:41
回答 2查看 162关注 0票数 1

我试着遵循"Scrap your boilerpolate" Revolutions纸上的程序。不幸的是,我发现提升脊椎视图部分中的程序不能在我的GHC中编译,有人能指出我哪里错了吗?

代码语言:javascript
复制
{-# 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' )
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-22 03:34:38

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

票数 1
EN

Stack Overflow用户

发布于 2011-11-21 13:02:36

在GHC 6.12.1上编译这段代码时,我得到的错误是:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8207288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档