Guest User

Untitled

a guest
Nov 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. module Demo
  2.  
  3. open FStar.Exn
  4. open FStar.All
  5. open FStar.Mul
  6.  
  7. type filename = string
  8.  
  9. (** [canWrite] is a function specifying whether a file [f] can be written *)
  10. let canWrite (f:filename) =
  11. match f with
  12. | "demo/tempfile" -> true
  13. | _ -> false
  14.  
  15. (** [canRead] is also a function ... *)
  16. let canRead (f:filename) =
  17. canWrite f (* writeable files are also readable *)
  18. || f="demo/README" (* and so is demo/README *)
  19.  
  20. val read : f:filename{canRead f} -> ML string
  21. let read f = FStar.IO.print_string ("Dummy read of file " ^ f ^ "\n"); f
  22.  
  23. val write : f:filename{canWrite f} -> string -> ML unit
  24. let write f s = FStar.IO.print_string ("Dummy write of string " ^ s ^ " to file " ^ f ^ "\n")
  25.  
  26. exception InvalidRead
  27. val checkedRead : filename -> ML string
  28. let checkedRead f =
  29. if canRead f then read f else raise InvalidRead
  30.  
  31. let passwd : filename = "demo/password"
  32. let readme : filename = "demo/README"
  33. let tmp : filename = "demo/tempfile"
  34.  
  35. (*
  36. val staticChecking : unit -> ML unit
  37. let staticChecking () =
  38. let v1 = read tmp in
  39. let v2 = read readme in
  40. let v3 = if false then read tmp else read passwd in // invalid read, fails type-checking
  41. write tmp "hello!"
  42. *)
  43.  
  44. (* ; write passwd "junk" // invalid write , fails type-checking *)
  45.  
  46. let max x y = if x > y then x else y
  47.  
  48. let _ = assert (
  49. forall x y.
  50. max x y >= x &&
  51. max x y >= y &&
  52. (max x y = x) || (max x y = y)
  53. )
  54.  
  55. val factorial: x:int{x >= 0} -> Tot int
  56. let rec factorial n =
  57. if n = 0 then 1 else n * (factorial (n - 1))
Add Comment
Please, Sign In to add comment