src/HOL/Tools/prop_logic.ML
author webertj
Mon Jul 25 15:51:30 2005 +0200 (2005-07-25)
changeset 16907 2187e3f94761
parent 15570 8d8c70b41bab
child 16909 acbc7a9c3864
permissions -rw-r--r--
defcnf modified to internally use a reference
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@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@16907
   179
(*      Auxiliary variables are introduced as switches for OR-nodes, based   *)
webertj@16907
   180
(*      on the observation that "fm1 OR (fm21 AND fm22)" is equisatisfiable  *)
webertj@16907
   181
(*      with "(fm1 OR ~aux) AND (fm21 OR aux) AND (fm22 OR aux)".            *)
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@16907
   188
		(* convert formula to NNF first *)
webertj@16907
   189
		val fm' = nnf fm
webertj@14452
   190
		(* 'new' specifies the next index that is available to introduce an auxiliary variable *)
webertj@16907
   191
		(* int ref *)
webertj@16907
   192
		val new = ref (maxidx fm' + 1)
webertj@16907
   193
		(* unit -> int *)
webertj@16907
   194
		fun new_idx () = let val idx = !new in new := idx+1; idx end
webertj@16907
   195
		(* prop_formula -> prop_formula *)
webertj@14452
   196
		fun
webertj@14452
   197
		(* constants *)
webertj@16907
   198
		    defcnf_from_nnf True  = True
webertj@16907
   199
		  | defcnf_from_nnf False = False
webertj@14452
   200
		(* literals *)
webertj@16907
   201
		  | defcnf_from_nnf (BoolVar i) = BoolVar i
webertj@16907
   202
		  | defcnf_from_nnf (Not fm1)   = Not fm1  (* 'fm1' must be a variable since the formula is in NNF *)
webertj@14939
   203
		(* pushing 'or' inside of 'and' using auxiliary variables *)
webertj@16907
   204
		  | defcnf_from_nnf (Or (fm1, fm2)) =
webertj@14452
   205
			let
webertj@16907
   206
				val fm1' = defcnf_from_nnf fm1
webertj@16907
   207
				val fm2' = defcnf_from_nnf fm2
webertj@16907
   208
				(* prop_formula * prop_formula -> prop_formula *)
webertj@16907
   209
				fun defcnf_or (And (fm11, fm12), fm2) =
webertj@14964
   210
					(case fm2 of
webertj@14964
   211
					(* do not introduce an auxiliary variable for literals *)
webertj@14964
   212
					  BoolVar _ =>
webertj@16907
   213
							And (defcnf_or (fm11, fm2), defcnf_or (fm12, fm2))
webertj@14964
   214
					| Not _ =>
webertj@16907
   215
							And (defcnf_or (fm11, fm2), defcnf_or (fm12, fm2))
webertj@14964
   216
					| _ =>
webertj@14964
   217
						let
webertj@16907
   218
							val aux = BoolVar (new_idx ())
webertj@14964
   219
						in
webertj@16907
   220
							And (And (defcnf_or (fm11, aux), defcnf_or (fm12, aux)), defcnf_or (fm2, Not aux))
webertj@14964
   221
						end)
webertj@16907
   222
				  | defcnf_or (fm1, And (fm21, fm22)) =
webertj@14964
   223
					(case fm1 of
webertj@14964
   224
					(* do not introduce an auxiliary variable for literals *)
webertj@14964
   225
					  BoolVar _ =>
webertj@16907
   226
							And (defcnf_or (fm1, fm21), defcnf_or (fm1, fm22))
webertj@14964
   227
					| Not _ =>
webertj@16907
   228
							And (defcnf_or (fm1, fm21), defcnf_or (fm1, fm22))
webertj@14964
   229
					| _ =>
webertj@14964
   230
						let
webertj@16907
   231
							val aux = BoolVar (new_idx ())
webertj@14964
   232
						in
webertj@16907
   233
							And (defcnf_or (fm1, Not aux), And (defcnf_or (fm21, aux), defcnf_or (fm22, aux)))
webertj@14964
   234
						end)
webertj@14939
   235
				(* neither subformula contains 'and' *)
webertj@16907
   236
				  | defcnf_or (fm1, fm2) =
webertj@16907
   237
					Or (fm1, fm2)
webertj@14939
   238
			in
webertj@16907
   239
				defcnf_or (fm1', fm2')
webertj@14452
   240
			end
webertj@14452
   241
		(* 'and' as outermost connective is left untouched *)
webertj@16907
   242
		  | defcnf_from_nnf (And (fm1, fm2)) =
webertj@16907
   243
				And (defcnf_from_nnf fm1, defcnf_from_nnf fm2)
webertj@14452
   244
	in
webertj@16907
   245
		defcnf_from_nnf fm'
webertj@14452
   246
	end;
webertj@14452
   247
webertj@14452
   248
(* ------------------------------------------------------------------------- *)
webertj@14452
   249
(* exists: computes the disjunction over a list 'xs' of propositional        *)
webertj@14452
   250
(*      formulas                                                             *)
webertj@14452
   251
(* ------------------------------------------------------------------------- *)
webertj@14452
   252
webertj@14452
   253
	(* prop_formula list -> prop_formula *)
webertj@14452
   254
skalberg@15570
   255
	fun exists xs = Library.foldl SOr (False, xs);
webertj@14452
   256
webertj@14452
   257
(* ------------------------------------------------------------------------- *)
webertj@14452
   258
(* all: computes the conjunction over a list 'xs' of propositional formulas  *)
webertj@14452
   259
(* ------------------------------------------------------------------------- *)
webertj@14452
   260
webertj@14452
   261
	(* prop_formula list -> prop_formula *)
webertj@14452
   262
skalberg@15570
   263
	fun all xs = Library.foldl SAnd (True, xs);
webertj@14452
   264
webertj@14452
   265
(* ------------------------------------------------------------------------- *)
webertj@14452
   266
(* dot_product: ([x1,...,xn], [y1,...,yn]) -> x1*y1+...+xn*yn                *)
webertj@14452
   267
(* ------------------------------------------------------------------------- *)
webertj@14452
   268
webertj@14452
   269
	(* prop_formula list * prop_formula list -> prop_formula *)
webertj@14452
   270
webertj@14452
   271
	fun dot_product (xs,ys) = exists (map SAnd (xs~~ys));
webertj@14452
   272
webertj@14452
   273
(* ------------------------------------------------------------------------- *)
webertj@14753
   274
(* eval: given an assignment 'a' of Boolean values to variable indices, the  *)
webertj@14452
   275
(*      truth value of a propositional formula 'fm' is computed              *)
webertj@14452
   276
(* ------------------------------------------------------------------------- *)
webertj@14452
   277
webertj@14452
   278
	(* (int -> bool) -> prop_formula -> bool *)
webertj@14452
   279
webertj@14452
   280
	fun eval a True            = true
webertj@14452
   281
	  | eval a False           = false
webertj@14452
   282
	  | eval a (BoolVar i)     = (a i)
webertj@14452
   283
	  | eval a (Not fm)        = not (eval a fm)
webertj@14452
   284
	  | eval a (Or (fm1,fm2))  = (eval a fm1) orelse (eval a fm2)
webertj@14452
   285
	  | eval a (And (fm1,fm2)) = (eval a fm1) andalso (eval a fm2);
webertj@14452
   286
webertj@14452
   287
end;