One-Slide Summary Inheritance and Godel's Proof • Inheritance allows a subclass to share behavior (methods and instance variables) with a superclass. • A class hierarchy shows how subclasses inherit from superclasses. Typically a single ultimate class, such as object , lies at the top of a class hierarchy. • An axiomatic system provides a way to reason mechanically about formal notions. An incomplete system fails to prove some true statements. An inconsistent system proves some false statements. • Any interesting logical system is incomplete: there is a true statement that cannot be proved in it. #2 Outline Implementing list-map in Python • Inheritance def scheme_map (f,p): • PS6 if not p: • Mechanical Reasoning return [] • Axiomatic Systems else: • Paradoxes return [f(p[0])] + scheme_map(f,p[1:]) • Gödel • This “literal” translation is not a good way to do things. #3 #4 Pythonic Mapping def mlist_map (f, p): for i in range(0, len(p)): p[i] = f(p[i]) return p Inheritance • Unlike the previous one, this mutates p. • Python has a built-in map. #5 #6
Hey, Scooby! Subclasses class Dog: def __init__(self,n): class TalkingDog (Dog): self.name = n def speak(self,stuff): def bark(self): print stuff print “wuff wuff wuff wuff” ClassDefinition ::= class SubClassName class TalkingDog (Dog): ( SuperClassName ) : FunctionDefinitions def speak(self,stuff): • TalkingDog is a subclass of Dog. print stuff • Dog is the superclass of TalkingDog. – Every TalkingDog is also a Dog. >>> scooby = TalkingDog(“Scooby”) – (But not vice-versa.) >>> scooby.speak(“Scooby Snack!”) Scooby Snack! #7 #8 Every Dog Has class Dog: Speaking About Inheritance def __init__(self,n): Its Day self.name = n def bark(self): • Inheritance is using the definition print “wuff wuff wuff wuff” class TalkingDog (Dog): of one class to define another Dog def speak(self,stuff): class. print stuff • TalkingDog inherits from Dog. >>> ginger = Dog(“Ginger”) • TalkingDog is a subclass of Dog. >>> scooby = TalkingDog(“Scooby”) TalkingDog >>> scooby.speak(“Scooby Snack!”) • The superclass of TalkingDog is Scooby Snack! Dog. >>> ginger.speak(“Blah blah blah”) • These all mean the same thing! AttributeError: Dog instance has no attribute 'speak' >>> scooby.bark() wuff wuff wuff wuff #9 #10 Problem Set 6 PS6 Classes SimObject • Make an adventure game by programming with objects. PhysicalObject Place • Many objects in our game have similar MobileObject properties and behaviors, so we use OwnableObject Person inheritance. Student PoliceOfficer #11 #12
Object-Oriented Terminology Python Dictionaries • An object is an entity that packages state and procedures. • The dictionary abstraction provides a lookup • The state variables that are part of an object table. are called instance variables . • Each entry is a pair: • The procedures that are part of an object are < key , value > called methods . • The key must be an immutable object. The • We invoke (call) a method. The object is the value can be anything. first parameter (self). • dictionary [ key ] evaluates to the value • Inheritance allows one class to refine and associated with key reuse the behavior of another. – Running time is approximately constant! • A constructor is a procedure that creates new – (e.g., “associative array” or “hash table”) objects (e.g., __init__). #13 #14 Dictionary Example Pencil & Paper: Histograms • Define a procedure histogram that takes a >>> d = {} # new empty dictionary text string as input. The procedure returns a >>> d['UVA'] = 1818 # make new entry dictionary in which each word in the input >>> d['UVA'] = 1819 # update entry string is mapped to the number of times it occurs in that string. >>> d['Cambridge'] = 1209 • Hints: >>> d['UVA'] – Iterate over each word, putting it in a dictionary. 1819 If you haven't seen it before, its count is 1. >>> d['Oxford'] Otherwise, increment its count. KeyError: 'Oxford' >>> 'here we go'.split() ['here', 'we', 'go'] #15 #16 Histogram Example Author Fingerprinting • [...] a comparison of phrases used in The Reign of King def histogram(text): Edward III with Shakespeare’s early works proves conclusively >>> d = that the Bard wrote the play in collaboration with Thomas d = {} histogram(declaration) Kyd, one of the most popular playwrights of his day. [...] He words = text.split() discovered that playwrights often use the same patterns of >>> show_dictionary(d) speech, meaning that they have a linguistic fingerprint. The for w in words: of: 79 program identifies phrases of three words or more in an the: 76 if w in d: author’s known work and searches for them in unattributed to: 64 plays. In tests where authors are known to be different, there d[w] = d[w] + 1 are up to 20 matches because some phrases are in common and: 55 usage. When Edward III was tested against Shakespeare’s else: ... works published before 1596 there were 200 matches. d[w] = 1 – Jack Malvern, “Computer program proves Shakespeare didn't work alone, researchers claim”, The Times of return d London , 12 Oct 2009 #17 #18
Liberal Arts Trivia: Physics Liberal Arts Trivia: Chemistry • Name the vector quantity in physics measured • Give the common name for hydragyrum, a in radians per second. The direction of the heavy metal element. It is the only element vector is perpendicular to the plane of that is liquid at standard temperature and rotation and is usually specified by the “right pressure and is often used in the construction hand rule”. of sphygmomanometers. In the 18 th to 19 th centuries it was used to make felt hats, and the psychological symptoms associated with its poisoning are sometimes used to explain the phrase “mad as a hatter”. • Bonus: What does a sphygmomanometer measure? #19 #20 Charge Story So Far • Start PS6 early • Much of the course so far: – PS6 is challenging – Getting comfortable with recursive definitions – Opportunity for creativity – Learning to write a program to do (almost) • Start thinking about PS9 Project ideas anything (PS1-4) – Learning more elegant ways of programming – If you want to do an “extra ambitious” project (PS5-6) convince me your idea is worthy before (ps7 and 8) or (ps8) • This Week: – Discuss ideas and look for partners on the – Getting un - comfortable with recursive forum definitions – Understanding why there are some things no program can do! #21 #22 Mechanical Reasoning Computer Science/Mathematics Aristotle (~350BC): Organon • Computer Science (Imperative Codify logical deduction with rules of Knowledge) inference (syllogisms) – Are there (well-defined) problems that cannot be solved by any procedure? Every A is a P X is an A Premises Today • Mathematics (Declarative Knowledge) X is a P Conclusion – Are there true conjectures that cannot Every human is mortal. Gödel is human. be the shown using any proof? Gödel is mortal. #23 #24
More Mechanical Reasoning Mechanical Reasoning • Euclid (~300BC): Elements • Late 1800s – many mathematicians working on codifying “laws of reasoning” – We can reduce geometry to a few axioms and derive the rest by following rules – George Boole, Laws of Thought • Newton (1687): Philosophiæ Naturalis – Augustus De Morgan Principia Mathematica • Whitehead and Russell, 1911-1913 – We can reduce the motion of objects – Principia Mathematica (including planets) to following axioms (laws) – Attempted to formalize all mathematical mechanically knowledge about numbers and sets #25 #26 Perfect Axiomatic System Derives all true statements, and no false All true statements statements starting from a finite number of axioms about numbers and following mechanical inference rules. #27 #28 Incomplete Axiomatic System Inconsistent Axiomatic System incomplete Derives Derives all true some, but not all true statements, and some false statements, and no false statements starting from a statements starting from a finite number of axioms finite number of axioms and following mechanical and following mechanical inference rules. inference rules. also derives some false statements #29 #30
Recommend
More recommend