Stored Programs for Routine Activities CPI 101: Meeting 7 Pat Langley

advertisement
CPI 101: Meeting 7
Stored Programs for
Routine Activities
Pat Langley
School of Computing and Informatics
Arizona State University
Tempe, Arizona USA
Routine Activities
We spend much of our time engaging in various routine activities:
 Morning activities like taking a shower and getting dressed
 Getting to the university or work and returning home
 Organizing and holding a party for your friends
 Driving or flying to visit your parents or siblings
 Having lunch or dinner at a restaurant or fast food outlet
We may value novelty, but familiar routines make our life much
simpler than it would be without them.
Familiar Automated Routines
We rely on devices for many of our routine activities, including:
 Alarm clocks or cell phones to wake up in the morning
 Microwave ovens to cook or reheat our meals
 Tivo boxes to record our favorite TV programs
 Thermostats to control air conditioners and heaters
 Washers and dryers to clean and dry our clothes
 iPods to play the songs or lectures we want to hear
These devices store and interpret routines so we do not have to
carry them out ourselves.
Stored Programs
How do we or our devices know how to carry out routine activity?
 Basic claim: We can store the steps of a routine activity as data
in memory in a way that a computer can interpret them.
This notion of a stored program is one of the basic insights that
underlies computing and informatics.
All of the applications we will consider in the course reside on
this important idea.
This concept is also central to all computational theories of human
thinking and intelligence.
Storing and Interpreting Programs
We need two things to automate routine activity on a computer:
 Encoding and storing the program in memory:
 Giving the program a name or index so we can access it.
 Specifying the program’s initial, intermediate, and final steps.
 Accessing and running the program on demand:
 Retrieving the program by its name or index.
 Passing any arguments to the program.
 Executing each of the program’s steps in turn.
 Halting the program and returning any results.
These ideas apply not only to routine behavior on silicon devices,
but also to our own everyday activities.
Obsession with Computer Programming
Because so many computing applications rely on stored programs,
traditional CS degrees begin with a course in programming.
This has been an unfortunate historical development because:
 One need not be a skilled programmer to understand or use
many computer applications.
 Most programming languages are low level and their syntax
obscures the key ideas.
 Religious disagreements have led to a plethora of alternative
programming languages.
 Many now believe that computer science equals programming.
In this course, we will convey the central ideas without getting
bogged down in the details.
Recipe for Jamaican Curry Chicken
Ingredients: 1/2 chicken, 4 stems thyme, 1 onion, 5 tbsp. curry,
2 cloves garlic, 3 tbsp. seasoning, 3 tbsp. black pepper, 1 tbsp.
chicken seasoning, 3 cubed potatoes,1 lemon,1 tbsp. vegetable
oil, 2 tbsp. butter, 2 c. water, 1/2 Scotch bonnet pepper
1. Wash chicken with lemon and cut into bite-sized pieces.
2. Season with all dry ingredients.
3. Chop all herbs and add to chicken (use hands to rub in
seasonings, and let sit in refrigerator for 1/2 hr.).
4. Place chicken, water, and oil in a pot, stir, cook on high until it
comes to a boil, stir, lower heat until chicken is almost cooked.
5. Add potatoes and butter.
6. Cook until water is reduced and potatoes are tender.
7. Serve over steamed white rice.
Sequential Action in Stored Programs
A stored program for carrying out some routine activity has:
 A name by which one can access the program.
 A set of inputs or arguments on which it operates.
 Initialization steps that get the program ready.
 An ordered list of steps that must occur in sequence.
 Halting steps that end the program and return results.
Interpreting a program involves executing accessing it, passing
arguments, and executing these steps in the obvious order.
These operations are analogous to following a recipe when one
is cooking a meal, where orders can be very important.
History of Stored Programs
The notion of stored programs has a diverse, extended history:
 Joseph-Marie Jacquard (~1800) introduces punched cards to
control a loom for weaving silk cloth.
 Herman Hollerith (~1890) uses punched cards for tabulating
data from the US Census.
 Alan Turing (1937) introduces the idea of a machine that can
store and interpret a program as data.
 William Mauchly and J. P. Eckert (~1946) design the first
stored-program digital computer.
 First commercial programmable computer (UNIVAC 1) goes
on the market (1951).
Modern computer programming builds on these innovations.
Some Stored Programs
Cookbook
Jacquard punched card
Hollerith punched card
Another Stored Program
To find Blackbeard the Pirate’s hidden treasure:
1.
2.
3.
4.
5.
Go to the dead tree in the center of the island.
Turn north and walk 200 paces.
Turn east and walk 100 paces.
Turn north and walk 50 paces.
Dig five feet down to the treasure.
This program requires one to count the number of steps taken,
resetting the counter twice in the process.
Using Variables in Stored Programs
A stored program may need to keep track of things that change
during its operation using one or more variables, which:
 Are place holders that store some form of information.
 Take some initial value at the program’s outset.
 Can change repeatedly during the program’s execution.
 May be returned by the program when it halts.
The use of variables adds considerably to the flexibility and
power of stored programs.
Hierarchical Nature of Routine Activities
Many everyday activities have an obvious hierarchical structure:
 Morning activity: get up, take a shower, clean teeth, get dressed
 Take a shower: turn on water, shampoo hair, wash body, dry off
 Clean teeth: floss teeth, brush teeth, use mouth wash
 Brush teeth: put paste on brush, clean inner, clean outer, rinse
 Put paste on brush: get brush, get paste, open paste, squeeze tube
We could get even more detailed, but this should be enough to
convince everyone that we organize activities hierarchically.
Subroutines in Stored Programs
We can produce hierarchical behavior in stored programs by:
 Replacing some steps with calls to other programs
 Invoking these programs when reaching their steps
 Passing variables as arguments to subroutines when called
 Returning results from subroutines when they are complete
This simplifies greatly construction and debugging of computer
programs because it makes them more modular and reusable.
Nearly all nontrivial programs have this hierarchical character.
More on Jamaican Curry Chicken
Note that our recipe incorporates some conditional statements:
1. Wash chicken with lemon and cut into bite-sized pieces.
2. Season with all dry ingredients.
3. Chop all herbs and add to chicken (use hands to rub in
seasonings, and let sit in refrigerator for 1/2 hr.).
4. Place chicken, water, and oil in a pot, stir, cook on high until it
comes to a boil, stir, lower heat until chicken is almost cooked.
5. Add potatoes and butter.
6. Cook until water is reduced and potatoes are tender.
7. Serve over steamed white rice.
These tests are essential to produce an edible dish that is neither
undercooked or overcooked.
Conditionals in Stored Programs
We can produce conditional behavior in stored programs by:
 Stating that an activity should continue until a test is met.
 Stating that an activity should continue while a test is met.
 Indicating that a step should happen only if a condition is met.
Such constructs simplify the construction of computer programs
because it makes them more flexible and adaptive.
Nearly all nontrivial stored programs have this conditional flavor.
Iteration in Stored Programs
Conditionals often occur within iterative/looping constructs that:
 Initialize the values of variables, including counters.
 State one or more steps that should occur on each loop
 Including incrementing/decrementing of any counters
 Indicate the conditions for halting the iteration.
Iterative statements simplify the specification of stored programs
because it handles repetitive behavior.
Nearly all nontrivial computer programs incorporate either
iteration or recursion.
Dicing an Onion Recursively
To dice a piece of onion O:
1.
2.
3.
4.
If the onion piece O is wider than 1 cm,
Then cut O into two pieces called P and Q.
Dice the piece of onion P.
Dice the piece of onion Q.
Recursion combines subroutines with conditional statements to
support very elegant stored programs.
Modern Stored Programs
Today’s computer programs incorporate a variety of features:
 Sequences of steps that should be executed
 Variables that store changing values
 Hierarchical calls to many levels of subroutines
 Tests that place conditions on some activities
 Iteration or recursion to handle repetitive activities
These are written in many different programming languages and
range from a few lines of code to millions.
What they hold in common is the idea that one can store programs
as data and interpret them to produce behavior.
Summary
The notion of a stored program is a powerful innovation that:
 Underlies all of modern computing and informatics
 But has a long history that goes back to at least 1800
 Lets us automate complex but routine activities on computers
 But also accounts for routine human behavior
 Can be implemented with standard programming languages
 But one need not master them to appreciate the key ideas
We increasingly rely on programs stored in electronic devices,
but we have always used them to carry out routine tasks.
Download