Skip to main content
Process journal of learning by Attila Vajda

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:

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:

--I'm confused, is this word soup?

πŸ› bruv




References #

I was inspired by the gibberish variables idea in Think Python. by Allen B. Downey