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