pyk.kast.inner module

final class KApply(label: str | KLabel, args: Iterable[KInner])[source]
final class KApply(label: str | KLabel, *args: KInner)

Bases: KInner

Represent the application of a KLabel in a K AST to arguments.

__init__(label: str | KLabel, args: Iterable[KInner])[source]
__init__(label: str | KLabel, *args: KInner)

Construct a new KApply given the input KLabel or str, applied to arguments.

args: tuple[KInner, ...]
property arity: int

Return the count of the arguments.

property is_cell: bool

Return whether this is a cell-label application (based on heuristic about label names).

label: KLabel
let(*, label: str | KLabel | None = None, args: Iterable[KInner] | None = None) KApply[source]

Return a copy of this KApply with either the label or the arguments updated.

let_terms(terms: Iterable[KInner]) KApply[source]
match(term: KInner) Subst | None[source]
property terms: tuple[KInner, ...]
final class KAs(pattern: KInner, alias: KInner)[source]

Bases: KInner

Represent a K #as pattern in the K AST format, with the original pattern and the variabl alias.

__init__(pattern: KInner, alias: KInner)[source]

Construct a new KAs given the original pattern and the alias.

alias: KInner
let(*, pattern: KInner | None = None, alias: KInner | None = None) KAs[source]

Return a copy of this KAs with potentially the pattern or alias updated.

let_terms(terms: Iterable[KInner]) KAs[source]
match(term: KInner) Subst | None[source]
pattern: KInner
property terms: tuple[KInner, KInner]
class KInner[source]

Bases: KAst

Represent the AST of a given K inner term.

This class represents the AST of a given term. The nodes in the AST should be coming from a given KDefinition, so that they can be checked for well-typedness.

static from_dict(dct: Mapping[str, Any]) KInner[source]

Deserialize a given KInner into a more specific type from a dictionary.

static from_json(s: str) KInner[source]
abstract let_terms(terms: Iterable[KInner]) KI[source]

Set children of this given KInner.

final map_inner(f: Callable[[KInner], KInner]) KI[source]

Apply a transformation to all children of this given KInner.

abstract match(term: KInner) Subst | None[source]

Perform syntactic pattern matching and return the substitution.

Parameters:

term – Term to match.

Returns:

A substitution instantiating self to term if one exists, None otherwise.

abstract property terms: tuple[KInner, ...]

Returns the children of this given KInner.

final to_dict() dict[str, Any][source]
final class KLabel(name: str, params: Iterable[str | KSort])[source]
final class KLabel(name: str, *params: str | KSort)

Bases: KAst

Represents a symbol that can be applied in a K AST, potentially with sort parameters.

__init__(name: str, params: Iterable[str | KSort])[source]
__init__(name: str, *params: str | KSort)

Construct a new KLabel, with optional sort parameters.

__iter__() Iterator[str | KSort][source]

Return this symbol as iterator with the name as the head and the parameters as the tail.

apply(args: Iterable[KInner]) KApply[source]
apply(*args: KInner) KApply

Construct a KApply with this KLabel as the AST head and the supplied parameters as the arguments.

static from_dict(d: Mapping[str, Any]) KLabel[source]
let(*, name: str | None = None, params: Iterable[str | KSort] | None = None) KLabel[source]

Return a copy of this KLabel with potentially the name or sort parameters updated.

name: str
params: tuple[KSort, ...]
to_dict() dict[str, Any][source]
final class KRewrite(lhs: KInner, rhs: KInner)[source]

Bases: KInner

Represent a K rewrite in the K AST.

__init__(lhs: KInner, rhs: KInner)[source]

Construct a KRewrite given the LHS (left-hand-side) and RHS (right-hand-side) to use.

__iter__() Iterator[KInner][source]

Return a two-element iterator with the LHS first and RHS second.

apply(term: KInner) KInner[source]

Attempt rewriting once at every position in a term bottom-up.

Parameters:

term – Term to rewrite.

Returns:

The term with rewrites applied at every node once starting from the bottom.

apply_top(term: KInner) KInner[source]

Rewrite a given term at the top.

Parameters:

term – Term to rewrite.

Returns:

The term with the rewrite applied once at the top.

let(*, lhs: KInner | None = None, rhs: KInner | None = None) KRewrite[source]

Return a copy of this KRewrite with potentially the LHS or RHS updated.

let_terms(terms: Iterable[KInner]) KRewrite[source]
lhs: KInner
match(term: KInner) Subst | None[source]
replace(term: KInner) KInner[source]

Similar to apply but using exact syntactic matching instead of pattern matching.

replace_top(term: KInner) KInner[source]

Similar to apply_top but using exact syntactic matching instead of pattern matching.

rhs: KInner
property terms: tuple[KInner, KInner]
final class KSequence(items: Iterable[KInner])[source]
final class KSequence(*items: KInner)

Bases: KInner, Sequence[KInner]

Represent a associative list of K as a cons-list of KItem for sequencing computation in K AST format.

__init__(items: Iterable[KInner])[source]
__init__(*items: KInner)

Construct a new KSequence given the arguments.

property arity: int

Return the count of KSequence items.

items: tuple[KInner, ...]
let(*, items: Iterable[KInner] | None = None) KSequence[source]

Return a copy of this KSequence with the items potentially updated.

let_terms(terms: Iterable[KInner]) KSequence[source]
match(term: KInner) Subst | None[source]
property terms: tuple[KInner, ...]
final class KSort(name: str)[source]

Bases: KAst

Store a simple sort name.

__init__(name: str)[source]

Construct a new sort given the name.

static from_dict(d: Mapping[str, Any]) KSort[source]
let(*, name: str | None = None) KSort[source]

Return a new KSort with the name potentially updated.

name: str
to_dict() dict[str, Any][source]
final class KToken(token: str, sort: str | KSort)[source]

Bases: KInner

Represent a domain-value in K AST.

__init__(token: str, sort: str | KSort)[source]

Construct a new KToken with a given string representation in the supplied sort.

let(*, token: str | None = None, sort: str | KSort | None = None) KToken[source]

Return a copy of the KToken with the token or sort potentially updated.

let_terms(terms: Iterable[KInner]) KToken[source]
match(term: KInner) Subst | None[source]
sort: KSort
property terms: tuple[()]
token: str
final class KVariable(name: str, sort: str | KSort | None = None)[source]

Bases: KInner

Represent a logical variable in a K AST, with a name and optionally a sort.

__init__(name: str, sort: str | KSort | None = None)[source]

Construct a new KVariable with a given name and optional sort.

__lt__(other: Any) bool[source]

Lexicographic comparison of KVariable based on name for sorting.

let(*, name: str | None = None, sort: str | KSort | None = None) KVariable[source]

Return a copy of this KVariable with potentially the name or sort updated.

let_sort(sort: KSort | None) KVariable[source]

Return a copy of this KVariable with just the sort updated.

let_terms(terms: Iterable[KInner]) KVariable[source]
match(term: KInner) Subst[source]
name: str
sort: KSort | None
property terms: tuple[()]
class Subst(subst: Mapping[str, KInner] = FrozenDict({}))[source]

Bases: Mapping[str, KInner]

Represents a substitution, which is a binding of variables to values of KInner.

__call__(term: KInner) KInner[source]

Overload for Subst.apply.

__getitem__(key: str) KInner[source]

Get the KInner associated with the given variable name from the underlying Subst mapping.

__init__(subst: Mapping[str, KInner] = FrozenDict({}))[source]

Construct a new Subst given a mapping fo variable names to KInner.

__iter__() Iterator[str][source]

Return the underlying Subst mapping as an iterator.

__len__() int[source]

Return the length of the underlying Subst mapping.

__mul__(other: Subst) Subst[source]

Overload for Subst.compose.

apply(term: KInner) KInner[source]

Apply the given substitution to KInner, replacing free variable occurances with their valuations defined in this Subst.

compose(other: Subst) Subst[source]

Union two substitutions together, preferring the assignments in self if present in both.

static from_dict(d: Mapping[str, Any]) Subst[source]

Deserialize a Subst from a given dictionary representing it.

static from_pred(pred: KInner) Subst[source]

Given a generic matching logic predicate, attempt to extract a Subst from it.

property is_identity: bool
minimize() Subst[source]

Return a new substitution with any identity items removed.

property pred: KInner

Turn this Subst into a boolean predicate using _==K_ operator.

to_dict() dict[str, Any][source]

Serialize a Subst to a dictionary representation.

unapply(term: KInner) KInner[source]

Replace occurances of valuations from this Subst with the variables that they are assigned to.

union(other: Subst) Subst | None[source]

Union two substitutions together, failing with None if there are conflicting assignments.

bottom_up(f: Callable[[KInner], KInner], kinner: KInner) KInner[source]

Transform a term from the bottom moving upward.

Parameters:
  • f – Function to apply to each node in the term.

  • kinner – Original term to transform.

Returns:

The transformed term.

bottom_up_with_summary(f: Callable[[KInner, list[A]], tuple[KInner, A]], kinner: KInner) tuple[KInner, A][source]

Traverse a term from the bottom moving upward, collecting information about it.

Parameters:
  • f – Function to apply at each AST node to transform it and collect summary.

  • kinner – Term to apply this transformation to.

Returns:

A tuple of the transformed term and the summarized results.

build_assoc(unit: KInner, label: str | KLabel, terms: Iterable[KInner]) KInner[source]

Build an associative list.

Parameters:
  • unit – The empty variant of the given list type.

  • label – The associative list join operator.

  • terms – List (potentially empty) of terms to join in an associative list.

Returns:

The list of terms joined using the supplied label, or the unit element in the case of no terms.

build_cons(unit: KInner, label: str | KLabel, terms: Iterable[KInner]) KInner[source]

Build a cons list.

Parameters:
  • unit – The empty variant of the given list type.

  • label – The associative list join operator.

  • terms – List (potentially empty) of terms to join in an associative list.

Returns:

The list of terms joined using the supplied label, terminated with the unit element.

collect(callback: Callable[[KInner], None], kinner: KInner) None[source]

Collect information about a given term traversing it bottom-up using a function with side effects.

Parameters:
  • callback – Function with the side effect of collecting desired information at each AST node.

  • kinner – The term to traverse.

flatten_label(label: str, kast: KInner) list[KInner][source]

Given a cons list, return a flat Python list of the elements.

Parameters:
  • label – The cons operator.

  • kast – The cons list to flatten.

Returns:

Items of cons list.

top_down(f: Callable[[KInner], KInner], kinner: KInner) KInner[source]

Transform a term from the top moving downward.

Parameters:
  • f – Function to apply to each node in the term.

  • kinner – Original term to transform.

Returns:

The transformed term.

var_occurrences(term: KInner) dict[str, list[KVariable]][source]

Collect the list of occurrences of each variable in a given term.

Parameters:

term – Term to collect variables from.

Returns:

A dictionary with variable names as keys and the list of all occurrences of the variable as values.