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