Induction & Recursion Discrete Mathematics and Its Applications Baojian Hua

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

C and Data Structures Baojian Hua
Linked List Variations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Data Structure & Abstract Data Type
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
C Module System C and Data Structures Baojian Hua
Abstract Syntax Tree Discrete Mathematics and Its Applications Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Discrete Structures & Algorithms More on Methods of Proof / Mathematical Induction EECE 320 — UBC.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Structures C and Data Structures Baojian Hua
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Abstract Syntax Trees Compiler Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
Relation Discrete Mathematics and Its Applications Baojian Hua
Graph C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Queue C and Data Structures Baojian Hua
Lexing Discrete Mathematics and Its Applications Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Functional List C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Object-oriented Languages Compiler Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Polymorphism C and Data Structures Baojian Hua
Automata and Regular Expression Discrete Mathematics and Its Applications Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
9-1 9 Variables and lifetime  Variables and storage  Simple vs composite variables  Lifetime: global, local, heap variables  Pointers  Commands 
Abstract Syntax Trees Compiler Baojian Hua
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Generic Programming in C
CSCE 210 Data Structures and Algorithms
Dynamic Allocation Review Structure and list processing
Abstract Data Types in C
Introduction to Linked Lists
Data Structures and Algorithms
Abstract Data Types and Stacks
Linked Lists Chris Wright Winter 2006.
Summary on linked lists
Lab 04 – Linked List.
Discrete Mathematics and
Discrete Maths 11. Recursion Objective
Data Structures and Algorithms
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Structures and List Processing
Abstract Data Types Stacks CSCI 240
Abstract Data Types and Stacks
Presentation transcript:

Induction & Recursion Discrete Mathematics and Its Applications Baojian Hua

Our Goal How to write data types (math.) via inductive definitions? How to turn inductive definitions into data structures? How to write algorithms operated on these data structures? How to argue the correctness of the algorithms?

Dead-simple Example: nat // Data types: 1. A zero, or 2. a number follow another nat. // Rewrite into inductive definition form: nat -> Zero -> Succ nat // Q: how to turn this into data structures?

Tagged Union in C // Rewrite into inductive definition form: nat -> Zero -> Succ nat typedef natStruct *nat; struct natStruct { enum natKind {Zero, Succ} kind; union { nat n; } u; }; u kind

Interface #ifndef NAT_H #define NAT_H typedef natStruct *nat; struct natStruct { enum natKind {Zero, Succ} kind; union { nat n; } u; };

Interface Continued nat newNatZero (); nat newNatSucc (nat n); nat natAdd (nat n1, nat n2); int natToInt (nat n); #endif

Generate nat nat newNatZero () { nat t = (nat)malloc (sizeof (*t)); t->kind = Zero; return t; } nat newNatSucc (nat n) { nat t = (nat)malloc (sizeof (*t)); t->kind = Succ; t->u.n = n; return t; } u kind nat -> Zero -> Succ nat

Example: 3 3: Succ(Succ(Succ(Zero))); three = newNatSucc( newNatSucc( newNatZero()))); u kind u u u three

Example: 3 3: Succ(Succ(Succ(Zero))); three = newNatSucc( newNatSucc( newNatZero()))); u Zero u Succ u u three

Addition n1 ⊕ n2 = { n2, if n1==Zero; { Succ (m ⊕ n2), if n1==Succ(m). nat natAdd (nat n1, nat n2) { switch (n1->kind) { case Zero: return n2; case Succ: return newNatSucc (natAdd (n1->u.n, n2)); default: error (…); } return NULL; } u kind

toInt toInt(n) = { 0, if n==Zero; { 1+toInt(m), if n1==Succ(m). int natToInt (nat n) { switch (n->kind) { case Zero: return 0; case Succ: return 1+(natToInt(n->u.n)); default: error (…); } return -1; } u kind

Linked List // Data types: 1. An empty list, or 2. a node, followed by another list. // Rewrite into inductive definition form: list -> Empty -> Cons T, list // Q: how to turn this into data structures?

Tagged Union in C // Rewrite into inductive definition form: list -> Empty -> Cons (T, list) typedef listStruct *list; struct listStruct { enum listKind {Empty, Cons} kind; union { struct { void *data; list next; } node; } u; }; u kind

Interface #ifndef LIST_H #define LIST_H typedef listStruct *list; struct listStruct { enum listKind {Empty, Cons} kind; union { struct { void *data; list next; } node; } u; };

Interface Continued list newListEmpty (); list newListCons (void *data, list next); int length (list l); list insertHead (list l, void *data); list insertTail (list l, void *data); #endif

Generate a List list newListEmpty () { list t = (list)malloc (sizeof (*t)); t->kind = Empty; return t; } list newListCons (void *data, list l) { list t = (list)malloc (sizeof (*t)); t->kind = Cons; t->u.node.data = data; t->u.node.next = l; return t; } u kind list -> Empty -> Cons (T, list)

Length length(l) = { 0, if l==Empty; { 1+length(m), if l==Cons (T, m) int length (list l) { switch (l->kind) { case Empty: return 0; case Cons: return 1 + length(l->u.node.next); default: error (…); } return -1; } u kind

Insert at Head f(d, l) = Cons (d, l) list insertHead (void *d, list l) { return newListCons (d, l); } u kind

Insert at Tail f(d, l) = { Cons (d, l), if l==Empty; { Cons (x, f(d, m)), if l==Cons(x, m). list insertTail (void *d, list l) { switch (l->kind) { case Empty: return newListCons (d, l); case Cons: return newListCons (l->u.node.data, insertTail (d, l->u.node.next)); default: error (…); } return NULL; } u kind How can you argue this is correct?

How does it Work? u Empty 3 Cons 6 4 list insert 9 at tail u Empty 9 Cons 3 6 newList 4 Cons

Persistent Data Structures Data structures never change once created you may want to compare the list here with the ones we discussed in Lab #1 Good for many purpose reason the correctness of the code local debugging important in emerging fields such as concurrent programming or multi-core

Functional Programming Stemming from mathematics Church and Turing, etc. Programming (and the resulting code) just behaves like writing mathematical functions persistent data structures h recursive functions An important programming idioms I personally love it more than procedural, OO, generic, etc. Stepping into industry Such as F# from M … S …

Summary Inductive Def ’ == Persistent DS Deduction == Recursion Math Proof == Program Correctness

Local Class Hierarchy abstract class List {} class ListEmpty extends List {} class ListCons extends List { X data; List next; ListCons (X data, List next) {…} }

Ugly Cast class Match { public int size (List l) { if (l instanceof ListEmpty ) return 0; else if (l instanceof ListCons ) …; else …; } // Or the visitor pattern

In C++ template class List { public: virtual void foo()=0; }; template class ListEmpty: public List { public: void foo (){}; };

In C++ template class ListCons: public List { public: X data; List next; ListCons(X adata, List anext) :data(adata), next(anext) {}; void foo () {}; }