From 4c12ffba24801a2c960abf4705a3015aed3140c2 Mon Sep 17 00:00:00 2001 From: Mathieu Barbin Date: Thu, 7 Nov 2024 10:59:00 +0100 Subject: [PATCH] Reduce use of Or_error in default scope --- lib/bopkit_bdd_command/src/dune | 2 -- lib/bopkit_block/src/bopkit_block.ml | 3 +-- lib/bopkit_command/src/dune | 2 -- lib/bopkit_compiler/src/dune | 2 -- lib/bopkit_compiler/src/pass_includes.ml | 2 -- .../src/bopkit_process_interpreter.ml | 6 ++---- .../src/interpreted_code.ml | 2 -- lib/bopkit_process_interpreter/src/operator.ml | 12 +++++------- lib/bopkit_simulator/src/dune | 2 -- .../visa/lib/visa_assembler/src/visa_assembler.ml | 2 -- project/visa/lib/visa_dsl/test/test__visa_dsl.ml | 2 -- project/visa/lib/visa_simulator/src/code.ml | 2 -- project/visa/lib/visa_simulator/src/memory.ml | 8 ++------ project/visa/lib/visa_simulator/src/memory.mli | 12 ++---------- .../visa/lib/visa_simulator/src/visa_simulator.ml | 7 ++++--- 15 files changed, 16 insertions(+), 50 deletions(-) diff --git a/lib/bopkit_bdd_command/src/dune b/lib/bopkit_bdd_command/src/dune index 62b5114a..91bf8f20 100644 --- a/lib/bopkit_bdd_command/src/dune +++ b/lib/bopkit_bdd_command/src/dune @@ -14,8 +14,6 @@ -open Stdio -open - Or_error.Let_syntax - -open Cmdlang) (libraries base diff --git a/lib/bopkit_block/src/bopkit_block.ml b/lib/bopkit_block/src/bopkit_block.ml index e1e09015..97f6b0cc 100644 --- a/lib/bopkit_block/src/bopkit_block.ml +++ b/lib/bopkit_block/src/bopkit_block.ml @@ -1,6 +1,5 @@ open! Base open! Stdio -open! Or_error.Let_syntax module Arity = struct type ('kind, 'signal) t = @@ -197,7 +196,7 @@ let run_line ~line = let output = create_output t.output_arity in - let%map () = + let%map.Or_error () = try let input = create_input t.input_arity ~line in t.f ~arguments ~input ~output; diff --git a/lib/bopkit_command/src/dune b/lib/bopkit_command/src/dune index 3e84ea6f..018037c5 100644 --- a/lib/bopkit_command/src/dune +++ b/lib/bopkit_command/src/dune @@ -14,8 +14,6 @@ -open Stdio -open - Or_error.Let_syntax - -open Cmdlang) (libraries base diff --git a/lib/bopkit_compiler/src/dune b/lib/bopkit_compiler/src/dune index 7f90d087..7189989f 100644 --- a/lib/bopkit_compiler/src/dune +++ b/lib/bopkit_compiler/src/dune @@ -12,8 +12,6 @@ -open Fpath_base -open - Or_error.Let_syntax - -open Cmdlang) (libraries appendable-list diff --git a/lib/bopkit_compiler/src/pass_includes.ml b/lib/bopkit_compiler/src/pass_includes.ml index c49c4d61..3b7a0d78 100644 --- a/lib/bopkit_compiler/src/pass_includes.ml +++ b/lib/bopkit_compiler/src/pass_includes.ml @@ -1,5 +1,3 @@ -open! Or_error.Let_syntax - let find_distribution_file ~path ~loc = Bopkit_sites.Sites.stdlib |> List.find_map ~f:(fun stdlib_directory -> diff --git a/lib/bopkit_process_interpreter/src/bopkit_process_interpreter.ml b/lib/bopkit_process_interpreter/src/bopkit_process_interpreter.ml index a38eafe3..58b9db9b 100644 --- a/lib/bopkit_process_interpreter/src/bopkit_process_interpreter.ml +++ b/lib/bopkit_process_interpreter/src/bopkit_process_interpreter.ml @@ -1,5 +1,3 @@ -open! Or_error.Let_syntax - let execute_instruction ~(break : unit Or_error.t With_return.return) ~architecture @@ -12,7 +10,7 @@ let execute_instruction let st = match In_channel.(input_line stdin) with | Some line -> line - | None -> break.return (return ()) + | None -> break.return (Or_error.return ()) in let input_length = String.length st in let expected_length = architecture * p in @@ -52,7 +50,7 @@ let execute_code ~interpreted_code:{ Interpreted_code.architecture; memory; code Array.iter code ~f:(fun instruction -> execute_instruction ~break ~architecture ~memory ~instruction) done; - return ()) + Or_error.return ()) ;; let run_program ~architecture ~program = diff --git a/lib/bopkit_process_interpreter/src/interpreted_code.ml b/lib/bopkit_process_interpreter/src/interpreted_code.ml index f267ea1e..f649cb46 100644 --- a/lib/bopkit_process_interpreter/src/interpreted_code.ml +++ b/lib/bopkit_process_interpreter/src/interpreted_code.ml @@ -1,5 +1,3 @@ -open! Or_error.Let_syntax - module Address = struct type t = { memory_index : int } [@@deriving equal, sexp_of] end diff --git a/lib/bopkit_process_interpreter/src/operator.ml b/lib/bopkit_process_interpreter/src/operator.ml index 49f9d338..39accb68 100644 --- a/lib/bopkit_process_interpreter/src/operator.ml +++ b/lib/bopkit_process_interpreter/src/operator.ml @@ -1,5 +1,3 @@ -open! Or_error.Let_syntax - type t = { operator_name : Bopkit_process.Operator_name.t ; arity : int @@ -18,7 +16,7 @@ let sexp_of_t { operator_name; arity; compute = _ } = let check_input_length ~input ~expected_length = let length = Array.length input in if expected_length = length - then return () + then Or_error.return () else Or_error.error_s [%sexp "Unexpected input length", { expected_length : int; length : int }] @@ -26,7 +24,7 @@ let check_input_length ~input ~expected_length = let unary ~operator_name ~compute = let compute ~operands:input = - let%bind () = check_input_length ~input ~expected_length:2 in + let%bind.Or_error () = check_input_length ~input ~expected_length:2 in compute ~dst:input.(0) input.(1) in { operator_name; arity = 1; compute } @@ -34,7 +32,7 @@ let unary ~operator_name ~compute = let binary ~operator_name ~compute = let compute ~operands:input = - let%bind () = check_input_length ~input ~expected_length:3 in + let%bind.Or_error () = check_input_length ~input ~expected_length:3 in compute ~dst:input.(0) input.(1) input.(2) in { operator_name; arity = 2; compute } @@ -70,13 +68,13 @@ let primitives : Env.t Lazy.t = let operator_name = Bopkit_process.Operator_name.of_string operator_name in unary ~operator_name ~compute:(fun ~dst a -> f ~dst a; - return ()) + Or_error.return ()) in let b operator_name f : t = let operator_name = Bopkit_process.Operator_name.of_string operator_name in binary ~operator_name ~compute:(fun ~dst a b -> f ~dst a b; - return ()) + Or_error.return ()) in lazy ([ u "not" p_not diff --git a/lib/bopkit_simulator/src/dune b/lib/bopkit_simulator/src/dune index 0df85e37..2636ac5b 100644 --- a/lib/bopkit_simulator/src/dune +++ b/lib/bopkit_simulator/src/dune @@ -12,8 +12,6 @@ -open Stdio -open - Or_error.Let_syntax - -open Cmdlang) (libraries base diff --git a/project/visa/lib/visa_assembler/src/visa_assembler.ml b/project/visa/lib/visa_assembler/src/visa_assembler.ml index e7ef229f..7a957219 100644 --- a/project/visa/lib/visa_assembler/src/visa_assembler.ml +++ b/project/visa/lib/visa_assembler/src/visa_assembler.ml @@ -1,5 +1,3 @@ -open! Or_error.Let_syntax - module Assembly_construct = struct type t = | Label_introduction of { label : Visa.Label.t Loc.Txt.t } diff --git a/project/visa/lib/visa_dsl/test/test__visa_dsl.ml b/project/visa/lib/visa_dsl/test/test__visa_dsl.ml index d276857b..5fd28dfd 100644 --- a/project/visa/lib/visa_dsl/test/test__visa_dsl.ml +++ b/project/visa/lib/visa_dsl/test/test__visa_dsl.ml @@ -1,5 +1,3 @@ -open! Or_error.Let_syntax - (* $MDX part-begin=program *) let loop () : Visa.Program.t = Visa_dsl.program (fun t -> diff --git a/project/visa/lib/visa_simulator/src/code.ml b/project/visa/lib/visa_simulator/src/code.ml index b30805e3..0a29175c 100644 --- a/project/visa/lib/visa_simulator/src/code.ml +++ b/project/visa/lib/visa_simulator/src/code.ml @@ -1,5 +1,3 @@ -open! Or_error.Let_syntax - module Statement = struct type t = { labels : Visa.Label.t Loc.Txt.t list diff --git a/project/visa/lib/visa_simulator/src/memory.ml b/project/visa/lib/visa_simulator/src/memory.ml index 8b1dbf15..b6042ca3 100644 --- a/project/visa/lib/visa_simulator/src/memory.ml +++ b/project/visa/lib/visa_simulator/src/memory.ml @@ -1,5 +1,3 @@ -open! Or_error.Let_syntax - module Ram = struct type t = int Hashtbl.M(Int).t @@ -79,15 +77,13 @@ let load t ~address ~register_name = let store t ~register_name ~address = let address = Visa.Address.to_int address in let value = register_value t ~register_name in - Hashtbl.set t.memory ~key:address ~data:value; - return () + Hashtbl.set t.memory ~key:address ~data:value ;; let write t ~register_name ~address = let address = Visa.Address.to_int address in let value = register_value t ~register_name in - Output_device.set t.output_device ~address ~value; - return () + Output_device.set t.output_device ~address ~value ;; let add t = diff --git a/project/visa/lib/visa_simulator/src/memory.mli b/project/visa/lib/visa_simulator/src/memory.mli index 90444814..f661df8a 100644 --- a/project/visa/lib/visa_simulator/src/memory.mli +++ b/project/visa/lib/visa_simulator/src/memory.mli @@ -30,20 +30,12 @@ val load_value : t -> value:int -> register_name:Visa.Register_name.t -> unit (** [store t ~register_name ~address] is the opposite operation of [load] - it will write to memory at the given address the value currently contained in the register whose name is supplied. *) -val store - : t - -> register_name:Visa.Register_name.t - -> address:Visa.Address.t - -> unit Or_error.t +val store : t -> register_name:Visa.Register_name.t -> address:Visa.Address.t -> unit (** [write t ~register_name ~address] is like [store] but writes to the output device. It will write to the output device at the given address the value currently contained in the register whose name is supplied. *) -val write - : t - -> register_name:Visa.Register_name.t - -> address:Visa.Address.t - -> unit Or_error.t +val write : t -> register_name:Visa.Register_name.t -> address:Visa.Address.t -> unit (** {1 Operations on registers} diff --git a/project/visa/lib/visa_simulator/src/visa_simulator.ml b/project/visa/lib/visa_simulator/src/visa_simulator.ml index 94566d4a..1ae9e7e1 100644 --- a/project/visa/lib/visa_simulator/src/visa_simulator.ml +++ b/project/visa/lib/visa_simulator/src/visa_simulator.ml @@ -1,4 +1,3 @@ -open Or_error.Let_syntax module Code = Code module Execution_stack = Execution_stack module Memory = Memory @@ -91,6 +90,7 @@ let rec increment_code_pointer (t : t) = ;; let execute_instruction t ~instruction = + let open Or_error.Let_syntax in match (instruction : Visa.Label.t Visa.Instruction.t) with | Nop -> return (increment_code_pointer t) | Add -> @@ -128,10 +128,10 @@ let execute_instruction t ~instruction = return true) else return (increment_code_pointer t) | Store { register_name; address } -> - let%bind () = Memory.store t.memory ~register_name ~address in + Memory.store t.memory ~register_name ~address; return (increment_code_pointer t) | Write { register_name; address } -> - let%bind () = Memory.write t.memory ~register_name ~address in + Memory.write t.memory ~register_name ~address; return (increment_code_pointer t) | Load_address { address; register_name } -> Memory.load t.memory ~address ~register_name; @@ -153,6 +153,7 @@ module Step_result = struct end let step (t : t) = + let open Or_error.Let_syntax in let environment = t.environment in let assembly_instruction = match Stack.top t.execution_stack.macro_frames with