flan/test/programs/pkgs/tree/tree.flan
Joseph Ferano d6fc15474b The count is length, so len is a name a program can have
The author: "I think I prefer length over len, because then I'll use len as
the variable name". One arm in check.ml, one row in the table beside it, and
every (len x) in lib, test, examples, vendor, spike, docs, web, emacs,
plan.org and NEXT.md rewritten.

Shadowing and builtin/ had already taken most of the sting out: a (defn len
...) was legal and won in its own file, and builtin/len reached past it. What
was left is that len was still a builtin — the defn earned a warning, and a
wrapper had to say builtin/ at every inner call. Now there is nothing under
the short name: len is an ordinary identifier in every position, which is
what (let [len (length xs)] ...) wants.

length takes over as shadowing's worked example rather than the feature
losing one. shadow-builtin.flan, builtin-qualified.flan, pkgs/shadowed and the
builtin/ rows in test_flan move to it and go on testing shadowing.

A call to a len nothing defines is answered where an unknown function is,
after every table and after the shadowing guard, so a program with its own len
never reaches it. The sentence is said rather than guessed at — len and length
are three edits apart and the did-you-mean's net is one — and the call is
written back out through spell_arg, as-slice's spelling lifted out of it and
now shared, so what is printed compiles.

sand.flan:33 still calls the old name and is the author's to change; until it
does, test_acceptance and test_session abort there. Both were run green
against a copy with that one line changed. FIX.org says so.
2026-09-21 11:58:56 +07:00

33 lines
1.2 KiB
Plaintext

;;;; A data type a package exports, which is what vendor/edn's Value needed.
;;;;
;;;; A struct imports by renaming one name. A data type has a second half — the
;;;; case table — and the question this package exists to answer is where each
;;;; half ends up. The answer is that a case is not a top-level name: it has no
;;;; existence apart from its type, so the only thing to qualify is the [Node.]
;;;; in front of it, and an importer's [(Leaf n)] pattern stays bare because it
;;;; resolves against the scrutinee's type and never against a name.
(defdata Node
[(Leaf [n i64])
(Branch [kids (Vec Node)])
(Empty [])])
;; Built inside the package, where the constructor is written unqualified and
;; the import has to rewrite it.
(defn leaf [n i64] Node (Node.Leaf {.n n}))
;; A case with no fields is a value and not a call, so this is the [Var] node
;; where the others are [Struct] nodes — the second shape the rename has to
;; catch, and the one it is easy to catch only half of.
(defn nothing [] Node Node.Empty)
(defn total [t Node] i64
(match t
(Leaf n) n
(Branch kids)
(let [s (i64 0)]
(dotimes [i (length kids)]
(set s (+ s (total (at kids i)))))
s)
Empty (i64 0)))