src/HOL/Tools/prop_logic.ML
author skalberg
Thu Mar 03 12:43:01 2005 +0100 (2005-03-03)
changeset 15570 8d8c70b41bab
parent 15548 aea2f1706fdf
child 16907 2187e3f94761
permissions -rw-r--r--
Move towards standard functions.
webertj@14452
     1
(*  Title:      HOL/Tools/prop_logic.ML
webertj@14452
     2
    ID:         $Id$
webertj@14452
     3
    Author:     Tjark Weber
webertj@14452
     4
    Copyright   2004
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@14452
    19
	val SNot : prop_formula -> prop_formula
webertj@14452
    20
	val SOr  : prop_formula * prop_formula -> prop_formula
webertj@14452
    21
	val SAnd : prop_formula * prop_formula -> prop_formula
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 nnf    : prop_formula -> prop_formula  (* negation normal form *)
webertj@14681
    27
	val cnf    : prop_formula -> prop_formula  (* conjunctive normal form *)
webertj@14452
    28
	val defcnf : prop_formula -> prop_formula  (* definitional cnf *)
webertj@14452
    29
webertj@14452
    30
	val exists      : prop_formula list -> prop_formula  (* finite disjunction *)
webertj@14452
    31
	val all         : prop_formula list -> prop_formula  (* finite conjunction *)
webertj@14452
    32
	val dot_product : prop_formula list * prop_formula list -> prop_formula
webertj@14452
    33
webertj@14452
    34
	val eval : (int -> bool) -> prop_formula -> bool  (* semantics *)
webertj@14452
    35
end;
webertj@14452
    36
webertj@14452
    37
structure PropLogic : PROP_LOGIC =
webertj@14452
    38
struct
webertj@14452
    39
webertj@14452
    40
(* ------------------------------------------------------------------------- *)
webertj@14753
    41
(* prop_formula: formulas of propositional logic, built from Boolean         *)
webertj@14452
    42
(*               variables (referred to by index) and True/False using       *)
webertj@14452
    43
(*               not/or/and                                                  *)
webertj@14452
    44
(* ------------------------------------------------------------------------- *)
webertj@14452
    45
webertj@14452
    46
	datatype prop_formula =
webertj@14452
    47
		  True
webertj@14452
    48
		| False
webertj@14452
    49
		| BoolVar of int  (* NOTE: only use indices >= 1 *)
webertj@14452
    50
		| Not of prop_formula
webertj@14452
    51
		| Or of prop_formula * prop_formula
webertj@14452
    52
		| And of prop_formula * prop_formula;
webertj@14452
    53
webertj@14452
    54
(* ------------------------------------------------------------------------- *)
webertj@14452
    55
(* The following constructor functions make sure that True and False do not  *)
webertj@14452
    56
(* occur within any of the other connectives (i.e. Not, Or, And), and        *)
webertj@14452
    57
(* perform double-negation elimination.                                      *)
webertj@14452
    58
(* ------------------------------------------------------------------------- *)
webertj@14452
    59
webertj@14452
    60
	(* prop_formula -> prop_formula *)
webertj@14452
    61
webertj@14452
    62
	fun SNot True     = False
webertj@14452
    63
	  | SNot False    = True
webertj@14452
    64
	  | SNot (Not fm) = fm
webertj@14452
    65
	  | SNot fm       = Not fm;
webertj@14452
    66
webertj@14452
    67
	(* prop_formula * prop_formula -> prop_formula *)
webertj@14452
    68
webertj@14452
    69
	fun SOr (True, _)   = True
webertj@14452
    70
	  | SOr (_, True)   = True
webertj@14452
    71
	  | SOr (False, fm) = fm
webertj@14452
    72
	  | SOr (fm, False) = fm
webertj@14452
    73
	  | SOr (fm1, fm2)  = Or (fm1, fm2);
webertj@14452
    74
webertj@14452
    75
	(* prop_formula * prop_formula -> prop_formula *)
webertj@14452
    76
webertj@14452
    77
	fun SAnd (True, fm) = fm
webertj@14452
    78
	  | SAnd (fm, True) = fm
webertj@14452
    79
	  | SAnd (False, _) = False
webertj@14452
    80
	  | SAnd (_, False) = False
webertj@14452
    81
	  | SAnd (fm1, fm2) = And (fm1, fm2);
webertj@14452
    82
webertj@14452
    83
(* ------------------------------------------------------------------------- *)
webertj@14753
    84
(* indices: collects all indices of Boolean variables that occur in a        *)
webertj@14452
    85
(*      propositional formula 'fm'; no duplicates                            *)
webertj@14452
    86
(* ------------------------------------------------------------------------- *)
webertj@14452
    87
webertj@14452
    88
	(* prop_formula -> int list *)
webertj@14452
    89
webertj@14452
    90
	fun indices True            = []
webertj@14452
    91
	  | indices False           = []
webertj@14452
    92
	  | indices (BoolVar i)     = [i]
webertj@14452
    93
	  | indices (Not fm)        = indices fm
webertj@14452
    94
	  | indices (Or (fm1,fm2))  = (indices fm1) union_int (indices fm2)
webertj@14452
    95
	  | indices (And (fm1,fm2)) = (indices fm1) union_int (indices fm2);
webertj@14452
    96
webertj@14452
    97
(* ------------------------------------------------------------------------- *)
webertj@14452
    98
(* maxidx: computes the maximal variable index occuring in a formula of      *)
webertj@14452
    99
(*      propositional logic 'fm'; 0 if 'fm' contains no variable             *)
webertj@14452
   100
(* ------------------------------------------------------------------------- *)
webertj@14452
   101
webertj@14452
   102
	(* prop_formula -> int *)
webertj@14452
   103
webertj@14452
   104
	fun maxidx True            = 0
webertj@14452
   105
	  | maxidx False           = 0
webertj@14452
   106
	  | maxidx (BoolVar i)     = i
webertj@14452
   107
	  | maxidx (Not fm)        = maxidx fm
webertj@14452
   108
	  | maxidx (Or (fm1,fm2))  = Int.max (maxidx fm1, maxidx fm2)
webertj@14452
   109
	  | maxidx (And (fm1,fm2)) = Int.max (maxidx fm1, maxidx fm2);
webertj@14452
   110
webertj@14452
   111
(* ------------------------------------------------------------------------- *)
webertj@14452
   112
(* nnf: computes the negation normal form of a formula 'fm' of propositional *)
webertj@14452
   113
(*      logic (i.e. only variables may be negated, but not subformulas)      *)
webertj@14452
   114
(* ------------------------------------------------------------------------- *)
webertj@14452
   115
webertj@14452
   116
	(* prop_formula -> prop_formula *)
webertj@14452
   117
webertj@14452
   118
	fun
webertj@14452
   119
	(* constants *)
webertj@14452
   120
	    nnf True                  = True
webertj@14452
   121
	  | nnf False                 = False
webertj@14452
   122
	(* variables *)
webertj@14939
   123
	  | nnf (BoolVar i)           = (BoolVar i)
webertj@14452
   124
	(* 'or' and 'and' as outermost connectives are left untouched *)
webertj@14452
   125
	  | nnf (Or  (fm1,fm2))       = SOr  (nnf fm1, nnf fm2)
webertj@14452
   126
	  | nnf (And (fm1,fm2))       = SAnd (nnf fm1, nnf fm2)
webertj@14452
   127
	(* 'not' + constant *)
webertj@14452
   128
	  | nnf (Not True)            = False
webertj@14452
   129
	  | nnf (Not False)           = True
webertj@14452
   130
	(* 'not' + variable *)
webertj@14452
   131
	  | nnf (Not (BoolVar i))     = Not (BoolVar i)
webertj@14452
   132
	(* pushing 'not' inside of 'or'/'and' using de Morgan's laws *)
webertj@14452
   133
	  | nnf (Not (Or  (fm1,fm2))) = SAnd (nnf (SNot fm1), nnf (SNot fm2))
webertj@14452
   134
	  | nnf (Not (And (fm1,fm2))) = SOr  (nnf (SNot fm1), nnf (SNot fm2))
webertj@14452
   135
	(* double-negation elimination *)
webertj@14452
   136
	  | nnf (Not (Not fm))        = nnf fm;
webertj@14452
   137
webertj@14452
   138
(* ------------------------------------------------------------------------- *)
webertj@14681
   139
(* cnf: computes the conjunctive normal form (i.e. a conjunction of          *)
webertj@14681
   140
(*      disjunctions) of a formula 'fm' of propositional logic.  The result  *)
webertj@14681
   141
(*      formula may be exponentially longer than 'fm'.                       *)
webertj@14452
   142
(* ------------------------------------------------------------------------- *)
webertj@14452
   143
webertj@14452
   144
	(* prop_formula -> prop_formula *)
webertj@14452
   145
webertj@14452
   146
	fun cnf fm =
webertj@14452
   147
	let
webertj@14452
   148
		fun
webertj@14452
   149
		(* constants *)
webertj@14939
   150
		    cnf_from_nnf True             = True
webertj@14939
   151
		  | cnf_from_nnf False            = False
webertj@14452
   152
		(* literals *)
webertj@14939
   153
		  | cnf_from_nnf (BoolVar i)      = BoolVar i
webertj@14939
   154
		  | cnf_from_nnf (Not fm1)        = Not fm1  (* 'fm1' must be a variable since the formula is in NNF *)
webertj@14452
   155
		(* pushing 'or' inside of 'and' using distributive laws *)
webertj@14939
   156
		  | cnf_from_nnf (Or (fm1, fm2))  =
webertj@14452
   157
			let
webertj@14939
   158
				fun cnf_or (And (fm11, fm12), fm2) =
webertj@14939
   159
					And (cnf_or (fm11, fm2), cnf_or (fm12, fm2))
webertj@14939
   160
				  | cnf_or (fm1, And (fm21, fm22)) =
webertj@14939
   161
					And (cnf_or (fm1, fm21), cnf_or (fm1, fm22))
webertj@14939
   162
				(* neither subformula contains 'and' *)
webertj@14939
   163
				  | cnf_or (fm1, fm2) =
webertj@14939
   164
					Or (fm1, fm2)
webertj@14452
   165
			in
webertj@14939
   166
				cnf_or (cnf_from_nnf fm1, cnf_from_nnf fm2)
webertj@14452
   167
			end
webertj@14452
   168
		(* 'and' as outermost connective is left untouched *)
webertj@14939
   169
		  | cnf_from_nnf (And (fm1, fm2)) = And (cnf_from_nnf fm1, cnf_from_nnf fm2)
webertj@14452
   170
	in
webertj@14452
   171
		(cnf_from_nnf o nnf) fm
webertj@14452
   172
	end;
webertj@14452
   173
webertj@14452
   174
(* ------------------------------------------------------------------------- *)
webertj@14681
   175
(* defcnf: computes the definitional conjunctive normal form of a formula    *)
webertj@14681
   176
(*      'fm' of propositional logic, introducing auxiliary variables if      *)
webertj@14681
   177
(*      necessary to avoid an exponential blowup of the formula.  The result *)
webertj@14681
   178
(*      formula is satisfiable if and only if 'fm' is satisfiable.           *)
webertj@14452
   179
(* ------------------------------------------------------------------------- *)
webertj@14452
   180
webertj@14452
   181
	(* prop_formula -> prop_formula *)
webertj@14452
   182
webertj@14452
   183
	fun defcnf fm =
webertj@14452
   184
	let
webertj@14452
   185
		(* prop_formula * int -> prop_formula * int *)
webertj@14452
   186
		(* 'new' specifies the next index that is available to introduce an auxiliary variable *)
webertj@14452
   187
		fun
webertj@14452
   188
		(* constants *)
webertj@14939
   189
		    defcnf_from_nnf (True, new)            = (True, new)
webertj@14939
   190
		  | defcnf_from_nnf (False, new)           = (False, new)
webertj@14452
   191
		(* literals *)
webertj@14939
   192
		  | defcnf_from_nnf (BoolVar i, new)       = (BoolVar i, new)
webertj@14939
   193
		  | defcnf_from_nnf (Not fm1, new)         = (Not fm1, new)  (* 'fm1' must be a variable since the formula is in NNF *)
webertj@14939
   194
		(* pushing 'or' inside of 'and' using auxiliary variables *)
webertj@14939
   195
		  | defcnf_from_nnf (Or (fm1, fm2), new)   =
webertj@14452
   196
			let
webertj@14939
   197
				val (fm1', new')  = defcnf_from_nnf (fm1, new)
webertj@14939
   198
				val (fm2', new'') = defcnf_from_nnf (fm2, new')
webertj@14939
   199
				(* prop_formula * prop_formula -> int -> prop_formula * int *)
webertj@14939
   200
				fun defcnf_or (And (fm11, fm12), fm2) new =
webertj@14964
   201
					(case fm2 of
webertj@14964
   202
					(* do not introduce an auxiliary variable for literals *)
webertj@14964
   203
					  BoolVar _ =>
webertj@14964
   204
						let
webertj@14964
   205
							val (fm_a, new')  = defcnf_or (fm11, fm2) new
webertj@14964
   206
							val (fm_b, new'') = defcnf_or (fm12, fm2) new'
webertj@14964
   207
						in
webertj@14964
   208
							(And (fm_a, fm_b), new'')
webertj@14964
   209
						end
webertj@14964
   210
					| Not _ =>
webertj@14964
   211
						let
webertj@14964
   212
							val (fm_a, new')  = defcnf_or (fm11, fm2) new
webertj@14964
   213
							val (fm_b, new'') = defcnf_or (fm12, fm2) new'
webertj@14964
   214
						in
webertj@14964
   215
							(And (fm_a, fm_b), new'')
webertj@14964
   216
						end
webertj@14964
   217
					| _ =>
webertj@14964
   218
						let
webertj@14964
   219
							val aux            = BoolVar new
webertj@14964
   220
							val (fm_a, new')   = defcnf_or (fm11, aux)     (new+1)
webertj@14964
   221
							val (fm_b, new'')  = defcnf_or (fm12, aux)     new'
webertj@14964
   222
							val (fm_c, new''') = defcnf_or (fm2,  Not aux) new''
webertj@14964
   223
						in
webertj@14964
   224
							(And (And (fm_a, fm_b), fm_c), new''')
webertj@14964
   225
						end)
webertj@14939
   226
				  | defcnf_or (fm1, And (fm21, fm22)) new =
webertj@14964
   227
					(case fm1 of
webertj@14964
   228
					(* do not introduce an auxiliary variable for literals *)
webertj@14964
   229
					  BoolVar _ =>
webertj@14964
   230
						let
webertj@14964
   231
							val (fm_a, new')  = defcnf_or (fm1, fm21) new
webertj@14964
   232
							val (fm_b, new'') = defcnf_or (fm1, fm22) new'
webertj@14964
   233
						in
webertj@14964
   234
							(And (fm_a, fm_b), new'')
webertj@14964
   235
						end
webertj@14964
   236
					| Not _ =>
webertj@14964
   237
						let
webertj@14964
   238
							val (fm_a, new')  = defcnf_or (fm1, fm21) new
webertj@14964
   239
							val (fm_b, new'') = defcnf_or (fm1, fm22) new'
webertj@14964
   240
						in
webertj@14964
   241
							(And (fm_a, fm_b), new'')
webertj@14964
   242
						end
webertj@14964
   243
					| _ =>
webertj@14964
   244
						let
webertj@14964
   245
							val aux            = BoolVar new
webertj@14964
   246
							val (fm_a, new')   = defcnf_or (fm1,  Not aux) (new+1)
webertj@14964
   247
							val (fm_b, new'')  = defcnf_or (fm21, aux)     new'
webertj@14964
   248
							val (fm_c, new''') = defcnf_or (fm22, aux)     new''
webertj@14964
   249
						in
webertj@14964
   250
							(And (fm_a, And (fm_b, fm_c)), new''')
webertj@14964
   251
						end)
webertj@14939
   252
				(* neither subformula contains 'and' *)
webertj@14939
   253
				  | defcnf_or (fm1, fm2) new =
webertj@14939
   254
					(Or (fm1, fm2), new)
webertj@14939
   255
			in
webertj@14939
   256
				defcnf_or (fm1', fm2') new''
webertj@14452
   257
			end
webertj@14452
   258
		(* 'and' as outermost connective is left untouched *)
webertj@14939
   259
		  | defcnf_from_nnf (And (fm1, fm2), new)   =
webertj@14452
   260
			let
webertj@14939
   261
				val (fm1', new')  = defcnf_from_nnf (fm1, new)
webertj@14939
   262
				val (fm2', new'') = defcnf_from_nnf (fm2, new')
webertj@14452
   263
			in
webertj@14939
   264
				(And (fm1', fm2'), new'')
webertj@14452
   265
			end
webertj@14964
   266
		val fm' = nnf fm
webertj@14452
   267
	in
webertj@14939
   268
		(fst o defcnf_from_nnf) (fm', (maxidx fm')+1)
webertj@14452
   269
	end;
webertj@14452
   270
webertj@14452
   271
(* ------------------------------------------------------------------------- *)
webertj@14452
   272
(* exists: computes the disjunction over a list 'xs' of propositional        *)
webertj@14452
   273
(*      formulas                                                             *)
webertj@14452
   274
(* ------------------------------------------------------------------------- *)
webertj@14452
   275
webertj@14452
   276
	(* prop_formula list -> prop_formula *)
webertj@14452
   277
skalberg@15570
   278
	fun exists xs = Library.foldl SOr (False, xs);
webertj@14452
   279
webertj@14452
   280
(* ------------------------------------------------------------------------- *)
webertj@14452
   281
(* all: computes the conjunction over a list 'xs' of propositional formulas  *)
webertj@14452
   282
(* ------------------------------------------------------------------------- *)
webertj@14452
   283
webertj@14452
   284
	(* prop_formula list -> prop_formula *)
webertj@14452
   285
skalberg@15570
   286
	fun all xs = Library.foldl SAnd (True, xs);
webertj@14452
   287
webertj@14452
   288
(* ------------------------------------------------------------------------- *)
webertj@14452
   289
(* dot_product: ([x1,...,xn], [y1,...,yn]) -> x1*y1+...+xn*yn                *)
webertj@14452
   290
(* ------------------------------------------------------------------------- *)
webertj@14452
   291
webertj@14452
   292
	(* prop_formula list * prop_formula list -> prop_formula *)
webertj@14452
   293
webertj@14452
   294
	fun dot_product (xs,ys) = exists (map SAnd (xs~~ys));
webertj@14452
   295
webertj@14452
   296
(* ------------------------------------------------------------------------- *)
webertj@14753
   297
(* eval: given an assignment 'a' of Boolean values to variable indices, the  *)
webertj@14452
   298
(*      truth value of a propositional formula 'fm' is computed              *)
webertj@14452
   299
(* ------------------------------------------------------------------------- *)
webertj@14452
   300
webertj@14452
   301
	(* (int -> bool) -> prop_formula -> bool *)
webertj@14452
   302
webertj@14452
   303
	fun eval a True            = true
webertj@14452
   304
	  | eval a False           = false
webertj@14452
   305
	  | eval a (BoolVar i)     = (a i)
webertj@14452
   306
	  | eval a (Not fm)        = not (eval a fm)
webertj@14452
   307
	  | eval a (Or (fm1,fm2))  = (eval a fm1) orelse (eval a fm2)
webertj@14452
   308
	  | eval a (And (fm1,fm2)) = (eval a fm1) andalso (eval a fm2);
webertj@14452
   309
webertj@14452
   310
end;