src/HOL/Tools/prop_logic.ML
author huffman
Fri Mar 30 12:32:35 2012 +0200 (2012-03-30)
changeset 47220 52426c62b5d0
parent 45740 132a3e1c0fe5
child 55436 9781e17dcc23
permissions -rw-r--r--
replace lemmas eval_nat_numeral with a simpler reformulation
webertj@14452
     1
(*  Title:      HOL/Tools/prop_logic.ML
webertj@14452
     2
    Author:     Tjark Weber
webertj@31220
     3
    Copyright   2004-2009
webertj@14452
     4
webertj@14452
     5
Formulas of propositional logic.
webertj@14452
     6
*)
webertj@14452
     7
webertj@14452
     8
signature PROP_LOGIC =
webertj@14452
     9
sig
wenzelm@41447
    10
  datatype prop_formula =
wenzelm@41447
    11
      True
wenzelm@41447
    12
    | False
wenzelm@41447
    13
    | BoolVar of int  (* NOTE: only use indices >= 1 *)
wenzelm@41447
    14
    | Not of prop_formula
wenzelm@41447
    15
    | Or of prop_formula * prop_formula
wenzelm@41447
    16
    | And of prop_formula * prop_formula
webertj@14452
    17
wenzelm@41447
    18
  val SNot: prop_formula -> prop_formula
wenzelm@41447
    19
  val SOr: prop_formula * prop_formula -> prop_formula
wenzelm@41447
    20
  val SAnd: prop_formula * prop_formula -> prop_formula
wenzelm@41447
    21
  val simplify: prop_formula -> prop_formula  (* eliminates True/False and double-negation *)
webertj@14452
    22
wenzelm@41447
    23
  val indices: prop_formula -> int list  (* set of all variable indices *)
wenzelm@41447
    24
  val maxidx: prop_formula -> int       (* maximal variable index *)
webertj@14452
    25
wenzelm@41447
    26
  val exists: prop_formula list -> prop_formula  (* finite disjunction *)
wenzelm@41447
    27
  val all: prop_formula list -> prop_formula  (* finite conjunction *)
wenzelm@41447
    28
  val dot_product: prop_formula list * prop_formula list -> prop_formula
webertj@14452
    29
wenzelm@41447
    30
  val is_nnf: prop_formula -> bool  (* returns true iff the formula is in negation normal form *)
wenzelm@41447
    31
  val is_cnf: prop_formula -> bool  (* returns true iff the formula is in conjunctive normal form *)
webertj@22441
    32
wenzelm@41447
    33
  val nnf: prop_formula -> prop_formula  (* negation normal form *)
wenzelm@41447
    34
  val cnf: prop_formula -> prop_formula  (* conjunctive normal form *)
wenzelm@41447
    35
  val defcnf: prop_formula -> prop_formula  (* definitional cnf *)
webertj@16909
    36
wenzelm@41447
    37
  val eval: (int -> bool) -> prop_formula -> bool  (* semantics *)
webertj@17809
    38
wenzelm@41447
    39
  (* propositional representation of HOL terms *)
wenzelm@41447
    40
  val prop_formula_of_term: term -> int Termtab.table -> prop_formula * int Termtab.table
wenzelm@41447
    41
  (* HOL term representation of propositional formulae *)
wenzelm@41447
    42
  val term_of_prop_formula: prop_formula -> term
webertj@14452
    43
end;
webertj@14452
    44
wenzelm@41471
    45
structure Prop_Logic : PROP_LOGIC =
webertj@14452
    46
struct
webertj@14452
    47
webertj@14452
    48
(* ------------------------------------------------------------------------- *)
webertj@14753
    49
(* prop_formula: formulas of propositional logic, built from Boolean         *)
webertj@14452
    50
(*               variables (referred to by index) and True/False using       *)
webertj@14452
    51
(*               not/or/and                                                  *)
webertj@14452
    52
(* ------------------------------------------------------------------------- *)
webertj@14452
    53
wenzelm@41447
    54
datatype prop_formula =
wenzelm@41447
    55
    True
wenzelm@41447
    56
  | False
wenzelm@41447
    57
  | BoolVar of int  (* NOTE: only use indices >= 1 *)
wenzelm@41447
    58
  | Not of prop_formula
wenzelm@41447
    59
  | Or of prop_formula * prop_formula
wenzelm@41447
    60
  | And of prop_formula * prop_formula;
webertj@14452
    61
webertj@14452
    62
(* ------------------------------------------------------------------------- *)
webertj@14452
    63
(* The following constructor functions make sure that True and False do not  *)
webertj@14452
    64
(* occur within any of the other connectives (i.e. Not, Or, And), and        *)
webertj@14452
    65
(* perform double-negation elimination.                                      *)
webertj@14452
    66
(* ------------------------------------------------------------------------- *)
webertj@14452
    67
wenzelm@41447
    68
fun SNot True = False
wenzelm@41447
    69
  | SNot False = True
wenzelm@41447
    70
  | SNot (Not fm) = fm
wenzelm@41447
    71
  | SNot fm = Not fm;
webertj@14452
    72
wenzelm@41447
    73
fun SOr (True, _) = True
wenzelm@41447
    74
  | SOr (_, True) = True
wenzelm@41447
    75
  | SOr (False, fm) = fm
wenzelm@41447
    76
  | SOr (fm, False) = fm
wenzelm@41447
    77
  | SOr (fm1, fm2) = Or (fm1, fm2);
webertj@14452
    78
wenzelm@41447
    79
fun SAnd (True, fm) = fm
wenzelm@41447
    80
  | SAnd (fm, True) = fm
wenzelm@41447
    81
  | SAnd (False, _) = False
wenzelm@41447
    82
  | SAnd (_, False) = False
wenzelm@41447
    83
  | SAnd (fm1, fm2) = And (fm1, fm2);
webertj@14452
    84
webertj@14452
    85
(* ------------------------------------------------------------------------- *)
webertj@16909
    86
(* simplify: eliminates True/False below other connectives, and double-      *)
webertj@16909
    87
(*      negation                                                             *)
webertj@16909
    88
(* ------------------------------------------------------------------------- *)
webertj@16909
    89
wenzelm@41447
    90
fun simplify (Not fm) = SNot (simplify fm)
wenzelm@41447
    91
  | simplify (Or (fm1, fm2)) = SOr (simplify fm1, simplify fm2)
wenzelm@41447
    92
  | simplify (And (fm1, fm2)) = SAnd (simplify fm1, simplify fm2)
wenzelm@41447
    93
  | simplify fm = fm;
webertj@16909
    94
webertj@16909
    95
(* ------------------------------------------------------------------------- *)
webertj@14753
    96
(* indices: collects all indices of Boolean variables that occur in a        *)
webertj@14452
    97
(*      propositional formula 'fm'; no duplicates                            *)
webertj@14452
    98
(* ------------------------------------------------------------------------- *)
webertj@14452
    99
wenzelm@41447
   100
fun indices True = []
wenzelm@41447
   101
  | indices False = []
wenzelm@41447
   102
  | indices (BoolVar i) = [i]
wenzelm@41447
   103
  | indices (Not fm) = indices fm
wenzelm@41447
   104
  | indices (Or (fm1, fm2)) = union (op =) (indices fm1) (indices fm2)
wenzelm@41447
   105
  | indices (And (fm1, fm2)) = union (op =) (indices fm1) (indices fm2);
webertj@14452
   106
webertj@14452
   107
(* ------------------------------------------------------------------------- *)
webertj@14452
   108
(* maxidx: computes the maximal variable index occuring in a formula of      *)
webertj@14452
   109
(*      propositional logic 'fm'; 0 if 'fm' contains no variable             *)
webertj@14452
   110
(* ------------------------------------------------------------------------- *)
webertj@14452
   111
wenzelm@41447
   112
fun maxidx True = 0
wenzelm@41447
   113
  | maxidx False = 0
wenzelm@41447
   114
  | maxidx (BoolVar i) = i
wenzelm@41447
   115
  | maxidx (Not fm) = maxidx fm
wenzelm@41447
   116
  | maxidx (Or (fm1, fm2)) = Int.max (maxidx fm1, maxidx fm2)
wenzelm@41447
   117
  | maxidx (And (fm1, fm2)) = Int.max (maxidx fm1, maxidx fm2);
webertj@16909
   118
webertj@16909
   119
(* ------------------------------------------------------------------------- *)
webertj@16909
   120
(* exists: computes the disjunction over a list 'xs' of propositional        *)
webertj@16909
   121
(*      formulas                                                             *)
webertj@16909
   122
(* ------------------------------------------------------------------------- *)
webertj@16909
   123
wenzelm@41447
   124
fun exists xs = Library.foldl SOr (False, xs);
webertj@16909
   125
webertj@16909
   126
(* ------------------------------------------------------------------------- *)
webertj@16909
   127
(* all: computes the conjunction over a list 'xs' of propositional formulas  *)
webertj@16909
   128
(* ------------------------------------------------------------------------- *)
webertj@16909
   129
wenzelm@41447
   130
fun all xs = Library.foldl SAnd (True, xs);
webertj@16909
   131
webertj@16909
   132
(* ------------------------------------------------------------------------- *)
webertj@16909
   133
(* dot_product: ([x1,...,xn], [y1,...,yn]) -> x1*y1+...+xn*yn                *)
webertj@16909
   134
(* ------------------------------------------------------------------------- *)
webertj@16909
   135
wenzelm@41447
   136
fun dot_product (xs, ys) = exists (map SAnd (xs ~~ ys));
webertj@14452
   137
webertj@14452
   138
(* ------------------------------------------------------------------------- *)
webertj@31220
   139
(* is_nnf: returns 'true' iff the formula is in negation normal form (i.e.,  *)
webertj@22441
   140
(*         only variables may be negated, but not subformulas).              *)
webertj@22441
   141
(* ------------------------------------------------------------------------- *)
webertj@22441
   142
wenzelm@41447
   143
local
wenzelm@41447
   144
  fun is_literal (BoolVar _) = true
wenzelm@41447
   145
    | is_literal (Not (BoolVar _)) = true
wenzelm@41447
   146
    | is_literal _ = false
wenzelm@41447
   147
  fun is_conj_disj (Or (fm1, fm2)) = is_conj_disj fm1 andalso is_conj_disj fm2
wenzelm@41447
   148
    | is_conj_disj (And (fm1, fm2)) = is_conj_disj fm1 andalso is_conj_disj fm2
wenzelm@41447
   149
    | is_conj_disj fm = is_literal fm
wenzelm@41447
   150
in
wenzelm@41447
   151
  fun is_nnf True = true
wenzelm@41447
   152
    | is_nnf False = true
wenzelm@41447
   153
    | is_nnf fm = is_conj_disj fm
wenzelm@41447
   154
end;
webertj@22441
   155
webertj@22441
   156
(* ------------------------------------------------------------------------- *)
webertj@22441
   157
(* is_cnf: returns 'true' iff the formula is in conjunctive normal form      *)
webertj@31220
   158
(*         (i.e., a conjunction of disjunctions of literals). 'is_cnf'       *)
webertj@31220
   159
(*         implies 'is_nnf'.                                                 *)
webertj@22441
   160
(* ------------------------------------------------------------------------- *)
webertj@22441
   161
wenzelm@41447
   162
local
wenzelm@41447
   163
  fun is_literal (BoolVar _) = true
wenzelm@41447
   164
    | is_literal (Not (BoolVar _)) = true
wenzelm@41447
   165
    | is_literal _ = false
wenzelm@41447
   166
  fun is_disj (Or (fm1, fm2)) = is_disj fm1 andalso is_disj fm2
wenzelm@41447
   167
    | is_disj fm = is_literal fm
wenzelm@41447
   168
  fun is_conj (And (fm1, fm2)) = is_conj fm1 andalso is_conj fm2
wenzelm@41447
   169
    | is_conj fm = is_disj fm
wenzelm@41447
   170
in
wenzelm@41447
   171
  fun is_cnf True = true
wenzelm@41447
   172
    | is_cnf False = true
wenzelm@41447
   173
    | is_cnf fm = is_conj fm
wenzelm@41447
   174
end;
webertj@22441
   175
webertj@22441
   176
(* ------------------------------------------------------------------------- *)
webertj@14452
   177
(* nnf: computes the negation normal form of a formula 'fm' of propositional *)
webertj@31220
   178
(*      logic (i.e., only variables may be negated, but not subformulas).    *)
webertj@31220
   179
(*      Simplification (cf. 'simplify') is performed as well. Not            *)
webertj@31220
   180
(*      surprisingly, 'is_nnf o nnf' always returns 'true'. 'nnf fm' returns *)
webertj@31220
   181
(*      'fm' if (and only if) 'is_nnf fm' returns 'true'.                    *)
webertj@14452
   182
(* ------------------------------------------------------------------------- *)
webertj@14452
   183
wenzelm@41447
   184
fun nnf fm =
wenzelm@41447
   185
  let
wenzelm@41447
   186
    fun
wenzelm@41447
   187
      (* constants *)
wenzelm@41447
   188
        nnf_aux True = True
wenzelm@41447
   189
      | nnf_aux False = False
wenzelm@41447
   190
      (* variables *)
wenzelm@41447
   191
      | nnf_aux (BoolVar i) = (BoolVar i)
wenzelm@41447
   192
      (* 'or' and 'and' as outermost connectives are left untouched *)
wenzelm@41447
   193
      | nnf_aux (Or  (fm1, fm2)) = SOr (nnf_aux fm1, nnf_aux fm2)
wenzelm@41447
   194
      | nnf_aux (And (fm1, fm2)) = SAnd (nnf_aux fm1, nnf_aux fm2)
wenzelm@41447
   195
      (* 'not' + constant *)
wenzelm@41447
   196
      | nnf_aux (Not True) = False
wenzelm@41447
   197
      | nnf_aux (Not False) = True
wenzelm@41447
   198
      (* 'not' + variable *)
wenzelm@41447
   199
      | nnf_aux (Not (BoolVar i)) = Not (BoolVar i)
wenzelm@41447
   200
      (* pushing 'not' inside of 'or'/'and' using de Morgan's laws *)
wenzelm@41447
   201
      | nnf_aux (Not (Or  (fm1, fm2))) = SAnd (nnf_aux (SNot fm1), nnf_aux (SNot fm2))
wenzelm@41447
   202
      | nnf_aux (Not (And (fm1, fm2))) = SOr  (nnf_aux (SNot fm1), nnf_aux (SNot fm2))
wenzelm@41447
   203
      (* double-negation elimination *)
wenzelm@41447
   204
      | nnf_aux (Not (Not fm)) = nnf_aux fm
wenzelm@41447
   205
  in
wenzelm@41447
   206
    if is_nnf fm then fm
wenzelm@41447
   207
    else nnf_aux fm
wenzelm@41447
   208
  end;
webertj@14452
   209
webertj@14452
   210
(* ------------------------------------------------------------------------- *)
webertj@31220
   211
(* cnf: computes the conjunctive normal form (i.e., a conjunction of         *)
webertj@31220
   212
(*      disjunctions of literals) of a formula 'fm' of propositional logic.  *)
webertj@31220
   213
(*      Simplification (cf. 'simplify') is performed as well. The result     *)
webertj@31220
   214
(*      is equivalent to 'fm', but may be exponentially longer. Not          *)
webertj@31220
   215
(*      surprisingly, 'is_cnf o cnf' always returns 'true'. 'cnf fm' returns *)
webertj@31220
   216
(*      'fm' if (and only if) 'is_cnf fm' returns 'true'.                    *)
webertj@14452
   217
(* ------------------------------------------------------------------------- *)
webertj@14452
   218
wenzelm@41447
   219
fun cnf fm =
wenzelm@41447
   220
  let
wenzelm@41447
   221
    (* function to push an 'Or' below 'And's, using distributive laws *)
wenzelm@41447
   222
    fun cnf_or (And (fm11, fm12), fm2) =
wenzelm@41447
   223
          And (cnf_or (fm11, fm2), cnf_or (fm12, fm2))
wenzelm@41447
   224
      | cnf_or (fm1, And (fm21, fm22)) =
wenzelm@41447
   225
          And (cnf_or (fm1, fm21), cnf_or (fm1, fm22))
wenzelm@41447
   226
    (* neither subformula contains 'And' *)
wenzelm@41447
   227
      | cnf_or (fm1, fm2) = Or (fm1, fm2)
wenzelm@41447
   228
    fun cnf_from_nnf True = True
wenzelm@41447
   229
      | cnf_from_nnf False = False
wenzelm@41447
   230
      | cnf_from_nnf (BoolVar i) = BoolVar i
wenzelm@41447
   231
    (* 'fm' must be a variable since the formula is in NNF *)
wenzelm@41447
   232
      | cnf_from_nnf (Not fm) = Not fm
wenzelm@41447
   233
    (* 'Or' may need to be pushed below 'And' *)
wenzelm@41447
   234
      | cnf_from_nnf (Or (fm1, fm2)) =
wenzelm@41447
   235
        cnf_or (cnf_from_nnf fm1, cnf_from_nnf fm2)
wenzelm@41447
   236
    (* 'And' as outermost connective is left untouched *)
wenzelm@41447
   237
      | cnf_from_nnf (And (fm1, fm2)) =
wenzelm@41447
   238
        And (cnf_from_nnf fm1, cnf_from_nnf fm2)
wenzelm@41447
   239
  in
wenzelm@41447
   240
    if is_cnf fm then fm
wenzelm@41447
   241
    else (cnf_from_nnf o nnf) fm
wenzelm@41447
   242
  end;
webertj@14452
   243
webertj@14452
   244
(* ------------------------------------------------------------------------- *)
webertj@31220
   245
(* defcnf: computes a definitional conjunctive normal form of a formula 'fm' *)
webertj@31220
   246
(*      of propositional logic. Simplification (cf. 'simplify') is performed *)
webertj@31220
   247
(*      as well. 'defcnf' may introduce auxiliary Boolean variables to avoid *)
webertj@31220
   248
(*      an exponential blowup of the formula.  The result is equisatisfiable *)
webertj@31220
   249
(*      (i.e., satisfiable if and only if 'fm' is satisfiable), but not      *)
webertj@31220
   250
(*      necessarily equivalent to 'fm'. Not surprisingly, 'is_cnf o defcnf'  *)
webertj@31220
   251
(*      always returns 'true'. 'defcnf fm' returns 'fm' if (and only if)     *)
webertj@31220
   252
(*      'is_cnf fm' returns 'true'.                                          *)
webertj@14452
   253
(* ------------------------------------------------------------------------- *)
webertj@14452
   254
wenzelm@41447
   255
fun defcnf fm =
wenzelm@41447
   256
  if is_cnf fm then fm
wenzelm@41447
   257
  else
wenzelm@41447
   258
    let
wenzelm@41447
   259
      val fm' = nnf fm
wenzelm@41447
   260
      (* 'new' specifies the next index that is available to introduce an auxiliary variable *)
wenzelm@41447
   261
      (* int ref *)
wenzelm@41447
   262
      val new = Unsynchronized.ref (maxidx fm' + 1)
wenzelm@41447
   263
      (* unit -> int *)
wenzelm@41447
   264
      fun new_idx () = let val idx = !new in new := idx+1; idx end
wenzelm@41447
   265
      (* replaces 'And' by an auxiliary variable (and its definition) *)
wenzelm@41447
   266
      (* prop_formula -> prop_formula * prop_formula list *)
wenzelm@41447
   267
      fun defcnf_or (And x) =
wenzelm@41447
   268
            let
wenzelm@41447
   269
              val i = new_idx ()
wenzelm@41447
   270
            in
wenzelm@41447
   271
              (* Note that definitions are in NNF, but not CNF. *)
wenzelm@41447
   272
              (BoolVar i, [Or (Not (BoolVar i), And x)])
wenzelm@41447
   273
            end
wenzelm@41447
   274
        | defcnf_or (Or (fm1, fm2)) =
wenzelm@41447
   275
            let
wenzelm@41447
   276
              val (fm1', defs1) = defcnf_or fm1
wenzelm@41447
   277
              val (fm2', defs2) = defcnf_or fm2
wenzelm@41447
   278
            in
wenzelm@41447
   279
              (Or (fm1', fm2'), defs1 @ defs2)
wenzelm@41447
   280
            end
wenzelm@41447
   281
        | defcnf_or fm = (fm, [])
wenzelm@41447
   282
      (* prop_formula -> prop_formula *)
wenzelm@41447
   283
      fun defcnf_from_nnf True = True
wenzelm@41447
   284
        | defcnf_from_nnf False = False
wenzelm@41447
   285
        | defcnf_from_nnf (BoolVar i) = BoolVar i
wenzelm@41447
   286
      (* 'fm' must be a variable since the formula is in NNF *)
wenzelm@41447
   287
        | defcnf_from_nnf (Not fm) = Not fm
wenzelm@41447
   288
      (* 'Or' may need to be pushed below 'And' *)
wenzelm@41447
   289
      (* 'Or' of literal and 'And': use distributivity *)
wenzelm@41447
   290
        | defcnf_from_nnf (Or (BoolVar i, And (fm1, fm2))) =
wenzelm@41447
   291
            And (defcnf_from_nnf (Or (BoolVar i, fm1)),
wenzelm@41447
   292
                 defcnf_from_nnf (Or (BoolVar i, fm2)))
wenzelm@41447
   293
        | defcnf_from_nnf (Or (Not (BoolVar i), And (fm1, fm2))) =
wenzelm@41447
   294
            And (defcnf_from_nnf (Or (Not (BoolVar i), fm1)),
wenzelm@41447
   295
                 defcnf_from_nnf (Or (Not (BoolVar i), fm2)))
wenzelm@41447
   296
        | defcnf_from_nnf (Or (And (fm1, fm2), BoolVar i)) =
wenzelm@41447
   297
            And (defcnf_from_nnf (Or (fm1, BoolVar i)),
wenzelm@41447
   298
                 defcnf_from_nnf (Or (fm2, BoolVar i)))
wenzelm@41447
   299
        | defcnf_from_nnf (Or (And (fm1, fm2), Not (BoolVar i))) =
wenzelm@41447
   300
            And (defcnf_from_nnf (Or (fm1, Not (BoolVar i))),
wenzelm@41447
   301
                 defcnf_from_nnf (Or (fm2, Not (BoolVar i))))
wenzelm@41447
   302
      (* all other cases: turn the formula into a disjunction of literals, *)
wenzelm@41447
   303
      (*                  adding definitions as necessary                  *)
wenzelm@41447
   304
        | defcnf_from_nnf (Or x) =
wenzelm@41447
   305
            let
wenzelm@41447
   306
              val (fm, defs) = defcnf_or (Or x)
wenzelm@41447
   307
              val cnf_defs = map defcnf_from_nnf defs
wenzelm@41447
   308
            in
wenzelm@41447
   309
              all (fm :: cnf_defs)
wenzelm@41447
   310
            end
wenzelm@41447
   311
      (* 'And' as outermost connective is left untouched *)
wenzelm@41447
   312
        | defcnf_from_nnf (And (fm1, fm2)) =
wenzelm@41447
   313
            And (defcnf_from_nnf fm1, defcnf_from_nnf fm2)
wenzelm@41447
   314
    in
wenzelm@41447
   315
      defcnf_from_nnf fm'
wenzelm@41447
   316
    end;
webertj@14452
   317
webertj@14452
   318
(* ------------------------------------------------------------------------- *)
webertj@14753
   319
(* eval: given an assignment 'a' of Boolean values to variable indices, the  *)
webertj@14452
   320
(*      truth value of a propositional formula 'fm' is computed              *)
webertj@14452
   321
(* ------------------------------------------------------------------------- *)
webertj@14452
   322
wenzelm@41447
   323
fun eval a True = true
wenzelm@41447
   324
  | eval a False = false
wenzelm@41447
   325
  | eval a (BoolVar i) = (a i)
wenzelm@41447
   326
  | eval a (Not fm) = not (eval a fm)
wenzelm@41447
   327
  | eval a (Or (fm1, fm2)) = (eval a fm1) orelse (eval a fm2)
wenzelm@41447
   328
  | eval a (And (fm1, fm2)) = (eval a fm1) andalso (eval a fm2);
webertj@14452
   329
webertj@17809
   330
(* ------------------------------------------------------------------------- *)
webertj@17809
   331
(* prop_formula_of_term: returns the propositional structure of a HOL term,  *)
webertj@17809
   332
(*      with subterms replaced by Boolean variables.  Also returns a table   *)
webertj@17809
   333
(*      of terms and corresponding variables that extends the table that was *)
webertj@17809
   334
(*      given as an argument.  Usually, you'll just want to use              *)
webertj@17809
   335
(*      'Termtab.empty' as value for 'table'.                                *)
webertj@17809
   336
(* ------------------------------------------------------------------------- *)
webertj@17809
   337
webertj@17809
   338
(* Note: The implementation is somewhat optimized; the next index to be used *)
webertj@17809
   339
(*       is computed only when it is actually needed.  However, when         *)
webertj@17809
   340
(*       'prop_formula_of_term' is invoked many times, it might be more      *)
webertj@17809
   341
(*       efficient to pass and return this value as an additional parameter, *)
webertj@17809
   342
(*       so that it does not have to be recomputed (by folding over the      *)
webertj@17809
   343
(*       table) for each invocation.                                         *)
webertj@17809
   344
wenzelm@41447
   345
fun prop_formula_of_term t table =
wenzelm@41447
   346
  let
wenzelm@41447
   347
    val next_idx_is_valid = Unsynchronized.ref false
wenzelm@41447
   348
    val next_idx = Unsynchronized.ref 0
wenzelm@41447
   349
    fun get_next_idx () =
wenzelm@41447
   350
      if !next_idx_is_valid then
wenzelm@41447
   351
        Unsynchronized.inc next_idx
wenzelm@41447
   352
      else (
wenzelm@41447
   353
        next_idx := Termtab.fold (Integer.max o snd) table 0;
wenzelm@41447
   354
        next_idx_is_valid := true;
wenzelm@41447
   355
        Unsynchronized.inc next_idx
wenzelm@41447
   356
      )
wenzelm@41447
   357
    fun aux (Const (@{const_name True}, _)) table = (True, table)
wenzelm@41447
   358
      | aux (Const (@{const_name False}, _)) table = (False, table)
wenzelm@41447
   359
      | aux (Const (@{const_name Not}, _) $ x) table = apfst Not (aux x table)
wenzelm@41447
   360
      | aux (Const (@{const_name HOL.disj}, _) $ x $ y) table =
wenzelm@41447
   361
          let
wenzelm@41447
   362
            val (fm1, table1) = aux x table
wenzelm@41447
   363
            val (fm2, table2) = aux y table1
wenzelm@41447
   364
          in
wenzelm@41447
   365
            (Or (fm1, fm2), table2)
wenzelm@41447
   366
          end
wenzelm@41447
   367
      | aux (Const (@{const_name HOL.conj}, _) $ x $ y) table =
wenzelm@41447
   368
          let
wenzelm@41447
   369
            val (fm1, table1) = aux x table
wenzelm@41447
   370
            val (fm2, table2) = aux y table1
wenzelm@41447
   371
          in
wenzelm@41447
   372
            (And (fm1, fm2), table2)
wenzelm@41447
   373
          end
wenzelm@41447
   374
      | aux x table =
wenzelm@41447
   375
          (case Termtab.lookup table x of
wenzelm@41447
   376
            SOME i => (BoolVar i, table)
wenzelm@41447
   377
          | NONE =>
wenzelm@41447
   378
              let
wenzelm@41447
   379
                val i = get_next_idx ()
wenzelm@41447
   380
              in
wenzelm@41447
   381
                (BoolVar i, Termtab.update (x, i) table)
wenzelm@41447
   382
              end)
wenzelm@41447
   383
  in
wenzelm@41447
   384
    aux t table
wenzelm@41447
   385
  end;
webertj@17809
   386
webertj@20442
   387
(* ------------------------------------------------------------------------- *)
webertj@20442
   388
(* term_of_prop_formula: returns a HOL term that corresponds to a            *)
webertj@20442
   389
(*      propositional formula, with Boolean variables replaced by Free's     *)
webertj@20442
   390
(* ------------------------------------------------------------------------- *)
webertj@20442
   391
webertj@20442
   392
(* Note: A more generic implementation should take another argument of type  *)
webertj@20442
   393
(*       Term.term Inttab.table (or so) that specifies HOL terms for some    *)
webertj@20442
   394
(*       Boolean variables in the formula, similar to 'prop_formula_of_term' *)
webertj@20442
   395
(*       (but the other way round).                                          *)
webertj@20442
   396
wenzelm@45740
   397
fun term_of_prop_formula True = @{term True}
wenzelm@45740
   398
  | term_of_prop_formula False = @{term False}
wenzelm@41491
   399
  | term_of_prop_formula (BoolVar i) = Free ("v" ^ string_of_int i, HOLogic.boolT)
wenzelm@41447
   400
  | term_of_prop_formula (Not fm) = HOLogic.mk_not (term_of_prop_formula fm)
wenzelm@41447
   401
  | term_of_prop_formula (Or (fm1, fm2)) =
wenzelm@41447
   402
      HOLogic.mk_disj (term_of_prop_formula fm1, term_of_prop_formula fm2)
wenzelm@41447
   403
  | term_of_prop_formula (And (fm1, fm2)) =
wenzelm@41447
   404
      HOLogic.mk_conj (term_of_prop_formula fm1, term_of_prop_formula fm2);
webertj@20442
   405
webertj@14452
   406
end;