Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.

Slides:



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

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Type Checking.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Approaches to Typing Programming Languages Robert Dewar.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
Programming Languages and Paradigms Object-Oriented Programming.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
Basic Semantics Associating meaning with language entities.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Types in programming languages1 What are types, and why do we need them?
Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CS536 Types 1. Roadmap Back from our LR Parsing Detour Name analysis – Static v dynamic – Scope Today – Type checking 2 Scanner Parser Tokens Semantic.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
UMBC CMSC 331 Java Review of objects and variables in Java.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Design issues for Object-Oriented Languages
EECE 309: Software Engineering
Polymorphism in Methods
Computer Organization and Design Pointers, Arrays and Strings in C
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Types CSCE 314 Spring 2016.
Semantic Analysis Type Checking
Lecture 4: Type Systems.
Java Primer 1: Types, Classes and Operators
Java Programming Language
Semantic Analysis with Emphasis on Name Analysis
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Type Systems Terms to learn about types: Related concepts: Type
Interfaces and Inheritance
Data Types.
Chapter 9 Inheritance and Polymorphism
Names, Binding, and Scope
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE341: Programming Languages Lecture 11 Type Inference
More Object-Oriented Programming
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
CSE341: Programming Languages Lecture 11 Type Inference
CISC/CMPE320 - Prof. McLeod
CSE341: Programming Languages Lecture 11 Type Inference
CS 242 Types John Mitchell.
CSCE 314: Programming Languages Dr. Dylan Shell
CSE 341 Lecture 11 b closures; scoping rules
Type Systems Terms to learn about types: Related concepts: Type
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Type Systems.
Presentation transcript:

Heath Carroll Bill Hanczaryk Rich Porter

 A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the idea of type polymorphism ◦ The programming language he developed, ML, was the first language to use polymorphic type inference and type-safe exception handling.

 Introduce the design of polymorphic type inference  Present three programming languages that utilize different degrees of typing ◦ ML – Bill Hanczaryk ◦ C# – Heath Carroll ◦ Python – Rich Porter

 Type Polymorphism – A feature of a program that allows different data types to be handled essentially the same way. ◦ Polymorphic Function – A function that can evaluate values of different data types. ◦ Polymorphic Data – A data type with only a generalized type.  Static Typing - type checking at compile-time.  Dynamic Typing – type checking at run-time.  Strong Typing - In order to mix types, you must use an explicit conversion.  Weak Typing - mixing types without an explicit conversion is okay.

 ML Typing ◦ static – type checking at compile time static ◦ strong –type mixing must be done explicitly strong ◦ inferred -> Next Slide inferred  Even though type inferred, basic types still exist ◦ int, real, string, bool, and char.  ML created (by Milner) to specifically utilize the Hindley-Milner type inference algorithm to automatically infer the types of expressions

 Type Inference - The ability of a programming language to automatically detect the data type of a variable.  A compiler is often able to infer the type of a variable without explicit type annotations having been given.  Why is it useful? ◦ A programmer does not have to explicitly define type annotations while still maintaining about the same level of type safety. ◦ Types often clutter programs and slow down programmer efficiency.

 Example -fun f(x) = 2+x; >val it = fn : int -> int  How does it work? ◦ + has two types: int*int -> int, real*real -> real ◦ 2 : int has only one type ◦ This implies + : int*int -> int ◦ From context, need x: int ◦ Therefore f(x:int) = 2+x has type int -> int

 Standard type checking ◦ int f(int x) { return x+1; }; ◦ int g(int y) { return f(y+1)*2;};  Type inference ◦ f(x) { return x+1; }; ◦ g(y) { return f(y+1)*2;};  Look at code without type information and figure out what types could have been declared.

 According to the Wikipedia C# is : ◦ static – compile time type checking static ◦ dynamic – runtime type checking dynamic ◦ strong – any type mixing must be done explicitly strong ◦ safe – type mismatches throw errors, find a way to work (ie adding an int to a string in javascript) safe ◦ nominative - two variables are type-compatible if and only if their declarations name the same type nominative Basically Java using C syntax, boxing, and garbage collection, but without global variables.

 How on earth can a language be both at the same time? Don’t they contradict each other? ◦ Sort of. It’s just redundant to type check at both compile time and runtime. ◦ But C# isn’t fully dynamic, it’s dynamic only when you’d want it to be, literally. C# has a new static type: Namely the dynamic type.

 dynamic d1 = new Foo(); dynamic d2 = new Bar(); string s;  d1.M(s, d2, 3, null);  Q: What does the compiler do when it comes across this function M()?

 A: Nothing! It allows the program to figure it out at runtime. (ok, so maybe it does something…)  The compiler sorts the function call to M(), so that at runtime M() can be invoked with the arguments passed in.  Which it does as follows:

1. Reflection is used to obtain the actual runtime types of the two objects, d1 and d2, that did not have a static type (or rather had the static type dynamic). The result is Foo for d1 and Bar for d2. Reflection - provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them.Type

 2. Method lookup and overload resolution is performed on the type Foo with the call M(string,Bar,3,null) using ordinary C# semantics.  Overload resolution – The Dynamic Runtime Library is responsible for figuring out which method to actually use if more than one is found. ◦ Betterness rules apply.

 3. If the method is found it is invoked; otherwise a runtime exception is thrown

 M(string s, int i = 1); M(object o); M(int i, string s = “Hello”); M(int i); Given the functions above are all applicable, which one or ones are most suitable and which one gets the call?

 Given these overloads, we can see the working of the rules above. M(string,int) is not applicable because 5 doesn’t convert to string. M(int,string) is applicable because its second parameter is optional, and so, obviously are M(object) and M(int).  M(int,string) and M(int) are both better than M(object) because the conversion from 5 to int is better than the conversion from 5 to object.  Finally M(int) is better than M(int,string) because no optional arguments are omitted.

 Strong ◦ No implicit type conversions  Duck ◦ “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck”  Dynamic ◦ No initialization/declaration ◦ Type “determined” at run time

 Cannot add int to string ◦ ‘a’ + 3 → error ◦ ‘a’ + ‘3’ → ‘a3’  Cannot add string to list ◦ ‘at’ + [‘bad’,‘cat’] → error ◦ [‘at’] + [‘bad’, ‘cat’] → [‘at’,‘bad’,‘cat’]  Interesting ◦ 3*3 → 9 ◦ ‘3’*3 → ‘333’ ◦ ‘3’*’3’ → error

 Not concerned with type inference  Only interested in the aspects that are used rather than the object itself.

 Values carry type not variables

 Type is determined at run time

 Normal type inference is not necessary with duck typing  Python Type Inference Applications: ◦ Optimized Interpreter ◦ Compile Python → Another Language (Perl, C, …)  Further Reading ◦ “Psyco”Psyco ◦ “Type Inference for Python”Type Inference for Python ◦ “Aggressive Type Inference”Aggressive Type Inference