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