Chapter 2: Data Abstraction 2

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Semantics Static semantics Dynamic semantics attribute grammars
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
CS 171: Introduction to Computer Science II
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Eight compound procedures and higher-order procedures.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Composite Design Pattern. Motivation – Dynamic Structure.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Plt /8/ Inductive Sets of Data Programming Language Essentials 2nd edition Chapter 1.2 Recursively Specified Programs.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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:
LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE
Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types Recall inductive BNF definition of binary trees: ::= | ( ) To write programs using.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
1 Recursive Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Chapter 1: Inductive Sets of Data
Programming Languages Dan Grossman 2013
Principles of programming languages 12: Functional programming
Data Structures and Design in Java © Rick Mercer
Fundamentals of Programming II Introduction to Trees
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4
A Simple Syntax-Directed Translator
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.2
SS 2017 Software Verification Software Model Checking 2 - Parallelism
Lecture Notes: Chapter 2
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1
From Templates to Folds
Syntax versus Semantics
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Binary Tree and General Tree
Proving Properties of Recursive Functions and Data Structures
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Chapter 6 Intermediate-Code Generation
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Computer Science and Engineering
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
Scheme: Basic Functionality
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Computer Science and Engineering
Computer Science and Engineering
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Heaps and Priority Queues
Chapter 3: Environment-Passing Interpreters
List and list operations (continue).
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
Operator precedence and AST’s
6.001 SICP Interpretation Parts of an interpreter
m-Way Search Trees A m-way Search tree of degree ‘m’ can have
Chapter 20: Binary Trees.
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

Chapter 2: Data Abstraction 2 Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types Recall inductive BNF definition of binary trees: <bintree> ::= <number> | (<symbol> <bintree> <bintree>) To write programs using bintree, we'll need: constructors for different kinds of bintrees (like Java) a predicate for telling whether an object is a bintree (unlike Java, because Scheme types are dynamic) a way of telling whether bintree is leaf or interior a way of getting out the leaf or interior component

An Abstraction for Inductive Data Types Use Dr. Scheme's built-in define-datatype form: Language → Choose Language... → Essentials of P.L. (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) What is this doing?

Defines a datatype bintree, with predicate bintree? (define-datatype bintree bintree? A bintree is either a leaf node consisting of a datum, which is a number (leaf-node (datum number?)) an interior node consisting of a key that is a symbol, a left child that is a bintree, and a right child that is a bintree (interior-node (key symbol?) (left bintree?) (right bintree?)))

Creates a dataype with the following interface: (define-datatype bintree bintree? A 1-argument procedure for constructing a leaf node, with a test to make sure that the argument is a number (leaf-node (datum number?)) A 3-argument procedure for building an interior node, with a test for the first arg being a symbol and the second and third being a bintree (interior-node (key symbol?) (left bintree?) (right bintree?)))

More on define-datatype General format: (define-datatype type-name type-predicate-name {(variant-name {(field-name predicate)}*}+) More on define-datatype (define-datatype bintree bintree? (leaf-node (datum number?))

Determining variants via cases (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (datum) datum) (interior-node (key left right) (+ (leaf-sum left) (leaf-sum right)))))) (Essentially, a pattern-matcher)

Determining variants via cases Supports type-checking: > (define tree-a (interior-node 'a (leaf-node 2) (leaf-node 3))) > (leaf-sum tree-a) 5 > (leaf-sum 'foo) case: not a bintree: foo

Determining variants via cases General format: (cases type-name expression {(variant-name ({field-name}*) consequent)}* (cases bintree tree (interior-node (key left right) (+ (leaf-sum left) (leaf-sum right))))))