Skip to content

[WIP] Type annotations - #14

Draft
nikolaushuber wants to merge 20 commits into
mainfrom
type-annotations
Draft

nikolaushuber wants to merge 20 commits into
mainfrom
type-annotations

Conversation

@nikolaushuber

Copy link
Copy Markdown
Collaborator

This PR adds support for adding type annotations to the generated code.

This is currently WIP, and must be switched on by an Isabelle option (see README).

Modules follow theories, and code equations may use constants of later
theories, so generated modules import each other (about 50 HOL modules
form one cycle). A module-level value that reads an attribute of a
module still being imported fails, which broke almost every module of
the slow suite. Such values are now printed as @functools.cache
functions without arguments and called at their uses; all other values
stay plain assignments.

The slow suite now also imports every generated module.
A value whose right-hand side calls an aborting constant (no code
equations, or declared undefined), or uses such a value, raised when its
module was imported. It is now printed as a cached function like the
values in an import cycle, so it raises only when used. This makes
Code_Test of the slow suite import, and changes the golden of
Example_no_eqs (uses_undefined = undefined).
Each example directory has its own copy of the library modules, so mypy
checks them one at a time. Two annotations mypy rejects are changed: a
polymorphic value gets Any for its type variables (`empty_list:
list[Any] = []`), and a function without code returns Never rather than
a type variable that only occurs in its return type.
Mypy infers the parameter types of a lambda passed to a generic function
by checking it once per candidate type, so nested lambdas took
exponential time: four modules of the slow suite (Quickcheck random
generators in Tree, Records, RBT, RBT_Impl) did not finish in half an
hour. With type annotations, every lambda, and every partial application
that would be printed as one, is now hoisted into a def whose parameters
and return type are annotated; the hoisted names differ within a
statement, since mypy rejects defs of one name with different
signatures in different match arms.

Both slow-suite directories now check in about a second (75 and 40
errors). Output without annotations is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant