Advertisement
Guest User

F* tutorial : Exercise 5a

a guest
Feb 28th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.34 KB | None | 0 0
  1. module RevIsOk
  2.  
  3. val append : list 'a -> list 'a -> Tot (list 'a)
  4. let rec append l1 l2 = match l1 with
  5.  | [] -> l2
  6.  | hd :: tl -> hd :: append tl l2
  7.  
  8. val reverse : list 'a -> Tot (list 'a)
  9. let rec reverse l =
  10.  match l with
  11.  | [] -> []
  12.  | hd::tl -> append (reverse tl) [hd]
  13.  
  14. val rev : l1:list 'a -> l2:list 'a -> Tot (list 'a) (decreases l2)
  15. let rec rev l1 l2 =
  16.   match l2 with
  17.   | []     -> l1
  18.   | hd::tl -> rev (hd::l1) tl
  19.  
  20. val append_assoc : xs:list 'a -> ys:list 'a -> zs:list 'a -> Lemma
  21.      (ensures (append (append xs ys) zs = append xs (append ys zs)))
  22. let rec append_assoc xs ys zs =
  23.  match xs with
  24.  | [] -> ()
  25.  | x::xs' -> append_assoc xs' ys zs
  26.  
  27. val rev_is_ok_aux : l1:list 'a -> l2:list 'a -> Lemma
  28.      (ensures (rev l1 l2 = append (reverse l2) l1)) (decreases l2)
  29. let rec rev_is_ok_aux l1 l2 =
  30.  match l2 with
  31.  | [] -> ()
  32.  | hd::tl  -> rev_is_ok_aux (hd::l1) tl; append_assoc (reverse tl) [hd] l1
  33. (*
  34. * To prove "rev l1 (hd::tl) = append (reverse (hd::tl)) l1"
  35. *
  36. * rev l1 (hd::tl)
  37.  * = rev (hd::l1) tl                (rev の定義)
  38.  * = append (reverse tl) (hd::l1)       (帰納法の仮定 : rev_is_ok_aux (hd::l1) tl)
  39.  * = append (reverse tl) (append [hd] l1)   (append の定義)
  40.  * = append (append (reverse tl) [hd]) l1   (append の結合性 : append_assoc)
  41.  * = append (reverse (hd::tl)) l1       (reverse の定義)
  42. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement