依赖类型的教堂编码:从Coq到Haskell

时间:2019-03-09 19:43:49

标签: haskell coq church-encoding

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

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的类型系统(包括其扩展名)是否足够强大,可以容纳此类定义?如果是,怎么办?

1 个答案:

答案 0 :(得分:4)

确定是

caches

使用常规GADT公式进一步证明(对于怀疑论者)同构:

{-# 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)