caml-urm

A OCaml module for manipulating unlimited register machines

Commit
aa3dbde94b95453dad5f9f4af228420426fc8ce6
Parent
ad9425a4e3719f2412c18696523be0e008c69ea7
Author
Pablo <pablo-escobar@riseup.net>
Date

Removed unnecessary whitespace

Diffstat

5 files changed, 53 insertions, 54 deletions

Status File Name N° Changes Insertions Deletions
Modified README.md 2 1 1
Modified main.ml 30 15 15
Modified urm.1 11 5 6
Modified urm.ml 38 19 19
Modified urm.mli 26 13 13
diff --git a/README.md b/README.md
@@ -8,7 +8,7 @@ provided in [Ramin Naimi's URM
 simulator](https://sites.oxy.edu/rnaimi/home/URMsim.htm), which is in turn
 based on that of [Computability, An introduction to recursive function
 theory](http://libgen.li/file.php?md5=147d472c8ebc58b832a0d93d02c03f3a) by
-Nigel J.  Cutland. For example, the instruction set is given by: 
+Nigel J.  Cutland. For example, the instruction set is given by:
 
 * `T(n, m)` transfers the contents of the `n`-th register to `m`-th register.
 * `Z(n)` sets the value of the `n`-th register to zero.
diff --git a/main.ml b/main.ml
@@ -1,6 +1,6 @@
 open List
 
-let usage () = 
+let usage () =
   Printf.printf "USAGE: urm [-i ITERS] REGISTERS FILE\n";
   Printf.printf "See urm.1 for additional information\n"
 
@@ -15,20 +15,20 @@ let sequence (xs : 'a option list) : 'a list option =
     [regs] and the maximum number of allowed iterations set to [iters] *)
 let main iters regs file =
   let contents = really_input_string file (in_channel_length file) in
-  try 
-    let program = Urm.parse contents 
-    and regs_fn i = if i < Array.length regs then regs.(i) else 0 
+  try
+    let program = Urm.parse contents
+    and regs_fn i = if i < Array.length regs then regs.(i) else 0
     in
       match Urm.nexec iters program (Urm.of_registers regs_fn) with
-      | Some m -> 
-        let regs_strs = 
+      | Some m ->
+        let regs_strs =
           Urm.register m
             |> List.init (Array.length regs)
             |> List.map (Printf.sprintf "%d")
         in Printf.printf "%s\n" (String.concat " " regs_strs)
-      | None -> 
-        Printf.eprintf 
-          "ERROR: the input program did not halt after %d iterations\n" 
+      | None ->
+        Printf.eprintf
+          "ERROR: the input program did not halt after %d iterations\n"
           iters;
         Printf.eprintf "The input program may not halt\n";
         exit 1
@@ -41,15 +41,15 @@ let (iters, regs_str, filepath) : int * string * string =
     match int_of_string_opt iters_str with
     | Some n when n > 0 ->
       n
-    | Some n -> 
-      Printf.eprintf 
+    | Some n ->
+      Printf.eprintf
         "ERROR: negative number of maximum iterations provided: %d\n" n;
       Printf.eprintf "Expected a positive integers\n";
       usage ();
       exit 1
     | None ->
-      Printf.eprintf 
-        "ERROR: invalid number of maximum iterations provided: '%s'\n" 
+      Printf.eprintf
+        "ERROR: invalid number of maximum iterations provided: '%s'\n"
         iters_str;
       Printf.eprintf "Expected a positive integers\n";
       usage ();
@@ -64,7 +64,7 @@ let (iters, regs_str, filepath) : int * string * string =
       Printf.eprintf "ERROR: not enought arguments provided\n";
       usage ();
       exit 1
-    | _ -> 
+    | _ ->
       Printf.eprintf "ERROR: invalid arguments provided\n";
       usage ();
       exit 1
@@ -78,7 +78,7 @@ let regs =
     | Some regs ->
       regs
     | None ->
-      Printf.eprintf 
+      Printf.eprintf
         "ERROR: invalid initial values for the registers provided: '%s'\n"
         regs_str;
       Printf.eprintf "Expected a list of integers separated by spaces\n";
diff --git a/urm.1 b/urm.1
@@ -1,11 +1,11 @@
 '\" t
-.\"   _   _ _ __ _ __ ___  
-.\"  | | | | '__| '_ ` _ \ 
+.\"   _   _ _ __ _ __ ___
+.\"  | | | | '__| '_ ` _ \
 .\"  | |_| | |  | | | | | |
 .\"   \__,_|_|  |_| |_| |_|
-.\"                                     
+.\"
 .\" Unlimited Register Machine simulator
-.\" 
+.\"
 .\" Copyright (C) 2021 Pablo
 .\" Free use of this software is granted under the terms of the GPL-3.0 License
 .TH "MAN" "1" "2021-12-11" "\ \&" "\ \&"
@@ -37,8 +37,7 @@ A simple Unlimited Register Machine (URM) simulator
 .SH "OPTIONS"
 .sp
 \fB-i ITERS\fP
-.RS 4
-Sets the maximum number of iterations allowed to \fBITERS\fP: an arbitrary
+.RS 4 Sets the maximum number of iterations allowed to \fBITERS\fP: an arbitrary
 program may not halt for a given input and this option configures how much
 instructions is the machine allowed to run before failing with an error.
 Defaults to \fB1000\fP
diff --git a/urm.ml b/urm.ml
@@ -1,14 +1,14 @@
 open Array
 open Genlex
 
-type instruction = 
+type instruction =
   | T of int * int
   | Z of int
   | S of int
   | P of int
   | J of int * int * int
 
-type t = 
+type t =
   { instruction_pointer : int;
     registers : int -> int;
   }
@@ -22,7 +22,7 @@ let of_registers f =
 
 let zeros = of_registers (fun _ -> 0)
 
-let register m = m.registers 
+let register m = m.registers
 
 (** Executes the next instruction. Returns [Some _] is the instruction pointer
  points to a valid adress and [None] otherwise. *)
@@ -31,34 +31,34 @@ let step (program : instruction array) (m : machine) : machine option =
   else
     let updated =
       match program.(m.instruction_pointer) with
-      | T (r1, r2) -> 
-        { registers = 
+      | T (r1, r2) ->
+        { registers =
             (fun n -> if n == r2 then m.registers r1 else m.registers n);
           instruction_pointer = m.instruction_pointer + 1;
         }
-      | Z r -> 
+      | Z r ->
         { registers = (fun n -> if n == r then 0 else m.registers n);
           instruction_pointer = m.instruction_pointer + 1;
         }
-      | S r -> 
-        { registers = 
+      | S r ->
+        { registers =
             (fun n -> if n == r then 1 + (m.registers n) else m.registers n);
           instruction_pointer = m.instruction_pointer + 1;
         }
-      | P r -> 
-        { registers = 
+      | P r ->
+        { registers =
             (fun n -> if n == r then (m.registers n) - 1 else m.registers n);
           instruction_pointer = m.instruction_pointer + 1;
         }
-      | J (r1, r2, i) when m.registers r1 == m.registers r2 -> 
+      | J (r1, r2, i) when m.registers r1 == m.registers r2 ->
         (* [i - 1] is used so that the instruction count starts at 1 instead of
          0 *)
         if i > 0 then { m with instruction_pointer = i - 1 }
-        else 
+        else
           let e = Printf.sprintf "invalid jump address: J(%d, %d, %d)" r1 r2 i
           in raise (Invalid_argument e)
-      | J _ -> 
-        { m with instruction_pointer = m.instruction_pointer + 1 } 
+      | J _ ->
+        { m with instruction_pointer = m.instruction_pointer + 1 }
     in Some updated
 
 let rec exec program m =
@@ -67,12 +67,12 @@ let rec exec program m =
   | Some n -> exec program n
 
 let nexec max program m =
-  if max <= 0 
+  if max <= 0
   then raise (Invalid_argument "the number of clock-cycles must be positive ")
   else
     let rec exec_safe_tail m clock_cycles =
       if clock_cycles <= max
-      then 
+      then
         match step program m with
         | None -> Some { m with instruction_pointer = 0 }
         | Some n -> exec_safe_tail n (clock_cycles + 1)
@@ -103,11 +103,11 @@ let parse_instruction (i : token list) : instruction =
       J (r1, r2, i)
   | [ Kwd "J"; Kwd "("; Int _; Kwd ","; Int _; Kwd ","; Int i; Kwd ")" ] ->
       raise (Syntax_error (Printf.sprintf "invalid jump address: %d" i))
-  | _ -> 
+  | _ ->
       raise (Syntax_error "invalid syntax")
 
 let parse (s : string) : instruction array =
-    let f s = 
+    let f s =
       let s = String.trim s in
       if s = "" then None
       else
@@ -116,7 +116,7 @@ let parse (s : string) : instruction array =
             |> Stream.npeek 8
             |> parse_instruction
             |> Option.some
-    in 
+    in
       s |> String.split_on_char '\n'
         |> List.filter_map f
         |> Array.of_list
diff --git a/urm.mli b/urm.mli
@@ -1,10 +1,10 @@
-(** Unlimited register machines 
+(** Unlimited register machines
 
  This module provides various types and functions for manipulating unlimited
  register machines (URMs). This implementation is besed on the specification
- provided in Ramin Naimi's URM simulator, which is in turn based on that of 
+ provided in Ramin Naimi's URM simulator, which is in turn based on that of
  {i Computability, An introduction to recursive function theory } by Nigel J.
- Cutland. For example, the instruction set is given by: 
+ Cutland. For example, the instruction set is given by:
 
  - [T(n, m)] transfers the contents of the [n]-th register to [m]-th register.
  - [Z(n)] sets the value of the [n]-th register to zero.
@@ -21,20 +21,20 @@
  this implementation they store arbitrary integers. I have also added the [P]
  instruction to help users handle negative integers.
 
- @see <https://sites.oxy.edu/rnaimi/home/URMsim.htm> URM simulator. 
+ @see <https://sites.oxy.edu/rnaimi/home/URMsim.htm> URM simulator.
  @see <libgen.li/file.php?md5=147d472c8ebc58b832a0d93d02c03f3a> Computability,
  An introduction to recursive function theory. *)
 
 (** The type of instructions. *)
-type instruction = 
-  | T of int * int 
-  | Z of int 
-  | S of int 
+type instruction =
+  | T of int * int
+  | Z of int
+  | S of int
   | P of int
-  | J of int * int * int 
+  | J of int * int * int
 
 (** The type of all possible states of a URM. *)
-type t 
+type t
 
 (** An alias for {! t } *)
 type machine
@@ -50,10 +50,10 @@ val zeros : machine
 
 (** [exec m program] returns a machine whose register values are given by the
     register values of
-    [m] with updates corresponding to the execution of [program]. 
+    [m] with updates corresponding to the execution of [program].
 
     This function only returns when [program] actually halts. Please use {!
-    nexec } if you don't trust [program]. 
+    nexec } if you don't trust [program].
 
     @raise Invalid_argument if [program] attempts to jump to a non-positive
     integer. *)
@@ -64,7 +64,7 @@ val exec : instruction array -> machine -> machine
     clock-cycle to execute.
 
     [nexec n program] returns [Some _] if [program] halts in at most [n]
-    clock-cycles and [None] otherwise. 
+    clock-cycles and [None] otherwise.
 
     @raise Invalid_argument if [program] attempts to jump to a non-positive
     integer.  @raise Invalid_argument if [n] is not a positive integer. *)