630 likes | 773 Views
Disciplined Software Engineering Lecture #9. Watts S. Humphrey Software Engineering Institute Carnegie Mellon University Pittsburgh, PA 15213 Sponsored by the U.S. Department of Defense. Additional material by James P Alstad March 2002, 2004 Indicated by italics.
E N D
Disciplined Software Engineering Lecture #9 • Watts S. Humphrey • Software Engineering Institute • Carnegie Mellon University • Pittsburgh, PA 15213 • Sponsored by the U.S. Department of Defense • Additional material by • James P Alstad • March 2002, 2004 • Indicated by italics
Design Notation - Overview • The importance of notation • Boolean algebra • Karnaugh maps • useful symbols • using Boolean functions • Simplification • Formal methods
The Importance of Notation • Many software development defects are caused by misunderstandings and confusion. • The notation used to represent program requirements and designs should be • precise and concise • convenient to use • commonly understood • suitable for representing a broad range of software functions • Formal notations meet these criteria.
Class Properties- Mathematical Notation English Symbol Example Union, sum A or B, A+B Intersection, product A and B, A*B, AB Complement, not ' A', not A All, universal 1 None, empty set 0 Is a subset of <= A <= B, 0 <= 1
Using This Notation • Advantages • builds familiarity with an important notation • consistent with the notation used in formal methods for proving program correctness • distinguishes logic from other expressions • Disadvantages • generally takes more time to enter • will take practice to build familiarity • your coworkers may not understand it
Boolean Algebra • The algebra of classes • defines and manipulates class relationships • consistency - whether statements are self- consistent • validity - whether statements represent reality • fairly transparent - simpler cases are easy to translate to/from English • Class representations • Venn diagrams • basic relationships • the universal and empty sets
Venn Diagrams • Venn diagrams are an easily understood way of illustrating simple Boolean situations Humans Humans Non-adult Women Women Non- adult Men Men Adult Men Adult Women Universal Class A’B’C’ A’BC’ AB’C’ ABC’ ABC AB’C A’BC A’B’C
Properties of Classes - 1 • Reflexive: for every x, x<=x • Idempotent: xx=x*, x+x=x* • Antisymmetric: if x<=y and y<=x then x=y • Transitive: if x<=y and y<=z then x<=z • Commutative: xy=yx, x+y=y+x • Associative: x(yz)=(xy)z, x+(y+z)=(x+y)+z • Distributive: x(y+z)=xy+xz, x+yz=(x+y)(x+z)* • *Different than arithmetic
Properties of Classes - 2 • Empty and universal classes: • xx’=0, x+x’=1, 0<=x<=1 • x0=0, x+1=1, x+0=x, x1=x • Consistency: x<=y, xy=x, x+y=y • are equivalent • Absorption: x(x+y)=x+xy=x • DeMorgan’s theorem: • (x’)’=x, (xy)’=x’+y’, (x+y)’=x’y’
An Example • Suppose you had a switch that you wanted to be on under the following conditions • A or B or C are on • not when either: • A and B are on, or • A and C are on • You would represent this as follows • On = (A+B+C)*[(A*B)+(A*C)]’
Simplifying Boolean Expressions • Consider the following guidelines when you simplify Boolean expressions • if there are any primed parenthetical expressions, use DeMorgan’s theorem to remove them • look for common terms that can be factored by the distributive law • make substitutions to reduce complexity • attempt to reduce expressions to a union of intersections
Simplification Example - 1 • Use the light bulb example • On = (A+B+C)*[(A*B)+(A*C)]’ • Apply DeMorgan’s theorem • On = (A+B+C)*[(A’+B’)*(A’+C’)] • Now, A’ is common to the last terms so • On = (A+B+C)*[A’+(B’*C’)] • Now, substitute x for B+C, and x’ for B’*C’ • On = (A+x)*(A’+x’)
Simplification Example - 2 • While this expression is fully simplified, it should be converted to a union of intersections • On = (A+x)*(A’+x’) • On = [(A+x)*A’]+[(A+x)*x’] • On = [(A*A’)+(x*A’)]+[(A*x’)+(x*x’)] • On = (x*A’)+(A*x’) • Now, replacing x with B+C • On = [(B+C)*A’]+[A*(B’*C’)] • On = (A*B’*C’)+[A’*(B+C)]
Simplification Exercises • Simplify the following expressions • 1. F = X+(X’*Y) • 2. F = (X+Y)+[(X*Z)+Y] • 3. F = (X’*Y’*Z’)+(X*Y*Z’)+(X*Y’*Z’) • 4. F = [X’*(Y+Z’)]’*(X+Y’+Z)*(X’*Y’*Z’)’
Exercise 1 Answer • Starting with F = X+(X’*Y) • Use the distributive law • F = (X+X’)*(X+Y) • Now, with the property of the universal class • X+X’ = 1 AND 1*(X+Y) = X+Y • So the answer is • F = X+Y
Exercise 2 Answer • Starting with F = (X+Y)+[(X*Z)+Y], • Apply the distributive law to give • F = (X+Y)+(X+Y)*(Z+Y) • Now, factor out the common X+Y term • F = (X+Y)*[1+(Z+Y)] • By the property of the universal class, 1+Z+Y=1 • so the answer is • F = X+Y
Exercise 3 Answer • Starting with F = (X’*Y’*Z’)+(X*Y*Z’)+(X*Y’*Z’) • Factor out the common Z’ term • F = Z’*(X’Y’+XY+XY’) • Factoring out the common X term • F = Z’*[X’Y’+X(Y+Y’)] = Z’*(X’Y’+X) • Now, with the distributive law • F = Z’*(X’+X)*(Y’+X) = Z’*(Y’+X)
Exercise 4 Answer • Starting with F = [X’*(Y+Z’)]’*(X+Y’+Z)*(X’*Y’*Z’)’ • First apply DeMorgan’s theorem, giving • F = (X+Y’Z)*(X+Y’+Z)*(X+Y+Z) • Factoring out the common X term gives • F = X+[Y’Z*(Y’+Z)*(Y+Z)] • Factoring out the common Z term • F = X+Y’Z*(Z+Y’Y) = X+Y’Z
Karnaugh Maps - 1 • Karnaugh maps are a graphical technique aiding simplification of boolean expressions of 6 (4?) variables or less. • Karnaugh maps are a structured way to represent all possible combinations of the variables in an expression. • Factoring is then done by associating related terms to achieve simplification.
Karnaugh Maps - 2 • Boolean expressions can be represented in minterm form. • The minterms are the products of the variables in all possible combinations. • The minterms for X and Y are: • X’*Y’, X*Y’, X’*Y, XY • There are 2**n minterms for n variables.
Karnaugh Maps - 3 • All possible values of one or more variables are represented in columns, and likewise for the rows. • Each box in the map represents a single minterm. X’Y Y X 0 1 0 1 1 • This is a two-variable Karnaugh map for the expression X’Y.
Karnaugh Maps - 4 XY’ = XY’Z+XY’Z’ X’Z YZ YZ X X 00 01 11 10 00 01 11 10 0 0 1 1 1 1 1 1 • One way of mapping any expression is to convert it to minterms, then fill in the box for each minterm. YZ XY’+X’Z X 00 01 11 10 0 1 1 1 1 1
A 4-Variable Karnaugh Map YZ WX 00 01 11 10 00 1 01 1 1 1 11 1 1 10 • 4-variable (and smaller) maps keep all boxes for equal values of a variable adjacent (considering wrap-around).
A 5 Variable Karnaugh Map XYZ VW 110 111 101 100 000 001 011 010 00 01 11 10
Simplifying Using Maps • Draw the map for the expression, first converting to minterm form if necessary. • Use factoring to extract simpler elements (next slides).
Map Factoring - 1 • When two adjacent cells both have 1s, then those cells can be factored, eliminating the variable that is different for the two cells. • For example, the 0100 and 0101 cells can be factored, eliminating the variable z. • This is shown on the following map.
Map Factoring - 2 YZ WX 00 01 11 10 00 1 01 1 1 1 11 1 1 10
Map Factoring - 3 • Similarly, when four adjacent cells have 1s, then those cells can be factored, eliminating the two variables that are different for the four cells. • For example, the 0101, 0111, 1101, and 1111 cells can be factored, eliminating the variables w and y. • This is shown on the following map.
Map Factoring - 4 YZ WX 00 01 11 10 00 1 01 1 1 1 11 1 1 10
Map Factoring - 5 • When all three of the possible factorings are made, the resulting expression is • F = w’yz + wxy’ + xz • This is shown on the following map.
Map Factoring - 6 YZ WX 00 01 11 10 00 1 01 1 1 1 11 1 1 10 W’YZ+W’XY’+XZ
Simplification Exercises • Using Karnaugh Maps, simplify the following expressions • 1. F = X+(X’*Y) • 2. F = (X+Y)+[(X*Z)+Y] • 3. F = (X’*Y’*Z’)+(X*Y*Z’)+(X*Y’*Z’) • 4. F = [X’*(Y+Z’)]’*(X+Y’+Z)*(X’*Y’*Z’)’
Exercise 1 Answer YZ The function is F = X+(X’*Y) X 00 01 11 10 0 0 0 1 1 1 1 1 1 1 YZ Factoring this gives F = X+Y X 00 01 11 10 0 0 0 1 1 1 1 1 1 1
Exercise 2 Answer YZ The function is F = (X+Y)+[(X*Z)+Y] = X+Y+XZ+Y = X+Y+XZ X 00 01 11 10 0 0 0 1 1 1 1 1 1 1 YZ Factoring this gives F = X+Y X 00 01 11 10 0 0 0 1 1 1 1 1 1 1
Exercise 3 Answer YZ The function is F = (X’*Y’*Z’)+(X*Y*Z’)+(X*Y’*Z’) = X’Y’Z’+XYZ’+XY’Z’ X 00 01 11 10 0 1 0 0 0 1 1 0 0 1 YZ Factoring this gives F = Y’Z’+XZ’ = Z’(Y’+X) X 00 01 11 10 0 1 0 0 0 1 1 0 0 1
Exercise 4 Answer - 1 X+Y’Z The function is F = [X’*(Y+Z’)]’*(X+Y’+Z)*(X’*Y’*Z’)’ = (X+Y’Z)*(X+Y’+Z)*(X+Y+Z) This can be covered in 3 maps. YZ X 00 01 11 10 0 0 1 0 0 1 1 1 1 1 X+Y’+Z X+Y+Z YZ YZ X 00 01 11 10 X 00 01 11 10 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1
Exercise 4 Answer - 2 The total function is the product of these maps, or a map with 1s where all 3 maps have 1s and 0s elsewhere. This is YZ X 00 01 11 10 0 0 1 0 0 1 1 1 1 1 Factoring this map gives the answer F = X+Y’Z YZ X 00 01 11 10 0 0 0 1 0 1 1 1 1 1
Factoring Zeros • When factoring 1s, the 1s on a map are covered. This gives the minterm form of the expression. • The same map can be factored by covering the 0s and then applying DeMorgan’s Theorem. • This gives the maxterm form of the expression, or the intersection of unions.
Example of Zeros Factoring The following map could be factored by covering 1s as follows, giving F = X+Y+Z YZ X 00 01 11 10 0 0 1 1 1 1 1 1 1 1 Covering the 0s, however, gives F’ = X’Y’Z’ Applying DeMorgan’s Theorem gives the answer F = X+Y+Z YZ X 00 01 11 10 0 0 1 1 1 1 1 1 1 1
Another Factoring Example The following map could be factored by covering 1s as follows, giving the minterm result F = XY’+X’Y+Z YZ X 00 01 11 10 0 0 1 1 1 1 1 1 1 0 Covering the 0s, however, gives F’ = X’Y’Z’+XYZ’ Applying DeMorgan’s Theorem gives the answer in maxterm form F = (X+Y+Z)*(X’+Y’+Z) = (X+Y)*(X’+Y’)+Z YZ X 00 01 11 10 0 0 1 1 1 1 1 1 1 0
Factoring Out Zeros • Sometimes, a 1s factoring will be complicated by one or two badly placed 0s. This can be handled as follows • Factor the map as if the inconvenient 0s were 1s. • Then, treating each of these factors as a small map, factor out the 0s.
Factoring Out 0s Example - 1 Except for two inconvenient 0s, this map has a very simple factoring F = Y’+W’Y = Y’+W’ YZ WX 00 01 11 10 00 1 0 1 1 Because of the 0 at W’X’Y’Z’ and the 0 at W’XYZ, however, we must factor out 0s. 01 1 1 1 0 11 0 1 1 0 10 1 0 0 1
Factoring Out 0s Example - 1 The first factor, Y’, creates the mini map in the variables W, X, and Z. On this map, W’X’Z’ is factored out: F1 = Y’*(W’X’Z’)’ = Y’*(W+X+Z) This factoring is essentially Y’ and not W’X’Z’. YZ WX 00 01 11 10 00 1 0 1 1 01 1 1 1 0 11 0 1 1 0 The second factoring is F2 = W’Y*(XZ)’ = W’Y*(X’+Z’) So the function is F = Y’*(W+X+Z)+W’Y*(X‘+Z’) 10 1 0 0 1
Factoring With More Variables • Karnaugh maps are possible with up to 6 variables. Beyond that, special tricks are needed. Or, you could use a program. • If the expression involves 6 or fewer variables in most of the terms with an occasional additional variable, an approach much like factoring out 0s can be used. • This method is called exception factoring and can be used with any number of variables.
Exception Factoring - 1 • To factor the following function on a 4-variable Karnaugh map • F = AX’Y’+WXY’+W’Y’Z+XY’Z+W’XY’Z’+WX’Y’ • 1. Construct the function, using the variables • W, X, Y, and Z. • 2. For the term with the A, instead of 1, enter an • A in that square. • 3. In factoring out zeros, treat A as A+0.
Exception Factoring - 2 Next, enter the A term. Put an A wherever there are 0s and nothing where there is a 1. First, construct the map, using only the terms containing W, X, Y, and Z. F1 = WXY’+W’Y’Z+XY’Z+W’XY’Z’+WX’Y’ F2 = AX’Y’ YZ YZ WX WX 00 01 11 10 00 01 11 10 00 00 0 0 1 0 0 A A 0 01 01 1 0 1 0 0 0 0 0 11 11 0 1 1 0 0 0 0 0 10 10 1 0 0 1 A 0 0 A F = F2 + F1
Exception Factoring - 3 First, factor out the 1s, assuming the A was a 1 F1 = Y’ Next, factor out the A term, treating it as A+0 F = Y’*[A+(W’X’Z’)’] = Y’*(A+W+X+Z) YZ WX 00 01 11 10 00 0 A 1 0 01 1 0 1 0 11 0 1 1 0 10 1 0 0 1
Other Useful Symbols • The following basic mathematical symbols are also useful in defining programming functions. • - means “is a member of the set” • - means “is not a member of the set” • - means “for all members of the set” • - means “there exists a member of the • set”
Notation Examples • C :: A means “when condition C is true, perform action A”. • To update a file when dataset D is a member of the file • (D File) :: Update • To add a number x(i), when every number i is an even integer • i when (i even) :: add x(i) • To clear a register when any number n is negative • i when (i < 0) :: Clear
Function Completeness • A set of functions is complete if it covers all possible conditions. • For a complete set of functions, the Karnaugh map for the sum (or union) of the functions covers all map positions. • There may be multiply-covered map positions.