Skip to content

v0.7.0

Choose a tag to compare

@simontreanor simontreanor released this 30 Aug 10:40
· 120 commits to main since this release
fe9e208

v0.7.0

A performance round driven by a real program: a Scrabble move generator that asks
Option questions millions of times per position. Its author hand-edited the
emitted Python to find where the time went, and each edit became a change to the
compiler. Along the way the same program turned up four cases where pyfun check
accepted a program that then failed at runtime, and all four are fixed.

Faster emitted Python

Each of these changes only what the compiler emits. Programs compile unchanged
and produce the same output.

Option and Result matches are if/isinstance ladders (#87). A match
on an Option or Result used to lower to a Python match statement with
class patterns and an unreachable case _: raise arm. It now lowers to what a
person writes:

if isinstance(o, Some):
    x = o._0
    return x
else:
    return 0

User-defined types keep match/case.

Nullary constructors are singletons (#89). A case with no fields
(Across in type Dir = Across | Down, and the prelude's None) is built once
at module level and referenced by name (_Across, _None_) instead of being
constructed at every use. Equality between two nullary values is an identity
check. Constructing through the class by hand still works and still compares
equal.

A match that consumes a lookup on the spot skips the Option (#88).
match Map.tryFind k m: with Some v/None arms lowers to
if k in m: v = m[k] …; List.head and List.get do the same. The
Option-returning helper stays for every other use.

Equality searches use the list's own methods (#86).
List.findIndex ((==) x) xs lowers to xs.index(x) under a try,
List.exists ((==) x) xs to x in xs, and List.find ((==) x) to the same
scan, instead of a Python-level loop over a fresh lambda.

A fold that destructures its element is linear (#85). The in-place fold
pass rejected List.fold (fun m (p, l) -> Map.add p l m) Map.empty pairs and
fell back to a copy-per-step reduce, which is quadratic. The element parameter
may now be any irrefutable pattern and emits Python's own
for (p, l) in pairs: header. On the program's 267,751-word automaton build
this is the difference between 1.85 seconds and not finishing.

Every emitted class uses __slots__. ADT cases, records and the prelude's
Some/Ok/Error are declared with @dataclass(frozen=True, slots=True),
so construction and field access are cheaper. This is the one change a Python
caller can notice: vars(instance) and instance.__dict__ no longer exist.
dataclasses.asdict, dataclasses.replace, equality, hashing and ordering are
unchanged.

Measured on the program that drove the round: move generation 1.29x faster
overall (9.84s to 7.63s on its slowest rack), and scoring 8% faster without
being the target, because it uses Option and Map.tryFind too.

Fixed

Four programs that passed pyfun check and failed at runtime. All are the
same kind of gap: the checker knew what the program meant and the lowering
lost it.

A block-local function applied to fewer arguments than it takes is a partial
application
(#84). let pair a b = a + b inside a function, then
List.map (pair 10) ys, was emitted as a full call pair(10) and died with a
TypeError. Local functions now carry their arity like top-level ones do, so
this emits functools.partial(pair, 10) and over-application applies the
remainder one argument at a time.

A match-arm capture no longer rebinds a same-named binding for the rest of
the function
(#92, #97). Python locals are function-wide; Pyfun's are
lexically scoped. case Some pair: pair in a function that also has a local
def pair used to overwrite it, so a later pair r 1 called an integer. The
compiler now emits such a capture as _pair and every reference follows. The
rename fires only when a reference outside the arm reads a binding that is live
across it, so ordinary code (case Ok x: … case Error x: …, the same capture
name in successive matches) keeps its names and its output is unchanged.

A let in a nested block no longer rebinds the enclosing binding (#96).
let x = 10 inside an if branch or a match arm overwrote the function's
x = 1, so x + y after the branch used 10. Same rule, same _x spelling,
same "only when it matters".

A let that shadows an outer name read earlier in the same function no
longer raises UnboundLocalError
(#99). Inside a nested function, let y = x + 1 followed by let x = 2 made Python treat x as local for the whole
function, and the first line failed. This covers let/let! inside option,
result, seq and async blocks too.

Upgrading

pip install --upgrade pyfun-lang. No source changes are needed. If Python
code on your side reads vars() or __dict__ of a Pyfun value, switch to
dataclasses.asdict or attribute access.