so i wanted to learn some category theory.

opened up a textbook on one side, agda on the other, and started trying to formalize the concepts and proofs

but i hit a bug in how agda’s pretty printer behaves. one i couldn’t easily work around

so i decided i’ll finally start contributing to agda and try to fix it myself

started poking around, accidentally figured out a different bug that I previously thought wasn’t worth looking into

but okay, gonna keep digging into the bug that actually led me down this path.

soon i found myself wanting to take a look at how agda’s AST looks in practice in one specific case, only to find that there isn’t really a way to do that, because haskell’s Show prints way too much information, all in one line, filling the entire terminal screen with mostly irrelevant info

so i did the natural thing and started writing a pretty printer for this purpose

eventually i got annoyed that each time i compiled agda, it would spend a good 30 seconds doing some kind of build system bookkeeping

like, i measured. a build with the entire cache fresh already would take 30 fucking seconds

as 30 seconds is enough time for me to start scrolling through fedi, i decided i need to look into this

so i started poking around the surprisingly complex makefile, learning more than i ever wanted to know about make

finally found the culprit: in a maze of includes, recursive make invocations and variable expansion, various variations of stack –dest-dir would get called like. way too many times during the build. and each of those would take 750ms to initialize, print the path to the build directory and exit

sigh, okay. i guess i’ll need to optimize the makefile a bit

managed to get it down to 10s for a do-nothing build. scoured the internet looking for a feature of make that would be useful to bring it down further

didn’t find it. as far as i can tell, “expand lazily, but then cache it” is not a behavior that’s available in gnu make, despite the absolute abundance of features they added on top of what posix specifies

now, thankfully i know better than to attempt upstreaming a feature into gnu make. and anyway, in this case the good folks at stack overflow gave me a hint on what absolute crimes i could commit in that makefile to make it go faster

but while i was contemplating my plan, i wondered: why does stack take 750ms to print a path in the first place?

after execsnoop and strace didn’t give me any hints, i decided to try using the linux profiler “perf”, which i’m sure you know is a master class in UX design

(it is not)

so i open the “perf report” and see a bunch of addresses for which i don’t have debuginfo. suspiciously small addresses too, like 0x5ddc

i start to notice the feeling of despair creeping up on me, but decide to persevere for at least a little bit more

i choose the hottest sample on the report and select “Annotate 00…005ddc”

now, i want you to consider that what i saw next was enough to get me to write out this entire rant. take a moment to internalize that.

what batshit insane assembly did i see? what could possibly have been this upsetting?

“Press ‘h’ for help on key bindings”, it said. followed immediately by “erf: Segmentation fault”, that’s right, erf, without the p.

then came a backtrace. one that listed the function name for each stack frame, as well as the source code file in which it were defined.

but the line number of all these entries is fucking 0 for some reason, as if to mock me

like, it’s not like i cared at this point where exactly the segfault happened, because even i do have my limits

but the computer apparently decided to find one more place in which it could subtly signal that there’s a nugget of insanity lurking within, to really get that eye twitchin’

and all i wanted was to learn some category theory

@mei I wish this had happened to me when I learned category theory 🙃
@jdw oh god why oh why would you want this to happen
@mei I guess it would imply that I'm really good with computers!
@jdw *monkey's paw curls*
@mei using computers is really a test of tenacity sometimes

@mei But, on the plus side, you were saved from learning category theory!

Temporarily at least. Sadly, there does not appear to be any permanent prevention.

@mei this sounds like the category equivalent of that Malcolm in the middle clip

"Hal, did you formalize your category theorems yet"
"WHAT DOES IT LOOK LIKE IM DOING"

@mei fwiw i just

cabal v2-build --ghc-options=-O0 --builddir=dist-newstyle -fdebug -- agda
# or
cabal v2-build --project-file=cabal.project.tc --builddir=dist-no-code
# or
cabal v2-build --ghc-options="-O2 -j" -foptimise-heavily --builddir=dist-optimise-heavily -fdebug -- agda

depending on what type of build i need

@amy i can't hear you over the sound of this weird SaaS debugger i'm trying out /hj

(i have little to no experience with haskell build tools so this might come in handy later)

(though, maybe this should also be in HACKING.md :P)
hacking.md

One daily markdown page: hacking.md

@mei this story has got everything – the missing p is just a cherry on top

But yeah I guess the depth of the rabbit holes you find yourself in is directly proportional to your knowledge, and hubris – I think I would've backed the hell out the moment I saw the giant Makefile

@mei not that this unfucks things, but there's a prettyprinting haskell lib available and you can teach ghci to call it on output as well as call it on whatever you got from show yourself

(but I don't know the name off the top of my head and we'd both be playing "find the google search term")

@flippac @mei it's just pretty-show right
@flippac @mei i guess nothing stops one from doing reportSLn "abstocon.whatever" 60 $ ppShow (killRange blah) other than us not already having pretty-show but pure haskell deps are completely fine to pull in if they build with ghc 9.whatever is the oldest we support.
@amy @mei hackage isn't testing it with anything that recent - it's not been updated for six years, but then why would it need to be?
@amy @flippac hmm, killRange would've been useful to know about (in general the thing I've missed the most in my foray into the Agda codebase is some equivalent of https://rustc-dev-guide.rust-lang.org/, honestly...)
Getting Started - Rust Compiler Development Guide

A guide to developing the Rust compiler (rustc)

@mei @flippac yeah. this comes up often whenever we meet but it is a fundamentally unexciting thing to work on and nobody's getting paid to work on it so just like editor integration it's probably never gonna get done because each developer always has more interesting things to work on
@amy @flippac what do you think is the chance that it'll get merged if I open a PR somewhere in that direction at some point?
@mei @flippac i mean if it's helpful i wouldn't see a reason not to fight for it. so you might have my sword. but it might be worth keeping in mind that generally im trying to move the language docs away from the sorta half-assed tutorial style thats hard to extend and correct that they're in now
@amy and we thank you for it ❤️
@amy @flippac soooo i gave `ppShow . killRange` a try and it took four and a half minutes to pretty-print the AST for the short 20-line test case I'm looking at 😅

I'll continue poking at the bespoke pretty-printer I think :3
@mei @amy Ouch! Fair enough: sorry it was a bad-as-in-slow rec
@flippac @amy to be fair, I think the data structure needs more pruning than just killRange to avoid printing lots of redundant info...
@mei @flippac probably changing the Show QName instance to show = show . pretty . nameConcrete . qnameName is the next thing to do
@amy @mei that's the one, yeah - and just having it in your own ghci setup (and perhaps global modules so you can :m + it) is a big help

@flippac @mei well, when i try to run agda in ghci it usually segfaults

in fact basically any nonstandard way of running agda segfaults. nonmoving gc, profiling if you're unlucky. the ghc runtime hates us

@flippac @mei i don't hold it against it. it's very much mutual
@amy @flippac uhhhhh wait what. hold on

how does *that* even happen

like, is the haskell runtime this fragile or is agda doing some absolutely horrendous crimes under the hood
@mei @flippac none of our own but maybe happy or alex are. i don't know. i just don't bother caring about this
@mei @flippac ive never really needed anything other than basic heap profiling because whenever im optimising a part of agda it is by replacing some brain-dead piece of code that predates my existence on this planet with something cleanroom, and not staring at flamegraphs
@mei @flippac my caring about compilers budget is already fully spent on agda itself
@mei As a mathematician who uses category theory professionally I can promise you that it's a lot easier than all this computer stuff you were dealing with!
@oantolin yeah, but doing math in a proof assistant helps trick my brain into paying attention, so 🙃

@mei

> so i did the natural thing and started writing a pretty printer for this purpose

TBH, what I usually do is C&P those outputs in an editor and do the pprinting it myself, like those people that read texts on computers by selecting it.