Skip to content

Commit

Permalink
Lean: fix generated lean projects (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
javra authored Dec 17, 2024
1 parent 725cbde commit 52d292a
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,6 @@
(%{workspace_root}/src/gen_lib/sail2_values.lem
as
src/gen_lib/sail2_values.lem)
(%{workspace_root}/src/sail_lean_backend/Sail/sail.lean
(%{workspace_root}/src/sail_lean_backend/Sail/Sail.lean
as
src/sail_lean_backend/Sail/sail.lean)))
src/sail_lean_backend/Sail/Sail.lean)))
File renamed without changes.
3 changes: 2 additions & 1 deletion src/sail_lean_backend/pretty_print_lean.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ open PPrint
open Pretty_print_common

let implicit_parens x = enclose (string "{") (string "}") x

let doc_id_ctor (Id_aux (i, _)) =
match i with Id i -> string i | Operator x -> string (Util.zencode_string ("op " ^ x))

let doc_kid (Kid_aux (Var x, _)) = string ("k_" ^ String.sub x 1 (String.length x - 1))
(* TODO do a proper renaming and keep track of it *)

Expand Down Expand Up @@ -298,6 +300,5 @@ let rec remove_imports (defs : (Libsail.Type_check.tannot, Libsail.Type_check.en
let pp_ast_lean ({ defs; _ } as ast : Libsail.Type_check.typed_ast) o =
let defs = remove_imports defs 0 in
let output : document = separate_map empty doc_def defs in
output_string o "import Sail.sail\n\n";
print o output;
()
12 changes: 8 additions & 4 deletions src/sail_lean_backend/sail_plugin_lean.ml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ let create_lake_project (out_name : string) default_sail_dir =
(* Change the base directory if the option '--lean-output-dir' is set *)
let base_dir = match !opt_lean_output_dir with Some dir -> dir | None -> "." in
let project_dir = Filename.concat base_dir out_name in
if !opt_lean_force_output && Sys.is_directory project_dir then (
if !opt_lean_force_output && Sys.file_exists project_dir && Sys.is_directory project_dir then (
let _ = Unix.system ("rm -r " ^ Filename.quote project_dir ^ "/*") in
()
)
Expand All @@ -174,15 +174,19 @@ let create_lake_project (out_name : string) default_sail_dir =
let out_name_camel = Libsail.Util.to_upper_camel_case out_name in
let lakefile = open_out (Filename.concat project_dir "lakefile.toml") in
output_string lakefile
("name = \"" ^ out_name ^ "\"\ndefaultTargets = [\"" ^ out_name_camel
^ "\"]\n\n[[lean_lib]]\nname = \"Sail\"\n\n[[lean_lib]]\nname = \"" ^ out_name_camel ^ "\""
("name = \"" ^ out_name ^ "\"\ndefaultTargets = [\"" ^ out_name_camel ^ "\"]\n\n[[lean_lib]]\nname = \""
^ out_name_camel ^ "\""
);
close_out lakefile;
let lean_src_dir = Filename.concat project_dir out_name_camel in
if not (Sys.file_exists lean_src_dir) then Unix.mkdir lean_src_dir 0o775;
let sail_dir = Reporting.get_sail_dir default_sail_dir in
let _ =
Unix.system ("cp -r " ^ Filename.quote (sail_dir ^ "/src/sail_lean_backend/Sail") ^ " " ^ Filename.quote project_dir)
Unix.system
("cp -r " ^ Filename.quote (sail_dir ^ "/src/sail_lean_backend/Sail") ^ " " ^ Filename.quote lean_src_dir)
in
let project_main = open_out (Filename.concat project_dir (out_name_camel ^ ".lean")) in
output_string project_main ("import " ^ out_name_camel ^ ".Sail.Sail\n\n");
project_main

let output (out_name : string) ast default_sail_dir =
Expand Down
2 changes: 1 addition & 1 deletion test/lean/bitvec_operation.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

def bitvector_eq (x : BitVec 16) (y : BitVec 16) : Bool :=
(Eq x y)
Expand Down
2 changes: 1 addition & 1 deletion test/lean/enum.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

inductive E where | A | B | C
deriving Inhabited
Expand Down
2 changes: 1 addition & 1 deletion test/lean/extern.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

def extern_add : Int :=
(Int.add 5 4)
Expand Down
2 changes: 1 addition & 1 deletion test/lean/extern_bitvec.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

def extern_const : BitVec 64 :=
(0xFFFF000012340000 : BitVec 64)
Expand Down
2 changes: 1 addition & 1 deletion test/lean/let.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

def foo : BitVec 16 :=
let z := (HOr.hOr (0xFFFF : BitVec 16) (0xABCD : BitVec 16))
Expand Down
2 changes: 1 addition & 1 deletion test/lean/struct.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

structure My_struct where
field1 : Int
Expand Down
2 changes: 1 addition & 1 deletion test/lean/trivial.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

def foo (y : Unit) : Unit :=
y
Expand Down
2 changes: 1 addition & 1 deletion test/lean/tuples.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

def tuple1 : (Int × Int × (BitVec 2 × Unit)) :=
(3, 5, ((0b10 : BitVec 2), ()))
Expand Down
2 changes: 1 addition & 1 deletion test/lean/typquant.expected.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Sail.sail
import Out.Sail.Sail

def foo (n : Int) : BitVec 4 :=
(0xF : BitVec 4)
Expand Down

0 comments on commit 52d292a

Please sign in to comment.