src/HOL/Tools/prop_logic.ML
author webertj
Wed Mar 10 20:27:56 2004 +0100 (2004-03-10)
changeset 14452 c24d90dbf0c9
child 14681 16fcef3a3174
permissions -rw-r--r--
Formulas of propositional logic
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@14452
    23
	val indices : prop_formula -> int list  (* all variable indices *)
webertj@14452
    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@14452
    27
	val cnf    : prop_formula -> prop_formula  (* clause 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@14452
    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@14452
    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@14452
   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@14452
   139
(* cnf: computes the clause normal form (i.e. a conjunction of disjunctions) *)
webertj@14452
   140
(*      of a formula 'fm' of propositional logic.  The result formula may be *)
webertj@14452
   141
(*      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@14452
   150
		    cnf_from_nnf True              = True
webertj@14452
   151
		  | cnf_from_nnf False             = False
webertj@14452
   152
		(* literals *)
webertj@14452
   153
		  | cnf_from_nnf (BoolVar i)       = BoolVar i
webertj@14452
   154
		  | cnf_from_nnf (Not (BoolVar i)) = Not (BoolVar i)
webertj@14452
   155
		(* pushing 'or' inside of 'and' using distributive laws *)
webertj@14452
   156
		  | cnf_from_nnf (Or (fm1,fm2)) =
webertj@14452
   157
			let
webertj@14452
   158
				val fm1' = cnf_from_nnf fm1
webertj@14452
   159
				val fm2' = cnf_from_nnf fm2
webertj@14452
   160
			in
webertj@14452
   161
				case fm1' of
webertj@14452
   162
				  And (fm11,fm12) => cnf_from_nnf (SAnd (SOr(fm11,fm2'),SOr(fm12,fm2')))
webertj@14452
   163
				| _               =>
webertj@14452
   164
					(case fm2' of
webertj@14452
   165
					  And (fm21,fm22) => cnf_from_nnf (SAnd (SOr(fm1',fm21),SOr(fm1',fm22)))
webertj@14452
   166
					(* neither subformula contains 'and' *)
webertj@14452
   167
					| _               => Or (fm1,fm2))
webertj@14452
   168
			end
webertj@14452
   169
		(* 'and' as outermost connective is left untouched *)
webertj@14452
   170
		  | cnf_from_nnf (And (fm1,fm2))   = SAnd (cnf_from_nnf fm1, cnf_from_nnf fm2)
webertj@14452
   171
		(* 'not' + something other than a variable: formula is not in negation normal form *)
webertj@14452
   172
		  | cnf_from_nnf _                 = raise ERROR
webertj@14452
   173
	in
webertj@14452
   174
		(cnf_from_nnf o nnf) fm
webertj@14452
   175
	end;
webertj@14452
   176
webertj@14452
   177
(* ------------------------------------------------------------------------- *)
webertj@14452
   178
(* defcnf: computes the definitional clause normal form of a formula 'fm' of *)
webertj@14452
   179
(*      propositional logic, introducing auxiliary variables if necessary to *)
webertj@14452
   180
(*      avoid an exponential blowup of the formula.  The result formula is   *)
webertj@14452
   181
(*      satisfiable if and only if 'fm' is satisfiable.                      *)
webertj@14452
   182
(* ------------------------------------------------------------------------- *)
webertj@14452
   183
webertj@14452
   184
	(* prop_formula -> prop_formula *)
webertj@14452
   185
webertj@14452
   186
	fun defcnf fm =
webertj@14452
   187
	let
webertj@14452
   188
		(* prop_formula * int -> prop_formula * int *)
webertj@14452
   189
		(* 'new' specifies the next index that is available to introduce an auxiliary variable *)
webertj@14452
   190
		fun
webertj@14452
   191
		(* constants *)
webertj@14452
   192
		    defcnf_from_nnf (True,new)            = (True, new)
webertj@14452
   193
		  | defcnf_from_nnf (False,new)           = (False, new)
webertj@14452
   194
		(* literals *)
webertj@14452
   195
		  | defcnf_from_nnf (BoolVar i,new)       = (BoolVar i, new)
webertj@14452
   196
		  | defcnf_from_nnf (Not (BoolVar i),new) = (Not (BoolVar i), new)
webertj@14452
   197
		(* pushing 'or' inside of 'and' using distributive laws *)
webertj@14452
   198
		  | defcnf_from_nnf (Or (fm1,fm2),new)    =
webertj@14452
   199
			let
webertj@14452
   200
				val (fm1',new')  = defcnf_from_nnf (fm1, new)
webertj@14452
   201
				val (fm2',new'') = defcnf_from_nnf (fm2, new')
webertj@14452
   202
			in
webertj@14452
   203
				case fm1' of
webertj@14452
   204
				  And (fm11,fm12) =>
webertj@14452
   205
					let
webertj@14452
   206
						val aux = BoolVar new''
webertj@14452
   207
					in
webertj@14452
   208
						(* '(fm11 AND fm12) OR fm2' is SAT-equivalent to '(fm11 OR aux) AND (fm12 OR aux) AND (fm2 OR NOT aux)' *)
webertj@14452
   209
						defcnf_from_nnf (SAnd (SAnd (SOr (fm11,aux), SOr (fm12,aux)), SOr(fm2', Not aux)), new''+1)
webertj@14452
   210
					end
webertj@14452
   211
				| _               =>
webertj@14452
   212
					(case fm2' of
webertj@14452
   213
					  And (fm21,fm22) =>
webertj@14452
   214
						let
webertj@14452
   215
							val aux = BoolVar new''
webertj@14452
   216
						in
webertj@14452
   217
							(* 'fm1 OR (fm21 AND fm22)' is SAT-equivalent to '(fm1 OR NOT aux) AND (fm21 OR aux) AND (fm22 OR NOT aux)' *)
webertj@14452
   218
							defcnf_from_nnf (SAnd (SOr (fm1', Not aux), SAnd (SOr (fm21,aux), SOr (fm22,aux))), new''+1)
webertj@14452
   219
						end
webertj@14452
   220
					(* neither subformula contains 'and' *)
webertj@14452
   221
					| _               => (Or (fm1,fm2),new))
webertj@14452
   222
			end
webertj@14452
   223
		(* 'and' as outermost connective is left untouched *)
webertj@14452
   224
		  | defcnf_from_nnf (And (fm1,fm2),new)   =
webertj@14452
   225
			let
webertj@14452
   226
				val (fm1',new')  = defcnf_from_nnf (fm1, new)
webertj@14452
   227
				val (fm2',new'') = defcnf_from_nnf (fm2, new')
webertj@14452
   228
			in
webertj@14452
   229
				(SAnd (fm1', fm2'), new'')
webertj@14452
   230
			end
webertj@14452
   231
		(* 'not' + something other than a variable: formula is not in negation normal form *)
webertj@14452
   232
		  | defcnf_from_nnf (_,_)                 = raise ERROR
webertj@14452
   233
	in
webertj@14452
   234
		(fst o defcnf_from_nnf) (nnf fm, (maxidx fm)+1)
webertj@14452
   235
	end;
webertj@14452
   236
webertj@14452
   237
(* ------------------------------------------------------------------------- *)
webertj@14452
   238
(* exists: computes the disjunction over a list 'xs' of propositional        *)
webertj@14452
   239
(*      formulas                                                             *)
webertj@14452
   240
(* ------------------------------------------------------------------------- *)
webertj@14452
   241
webertj@14452
   242
	(* prop_formula list -> prop_formula *)
webertj@14452
   243
webertj@14452
   244
	fun exists xs = foldl SOr (False, xs);
webertj@14452
   245
webertj@14452
   246
(* ------------------------------------------------------------------------- *)
webertj@14452
   247
(* all: computes the conjunction over a list 'xs' of propositional formulas  *)
webertj@14452
   248
(* ------------------------------------------------------------------------- *)
webertj@14452
   249
webertj@14452
   250
	(* prop_formula list -> prop_formula *)
webertj@14452
   251
webertj@14452
   252
	fun all xs = foldl SAnd (True, xs);
webertj@14452
   253
webertj@14452
   254
(* ------------------------------------------------------------------------- *)
webertj@14452
   255
(* dot_product: ([x1,...,xn], [y1,...,yn]) -> x1*y1+...+xn*yn                *)
webertj@14452
   256
(* ------------------------------------------------------------------------- *)
webertj@14452
   257
webertj@14452
   258
	(* prop_formula list * prop_formula list -> prop_formula *)
webertj@14452
   259
webertj@14452
   260
	fun dot_product (xs,ys) = exists (map SAnd (xs~~ys));
webertj@14452
   261
webertj@14452
   262
(* ------------------------------------------------------------------------- *)
webertj@14452
   263
(* eval: given an assignment 'a' of boolean values to variable indices, the  *)
webertj@14452
   264
(*      truth value of a propositional formula 'fm' is computed              *)
webertj@14452
   265
(* ------------------------------------------------------------------------- *)
webertj@14452
   266
webertj@14452
   267
	(* (int -> bool) -> prop_formula -> bool *)
webertj@14452
   268
webertj@14452
   269
	fun eval a True            = true
webertj@14452
   270
	  | eval a False           = false
webertj@14452
   271
	  | eval a (BoolVar i)     = (a i)
webertj@14452
   272
	  | eval a (Not fm)        = not (eval a fm)
webertj@14452
   273
	  | eval a (Or (fm1,fm2))  = (eval a fm1) orelse (eval a fm2)
webertj@14452
   274
	  | eval a (And (fm1,fm2)) = (eval a fm1) andalso (eval a fm2);
webertj@14452
   275
webertj@14452
   276
end;