Key components of a lang. Deconstructing OCaml. In OCaml. Units of computation. In Java/Python. In OCaml. Units of computation.

Similar documents
Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

CSE 130 : Fall Programming Languages. Lecture 11: Ranjit Jhala UC San Diego. programming

CS558 Programming Languages

CMSC 330: Organization of Programming Languages

Introduction to OCaml

CS 360 Programming Languages Interpreters

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

So what does studying PL buy me?

Programming Languages, Summary CSC419; Odelia Schwartz

Recap: Functions as first-class values

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE : Fall 2017

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE 130 : Fall 2013

Programming Languages

CS457/557 Functional Languages

Functional Languages. Hwansoo Han

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Chapter 11 :: Functional Languages

Semester Review CSC 301

Topic 9: Type Checking

Topic 9: Type Checking

Types and Type Inference

CS 11 Haskell track: lecture 1

Types and Type Inference

Dynamically-typed Languages. David Miller

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Type Checking and Type Inference

Object Oriented Paradigm

Programming Languages

CSCI-GA Scripting Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

Hi! My name is Sorin. Programming Languages. Why study PL? (discussion) Why study PL? Course Goals. CSE 130 : Fall 2014

Programming Languages

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

CS558 Programming Languages

Concepts of Programming Languages

CS558 Programming Languages

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

SOFTWARE ARCHITECTURE 6. LISP

Adv. Course in Programming Languages

CMSC 330: Organization of Programming Languages

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Programming Languages

Types, Type Inference and Unification

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Functional Programming. Pure Functional Programming

CS312: Programming Languages. Lecture 21: JavaScript

New Programming Paradigms

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Programming Languages

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

CS558 Programming Languages

Com S 541. Programming Languages I

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

SCHEME AND CALCULATOR 5b

Variables and Bindings

! Broaden your language horizons. ! Study how languages are implemented. ! Study how languages are described / specified

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Weeks 6&7: Procedures and Parameter Passing

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Pierce Ch. 3, 8, 11, 15. Type Systems

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Functional Programming. Big Picture. Design of Programming Languages

Is Functional Programming (FP) for me? ACCU Conference 2008 Hubert Matthews

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Programming Languages 2nd edition Tucker and Noonan"

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

COSE212: Programming Languages. Lecture 0 Course Overview

Lecturer: William W.Y. Hsu. Programming Languages

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Semantic Analysis. Lecture 9. February 7, 2018

CSC 533: Organization of Programming Languages. Spring 2005

Programming Language Pragmatics

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Introduction to programming with Python

CSCC24 Functional Programming Scheme Part 2

301AA - Advanced Programming [AP-2017]

Lecture #23: Conversion and Type Inference

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Control in Sequential Languages

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

Transcription:

Key components of a lang Deconstructing OCaml What makes up a language Units of computation Types Memory model In OCaml Units of computation In OCaml In Java/Python Expressions that evaluate to values Everything is an expression int, bool, real if-then-else let-in match fn x -> x+1 e1 e2 1

In Java/Python In Prolog Store and update commands Message sends In Prolog Logical facts Inference rules Types Mexican(CARNITAS) Food(CARNITAS) Mexican(X) Food(X) Delicious(X) Delicious(CARNITAS) Fact Fact Fact Rule Types Used to classify things created by the programmer Classification used to check what can be done with/to those things In OCaml: Static typing Types are assigned statically at compile time Without computing values Rules state when expressions are typecorrect e1:t1 T2 e2: T1 e1 e2 : T2 2

In OCaml: Static typing How can one reuse code for different types? parametric types: a * b -> b * a implicit forall Type discovered (inferred) automatically from code less burden on the programmer In Python: Dynamic typing Types assigned to values/objects as they are computed, ie: dynamically Before an operation is performed, check that operands are compatible with operation In Python: Dynamic typing Dynamic vs. Static, OO vs. Func More programs are accepted by compiler More flexible, but find errors late Statically typed Dynamically typed [1, abc, 1.8, [ efg, 20]] OO let x = if b then 1 else abc let y = if b then x + 1 else x ^ efg Functional Dynamic vs. Static, OO vs. Func Polymorphism OO Statically typed Java Dynamically typed Python, Smalltalk Can a language be dynamically typed, but not polymorphic? Every dynamically typed language is polymorphic functions just simply work on any datatype that can be operated on at runtime Functional Ocaml, Haskell Lisp/Scheme Only need explicit polymorphism in statically typed languages to assign at compile time a suitably general polymorphic type 3

Data model in functional langs Memory/Data model Environment of bindings (phonebook) aka: what do variables refer to? Never change a binding add new bindings at the end of the phonebook Data model in functional langs Variables are names that refer into the phonebook Most recent entry looked up during evaluation Environment frozen inside function value so that the behavior of the function cannot be changed later on (easier reasoning) Data model in OO langs Variables are cells in memory Can change them by assigning into them Variables point to objects on the heap x = x + 10 Data model in Prolog Variables in Prolog are unknowns to solve for Mexican(CARNITAS) Final words on functional programming Food(CARNITAS) X Mexican(X) Food(X) Delicious(X) Delicious(Y)? Q: What is delicious? A: CARNITAS! 4

What s the point of all this? Advantages of functional progs Functional programming more concise one line of lisp can replace 20 lines of C (quote from http://www.ddj.com/dept/architect/184414500?pgno=3) Recall reverse function in OCaml: let reverse = fold (::) [];; How many lines in C, C++? Don t be fooled Some of the programming assignments made you do certain things using fold in order to force you to think about it, even though using fold was not the easiest way to do it. But there are many cases where map and fold make life A LOT EASIER. Can better reason about progs No side effects. Call a function twice with same params, produces same value As a result, computations can be reordered more easily They can also be parallelized more easily So what? From the authors of map reduce: Inspired by similar primitives in LISP and other languages http://research.google.com/archive/mapreduce-osdi04-slides/index-auto-0003.html The point is this: programmers who only know Java/C/C++ would probably not have come up with this idea Many other similar examples in industry This stuff is for real: F# F# = Microsoft s Ocaml-on-steroids http://channel9.msdn.com/pdc2008/tl11/ Why FP is way cool How FP works with Objects (C#) How FP allows you to write parallel code all with an extremely engaging speaker 5

Remember The next time you use google, think of how functional programming has inspired some of the technical ideas behind their engine And of course: Free your mind -Morpheus Recap of the course so far 4 weeks of functional with Ocaml Next: 4 weeks of OO with Python After that: 1 week of constraint logic programming with Prolog OO at the highest level What is OO programming? OO at the highest level What is OO programming? Answer: objects message sends dynamic dispatch Just to whet your appetite Say we have objects, like cars, ducks, pig, cell_phones Say we have a message name: make_some_noise Just to whet your appetite Each object has its own implementation for make_some_noise: these are traditionally called methods. car: vroom vroom, pig : oink oink, duck: quack quack We can send make_some_noise to any object. Depending on the actually run-time object, we ll get a different noise! 6

OO programming Message: the name of an operation Method: the implementation of an operation Dynamic dispatch: the act of determining at based on the dynamic type which method should be run for a given message send. These are the core ideas of OO This brings us to Python... We ll use Python as our vehicle for OO programming Fun and useful language Let s compare with OCaml along some of the dimensions we saw last time OCaml/Python comparison OCaml/Python comparison ML Python ML Python PL paradigm Basic unit Types PL paradigm functional OO/imperative Basic unit Expr/value Objects/ messages Types statically dynamicaclly DataModel DataModel env lookup pointers to mutable objs Python Python has a very relaxed philosophy if something "can be done" then it is allowed. Combination of dynamic types + everything is an object makes for very flexible, very intuitive code. No static types No static type system to "prohibit" operations. No more of that OCaml compiler giving you hard-to-decypher error messages! And... No need to formally define the type system (although still need to define the dynamic semantics somehow) 7

No static types: but what instead? Dynamic typing At runtime, every "operation" is translated to a method call on the appropriate object. If the object supports the method, then the computation proceeds. Duck-typing: if it looks like a duck, quacks like a duck, then it is a duck! Dynamic typing This loose, comfortable, free-style, philosophy is at the heart of python. But... beware, can get burned... One way to think about it: Dynamic types good for quick prototyping Static types good for large systems Although Gmail in Javascript? Similarities to Ocaml Uniform model: everything is an object, including functions Can pass functions around just as with objects Supports functional programming style with map and fold Other cool things about Python A lot of stuff that you may first think is a "language feature" is actually just translated under the hood to a method call... Very widely used, supported. Has libraries for all sorts of things. Ok, let s start playing with Python! Like Perl, python is a "managed" or "interpreted" language that runs under the python environment, i.e. not compiled to machine code. Makes it convenient to rapidly write and test code! Ways to run Python code At an interactive Python prompt: like "read-eval-print" loop of ML, As shell scripts, As stand-alone programs run from the shell. 8

Let s fire it up! Ok, let s give it a try... See py file for the rest... 9