From 5e8b3394400ed35f512350187770ccd9dfb1cecf Mon Sep 17 00:00:00 2001 From: elvis Date: Mon, 27 Jan 2025 00:18:23 +0100 Subject: [PATCH] Better styling for miniFun --- lib/miniFun/Semantics.ml | 9 ++++--- lib/miniFun/TypeChecker.ml | 6 +++-- lib/miniFun/Types.ml | 55 +++++++++++++++++++------------------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/lib/miniFun/Semantics.ml b/lib/miniFun/Semantics.ml index 49640d6..5661a93 100644 --- a/lib/miniFun/Semantics.ml +++ b/lib/miniFun/Semantics.ml @@ -5,14 +5,17 @@ Random.self_init () let (let*) = Result.bind -let rec evaluate (mem: memory) (command: t_exp) : (permittedValues, [> error]) result = +let rec evaluate (mem: memory) (command: t_exp) : + (permittedValues, [> error]) result = match command with Integer n -> Ok (IntegerPermitted n) | Boolean b -> Ok (BooleanPermitted b) | Variable v -> ( match VariableMap.find_opt v mem.assignments with - None -> Error (`AbsentAssignment ("The variable " ^ v ^ " is not defined.")) - | Some a -> Ok a + | None -> + Error (`AbsentAssignment ("The variable " ^ v ^ " is not defined.")) + | Some a -> + Ok a ) | Tuple (x, y) -> ( let* xval = evaluate mem x in diff --git a/lib/miniFun/TypeChecker.ml b/lib/miniFun/TypeChecker.ml index a19378e..d3d8df0 100644 --- a/lib/miniFun/TypeChecker.ml +++ b/lib/miniFun/TypeChecker.ml @@ -5,7 +5,8 @@ Random.self_init () let (let*) = Result.bind -let rec evaluate_type (program: t_exp) (context: ftype VariableMap.t) : (ftype, [> typechecking_error]) result = +let rec evaluate_type (program: t_exp) (context: ftype VariableMap.t) : + (ftype, [> typechecking_error]) result = match program with Integer _ -> Ok IntegerType | Boolean _ -> Ok BooleanType @@ -25,7 +26,8 @@ let rec evaluate_type (program: t_exp) (context: ftype VariableMap.t) : (ftype, the type of the body using the bindings for the input *) match typef with FunctionType (tin, tout) -> ( - let* typefbody = evaluate_type fbody (VariableMap.add x tin context) in + let* typefbody = evaluate_type fbody (VariableMap.add x tin context) + in if (typefbody = tout) then Ok typef else diff --git a/lib/miniFun/Types.ml b/lib/miniFun/Types.ml index 9bfae31..a32281e 100644 --- a/lib/miniFun/Types.ml +++ b/lib/miniFun/Types.ml @@ -9,33 +9,34 @@ type ftype = | FunctionType of ftype * ftype type t_exp = - Integer of int (* x := a *) - | Boolean of bool (* v *) - | Variable of variable (* x *) - | Tuple of t_exp * t_exp (* (a, b) *) - | Function of variable * ftype * t_exp (* lambda x: t. x *) - | Application of t_exp * t_exp (* x x *) - | Plus of t_exp * t_exp (* x + x *) - | Minus of t_exp * t_exp (* x - x *) - | Times of t_exp * t_exp (* x * x *) - | Division of t_exp * t_exp (* x / x *) - | Modulo of t_exp * t_exp (* x % x *) - | Power of t_exp * t_exp (* x ^ x *) - | PowerMod of t_exp * t_exp * t_exp (* (x ^ x) % x *) - | Rand of t_exp (* rand(0, x) *) - | BAnd of t_exp * t_exp (* x && x *) - | BOr of t_exp * t_exp (* x || x *) - | BNot of t_exp (* not x *) - | First of t_exp (* fst x *) - | Second of t_exp (* scn x *) - | Cmp of t_exp * t_exp (* x == x *) - | CmpLess of t_exp * t_exp (* x < x *) - | CmpLessEq of t_exp * t_exp (* x <= x *) - | CmpGreater of t_exp * t_exp (* x > x *) - | CmpGreaterEq of t_exp * t_exp (* x >= x *) - | IfThenElse of t_exp * t_exp * t_exp (* if b then c else c *) - | LetIn of variable * t_exp * t_exp (* let x = x in x *) - | LetFun of variable * variable * ftype * t_exp * t_exp (* let rec x. y: t. x in x*) + Integer of int (* x := a *) + | Boolean of bool (* v *) + | Variable of variable (* x *) + | Tuple of t_exp * t_exp (* (a, b) *) + | Function of variable * ftype * t_exp (* lambda x: t. x *) + | Application of t_exp * t_exp (* x x *) + | Plus of t_exp * t_exp (* x + x *) + | Minus of t_exp * t_exp (* x - x *) + | Times of t_exp * t_exp (* x * x *) + | Division of t_exp * t_exp (* x / x *) + | Modulo of t_exp * t_exp (* x % x *) + | Power of t_exp * t_exp (* x ^ x *) + | PowerMod of t_exp * t_exp * t_exp (* (x ^ x) % x *) + | Rand of t_exp (* rand(0, x) *) + | BAnd of t_exp * t_exp (* x && x *) + | BOr of t_exp * t_exp (* x || x *) + | BNot of t_exp (* not x *) + | First of t_exp (* fst x *) + | Second of t_exp (* scn x *) + | Cmp of t_exp * t_exp (* x == x *) + | CmpLess of t_exp * t_exp (* x < x *) + | CmpLessEq of t_exp * t_exp (* x <= x *) + | CmpGreater of t_exp * t_exp (* x > x *) + | CmpGreaterEq of t_exp * t_exp (* x >= x *) + | IfThenElse of t_exp * t_exp * t_exp (* if b then c else c *) + | LetIn of variable * t_exp * t_exp (* let x = x in x *) + | LetFun of variable * variable * ftype * t_exp * t_exp + (* let rec x. y: t. x in x*) type permittedValues = IntegerPermitted of int