Skip to content

Instantly share code, notes, and snippets.

@Invizory
Last active October 11, 2023 00:09
Show Gist options
  • Save Invizory/7104c5d1b3897b230d85a9388546375b to your computer and use it in GitHub Desktop.
Save Invizory/7104c5d1b3897b230d85a9388546375b to your computer and use it in GitHub Desktop.
Практикум, 06.10.2023
open Base;
module type Equal = {
type t;
let (==): (t, t) => bool;
};
module type CompleteLattice = {
type t;
include Equal with type t := t;
let meet: (t, t) => t;
let join: (t, t) => t;
let top: t;
let bottom: t;
};
module type PartialOrder = {
type t;
let (<=): (t, t) => bool;
};
module NatDivisionLattice: {
type t = int;
include CompleteLattice with type t := t;
} = {
type t = int;
let rec gcd = (a, b) =>
switch (a, b) {
| (a, 0) => a
| (a, b) => gcd(b, a % b)
};
let lcm = (a, b) => a * b / gcd(a, b);
let meet = lcm;
let join = gcd;
let (==) = (==);
let top = 0;
let bottom = 1;
};
module PartialOrderFromCompleteLattice =
(L: CompleteLattice)
: {
type t = L.t;
include PartialOrder with type t := t;
} => {
type t = L.t;
let (<=) = (a, b) => L.(join(a, b) == a);
};
module NatDivisionOrder = PartialOrderFromCompleteLattice(NatDivisionLattice);
assert(NatDivisionOrder.(2 <= 4));
module Lift =
(L: CompleteLattice)
: {
include CompleteLattice;
let lift: L.t => t;
} => {
type t =
| Element(L.t)
| Bottom;
let lift = x => x |> Element;
let top = L.top |> Element;
let bottom = Bottom;
let (==) = (a, b) =>
switch (a, b) {
| (Element(a), Element(b)) => L.(a == b)
| (Bottom, Bottom) => true
| _ => false
};
let meet = (a, b) =>
switch (a, b) {
| (Element(a), Element(b)) => L.meet(a, b) |> Element
| (x, Bottom)
| (Bottom, x) => x
};
let join = (a, b) =>
switch (a, b) {
| (Element(a), Element(b)) => L.join(a, b) |> Element
| _ => Bottom
};
};
open Base;
module type S = {
type t;
let of_int: int => Result.t(t, [> | `NegativeQuantityError]);
let to_int: t => int;
let to_string: t => string;
};
module Quantity: S = {
type t = int;
let of_int =
fun
| x when x > 0 => Ok(x)
| _ => Error(`NegativeQuantityError);
let to_int = x => x;
let to_string = Int.to_string;
};
let q = Quantity.of_int(1);
let w =
switch (q) {
| Ok(x) => Quantity.to_int(x) - 100
| Error(`NegativeQuantityError) => (-1)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment