我正在关注F * https://www.fstar-lang.org/tutorial/的教程,并在ubuntu上使用fstar-mode https://github.com/FStarLang/fstar-mode.el设置了Emacs。我想我不明白如何正确地对文件进行类型检查,因为当我从教程中使用两种不同的方法对此文件进行检查时:
module Ex01a
open FStar.Exn
open FStar.All
//safe-read-write
type filename = string
(** [canWrite] is a function specifying whether a file [f] can be written *)
let canWrite (f:filename) =
match f with
| "demo/tempfile" -> true
| _ -> false
(** [canRead] is also a function ... *)
let canRead (f:filename) =
canWrite f (* writeable files are also readable *)
|| f="demo/README" (* and so is demo/README *)
val read : f:filename{canRead f} -> ML string
let read f = FStar.IO.print_string ("Dummy read of file " ^ f ^ "\n"); f
val write : f:filename{canWrite f} -> string -> ML unit
let write f s = FStar.IO.print_string ("Dummy write of string " ^ s ^ " to file " ^ f ^ "\n")
let passwd : filename = "demo/password"
let readme : filename = "demo/README"
let tmp : filename = "demo/tempfile"
val staticChecking : unit -> ML unit
let staticChecking () =
let v1 = read tmp in
let v2 = read readme in
let v3 = read passwd in
write tmp "hello!"
(* ; write passwd "junk" // invalid write , fails type-checking *)
exception InvalidRead
val checkedRead : filename -> ML string
let checkedRead f =
if canRead f then read f else raise InvalidRead
assume val checkedWrite : filename -> string -> ML unit
let dynamicChecking () =
let v1 = checkedRead tmp in
let v2 = checkedRead readme in
let v3 = checkedRead passwd in (* this raises exception *)
checkedWrite tmp "hello!";
checkedWrite passwd "junk" (* this raises exception *)
let main = staticChecking (); dynamicChecking ()
然后根据使用命令行或Emacs typecheck的不同,我得到两个不同的结果(分别是不正确和正确),如下所示:
在顶部,使用“重新加载并进行类型检查以指向”,它正确地在“写密码“垃圾””行中找到一个错误,而底部的控制台行声称所有验证条件均已成功释放。
我想念什么?