curry.typecheck.serialize module
The serializer: prints the description of a Python-built expression as Curry text (item Y8 of the typed boundary, epic #48).
A spec tree (engine, builder) describes the arguments of
curry.expr. serialize() prints it in Curry syntax, so that the
same expression can be compiled by the front end and evaluated by the PAKCS
REPL, the oracles of the tests. Python values are written as Curry
literals. An operand without Curry text, a Curry node of an earlier
evaluation or an iterator, becomes a parameter. A free marker becomes a
let x1 free in declaration, or a parameter when the caller lifts the
markers as the REPL does for where x free. Anchors and references
become a recursive let. Names of the Prelude print unqualified, names
of other modules qualified, so the text compiles in a module that imports
them.
Both passes of the serializer, the naming and the printing, walk the tree with an explicit stack, so the depth of a description does not bound the depth of the Python stack.
- exception curry.typecheck.serialize.SerializeError[source]
Bases:
ValueErrorA part of the description has no Curry text, or cannot be written.
- class curry.typecheck.serialize.Serialized(body, params, frees, bindings, exprtype, modules)[source]
Bases:
objectThe Curry text of a description.
- Variables:
body – The expression without the declarations of the free markers and the anchors.
params – The names of the operands without Curry text, in the order of their first occurrence: Curry nodes, iterators, and the other untyped parts.
frees – The names of the free markers, in the order of their first occurrence.
bindings – The anchors as pairs of a name and the text of the value, in the order of their first occurrence.
exprtype – The type the expression was annotated with, or None.
modules – The names of the modules whose symbols the text names qualified.
- definition(name='compiled_expression', lift_frees=True)[source]
A Curry function definition of the expression:
name p1 x1 = body, whose parameters are the operands without text and, withlift_frees, the free markers.
- expression(lift_frees=False)[source]
The text of the expression. With
lift_freesthe free markers are not declared: they are parameters, as the REPL of PAKCS makes them forwhere x free.
- parameters(lift_frees=True)[source]
The names of the parameters of
definition().
- property text
the free markers declared with
let x1 free in, the anchors bound by a recursivelet. The parameters, if any, are free names of the text; seedefinition().- Type:
The expression as Curry text
- curry.typecheck.serialize.curry_literal(value, as_char=None)[source]
A Python value as a Curry literal: a bool, an int, a float, a str or bytes. A one-character string is a
Charwhenas_charis true, a string when it is false, and aCharby default. Any other value, andNone, has no literal.
- curry.typecheck.serialize.float_text(value)[source]
A Python float as a Curry literal. The mantissa always has a decimal point, the exponent has no plus sign:
1.0e20,1.5e-7,2.5. A negative number is written in parentheses. An infinity or a NaN has no literal.
- curry.typecheck.serialize.is_identifier(name)[source]
Whether
nameis a Curry variable name that is no keyword. A name that starts with an underscore does not count: the builder names its own anchors that way.
- curry.typecheck.serialize.serialize(spec, exprtype=None, module=None, anchor_names=None)[source]
Prints a spec tree as Curry text.
- Parameters:
spec – The root of the tree: a spec of the engine or of the builder, or a
Description.exprtype – The type of the expression in Curry syntax, or None. The body is annotated with it:
(body :: T).module – The name of the module whose symbols print unqualified, besides the Prelude; the module the text is compiled in.
anchor_names – A dict from the type variable of an anchor to its name, as the builder keeps it (
TypedBuilder.anchor_varsinverted). A name that is a free Curry identifier is kept.
- Returns:
A
Serialized.- Raises:
SerializeError – A part of the tree has no Curry text and cannot be a parameter:
None, a set guard, a constraint, a 1-tuple, a float without a literal.
- curry.typecheck.serialize.serialize_call(interp, args, anchors=None, exprtype=None, module=None, typed=True)[source]
Prints the arguments of
curry.expras Curry text.argsare the positional arguments,anchorsthe keyword anchors as a dict. Withtypedthe tree is typed first (phase 1 of the engine), so that a one-character string under[Char]prints as a string; a typing failure leaves the tree untyped, and the text follows the Python types.
- curry.typecheck.serialize.serialize_description(description, exprtype=None, module=None, typed=True)[source]
Prints a
Description.