summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Bauereiss2017-10-25 18:06:19 +0100
committerThomas Bauereiss2017-10-26 16:42:28 +0100
commitb204b093a2fa23f10acd283a43397cb83dfa4757 (patch)
treed473631c83f5253c8f923ed5b39c5a3e9e70432a /src
parent5fc7d18f2ab65100b2a0894daae874145b5d6813 (diff)
Experiment with pretty-printing non-atomic nexps in Lem
Diffstat (limited to 'src')
-rw-r--r--src/ast_util.ml37
-rw-r--r--src/ast_util.mli9
-rw-r--r--src/pretty_print_lem.ml118
3 files changed, 111 insertions, 53 deletions
diff --git a/src/ast_util.ml b/src/ast_util.ml
index b0f4c052..4a887898 100644
--- a/src/ast_util.ml
+++ b/src/ast_util.ml
@@ -592,11 +592,36 @@ module Id = struct
| Id_aux (DeIid _, _), Id_aux (Id _, _) -> 1
end
+module Nexp = struct
+ type t = nexp
+ let rec compare (Nexp_aux (nexp1, _)) (Nexp_aux (nexp2, _)) =
+ let lex_ord (c1, c2) = if c1 = 0 then c2 else c1 in
+ match nexp1, nexp2 with
+ | Nexp_id v1, Nexp_id v2 -> Id.compare v1 v2
+ | Nexp_var kid1, Nexp_var kid2 -> Kid.compare kid1 kid2
+ | Nexp_constant c1, Nexp_constant c2 -> Pervasives.compare c1 c2
+ | Nexp_times (n1a, n1b), Nexp_times (n2a, n2b)
+ | Nexp_sum (n1a, n1b), Nexp_sum (n2a, n2b)
+ | Nexp_minus (n1a, n1b), Nexp_minus (n2a, n2b) ->
+ lex_ord (compare n1a n2a, compare n1b n2b)
+ | Nexp_exp n1, Nexp_exp n2 -> compare n1 n2
+ | Nexp_neg n1, Nexp_neg n2 -> compare n1 n2
+ | Nexp_constant _, _ -> -1 | _, Nexp_constant _ -> 1
+ | Nexp_id _, _ -> -1 | _, Nexp_id _ -> 1
+ | Nexp_var _, _ -> -1 | _, Nexp_var _ -> 1
+ | Nexp_neg _, _ -> -1 | _, Nexp_neg _ -> 1
+ | Nexp_exp _, _ -> -1 | _, Nexp_exp _ -> 1
+ | Nexp_minus _, _ -> -1 | _, Nexp_minus _ -> 1
+ | Nexp_sum _, _ -> -1 | _, Nexp_sum _ -> 1
+ | Nexp_times _, _ -> -1 | _, Nexp_times _ -> 1
+end
+
module BESet = Set.Make(BE)
module Bindings = Map.Make(Id)
module IdSet = Set.Make(Id)
module KBindings = Map.Make(Kid)
module KidSet = Set.Make(Kid)
+module NexpSet = Set.Make(Nexp)
let rec nexp_frees (Nexp_aux (nexp, l)) =
match nexp with
@@ -609,17 +634,7 @@ let rec nexp_frees (Nexp_aux (nexp, l)) =
| Nexp_exp n -> nexp_frees n
| Nexp_neg n -> nexp_frees n
-let rec nexp_identical (Nexp_aux (nexp1, _)) (Nexp_aux (nexp2, _)) =
- match nexp1, nexp2 with
- | Nexp_id v1, Nexp_id v2 -> Id.compare v1 v2 = 0
- | Nexp_var kid1, Nexp_var kid2 -> Kid.compare kid1 kid2 = 0
- | Nexp_constant c1, Nexp_constant c2 -> c1 = c2
- | Nexp_times (n1a, n1b), Nexp_times (n2a, n2b) -> nexp_identical n1a n2a && nexp_identical n1b n2b
- | Nexp_sum (n1a, n1b), Nexp_sum (n2a, n2b) -> nexp_identical n1a n2a && nexp_identical n1b n2b
- | Nexp_minus (n1a, n1b), Nexp_minus (n2a, n2b) -> nexp_identical n1a n2a && nexp_identical n1b n2b
- | Nexp_exp n1, Nexp_exp n2 -> nexp_identical n1 n2
- | Nexp_neg n1, Nexp_neg n2 -> nexp_identical n1 n2
- | _, _ -> false
+let rec nexp_identical nexp1 nexp2 = (Nexp.compare nexp1 nexp2 = 0)
let rec is_nexp_constant (Nexp_aux (nexp, _)) = match nexp with
| Nexp_id _ | Nexp_var _ -> false
diff --git a/src/ast_util.mli b/src/ast_util.mli
index ef367e4e..d497a687 100644
--- a/src/ast_util.mli
+++ b/src/ast_util.mli
@@ -187,6 +187,11 @@ module Kid : sig
val compare : kid -> kid -> int
end
+module Nexp : sig
+ type t = nexp
+ val compare : nexp -> nexp -> int
+end
+
module BE : sig
type t = base_effect
val compare : base_effect -> base_effect -> int
@@ -196,6 +201,10 @@ module IdSet : sig
include Set.S with type elt = id
end
+module NexpSet : sig
+ include Set.S with type elt = nexp
+end
+
module BESet : sig
include Set.S with type elt = base_effect
end
diff --git a/src/pretty_print_lem.ml b/src/pretty_print_lem.ml
index 23fc8287..a0a4878b 100644
--- a/src/pretty_print_lem.ml
+++ b/src/pretty_print_lem.ml
@@ -152,47 +152,80 @@ let is_regtyp (Typ_aux (typ, _)) env = match typ with
let doc_nexp_lem (Nexp_aux (nexp, l) as full_nexp) = match nexp with
| Nexp_constant i -> string ("ty" ^ string_of_int i)
- | Nexp_var v -> string (string_of_kid v)
- | _ -> raise (Reporting_basic.err_unreachable l
- ("cannot pretty-print non-atomic nexp \"" ^ string_of_nexp full_nexp ^ "\""))
+ | Nexp_var v -> string (string_of_kid (orig_kid v))
+ | _ ->
+ let rec mangle_nexp (Nexp_aux (nexp, _)) = begin
+ match nexp with
+ | Nexp_id id -> string_of_id id
+ | Nexp_var kid -> string_of_id (id_of_kid (orig_kid kid))
+ | Nexp_constant i -> Pretty_print_lem_ast.lemnum string_of_int i
+ | Nexp_times (n1, n2) -> mangle_nexp n1 ^ "_times_" ^ mangle_nexp n2
+ | Nexp_sum (n1, n2) -> mangle_nexp n1 ^ "_plus_" ^ mangle_nexp n2
+ | Nexp_minus (n1, n2) -> mangle_nexp n1 ^ "_minus_" ^ mangle_nexp n2
+ | Nexp_exp n -> "exp_" ^ mangle_nexp n
+ | Nexp_neg n -> "neg_" ^ mangle_nexp n
+ end in
+ string ("'" ^ mangle_nexp full_nexp)
+ (* raise (Reporting_basic.err_unreachable l
+ ("cannot pretty-print non-atomic nexp \"" ^ string_of_nexp full_nexp ^ "\"")) *)
+
+(* Rewrite mangled names of type variables to the original names *)
+let rec orig_nexp (Nexp_aux (nexp, l)) =
+ let rewrap nexp = Nexp_aux (nexp, l) in
+ match nexp with
+ | Nexp_var kid -> rewrap (Nexp_var (orig_kid kid))
+ | Nexp_times (n1, n2) -> rewrap (Nexp_times (orig_nexp n1, orig_nexp n2))
+ | Nexp_sum (n1, n2) -> rewrap (Nexp_sum (orig_nexp n1, orig_nexp n2))
+ | Nexp_minus (n1, n2) -> rewrap (Nexp_minus (orig_nexp n1, orig_nexp n2))
+ | Nexp_exp n -> rewrap (Nexp_exp (orig_nexp n))
+ | Nexp_neg n -> rewrap (Nexp_neg (orig_nexp n))
+ | _ -> rewrap nexp
(* Returns the set of type variables that will appear in the Lem output,
which may be smaller than those in the Sail type. May need to be
updated with doc_typ_lem *)
-let rec lem_tyvars_of_typ sequential mwords (Typ_aux (t,_)) =
- let trec = lem_tyvars_of_typ sequential mwords in
+let rec lem_nexps_of_typ sequential mwords (Typ_aux (t,_)) =
+ let trec = lem_nexps_of_typ sequential mwords in
match t with
| Typ_wild
- | Typ_id _ -> KidSet.empty
- | Typ_var kid -> KidSet.singleton kid
- | Typ_fn (t1,t2,_) -> KidSet.union (trec t1) (trec t2)
+ | Typ_id _ -> NexpSet.empty
+ | Typ_var kid -> NexpSet.singleton (orig_nexp (nvar kid))
+ | Typ_fn (t1,t2,_) -> NexpSet.union (trec t1) (trec t2)
| Typ_tup ts ->
- List.fold_left (fun s t -> KidSet.union s (trec t))
- KidSet.empty ts
+ List.fold_left (fun s t -> NexpSet.union s (trec t))
+ NexpSet.empty ts
| Typ_app(Id_aux (Id "vector", _), [
Typ_arg_aux (Typ_arg_nexp n, _);
Typ_arg_aux (Typ_arg_nexp m, _);
Typ_arg_aux (Typ_arg_order ord, _);
Typ_arg_aux (Typ_arg_typ elem_typ, _)]) ->
- KidSet.union
- (if mwords then tyvars_of_nexp (simplify_nexp m) else KidSet.empty)
- (trec elem_typ)
+ let m = simplify_nexp m in
+ if mwords && is_bit_typ elem_typ && not (is_nexp_constant m) then
+ NexpSet.singleton (orig_nexp m)
+ else trec elem_typ
+ (* NexpSet.union
+ (if mwords then tyvars_of_nexp (simplify_nexp m) else NexpSet.empty)
+ (trec elem_typ) *)
| Typ_app(Id_aux (Id "register", _), [Typ_arg_aux (Typ_arg_typ etyp, _)]) ->
- if sequential then trec etyp else KidSet.empty
+ if sequential then trec etyp else NexpSet.empty
| Typ_app(Id_aux (Id "range", _),_)
| Typ_app(Id_aux (Id "implicit", _),_)
- | Typ_app(Id_aux (Id "atom", _), _) -> KidSet.empty
+ | Typ_app(Id_aux (Id "atom", _), _) -> NexpSet.empty
| Typ_app (_,tas) ->
- List.fold_left (fun s ta -> KidSet.union s (lem_tyvars_of_typ_arg sequential mwords ta))
- KidSet.empty tas
+ List.fold_left (fun s ta -> NexpSet.union s (lem_nexps_of_typ_arg sequential mwords ta))
+ NexpSet.empty tas
| Typ_exist (kids,_,t) ->
let s = trec t in
- List.fold_left (fun s k -> KidSet.remove k s) s kids
-and lem_tyvars_of_typ_arg sequential mwords (Typ_arg_aux (ta,_)) =
+ List.fold_left (fun s k -> NexpSet.remove k s) s (List.map nvar kids)
+and lem_nexps_of_typ_arg sequential mwords (Typ_arg_aux (ta,_)) =
match ta with
- | Typ_arg_nexp nexp -> tyvars_of_nexp nexp
- | Typ_arg_typ typ -> lem_tyvars_of_typ sequential mwords typ
- | Typ_arg_order _ -> KidSet.empty
+ | Typ_arg_nexp nexp -> NexpSet.singleton (orig_nexp (simplify_nexp nexp))
+ | Typ_arg_typ typ -> lem_nexps_of_typ sequential mwords typ
+ | Typ_arg_order _ -> NexpSet.empty
+
+let lem_tyvars_of_typ sequential mwords typ =
+ NexpSet.fold (fun nexp ks -> KidSet.union ks (tyvars_of_nexp nexp))
+ (lem_nexps_of_typ sequential mwords typ) KidSet.empty
(* When making changes here, check whether they affect lem_tyvars_of_typ *)
let doc_typ_lem, doc_atomic_typ_lem =
@@ -364,7 +397,9 @@ let doc_quant_item vars_included (QI_aux (qi, _)) = match qi with
| QI_id (KOpt_aux (KOpt_kind (_, kid), _)) ->
(match vars_included with
None -> doc_var kid
- | Some set when KidSet.mem kid set -> doc_var kid
+ | Some set -> (*when KidSet.mem kid set -> doc_var kid*)
+ let nexps = NexpSet.filter (fun nexp -> KidSet.mem (orig_kid kid) (nexp_frees nexp)) set in
+ separate_map space doc_nexp_lem (NexpSet.elements nexps)
| _ -> empty)
| _ -> empty
@@ -381,39 +416,38 @@ let doc_typquant_lem (TypQ_aux(tq,_)) vars_included typ = match tq with
machine words. Often these will be unnecessary, but this simple
approach will do for now. *)
-let rec typeclass_vars (Typ_aux(t,_)) = match t with
+let rec typeclass_nexps (Typ_aux(t,_)) = match t with
| Typ_wild
| Typ_id _
| Typ_var _
- -> []
-| Typ_fn (t1,t2,_) -> typeclass_vars t1 @ typeclass_vars t2
-| Typ_tup ts -> List.concat (List.map typeclass_vars ts)
+ -> NexpSet.empty
+| Typ_fn (t1,t2,_) -> NexpSet.union (typeclass_nexps t1) (typeclass_nexps t2)
+| Typ_tup ts -> List.fold_left NexpSet.union NexpSet.empty (List.map typeclass_nexps ts)
| Typ_app (Id_aux (Id "vector",_),
- [_;Typ_arg_aux (Typ_arg_nexp (Nexp_aux (Nexp_var v,_)),_);
+ [_;Typ_arg_aux (Typ_arg_nexp size_nexp,_);
_;Typ_arg_aux (Typ_arg_typ (Typ_aux (Typ_id (Id_aux (Id "bit",_)),_)),_)]) ->
- [v]
-| Typ_app _ -> []
-| Typ_exist (kids,_,t) -> [] (* todo *)
+ if is_nexp_constant (simplify_nexp size_nexp) then NexpSet.empty else
+ NexpSet.singleton (orig_nexp size_nexp)
+| Typ_app _ -> NexpSet.empty
+| Typ_exist (kids,_,t) -> NexpSet.empty (* todo *)
let doc_typclasses_lem mwords t =
if mwords then
- let vars = typeclass_vars t in
- let vars = List.sort_uniq Kid.compare vars in
- match vars with
- | [] -> (empty, KidSet.empty)
- | _ -> (separate_map comma_sp (fun var -> string "Size " ^^ doc_var var) vars ^^ string " => ", KidSet.of_list vars)
- else (empty, KidSet.empty)
+ let nexps = typeclass_nexps t in
+ if NexpSet.is_empty nexps then (empty, NexpSet.empty) else
+ (separate_map comma_sp (fun nexp -> string "Size " ^^ doc_nexp_lem nexp) (NexpSet.elements nexps) ^^ string " => ", nexps)
+ else (empty, NexpSet.empty)
let doc_typschm_lem sequential mwords quants (TypSchm_aux(TypSchm_ts(tq,t),_)) =
let pt = doc_typ_lem sequential mwords t in
if quants
then
- let tyvars_used = lem_tyvars_of_typ sequential mwords t in
- let ptyc, tyvars_sizes = doc_typclasses_lem mwords t in
- let tyvars_to_include = KidSet.union tyvars_used tyvars_sizes in
- if KidSet.is_empty tyvars_to_include
+ let nexps_used = lem_nexps_of_typ sequential mwords t in
+ let ptyc, nexps_sizes = doc_typclasses_lem mwords t in
+ let nexps_to_include = NexpSet.union nexps_used nexps_sizes in
+ if NexpSet.is_empty nexps_to_include
then pt
- else doc_typquant_lem tq (Some tyvars_to_include) (ptyc ^^ pt)
+ else doc_typquant_lem tq (Some nexps_to_include) (ptyc ^^ pt)
else pt
let is_ctor env id = match Env.lookup_id id env with