building R from scratch
skankin' R π· #
I have
class R (a : Type) where
zero : a
one : a
add : a β a β a
mul : a β a β a
and error messages
--It's maybe a bit like building an app, a code, a project, something, a portfolio project
Β I rebuilt R and sin x
#restorative
Before checking, pause 3 s, exhale, soften shoulders
β make this the actual goal.
structure FinancialState :=
(income : β)
(expenses : β)
(savings : β)
wow
e
and sin
are
growth π± & waves π
def proof {A B : Prop} (f : A β B) : A β B := f
structure Lin (V W : Type) := (map : V β W)
πΆtimbre:
bright β exp,
nasal β sin.s
def zork (r : R) (blip : r) := (neg := fun x => ???, inv := fun x => ???)
-- guess: zork blip -> (neg blip, inv blip)
π¨
def frotz(zo, bl):
return ("neg", -bl), ("inv", 1/bl)
# guess output?
πͺ΄
struct Q{int gl;}; struct Q goop(struct Q q){q.gl=-q.gl; return q;}
/* guess resulting q.gl */
π₯
def glorp {a} [R a] (x y : a) :=
(add x (neg y), mul (inv x) y)
-- guess: what does `glorp 4 2` produce in β?
βΎοΈ
def snarf(a,b):
return (a - b, b / a)
# guess: snarf(5,10)?
β©
--I definitely had an aha moment here, struggling with the first Lean4 more abstract definition, then seeing the Python one clarity ensued! π
π€πΏ Thatβs exactly the magic β¨ β kleine βAhaβ-Erlebnisse π turn foggy abstractions into clear shapes. Itβs like suddenly spotting the lighthouse πΌ while sailing at night β΅ β same waters, but now you see the route.
π Your R
is nearly ready, but youβll want:
- Fix type var: use
a
instead ofr
in field types. - Maybe add
sub
anddiv
as derived ops fromadd+neg
andmul+inv
. - Possibly include
eq
or decidable equality for comparisons.
add+neg
and mul+inv
are sub
, and div
, so cool! π
In my experience it is much nicer to learn this way, because I used to struggle to see these patterns in textbooks, and now while thinking about these puzzles, and components of constructing R
from scratch I remember more readily... (maybe the struggle to see these patterns through textbooks helps with now learning it easier? Like "Oh finally I have a means to learn these things...)
Β Like "Oh what do I need to be able to use exp
"?
Β Β I needR
, which is a Type
with add
, zero
, one
...
βWhat do I need for exp?β
π¦ You cracked it β a ring-like R
with add
, zero
, one
, mul
, neg
, inv
.
e
- Cracked β π
Β (exp still seems terribly complex for me (πβ¨ "YET"))
x^n/n! is very similar to sin x, are they the same function?
exp grows, sin waves back and forth
β
def floop (x : Float) (N : Nat) :=
List.range (N+1).sum (Ξ» n => x^n / Nat.factorial n)
# guess floop 1 3?
--Ξ» n => x^n, how is n changed, it ranges over, iterated over range(3)?
π€πΏ Exactly! Ξ» n => x^n / n! is applied for each n in List.range (N+1) = {0,1,2,3} here, then summed. So n iterates over 0 to N, producing terms in the series.
π΅ "Is this the categonified version?"
πββ¬β¨ Yes! The function Ξ» n => x^n / n! is a morphism from the discrete category of natural numbers (0 β 1 β 2 β ...) into your numeric type, then combined by colimit (sum).
Think:
- Objects: indices n
- Morphisms: ordering nβ€m
- Functor: sends nβ¦xn/n!
- Colimit = sum over all n β a categorial fold! π§Άβ΅οΈ
--I'm confused, is this word soup?
π bruv
References #
I was inspired by the gibberish variables idea in Think Python. by Allen B. Downey
- Previous: arrow only categories
- Next: Term Theory