Calculemus


Fol Module

Basic stuff for first order logic: datatype, parsing and printing, semantics, syntax operations and substitution.

Table of contents

Types

Type Description

fol

Type of atomic first order formulas.

term

Type of first order terms.

Parsing terms

Functions and values

Function or value Description

!!!s

Full Usage: !!!s

Parameters:
    s : string - The input string.

Returns: term The term corresponding to the input, if this is valid.

A convenient operator to make it easier to parse terms.

It is just a shortcut to call Fol.parset.

s : string

The input string.

Returns: term

The term corresponding to the input, if this is valid.

Exception Thrown when the input string is not a valid term.
Example

 !!! "sqrt(1 - power(cos(x + y,2)))"
Evaluates to
 Fn("sqrt",[Fn("-",[Fn("1",[]);
                    Fn("power",[Fn("cos",[Fn("+",[Var "x"; Var "y"]);
                                         Fn("2",[])])])])])

Example

 !!! "sqrt(1 - power(cos(x + y,2"
Throws System.Exception: Closing bracket expected.

!!!>tms

Full Usage: !!!>tms

Parameters:
    tms : string list - The input string list.

Returns: term list The list of parsed terms.

Parses a list of strings into a list of terms.

tms : string list

The input string list.

Returns: term list

The list of parsed terms.

Exception Thrown when one of the input string is not a valid term.
Example

 !!!>["0";"1"]
Evaluates to [``0``;``1``].

!!!>>tms

Full Usage: !!!>>tms

Parameters:
    tms : string list list - The input list of string list.

Returns: term list list The list of parsed term lists.

Parses a list of string list into a list of term lists.

tms : string list list

The input list of string list.

Returns: term list list

The list of parsed term lists.

Exception Thrown when one of the input string is not a valid term.
Example

 !!!>>[["0";"1"];["0"]]
Evaluates to [[``0``;``1``];[``0``]].

parse_atomic_term vs inp

Full Usage: parse_atomic_term vs inp

Parameters:
    vs : string list
    inp : string list

Returns: term * string list

Parses an atomic term.

vs : string list
inp : string list
Returns: term * string list

parse_term vs inp

Full Usage: parse_term vs inp

Parameters:
    vs : string list
    inp : string list

Returns: term * string list

Recursive descent parser of terms built up from an atomic term parser by cascading instances of parse infix in order of precedence, following the conventions with '^' coming highest and '::' lowest.

It takes a list of string tokens `inp` and returns a pair consisting of the parsed term tree together with any unparsed input.

In order to check whether a name is within the scope of a quantifier, it takes an additional argument `vs` which is the set of bound variables in the current scope.

vs : string list
inp : string list
Returns: term * string list

parset s

Full Usage: parset s

Parameters:
    s : string - The input string.

Returns: term The term corresponding to the input, if this is valid.

Parses a string into a term.

s : string

The input string.

Returns: term

The term corresponding to the input, if this is valid.

Exception Thrown when the input string is not a valid term.
Example

 parset "sqrt(1 - power(cos(x + y,2)))"
Evaluates to
 Fn("sqrt",[Fn("-",[Fn("1",[]);
                    Fn("power",[Fn("cos",[Fn("+",[Var "x"; Var "y"]);
                                         Fn("2",[])])])])])

Example

 parset "sqrt(1 - power(cos(x + y,2"
Throws System.Exception: Closing bracket expected.

Parsing formulas

Functions and values

Function or value Description

!!s

Full Usage: !!s

Parameters:
    s : string - The input string.

Returns: formula<fol> The fol formula corresponding to the input, if this is syntactically valid.

A convenient operator to make it easier to parse formulas.

It is just a shortcut to call Fol.parse.

s : string

The input string.

Returns: formula<fol>

The fol formula corresponding to the input, if this is syntactically valid.

Exception Thrown when the input string is not a syntactically valid fol formula.
Example

 parse "x + y < z"
Evaluates to Atom (R ("<", [Fn ("+", [Var "x"; Var "y"]); Var "z"])).

Example

 parse "x + y"
Throws System.Exception: Unparsed input: 2 tokens remaining in buffer.

!!>xs

Full Usage: !!>xs

Parameters:
    xs : string list

Returns: formula<fol> list The list of fol formulas corresponding to the input, if this is syntactically valid.

A convenient operator to make it easier to parse list of formulas.

xs : string list
Returns: formula<fol> list

The list of fol formulas corresponding to the input, if this is syntactically valid.

Exception Thrown when the input strings are not syntactically valid fol formulas.
Example

 !!>["P(x)";"Q(x)"]
Evaluates to [Atom (R ("P", Var "x"); Atom (R ("Q", Var "x")])).

parse s

Full Usage: parse s

Parameters:
    s : string - The input string.

Returns: formula<fol> The fol formula corresponding to the input, if this is valid.

Parses string into a fol formula.

s : string

The input string.

Returns: formula<fol>

The fol formula corresponding to the input, if this is valid.

Exception Thrown when the input string is not a syntactically valid fol formula.
Example

 parse "x + y < z"
Evaluates to Atom (R ("<", [Fn ("+", [Var "x"; Var "y"]); Var "z"])).

Example

 parse "x + y"
Throws System.Exception: Unparsed input: 2 tokens remaining in buffer.

parse_atom vs inp

Full Usage: parse_atom vs inp

Parameters:
    vs : string list
    inp : string list

Returns: formula<fol> * string list

Parses atomic fol

vs : string list
inp : string list
Returns: formula<fol> * string list

parse_infix_atom vs inp

Full Usage: parse_infix_atom vs inp

Parameters:
    vs : string list
    inp : string list

Returns: formula<fol> * string list

A special recognizer for 'infix' atomic formulas like s < t.

vs : string list
inp : string list
Returns: formula<fol> * string list

Printing terms

Functions and values

Function or value Description

fprint_fargs tw f args

Full Usage: fprint_fargs tw f args

Parameters:

Prints a function and its arguments.

tw : TextWriter
f : string
args : term list

fprint_infix_term tw isleft oldprec newprec sym p q

Full Usage: fprint_infix_term tw isleft oldprec newprec sym p q

Parameters:
    tw : TextWriter
    isleft : bool
    oldprec : int
    newprec : int
    sym : string
    p : term
    q : term

Prints an infix operation.

tw : TextWriter
isleft : bool
oldprec : int
newprec : int
sym : string
p : term
q : term

fprint_term tw prec fm

Full Usage: fprint_term tw prec fm

Parameters:

Prints terms.

tw : TextWriter
prec : int
fm : term

fprintert tw tm

Full Usage: fprintert tw tm

Parameters:

Term printer with TextWriter.

tw : TextWriter
tm : term

print_term t

Full Usage: print_term t

Parameters:
    t : term - The input term.

Modifiers: inline

Prints to the stdout the concrete syntax representation of a term.

t : term

The input term.

Example
 Fn("sqrt",[Fn("-",[Fn("1",[]);
                    Fn("power",[Fn("cos",[Fn("+",[Var "x"; Var "y"]);
                                         Fn("2",[])])])])])
 |> print_term
After evaluation the text ``sqrt(1 - power(cos(x + y,2)))`` is printed to the stdout.

sprint_term t

Full Usage: sprint_term t

Parameters:
    t : term - The input term.

Returns: string The terms's concrete syntax representation.
Modifiers: inline

Returns the concrete syntax representation of a term.

Use the interactive option

 fsi.AddPrinter sprint_term
to automatically return the concrete syntax representation of terms when working in an F# interactive session.

t : term

The input term.

Returns: string

The terms's concrete syntax representation.

Note

The opening and closing quotation symbols <<||>> used in the Handbook for terms have been here replaced with ````.

Example

 Fn("sqrt",[Fn("-",[Fn("1",[]);
                    Fn("power",[Fn("cos",[Fn("+",[Var "x"; Var "y"]);
                                         Fn("2",[])])])])])
 |> sprint_term
Evaluates to "``sqrt(1 - power(cos(x + y,2)))``".

sprint_termList tms

Full Usage: sprint_termList tms

Parameters:
Returns: string list

Returns a list of terms with the concrete syntax representation of terms.

tms : term list
Returns: string list
Note

This function is not part of che book code and is added here for convenience.

sprint_termListList tms

Full Usage: sprint_termListList tms

Parameters:
    tms : term list list

Returns: string list list

Returns a list of term lists with the concrete syntax representation of terms.

tms : term list list
Returns: string list list
Note

This function is not part of che book code and is added here for convenience.

Printing formulas

Functions and values

Function or value Description

fprint_atom tw prec arg3

Full Usage: fprint_atom tw prec arg3

Parameters:

Printer of atomic fol formulas with TextWriter.

tw : TextWriter
prec : 'a
arg2 : fol

fprint_fol_formula tw

Full Usage: fprint_fol_formula tw

Parameters:
Returns: formula<fol> -> unit

Printer of fol formulas with TextWriter.

tw : TextWriter
Returns: formula<fol> -> unit

fprint_latex_fol_formula tw

Full Usage: fprint_latex_fol_formula tw

Parameters:
Returns: formula<fol> -> unit

Printer of fol formulas to latex with TextWriter.

tw : TextWriter
Returns: formula<fol> -> unit

print_atom prec arg

Full Usage: print_atom prec arg

Parameters:
    prec : 'a
    arg : fol

Modifiers: inline
Type parameters: 'a

Printer of atomic fol

prec : 'a
arg : fol

print_fol_formula f

Full Usage: print_fol_formula f

Parameters:
Modifiers: inline

Prints to the stdout the concrete syntax representation of a fol formula.

f : formula<fol>

The input formula.

Example
 Forall
 ("x",
  Forall
    ("y",
     Exists
       ("z",
        And
          (Atom (R ("<", [Var "x"; Var "z"])),
           Atom (R ("<", [Var "y"; Var "z"]))))))
After evaluation the text `forall x y. exists z. x < z /\ y < z` is printed to the stdout.

print_latex_fol_formula f

Full Usage: print_latex_fol_formula f

Parameters:
Modifiers: inline

Prints to the stdout the latex syntax of a fol formula.

f : formula<fol>

sprint_atom prec arg

Full Usage: sprint_atom prec arg

Parameters:
    prec : 'a
    arg : fol

Returns: string
Modifiers: inline
Type parameters: 'a

Returns the concrete syntax representation of an atom.

prec : 'a
arg : fol
Returns: string

sprint_fol_formula f

Full Usage: sprint_fol_formula f

Parameters:
Returns: string The formula's concrete syntax representation.
Modifiers: inline

Returns the concrete syntax representation of a fol formula.

Use the interactive option

 fsi.AddPrinter sprint_fol_formula
to automatically return the concrete syntax representation of fol formulas when working in an F# interactive session.

f : formula<fol>

The input formula.

Returns: string

The formula's concrete syntax representation.

Note

The opening and closing quotation symbols <<||>> used in the Handbook for terms have been here replaced with ``.

Example

 Forall
 ("x",
  Forall
    ("y",
     Exists
       ("z",
        And
          (Atom (R ("<", [Var "x"; Var "z"])),
           Atom (R ("<", [Var "y"; Var "z"]))))))
 |> sprint_fol_formula
Evaluates to "`forall x y. exists z. x < z /\ y < z`".

sprint_latex_fol_formula f

Full Usage: sprint_latex_fol_formula f

Parameters:
Returns: string
Modifiers: inline

Returns the latex syntax of a fol formula.

f : formula<fol>
Returns: string

Semantics

Functions and values

Function or value Description

holds (domain, func, pred) v fm

Full Usage: holds (domain, func, pred) v fm

Parameters:
    domain : 'a list - The domain of the interpretation: the non-empty set \(D\) in which the value of each term lies.
    func : string -> 'a list -> 'a - The mapping of each \(n\)-ary function symbol to a function \(f_M : D^n \rightarrow D\).
    pred : string -> 'a list -> bool - The mapping of each \(n\)-ary predicate symbol to a boolean function \(P_M : D^n \rightarrow \{falso,vero\}\).
    v : func<string, 'a> - The valuation of the variables: an fpf that maps each variable to an element of the domain.
    fm : formula<fol> - The input formula.

Returns: bool The truth-value of the formula in the given interpretation and valuation.

Evaluates a fol formula fm in the interpretation (say \(M\)) specified by the triplet domain, func, pred and the variables valuation v.

domain : 'a list

The domain of the interpretation: the non-empty set \(D\) in which the value of each term lies.

func : string -> 'a list -> 'a

The mapping of each \(n\)-ary function symbol to a function \(f_M : D^n \rightarrow D\).

pred : string -> 'a list -> bool

The mapping of each \(n\)-ary predicate symbol to a boolean function \(P_M : D^n \rightarrow \{falso,vero\}\).

v : func<string, 'a>

The valuation of the variables: an fpf that maps each variable to an element of the domain.

fm : formula<fol>

The input formula.

Returns: bool

The truth-value of the formula in the given interpretation and valuation.

Example

 let fm = !! @"forall x. (x = 0) \/ (x = 1)" 
   
 holds bool_interp undefined fm    // evaluates to true
 holds (mod_interp 2) undefined fm // evaluates to true
 holds (mod_interp 3) undefined fm // evaluates to false
val fm: obj

termval (domain, func, pred) v tm

Full Usage: termval (domain, func, pred) v tm

Parameters:
    domain : 'a - The domain of the interpretation: the non-empty set \(D\) in which the value of each term lies.
    func : string -> 'b list -> 'b - The mapping of each \(n\)-ary function symbol to a function \(f_M : D^n \rightarrow D\).
    pred : 'c - The mapping of each \(n\)-ary predicate symbol to a boolean function \(P_M : D^n \rightarrow \{falso,vero\}\).
    v : func<string, 'b> - The valuation of the variables: an fpf that maps each variable to an element of the domain.
    tm : term - The input term.

Returns: 'b The element of the domain that corresponds to the term value in the given interpretation and valuation.

Returns the value of a term tm in the interpretation (say \(M\)) specified by the triplet domain, func, pred and valuation v.

domain : 'a

The domain of the interpretation: the non-empty set \(D\) in which the value of each term lies.

func : string -> 'b list -> 'b

The mapping of each \(n\)-ary function symbol to a function \(f_M : D^n \rightarrow D\).

pred : 'c

The mapping of each \(n\)-ary predicate symbol to a boolean function \(P_M : D^n \rightarrow \{falso,vero\}\).

v : func<string, 'b>

The valuation of the variables: an fpf that maps each variable to an element of the domain.

tm : term

The input term.

Returns: 'b

The element of the domain that corresponds to the term value in the given interpretation and valuation.

Example

 !!! "0" |> termval bool_interp undefined    // evaluates to false
 !!! "0" |> termval (mod_interp 3) undefined // evaluates to 0

Sample interpretations

Functions and values

Function or value Description

bool_interp

Full Usage: bool_interp

Returns: bool list * (string -> bool list -> bool) * (string -> 'a list -> bool)
  • Domain \(\{true, false\}\)
  • Function symbols mapping: \(\text{0} \mapsto false, \text{1} \mapsto true, \text{+} \mapsto \lor, * \mapsto \land\)
  • Predicate symbols mapping: \(\text{=} \mapsto =\)

An interpretation a la Boole.

Returns: bool list * (string -> bool list -> bool) * (string -> 'a list -> bool)

  • Domain \(\{true, false\}\)
  • Function symbols mapping: \(\text{0} \mapsto false, \text{1} \mapsto true, \text{+} \mapsto \lor, * \mapsto \land\)
  • Predicate symbols mapping: \(\text{=} \mapsto =\)

mod_interp n

Full Usage: mod_interp n

Parameters:
    n : int - The input modulo.

Returns: int list * (string -> int list -> int) * (string -> 'a list -> bool)
  • Domain \(\{0,\ldots, n-1\}\)
  • Function symbols mapping: \(\text{0} \mapsto 0, \text{1} \mapsto 1 \mod n, \text{+} \mapsto (x + y) \mod n, * \mapsto (x * y) \mod n\)
  • Predicate symbols mapping: \(\text{=} \mapsto =\)

An arithmetic modulo n interpretation.

n : int

The input modulo.

Returns: int list * (string -> int list -> int) * (string -> 'a list -> bool)

  • Domain \(\{0,\ldots, n-1\}\)
  • Function symbols mapping: \(\text{0} \mapsto 0, \text{1} \mapsto 1 \mod n, \text{+} \mapsto (x + y) \mod n, * \mapsto (x * y) \mod n\)
  • Predicate symbols mapping: \(\text{=} \mapsto =\)

Example

 !!! "0" |> termval (mod_interp 3) undefined                 // evaluates to 0
 !!! "1" |> termval (mod_interp 3) undefined                 // evaluates to 1
 !!! "1 + 1" |> termval (mod_interp 3) undefined             // evaluates to 2
 !!! "1 + 1 + 1" |> termval (mod_interp 3) undefined         // evaluates to 0
 !!! "1 + 1 + 1 + 1" |> termval (mod_interp 3) undefined     // evaluates to 1
 !!! "1 + 1 + 1 + 1 + 1" |> termval (mod_interp 3) undefined // evaluates to 2

Free variables

Functions and values

Function or value Description

fv fm

Full Usage: fv fm

Parameters:
Returns: string list The list of the free variables in the formula fm

Returns the free variables in the fol formula fm.

fm : formula<fol>

The input formula.

Returns: string list

The list of the free variables in the formula fm

Example

 fv !!"forall x. x + f(y,z) > w"
Evaluates to ["w"; "y"; "z"].

fvt tm

Full Usage: fvt tm

Parameters:
    tm : term - The input term.

Returns: string list The list of free variables in the term tm

Returns the free variables in the term tm.

tm : term

The input term.

Returns: string list

The list of free variables in the term tm

Example

 fvt !!!"x + f(y,z)"
Evaluates to ["x"; "y"; "z"].

var fm

Full Usage: var fm

Parameters:
Returns: string list The list of all the variables in the formula fm

Returns all the variables in the fol formula fm.

fm : formula<fol>

The input formula.

Returns: string list

The list of all the variables in the formula fm

Example

 var !!"forall x. x + f(y,z) > w"
Evaluates to ["w"; "x"; "y"; "z"].

Substitution

Functions and values

Function or value Description

subst subfn fm

Full Usage: subst subfn fm

Parameters:
    subfn : func<string, term> - The fpf that contains the mapping for the variables to replace.
    fm : formula<fol> - The input formula.

Returns: formula<fol> The formula obtained from fm by replacing its variables with the given mappings.

Applies the substitution function subfn to fm.

Bound variables will be renamed if necessary to avoid capture.

subfn : func<string, term>

The fpf that contains the mapping for the variables to replace.

fm : formula<fol>

The input formula.

Returns: formula<fol>

The formula obtained from fm by replacing its variables with the given mappings.

Example

 subst ("y" |=> Var "x") !!"forall x. x = y"
Evaluates to `forall x'. x' = x`.

Example

 !!"forall x x'. x = y ==> x = x'"
 |> subst ("y" |=> Var "x")
Evaluates to `forall x' x''. x' = x ==> x' = x''`.

substq subfn quant x p

Full Usage: substq subfn quant x p

Parameters:
    subfn : func<string, term> - The fpf that contains the mapping for the variables to replace.
    quant : string -> formula<fol> -> formula<fol> - The quantification constructor to apply when reconstructing a quantified formula.
    x : string - The variable to check.
    p : formula<fol> - The input formula.

Returns: formula<fol> The reconstructed quantified formula with appropriate variables variants if needed.

Checks for variable captures in quantified formulas substitutions.

Checks whether there would be variable capture if the bound variable x is not renamed and, if so, creates the appropriate variants.

It is use to define the Fol.subst quantified formulas steps.

subfn : func<string, term>

The fpf that contains the mapping for the variables to replace.

quant : string -> formula<fol> -> formula<fol>

The quantification constructor to apply when reconstructing a quantified formula.

x : string

The variable to check.

p : formula<fol>

The input formula.

Returns: formula<fol>

The reconstructed quantified formula with appropriate variables variants if needed.

Example

 substq ("y" |=> Var "x") mk_forall "x" !!"x = y"
Evaluates to `forall x'. x' = x`.

tsubst subfn tm

Full Usage: tsubst subfn tm

Parameters:
    subfn : func<string, term> - The fpf that contains the mapping for the variables to replace.
    tm : term - The input term.

Returns: term The term obtained from tm by replacing its variables with the given mappings.

Applies the substitution function subfn to tm.

Replaces every variable in tm that matches an argument of sfn with its value.

subfn : func<string, term>

The fpf that contains the mapping for the variables to replace.

tm : term

The input term.

Returns: term

The term obtained from tm by replacing its variables with the given mappings.

Example

 !!!"x + f(y,z)" 
 |> tsubst (fpf ["x";"z"] [!!!"1";!!!"2"])
Evaluates to ``1 + f(y,2)``.

variant x vars

Full Usage: variant x vars

Parameters:
    x : string - The input variable name.
    vars : string list - The list of names to avoid.

Returns: string The x itself it if is not contained in vars; otherwise, the string obtained from x adding prime characters to it until it is distinct from every element of vars.

Creates a variant of the variable name x given a list of names (vars) to avoid.

Creates a 'variant' of a variable name by adding prime characters to it until it is distinct from every element of vars.

x : string

The input variable name.

vars : string list

The list of names to avoid.

Returns: string

The x itself it if is not contained in vars; otherwise, the string obtained from x adding prime characters to it until it is distinct from every element of vars.

Example

 variant "x" ["y"; "z"]  // evaluates to "x"
 variant "x" ["x"; "y"]  // evaluates to "x'"
 variant "x" ["x"; "x'"] // evaluates to "x''"

Other syntax operations

Functions and values

Function or value Description

generalize fm

Full Usage: generalize fm

Parameters:
Returns: formula<fol> The universal closure of fm.

Universal closure of a formula.

Binds every free variable in the formula with a universal quantifier.

fm : formula<fol>

The input formula.

Returns: formula<fol>

The universal closure of fm.

Example

 generalize !!"x + f(y,z) > w"
Evaluates to `forall w x y z. x + f(y,z) > w`.

is_const_name s

Full Usage: is_const_name s

Parameters:
    s : string - The input string.

Returns: bool true, if the input is a constant term, otherwise false.

Checks if a string is a constant term.

Only numerals and the empty list constant nil are considered as constants.

s : string

The input string.

Returns: bool

true, if the input is a constant term, otherwise false.

Example

 is_const_name "nil" // evaluates to true
 is_const_name "123" // evaluates to true
 is_const_name "x"   // evaluates to false

onformula f fm

Full Usage: onformula f fm

Parameters:
Returns: formula<fol> The result of applying f.

Applies a function f to all the top terms in a fol formula fm, but otherwise leaves the structure unchanged.

It is similar to Formulas.onatoms but specific for fol formulas.

f : term -> term

The function to apply.

fm : formula<fol>

The input formula.

Returns: formula<fol>

The result of applying f.

Example

 !! "P(x,f(z)) ==> Q(x)"
 |> onformula (function 
     | Var x -> Var (x + "_1") 
     | tm -> tm
 )
Evaluates to `P(x_1,f(z)) ==> Q(x_1)`.