src/HOL/Tools/prop_logic.ML
author wenzelm
Mon, 29 Aug 2005 16:18:04 +0200
changeset 17184 3d80209e9a53
parent 16913 1d8a8d010e69
child 17809 195045659c06
permissions -rw-r--r--
use AList operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
     1
(*  Title:      HOL/Tools/prop_logic.ML
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
     2
    ID:         $Id$
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
     3
    Author:     Tjark Weber
16907
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
     4
    Copyright   2004-2005
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
     5
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
     6
Formulas of propositional logic.
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
     7
*)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
     8
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
     9
signature PROP_LOGIC =
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    10
sig
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    11
	datatype prop_formula =
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    12
		  True
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    13
		| False
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    14
		| BoolVar of int  (* NOTE: only use indices >= 1 *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    15
		| Not of prop_formula
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    16
		| Or of prop_formula * prop_formula
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    17
		| And of prop_formula * prop_formula
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    18
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    19
	val SNot     : prop_formula -> prop_formula
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    20
	val SOr      : prop_formula * prop_formula -> prop_formula
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    21
	val SAnd     : prop_formula * prop_formula -> prop_formula
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    22
	val simplify : prop_formula -> prop_formula  (* eliminates True/False and double-negation *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    23
14681
16fcef3a3174 comments modified
webertj
parents: 14452
diff changeset
    24
	val indices : prop_formula -> int list  (* set of all variable indices *)
15301
26724034de5e comment modified
webertj
parents: 14964
diff changeset
    25
	val maxidx  : prop_formula -> int       (* maximal variable index *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    26
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    27
	val exists      : prop_formula list -> prop_formula  (* finite disjunction *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    28
	val all         : prop_formula list -> prop_formula  (* finite conjunction *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    29
	val dot_product : prop_formula list * prop_formula list -> prop_formula
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    30
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    31
	val nnf    : prop_formula -> prop_formula  (* negation normal form *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    32
	val cnf    : prop_formula -> prop_formula  (* conjunctive normal form *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    33
	val auxcnf : prop_formula -> prop_formula  (* cnf with auxiliary variables *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    34
	val defcnf : prop_formula -> prop_formula  (* definitional cnf *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    35
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    36
	val eval : (int -> bool) -> prop_formula -> bool  (* semantics *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    37
end;
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    38
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    39
structure PropLogic : PROP_LOGIC =
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    40
struct
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    41
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    42
(* ------------------------------------------------------------------------- *)
14753
f40b45db8cf0 Comments fixed
webertj
parents: 14681
diff changeset
    43
(* prop_formula: formulas of propositional logic, built from Boolean         *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    44
(*               variables (referred to by index) and True/False using       *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    45
(*               not/or/and                                                  *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    46
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    47
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    48
	datatype prop_formula =
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    49
		  True
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    50
		| False
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    51
		| BoolVar of int  (* NOTE: only use indices >= 1 *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    52
		| Not of prop_formula
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    53
		| Or of prop_formula * prop_formula
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    54
		| And of prop_formula * prop_formula;
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    55
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    56
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    57
(* The following constructor functions make sure that True and False do not  *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    58
(* occur within any of the other connectives (i.e. Not, Or, And), and        *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    59
(* perform double-negation elimination.                                      *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    60
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    61
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    62
	(* prop_formula -> prop_formula *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    63
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    64
	fun SNot True     = False
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    65
	  | SNot False    = True
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    66
	  | SNot (Not fm) = fm
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    67
	  | SNot fm       = Not fm;
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    68
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    69
	(* prop_formula * prop_formula -> prop_formula *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    70
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    71
	fun SOr (True, _)   = True
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    72
	  | SOr (_, True)   = True
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    73
	  | SOr (False, fm) = fm
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    74
	  | SOr (fm, False) = fm
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    75
	  | SOr (fm1, fm2)  = Or (fm1, fm2);
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    76
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    77
	(* prop_formula * prop_formula -> prop_formula *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    78
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    79
	fun SAnd (True, fm) = fm
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    80
	  | SAnd (fm, True) = fm
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    81
	  | SAnd (False, _) = False
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    82
	  | SAnd (_, False) = False
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    83
	  | SAnd (fm1, fm2) = And (fm1, fm2);
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    84
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    85
(* ------------------------------------------------------------------------- *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    86
(* simplify: eliminates True/False below other connectives, and double-      *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    87
(*      negation                                                             *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    88
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    89
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    90
	(* prop_formula -> prop_formula *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    91
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    92
	fun simplify (Not fm)         = SNot (simplify fm)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    93
	  | simplify (Or (fm1, fm2))  = SOr (simplify fm1, simplify fm2)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    94
	  | simplify (And (fm1, fm2)) = SAnd (simplify fm1, simplify fm2)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    95
	  | simplify fm               = fm;
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    96
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
    97
(* ------------------------------------------------------------------------- *)
14753
f40b45db8cf0 Comments fixed
webertj
parents: 14681
diff changeset
    98
(* indices: collects all indices of Boolean variables that occur in a        *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
    99
(*      propositional formula 'fm'; no duplicates                            *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   100
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   101
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   102
	(* prop_formula -> int list *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   103
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   104
	fun indices True             = []
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   105
	  | indices False            = []
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   106
	  | indices (BoolVar i)      = [i]
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   107
	  | indices (Not fm)         = indices fm
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   108
	  | indices (Or (fm1, fm2))  = (indices fm1) union_int (indices fm2)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   109
	  | indices (And (fm1, fm2)) = (indices fm1) union_int (indices fm2);
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   110
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   111
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   112
(* maxidx: computes the maximal variable index occuring in a formula of      *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   113
(*      propositional logic 'fm'; 0 if 'fm' contains no variable             *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   114
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   115
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   116
	(* prop_formula -> int *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   117
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   118
	fun maxidx True             = 0
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   119
	  | maxidx False            = 0
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   120
	  | maxidx (BoolVar i)      = i
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   121
	  | maxidx (Not fm)         = maxidx fm
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   122
	  | maxidx (Or (fm1, fm2))  = Int.max (maxidx fm1, maxidx fm2)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   123
	  | maxidx (And (fm1, fm2)) = Int.max (maxidx fm1, maxidx fm2);
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   124
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   125
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   126
(* exists: computes the disjunction over a list 'xs' of propositional        *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   127
(*      formulas                                                             *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   128
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   129
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   130
	(* prop_formula list -> prop_formula *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   131
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   132
	fun exists xs = Library.foldl SOr (False, xs);
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   133
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   134
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   135
(* all: computes the conjunction over a list 'xs' of propositional formulas  *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   136
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   137
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   138
	(* prop_formula list -> prop_formula *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   139
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   140
	fun all xs = Library.foldl SAnd (True, xs);
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   141
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   142
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   143
(* dot_product: ([x1,...,xn], [y1,...,yn]) -> x1*y1+...+xn*yn                *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   144
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   145
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   146
	(* prop_formula list * prop_formula list -> prop_formula *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   147
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   148
	fun dot_product (xs,ys) = exists (map SAnd (xs~~ys));
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   149
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   150
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   151
(* nnf: computes the negation normal form of a formula 'fm' of propositional *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   152
(*      logic (i.e. only variables may be negated, but not subformulas).     *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   153
(*      Simplification (c.f. 'simplify') is performed as well.               *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   154
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   155
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   156
	(* prop_formula -> prop_formula *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   157
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   158
	fun
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   159
	(* constants *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   160
	    nnf True                   = True
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   161
	  | nnf False                  = False
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   162
	(* variables *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   163
	  | nnf (BoolVar i)            = (BoolVar i)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   164
	(* 'or' and 'and' as outermost connectives are left untouched *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   165
	  | nnf (Or  (fm1, fm2))       = SOr  (nnf fm1, nnf fm2)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   166
	  | nnf (And (fm1, fm2))       = SAnd (nnf fm1, nnf fm2)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   167
	(* 'not' + constant *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   168
	  | nnf (Not True)             = False
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   169
	  | nnf (Not False)            = True
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   170
	(* 'not' + variable *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   171
	  | nnf (Not (BoolVar i))      = Not (BoolVar i)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   172
	(* pushing 'not' inside of 'or'/'and' using de Morgan's laws *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   173
	  | nnf (Not (Or  (fm1, fm2))) = SAnd (nnf (SNot fm1), nnf (SNot fm2))
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   174
	  | nnf (Not (And (fm1, fm2))) = SOr  (nnf (SNot fm1), nnf (SNot fm2))
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   175
	(* double-negation elimination *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   176
	  | nnf (Not (Not fm))         = nnf fm;
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   177
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   178
(* ------------------------------------------------------------------------- *)
14681
16fcef3a3174 comments modified
webertj
parents: 14452
diff changeset
   179
(* cnf: computes the conjunctive normal form (i.e. a conjunction of          *)
16fcef3a3174 comments modified
webertj
parents: 14452
diff changeset
   180
(*      disjunctions) of a formula 'fm' of propositional logic.  The result  *)
16fcef3a3174 comments modified
webertj
parents: 14452
diff changeset
   181
(*      formula may be exponentially longer than 'fm'.                       *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   182
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   183
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   184
	(* prop_formula -> prop_formula *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   185
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   186
	fun cnf fm =
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   187
	let
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   188
		fun
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   189
		(* constants *)
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   190
		    cnf_from_nnf True             = True
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   191
		  | cnf_from_nnf False            = False
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   192
		(* literals *)
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   193
		  | cnf_from_nnf (BoolVar i)      = BoolVar i
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   194
		  | cnf_from_nnf (Not fm1)        = Not fm1  (* 'fm1' must be a variable since the formula is in NNF *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   195
		(* pushing 'or' inside of 'and' using distributive laws *)
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   196
		  | cnf_from_nnf (Or (fm1, fm2))  =
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   197
			let
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   198
				fun cnf_or (And (fm11, fm12), fm2) =
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   199
					And (cnf_or (fm11, fm2), cnf_or (fm12, fm2))
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   200
				  | cnf_or (fm1, And (fm21, fm22)) =
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   201
					And (cnf_or (fm1, fm21), cnf_or (fm1, fm22))
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   202
				(* neither subformula contains 'and' *)
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   203
				  | cnf_or (fm1, fm2) =
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   204
					Or (fm1, fm2)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   205
			in
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   206
				cnf_or (cnf_from_nnf fm1, cnf_from_nnf fm2)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   207
			end
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   208
		(* 'and' as outermost connective is left untouched *)
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   209
		  | cnf_from_nnf (And (fm1, fm2)) = And (cnf_from_nnf fm1, cnf_from_nnf fm2)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   210
	in
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   211
		(cnf_from_nnf o nnf) fm
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   212
	end;
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   213
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   214
(* ------------------------------------------------------------------------- *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   215
(* auxcnf: computes the definitional conjunctive normal form of a formula    *)
14681
16fcef3a3174 comments modified
webertj
parents: 14452
diff changeset
   216
(*      'fm' of propositional logic, introducing auxiliary variables if      *)
16fcef3a3174 comments modified
webertj
parents: 14452
diff changeset
   217
(*      necessary to avoid an exponential blowup of the formula.  The result *)
16fcef3a3174 comments modified
webertj
parents: 14452
diff changeset
   218
(*      formula is satisfiable if and only if 'fm' is satisfiable.           *)
16907
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   219
(*      Auxiliary variables are introduced as switches for OR-nodes, based   *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   220
(*      on the observation that e.g. "fm1 OR (fm21 AND fm22)" is             *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   221
(*      equisatisfiable with "(fm1 OR ~aux) AND (fm21 OR aux) AND (fm22 OR   *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   222
(*      aux)".                                                               *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   223
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   224
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   225
(* ------------------------------------------------------------------------- *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   226
(* Note: 'auxcnf' tends to use fewer variables and fewer clauses than        *)
16913
1d8a8d010e69 comment modified
webertj
parents: 16909
diff changeset
   227
(*      'defcnf' below, but sometimes generates much larger SAT problems     *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   228
(*      overall (hence it must sometimes generate longer clauses than        *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   229
(*      'defcnf' does).  It is currently not quite clear to me if one of the *)
16913
1d8a8d010e69 comment modified
webertj
parents: 16909
diff changeset
   230
(*      algorithms is clearly superior to the other, but I suggest using     *)
1d8a8d010e69 comment modified
webertj
parents: 16909
diff changeset
   231
(*      'defcnf' instead.                                                    *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   232
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   233
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   234
	(* prop_formula -> prop_formula *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   235
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   236
	fun auxcnf fm =
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   237
	let
16907
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   238
		(* convert formula to NNF first *)
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   239
		val fm' = nnf fm
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   240
		(* 'new' specifies the next index that is available to introduce an auxiliary variable *)
16907
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   241
		(* int ref *)
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   242
		val new = ref (maxidx fm' + 1)
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   243
		(* unit -> int *)
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   244
		fun new_idx () = let val idx = !new in new := idx+1; idx end
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   245
		(* prop_formula -> prop_formula *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   246
		fun
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   247
		(* constants *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   248
		    auxcnf_from_nnf True  = True
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   249
		  | auxcnf_from_nnf False = False
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   250
		(* literals *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   251
		  | auxcnf_from_nnf (BoolVar i) = BoolVar i
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   252
		  | auxcnf_from_nnf (Not fm1)   = Not fm1  (* 'fm1' must be a variable since the formula is in NNF *)
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   253
		(* pushing 'or' inside of 'and' using auxiliary variables *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   254
		  | auxcnf_from_nnf (Or (fm1, fm2)) =
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   255
			let
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   256
				val fm1' = auxcnf_from_nnf fm1
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   257
				val fm2' = auxcnf_from_nnf fm2
16907
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   258
				(* prop_formula * prop_formula -> prop_formula *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   259
				fun auxcnf_or (And (fm11, fm12), fm2) =
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   260
					(case fm2 of
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   261
					(* do not introduce an auxiliary variable for literals *)
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   262
					  BoolVar _ =>
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   263
							And (auxcnf_or (fm11, fm2), auxcnf_or (fm12, fm2))
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   264
					| Not _ =>
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   265
							And (auxcnf_or (fm11, fm2), auxcnf_or (fm12, fm2))
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   266
					| _ =>
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   267
						let
16907
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   268
							val aux = BoolVar (new_idx ())
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   269
						in
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   270
							And (And (auxcnf_or (fm11, aux), auxcnf_or (fm12, aux)), auxcnf_or (fm2, Not aux))
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   271
						end)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   272
				  | auxcnf_or (fm1, And (fm21, fm22)) =
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   273
					(case fm1 of
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   274
					(* do not introduce an auxiliary variable for literals *)
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   275
					  BoolVar _ =>
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   276
							And (auxcnf_or (fm1, fm21), auxcnf_or (fm1, fm22))
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   277
					| Not _ =>
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   278
							And (auxcnf_or (fm1, fm21), auxcnf_or (fm1, fm22))
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   279
					| _ =>
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   280
						let
16907
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   281
							val aux = BoolVar (new_idx ())
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   282
						in
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   283
							And (auxcnf_or (fm1, Not aux), And (auxcnf_or (fm21, aux), auxcnf_or (fm22, aux)))
14964
2c1456d705e9 improved defcnf conversion
webertj
parents: 14939
diff changeset
   284
						end)
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   285
				(* neither subformula contains 'and' *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   286
				  | auxcnf_or (fm1, fm2) =
16907
2187e3f94761 defcnf modified to internally use a reference
webertj
parents: 15570
diff changeset
   287
					Or (fm1, fm2)
14939
29fe4a9a7cb5 faster defcnf conversion
webertj
parents: 14753
diff changeset
   288
			in
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   289
				auxcnf_or (fm1', fm2')
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   290
			end
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   291
		(* 'and' as outermost connective is left untouched *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   292
		  | auxcnf_from_nnf (And (fm1, fm2)) =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   293
				And (auxcnf_from_nnf fm1, auxcnf_from_nnf fm2)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   294
	in
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   295
		auxcnf_from_nnf fm'
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   296
	end;
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   297
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   298
(* ------------------------------------------------------------------------- *)
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   299
(* defcnf: computes the definitional conjunctive normal form of a formula    *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   300
(*      'fm' of propositional logic, introducing auxiliary variables to      *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   301
(*      avoid an exponential blowup of the formula.  The result formula is   *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   302
(*      satisfiable if and only if 'fm' is satisfiable.  Auxiliary variables *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   303
(*      are introduced as abbreviations for AND-, OR-, and NOT-nodes, based  *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   304
(*      on the following equisatisfiabilities (+/- indicates polarity):      *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   305
(*      LITERAL+       == LITERAL                                            *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   306
(*      LITERAL-       == NOT LITERAL                                        *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   307
(*      (NOT fm1)+     == aux AND (NOT aux OR fm1-)                          *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   308
(*      (fm1 OR fm2)+  == aux AND (NOT aux OR fm1+ OR fm2+)                  *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   309
(*      (fm1 AND fm2)+ == aux AND (NOT aux OR fm1+) AND (NOT aux OR fm2+)    *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   310
(*      (NOT fm1)-     == aux AND (NOT aux OR fm1+)                          *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   311
(*      (fm1 OR fm2)-  == aux AND (NOT aux OR fm1-) AND (NOT aux OR fm2-)    *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   312
(*      (fm1 AND fm2)- == aux AND (NOT aux OR fm1- OR fm2-)                  *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   313
(*      Example:                                                             *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   314
(*      NOT (a AND b) == aux1 AND (NOT aux1 OR aux2)                         *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   315
(*                            AND (NOT aux2 OR NOT a OR NOT b)               *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   316
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   317
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   318
	(* prop_formula -> prop_formula *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   319
16909
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   320
	fun defcnf fm =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   321
	let
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   322
		(* simplify formula first *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   323
		val fm' = simplify fm
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   324
		(* 'new' specifies the next index that is available to introduce an auxiliary variable *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   325
		(* int ref *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   326
		val new = ref (maxidx fm' + 1)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   327
		(* unit -> int *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   328
		fun new_idx () = let val idx = !new in new := idx+1; idx end
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   329
		(* optimization for n-ary disjunction/conjunction *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   330
		(* prop_formula -> prop_formula list *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   331
		fun disjuncts (Or (fm1, fm2)) = (disjuncts fm1) @ (disjuncts fm2)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   332
		  | disjuncts fm1             = [fm1]
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   333
		(* prop_formula -> prop_formula list *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   334
		fun conjuncts (And (fm1, fm2)) = (conjuncts fm1) @ (conjuncts fm2)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   335
		  | conjuncts fm1              = [fm1]
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   336
		(* polarity -> formula -> (defining clauses, literal) *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   337
		(* bool -> prop_formula -> prop_formula * prop_formula *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   338
		fun
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   339
		(* constants *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   340
		    defcnf' true  True  = (True, True)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   341
		  | defcnf' false True  = (*(True, False)*) error "formula is not simplified, True occurs with negative polarity"
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   342
		  | defcnf' true  False = (True, False)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   343
		  | defcnf' false False = (*(True, True)*) error "formula is not simplified, False occurs with negative polarity"
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   344
		(* literals *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   345
		  | defcnf' true  (BoolVar i)       = (True, BoolVar i)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   346
		  | defcnf' false (BoolVar i)       = (True, Not (BoolVar i))
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   347
		  | defcnf' true  (Not (BoolVar i)) = (True, Not (BoolVar i))
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   348
		  | defcnf' false (Not (BoolVar i)) = (True, BoolVar i)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   349
		(* 'not' *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   350
		  | defcnf' polarity (Not fm1) =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   351
			let
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   352
				val (def1, aux1) = defcnf' (not polarity) fm1
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   353
				val aux          = BoolVar (new_idx ())
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   354
				val def          = Or (Not aux, aux1)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   355
			in
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   356
				(SAnd (def1, def), aux)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   357
			end
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   358
		(* 'or' *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   359
		  | defcnf' polarity (Or (fm1, fm2)) =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   360
			let
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   361
				val fms          = disjuncts (Or (fm1, fm2))
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   362
				val (defs, auxs) = split_list (map (defcnf' polarity) fms)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   363
				val aux          = BoolVar (new_idx ())
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   364
				val def          = if polarity then Or (Not aux, exists auxs) else all (map (fn a => Or (Not aux, a)) auxs)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   365
			in
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   366
				(SAnd (all defs, def), aux)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   367
			end
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   368
		(* 'and' *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   369
		  | defcnf' polarity (And (fm1, fm2)) =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   370
			let
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   371
				val fms          = conjuncts (And (fm1, fm2))
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   372
				val (defs, auxs) = split_list (map (defcnf' polarity) fms)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   373
				val aux          = BoolVar (new_idx ())
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   374
				val def          = if not polarity then Or (Not aux, exists auxs) else all (map (fn a => Or (Not aux, a)) auxs)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   375
			in
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   376
				(SAnd (all defs, def), aux)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   377
			end
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   378
		(* optimization: do not introduce auxiliary variables for parts of the formula that are in CNF already *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   379
		(* prop_formula -> prop_formula * prop_formula *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   380
		fun defcnf_or (Or (fm1, fm2)) =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   381
			let
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   382
				val (def1, aux1) = defcnf_or fm1
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   383
				val (def2, aux2) = defcnf_or fm2
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   384
			in
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   385
				(SAnd (def1, def2), Or (aux1, aux2))
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   386
			end
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   387
		  | defcnf_or fm =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   388
			defcnf' true fm
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   389
		(* prop_formula -> prop_formula * prop_formula *)
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   390
		fun defcnf_and (And (fm1, fm2)) =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   391
			let
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   392
				val (def1, aux1) = defcnf_and fm1
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   393
				val (def2, aux2) = defcnf_and fm2
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   394
			in
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   395
				(SAnd (def1, def2), And (aux1, aux2))
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   396
			end
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   397
		  | defcnf_and (Or (fm1, fm2)) =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   398
			let
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   399
				val (def1, aux1) = defcnf_or fm1
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   400
				val (def2, aux2) = defcnf_or fm2
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   401
			in
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   402
				(SAnd (def1, def2), Or (aux1, aux2))
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   403
			end
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   404
		  | defcnf_and fm =
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   405
			defcnf' true fm
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   406
	in
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   407
		SAnd (defcnf_and fm')
acbc7a9c3864 defcnf renamed to auxcnf, new defcnf algorithm added, simplify added
webertj
parents: 16907
diff changeset
   408
	end;
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   409
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   410
(* ------------------------------------------------------------------------- *)
14753
f40b45db8cf0 Comments fixed
webertj
parents: 14681
diff changeset
   411
(* eval: given an assignment 'a' of Boolean values to variable indices, the  *)
14452
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   412
(*      truth value of a propositional formula 'fm' is computed              *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   413
(* ------------------------------------------------------------------------- *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   414
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   415
	(* (int -> bool) -> prop_formula -> bool *)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   416
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   417
	fun eval a True            = true
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   418
	  | eval a False           = false
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   419
	  | eval a (BoolVar i)     = (a i)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   420
	  | eval a (Not fm)        = not (eval a fm)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   421
	  | eval a (Or (fm1,fm2))  = (eval a fm1) orelse (eval a fm2)
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   422
	  | eval a (And (fm1,fm2)) = (eval a fm1) andalso (eval a fm2);
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   423
c24d90dbf0c9 Formulas of propositional logic
webertj
parents:
diff changeset
   424
end;