separate numerical operations into int and float folders, changing names of exposed functions from stdlib
This commit is contained in:
parent
0956d7cc02
commit
99d1f15c9d
@ -44,4 +44,4 @@ Also take note of the fact that I typically compile everything with `-S` and `-O
|
|||||||
|
|
||||||
## The Core Library
|
## 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
6
lib/float.ml
Normal 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
7
lib/float.mli
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
val ( + ) : float -> float -> float
|
||||||
|
|
||||||
|
val ( - ) : float -> float -> float
|
||||||
|
|
||||||
|
val ( * ) : float -> float -> float
|
||||||
|
|
||||||
|
val ( / ) : float -> float -> float
|
@ -5,16 +5,16 @@ let printf = Printf.printf
|
|||||||
|
|
||||||
external ( |> ) : 'a -> ('a -> 'b) -> 'b = "%revapply"
|
external ( |> ) : 'a -> ('a -> 'b) -> 'b = "%revapply"
|
||||||
|
|
||||||
external ( + ) : int -> int -> int = "%addint"
|
external stdlib_plus_int : int -> int -> int = "%addint"
|
||||||
external ( - ) : int -> int -> int = "%subint"
|
external stdlib_minus_int : int -> int -> int = "%subint"
|
||||||
external ( * ) : int -> int -> int = "%mulint"
|
external stdlib_multiply_int : int -> int -> int = "%mulint"
|
||||||
external ( / ) : int -> int -> int = "%divint"
|
external stdlib_divide_int : int -> int -> int = "%divint"
|
||||||
external ( mod ) : int -> int -> int = "%modint"
|
external stdlib_mod_int : int -> int -> int = "%modint"
|
||||||
|
|
||||||
external ( +. ) : float -> float -> float = "%addfloat"
|
external stdlib_plus_float : float -> float -> float = "%addfloat"
|
||||||
external ( -. ) : float -> float -> float = "%subfloat"
|
external stdlib_minus_float : float -> float -> float = "%subfloat"
|
||||||
external ( *. ) : float -> float -> float = "%mulfloat"
|
external stdlib_multiply_float : float -> float -> float = "%mulfloat"
|
||||||
external ( /. ) : float -> float -> float = "%divfloat"
|
external stdlib_divide_float : float -> float -> float = "%divfloat"
|
||||||
|
|
||||||
external ( = ) : 'a -> 'a -> bool = "%equal"
|
external ( = ) : 'a -> 'a -> bool = "%equal"
|
||||||
external ( <> ) : 'a -> 'a -> bool = "%notequal"
|
external ( <> ) : 'a -> 'a -> bool = "%notequal"
|
||||||
|
8
lib/int.ml
Normal file
8
lib/int.ml
Normal 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
9
lib/int.mli
Normal 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
|
@ -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 =
|
let rec find_helper (f : 'a -> bool) (ls : 'a list) (index : int) : ('a * int) option =
|
||||||
match ls with
|
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
|
| [] -> None
|
||||||
|
|
||||||
let find (f : 'a -> bool) (ls : 'a list) : ('a * int) option =
|
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
|
if length = index then
|
||||||
acc
|
acc
|
||||||
else
|
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 =
|
let initialize (f : int -> 'a) (length : int) : 'a list =
|
||||||
initialize_helper f length 0 []
|
initialize_helper f length 0 []
|
@ -1,4 +1,3 @@
|
|||||||
open FromStdlib open Types
|
open FromStdlib open Types
|
||||||
|
|
||||||
|
|
||||||
let _ = printf "Hello, World\n"
|
let _ = printf "Hello, World\n"
|
@ -8,6 +8,9 @@ build:
|
|||||||
ocamlopt -S -O3 -nopervasives -c types.mli types.ml
|
ocamlopt -S -O3 -nopervasives -c types.mli types.ml
|
||||||
|
|
||||||
# the following files make up the core custom standard library code
|
# 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 functions.mli functions.ml
|
||||||
ocamlopt -S -O3 -nopervasives -c stack.mli stack.ml
|
ocamlopt -S -O3 -nopervasives -c stack.mli stack.ml
|
||||||
ocamlopt -S -O3 -nopervasives -c list.mli list.ml
|
ocamlopt -S -O3 -nopervasives -c list.mli list.ml
|
||||||
@ -21,7 +24,7 @@ build:
|
|||||||
ocamlopt -S -O3 -nopervasives -c main.mli main.ml
|
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
|
# 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 removes all except source files. autogenerated mli files are also removed.
|
||||||
clean:
|
clean:
|
||||||
|
@ -111,7 +111,7 @@ module RBTreeSet (M : SetSpecification) : RBTreeSet with type member = M.member
|
|||||||
match set with
|
match set with
|
||||||
| Leaf -> 0
|
| Leaf -> 0
|
||||||
| Branch (_, _, left, right) ->
|
| Branch (_, _, left, right) ->
|
||||||
cardinality left + cardinality right + 1
|
Int.(cardinality left + cardinality right + 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
(* Planned Functions:
|
(* Planned Functions:
|
||||||
|
@ -19,7 +19,7 @@ let empty : 'a stack = Empty
|
|||||||
let rec height_helper (height : int) (st : 'a stack) =
|
let rec height_helper (height : int) (st : 'a stack) =
|
||||||
match st with
|
match st with
|
||||||
| Empty -> height
|
| 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 =
|
let height (st : 'a stack) : int =
|
||||||
height_helper 0 st
|
height_helper 0 st
|
||||||
|
Loading…
Reference in New Issue
Block a user