From c8f6dd3df2fb04cf1ab285e3441cf414bc2cc787 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Thu, 12 Feb 2026 11:50:38 -0800 Subject: [PATCH] lec 06 modules??? --- lecture06/bstree.ml | 63 +++++++++++++++ lecture06/bstree.mli | 14 ++++ lecture06/bstree2.ml | 68 ++++++++++++++++ lecture06/functor/_build/_digests | 6 ++ lecture06/functor/_build/_log | 12 +++ lecture06/functor/_build/bstree.ml | 86 +++++++++++++++++++++ lecture06/functor/_build/bstree.ml.depends | 1 + lecture06/functor/_build/bstree.mli | 20 +++++ lecture06/functor/_build/bstree.mli.depends | 1 + lecture06/functor/bstree.ml | 86 +++++++++++++++++++++ lecture06/functor/bstree.mli | 20 +++++ lecture06/notes.ml | 0 lecture06/notes.txt | 8 ++ lecture06/queue.ml | 37 +++++++++ lecture06/two_list_queue.ml | 42 ++++++++++ 15 files changed, 464 insertions(+) create mode 100644 lecture06/bstree.ml create mode 100644 lecture06/bstree.mli create mode 100644 lecture06/bstree2.ml create mode 100644 lecture06/functor/_build/_digests create mode 100644 lecture06/functor/_build/_log create mode 100644 lecture06/functor/_build/bstree.ml create mode 100644 lecture06/functor/_build/bstree.ml.depends create mode 100644 lecture06/functor/_build/bstree.mli create mode 100644 lecture06/functor/_build/bstree.mli.depends create mode 100644 lecture06/functor/bstree.ml create mode 100644 lecture06/functor/bstree.mli create mode 100644 lecture06/notes.ml create mode 100644 lecture06/notes.txt create mode 100644 lecture06/queue.ml create mode 100644 lecture06/two_list_queue.ml diff --git a/lecture06/bstree.ml b/lecture06/bstree.ml new file mode 100644 index 0000000..880dddc --- /dev/null +++ b/lecture06/bstree.ml @@ -0,0 +1,63 @@ +type 'a t = Leaf | Node of 'a * 'a t * 'a t;; + +let rec size t = + match t with + | Leaf -> 0 + | Node (_, l, r) -> + 1 + size l + size r;; + +let rec height t = + match t with + | Leaf -> 0 + | Node (_, l, r) -> + 1 + max (height l) (height r);; + +let empty = Leaf;; + +let is_empty t = t = Leaf;; + +let rec insert x t = + match t with + | Leaf -> Node (x, Leaf, Leaf) + | Node (x', l, r) when x < x' -> + Node (x', insert x l, r) + | Node (x', l, r) when x > x' -> + Node (x', l, insert x r) + | _ -> t;; + +let of_list l = + List.fold_left (Fun.flip insert) Leaf l;; + +let rec mem x t = + match t with + | Leaf -> false + | Node (x', l, _) when x < x' -> + mem x l + | Node (x', _, r) when x > x' -> + mem x r + | _ -> true;; + +let rec largest t = + match t with + | Leaf -> failwith "largest: empty tree" + | Node (x, _, Leaf) -> x + | Node (_, _, r) -> largest r;; + +let rec smallest t = + match t with + | Leaf -> failwith "smallest: empty tree" + | Node (x, Leaf, _) -> x + | Node (_, l, _) -> smallest l;; + +let rec delete x t = + match t with + | Leaf -> Leaf + | Node (x', l, r) when x < x' -> + Node (x', delete x l, r) + | Node (x', l, r) when x > x' -> + Node (x', l, delete x r) + | Node (_, l, Leaf) -> l + | Node (_, Leaf, r) -> r + | Node (_, l, r) -> + let succ = largest l in + Node (succ, delete succ l, r);; diff --git a/lecture06/bstree.mli b/lecture06/bstree.mli new file mode 100644 index 0000000..a19ad1a --- /dev/null +++ b/lecture06/bstree.mli @@ -0,0 +1,14 @@ +(* type 'a t = Leaf | Node of 'a * 'a t * 'a t *) + +type 'a t (* for abstract data type *) + +val size : 'a t -> int +val height : 'a t -> int +val empty : 'a t +val is_empty : 'a t -> bool +val insert : 'a -> 'a t -> 'a t +val of_list : 'a list -> 'a t +val mem : 'a -> 'a t -> bool +val largest : 'a t -> 'a +val smallest : 'a t -> 'a +val delete : 'a -> 'a t -> 'a t diff --git a/lecture06/bstree2.ml b/lecture06/bstree2.ml new file mode 100644 index 0000000..8965897 --- /dev/null +++ b/lecture06/bstree2.ml @@ -0,0 +1,68 @@ +(* dont do this *) + + +module Bstree = struct +type 'a t = Leaf | Node of 'a * 'a t * 'a t;; + +let rec size t = + match t with + | Leaf -> 0 + | Node (_, l, r) -> + 1 + size l + size r;; + +let rec height t = + match t with + | Leaf -> 0 + | Node (_, l, r) -> + 1 + max (height l) (height r);; + +let empty = Leaf;; + +let is_empty t = t = Leaf;; + +let rec insert x t = + match t with + | Leaf -> Node (x, Leaf, Leaf) + | Node (x', l, r) when x < x' -> + Node (x', insert x l, r) + | Node (x', l, r) when x > x' -> + Node (x', l, insert x r) + | _ -> t;; + +let of_list l = + List.fold_left (Fun.flip insert) Leaf l;; + +let rec mem x t = + match t with + | Leaf -> false + | Node (x', l, _) when x < x' -> + mem x l + | Node (x', _, r) when x > x' -> + mem x r + | _ -> true;; + +let rec largest t = + match t with + | Leaf -> failwith "largest: empty tree" + | Node (x, _, Leaf) -> x + | Node (_, _, r) -> largest r;; + +let rec smallest t = + match t with + | Leaf -> failwith "smallest: empty tree" + | Node (x, Leaf, _) -> x + | Node (_, l, _) -> smallest l;; + +let rec delete x t = + match t with + | Leaf -> Leaf + | Node (x', l, r) when x < x' -> + Node (x', delete x l, r) + | Node (x', l, r) when x > x' -> + Node (x', l, delete x r) + | Node (_, l, Leaf) -> l + | Node (_, Leaf, r) -> r + | Node (_, l, r) -> + let succ = largest l in + Node (succ, delete succ l, r);; +end diff --git a/lecture06/functor/_build/_digests b/lecture06/functor/_build/_digests new file mode 100644 index 0000000..adde2c5 --- /dev/null +++ b/lecture06/functor/_build/_digests @@ -0,0 +1,6 @@ +"Rule: ocaml dependencies mli (%=bstree )": "!\134$\216+23\213\196T\181K\021\128\195\018" +"Resource: /home/flami/Projects/comp3958/lecture06/functor/bstree.mli": "\194\172\208\246\172^Y\171\1563@\144\158H\027l" +"Rule: ocaml: ml & cmi -> cmo (%=bstree )": "\172\028\140\193\160\2525\162\225\231@cE\2307\244" +"Resource: /home/flami/Projects/comp3958/lecture06/functor/bstree.ml": "\228\211>\161\t\1291\180\144c_ \189\147\236x" +"Rule: ocaml: mli -> cmi (%=bstree )": "\225aH6\244\021+9l\172\247\209 d\183\159" +"Rule: ocaml dependencies ml (%=bstree )": "\031$\199\007@\208\227\1694l\138\193r\227\203\171" diff --git a/lecture06/functor/_build/_log b/lecture06/functor/_build/_log new file mode 100644 index 0000000..0a357e2 --- /dev/null +++ b/lecture06/functor/_build/_log @@ -0,0 +1,12 @@ +### Starting build. +# Target: /home/flami/.opam/default/bin/ocamlc.opt -config, tags: { } +/home/flami/.opam/default/bin/ocamlc.opt -config +# Target: bstree.mli.depends, tags: { extension:mli, file:bstree.mli, ocaml, ocamldep, quiet } +/home/flami/.opam/default/bin/ocamldep.opt -modules bstree.mli > bstree.mli.depends # cached +# Target: bstree.cmi, tags: { byte, compile, extension:mli, file:bstree.mli, interf, ocaml, quiet } +/home/flami/.opam/default/bin/ocamlc.opt -c -o bstree.cmi bstree.mli # cached +# Target: bstree.ml.depends, tags: { extension:ml, file:bstree.ml, ocaml, ocamldep, quiet } +/home/flami/.opam/default/bin/ocamldep.opt -modules bstree.ml > bstree.ml.depends +# Target: bstree.cmo, tags: { byte, compile, extension:cmo, extension:ml, file:bstree.cmo, file:bstree.ml, implem, ocaml, quiet } +/home/flami/.opam/default/bin/ocamlc.opt -c -o bstree.cmo bstree.ml +# Compilation successful. diff --git a/lecture06/functor/_build/bstree.ml b/lecture06/functor/_build/bstree.ml new file mode 100644 index 0000000..cc25555 --- /dev/null +++ b/lecture06/functor/_build/bstree.ml @@ -0,0 +1,86 @@ +module type OrderedType = sig + type t + val compare : t -> t -> int +end + +module type S = sig + type elt + type t + + val size : t -> int + val height : t -> int + val empty : t + val is_empty : t -> bool + val insert : elt -> t -> t + val of_list : elt list -> t + val mem : elt -> t -> bool + val delete : elt -> t -> t +end + +module Make(Ord : OrderedType) = struct + type elt = Ord.t;; + type t = L | N of elt * t * t;; + + let rec size t = + match t with + | L -> 0 + | N (_, l, r) -> + 1 + size l + size r;; + + let rec height t = + match t with + | L -> 0 + | N (_, l, r) -> + 1 + max (height l) (height r);; + + let empty = L;; + + let is_empty t = t = L;; + + let rec insert x t = + match t with + | L -> N (x, L, L) + | N (x', l, r) when Ord.compare x x' < 0 -> + N (x', insert x l, r) + | N (x', l, r) when Ord.compare x x' > 0 -> + N (x', l, insert x r) + | _ -> t;; + + let of_list l = + List.fold_left (Fun.flip insert) L l;; + + let rec mem x t = + match t with + | L -> false + | N (x', l, _) when Ord.compare x x' < 0 -> + mem x l + | N (x', _, r) when Ord.compare x x' > 0 -> + mem x r + | _ -> true;; + + let rec largest t = + match t with + | L -> failwith "largest: empty tree" + | N (x, _, L) -> x + | N (_, _, r) -> largest r;; + + let rec smallest t = + match t with + | L -> failwith "smallest: empty tree" + | N (x, L, _) -> x + | N (_, l, _) -> smallest l;; + + let rec delete x t = + match t with + | L -> L + | N (x', l, r) when Ord.compare x x' < 0 -> + N (x', delete x l, r) + | N (x', l, r) when Ord.compare x x' > 0 -> + N (x', l, delete x r) + | N (_, L, L) -> L (* this does not need to be here, its a special case but ill leave it for clarity *) + | N (_, l, L) -> l + | N (_, L, r) -> r + | N (_, l, r) -> + let succ = largest l in + N (succ, delete succ l, r);; +end diff --git a/lecture06/functor/_build/bstree.ml.depends b/lecture06/functor/_build/bstree.ml.depends new file mode 100644 index 0000000..2c0eb27 --- /dev/null +++ b/lecture06/functor/_build/bstree.ml.depends @@ -0,0 +1 @@ +bstree.ml: Fun List diff --git a/lecture06/functor/_build/bstree.mli b/lecture06/functor/_build/bstree.mli new file mode 100644 index 0000000..54c82b4 --- /dev/null +++ b/lecture06/functor/_build/bstree.mli @@ -0,0 +1,20 @@ +module type OrderedType = sig + type t + val compare : t -> t -> int +end + +module type S = sig + type elt + type t + + val size : t -> int + val height : t -> int + val empty : t + val is_empty : t -> bool + val insert : elt -> t -> t + val of_list : elt list -> t + val mem : elt -> t -> bool + val delete : elt -> t -> t +end + +module Make(Ord: OrderedType) : S with type elt = Ord.t diff --git a/lecture06/functor/_build/bstree.mli.depends b/lecture06/functor/_build/bstree.mli.depends new file mode 100644 index 0000000..61be886 --- /dev/null +++ b/lecture06/functor/_build/bstree.mli.depends @@ -0,0 +1 @@ +bstree.mli: diff --git a/lecture06/functor/bstree.ml b/lecture06/functor/bstree.ml new file mode 100644 index 0000000..cc25555 --- /dev/null +++ b/lecture06/functor/bstree.ml @@ -0,0 +1,86 @@ +module type OrderedType = sig + type t + val compare : t -> t -> int +end + +module type S = sig + type elt + type t + + val size : t -> int + val height : t -> int + val empty : t + val is_empty : t -> bool + val insert : elt -> t -> t + val of_list : elt list -> t + val mem : elt -> t -> bool + val delete : elt -> t -> t +end + +module Make(Ord : OrderedType) = struct + type elt = Ord.t;; + type t = L | N of elt * t * t;; + + let rec size t = + match t with + | L -> 0 + | N (_, l, r) -> + 1 + size l + size r;; + + let rec height t = + match t with + | L -> 0 + | N (_, l, r) -> + 1 + max (height l) (height r);; + + let empty = L;; + + let is_empty t = t = L;; + + let rec insert x t = + match t with + | L -> N (x, L, L) + | N (x', l, r) when Ord.compare x x' < 0 -> + N (x', insert x l, r) + | N (x', l, r) when Ord.compare x x' > 0 -> + N (x', l, insert x r) + | _ -> t;; + + let of_list l = + List.fold_left (Fun.flip insert) L l;; + + let rec mem x t = + match t with + | L -> false + | N (x', l, _) when Ord.compare x x' < 0 -> + mem x l + | N (x', _, r) when Ord.compare x x' > 0 -> + mem x r + | _ -> true;; + + let rec largest t = + match t with + | L -> failwith "largest: empty tree" + | N (x, _, L) -> x + | N (_, _, r) -> largest r;; + + let rec smallest t = + match t with + | L -> failwith "smallest: empty tree" + | N (x, L, _) -> x + | N (_, l, _) -> smallest l;; + + let rec delete x t = + match t with + | L -> L + | N (x', l, r) when Ord.compare x x' < 0 -> + N (x', delete x l, r) + | N (x', l, r) when Ord.compare x x' > 0 -> + N (x', l, delete x r) + | N (_, L, L) -> L (* this does not need to be here, its a special case but ill leave it for clarity *) + | N (_, l, L) -> l + | N (_, L, r) -> r + | N (_, l, r) -> + let succ = largest l in + N (succ, delete succ l, r);; +end diff --git a/lecture06/functor/bstree.mli b/lecture06/functor/bstree.mli new file mode 100644 index 0000000..54c82b4 --- /dev/null +++ b/lecture06/functor/bstree.mli @@ -0,0 +1,20 @@ +module type OrderedType = sig + type t + val compare : t -> t -> int +end + +module type S = sig + type elt + type t + + val size : t -> int + val height : t -> int + val empty : t + val is_empty : t -> bool + val insert : elt -> t -> t + val of_list : elt list -> t + val mem : elt -> t -> bool + val delete : elt -> t -> t +end + +module Make(Ord: OrderedType) : S with type elt = Ord.t diff --git a/lecture06/notes.ml b/lecture06/notes.ml new file mode 100644 index 0000000..e69de29 diff --git a/lecture06/notes.txt b/lecture06/notes.txt new file mode 100644 index 0000000..77fafc7 --- /dev/null +++ b/lecture06/notes.txt @@ -0,0 +1,8 @@ +module / struct +module type / sig / interface + +open M +let opem M in .. +M.(...) + + diff --git a/lecture06/queue.ml b/lecture06/queue.ml new file mode 100644 index 0000000..2c46d26 --- /dev/null +++ b/lecture06/queue.ml @@ -0,0 +1,37 @@ +type 'a t = 'a list;; + +exception Empty + +let empty = [];; + +let is_empty q = q = [];; + +let enqueue x q = q @ [x];; + +let dequeue q = + match q with + | [] -> raise Empty + | _ :: xs -> xs;; + +let dequeue_opt q = + match q with + | [] -> None + | _ :: xs -> Some xs;; + +let front q = + match q with + | [] -> raise Empty + | x :: _ -> x;; + +let front_opt q = + match q with + | [] -> None + | x :: _ -> Some x;; + +let length q = + List.length q;; + +let of_list l = l;; + +let to_list q = q;; + diff --git a/lecture06/two_list_queue.ml b/lecture06/two_list_queue.ml new file mode 100644 index 0000000..48eff0c --- /dev/null +++ b/lecture06/two_list_queue.ml @@ -0,0 +1,42 @@ +(* queue is empty iff the first list is empty *) +type 'a t = 'a list * 'a list;; + +exception Empty;; + +let empty = ([], []);; + +let is_empty (l, _) = l = [];; + +let enqueue x (l1, l2) = + match l1 with + | [] -> [x], l2 + | _ -> (l1, x :: l2);; + +let dequeue (l1, l2) = + match l1 with + | [] -> raise Empty + | [_] -> (List.rev l2, []) + | _ :: xs -> (xs, l2);; + +let dequeue_opt q = + try + Some (dequeue q) + with + | Empty -> None;; + +let front (l, _) = + match l with + | [] -> raise Empty + | x :: _ -> x;; + +let front_opt (l, _) = + match l with + | [] -> None + | x :: _ -> Some x;; + +let of_list l = + List.fold_left (Fun.flip enqueue) empty l;; + +let to_list (l1, l2) = + l1 @ List.rev l2;; +