CSC302 2005F, Class 3: The Science of Programming: Boolean Logic Admin * No reading for Monday * Guy Steele on video - Helped develop Scheme; Helped design Java * Reading for Wednesday * Code is intended to mean something * Reading for next Friday * Gries, Chapter 2 * Apologies: Some of you will find this stuff easy, some of you will find it difficult * When to submit questions! (By 8 p.m.) * FAQ Overview * Background: * What is "The Science of Programming"? (SOP) * Why study SOP? * Boolean logic basics * A grammar for Boolean propositions * Reasoning with Booleans Historical background: * Early programming (pre 1980's) variable * Difficult to ensure correctness * Sometimes difficult to say "this is what I want" * Focus is still on "computation" * Dijsktra: A "discipline" of programming * To contrast with "art" * Develop and apply careful techniques * To the development of programs * To the specification of programming languages * How do/did you specify the meaning of a programming language? * Our goal: Specify enough about the meaning of the language that a programmer can write a program and have some reasonable expectation about what it will do. * Example: cons * A procedure of two parameters (x and y) that prepends x to y * Natural language (English) * Time flies like an arrow * Fruit flies like a banana * (cons x y) is like new Pair(x,y) * Demonstrate reduction of new programming language to known programming language * Try it, you'll like it! * Operationally * Define it with mathematics and the rules of logic! (car (cons x y)) = x (cdr (cons x y)) = y (pairP (cons x y)) = #true (nullP (cons x y)) = #false * Dijsktra, Gries, and company wanted to apply similar care to the definition of imperative languages Gries will tell us: * How to reason with logic * How to use logic to carefully specify the meaning of operations * How to use those meanings to more carefully design programs Why study Gries in CSC302? * Useful to see how to define the semantics (meaning) of a programming language * We think it's important that you study it somewhere, as the "discipline" of programming will make you better programmers (now in 201) 151-152-201 or 151-152-223-201 * Chapter one is about "the basics of Boolean logic" * Defining a "new" language * Syntax * Semantics * Examples of use Defining the basics of Boolean logic * Goal: Reasoning about "utterences" (propositions) that may have "truth" associated with them * Basic values in the language: true and false What are the "legal" utterences (propositions) Basic propositions * true is a proposition * false is a proposition * a variable is a proposition (e.g. x is a proposition) * Worry: How do we discuss the "truthfulness" of variables? * Foreshadowing: States Recursive propositions: Assume p1 and p2 are propositions * ( not p1 ) is a proposition * ( p1 or p2 ) is a proposition * ( p1 and p2 ) is a proposition * ( p1 implies p2 ) is a proposition * ( p1 equals p2 ) is a proposition NOTHING ELSE IS A PROPOSITION Which of these are propositions? * x - YES * x and y - NO * (x implies true) - YES * ((x) implies true) - NO Observation: More parentheses than Scheme x && y || z Language design goal: Change the syntax of the utterences so that we don't have to write so many parens * Check Gries * A grammar that formally specifies association and precedence * Association and precedence parenthesization not and or implies equals * Associativity: Given two instances of the same operation, which way do you group? * a OP b OP c * (a OP b) OP c - LEFT ASSOCIATIVE * a OP (b OP c) - RIGHT ASSOCIATIVE * And does it matter? and - NOT FOR THE TYPICAL MEANING OF AND or - NOT FOR THE TYPICAL MEANIGN OF OR implies equals How do we assign meanings to utterances so that we can answer that question? * Truth table for basic operations * Assumption of recursive evaluation for non-base subterms x y x implies y x equals y F F T T F T T F T F F F T T T T Do there exist a b and c for whic (a implies b) implies c != a implies (b implies c) Let a be false, b be false, c be true (false implies false) implies true != false implies (false implies true) true implies true != false implies true true == true