val fibonacci_greater_than_arg (n:nat{n >= 2}) : Lemma (fibonacci n >= n) letrec fibonacci_greater_than_arg n = if n = 2then() else fibonacci_greater_than_arg (n - 1)
证明一个用于合并列表的函数的返回值长度为参数之和。
对列表进行递归即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
letrec app #a (l1 l2:list a) : list a = match l1 with | [] -> l2 | hd :: tl -> hd :: app tl l2
letrec length #a (l:list a) : nat = match l with | [] -> 0 | _ :: tl -> 1 + length tl
val app_length (#a:Type) (l1 l2:list a) : Lemma (length (app l1 l2) = length l1 + length l2) letrec app_length l1 l2 = match l1 with | [] -> () | _ :: tl -> app_length tl l2
val rev_is_ok (#a:_) (l:list a) : Lemma (rev l == reverse l) letrec rev_is_ok l = match l with | [] -> () | hd :: tl -> rev_step_snoc [] tl hd; rev_is_ok tl
letrec fib_aux (a b n:nat) : Tot nat (decreases n) = match n with | 0 -> a | _ -> fib_aux b (a+b) (n-1)
let fib (n:nat) : nat = fib_aux 11 n
letrec fib_step_aux (a b:nat) (n:nat{n>1}) : Lemma (ensures fib_aux a b (n - 2) + fib_aux a b (n - 1) == fib_aux a b n) (decreases n) = match n with | 2 -> () | _ -> fib_step_aux b (a + b) (n - 1)
letrec fib_is_ok (n:nat) : Lemma (fibonacci n == fib n) = match n with | 0 -> () | 1 -> () | _ -> fib_step_aux 11 n; fib_is_ok (n - 1); fib_is_ok (n - 2)
为查找函数找到隐式提纯类别和显式性质证明。
隐式需要使用到 o:option,在前文没看到,我直接看了答案。
1 2 3 4 5 6 7
//write a typefor find val find (#a:Type) (f:a -> bool) (l:list a) : o:option a{Some? o ==> f (Some?.v o)} letrec find f l = match l with | [] -> None | hd :: tl -> if f hd thenSome hd else find f tl
显式的默认在记事本里面已经给出了,注意 Lemma 里面也可以 match 即可,
剩下的是简单的递归。
1 2 3 4 5 6 7 8 9 10 11 12
letrec find_alt f l = match l with | [] -> None | hd :: tl -> if f hd thenSome hd else find_alt f tl
letrec find_alt_ok #a (f:a -> bool) (l:list a) : Lemma (match find_alt f l with | Some x -> f x | _ -> true) = match l with | [] -> () | _ :: tl -> find_alt_ok f tl