假设我们正在编写lambda演算的实现,作为其中的一部分,我们希望能够选择一个新的非冲突名称:
record Ctx where
constructor MkCtx
bindings : List String
emptyCtx : Ctx
emptyCtx = MkCtx []
addCtx : String -> Ctx -> Ctx
addCtx name = record { bindings $= (name ::) }
pickName : String -> Ctx -> (String, Ctx)
pickName = go Z
where
mkName : Nat -> String -> String
mkName Z name = name
mkName n name = name ++ show n
go n name ctx = let name' = mkName n name in
if name' `elem` bindings ctx
then go (S n) name ctx
else (name', addCtx name' ctx)
由于pickName
中的递归路径,Idris总体检查员认为go
不是完全的,并且理所当然:事实上,总体证明并不依赖于语法上更小的任何术语,而是依赖于如果bindings
具有k
元素的观察,那么它只需要k + 1
递归调用就可以找到一个新名字。但是如何在代码中表达这一点呢?
在首先编写函数然后编写(类型检查,但从不执行)证明它具有正确属性的意义上,我也倾向于外部验证。在这种情况下,pickName
的整体性是否可能?
受到@HTNW的启发,看起来正确的方法就是使用Vect
而不是列表。从向量中移除元素将使其大小(以类型表示)在语法上更小,从而无需自己证明它。所以,pickName
的(略有重构)版本将是
pickName : String -> Vect n String -> String
pickName name vect = go Z vect
where
mkName : Nat -> String
mkName Z = name
mkName n = name ++ show n
go : Nat -> Vect k String -> String
go {k = Z} n _ = mkName n
go {k = (S k)} n vect' =
let name' = mkName n in
case name' `isElem` vect' of
Yes prf => go (S n) $ dropElem vect' prf
No _ => name'
在序曲中,我们有:
Smaller x y = size x `LT` size y
instance Sized (List a) where size = length
sizeAccessible : Sized a => (x : a) -> Accessible Smaller x
accRec : (step : (x : a) -> ((y : a) -> rel y x -> b) -> b) ->
(z : a) -> Accessible rel z -> b
accRec
允许您以编译器可以理解为total
的方式使用“非标准递归模式”。它基本上是fix : ((a -> b) -> (a -> b)) -> (a -> b)
,除了open-recursive函数有义务传递一个额外的证明项来证明递归参数以某种方式“更小”。 Accessible
参数决定了所使用的递归模式;这里是简单的“减少Nat
大小”模式。最好是,我们使用sizeRec
而不是accRec
+ sizeAccessible
,但我不能使它工作。随意以“正确”的方式编辑它。
在函数的每次迭代中,如果找到它,都可以删除该名称。
delFirst : DecEq a => (x : a) -> (xs : List a)
-> Maybe (ys : List a ** length xs = S (length ys))
delFirst _ [] = Nothing
delFirst x (y :: xs) with (decEq x y)
delFirst x (x :: xs) | Yes Refl = Just (xs ** Refl)
delFirst x (y :: xs) | No _ with (delFirst x xs)
| Nothing = Nothing
| Just (ys ** prf) = Just (x :: ys ** cong prf)
现在,您可以在pickName
中使用开放,有根据的递归:
pickName : String -> Ctx -> (String, Ctx)
pickName s ctx = let new = go s (bindings ctx) Z
in (new, addCtx new ctx)
where mkName : Nat -> String -> String
mkName Z name = name
mkName n name = name ++ show n
ltFromRefl : n = S m -> LT m n
ltFromRefl Refl = lteRefl
go : String -> List String -> Nat -> String
go name binds = accRec (\binds, rec, n =>
let name' = mkName n name
in case delFirst name' binds of
Nothing => name'
Just (binds' ** prf) => rec binds' (ltFromRefl prf) (S n)
) binds (sizeAccessible binds)
Nat -> a
和Stream a
是一样的,所以IMO,这有点好看:
findNew : DecEq a => (olds : List a) -> (news : Stream a) -> a
findNew olds = accRec (\olds, rec, (new :: news) =>
case delFirst new olds of
Nothing => new
Just (olds' ** prf) => rec olds' (ltFromRefl prf) news
) olds (sizeAccessible olds)
where ltFromRefl : n = S m -> LT m n
ltFromRefl Refl = lteRefl
pickName : String -> Ctx -> (String, Ctx)
pickName name ctx = let new = findNew (bindings ctx)
(name :: map ((name ++) . show) (iterate S 1))
in (new, addCtx new ctx)
我认为,它抓住了这个想法背后的直觉,如果你有无限的名字,但只有有限的许多旧的,你肯定有无数的新的。
(此外,代码中的逻辑似乎是错误的。你是否翻过了if
的分支?)