5 october 2001ifip wg 2.31 preparation start drscheme set language level to beginner open in...
TRANSCRIPT
5 October 2001 IFIP WG 2.3 1
preparation
• start drscheme • set language level to Beginner• open in Presentations
– length1.ss– planets0.ss– lambda.ss– convert.ss
• add teachpack convert.ss, then throw out (easy to find)
5 October 2001 IFIP WG 2.3 2
Why do “They” Still Teach SchemeWhen Haskell is So Much Better?
Matthias Felleisen (PLT)Northeastern University
Rice University
Houston, Texas
5 October 2001 IFIP WG 2.3 3
What does the question mean?
• Programmers in industry?
• College students in senior year? – need buzz words/current technology
• College students in first year? – need systematic design skills
5 October 2001 IFIP WG 2.3 4
Haskell vs Scheme: Two Different Universes
• conventional syntax
• type system– partitions
• call-by-need
• controlled effects
• parenthesized, prefix
• unitype system – predicates
• call-by-value
• full power of effects
5 October 2001 IFIP WG 2.3 5
Haskell and Scheme: One Approach to Teaching
• layered languages
• higher-order functions
• design via classes of values
• emphasize composition over effects
views of PLT not necessarily the general Scheme community
5 October 2001 IFIP WG 2.3 6
Haskell loves Scheme, Scheme loves Haskell :-)
The Commonalties outweigh the differences. So what’s the true question?
5 October 2001 IFIP WG 2.3 7
Why do “They” Still Teach Scheme or Haskell When Java and C# are So
Fashionable?
5 October 2001 IFIP WG 2.3 8
Outline
• answer both questions
• PLT’s TeachScheme! perspective national and international efforts to expose college and high school teachers to program design
• “TeachHaskell”?
5 October 2001 IFIP WG 2.3 9
PLT’s TeachScheme! Program
• Context: introductory programming at college and high school level
• introduce two important ideas: – program design – model(s) of computation
• write significant code– reaches non-majors as well as majors
5 October 2001 IFIP WG 2.3 10
PLT’s TeachScheme! Program
• Scheme: simple syntax, simple semantics
• DrScheme: a PDE for beginners
• “How to Design Programs” (MIT, 2001) – the structure and organization of programs
(systematic program design)
5 October 2001 IFIP WG 2.3 11
Part I: The Programming Language
5 October 2001 IFIP WG 2.3 12
Programming Language: Scheme
• Scheme’s notation is simple: – (, operation, operands, )
• Scheme’s semantics is easy: – it’s just the rules of mathematics:
• 1+1 = 2
• f(12) = 12 * 12 + 25 = …
• With Scheme, we can focus on ideas
5 October 2001 IFIP WG 2.3 13
Programming Language: Scheme Again
• simple syntax
• simple semantics
• powerful PE
• rich language
• it’s a lie!
• more lies!
• do you believe this?
• so where are the GUIs?
5 October 2001 IFIP WG 2.3 14
length1 returns 0, no matter what input
algebraic syntax
5 October 2001 IFIP WG 2.3 15
more algebraic syntax
an error message concerning procedures,whatever those things are
5 October 2001 IFIP WG 2.3 16
Syntax is a Problem
• simple notational mistakes produce strange results -- without warning
• simple notational mistakes produce error messages beyond the students’ knowledge
• even in Scheme there are just too many features for focus on design
5 October 2001 IFIP WG 2.3 17
Programming Languages: Not One, Many
• language 1: first-order functional PL– functions and structures– simple conditional expressions
• language 2: higher-order functional PL– local function definitions– higher-order functions
• language 3: functions and effects– set! and structure mutation
5 October 2001 IFIP WG 2.3 18
Beginning Student Scheme
(define (f x) (+ (* 5 (square x)) (* 3 x) 27))
(define-struct dollar (amount))
(define-struct planet (name picture))
;; sign : RealNumber Symbol (define (sign x) (cond [(> x 0) ‘+] [(= x 0) ‘=] [(< x 0) ‘–])))
5 October 2001 IFIP WG 2.3 19
Intermediate Student Scheme
;; h : Num (listof Num) (listof Num)(define (h D alon) (local ((define (h-aux l) (cond [(empty? l) empty] [else (cons (+ (first s) D) (h-aux (rest s)))]))) (h-aux alon)))
;; d/dx : (Num Num) (Num Num)(define (d/dx f) (local ((define (f-prime x) (/ (- (f (+ x eps)) (f (- x eps))) 2 eps)) f-prime))
5 October 2001 IFIP WG 2.3 20
Advanced Student Scheme
(define counter 0)
(define (bump) (set! bump (+ bump 1)))
(define (zero-all boxes) (cond [(empty? boxes) (void)] [else (begin (set-box! (first boxes) 0) (zero-all (rest boxes)))]))
5 October 2001 IFIP WG 2.3 21
Lesson on Programming Languages
• arrange programming language in pedagogic layers
• … put students into a knowledge-appropriate context
• … focus on design ideas relative to this context
5 October 2001 IFIP WG 2.3 22
Part II: The Programming Environment
5 October 2001 IFIP WG 2.3 23
The Programming Environment
• one PE for all languages – supplemental code that does not
conform to the language level
• interactive– ensure model-view separation (MVC)
• exploration of– program structure– program evaluation
5 October 2001 IFIP WG 2.3 24
DrScheme: Demo
• language level: catching simple errors• pictures as values • lexical structure, alpha renaming • evaluation as algebraic reduction • teachpacks: model-view separation
– more in a moment …
5 October 2001 IFIP WG 2.3 25
Part III: Program Design Methods
5 October 2001 IFIP WG 2.3 26
Program Design for Beginners
• foster basic good habits in beginners
• emphasize systematic design of programs
• deemphasize input/output
5 October 2001 IFIP WG 2.3 27
Design Recipes
to be designedin out
How do we wire the “program” to the rest of the world?
5 October 2001 IFIP WG 2.3 28
Design Recipes
to be designedin out
• read-from-console/compute/print• file I/o• graphical user interface (GUI)• common gateway interface (CGI)• etc.
5 October 2001 IFIP WG 2.3 29
Design Recipes
to be designedin out
How do we wire the “program” to the rest of the world?
IMPERATIVE: Teach Model-View Separation
5 October 2001 IFIP WG 2.3 30
Design Recipes
• understand classes of data – representation of (external) information as
data in your favorite language
• understand “program” as function – that is triggered by events– that consumes/produces data
• most important: connect class definition and function definition
5 October 2001 IFIP WG 2.3 31
The Basic Design Recipe
• data analysis and class definition
• contract, purpose statement, header
• in-out (effect) examples
• function template
• function definition
• testing, test suite development
5 October 2001 IFIP WG 2.3 32
From Class Definitions to Function Templates
• the structure class definitions
• the structure of function templates
5 October 2001 IFIP WG 2.3 33
Design Recipes: Class Definitions
• use rigorous language, not formalism
• naïve set theory– basic sets: numbers, chars, booleans– intervals– (labeled) products, that is, structures – (tagged) unions– self-references– mutual references – vectors (natural numbers)
5 October 2001 IFIP WG 2.3 34
Design Recipes: Class Definitions (2)
(define-struct spider (name size legs))
A spider is a structure:
(make-spider symbol number number)
5 October 2001 IFIP WG 2.3 35
Design Recipes: Class Definitions (3)
A zoo animal is either • a spider• an elephant • a giraffe • a mouse • …
Each of these classes of animals has its own definition
5 October 2001 IFIP WG 2.3 36
Design Recipes: Class Definitions (4)
A list of zoo animals is either • empty• (cons animal a-list-of-zoo-animals)
Let’s make examples: • empty (by definition)• (cons (make-spider ‘Asterix 1 6) empty)• (cons (make-spider ‘Obelix 99 6) (cons … …))
5 October 2001 IFIP WG 2.3 37
Design Recipes: Class Definitions (5)
(define-struct child (name father mother))
A family tree is either • ‘unknown • (make-child symbol a-family-tree a-family-tree-2)
Many, if not most, interesting class definitions are self-referential.
5 October 2001 IFIP WG 2.3 38
Design Recipes: Function Templates (Structure)
• a function template reflects the structure of the class definitions
• this match helps – designers guide the process– readers comprehend– teachers diagnose weaknesses– modifiers/maintainers analyze or change
5 October 2001 IFIP WG 2.3 39
Design Recipes: Templates (2)
• is it a basic class?
• is it a union?
• is it a structure?
• is it self-referential?
• “domain knowledge”
• case analysis
• extract field values
• annotate for recursion
5 October 2001 IFIP WG 2.3 40
Design Recipes: Templates (3)
A list of zoo animals is either • empty• (cons animal a-list-of-zoo-animals)
;; fun-for-zoo : list-of-zoo-animals -> ???(define (fun-for-zoo a-loZA) … )
is it a union?
5 October 2001 IFIP WG 2.3 41
Design Recipes: Templates (4)
A list of zoo animals is either • empty• (cons animal a-list-of-zoo-animals)
;; fun-for-zoo : list-of-zoo-animals -> ???(define (fun-for-zoo a-loZA) (cond [ <<condition>> <<answer>> ] [ <<condition>> <<answer>> ]))
what are the sub-classes
5 October 2001 IFIP WG 2.3 42
Design Recipes: Templates (5)
A list of zoo animals is either • empty• (cons animal a-list-of-zoo-animals)
;; fun-for-zoo : list-of-zoo-animals -> ???(define (fun-for-zoo a-loZA) (cond [ (empty? a-loZA) <<answer>> ] [ (cons? a-loZA) <<answer>> ]))
are any of the potentialinputs structures?
5 October 2001 IFIP WG 2.3 43
Design Recipes: Templates (6)
A list of zoo animals is either • empty• (cons animal a-list-of-zoo-animals)
;; fun-for-zoo : list-of-zoo-animals -> ???(define (fun-for-zoo a-loZA) (cond [ (empty? a-loZA) … ] [ (cons? a-loZA) … (first a-loZA) … … (rest a-loZA) … ]))
is the class definition self-referential?
5 October 2001 IFIP WG 2.3 44
Design Recipes: Templates (7)
A list of zoo animals is either • empty• (cons animal a-list-of-zoo-animals)
;; fun-for-zoo : list-of-zoo-animals -> ???(define (fun-for-zoo a-loZA) (cond [ (empty? a-loZA) … ] [ (cons? a-loZA) … (first a-loZA) … … (rest a-loZA) … ]))
5 October 2001 IFIP WG 2.3 45
Design Recipes: Defining Functions
• templates remind beginners of all the information that is available– which cases– which field values, argument values – which natural recursions are computed
• the goal of function definitions is– to compute with the available values – to combine the computed effects
5 October 2001 IFIP WG 2.3 46
Design Recipes: Overview
• basic data, intervals of numbers • structures • unions• self-reference in class description • mutual references • generative recursion• special attributes:
– accumulators– effects
• abstraction of designs
5 October 2001 IFIP WG 2.3 47
Design Recipes: Conclusion
• get students used to discipline from DAY ONE
• use scripted question-and-answer game until they realize they can do it on their own
• works well as long as class definitions are “standard”
5 October 2001 IFIP WG 2.3 48
Part IV: From Scheme to Java
Training OO Programmers
5 October 2001 IFIP WG 2.3 49
Scheme to Java: OO Computing
• focus: objects and method invocation
• basic operations:– creation– select field – mutate field
• select method via “polymorphism”
• structures and functions
• basic operations:– creation– select field – mutate field – recognize kind
• f(o) becomes o.f()
5 October 2001 IFIP WG 2.3 50
Scheme to Java: OO Programming
• develop class and interface hierarchy
• allocate code of function to proper subclass
• develop class definitions
• allocate code of function to proper cond-clause
5 October 2001 IFIP WG 2.3 51
Scheme to Java: Class Hierarchy
A list of zoo animals is either • empty• (cons animal a-list-of-zoo-animals)
List of zoo animals
Empty Cons: animal list of zoo animals
5 October 2001 IFIP WG 2.3 52
Scheme to Java: Code Allocation
;; fun-for-zoo : list-of-zoo-animals -> ???(define (fun-for-zoo a-loZA) (cond [ (empty? a-loZA) ] [ (cons? a-loZA) … (first a-loZA) … … (rest a-loZA) … ]))
List of zoo animals
Empty:
Cons: animal list of zoo animals
5 October 2001 IFIP WG 2.3 53
Scheme to Java: Ketchup & Caviar
abstract class List_Zoo_Animal { int fun_for_list(); }
class Cons extends List_Zoo_Animal { Zoo_Animal first; List_Zoo_Animal rest;
int fun_for_list() { return 1 + rest.fun_for_list(); }}
class Empty extends List_Zoo_Animal { int fun_for_list() { return 0; }}
5 October 2001 IFIP WG 2.3 54
Scheme to Java: Ketchup & Caviar
abstract class List_Zoo_Animal { int fun_for_list(); }
class Cons extends List_Zoo_Animal { Zoo_Animal first; List_Zoo_Animal rest;
int fun_for_list() { return 1 + rest.fun_for_list(); }}
class Empty extends List_Zoo_Animal { int fun_for_list() { return 0; }}
5 October 2001 IFIP WG 2.3 55
Scheme to Java
• the design recipes work step for step for the production of OO programs
• the differences are notational
• the differences are instructive
5 October 2001 IFIP WG 2.3 56
Why not just Java first?
• complex notation, complex mistakes
• no PE supports stratified Java
• design recipes drown in syntax
5 October 2001 IFIP WG 2.3 57
Part V: Experiences
5 October 2001 IFIP WG 2.3 58
Experiences: Rice Constraints
• life-long learners
• accommodate industry long-time
• enable students to gain industry experience after two semesters
• no trends, no fashion
• oo programming components
• until recently: – C++
• now more and more:– Java, true OOP
5 October 2001 IFIP WG 2.3 59
Experiences: The Rice Experiment
beginners: no experience, up to three years of experience
comp sci introduction:• TeachScheme curriculum• good evaluation• huge growth • many different teachers
applied comp introduction:• C/C++ curriculum• weak evaluations• little growth •several teachers
second semester: OOP, classical data structures, patterns
5 October 2001 IFIP WG 2.3 60
Experiences: The Rice Experiment
• Faculty with preferences for C/C++ state that students from the Scheme introduction perform better on exams and projects in second course than students from the C/C++ introduction
• Students with lots of experiences eventually understand how much the course adds to their basis
5 October 2001 IFIP WG 2.3 61
Experiences: Secondary Schools
• trained around 200 teachers/professors
• 120+ deployed the curriculum, reuse it
• better basis for second courses
• much higher retention rate– especially among females
5 October 2001 IFIP WG 2.3 62
Part V: Teach Haskell ?
5 October 2001 IFIP WG 2.3 63
Social Theorem 1: Beginners make mistakes.
Corollary: Teaching efforts must help beginners and teachers find and help mistakes in designs, notation, and evaluations.
5 October 2001 IFIP WG 2.3 64
Social Theorem 2: To beginners, it’s about two things: the computer and the human.
Corollary: Adding intermediate layers doesn’t help.
5 October 2001 IFIP WG 2.3 65
Haskell’s Key Features
• conventional syntax
• types
• lazy data
• controlled effects
• see theorem 1
• see theorem 2
• yet another recipe
• too difficult, interesting
5 October 2001 IFIP WG 2.3 66
Working with Haskell (1)
queens_kill :: Int -> [Int] -> Bool
-- determine whether a queen can be placed in file lcon, -- places the preceding files of the boardqueesn_kill locn places = helper places 1 where helper [] _ = False helper (hd:tl) offset = lcon == hd || -- same file lcon == hd + offset || -- right diagonal lcon == hd - offset || -- left diagonal helper tl offset + 1
5 October 2001 IFIP WG 2.3 67
Working with Haskell (2)
Reading file: `nq.hs`: Type checkingERROR: `nq.hs` (line 6): Bool is not an instance of class `Num`
line 6: helper [] _ = False
5 October 2001 IFIP WG 2.3 68
Working with Haskell (3)
queens_kill :: Int -> [Int] -> Bool
-- determine whether a queen can be placed in file lcon, -- places the preceding files of the boardqueesn_kill locn places = helper places 1 where helper [] _ = false helper (hd:tl) offset = lcon == hd || -- same file lcon == hd + offset || -- right diagonal lcon == hd - offset || -- left diagonal helper tl offset + 1
5 October 2001 IFIP WG 2.3 69
Working with Haskell (3)
queens_kill :: Int -> [Int] -> Bool
-- determine whether a queen can be placed in file lcon, -- places the preceding files of the boardqueesn_kill locn places = helper places 1 where helper [] _ = false helper (hd:tl) offset = lcon == hd || -- same file lcon == hd + offset || -- right diagonal lcon == hd - offset || -- left diagonal helper tl (offset + 1)
5 October 2001 IFIP WG 2.3 70
Keys to TeachScheme!’s Success: on to Haskell
• tower of languages
• pde support for (1)
• other pde support
• design recipes– emphasis on classes
of values
• tower of types• tower of notation • pde support for both
• easy
• adapt for lazy data• adapt for controlled
effects
5 October 2001 IFIP WG 2.3 71
Conclusions
5 October 2001 IFIP WG 2.3 72
Conclusion
• training good programmers does not mean starting them on “currently fashionable” languages
• provide a strong, rigorous foundation in data-oriented, class-oriented thinking
• then, and only then, expose to fashion
5 October 2001 IFIP WG 2.3 73
Conclusion
• students wish to intern during summer
• OOP languages are here to stay
• constraints on introductory courses: – students likely to have some experience– students need to see some OOP language– non-standard students check out “CS”
5 October 2001 IFIP WG 2.3 74
Conclusion
• teach Scheme for the first semester
• teach an OOP language for second
• teaches students: – about classes of values– rigorous program development – interests non-standard students
5 October 2001 IFIP WG 2.3 75
My Ideal Scenario
• TeachScheme!: for speedy start
• (O)CAML: for types (and classes)
• Haskell: for reasoning about effects and lazy data
• Java/C#: for classes and “real world”
• Felleisen
• Harper
• Hudak
• Steele
5 October 2001 IFIP WG 2.3 76
The End
5 October 2001 IFIP WG 2.3 77
Credits
• Findler• Flanagan• Flatt• Krishnamurthi
• Cartwright• Clements• Friedman• Steckler