curry.backends.py.graph.variable module

class curry.backends.py.graph.variable.Variable(rts, root, target, realpath=None, guards=None)[source]

Bases: object

Represents an ordinary (non-free) Curry variable.

This objects tracks a root, target, path between them, and set guards crossed. It is the focal point when working with expressions in generated code. These objects help with accessing and head-normalizing subexpressions, getting case selectors, building replacement expressions, and rewriting.

Variables:
  • root – The function-labeled redex root. Dominates the target expression.

  • target – The Curry expression referenced by this variable. Typically an inductive position of the root function.

  • realpath – The real path from root to target.

  • guards – A set containing the set guards crossed on the path from root to target.

  • rvalue – The value to use when this variable appears in a RHS replacement expression. Any guards crossed along the path from root to target are inserted before the target.

Example

The Prelude.head function is defined as follows:

head (a:as) = a

Here, both ‘a’ and ‘as’ are variables. In addition, for the purpose of finding redexes, the compiler generates variables for unnamed inductive positions, such as (:) in this example. This might be compiled to the following ICurry:

var $1
$1 <- $0[0]
case $1 of
  Prelude.[] -> exempt
  Prelude.: _ _ ->
    var $2
    $2 <- $1[0]
    return $2

The generated Python code might use Variable as follows:

def step(rts, _0):
  # _0 = rts.variable(_0)    # (1)
  _1 = None
  _1 = rts.variable(_0, 0)   # (2)
  _1.hnf(typedef=ty_Nil)     # (3)
  if _1.tag == 1:            # (4)
    _0.rewrite(rts.Failure)  # (5a)
  else:
    _2 = None
    _2 = rts.variable(_1, 0)
    _0.rewrite(rts.Fwd, _2)  # (5b)

At (1), the root variable, _0, is constructed. This will serve as the base for other variables, and for rewriting. This is in fact taken care of by the system before calling the step function. At (2), the variable corresponding to _1 in ICurry or ‘:’ in the source code is defined. Step (3) head-normalizes the subexpression at _1. The tag is inspected at (4). At (5a,5b), the root is rewritten. When considering set functions, this step might need to insert set guards before _2.

extend()[source]

Step over a forward node or set guard at the target position.

This might be needed, for instance, after a rewrite step that modifies the target.

static from_logicalpath(rts, parent, logicalpath)[source]
static from_root(rts, parent)[source]
guards
hnf(typedef=None, values=None)[source]

Head-normalizes this variable.

hnf_or_free(typedef=None, **kwds)[source]
property info
property is_boxed
property is_root
realpath
replace_target(replacement)[source]

Replace the target by rewriting the root.

rewrite(nodeinfo, *args)[source]

Rewrite this variable.

root
rts
property rvalue

Returns the target with the appropriate set guards inserted.

property successors
property tag
target
property typedef
property unboxed_value
curry.backends.py.graph.variable.variable(rts, parent, logicalpath=None)[source]

Creates an instance of Variable.