Library SMT

Require Export List.
Require Export ZArith.

Symbols, terms, and formulae


Definition symbol := nat.

Inductive term :=
| t_lit: Z -> term
| t_symb: symbol -> term
| t_add: term -> term -> term
.

Inductive formula :=
| f_eq: term -> term -> formula
| f_lt: term -> term -> formula
| f_and: formula -> formula -> formula
| f_not: formula -> formula.

Definition f_true := f_eq (t_lit 0) (t_lit 0).

A minimal SMT solver


Module Solver.

Definition result0 := option (list formula).

Definition term_dec(t1 t2: term): {t1 = t2} + {t1 <> t2}.
Defined.

Definition formula_dec(f1 f2: formula): {f1 = f2} + {f1 <> f2}.
Defined.

Definition is_fact f facts := in_dec formula_dec f facts.

Definition assume_neq t1 t2 facts :=
  if term_dec t1 t2 then None else
  if is_fact (f_eq t1 t2) facts then None else
  Some (f_not (f_eq t1 t2)::f_not (f_eq t2 t1)::facts).

Definition seq0 op1 (op2: list formula -> result0) (facts: list formula) :=
  match op1 facts with
    None => None
  | Some facts => op2 facts
  end.

Fixpoint assume f facts :=
  if is_fact (f_not f) facts then None else
  match f with
    f_eq t1 t2 =>
    Some (f_eq t1 t2::f_eq t2 t1::facts)
  | f_lt t1 t2 =>
    seq0
      (assume_neq t1 t2)
      (fun facts => Some (f::facts))
      facts
  | f_and f1 f2 =>
    seq0
      (assume f1)
      (assume f2)
      facts
  | f_not f => assume_false f facts
  end
with assume_false f facts :=
  if is_fact f facts then None else
  match f with
    f_eq t1 t2 =>
    if term_dec t1 t2 then None else
    Some (f_not f::facts)
  | f_lt t1 t2 =>
    Some (f_not f::facts)
  | f_and f1 f2 =>
    Some (f_not f::facts)
  | f_not f => assume f facts
  end.

Inductive result := valid | undecided.

Definition solver(f: formula): result :=
  match assume_false f nil with
    None => valid
  | _ => undecided
  end.

End Solver.

Definition solver := Solver.solver.

Definition follows(pc: formula)(goal: formula) :=
  if solver (f_not (f_and pc (f_not goal))) then true else false.

Free symbols


Fixpoint term_symbols t :=
  match t with
    t_lit z => nil
  | t_symb s => s::nil
  | t_add t1 t2 => term_symbols t1 ++ term_symbols t2
  end.

Fixpoint formula_symbols f :=
  match f with
    f_eq t1 t2 => term_symbols t1 ++ term_symbols t2
  | f_lt t1 t2 => term_symbols t1 ++ term_symbols t2
  | f_and f1 f2 => formula_symbols f1 ++ formula_symbols f2
  | f_not f => formula_symbols f
  end.

Definition fresh_symbol ss :=
  fold_left (fun m s => max m (S s)) ss 0.