Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit 7e8bbf7

Browse files
committed
More documentation
1 parent dc40d4f commit 7e8bbf7

11 files changed

Lines changed: 245 additions & 137 deletions

File tree

src/mlang/mir_interpreter/context.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ let empty_ctx ?inputs ?events (p : M_ir.Mir.program) : 'a ctx =
232232
Option.iter (update_ctx_with_events res) events;
233233
res
234234

235-
module Make (N : M_ir.Mir_number.NumberInterface) = struct
235+
module Make (N : Number.S) = struct
236236
let get_var_space (ctx : N.t ctx) (m_sp_opt : Com.var_space) =
237237
let i_sp =
238238
match m_sp_opt with None -> ctx.ctx_var_space | Some (_, i_sp) -> i_sp

src/mlang/mir_interpreter/context.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,4 @@ val empty_ctx :
111111
the context of [p] (for example, with variables declared in [p]. Parameters
112112
[inputs] and [events] are required for interpreting the whole program. *)
113113

114-
module Make (N : M_ir.Mir_number.NumberInterface) :
115-
S with type custom_float := N.t
114+
module Make (N : Number.S) : S with type custom_float := N.t

src/mlang/mir_interpreter/eval.ml

Lines changed: 28 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ module type S = sig
5252
(custom_float value, Com.Var.t) Com.event_value
5353
end
5454

55-
module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor) :
56-
S with type custom_float = N.t = struct
55+
module Make (N : Types.Number) : S with type custom_float = N.t = struct
5756
(* Careful : this behavior mimics the one imposed by the original Mlang
5857
compiler... *)
5958

60-
module R = RF (N)
61-
module Funs = Functions.Make (N) (R)
59+
module Funs = Functions.Make (N)
6260
module C = Context.Make (N)
63-
module Print = Print.Make (N) (C)
61+
module Print = Print.Make (N)
6462

6563
type custom_float = N.t
6664

@@ -72,7 +70,7 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
7270

7371
exception BlockingError
7472

75-
let roundf (x : N.t) = R.roundf x
73+
let roundf (x : N.t) = N.roundf x
7674

7775
let literal_to_value (l : Com.literal) : value =
7876
match l with
@@ -312,8 +310,14 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
312310
(fun (arg : Com.Var.t Com.print_arg Pos.marked) ->
313311
match Pos.unmark arg with
314312
| PrintString s -> Print.string pctx s
315-
| PrintAccess (info, m_a) ->
316-
Print.access ~eval:evaluate_expr pctx info (Pos.unmark m_a)
313+
| PrintAccess (info, m_a) -> (
314+
match
315+
C.get_access_var ~eval:evaluate_expr pctx.ctx (Pos.unmark m_a)
316+
with
317+
| None -> ()
318+
| Some (vsd, var, _) ->
319+
let _, v, _ = C.get_var pctx.ctx None var in
320+
Print.access pctx info vsd v)
317321
| PrintIndent e -> Print.indent pctx (evaluate_expr pctx.ctx e)
318322
| PrintExpr (e, mi, ma) ->
319323
Print.value pctx mi ma (evaluate_expr pctx.ctx e))
@@ -789,61 +793,21 @@ module Make (N : Mir_number.NumberInterface) (RF : Mir_roundops.RoundOpsFunctor)
789793
| Stop_instruction SKTarget -> (* May not be caught by anything else *) ()
790794
end
791795

792-
module BigIntPrecision = struct
793-
let scaling_factor_bits = ref 64
794-
end
795-
796-
module MainframeLongSize = struct
797-
let max_long = ref Int64.max_int
798-
end
799-
800-
module FloatDefInterp =
801-
Make (Mir_number.RegularFloatNumber) (Mir_roundops.DefaultRoundOps)
802-
module FloatMultInterp =
803-
Make (Mir_number.RegularFloatNumber) (Mir_roundops.MultiRoundOps)
804-
module FloatMfInterp =
805-
Make
806-
(Mir_number.RegularFloatNumber)
807-
(Mir_roundops.MainframeRoundOps (MainframeLongSize))
808-
module MPFRDefInterp =
809-
Make (Mir_number.MPFRNumber) (Mir_roundops.DefaultRoundOps)
810-
module MPFRMultInterp =
811-
Make (Mir_number.MPFRNumber) (Mir_roundops.MultiRoundOps)
812-
module MPFRMfInterp =
813-
Make
814-
(Mir_number.MPFRNumber)
815-
(Mir_roundops.MainframeRoundOps (MainframeLongSize))
816-
module BigIntDefInterp =
817-
Make
818-
(Mir_number.BigIntFixedPointNumber
819-
(BigIntPrecision))
820-
(Mir_roundops.DefaultRoundOps)
821-
module BigIntMultInterp =
822-
Make
823-
(Mir_number.BigIntFixedPointNumber
824-
(BigIntPrecision))
825-
(Mir_roundops.MultiRoundOps)
826-
module BigIntMfInterp =
827-
Make
828-
(Mir_number.BigIntFixedPointNumber
829-
(BigIntPrecision))
830-
(Mir_roundops.MainframeRoundOps (MainframeLongSize))
831-
module IntvDefInterp =
832-
Make (Mir_number.IntervalNumber) (Mir_roundops.DefaultRoundOps)
833-
module IntvMultInterp =
834-
Make (Mir_number.IntervalNumber) (Mir_roundops.MultiRoundOps)
835-
module IntvMfInterp =
836-
Make
837-
(Mir_number.IntervalNumber)
838-
(Mir_roundops.MainframeRoundOps (MainframeLongSize))
839-
module RatDefInterp =
840-
Make (Mir_number.RationalNumber) (Mir_roundops.DefaultRoundOps)
841-
module RatMultInterp =
842-
Make (Mir_number.RationalNumber) (Mir_roundops.MultiRoundOps)
843-
module RatMfInterp =
844-
Make
845-
(Mir_number.RationalNumber)
846-
(Mir_roundops.MainframeRoundOps (MainframeLongSize))
796+
module FloatDefInterp = Make (Number.FloatDef)
797+
module FloatMultInterp = Make (Number.FloatMult)
798+
module FloatMfInterp = Make (Number.FloatMf)
799+
module MPFRDefInterp = Make (Number.MPFRDef)
800+
module MPFRMultInterp = Make (Number.MPFRMult)
801+
module MPFRMfInterp = Make (Number.MPFRMf)
802+
module BigIntDefInterp = Make (Number.BigIntDef)
803+
module BigIntMultInterp = Make (Number.BigIntMult)
804+
module BigIntMfInterp = Make (Number.BigIntMf)
805+
module IntvDefInterp = Make (Number.IntvDef)
806+
module IntvMultInterp = Make (Number.IntvMult)
807+
module IntvMfInterp = Make (Number.IntvMf)
808+
module RatDefInterp = Make (Number.RatDef)
809+
module RatMultInterp = Make (Number.RatMult)
810+
module RatMfInterp = Make (Number.RatMf)
847811

848812
let get_interp (sort : Config.value_sort) (roundops : Config.round_ops) :
849813
(module S) =
@@ -864,31 +828,11 @@ let get_interp (sort : Config.value_sort) (roundops : Config.round_ops) :
864828
| Rational, ROMulti -> (module RatMultInterp)
865829
| Rational, ROMainframe _ -> (module RatMfInterp)
866830

867-
let prepare_interp (sort : Config.value_sort) (roundops : Config.round_ops) :
868-
unit =
869-
begin
870-
match sort with
871-
| MPFR prec -> Mpfr.set_default_prec prec
872-
| BigInt prec -> BigIntPrecision.scaling_factor_bits := prec
873-
| Interval -> Mpfr.set_default_prec 64
874-
| _ -> ()
875-
end;
876-
match roundops with
877-
| ROMainframe long_size ->
878-
let max_long =
879-
if long_size = 32 then Int64.of_int32 Int32.max_int
880-
else if long_size = 64 then Int64.max_int
881-
else assert false
882-
(* checked when parsing command line *)
883-
in
884-
MainframeLongSize.max_long := max_long
885-
| _ -> ()
886-
887831
let evaluate_program ~(p : Mir.program) ~(inputs : Com.literal Com.Var.Map.t)
888832
~(events : (Com.literal, Com.Var.t) Com.event_value StrMap.t list)
889833
~(sort : Config.value_sort) ~(round_ops : Config.round_ops) :
890834
Com.literal Com.Var.Map.t * Com.Error.Set.t =
891-
prepare_interp sort round_ops;
835+
Number.setup_precision sort round_ops;
892836
let module Interp = (val get_interp sort round_ops : S) in
893837
let ctx =
894838
let inputs = Com.Var.Map.map Interp.literal_to_value inputs in

src/mlang/mir_interpreter/eval.mli

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,56 +62,54 @@ end
6262
(** Builds an intepretation engine from a number interface
6363
({!module: M_ir.Mir_number}) and a rounding strategy
6464
({!module: M_ir.Mir_roundops}). *)
65-
module Make
66-
(N : M_ir.Mir_number.NumberInterface)
67-
(RF : M_ir.Mir_roundops.RoundOpsFunctor) : S with type custom_float = N.t
65+
module Make (N : Number.S) : S with type custom_float = N.t
6866

6967
(** {2 Engines} *)
7068

7169
(** These modules are instanes of Make with modules defined in
7270
{!module: M_ir.Mir_number} and {!module: M_ir.Mir_roundops}. *)
7371

74-
(** Float with default rounding strategy. *)
7572
module FloatDefInterp : S with type custom_float = float
73+
(** Float with default rounding strategy. *)
7674

77-
(** Float with multithread rounding strategy. *)
7875
module FloatMultInterp : S with type custom_float = float
76+
(** Float with multithread rounding strategy. *)
7977

80-
(** Float with mainframe rounding strategy. *)
8178
module FloatMfInterp : S with type custom_float = float
79+
(** Float with mainframe rounding strategy. *)
8280

83-
(** Multiple-precision floating-point with default rounding strategy. *)
8481
module MPFRDefInterp : S with type custom_float = Mpfrf.t
82+
(** Multiple-precision floating-point with default rounding strategy. *)
8583

86-
(** Multiple-precision floating-point with multithread rounding strategy. *)
8784
module MPFRMultInterp : S with type custom_float = Mpfrf.t
85+
(** Multiple-precision floating-point with multithread rounding strategy. *)
8886

89-
(** Multiple-precision floating-point with mainframe rounding strategy. *)
9087
module MPFRMfInterp : S with type custom_float = Mpfrf.t
88+
(** Multiple-precision floating-point with mainframe rounding strategy. *)
9189

92-
(** Multiple precision integer arithmetic with default rounding strategy. *)
9390
module BigIntDefInterp : S with type custom_float = Mpzf.t
91+
(** Multiple precision integer arithmetic with default rounding strategy. *)
9492

95-
(** Multiple precision integer arithmetic with multihtread rounding strategy. *)
9693
module BigIntMultInterp : S with type custom_float = Mpzf.t
94+
(** Multiple precision integer arithmetic with multihtread rounding strategy. *)
9795

98-
(** Multiple precision integer arithmetic with mainframe rounding strategy. *)
9996
module BigIntMfInterp : S with type custom_float = Mpzf.t
97+
(** Multiple precision integer arithmetic with mainframe rounding strategy. *)
10098

101-
(** Multiple-precision floating-point intervals with default rounding strategy. *)
10299
module IntvDefInterp : S with type custom_float = M_ir.Mir_number.interval
100+
(** Multiple-precision floating-point intervals with default rounding strategy. *)
103101

104-
(** Multiple-precision floating-point intervals with multithread rounding strategy. *)
105102
module IntvMultInterp : S with type custom_float = M_ir.Mir_number.interval
103+
(** Multiple-precision floating-point intervals with multithread rounding strategy. *)
106104

107-
(** Multiple-precision floating-point intervals with mainframe rounding strategy. *)
108105
module IntvMfInterp : S with type custom_float = M_ir.Mir_number.interval
106+
(** Multiple-precision floating-point intervals with mainframe rounding strategy. *)
109107

110-
(** Multiple-precision rationals with default rounding strategy. *)
111108
module RatDefInterp : S with type custom_float = Mpqf.t
109+
(** Multiple-precision rationals with default rounding strategy. *)
112110

113-
(** Multiple-precision rationals with multithread rounding strategy. *)
114111
module RatMultInterp : S with type custom_float = Mpqf.t
112+
(** Multiple-precision rationals with multithread rounding strategy. *)
115113

116-
(** Multiple-precision rationals with mainframe rounding strategy. *)
117114
module RatMfInterp : S with type custom_float = Mpqf.t
115+
(** Multiple-precision rationals with mainframe rounding strategy. *)

src/mlang/mir_interpreter/functions.ml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
(* TODO: move functions here *)
2-
open M_ir
32
open Types
43

5-
module Make
6-
(N : Mir_number.NumberInterface)
7-
(R : Mir_roundops.RoundOpsInterface with type t = N.t) =
8-
struct
4+
module Make (N : Types.Number) = struct
95
let false_value () = Number (N.zero ())
106

117
let true_value () = Number (N.one ())
128

139
let arr = function
14-
| Number x -> Number (R.roundf x)
10+
| Number x -> Number (N.roundf x)
1511
| Undefined -> Undefined (*nope:Float 0.*)
1612

1713
let inf = function
18-
| Number x -> Number (R.truncatef x)
14+
| Number x -> Number (N.truncatef x)
1915
| Undefined -> Undefined
2016

2117
let present = function Undefined -> false_value () | _ -> true_value ()
@@ -44,7 +40,7 @@ struct
4440
| Undefined, _ -> Undefined
4541
| Number n, _ when N.is_zero n -> Undefined
4642
| Number f, `Table l ->
47-
let nb = Int64.to_int @@ N.to_int @@ R.roundf f in
43+
let nb = Int64.to_int @@ N.to_int @@ N.roundf f in
4844
let rec loop res cpt = function
4945
| [] -> res
5046
| _ when cpt >= nb -> res
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
1-
module Make
2-
(N : M_ir.Mir_number.NumberInterface)
3-
(_ : M_ir.Mir_roundops.RoundOpsInterface with type t = N.t) : sig
1+
(** Implementation of several function calls as described in
2+
{{:../../../../../fonctions.html#valeurs}the functions documentation}.
3+
4+
Note: the interpreter implementation is supposed to be the legitimate
5+
specification. If there are inconsistencies between the documentation
6+
or the C code and the actual behavior of the interprer, the interpreter
7+
is the reference. *)
8+
9+
module Make (N : Types.Number) : sig
410
val arr : N.t Types.value -> N.t Types.value
11+
(** Implements the 'arr' call (rounding). *)
512

613
val inf : N.t Types.value -> N.t Types.value
14+
(** Implements the 'inf' call (truncate). *)
715

816
val present : 'a Types.value -> N.t Types.value
17+
(** Implements the 'present' call that checks if the value xis not equal to
18+
undefined. *)
919

1020
val supzero : N.t Types.value -> N.t Types.value
21+
(** Implements the 'supzero' call, which returns undefined for strictly negative
22+
values or the argument otherwise. *)
1123

1224
val abs : N.t Types.value -> N.t Types.value
25+
(** Implements the 'abs' call, calculating the absolute value of its
26+
argument. *)
1327

1428
val min : N.t Types.value -> N.t Types.value -> N.t Types.value
29+
(** Implements the 'min' call, returning the minimum between two values. *)
1530

1631
val max : N.t Types.value -> N.t Types.value -> N.t Types.value
32+
(** Implements the 'max' call, returning the maximum between two values. *)
1733

1834
val multimax :
1935
N.t Types.value ->
2036
[ `Table of N.t Types.value list | `Var of N.t Types.value ] ->
2137
N.t Types.value
38+
(** Implements the 'multimax' call, returning the max value of a subtable. *)
2239

2340
val nb_events : 'a Types.ctx -> N.t Types.value
41+
(** Implements the 'nb_events' call, returning the number of currently defined
42+
events. *)
2443
end

0 commit comments

Comments
 (0)