src/HOL/Tools/prop_logic.ML
author webertj
Mon Jul 25 21:40:43 2005 +0200 (2005-07-25)
changeset 16909 acbc7a9c3864
parent 16907 2187e3f94761
child 16913 1d8a8d010e69
permissions -rw-r--r--
defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj@14452
     1
(*  Title:      HOL/Tools/prop_logic.ML
webertj@14452
     2
    ID:         $Id$
webertj@14452
     3
    Author:     Tjark Weber
webertj@16907
     4
    Copyright   2004-2005
webertj@14452
     5
webertj@14452
     6
Formulas of propositional logic.
webertj@14452
     7
*)
webertj@14452
     8
webertj@14452
     9
signature PROP_LOGIC =
webertj@14452
    10
sig
webertj@14452
    11
	datatype prop_formula =
webertj@14452
    12
		  True
webertj@14452
    13
		| False
webertj@14452
    14
		| BoolVar of int  (* NOTE: only use indices >= 1 *)
webertj@14452
    15
		| Not of prop_formula
webertj@14452
    16
		| Or of prop_formula * prop_formula
webertj@14452
    17
		| And of prop_formula * prop_formula
webertj@14452
    18
webertj@16909
    19
	val SNot     : prop_formula -> prop_formula
webertj@16909
    20
	val SOr      : prop_formula * prop_formula -> prop_formula
webertj@16909
    21
	val SAnd     : prop_formula * prop_formula -> prop_formula
webertj@16909
    22
	val simplify : prop_formula -> prop_formula  (* eliminates True/False and double-negation *)
webertj@14452
    23
webertj@14681
    24
	val indices : prop_formula -> int list  (* set of all variable indices *)
webertj@15301
    25
	val maxidx  : prop_formula -> int       (* maximal variable index *)
webertj@14452
    26
webertj@14452
    27
	val exists      : prop_formula list -> prop_formula  (* finite disjunction *)
webertj@14452
    28
	val all         : prop_formula list -> prop_formula  (* finite conjunction *)
webertj@14452
    29
	val dot_product : prop_formula list * prop_formula list -> prop_formula
webertj@14452
    30
webertj@16909
    31
	val nnf    : prop_formula -> prop_formula  (* negation normal form *)
webertj@16909
    32
	val cnf    : prop_formula -> prop_formula  (* conjunctive normal form *)
webertj@16909
    33
	val auxcnf : prop_formula -> prop_formula  (* cnf with auxiliary variables *)
webertj@16909
    34
	val defcnf : prop_formula -> prop_formula  (* definitional cnf *)
webertj@16909
    35
webertj@14452
    36
	val eval : (int -> bool) -> prop_formula -> bool  (* semantics *)
webertj@14452
    37
end;
webertj@14452
    38
webertj@14452
    39
structure PropLogic : PROP_LOGIC =
webertj@14452
    40
struct
webertj@14452
    41
webertj@14452
    42
(* ------------------------------------------------------------------------- *)
webertj@14753
    43
(* prop_formula: formulas of propositional logic, built from Boolean         *)
webertj@14452
    44
(*               variables (referred to by index) and True/False using       *)
webertj@14452
    45
(*               not/or/and                                                  *)
webertj@14452
    46
(* ------------------------------------------------------------------------- *)
webertj@14452
    47
webertj@14452
    48
	datatype prop_formula =
webertj@14452
    49
		  True
webertj@14452
    50
		| False
webertj@14452
    51
		| BoolVar of int  (* NOTE: only use indices >= 1 *)
webertj@14452
    52
		| Not of prop_formula
webertj@14452
    53
		| Or of prop_formula * prop_formula
webertj@14452
    54
		| And of prop_formula * prop_formula;
webertj@14452
    55
webertj@14452
    56
(* ------------------------------------------------------------------------- *)
webertj@14452
    57
(* The following constructor functions make sure that True and False do not  *)
webertj@14452
    58
(* occur within any of the other connectives (i.e. Not, Or, And), and        *)
webertj@14452
    59
(* perform double-negation elimination.                                      *)
webertj@14452
    60
(* ------------------------------------------------------------------------- *)
webertj@14452
    61
webertj@14452
    62
	(* prop_formula -> prop_formula *)
webertj@14452
    63
webertj@14452
    64
	fun SNot True     = False
webertj@14452
    65
	  | SNot False    = True
webertj@14452
    66
	  | SNot (Not fm) = fm
webertj@14452
    67
	  | SNot fm       = Not fm;
webertj@14452
    68
webertj@14452
    69
	(* prop_formula * prop_formula -> prop_formula *)
webertj@14452
    70
webertj@14452
    71
	fun SOr (True, _)   = True
webertj@14452
    72
	  | SOr (_, True)   = True
webertj@14452
    73
	  | SOr (False, fm) = fm
webertj@14452
    74
	  | SOr (fm, False) = fm
webertj@14452
    75
	  | SOr (fm1, fm2)  = Or (fm1, fm2);
webertj@14452
    76
webertj@14452
    77
	(* prop_formula * prop_formula -> prop_formula *)
webertj@14452
    78
webertj@14452
    79
	fun SAnd (True, fm) = fm
webertj@14452
    80
	  | SAnd (fm, True) = fm
webertj@14452
    81
	  | SAnd (False, _) = False
webertj@14452
    82
	  | SAnd (_, False) = False
webertj@14452
    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
webertj@16909
    90
	(* prop_formula -> prop_formula *)
webertj@16909
    91
webertj@16909
    92
	fun simplify (Not fm)         = SNot (simplify fm)
webertj@16909
    93
	  | simplify (Or (fm1, fm2))  = SOr (simplify fm1, simplify fm2)
webertj@16909
    94
	  | simplify (And (fm1, fm2)) = SAnd (simplify fm1, simplify fm2)
webertj@16909
    95
	  | simplify fm               = fm;
webertj@16909
    96
webertj@16909
    97
(* ------------------------------------------------------------------------- *)
webertj@14753
    98
(* indices: collects all indices of Boolean variables that occur in a        *)
webertj@14452
    99
(*      propositional formula 'fm'; no duplicates                            *)
webertj@14452
   100
(* ------------------------------------------------------------------------- *)
webertj@14452
   101
webertj@14452
   102
	(* prop_formula -> int list *)
webertj@14452
   103
webertj@16909
   104
	fun indices True             = []
webertj@16909
   105
	  | indices False            = []
webertj@16909
   106
	  | indices (BoolVar i)      = [i]
webertj@16909
   107
	  | indices (Not fm)         = indices fm
webertj@16909
   108
	  | indices (Or (fm1, fm2))  = (indices fm1) union_int (indices fm2)
webertj@16909
   109
	  | indices (And (fm1, fm2)) = (indices fm1) union_int (indices fm2);
webertj@14452
   110
webertj@14452
   111
(* ------------------------------------------------------------------------- *)
webertj@14452
   112
(* maxidx: computes the maximal variable index occuring in a formula of      *)
webertj@14452
   113
(*      propositional logic 'fm'; 0 if 'fm' contains no variable             *)
webertj@14452
   114
(* ------------------------------------------------------------------------- *)
webertj@14452
   115
webertj@14452
   116
	(* prop_formula -> int *)
webertj@14452
   117
webertj@16909
   118
	fun maxidx True             = 0
webertj@16909
   119
	  | maxidx False            = 0
webertj@16909
   120
	  | maxidx (BoolVar i)      = i
webertj@16909
   121
	  | maxidx (Not fm)         = maxidx fm
webertj@16909
   122
	  | maxidx (Or (fm1, fm2))  = Int.max (maxidx fm1, maxidx fm2)
webertj@16909
   123
	  | maxidx (And (fm1, fm2)) = Int.max (maxidx fm1, maxidx fm2);
webertj@16909
   124
webertj@16909
   125
(* ------------------------------------------------------------------------- *)
webertj@16909
   126
(* exists: computes the disjunction over a list 'xs' of propositional        *)
webertj@16909
   127
(*      formulas                                                             *)
webertj@16909
   128
(* ------------------------------------------------------------------------- *)
webertj@16909
   129
webertj@16909
   130
	(* prop_formula list -> prop_formula *)
webertj@16909
   131
webertj@16909
   132
	fun exists xs = Library.foldl SOr (False, xs);
webertj@16909
   133
webertj@16909
   134
(* ------------------------------------------------------------------------- *)
webertj@16909
   135
(* all: computes the conjunction over a list 'xs' of propositional formulas  *)
webertj@16909
   136
(* ------------------------------------------------------------------------- *)
webertj@16909
   137
webertj@16909
   138
	(* prop_formula list -> prop_formula *)
webertj@16909
   139
webertj@16909
   140
	fun all xs = Library.foldl SAnd (True, xs);
webertj@16909
   141
webertj@16909
   142
(* ------------------------------------------------------------------------- *)
webertj@16909
   143
(* dot_product: ([x1,...,xn], [y1,...,yn]) -> x1*y1+...+xn*yn                *)
webertj@16909
   144
(* ------------------------------------------------------------------------- *)
webertj@16909
   145
webertj@16909
   146
	(* prop_formula list * prop_formula list -> prop_formula *)
webertj@16909
   147
webertj@16909
   148
	fun dot_product (xs,ys) = exists (map SAnd (xs~~ys));
webertj@14452
   149
webertj@14452
   150
(* ------------------------------------------------------------------------- *)
webertj@14452
   151
(* nnf: computes the negation normal form of a formula 'fm' of propositional *)
webertj@16909
   152
(*      logic (i.e. only variables may be negated, but not subformulas).     *)
webertj@16909
   153
(*      Simplification (c.f. 'simplify') is performed as well.               *)
webertj@14452
   154
(* ------------------------------------------------------------------------- *)
webertj@14452
   155
webertj@14452
   156
	(* prop_formula -> prop_formula *)
webertj@14452
   157
webertj@14452
   158
	fun
webertj@14452
   159
	(* constants *)
webertj@16909
   160
	    nnf True                   = True
webertj@16909
   161
	  | nnf False                  = False
webertj@14452
   162
	(* variables *)
webertj@16909
   163
	  | nnf (BoolVar i)            = (BoolVar i)
webertj@14452
   164
	(* 'or' and 'and' as outermost connectives are left untouched *)
webertj@16909
   165
	  | nnf (Or  (fm1, fm2))       = SOr  (nnf fm1, nnf fm2)
webertj@16909
   166
	  | nnf (And (fm1, fm2))       = SAnd (nnf fm1, nnf fm2)
webertj@14452
   167
	(* 'not' + constant *)
webertj@16909
   168
	  | nnf (Not True)             = False
webertj@16909
   169
	  | nnf (Not False)            = True
webertj@14452
   170
	(* 'not' + variable *)
webertj@16909
   171
	  | nnf (Not (BoolVar i))      = Not (BoolVar i)
webertj@14452
   172
	(* pushing 'not' inside of 'or'/'and' using de Morgan's laws *)
webertj@16909
   173
	  | nnf (Not (Or  (fm1, fm2))) = SAnd (nnf (SNot fm1), nnf (SNot fm2))
webertj@16909
   174
	  | nnf (Not (And (fm1, fm2))) = SOr  (nnf (SNot fm1), nnf (SNot fm2))
webertj@14452
   175
	(* double-negation elimination *)
webertj@16909
   176
	  | nnf (Not (Not fm))         = nnf fm;
webertj@14452
   177
webertj@14452
   178
(* ------------------------------------------------------------------------- *)
webertj@14681
   179
(* cnf: computes the conjunctive normal form (i.e. a conjunction of          *)
webertj@14681
   180
(*      disjunctions) of a formula 'fm' of propositional logic.  The result  *)
webertj@14681
   181
(*      formula may be exponentially longer than 'fm'.                       *)
webertj@14452
   182
(* ------------------------------------------------------------------------- *)
webertj@14452
   183
webertj@14452
   184
	(* prop_formula -> prop_formula *)
webertj@14452
   185
webertj@14452
   186
	fun cnf fm =
webertj@14452
   187
	let
webertj@14452
   188
		fun
webertj@14452
   189
		(* constants *)
webertj@14939
   190
		    cnf_from_nnf True             = True
webertj@14939
   191
		  | cnf_from_nnf False            = False
webertj@14452
   192
		(* literals *)
webertj@14939
   193
		  | cnf_from_nnf (BoolVar i)      = BoolVar i
webertj@14939
   194
		  | cnf_from_nnf (Not fm1)        = Not fm1  (* 'fm1' must be a variable since the formula is in NNF *)
webertj@14452
   195
		(* pushing 'or' inside of 'and' using distributive laws *)
webertj@14939
   196
		  | cnf_from_nnf (Or (fm1, fm2))  =
webertj@14452
   197
			let
webertj@14939
   198
				fun cnf_or (And (fm11, fm12), fm2) =
webertj@14939
   199
					And (cnf_or (fm11, fm2), cnf_or (fm12, fm2))
webertj@14939
   200
				  | cnf_or (fm1, And (fm21, fm22)) =
webertj@14939
   201
					And (cnf_or (fm1, fm21), cnf_or (fm1, fm22))
webertj@14939
   202
				(* neither subformula contains 'and' *)
webertj@14939
   203
				  | cnf_or (fm1, fm2) =
webertj@14939
   204
					Or (fm1, fm2)
webertj@14452
   205
			in
webertj@14939
   206
				cnf_or (cnf_from_nnf fm1, cnf_from_nnf fm2)
webertj@14452
   207
			end
webertj@14452
   208
		(* 'and' as outermost connective is left untouched *)
webertj@14939
   209
		  | cnf_from_nnf (And (fm1, fm2)) = And (cnf_from_nnf fm1, cnf_from_nnf fm2)
webertj@14452
   210
	in
webertj@14452
   211
		(cnf_from_nnf o nnf) fm
webertj@14452
   212
	end;
webertj@14452
   213
webertj@14452
   214
(* ------------------------------------------------------------------------- *)
webertj@16909
   215
(* auxcnf: computes the definitional conjunctive normal form of a formula    *)
webertj@14681
   216
(*      'fm' of propositional logic, introducing auxiliary variables if      *)
webertj@14681
   217
(*      necessary to avoid an exponential blowup of the formula.  The result *)
webertj@14681
   218
(*      formula is satisfiable if and only if 'fm' is satisfiable.           *)
webertj@16907
   219
(*      Auxiliary variables are introduced as switches for OR-nodes, based   *)
webertj@16909
   220
(*      on the observation that e.g. "fm1 OR (fm21 AND fm22)" is             *)
webertj@16909
   221
(*      equisatisfiable with "(fm1 OR ~aux) AND (fm21 OR aux) AND (fm22 OR   *)
webertj@16909
   222
(*      aux)".                                                               *)
webertj@16909
   223
(* ------------------------------------------------------------------------- *)
webertj@16909
   224
webertj@16909
   225
(* ------------------------------------------------------------------------- *)
webertj@16909
   226
(* Note: 'auxcnf' tends to use fewer variables and fewer clauses than        *)
webertj@16909
   227
(*      'defcnf' below, but sometimes generates slightly larger SAT problems *)
webertj@16909
   228
(*      overall (hence it must sometimes generate longer clauses than        *)
webertj@16909
   229
(*      'defcnf' does).  It is currently not quite clear to me if one of the *)
webertj@16909
   230
(*      algorithms is clearly superior to the other.                         *)
webertj@14452
   231
(* ------------------------------------------------------------------------- *)
webertj@14452
   232
webertj@14452
   233
	(* prop_formula -> prop_formula *)
webertj@14452
   234
webertj@16909
   235
	fun auxcnf fm =
webertj@14452
   236
	let
webertj@16907
   237
		(* convert formula to NNF first *)
webertj@16907
   238
		val fm' = nnf fm
webertj@14452
   239
		(* 'new' specifies the next index that is available to introduce an auxiliary variable *)
webertj@16907
   240
		(* int ref *)
webertj@16907
   241
		val new = ref (maxidx fm' + 1)
webertj@16907
   242
		(* unit -> int *)
webertj@16907
   243
		fun new_idx () = let val idx = !new in new := idx+1; idx end
webertj@16907
   244
		(* prop_formula -> prop_formula *)
webertj@14452
   245
		fun
webertj@14452
   246
		(* constants *)
webertj@16909
   247
		    auxcnf_from_nnf True  = True
webertj@16909
   248
		  | auxcnf_from_nnf False = False
webertj@14452
   249
		(* literals *)
webertj@16909
   250
		  | auxcnf_from_nnf (BoolVar i) = BoolVar i
webertj@16909
   251
		  | auxcnf_from_nnf (Not fm1)   = Not fm1  (* 'fm1' must be a variable since the formula is in NNF *)
webertj@14939
   252
		(* pushing 'or' inside of 'and' using auxiliary variables *)
webertj@16909
   253
		  | auxcnf_from_nnf (Or (fm1, fm2)) =
webertj@14452
   254
			let
webertj@16909
   255
				val fm1' = auxcnf_from_nnf fm1
webertj@16909
   256
				val fm2' = auxcnf_from_nnf fm2
webertj@16907
   257
				(* prop_formula * prop_formula -> prop_formula *)
webertj@16909
   258
				fun auxcnf_or (And (fm11, fm12), fm2) =
webertj@14964
   259
					(case fm2 of
webertj@14964
   260
					(* do not introduce an auxiliary variable for literals *)
webertj@14964
   261
					  BoolVar _ =>
webertj@16909
   262
							And (auxcnf_or (fm11, fm2), auxcnf_or (fm12, fm2))
webertj@14964
   263
					| Not _ =>
webertj@16909
   264
							And (auxcnf_or (fm11, fm2), auxcnf_or (fm12, fm2))
webertj@14964
   265
					| _ =>
webertj@14964
   266
						let
webertj@16907
   267
							val aux = BoolVar (new_idx ())
webertj@14964
   268
						in
webertj@16909
   269
							And (And (auxcnf_or (fm11, aux), auxcnf_or (fm12, aux)), auxcnf_or (fm2, Not aux))
webertj@14964
   270
						end)
webertj@16909
   271
				  | auxcnf_or (fm1, And (fm21, fm22)) =
webertj@14964
   272
					(case fm1 of
webertj@14964
   273
					(* do not introduce an auxiliary variable for literals *)
webertj@14964
   274
					  BoolVar _ =>
webertj@16909
   275
							And (auxcnf_or (fm1, fm21), auxcnf_or (fm1, fm22))
webertj@14964
   276
					| Not _ =>
webertj@16909
   277
							And (auxcnf_or (fm1, fm21), auxcnf_or (fm1, fm22))
webertj@14964
   278
					| _ =>
webertj@14964
   279
						let
webertj@16907
   280
							val aux = BoolVar (new_idx ())
webertj@14964
   281
						in
webertj@16909
   282
							And (auxcnf_or (fm1, Not aux), And (auxcnf_or (fm21, aux), auxcnf_or (fm22, aux)))
webertj@14964
   283
						end)
webertj@14939
   284
				(* neither subformula contains 'and' *)
webertj@16909
   285
				  | auxcnf_or (fm1, fm2) =
webertj@16907
   286
					Or (fm1, fm2)
webertj@14939
   287
			in
webertj@16909
   288
				auxcnf_or (fm1', fm2')
webertj@14452
   289
			end
webertj@14452
   290
		(* 'and' as outermost connective is left untouched *)
webertj@16909
   291
		  | auxcnf_from_nnf (And (fm1, fm2)) =
webertj@16909
   292
				And (auxcnf_from_nnf fm1, auxcnf_from_nnf fm2)
webertj@14452
   293
	in
webertj@16909
   294
		auxcnf_from_nnf fm'
webertj@14452
   295
	end;
webertj@14452
   296
webertj@14452
   297
(* ------------------------------------------------------------------------- *)
webertj@16909
   298
(* defcnf: computes the definitional conjunctive normal form of a formula    *)
webertj@16909
   299
(*      'fm' of propositional logic, introducing auxiliary variables to      *)
webertj@16909
   300
(*      avoid an exponential blowup of the formula.  The result formula is   *)
webertj@16909
   301
(*      satisfiable if and only if 'fm' is satisfiable.  Auxiliary variables *)
webertj@16909
   302
(*      are introduced as abbreviations for AND-, OR-, and NOT-nodes, based  *)
webertj@16909
   303
(*      on the following equisatisfiabilities (+/- indicates polarity):      *)
webertj@16909
   304
(*      LITERAL+       == LITERAL                                            *)
webertj@16909
   305
(*      LITERAL-       == NOT LITERAL                                        *)
webertj@16909
   306
(*      (NOT fm1)+     == aux AND (NOT aux OR fm1-)                          *)
webertj@16909
   307
(*      (fm1 OR fm2)+  == aux AND (NOT aux OR fm1+ OR fm2+)                  *)
webertj@16909
   308
(*      (fm1 AND fm2)+ == aux AND (NOT aux OR fm1+) AND (NOT aux OR fm2+)    *)
webertj@16909
   309
(*      (NOT fm1)-     == aux AND (NOT aux OR fm1+)                          *)
webertj@16909
   310
(*      (fm1 OR fm2)-  == aux AND (NOT aux OR fm1-) AND (NOT aux OR fm2-)    *)
webertj@16909
   311
(*      (fm1 AND fm2)- == aux AND (NOT aux OR fm1- OR fm2-)                  *)
webertj@16909
   312
(*      Example:                                                             *)
webertj@16909
   313
(*      NOT (a AND b) == aux1 AND (NOT aux1 OR aux2)                         *)
webertj@16909
   314
(*                            AND (NOT aux2 OR NOT a OR NOT b)               *)
webertj@14452
   315
(* ------------------------------------------------------------------------- *)
webertj@14452
   316
webertj@16909
   317
	(* prop_formula -> prop_formula *)
webertj@14452
   318
webertj@16909
   319
	fun defcnf fm =
webertj@16909
   320
	let
webertj@16909
   321
		(* simplify formula first *)
webertj@16909
   322
		val fm' = simplify fm
webertj@16909
   323
		(* 'new' specifies the next index that is available to introduce an auxiliary variable *)
webertj@16909
   324
		(* int ref *)
webertj@16909
   325
		val new = ref (maxidx fm' + 1)
webertj@16909
   326
		(* unit -> int *)
webertj@16909
   327
		fun new_idx () = let val idx = !new in new := idx+1; idx end
webertj@16909
   328
		(* optimization for n-ary disjunction/conjunction *)
webertj@16909
   329
		(* prop_formula -> prop_formula list *)
webertj@16909
   330
		fun disjuncts (Or (fm1, fm2)) = (disjuncts fm1) @ (disjuncts fm2)
webertj@16909
   331
		  | disjuncts fm1             = [fm1]
webertj@16909
   332
		(* prop_formula -> prop_formula list *)
webertj@16909
   333
		fun conjuncts (And (fm1, fm2)) = (conjuncts fm1) @ (conjuncts fm2)
webertj@16909
   334
		  | conjuncts fm1              = [fm1]
webertj@16909
   335
		(* polarity -> formula -> (defining clauses, literal) *)
webertj@16909
   336
		(* bool -> prop_formula -> prop_formula * prop_formula *)
webertj@16909
   337
		fun
webertj@16909
   338
		(* constants *)
webertj@16909
   339
		    defcnf' true  True  = (True, True)
webertj@16909
   340
		  | defcnf' false True  = (*(True, False)*) error "formula is not simplified, True occurs with negative polarity"
webertj@16909
   341
		  | defcnf' true  False = (True, False)
webertj@16909
   342
		  | defcnf' false False = (*(True, True)*) error "formula is not simplified, False occurs with negative polarity"
webertj@16909
   343
		(* literals *)
webertj@16909
   344
		  | defcnf' true  (BoolVar i)       = (True, BoolVar i)
webertj@16909
   345
		  | defcnf' false (BoolVar i)       = (True, Not (BoolVar i))
webertj@16909
   346
		  | defcnf' true  (Not (BoolVar i)) = (True, Not (BoolVar i))
webertj@16909
   347
		  | defcnf' false (Not (BoolVar i)) = (True, BoolVar i)
webertj@16909
   348
		(* 'not' *)
webertj@16909
   349
		  | defcnf' polarity (Not fm1) =
webertj@16909
   350
			let
webertj@16909
   351
				val (def1, aux1) = defcnf' (not polarity) fm1
webertj@16909
   352
				val aux          = BoolVar (new_idx ())
webertj@16909
   353
				val def          = Or (Not aux, aux1)
webertj@16909
   354
			in
webertj@16909
   355
				(SAnd (def1, def), aux)
webertj@16909
   356
			end
webertj@16909
   357
		(* 'or' *)
webertj@16909
   358
		  | defcnf' polarity (Or (fm1, fm2)) =
webertj@16909
   359
			let
webertj@16909
   360
				val fms          = disjuncts (Or (fm1, fm2))
webertj@16909
   361
				val (defs, auxs) = split_list (map (defcnf' polarity) fms)
webertj@16909
   362
				val aux          = BoolVar (new_idx ())
webertj@16909
   363
				val def          = if polarity then Or (Not aux, exists auxs) else all (map (fn a => Or (Not aux, a)) auxs)
webertj@16909
   364
			in
webertj@16909
   365
				(SAnd (all defs, def), aux)
webertj@16909
   366
			end
webertj@16909
   367
		(* 'and' *)
webertj@16909
   368
		  | defcnf' polarity (And (fm1, fm2)) =
webertj@16909
   369
			let
webertj@16909
   370
				val fms          = conjuncts (And (fm1, fm2))
webertj@16909
   371
				val (defs, auxs) = split_list (map (defcnf' polarity) fms)
webertj@16909
   372
				val aux          = BoolVar (new_idx ())
webertj@16909
   373
				val def          = if not polarity then Or (Not aux, exists auxs) else all (map (fn a => Or (Not aux, a)) auxs)
webertj@16909
   374
			in
webertj@16909
   375
				(SAnd (all defs, def), aux)
webertj@16909
   376
			end
webertj@16909
   377
		(* optimization: do not introduce auxiliary variables for parts of the formula that are in CNF already *)
webertj@16909
   378
		(* prop_formula -> prop_formula * prop_formula *)
webertj@16909
   379
		fun defcnf_or (Or (fm1, fm2)) =
webertj@16909
   380
			let
webertj@16909
   381
				val (def1, aux1) = defcnf_or fm1
webertj@16909
   382
				val (def2, aux2) = defcnf_or fm2
webertj@16909
   383
			in
webertj@16909
   384
				(SAnd (def1, def2), Or (aux1, aux2))
webertj@16909
   385
			end
webertj@16909
   386
		  | defcnf_or fm =
webertj@16909
   387
			defcnf' true fm
webertj@16909
   388
		(* prop_formula -> prop_formula * prop_formula *)
webertj@16909
   389
		fun defcnf_and (And (fm1, fm2)) =
webertj@16909
   390
			let
webertj@16909
   391
				val (def1, aux1) = defcnf_and fm1
webertj@16909
   392
				val (def2, aux2) = defcnf_and fm2
webertj@16909
   393
			in
webertj@16909
   394
				(SAnd (def1, def2), And (aux1, aux2))
webertj@16909
   395
			end
webertj@16909
   396
		  | defcnf_and (Or (fm1, fm2)) =
webertj@16909
   397
			let
webertj@16909
   398
				val (def1, aux1) = defcnf_or fm1
webertj@16909
   399
				val (def2, aux2) = defcnf_or fm2
webertj@16909
   400
			in
webertj@16909
   401
				(SAnd (def1, def2), Or (aux1, aux2))
webertj@16909
   402
			end
webertj@16909
   403
		  | defcnf_and fm =
webertj@16909
   404
			defcnf' true fm
webertj@16909
   405
	in
webertj@16909
   406
		SAnd (defcnf_and fm')
webertj@16909
   407
	end;
webertj@14452
   408
webertj@14452
   409
(* ------------------------------------------------------------------------- *)
webertj@14753
   410
(* eval: given an assignment 'a' of Boolean values to variable indices, the  *)
webertj@14452
   411
(*      truth value of a propositional formula 'fm' is computed              *)
webertj@14452
   412
(* ------------------------------------------------------------------------- *)
webertj@14452
   413
webertj@14452
   414
	(* (int -> bool) -> prop_formula -> bool *)
webertj@14452
   415
webertj@14452
   416
	fun eval a True            = true
webertj@14452
   417
	  | eval a False           = false
webertj@14452
   418
	  | eval a (BoolVar i)     = (a i)
webertj@14452
   419
	  | eval a (Not fm)        = not (eval a fm)
webertj@14452
   420
	  | eval a (Or (fm1,fm2))  = (eval a fm1) orelse (eval a fm2)
webertj@14452
   421
	  | eval a (And (fm1,fm2)) = (eval a fm1) andalso (eval a fm2);
webertj@14452
   422
webertj@14452
   423
end;