Mic*_*mas 3 gadt higher-rank-types purescript
我一直在尝试使用rank-2类型在PureScript中编码GADT,如此处针对Haskell所述
我的代码看起来像:
data Z
data S n
data List a n
= Nil (Z -> n)
| Cons forall m. a (List a m) (S m -> n)
fw :: forall f a. (Functor f) => (forall b . (a -> b) -> f b) -> f a
fw f = f id
bw :: forall f a. (Functor f) => f a -> (forall b . (a -> b) -> f b)
bw x f = map f x
nil :: forall a. List a Z
nil = fw Nil
cons :: forall a n. a -> List a (S n)
cons a as = fw (Cons a as)
instance listFunctor :: Functor (List a) where
map f (Nil k) = Nil (f <<< k)
map f (Cons x xs k) = Cons x xs (f <<< k)
Run Code Online (Sandbox Code Playgroud)
编译器抱怨Wrong number of arguments to constructor Main.Cons,引用Functor实例中的LHS模式匹配.
这里出了什么问题?
问候,
迈克尔
用于Haskell中存在类型的语法在PureScript中不存在.您所写的Cons是具有单个通用量化参数的数据构造函数.
您可能希望尝试使用purescript-exists编码存在类型.
另一种选择是使用GADT的最终无标记编码:
class Listy l where
nil :: forall a. l Z a
cons :: forall a n. a -> l n a -> l (S n) a
Run Code Online (Sandbox Code Playgroud)
您可以为任何有效Listy实例编写术语:
myList :: forall l. (Listy l) => l (S (S Z)) Int
myList = cons 1 (cons 2 nil)
Run Code Online (Sandbox Code Playgroud)
并通过编写实例来解释它们
newtype Length n a = Length Int
instance lengthListy :: Listy Length where
nil = Length 0
cons _ (Length n) = Length (n + 1)
newtype AsList n a = AsList (List a)
instance asListListy :: Listy AsList where
nil = AsList Nil
cons x (AsList xs) = AsList (Cons x xs)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
450 次 |
| 最近记录: |