polymorphic -calculuswiki.ifs.hsr.ch/semprogantr/files/mmeili_presentation_first_draft.pdf ·...

28
Polymorphic -Calculus

Upload: vutu

Post on 11-May-2018

232 views

Category:

Documents


1 download

TRANSCRIPT

Polymorphic -Calculus

Polymorphic -Calculus

Mario MeiliUniversity of Applied Sciences RapperswilSupervised by Prof. Dr. Josef M. JollerSeminar HS2015

 Content

— Motivation

— Parametric Polymorphism

— The Polymorphic -Calculus

— The pure calculus

— Extensions

— High level languages

— Type Reconstruction

Motivation

Motivation (I)

— Polymorphism in almost every modern language

— Subtyping, Function Overloading

— Generic functions and data types

— Not possible in

— Need for extension arises

 Motivation (II)

— Jean-Yves Girard: Logician

— Seeking analogy between types and propositions

— Discovered System F in 1972

Motivation (III)

— John C. Reynolds: Theory of programming languages

— Extend conventional typed programming languages to permit definition of polymorphic procedures

— Polymorphic Lambda Calculus in 1974

 Motivation (IV)

It is extraordinary that essentially the same programming language was formulated by the two of us, especially since we were led to the language by entirely different motivations.

— John C. Reynolds

Parametric Polymorphism

Parametric Polymorphism (I)

— Abstraction Principle: Each piece of functionality in just one place

— Doubling function in :

 Parametric Polymorphism (II)

— Single piece of code typed generically

— Generic functions and data types

public class Main {

// generic print function private static <T> void print(T t) { System.out.println(t); }

public static void main(String[] args) { print(100); // 100 print("Para poly"); // Para poly print(true); // true print(new Object()); // java.lang.Object@15db9742 }}

The Polymorphic -Calculus

 The Polymorphic -Calculus: The pure calculus (I)

— Additions to Syntax— Terms:

— Type abstraction:

— Type application:

— Types:

— Type variable:

— Universal type:

 The Polymorphic -Calculus: The pure calculus (II)

— Additions to Syntax— Contexts:

— type variable binding:

— Additions to Evaluation

— (E-TAPP):

 The Polymorphic -Calculus: The pure calculus (III)

— Additions to Evaluation— (E-TAPPTABS): (

— Additions to Typing

— (T-TABS):

— (T-TAPP):

 The Polymorphic -Calculus: The pure calculus (IV)

— : Example(s)

— Maybe deriavtion tree

— Generically type some function out of

 The Polymorphic -Calculus: Extensions (I)

— Every type expressible in expressible in 2

— Generic containers (Lists, Arrays,...)

— Integers, Booleans

— and so on

 The Polymorphic -Calculus: Extensions (II)

— Church Encodings

— Example: Church numerals

— Untyped Church numerals (foundation)

 The Polymorphic -Calculus: Extensions (III)

— Typed Church numerals in 2

— Type:

High level languages

High level languages (I)

Haskell

-- generic doubling functiondouble :: (a -> a) -> a -> adouble f x = f (f x)

-- example function for call with intincr :: Int -> Intincr x = x + 1

-- example function for call with boolneg :: Bool -> Boolneg b = not b

High level languages (II)

C++

// generic doubling functiontemplate<typename T>T doubleFunction(T (*f)(T), T x) { return f(f(x));}

// example function for call with intint incr(int x) { return ++x;}

// example function for call with boolbool neg(bool x) { return !x;}

High level languages (III)

Java

// generic doubling methodprivate static <T> T doubleFunction(Function<T,T> f, T x) { return f.apply(f.apply(x));}

// example method for call with intprivate static Function<Integer,Integer> incr = new Function<Integer,Integer>() { public Integer apply(Integer x) { return x+1; }};

// example method for call with booleanprivate static Function<Boolean,Boolean> neg = new Function<Boolean,Boolean>() { public Boolean apply(Boolean b) { return !b; }};

High level languages (IV)

C#

// generic doubling methodstatic T doubleFunction<T>(Func<T,T> f, T x){ return f(f(x));}

// example method for call with intstatic int incr(int x){ return x + 1;}

// example method for call with boolstatic bool neg(bool b){ return !b;}

Type Reconstruction

Type Reconstruction (I)

— Problem

— Types annotations omitted

— Type application in function call omitted

— All annotations regarding types omitted

— (Partial) Solution

— Type reconstruction

Type Reconstruction (II)

— Type reconstruction for solvable

— Problem

— Not solvable for 2

— Solution

— In practice: Undeterministic algorithms

— Only fail on invalid terms (rare)

Questions