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