diff --git a/README.md b/README.md index ba5d004..3b22f85 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,4 @@ Also take note of the fact that I typically compile everything with `-S` and `-O ## The Core Library -One of the unfortunate consequences of the way OCaml's compilation works, is that there is a library called the core library, documented [here](https://ocaml.org/manual/core.html), which contains some definitions for types and exceptions, yet does not include the code from the stdlib that uses them. When compiling with the `-nopervasives` flag, this is still included but without the standard library. While this makes sense from the perspective of having some fundamental exceptions always available, having types like `list` included makes it very annoying when implemented a custom standard library. This quirk is why my library has no type definition for `list`, `bool`, `option`, etc. but still uses these types. +One of the unfortunate consequences of the way OCaml's compilation works, is that there is a library called the core library, documented [here](https://ocaml.org/manual/core.html), which contains some definitions for types and exceptions, yet does not include the code from the stdlib that uses them. When compiling with the `-nopervasives` flag, this is still included but without the standard library. While this makes sense from the perspective of having some fundamental exceptions always available, having types like `list` included makes it very annoying when implemented a custom standard library. This quirk is why my library has no type definition for `list`, `bool`, `option`, etc. but still uses these types. \ No newline at end of file diff --git a/lib/float.ml b/lib/float.ml new file mode 100644 index 0000000..7decf01 --- /dev/null +++ b/lib/float.ml @@ -0,0 +1,6 @@ +open FromStdlib + +let ( + ) a b = stdlib_plus_float a b +let ( - ) a b = stdlib_minus_float a b +let ( * ) a b = stdlib_multiply_float a b +let ( / ) a b = stdlib_divide_float a b diff --git a/lib/float.mli b/lib/float.mli new file mode 100644 index 0000000..d7b1341 --- /dev/null +++ b/lib/float.mli @@ -0,0 +1,7 @@ +val ( + ) : float -> float -> float + +val ( - ) : float -> float -> float + +val ( * ) : float -> float -> float + +val ( / ) : float -> float -> float diff --git a/lib/fromStdlib.ml b/lib/fromStdlib.ml index 2419518..8cfacc4 100644 --- a/lib/fromStdlib.ml +++ b/lib/fromStdlib.ml @@ -5,16 +5,16 @@ let printf = Printf.printf external ( |> ) : 'a -> ('a -> 'b) -> 'b = "%revapply" -external ( + ) : int -> int -> int = "%addint" -external ( - ) : int -> int -> int = "%subint" -external ( * ) : int -> int -> int = "%mulint" -external ( / ) : int -> int -> int = "%divint" -external ( mod ) : int -> int -> int = "%modint" +external stdlib_plus_int : int -> int -> int = "%addint" +external stdlib_minus_int : int -> int -> int = "%subint" +external stdlib_multiply_int : int -> int -> int = "%mulint" +external stdlib_divide_int : int -> int -> int = "%divint" +external stdlib_mod_int : int -> int -> int = "%modint" -external ( +. ) : float -> float -> float = "%addfloat" -external ( -. ) : float -> float -> float = "%subfloat" -external ( *. ) : float -> float -> float = "%mulfloat" -external ( /. ) : float -> float -> float = "%divfloat" +external stdlib_plus_float : float -> float -> float = "%addfloat" +external stdlib_minus_float : float -> float -> float = "%subfloat" +external stdlib_multiply_float : float -> float -> float = "%mulfloat" +external stdlib_divide_float : float -> float -> float = "%divfloat" external ( = ) : 'a -> 'a -> bool = "%equal" external ( <> ) : 'a -> 'a -> bool = "%notequal" diff --git a/lib/int.ml b/lib/int.ml new file mode 100644 index 0000000..f8e779b --- /dev/null +++ b/lib/int.ml @@ -0,0 +1,8 @@ +open FromStdlib + +let ( + ) a b = stdlib_plus_int a b +let ( - ) a b = stdlib_minus_int a b +let ( * ) a b = stdlib_multiply_int a b +let ( / ) a b = stdlib_divide_int a b +let ( mod ) a b = stdlib_mod_int a b + diff --git a/lib/int.mli b/lib/int.mli new file mode 100644 index 0000000..1be553c --- /dev/null +++ b/lib/int.mli @@ -0,0 +1,9 @@ +val ( + ) : int -> int -> int + +val ( - ) : int -> int -> int + +val ( * ) : int -> int -> int + +val ( / ) : int -> int -> int + +val ( mod ) : int -> int -> int \ No newline at end of file diff --git a/lib/list.ml b/lib/list.ml index 186d5a4..76d1940 100644 --- a/lib/list.ml +++ b/lib/list.ml @@ -130,7 +130,7 @@ let filter_rev_tr (f : 'a -> bool) (ls : 'a list) : 'a list = let rec find_helper (f : 'a -> bool) (ls : 'a list) (index : int) : ('a * int) option = match ls with - | x :: xs -> if f x then Some (x, index) else find_helper f xs (1 + index) + | x :: xs -> if f x then Some (x, index) else find_helper f xs Int.(1 + index) | [] -> None let find (f : 'a -> bool) (ls : 'a list) : ('a * int) option = @@ -140,7 +140,7 @@ let rec initialize_helper (f : int -> 'a) (length : int) (index : int) (acc : 'a if length = index then acc else - initialize_helper f length (index + 1) ((f index) :: acc) + initialize_helper f length Int.(index + 1) ((f index) :: acc) let initialize (f : int -> 'a) (length : int) : 'a list = initialize_helper f length 0 [] \ No newline at end of file diff --git a/lib/main.ml b/lib/main.ml index 6af1112..7ac0171 100644 --- a/lib/main.ml +++ b/lib/main.ml @@ -1,4 +1,3 @@ open FromStdlib open Types - let _ = printf "Hello, World\n" \ No newline at end of file diff --git a/lib/makefile b/lib/makefile index ab77285..f85bcb8 100644 --- a/lib/makefile +++ b/lib/makefile @@ -8,6 +8,9 @@ build: ocamlopt -S -O3 -nopervasives -c types.mli types.ml # the following files make up the core custom standard library code + ocamlopt -S -O3 -nopervasives -c int.mli int.ml + ocamlopt -S -O3 -nopervasives -c float.mli float.ml + ocamlopt -S -O3 -nopervasives -c float.mli float.ml ocamlopt -S -O3 -nopervasives -c functions.mli functions.ml ocamlopt -S -O3 -nopervasives -c stack.mli stack.ml ocamlopt -S -O3 -nopervasives -c list.mli list.ml @@ -21,7 +24,7 @@ build: ocamlopt -S -O3 -nopervasives -c main.mli main.ml # after all files are individually compiled with -nopervasives, this is compiled with it so that fromStdlib has the necessary linking - ocamlopt -S -O3 fromStdlib.cmx types.cmx functions.cmx stack.cmx list.cmx map.cmx queue.cmx set.cmx tree.cmx main.cmx -o program + ocamlopt -S -O3 fromStdlib.cmx types.cmx int.cmx float.cmx functions.cmx stack.cmx list.cmx map.cmx queue.cmx set.cmx tree.cmx main.cmx -o program # clean removes all except source files. autogenerated mli files are also removed. clean: diff --git a/lib/set.ml b/lib/set.ml index 4a7b2c7..57ef253 100644 --- a/lib/set.ml +++ b/lib/set.ml @@ -111,7 +111,7 @@ module RBTreeSet (M : SetSpecification) : RBTreeSet with type member = M.member match set with | Leaf -> 0 | Branch (_, _, left, right) -> - cardinality left + cardinality right + 1 + Int.(cardinality left + cardinality right + 1) end (* Planned Functions: diff --git a/lib/stack.ml b/lib/stack.ml index c0982f7..53dfa4d 100644 --- a/lib/stack.ml +++ b/lib/stack.ml @@ -19,7 +19,7 @@ let empty : 'a stack = Empty let rec height_helper (height : int) (st : 'a stack) = match st with | Empty -> height - | Stacked (x, xs) -> height_helper (height + 1) xs + | Stacked (x, xs) -> height_helper (Int.(height + 1)) xs let height (st : 'a stack) : int = height_helper 0 st