separate numerical operations into int and float folders, changing names of exposed functions from stdlib

This commit is contained in:
Aaron Manning 2021-12-18 20:20:30 +11:00 committed by aaron-jack-manning
parent 0956d7cc02
commit 99d1f15c9d
11 changed files with 48 additions and 16 deletions

View File

@ -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.

6
lib/float.ml Normal file
View File

@ -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

7
lib/float.mli Normal file
View File

@ -0,0 +1,7 @@
val ( + ) : float -> float -> float
val ( - ) : float -> float -> float
val ( * ) : float -> float -> float
val ( / ) : float -> float -> float

View File

@ -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"

8
lib/int.ml Normal file
View File

@ -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

9
lib/int.mli Normal file
View File

@ -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

View File

@ -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 []

View File

@ -1,4 +1,3 @@
open FromStdlib open Types
let _ = printf "Hello, World\n"

View File

@ -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:

View File

@ -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:

View File

@ -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