@@ -53,6 +53,28 @@ Module Compilers.
53
53
| base.type.list A => list (interp A)
54
54
| base.type.option A => option (interp A)
55
55
end %type.
56
+ Fixpoint bitwidth {t} : interp t -> binterp t
57
+ := match t with
58
+ | base.type.type_base base.type.Z => fun r => ZRange.bitwidth r
59
+ | base.type.type_base _ as t
60
+ | base.type.unit as t
61
+ => fun x => x
62
+ | base.type.prod A B => fun '(a, b) => (@bitwidth A a, @bitwidth B b)
63
+ | base.type.list A => List.map (@bitwidth A)
64
+ | base.type.option A => option_map (@bitwidth A)
65
+ end .
66
+ Fixpoint of_bitwidth (signed : bool) {t} : binterp t -> interp t
67
+ := match t with
68
+ | base.type.type_base base.type.Z => fun r => ZRange.of_bitwidth signed r
69
+ | base.type.type_base _ as t
70
+ | base.type.unit as t
71
+ => fun x => x
72
+ | base.type.prod A B => fun '(a, b) => (@of_bitwidth signed A a, @of_bitwidth signed B b)
73
+ | base.type.list A => List.map (@of_bitwidth signed A)
74
+ | base.type.option A => option_map (@of_bitwidth signed A)
75
+ end %type.
76
+ Notation of_bitwidth_unsigned := (@of_bitwidth false).
77
+ Notation of_bitwidth_signed := (@of_bitwidth true).
56
78
Fixpoint map_ranges (f : zrange -> zrange) {t} : interp t -> interp t
57
79
:= match t with
58
80
| base.type.type_base base.type.Z => f
@@ -406,6 +428,18 @@ Module Compilers.
406
428
| type.base x => @base.is_bounded_by x
407
429
| type.arrow s d => fun _ _ => false
408
430
end .
431
+ Fixpoint bitwidth (signed : bool) {t} : interp t -> einterp t
432
+ := match t with
433
+ | type.base x => @base.bitwidth x
434
+ | type.arrow s d => fun fr sz => @bitwidth signed d (fr (@of_bitwidth signed s sz))
435
+ end
436
+ with of_bitwidth (signed : bool) {t} : einterp t -> interp t
437
+ := match t with
438
+ | type.base x => @base.of_bitwidth signed x
439
+ | type.arrow s d => fun fr sz => @of_bitwidth signed d (fr (@bitwidth signed s sz))
440
+ end .
441
+ Notation of_bitwidth_unsigned := (@of_bitwidth false).
442
+ Notation of_bitwidth_signed := (@of_bitwidth true).
409
443
Module option.
410
444
(** turn a [type] into a [Set] describing the type of optional
411
445
bounds on that base type; bounds on a [Z] may be either a
0 commit comments