Top Banner
Polymorphic -Calculus
28

Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

May 11, 2018

Download

Documents

vutu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Polymorphic -Calculus

Page 2: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Polymorphic -Calculus

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

Page 3: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 Content

— Motivation

— Parametric Polymorphism

— The Polymorphic -Calculus

— The pure calculus

— Extensions

— High level languages

— Type Reconstruction

Page 4: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Motivation

Page 5: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Motivation (I)

— Polymorphism in almost every modern language

— Subtyping, Function Overloading

— Generic functions and data types

— Not possible in

— Need for extension arises

Page 6: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 Motivation (II)

— Jean-Yves Girard: Logician

— Seeking analogy between types and propositions

— Discovered System F in 1972

Page 7: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

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

Page 8: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 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

Page 9: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Parametric Polymorphism

Page 10: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Parametric Polymorphism (I)

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

— Doubling function in :

Page 11: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 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 }}

Page 12: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

The Polymorphic -Calculus

Page 13: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 The Polymorphic -Calculus: The pure calculus (I)

— Additions to Syntax— Terms:

— Type abstraction:

— Type application:

— Types:

— Type variable:

— Universal type:

Page 14: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 The Polymorphic -Calculus: The pure calculus (II)

— Additions to Syntax— Contexts:

— type variable binding:

— Additions to Evaluation

— (E-TAPP):

Page 15: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 The Polymorphic -Calculus: The pure calculus (III)

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

— Additions to Typing

— (T-TABS):

— (T-TAPP):

Page 16: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 The Polymorphic -Calculus: The pure calculus (IV)

— : Example(s)

— Maybe deriavtion tree

— Generically type some function out of

Page 17: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 The Polymorphic -Calculus: Extensions (I)

— Every type expressible in expressible in 2

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

— Integers, Booleans

— and so on

Page 18: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 The Polymorphic -Calculus: Extensions (II)

— Church Encodings

— Example: Church numerals

— Untyped Church numerals (foundation)

Page 19: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

 The Polymorphic -Calculus: Extensions (III)

— Typed Church numerals in 2

— Type:

Page 20: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

High level languages

Page 21: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

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

Page 22: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

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;}

Page 23: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

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; }};

Page 24: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

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;}

Page 25: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Type Reconstruction

Page 26: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Type Reconstruction (I)

— Problem

— Types annotations omitted

— Type application in function call omitted

— All annotations regarding types omitted

— (Partial) Solution

— Type reconstruction

Page 27: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Type Reconstruction (II)

— Type reconstruction for solvable

— Problem

— Not solvable for 2

— Solution

— In practice: Undeterministic algorithms

— Only fail on invalid terms (rare)

Page 28: Polymorphic -Calculuswiki.ifs.hsr.ch/SemProgAnTr/files/mmeili_presentation_first_draft.pdf · Seminar HS2015 Content — Motivation — Parametric Polymorphism — The Polymorphic

Questions