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: ValueError

A 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: object

The 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, with lift_frees, the free markers.

expression(lift_frees=False)[source]

The text of the expression. With lift_frees the free markers are not declared: they are parameters, as the REPL of PAKCS makes them for where 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 recursive let. The parameters, if any, are free names of the text; see definition().

Type:

The expression as Curry text

where_text()[source]

The expression with a trailing where x1, x2 free clause instead of the let: the form the text route lifts to parameters. The description may have no operand without 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 Char when as_char is true, a string when it is false, and a Char by default. Any other value, and None, 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 name is 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_vars inverted). 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.expr as Curry text. args are the positional arguments, anchors the keyword anchors as a dict. With typed the 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.