curry.typecheck.goals module
Goals: the expressions that curry.eval, curry.compile(mode='expr'),
the REPL and sprite-exec evaluate.
A goal without a type signature is a function whose leading parameters are
class dictionaries (issue #39). This module builds the expression that runs
such a goal: it defaults the constrained type variables with the table of the
PAKCS REPL (defaulting) and applies the goal to one base instance per
constraint, in the order of the scheme, each a partial application of
_inst#Class#Type with one missing argument, as compiled code passes a
dictionary. Value parameters that remain give a function value. No front
end runs and no module is written.
The text route lifts the variables of a trailing where x, y free to
parameters of the compiled expression, as the REPL does. A lifted variable
whose type is absent from the result type of the expression cannot show in a
value, so the values of such a goal come back as Bindings with the
binding of every such variable, printed the way PAKCS prints them:
{xs=[1,2]} True. A variable whose type occurs in the result type is not
reported: the value, with the variable in it, is the answer. The goal
objects of this module stay inside curry.eval; compile(mode='expr')
returns a node (see lifted_goal()).
- class curry.typecheck.goals.Bindings(value, bindings, raw=None)[source]
Bases:
objectOne value of a goal with
where ... freevariables, with the bindings of those variables. Prints as PAKCS prints it:{xs=[1,2]} True.- Variables:
value – The value of the goal.
bindings – A dict from the name of a variable to its binding, in the order of the declaration.
raw – The Curry tuple of the value and the bindings, as the evaluation produced it, or None when the parts were converted to Python. The tuple is printed as one expression, so that an unbound variable gets one name in every part.
- bindings
- raw
- value
- class curry.typecheck.goals.Goal(expr, freevars=(), scheme=None, text=None)[source]
Bases:
objectAn expression to evaluate.
- Variables:
expr – The node to evaluate, or None once the evaluation has started. For a goal with reported free variables, the constructor of the expression module (
compile.LIFTED_NAME) around the tuple of the value and the variables.freevars – The names of the reported
where ... freevariables, in the order of the tuple.scheme – The scheme of the compiled expression, or None.
text – The text of the goal, or None.
- evaluate(interp, convert=None)[source]
Starts the evaluation and returns the generator of the values. Each value is converted with
convert; a goal with reported free variables yieldsBindings.The goal hands its node to the runtime state and keeps no reference to it. The root of the configuration moves with the rewrites, but a reference to the original node keeps every node of the search above the live configurations reachable (the choices pulled to the root with their failed alternatives), so a holder of the node roots the history of the evaluation and each collection marks it. The generator holds the names of the free variables and not the node, so no caller roots the history by holding a goal object through the evaluation.
- Raises:
ValueError – The goal was evaluated already.
- property expr
- freevars
- scheme
- text
- class curry.typecheck.goals.LiftedGoal(freevars, scheme, text)
Bases:
tuple- freevars
Alias for field number 0
- scheme
Alias for field number 1
- text
Alias for field number 2
- curry.typecheck.goals.absent_from_result(scheme, nfree)[source]
The indices of the lifted variables whose binding
curry.evalreports.schemeis the scheme ofcompiled_expression x1 ... xn = <text>, so its type without the dictionaries ist1 -> ... -> tn -> r. A variable whose type occurs in the result typercan show in a value and is left to the value; a variable whose type is absent fromrcannot, and is reported with its binding. The rule is Sprite’s: the REPL of PAKCS prints the binding of every lifted variable.- Raises:
CurryTypeError – The type has fewer than
nfreeparameters.
- curry.typecheck.goals.check_application(args)[source]
Checks the arguments of
curry.evalthat apply a symbol to arguments. A symbol with dictionary parameters applied to anything but its dictionaries would take the first argument as a dictionary and build a wrong expression without a word; this raises instead. A caller that passes the dictionaries first is left alone.- Raises:
CurryTypeError – The head symbol has dictionary parameters and the first argument is not a dictionary.
- curry.typecheck.goals.defaulted_goal(interp, symbol, scheme, args=(), what=None, hint=None)[source]
The expression of a goal whose scheme has dictionary parameters: the symbol applied to one base instance per constraint and then to
args. With value parameters left over, the expression is a function value.- Raises:
DefaultingError – The table cannot default the constraints.
- curry.typecheck.goals.dictionaries(interp, defaulting)[source]
The dictionary nodes of a defaulted scheme: for each constraint, in the order of the scheme, the partial application of
_inst#Class#Typeto no argument.- Raises:
CurryTypeError – No loaded module declares the instance.
- curry.typecheck.goals.flat_type_text(scheme)[source]
The FlatCurry type of a scheme as text, for the footer of a saved module.
- curry.typecheck.goals.is_dictionary(arg)[source]
Whether an argument of
expris a class dictionary: the symbol of an instance function,_inst#Class#Type, a node built from one, or a list that starts with one.
- curry.typecheck.goals.lifted_goal(interp, node)[source]
The
Goalof a node thatcompile(mode='expr')built for a text withwhere ... freevariables absent from its result type (seeregister_lifted), or None for any other argument.
- curry.typecheck.goals.make_goal(interp, args, exprtype=None, anchors=None)[source]
The
Goalof the arguments ofcurry.eval: a goal object as it is, the root of a compiled text with reported variables (lifted_goal()), a symbol with dictionary parameters applied to its dictionaries, and anything else throughinterp.exprwithexprtypeand the keywordanchors. The typed builder ofinterp.exprsupplies the dictionaries of a symbol applied to arguments, and types a lone symbol underexprtype; with the flagtyped_exproff,check_application()refuses a symbol applied to arguments.
- curry.typecheck.goals.register_lifted(interp, ctor, freevars, scheme, text)[source]
Records the reported variables of a compiled text whose root is
ctor (expr, xi, ...): the constructor the expression module declares (compile.LIFTED_NAME) around the tuple of the expression and the variables offreevars, in that order. The record is keyed by the info table ofctor, which the module keeps alive until the interpreter resets (Interpreter._lifted_goals);lifted_goalfinds it from the root. A constructor node is never rewritten, so the root is recognized after an evaluation as before it.
- curry.typecheck.goals.scheme_from_flat_text(symbol, text)[source]
The scheme of a symbol from the text
flat_type_text()wrote. The arity comes from the symbol.
- curry.typecheck.goals.split_tuple_text(text, n)[source]
Splits the text of a Curry tuple of
nelements,(e1, ..., en), at its top-level commas. Brackets, strings and character literals are skipped. Returns None when the text is not such a tuple.
- curry.typecheck.goals.split_where_free(text)[source]
Splits a trailing
where x, y freefrom the text of a goal, as the REPL of PAKCS does (splitWhereFreeinc2p.pl). Returns the expression and the list of variable names; the list is empty when the text has no such clause.
- curry.typecheck.goals.symbol_goal(interp, symbol)[source]
The goal expression of a symbol evaluated on its own, or None when the symbol needs no dictionary: a function with a scheme whose leading parameters are dictionaries gets them. A symbol without a scheme is left to the plain builder.
- curry.typecheck.goals.type_arity(interp)[source]
A function from the qualified name of a type constructor to its declared arity, or None for an unknown type; for
defaulting.normalize().