4

在 Coq 中,我可以为长度为 n 的列表定义 Church 编码:

Definition listn (A : Type) : nat -> Type :=
fun m => forall (X : nat -> Type), X 0 -> (forall m, A -> X m -> X (S m)) -> X m.

Definition niln (A : Type) : listn A 0 :=
fun X n c => n.

Definition consn (A : Type) (m : nat) (a : A) (l : listn A m) : listn A (S m) :=
fun X n c => c m a (l X n c).

Haskell 的类型系统(包括它的扩展)是否足够强大以适应这样的定义?如果是,如何?

4

1 回答 1

4

是的:

{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}

import Data.Kind        -- Needed for `Type`

data Nat = Z | S Nat    -- Roll your own...

type List (a :: Type) (n :: Nat) =
  forall (x :: Nat -> Type). x Z -> (forall (m :: Nat). a -> x m -> x (S m)) -> x n

niln :: List a Z
niln = \z _ -> z

consn :: a -> List a n -> List a (S n)
consn a l = \n c -> c a (l n c)

与通常的 GADT 公式的同构的进一步证明(对于怀疑论者):

data List' (a :: Type) (n :: Nat) where
  Nil :: List' a Z
  Cons :: a -> List' a m -> List' a (S m)

to :: List' a n -> List a n
to Nil = niln
to (Cons a l) = consn a (to l)

from :: List a n -> List' a n
from l = l Nil Cons
于 2019-03-09T20:25:16.117 回答