Chapter 1: Inductive Sets of Data

Slides:



Advertisements
Similar presentations
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Advertisements

CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
331 Final Fall Details 3:30-5:30 Monday, December 16 ITE 229 (this room!) Comprehensive with more emphasis on material since the midterm Look at.
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Chapter 3: Formal Translation Models
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Plt /8/ Inductive Sets of Data Programming Language Essentials 2nd edition Chapter 1.2 Recursively Specified Programs.
331 Final Spring Details 6-8 pm next Monday Comprehensive with more emphasis on material since the midterm Study example finals and midterm exams.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Chapter 1: Inductive Sets of Data Recall definition of list  '() is a list.  If l is a list and a is any object, then (cons a l ) is a list More generally:
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
331 Final Fall Details 3:30-5:30 Friday December 17 th LH4 Comprehensive with more emphasis on material since the midterm Study example finals and.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Classifications LanguageGrammarAutomaton Regular, right- linear Right-linear, left-linear DFA, NFA Context-free PDA Context- sensitive LBA Recursively.
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Autumn 2012 CSE
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Functional Programming
Chapter 3 – Describing Syntax
CPSC 121: Models of Computation 2008/9 Winter Term 2
Chapter 2 :: Programming Language Syntax
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Introduction to Scheme
Chapter 2: Data Abstraction 2
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz
Scheme : variant of LISP
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Chapter 3 The Real Numbers.
Corky Cartwright January 18, 2017
Lecture 14 Grammars – Parse Trees– Normal Forms
Proving Properties of Recursive Functions and Data Structures
Language Recognition (12.4)
Relationship to Left- and Rightmost Derivations
Programming Language Syntax 2
Context-Free Grammars
The Metacircular Evaluator
Definition: Let G = (V, T, P, S) be a CFL
CS21 Decidability and Tractability
CHAPTER 2 Context-Free Languages
Finite Automata and Formal Languages
PROGRAMMING IN HASKELL
The Metacircular Evaluator
Introduction to Finite Automata
Properties of Context-Free Languages
Chapter Twelve: Context-Free Languages
CS 3304 Comparative Languages
Computer Science and Engineering
Chapter 1 Review: BNF Grammar for lists:
September 13th Grammars.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Relationship to Left- and Rightmost Derivations
Chapter 2 :: Programming Language Syntax
Language Recognition (12.4)
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Announcements Quiz 5 HW6 due October 23
Syntax vs Semantics Backus-Naur Form Extended BNF Derivations
List and list operations (continue).
Chapter 3 Describing Syntax and Semantics.
Chapter 2 :: Programming Language Syntax
Applications of Regular Closure
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of formalizing the structure.
Lisp.
Presentation transcript:

Chapter 1: Inductive Sets of Data Recall definition of list '() is a list. If l is a list and a is any object, then (cons a l) is a list More generally: Define a simplest element Define remaining elements by induction

Inductive Sets of Data Can define (natural) numbers this way: 0 is a number If i is a number, then (s i) is a number E.g., 3 is (s (s (s 0))) – “Church numerals” Russell & Whitehead (1925): try to build all of arithmethic like this (fails because of Gödel's Incompleteness theorem)

Backus-Nauer Form (BNF) Simplifies description of inductive data types <list-of-numbers> ::= 0 <list-of-numbers> ::= (<number> . <list-of-numbers>) [where (a . b) is like (cons a b)] Consists of Terminals: 0 ) . ( Non-terminals: <list-of-numbers> Productions: <list-of-numbers> ::= ()

BNF 7 5 9 11 3 6 8 Can only describe context-free structures; e.g., <bin-search-tree> ::= (<key> . <bin-search-tree> <bin-search-tree>) is inadequate for binary search trees: 7 5 9 11 3 6 8 which require a context-sensitive description

BNF: Notation Disjunction (this or that): | <number> ::= <even-number> | <odd-number> Kleene Star (zero or more): {}* <list-of-numbers> ::= ({<number>}*) Kleene Plus (one or more): {}+ <nonempty-list-of-numbers> ::= ({<number>}+

Induction What can we do with these (BNF) inductive definitions? 1) Prove theorems about data structures. E.g. given <bintree> ::= <symbol> | ( <bintree> <bintree> ) prove that a bintree always has an even # of parens

Induction Basis: A tree derived from <bintree> ::= <symbol> has zero parens. Zero is even. Inductive step: Assume that the relation holds for two bin trees b1, with 2m ≥ 0 parens, and b2, with 2n ≥ 0 parens. Using the rule <bintree> ::= (<bintree> <bintree>) we make a new bin tree b3 = (b1 b2 ) from these, which has length 2m+2n+2 = 2[m+n+1], which is even. There is no other way to make a bin tree. Hence the relation holds for any bin tree with zero or more parens, Q.E.D.

Induction What can we do with these (BNF) inductive definitions? 2) Write programs that manipulate inductive data ; count parentheses in a binary tree (define count-bt-parens (lambda (bt) (if (atom? bt) 0 ; base case (+ (count-bt-parens (car bt)) (count-bt-parens (cadr bt)) 2)))) ; inductive step

Important Points Program mirrors BNF description mirrors proof. (if (atom? bt) <bintree> ::= <symbol> Basis: A tree derived from <bintree> ::= <symbol> has zero parens.

Program mirrors BNF description mirrors proof: (+ (count-bt-parens (car bt)) (count-bt-parens (cadr bt)) 2)))) <bintree> ::= (<bintree> <bintree>) Inductive step: Assume that the relation holds for two bin trees b1, with 2m ≥ 0 parens, and b2, with 2n ≥ 0 parens....

Follow the Grammar! When defining a program based on structural induction, the structure of the program should be patterned after the structure of the data.

Another Point Program assumes we're passing it a binary tree: dynamic type-checking (vs. Java, static / compile-time type check) Easier to make type errors: > (count-bt-parens '(dont taze me bro)) car: expects argument of type <pair>; given (dont taze me bro)

Another Example ; remove first occurrence of symbol from ; list of symbols (define remove-first (lambda(s los) (if (null? los) '() (if (eqv? (car los) s) (cdr los) (cons (car los) (remove-first s (cdr los))))))) <list-of-symbols> ::= ( ) | (<symbol> . <list-of-symbols> )