Syntax¶
The fosf.syntax module handles the internal representation of fuzzy OSF logic
objects, including tags, features, sorts, constraints, clauses, terms, theories and fuzzy
taxonomies.
Base OSF syntax¶
- class fosf.syntax.base.Tag[source]¶
Bases:
strRepresent a tag symbol in an OSF term or OSF clause.
Tagis a string subclass used to distinguish tags from regular strings. It is hashable and comparable. Equality is based on both the type and the string value.
- class fosf.syntax.base.Feature[source]¶
Bases:
strRepresent a feature symbol in an OSF term or OSF clause.
Featureis a string subclass used to distinguish features from regular strings. It is hashable and comparable. Equality is based on both the type and the string value.
- class fosf.syntax.base.Sort(value)[source]¶
Bases:
objectRepresent a single sort, a named element of a sort taxonomy.
Sortobjects are hashable and comparable. Equality is based on both the type and the string value.- Parameters:
value (str or Sort) – The name of the sort, or an existing
Sortinstance. If initialized from another Sort, the .value attribute is used. Disjunctive sorts (e.g.,DisjunctiveSort,FrozenDisjunctiveSort) are not allowed.- Variables:
value (str) – The name of the sort.
- Raises:
TypeError – If value is not a string or a Sort instance.
- class fosf.syntax.base.DisjunctiveSort(*sorts)[source]¶
Bases:
SortRepresent a mutable disjunctive sort.
A
DisjunctiveSortis set of individualSortobjects and represents their union. It is mutable: sorts can be added after construction. A frozen variant can be obtained via thefreeze()method.- freeze()[source]¶
Return an immutable
FrozenDisjunctiveSortcontaining the same sorts.- Return type:
- class fosf.syntax.base.FrozenDisjunctiveSort(*sorts)[source]¶
Bases:
SortRepresent an immutable disjunctive sort.
A
FrozenDisjunctiveSortcontains a fixed set of Sort objects representing their union. Unlike DisjunctiveSort, this class is immutable and hashable. The mutable variant can be obtained via thethaw()method.- Parameters:
*sorts (str or Sort) – One or more sorts to initialize the frozen disjunction.
- Variables:
- thaw()[source]¶
Return a mutable
DisjunctiveSortcontaining the same sorts.- Return type:
Taxonomies¶
- class fosf.syntax.taxonomy.BaseTaxonomy(edges)[source]¶
Bases:
Generic[T,R]Generic taxonomy over directed acyclic graphs with bitvector encoding.
BaseTaxonomyrepresents a partially ordered set of nodes using a directed acyclic graph (DAG) structure. Each node is encoded as a bitvector for efficient computation of greatest lower bounds (GLBs) and subsort relationships. Optional weights can be associated with edges. Special top and bottom elements are automatically added if missing from the input edges.This class is generic in the node type T and the return type R for GLB computations when multiple maximal lower bounds exist.
- Parameters:
edges (Iterable[tuple[str | T, str | T] | tuple[str | T, str | T, float]]) – An iterable of edges defining the DAG. Each edge can optionally include a weight (float). Nodes can be strings or instances of T.
- Variables:
graph (nx.DiGraph) – The DAG representation of the taxonomy.
bot (T) – The bottom node in the taxonomy.
top (T) – The top node in the taxonomy.
topo (list[T]) – The list of nodes in topological order.
rank (dict[T, int]) – A mapping from nodes to their rank in the topological order.
node_to_code (dict[T, int]) – A mapping from nodes to their bitcode.
code_to_node (dict[int, T]) – A mapping from bitcodes to nodes.
- Raises:
fosf.exceptions.NotADag – If the input edges form a cycle.
- glb(*nodes)[source]¶
Return the greatest lower bound of the input nodes:
- Parameters:
nodes (T | str)
- Return type:
T | R
- class fosf.syntax.taxonomy.TagTaxonomy(edges)[source]¶
Bases:
BaseTaxonomy[Tag,set[Tag]]Taxonomy of tag symbols.
Used in
fosf.syntax.theory.OsfTheoryto store the ordering of Tags induced by an OSF theory.- Parameters:
edges (Iterable[tuple[str | Tag, str | Tag]]) – An iterable of edges defining the DAG. Nodes can be strings or
Tag’s.- Variables:
graph (nx.DiGraph) – The DAG representation of the Tag taxonomy.
bot (Tag) – The bottom Tag in the taxonomy.
top (Tag) – The top Tag in the taxonomy.
rank (dict[Tag, int]) – A mapping from tags to their rank in the topological order.
node_to_code (dict[Tag, int]) – A mapping from tags to their bitcode.
code_to_node (dict[int, Tag]) – A mapping from bitcodes to tags.
- class fosf.syntax.taxonomy.SortTaxonomy(edges, instances=None)[source]¶
Bases:
BaseTaxonomy[Sort,DisjunctiveSort]Taxonomy of sort symbols.
- Parameters:
edges (Iterable[tuple[str | Sort, str | Sort] | tuple[str | Sort, str | Sort, float]]) – An iterable of edges defining the DAG. Each edge can optionally include a weight (float). Nodes can be strings or instances of
Sort.instances (dict[str, dict[Sort, float]] | None) – Optional dict mapping instances to the sorts they are direct members of, together with their membership degree
- Variables:
graph (nx.DiGraph) – The DAG representation of the sort taxonomy.
bot (Sort) – The bottom sort in the taxonomy.
top (Sort) – The top sort in the taxonomy.
rank (dict[Sort, int]) – A mapping from sorts to their rank in the topological order.
node_to_code (dict[Sort, int]) – A mapping from sorts to their bitcode.
code_to_node (dict[int, Sort]) – A mapping from bitcodes to sorts.
- Raises:
fosf.exceptions.NotADag – If the input edges form a cycle.
- class fosf.syntax.taxonomy.FuzzySortTaxonomy(edges, instances=None)[source]¶
Bases:
SortTaxonomy- Parameters:
edges (Iterable[tuple[str | Sort, str | Sort] | tuple[str | Sort, str | Sort, float]]) – An iterable of edges defining the DAG. Each edge can optionally include a weight (float). Nodes can be strings or instances of
Sort.instances (dict[str, dict[Sort, float]] | None) – Optional dict mapping instances to the sorts they are direct members of, together with their membership degree
- Variables:
graph (nx.DiGraph) – The DAG representation of the sort taxonomy.
bot (Sort) – The bottom sort in the taxonomy.
top (Sort) – The top sort in the taxonomy.
rank (dict[Sort, int]) – A mapping from sorts to their rank in the topological order.
node_to_code (dict[Sort, int]) – A mapping from sorts to their bitcode.
code_to_node (dict[int, Sort]) – A mapping from bitcodes to sorts.
- Raises:
fosf.exceptions.NotADag – If the input edges form a cycle.
- add_instance(instance, sort, degree=1.0, check=True)[source]¶
Add an instance to a sort with a membership degree.
- membership_degree(instance, sort)[source]¶
Compute the degree of membership of an instance to a sort or sorts.
- degree(s: Sort | str, t: Sort | str) float[source]¶
- degree(s: Sort | str, t: Iterable[Sort | str]) dict[Sort, float]
- degree(s: Iterable[Sort | str], t: Sort | str | Iterable[Sort | str]) dict[Sort, dict[Sort, float]]
Compute the subsumption degree of one or more sorts with repect to one or more sorts.
Terms¶
- class fosf.syntax.terms.Term(X, s=None, subterms=None)[source]¶
Bases:
objectRepresent an OSF term X:s(f1 -> t1, …, fn -> tn).
- Parameters:
X (Tag) – The root tag of the term.
s (Sort | None) – The root sort of the term. If None, when the OSF term is processes, it is assumed to be the top sort in a given Sort taxonomy.
subterms (dict[Feature, list[Term]] | None) – The subterms of the OSF term. In a non-normal OSF term, the same feature may point to different subterms.
- Variables:
- class fosf.syntax.terms.NormalTerm(X, s=None, subterms=None)[source]¶
Bases:
TermRepresent an OSF term in normal form.
- Parameters:
X (Tag) – The root tag.
s (Sort | None) – The root sort. If None, when the OSF term is processes, it is assumed to be the top sort in a given Sort taxonomy.
subterms (dict[Feature, NormalTerm] | None) – The subterms of the OSF term. In a normal OSF term, each feature may point to at most one (normal) subterm.
- Variables:
- to_clause()[source]¶
Transform the OSF term into an equivalent rooted solved clause.
- Returns:
The rooted solved clause corresponding to the OSF term.
- Return type:
- equivalent_to(other)[source]¶
Return whether this Term is equivalent to another normal term.
- Parameters:
other (NormalTerm)
- Return type:
Constraints¶
- class fosf.syntax.constraints.Constraint[source]¶
Bases:
ABCAbstract base class for an OSF constraint.
- class fosf.syntax.constraints.SortConstraint(X, s)[source]¶
Bases:
ConstraintRepresent a sort constraint
X : s.
- class fosf.syntax.constraints.FeatureConstraint(X, f, Y)[source]¶
Bases:
ConstraintRepresent a feature constraint
X.f = Y.
- class fosf.syntax.constraints.EqualityConstraint(X, Y)[source]¶
Bases:
ConstraintRepresent an equality constraint
X = Y.
- class fosf.syntax.constraints.Clause(*constraints)[source]¶
Bases:
objectRepresent an OSF clause, a conjunctive set of OSF constraints.
- Parameters:
*constraints (Constraint) – The initial constraints of the clause.
- Variables:
- add(*constraints)[source]¶
Add constraints to the clause.
- Parameters:
constraints (Constraint)
- normalize(taxonomy)[source]¶
Compute the normal (or solved) form of the clause.
- Parameters:
taxonomy (SortTaxonomy)
- Return type:
- class fosf.syntax.constraints.RootedClause(root, *constraints, ensure_rooted=False)[source]¶
Bases:
ClauseRepresent a rooted OSF clause, a clause with a distinguished root tag.
- Parameters:
root (Tag) – The root of the clause.
*constraints (Constraint) – The initial constraints of the clause.
ensure_rooted (bool, default=False) – If True, ensure that the added constraints are indeed rooted.
- Variables:
- add(*constraints, ensure_rooted=False)[source]¶
Add constraints to the clause.
- Parameters:
*constraints (Constraint) – The constraints to be added
ensure_rooted (bool, default=False) – If True, ensure that the constraints keep the clause rooted.
- Raises:
RuntimeError – If ensure_rooted is True, and the added constraints do not maintain the clause rooted.
- normalize(taxonomy)[source]¶
Compute the normal (or solved) form of the clause.
- Parameters:
taxonomy (SortTaxonomy)
- Return type:
- class fosf.syntax.constraints.SolvedClause(*constraints)[source]¶
Bases:
ClauseRepresent an OSF clause in solved form.
- Parameters:
*constraints (Constraint) – The initial constraints of the clause.
- Variables:
- add(*constraints)[source]¶
Add constraints to the clause.
- Parameters:
*constraints (Constraint) – The constraints to be added
- Raises:
RuntimeError – If the added constraints do not maintain the clause solved.
- normalize(_)[source]¶
Compute the normal (or solved) form of the clause.
- Parameters:
_ (SortTaxonomy)
- Return type:
- class fosf.syntax.constraints.RootedSolvedClause(root, *constraints)[source]¶
Bases:
SolvedClause,RootedClauseRepresent a solved OSF clause with a distinguished Tag as its root.
- Parameters:
root (Tag) – The root of the clause.
*constraints (Constraint) – The initial constraints of the clause.
- Variables:
- equivalent_to(other)[source]¶
Return whether this clause is equivalent to another rooted solved clause.
- Parameters:
other (RootedSolvedClause)
- Return type:
Theory¶
- class fosf.syntax.theory.TheoryTag(tag, sort, features=None)[source]¶
Bases:
objectRepresent a Tag used in an OSF theory.
- Parameters:
- Variables:
- class fosf.syntax.theory.OsfTheory(taxonomy, definitions, tags=None, ensure_closed=False)[source]¶
Bases:
objectRepresent an OSF theory of sort definitition.
A sort definition imposes structural constraints on OSF terms.
- Parameters:
taxonomy (SortTaxonomy) – The sort taxonomy for this theory
definitions (dict[Sort, NormalTerm]) – A mapping from each
Sortto its definition as aNormalTermtags (dict[Tag, TheoryTag] | None, default=None) – A mapping from each
Tagto itsTheoryTagobject. If None, it is computed.ensure_closed (bool, default=False) – If True, close the OSF theory to ensure its order-consistency.
- Variables:
taxonomy (SortTaxonomy) – The background sort subsumption taxonomy.
definitions (dict[Sort, NormalTerm]) – A mapping from each
Sortto its definition as aNormalTermtags (dict[Tag, TheoryTag]) – A mapping from each
Tagto itsTheoryTagobject.tag_taxonomy (TagTaxonomy) – The tag taxonomy representing the ordering on theory tags induced by the sort taxonomy and the theory.