Generic/Number.bruijn


# MIT License, Copyright (c) 2024 Marvin Borner
# generic number template, meant to be :input-ted
# assumes: zero, zero?, even?, gt?, zero?, eq?, add, inc, dec, compare-case

# TODO: also include std/Number more std/Math etc.? Requires solution for recursive imports
# then: give numeral systems unary id, match with index-unary
# or: have unified way of translating to ternary (then add hash function)

:import std/Combinator .
:import std/Logic .

# returns true if number is not zero
not-zero? not! ∘ zero? ⧗ Generic → Boolean

≠?‣ not-zero?

# returns true if two numbers are not equal
not-eq? not! ∘∘ eq? ⧗ Generic → Generic → Boolean

…≠?… not-eq?

# prefix for comparing functions
?‣ &eq?

# returns true if number is less than other number
lt? \gt? ⧗ Generic → Generic → Boolean

…<?… lt?

# returns true if number is less than or equal to other number
le? not! ∘∘ gt? ⧗ Generic → Generic → Boolean

…≤?… le?

# returns true if number is greater than or equal to other number
ge? \le? ⧗ Generic → Generic → Boolean

…≥?… ge?

# returns 1 if a>b, -1 if a<b and 0 if a=b
# also: spaceship operator
compare compare-case (+0) (+1) (-1) ⧗ Generic → Generic → Generic

…<=>… compare

<=>‣ &compare

# returns true if comparison result is equal (EQ)
c-eq? eq? (+0) ⧗ Generic → Generic

# returns true if comparison result is less than (LT)
c-lt? eq? (-1) ⧗ Generic → Generic

# returns true if comparison result is greater than (GT)
c-gt? eq? (+1) ⧗ Generic → Generic

# returns max number of two
max [[(1 ≤? 0) 0 1]] ⧗ Generic → Generic → Generic

# returns min number of two
min [[(1 ≤? 0) 1 0]] ⧗ Generic → Generic → Generic

# clamps a number between two numbers
clamp [[[min 1 (max 0 2)]]] ⧗ Generic → Generic → Generic

# returns true if the number is odd (remainder mod 2 == 1)
odd? ¬‣ ∘ even? ⧗ Generic → Boolean

# ================= #
# recursion schemes #
# ================= #

:import std/Option O

# TODO
fmap [[0 (O.some zero) (O.some ∘ 1)]] ⧗ (a → b) → (GenericF a) → (GenericF b)

fix O.map-or zero inc ⧗ (GenericF a) → (Fix (GenericF a))

unFix [=?0 O.none (O.some --0)] ⧗ (Fix (GenericF a)) → (GenericF a)

:input std/Generic/Schemes