Calculemus


Unif Module

Unification for first order terms.

Functions and values

Function or value Description

fullunify eqs

Full Usage: fullunify eqs

Parameters:
    eqs : (term * term) list - The list of term-term pairs to be unified.

Returns: func<string, term> A variable-term mappings (represented as a finite partial function) that is an MGU for eqs, if it is actually unifiable.

Returns an MGU for a list of term-term pairs eqs.

eqs : (term * term) list

The list of term-term pairs to be unified.

Returns: func<string, term>

A variable-term mappings (represented as a finite partial function) that is an MGU for eqs, if it is actually unifiable.

Exception Thrown with message cyclic when there is a cyclic assignment
Exception Thrown with message 'impossible unification' when the unification is not possible.
Example

 fullunify [(!!!"x", !!!"0"); (!!!"x", !!!"y")]
 |> graph
Evaluates to [("x", ``0``); ("y", ``0``)].

Example

 fullunify [!!!"f(x,g(y))", !!!"f(y,x)"]
Throws System.Exception: cyclic.

Example

 fullunify [!!!"0",!!!"1"]
Throws System.Exception: cyclic.

istriv env x t

Full Usage: istriv env x t

Parameters:
    env : func<string, term> - The environment of mappings (represented as a finite partial function) from variables to terms in which to check the new assignment.
    x : string - The variable to be assigned.
    t : term - The term assigned to the variable.

Returns: bool true, if the assignment is trivial (t = x); otherwise, if there are no cycle, false.

Checks the new assignment x |-> t against a given environment of already existing assignments env.

env : func<string, term>

The environment of mappings (represented as a finite partial function) from variables to terms in which to check the new assignment.

x : string

The variable to be assigned.

t : term

The term assigned to the variable.

Returns: bool

true, if the assignment is trivial (t = x); otherwise, if there are no cycle, false.

Exception Thrown with message cyclic, when the new assignment would generate a cycle.
Example

Trivial assignment

 istriv undefined "x" !!!"x"
Evaluates to true.

Example

Acyclic nontrivial assignment

 istriv undefined "x" !!!"y"
Evaluates to false.

Example

Cyclic nontrivial assignment

 istriv (("y" |-> !!!"x")undefined) "x" !!!"f(y)"
Throws System.Exception: cyclic.

solve env

Full Usage: solve env

Parameters:
    env : func<string, term> - The environment of mappings to be reduced.

Returns: func<string, term> An MGU from env.

Reduces an input unifier env to an MGU.

Removes useless mappings from the input variable-term mappings env returned by Unif.unify, giving a most general unifier (MGU).

env : func<string, term>

The environment of mappings to be reduced.

Returns: func<string, term>

An MGU from env.

Note

The function never fails. However, it would cause a StackOverflow crash if called on a cyclic env. Unif.unify is specifically designed not to produce cyclic mappings.

Example

The following reduces \(x \mapsto y, x \mapsto 0\) to \(x \mapsto 0\).

 solve (("x" |-> !!!"0")(("x" |-> !!!"y")undefined))
 |> graph
Evaluates to [("x", ``0``)].

Example

The following returns the input unchanged \(x \mapsto 1, y \mapsto 0\) since the input is already an MGU.

 solve (("y" |-> !!!"0")(("x" |-> !!!"1")undefined))
 |> graph
Evaluates to [("x", ``1``); ("y", ``0``)].

Example

The following would cause a StackOverflow crash.

 solve (("y" |-> !!!"f(y)")undefined)

unify env eqs

Full Usage: unify env eqs

Parameters:
    env : func<string, term> - An environment of mappings (represented as a finite partial function) from variables to terms, used as an accumulator for the final result of the unification procedure.
    eqs : (term * term) list - The list of term-term pairs to be unified.

Returns: func<string, term> A variable-term mappings that unify eqs, if the unification is possible and there are no cycles.

Returns a unifier for a list of term-term pairs eqs.

It applies some transformations to eqs and incorporates the resulting variable-term mappings into env. env might contain mappings that could map a variable to a term containing other variables that are themselves assigned: for example \(x \mapsto y\) and \(y \mapsto z\) instead of just \(x \mapsto z\) directly. The call to Unif.istriv guarantees that there are no cycle or detects it and stops immediately the unification process with a failure.

env : func<string, term>

An environment of mappings (represented as a finite partial function) from variables to terms, used as an accumulator for the final result of the unification procedure.

eqs : (term * term) list

The list of term-term pairs to be unified.

Returns: func<string, term>

A variable-term mappings that unify eqs, if the unification is possible and there are no cycles.

Exception Thrown with message cyclic when there is a cyclic assignment.
Exception Thrown with message impossible unification when the unification is not possible.
Example

No previous assignments

 unify undefined [!!!"x", !!!"0"]
 |> graph
Evaluates to [("x", ``0``)].

Example

Augmented assignment

 unify (("x" |-> !!!"y")undefined) [!!!"x", !!!"0"]
 |> graph
Evaluates to [("x", ``y``); ("y", ``0``)].

Example

Direct cycle (\(y \mapsto f(y)\))

 unify undefined [!!!"y", !!!"f(y)"]
Throws System.Exception: cyclic.

Example

Derived cycle (\(x \mapsto y, y \mapsto f(x)\))

 unify (("x" |-> !!!"y")undefined) [!!!"y", !!!"f(x)"]
Throws System.Exception: cyclic.

Example

Impossible unification

 unify undefined [!!!"0", !!!"1"]
Throws System.Exception: impossible unification.

unify_and_apply eqs

Full Usage: unify_and_apply eqs

Parameters:
    eqs : (term * term) list - The list of term-term pairs to be unified.

Returns: (term * term) list The unified result, if it exists.

Finds and apply an MGU to a list of term-term pairs eqs.

Finds an MGU for a list of term-term pairs eqs, if it is unifiable, and applies the instantiation to give the unified result.

eqs : (term * term) list

The list of term-term pairs to be unified.

Returns: (term * term) list

The unified result, if it exists.

Exception Thrown with message cyclic when there is a cyclic assignment.
Exception Thrown with message impossible unification when the unification is not possible.
Example

 unify_and_apply [!!!"f(x,g(y))",!!!"f(f(z),w)"]
Evaluates to [(``f(f(z),g(y))``, ``f(f(z),g(y))``)].

Example

 unify_and_apply [!!!"f(x,g(y))", !!!"f(y,x)"]
Throws System.Exception: cyclic.

Example

 unify_and_apply [!!!"0",!!!"1"]
Throws System.Exception: impossible unification.