flan/DISCUSS.md

154 lines
9.9 KiB
Markdown

# Let's discuss
Open questions, raised and deliberately not answered yet. Nothing here is a decision or a task. Each entry is the
question as asked, plus what is already known in this repo that bears on it — so the investigation starts from what
exists rather than from scratch.
Settled decisions live in `NEXT.md`. Reasons for what already exists live in `BUILT.md`.
---
## 1. `i`, the inspector, and the frame it cannot see
Two things got conflated here and they should be separated.
**What the inspector already does.** Most of what was asked for is built. `flan-inspect` opens its own buffer, lays a
value's fields one per line, `RET` walks into one, `l` comes back, `g` re-reads. The renderer bounds its walk at depth 4
and span 8, and entering a field renders *that field* from depth 0 — so the elision moves with you rather than
truncating permanently. It is CIDER's inspector adapted, and the file says what the adaptation changed.
**What is actually missing** is detail on the leaves: a number shows in decimal only, with no hex and no binary, and a
pointer does not show its address. Purely additive, small, and worth doing.
**The real problem, and it is sharper than "a bug".** The locals listing renders from each frame's own slot addresses,
so it is frame-accurate. The inspector is built on a stack of **expressions** — going into a field means sending a
different expression (`(.pos b)` where the last was `b`), and `l` works by popping back to the previous one. That design
is forced: a Flan value has no header, the thunk that rendered it is `dlclose`d as soon as it returns, and there is no
heap to retain anything in, so nothing can be held server-side the way CIDER holds a JVM object.
The consequence is that `i` evaluates a name wherever the evaluator stands, **not in the frame being looked at**. On the
innermost frame that happens to be right. On any other it may resolve to a global, to a different binding, or fail —
with nothing saying so.
**An earlier suggestion in this conversation — "root the inspector at the slot's address" — does not work**, and the
reason is worth keeping: an address is not an expression, so the first `RET` has nothing to build the next expression
from and navigation dies at step one. Recorded because it is the obvious fix and it is wrong.
So the options are genuinely three, and none is free:
1. **Teach the program to evaluate an expression relative to a frame.** The most useful and the most work: the frame's
slots would have to be in scope for a compiled thunk, which means the daemon building a thunk whose free names bind
to that frame's addresses. It would also fix `C-x C-e` while stopped, which has the same blindness.
2. **Give the inspector a second rooting mode** — an address root that can still walk, by carrying a type alongside the
address and stepping to a field's address rather than to a sub-expression. Navigation then works, but the two modes
have different capabilities and `l` has to cross between them.
3. **Refuse `i` outside the innermost frame**, honestly and by name. Cheapest, and it gives up the feature exactly where
it is most wanted, since the innermost frame is the one already fully visible.
## 2. Annotating the IR and the disassembly with the source
The compiler should interleave the originating Flan expression into both the emitted LLVM IR and the disassembly
listing.
What already exists: `emit.ml` writes `.ll` as text, so a comment costs nothing and cannot break anything. Every typed IR
node carries a `Loc.t`. DWARF is emitted under `--debug`, with a line table naming the `.flan` file, and the disassembly
listing already rebases addresses and annotates cells, calls and branch targets. So both halves have the information
already; what is missing is the interleaving.
The IR half is nearly free. The disassembly half is more interesting and more valuable — it is what would let you see
what one line of Flan actually costs, including the indirection cell a dev build puts on every cross-function call.
## 3. `def`, `defvar`, `defconst`
There is no `def`. There is `defvar` (mutable global) and `defconst` (compile-time constant, folded).
The proposal: a `def` that is mutable, where `defvar` only rewrites the value if the variable is *new* — Common Lisp's
actual `defvar` semantics, where re-evaluating a `defvar` deliberately does not clobber a value you have been building up
at runtime. That distinction matters much more here than in most languages, because `C-c C-k` on a whole buffer
re-evaluates every top-level form against a *running* program, and today that resets state you may have spent a session
accumulating.
`defconst` under redefinition is the unclear one, as noted — it is folded into its use sites, so changing one is closer to
a recompile than to an assignment.
## 4. Structural typing, row polymorphism, anonymous structs
Worth noting before this is scoped: `types.ml` already does **structural equality** on resolved types, and a Flan struct
is exactly its C layout with no header or tag word. So the representation is already structural; what is nominal is the
*checking*, not the data.
The questions this opens: whether a function can take "any struct with an `x` and a `y`"; whether anonymous structs get a
spelling; how this interacts with the FFI, where the shim generates a C typedef per named struct; and how it interacts
with the planned managed classes, which are explicitly the *opposite* direction — identity and metadata rather than plain
layout.
## 5. A performant JS transpiler
Asked as a feasibility question. Bear in mind the web target already exists and ships real machine code via wasm, so this
is not the only route to a browser and the case for it needs stating: smaller artifacts, no wasm toolchain, debuggability
in browser devtools, or something else.
The hard parts are the ones the wasm target got for free from clang: the memory model (Flan is pointers and explicit
layout; JS is not), the FFI, and the fact that the whole raylib layer is C. A JS backend that cannot run raylib is a
different product from the one that can.
## 6. C interop as seamless as Zig's
Today: `declare-c` names one C function per line, and the compiler generates the wrapper, the typedefs and the flattened
declaration. 175 of them for raylib. **No header is ever read, deliberately** — which means nothing can check that a
declaration matches the real signature, and that is written down as trusted rather than guaranteed.
The proposal is to read the header, prefix a namespace, and get `rl/InitWindow` for free — plus possibly automatic
kebab-casing to `rl/init-window`.
This is a large change in kind, not just in size: it means a C parser or a libclang dependency in the build, and it
trades an explicit, checkable list for an implicit surface. Worth weighing against what `declare-c` already buys, which
is that a binding is one line and the wrapper is generated. The auto-kebab-case question is separable and much smaller —
and note the FFI currently keeps C's own spelling on purpose, so the mapping would need to be reversible.
## 7. Where `defclass` stands
Specified in `plan.org` and **deliberately not started** — its own last line says nothing happens until ordinary
`struct`, `Handle` and reload semantics work. Those have largely landed since that was written, so the gate may be closer
than the document assumes.
What it is meant to add: identity, runtime shape metadata, an implementation-defined representation, generic-function
dispatch, and live schema change with an explicit migration at a frame boundary — which is the answer to the one thing
redefinition still cannot do, changing a struct's layout while instances exist.
Three findings from an earlier review, recorded in `NEXT.md` and not yet in `plan.org`:
- **A generic function is a cell.** Adding a method from a later module is the same problem indirection cells already
solve, so the expensive half is built and tested.
- **The pool is not one storage option among three.** `migrate-instances` has to enumerate live instances, which a pool
behind a generational `Handle` gives by construction and the other two options do not.
- **Every layout version must stay resolvable** for as long as any instance holds it — the same rule as "nothing is ever
`dlclose`d".
Also open and related: whether a *condition* can be a class, which decides whether handler matching has one path or two.
`NEXT.md` records the argument; decision 4 there took the cheaper parent-link route for now and explicitly left real
inheritance possible later.
## 8. Watching variables
Raised while designing item 1, and deliberately separated from it.
Item 1 shows globals *per frame*, chosen by what the code in that frame references. That is the right default and it
cannot cover the case where the thing you care about is not named by the frame you are standing in — stopped deep in a
helper, still wanting to see the grid.
So: a way to say "always show me this", surviving a resume and the next break.
Questions it opens:
- **What can be watched.** A global is easy — it has a name and an address that does not move. A *local* is harder: it
belongs to a frame that is gone as soon as you resume, so "watch `y`" either means a different `y` every time or means
nothing. A watched expression — `(at velocity 40 12)` — is the most useful and the most expensive, since it has to be
compiled and run in the program each time, which is what `eval-expr` already does.
- **Where the list lives.** Per project, per session, or in the file. A watch list that vanishes when Emacs restarts is
one you stop using; one in the repo is one you accidentally commit.
- **When it updates.** Only on entering a break is cheap and probably enough. Live-updating while the program runs is a
different feature — closer to a HUD than a debugger — and worth not conflating.
- **Whether it belongs in the break buffer at all**, or in its own window that is useful while the program is *running*,
which is arguably where a game developer wants it.