Skip to content

Commit 9dcbe98

Browse files
committed
Add function Value.default_of_type (#483)
Closes #483
1 parent 05313ce commit 9dcbe98

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/smtml/value.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ let map v f = match v with Nothing -> Nothing | _ -> f v
8383

8484
let ( let+ ) = map
8585

86+
let default_of_type = function
87+
| Ty.Ty_bool -> False
88+
| Ty_int -> Int 0
89+
| Ty_real -> Real 0.0
90+
| Ty_str -> Str ""
91+
| Ty_bitv m -> Bitv (Bitvector.make Z.zero m)
92+
| Ty_fp 32 -> Num (F32 0l)
93+
| Ty_fp 64 -> Num (F64 0L)
94+
| Ty_list -> List []
95+
| Ty_unit -> Unit
96+
| Ty_none -> Nothing
97+
| (Ty_fp _ | Ty_app | Ty_regexp | Ty_roundingMode) as ty ->
98+
Fmt.failwith "No default value for type %a" Ty.pp ty
99+
86100
let rec pp fmt = function
87101
| True -> Fmt.string fmt "true"
88102
| False -> Fmt.string fmt "false"

src/smtml/value.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ val map : t -> (t -> t) -> t
4747
(** [let+ v f] is a convenience operator for applying [f] to [v]. *)
4848
val ( let+ ) : t -> (t -> t) -> t
4949

50+
(** [default_of_type ty] returns the default value for type [ty]. *)
51+
val default_of_type : Ty.t -> t
52+
5053
(** {1 Pretty Printing} *)
5154

5255
(** [pp fmt v] pretty-prints the value [v] using the formatter [fmt]. *)

0 commit comments

Comments
 (0)