Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Similar documents
Chapter 15 Functional Programming Languages

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Aspect-Oriented Programming with Type Classes

Moving from CS 61A Scheme to CS 61B Java

Habanero Extreme Scale Software Research Project

Chapter 7: Functional Programming Languages

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Functional Programming

CSE 307: Principles of Programming Languages

The C Programming Language course syllabus associate level

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Introduction to Python

Semester Review. CSC 301, Fall 2015

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Semantic Analysis: Types and Type Checking

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

PostgreSQL Functions By Example

The Needle Programming Language

Chapter 5 Names, Bindings, Type Checking, and Scopes

Lecture 12: Abstract data types

Type Classes with Functional Dependencies

opalang - Rapid & Secure Web Development

Announcements FORTRAN ALGOL COBOL. Simula & Smalltalk. Programming Languages

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

McGraw-Hill The McGraw-Hill Companies, Inc.,

Class 16: Function Parameters and Polymorphism

CSCI 3136 Principles of Programming Languages

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Getting Started with the Internet Communications Engine

Object Oriented Software Design

Fundamentals of Programming Languages

MongoDB. Or how I learned to stop worrying and love the database. Mathias Stearn. N*SQL Berlin October 22th, gen

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

AP Computer Science Java Subset

History OOP languages Year Language 1967 Simula Smalltalk

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Sources: On the Web: Slides will be available on:

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Computing Concepts with Java Essentials

Stack Allocation. Run-Time Data Structures. Static Structures

Example of a Java program

CS 111 Classes I 1. Software Organization View to this point:

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Outline Basic concepts of Python language

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Rigorous Software Development CSCI-GA

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Machine Learning and Data Mining. Regression Problem. (adapted from) Prof. Alexander Ihler

Idris, a General Purpose Dependently Typed Programming Language: Design and Implementation

Poor Man's Type Classes

Visual Basic Programming. An Introduction

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Web Programming Step by Step

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Databases 2011 The Relational Model and SQL

Special Directions for this Test

Anatomy of Programming Languages. William R. Cook

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

Lecture 11: Tail Recursion; Continuations

Intermediate Code. Intermediate Code Generation

Compiler Construction

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Chapter 5 Functions. Introducing Functions

Attacking MongoDB. Firstov Mihail

dictionary find definition word definition book index find relevant pages term list of page numbers

Informatica e Sistemi in Tempo Reale

Compiler Construction

Introduction to Java

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Lecture 9. Semantic Analysis Scoping and Symbol Table

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Chapter 2: Elements of Java

Java Programming Fundamentals

COS 301 Programming Languages

The Smalltalk Programming Language. Beatrice Åkerblom

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

CSE 130 Programming Language Principles & Paradigms

Timber: A Programming Language for Real-Time Embedded Systems

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Extending Tizen Native Framework with Node.js

CS170 Lab 11 Abstract Data Types & Objects

MAT 2170: Laboratory 3

Pemrograman Dasar. Basic Elements Of Java

Single Page Web App Generator (SPWAG)

CS 106 Introduction to Computer Science I

The gradual typing approach to mixing static and dynamic typing

Transcription:

Programming Language Rankings Lecture 15: Type Inference, polymorphism & Type Classes CSC 131 Fall, 2014 Kim Bruce Top Combined Tiobe Index 1. JavaScript (+1) 2. Java (-1) 3. PHP 4. C# (+2) 5. Python (-1) 6. C++ (+1) 7. Ruby (-2) 8. C 9. Objective-C 10.CSS (new) 11. Perl 12. Shell (-2) 13. Scala (-1) 14. Haskell 15. R (1) 16. Matlab (+3) 17. Clojure (+5) 18. CoffeeScript (-1) 19. Visual Basic (+1) 20. Groovy (-2) Scala #41 Haskell #44 ML #25 Go #38 Dart #42 Scheme #48

Restrictions on ML/Haskell Polymorphism Type (a b) ([a] [b]) stands for: - a. b. (a b) ([a] [b]) Haskell functions may not take polymorphic arguments. E.g., no type: - b. (( a.(a a)) (b b)) - define: foo f (x,y) = (f x, f y) ; - id z = z - foo id (7, True) -- gives type error - Type of foo is only (t -> s) -> (t, t) -> (s, s) Restrictions on Implicit Polymorphism Polymorphic types can be defined at top level or in let clauses, but can t be used as arguments of functions id x = x in (id "ab", id 17) OK, but can't write test g = (g ab, g 17) Can t find type of test w/unification. More general type inference is undecidable. Explicit Polymorphism Explicit Polymorphism Easy to type w/ explicit polymorphism: test (g: forall t.t -> t) = (g ab, g 17) in test (\ t => \ (x:t) -> x) Languages w/explicit polymorphism: Clu, Ada, C++, Eiffel, Java 5, C#, Scala, Grace Clu, Ada, C++, Java C++ macro expanded at link time rather than compile time. Java compiles away polymorphism, but checks it statically. Better implementations keep track of type parameters.

Summary Modern tendency: strengthen typing & avoid implicit holes, but leave explicit escapes Push errors closer to compile time by: - Require over-specification of types - Distinguishing between different uses of same type - Mandate constructs that eliminate type holes - Minimizing or eliminating explicit pointers Holy grail: Provide type safety, increase flexibility Polymorphism vs Overloading Parametric polymorphism - Single algorithm may be given many types - Type variable may be replaced by any type Examples: hd, tail ::[t]->t, map::(a->b)->[a]->[b] Overloading - A single symbol may refer to more than one algorithm. - Each algorithm may have different type. - Choice of algorithm determined by type context. + has types Int Int Int and Float Float Float, but not t t t for arbitrary t. Why Overloading? Overloading Arithmetic Many useful functions not parametric - List membership requires equality member: [w] -> w -> Bool (for good w) - Sorting requires ordering sort: [w] -> [w] (for w supporting <,>,...) What are problems in supporting it in a PL? - Static type inference makes it hard - Why are Haskell type classes a solution? First try: allow fcns w/overloaded ops to define multiple functions - square x = x * x versions for Int -> Int and Float -> Float - But then squares (x,y,z) = (square x, square y, square z)... has 8 different versions - Too complex to support

ML & Overloading Equality Functions like +, * can be overloaded, but not functions defined from them 3 * 3 3.14 * 3.14 -- legal -- legal square x = x * x -- Int -> Int square 3 -- legal square 3.14 -- illegal - To get other functions, must include type: squaref (x:float) = x * x -- float -> float Equality worse - Only defined for types not containing functions, files, or abstract types -- why? - Again restrict functions using == ML ended up defining eq types, with special mark on type variables. - member: a -> [ a] -> Bool - Can t apply to list of functions Type Classes Proposed for Haskell in 1989. Provide concise types to describe overloaded functions -- avoiding exponential blow-up Allow users to define functions using overloaded operations: +, *, <, etc. Allow users to declare new overloaded functions. - Generalize ML s eqtypes - Fit within type inference framework Recall... Definition of quicksort & partition: partition lthan (pivot, []) = ([],[]) partition lthan (pivot, first : others) = let (smalls, bigs) = partition lthan (pivot, others) in if (lthan first pivot) then (first:smalls, bigs) else (smalls, first:bigs) Allowed partition to be parametric - Steal this idea to pass overloaded functions - Implicitly pass argument with any overloaded functions needed

Example Using Dictionaries Recall class Order a where (<) :: a -> a -> Bool (>) :: a -> a -> Bool... Implement w/dictionary: data OrdDict a = MkOrdDict (a -> a -> Bool) (a -> a -> Bool)... getlt (MkOrdDict lt gt...) = lt getgt (MkOrdDict lt gt) = gt partition dict (pivot, []) = ([],[]) partition dict (pivot, first : others) = let (smalls, bigs) = partition dict (pivot, others) in if ((getlt dict) first pivot) then (first:smalls, bigs) else (smalls, first:bigs) partition:: OrdDict a -> [a] -> ([a].[a]) Compiler adds dictionary parameter to all calls of partition. Reports type of partition (w/out lthan parameter) as (Ord a) => [a] -> ([a].[a]) Instances Implementation Summary Compiler translates each function using an overloaded symbol into function with extra parameter: the dictionary. Declaration - instance Show TrafficLight where show Red = "Red light" show Yellow = "Yellow light" show Green = "Green light" - Creates dictionary for show References to overloaded symbols are rewritten by the compiler to lookup the symbol in the dictionary. The compiler converts each type class declaration into a dictionary type declaration and a set of selector functions. The compiler converts each instance declaration into a dictionary of the appropriate type. The compiler rewrites calls to overloaded functions to pass a dictionary. It uses the static, qualified type of the function to select the dictionary.

Multiple Dictionaries Compositionality Example: - squares :: (Num a, Num b, Num c) => (a, b, c) -> (a, b, c) - squares(x,y,z) = (square x, square y, square z) goes to: - squares (da,db,dc) (x, y, z) = (square da x, square db y, square dc z) Build compounds from simpler - class Eq a where (==) :: a -> a -> Bool - instance Eq Int where (==) = inteq -- where inteq primitive equality - instance (Eq a, Eq b) => Eq(a,b) where (u,v) == (x,y) = (u == x) && (v == y) - instance Eq a => Eq [a] where (==) [] [] = True (==) (x:xs) (y:ys) = x==y && xs == ys (==) = False Subclasses What about Literals? frominteger in Num class makes it possible Example: - class (Eq a) => Num a where (+) :: a -> a -> a... other arith ops frominteger :: Integer -> a instance of Num a must be Eq a dictionary for Eq is part of that for Num - data Complex a = MkCmpx a a deriving Eq - instance Show a => Show (Complex a) where show (MkCmpx rv iv) = (show rv)++" + "++ (show iv)++"i" - instance Num a => Num (Complex a) where (MkCmpx r1 i1) + (MkCmpx r2 i2) = MkCmpx (r1+r2) (i1+i2)... frominteger n = MkCmpx (frominteger n) 0 - frominteger will be called implicitly when needed