建立有界自然与满足边界的自然之间的同构?

时间:2016-03-22 00:21:56

标签: functional-programming numbers dependent-type idris isomorphism

在Idris中,您能否在 if(answer.equals("deposit")){ System.out.println("How much do you want to deposit?"); double money = Double.parseDouble(in.nextLine()); deposit(money); } Fin n之间建立同构? (我实际上并不知道Idris,所以这些类型可能无效。一般的想法是我们的​​数据类型通过构造保证小于(x ** So (x < n)),另一个保证少于n通过测试比n。)

1 个答案:

答案 0 :(得分:2)

以下是Idris 0.10.2中的证据正如您所见,roundtrip2是唯一棘手的证明。

import Data.Fin
%default total

Bound : Nat -> Type
Bound n = DPair Nat (\x => x `LT` n)

bZ : Bound (S n)
bZ = (0 ** LTESucc LTEZero)

bS : Bound n -> Bound (S n)
bS (x ** bound) = (S x ** LTESucc bound)

fromFin : Fin n -> Bound n
fromFin FZ = bZ
fromFin (FS k) = bS (fromFin k)

toFin : Bound n -> Fin n
toFin (Z ** LTEZero) impossible
toFin {n = S n} (Z ** bound) = FZ
toFin (S x ** LTESucc bound) = FS (toFin (x ** bound))

roundtrip1 : {n : Nat} -> (k : Bound n) -> fromFin (toFin k) = k
roundtrip1 (Z ** LTEZero) impossible
roundtrip1 {n = S n} (Z ** LTESucc LTEZero) = Refl
roundtrip1 (S x ** LTESucc bound) = rewrite (roundtrip1 (x ** bound)) in Refl

roundtrip2 : {n : Nat} -> (k : Fin n) -> toFin (fromFin k) = k
roundtrip2 FZ = Refl
roundtrip2 (FS k) = rewrite (lemma (fromFin k)) in cong {f = FS} (roundtrip2 k)
  where
    lemma : {n : Nat} -> (k : Bound n) -> toFin (bS k) = FS (toFin k)
    lemma (x ** pf) = Refl

如果你拥有的是非命题So (x < n)而不是x `LT` n,那么你需要将其转换为证明表格。我能够做到这一点:

import Data.So

%default total

stepBack : So (S x < S y) -> So (x < y)
stepBack {x = x} {y = y} so with (compare x y)
  | LT = so
  | EQ = so
  | GT = so

correct : So (x < y) -> x `LT` y
correct {x = Z}   {y = Z}     Oh impossible
correct {x = S _} {y = Z}     Oh impossible
correct {x = Z}   {y = S _} so = LTESucc LTEZero
correct {x = S x} {y = S y} so = LTESucc $ correct $ stepBack so