• Keine Ergebnisse gefunden

Higher-Order Types

N/A
N/A
Protected

Academic year: 2022

Aktie "Higher-Order Types"

Copied!
22
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Higher-Order Types

Klaus Ostermann University of Marburg

(2)

Motivation:

Limitations of first-order types in Scala

From “Generics of a Higher Kind” by Moors et al, 2008

(3)

Solution using higher-order types

(4)

Universes in Scala

(5)

Motivation:

Higher-Order types in Haskell

data Tree a = Leaf a | Branch (Tree a) (Tree a) class Functor f where -- f must have kind *->*

fmap :: (a -> b) -> f a -> f b instance Functor Tree where

fmap f (Leaf x) = Leaf (f x)

fmap f (Branch t1 t2) = Branch (fmap f t1) (fmap f t2) addone :: Tree Int -> Tree Int

addone t = fmap (+ 1) t

-- instance Functor Integer where kind error

(6)

Adding kinds to simply-typed LC

• Syntax

Syntax of terms and values unchanged

(7)

Evaluation

• Like in simply-typed LC, no changes

(8)

Kinding rules

This is basically a copy of the STLC “one level up”!

(9)

Typing Rules

• We need a notion of type equivalence!

• T-Eq is not syntax-directed, like the subsumption rule in subtyping

(10)

Type Equivalence

(11)

Nice, but…

• Adding kinds to STLC is not really useful.

• A program in this language can trivially be rewritten to STLC w/o kinds by just

normalizing every type expression in place.

• To gain real expressive power we need universal types, too.

• Let’s hack System F, then!

(12)

Adding kinds to System F – a.k.a. F

• Syntax of terms and values

(13)

Adding kinds to System F – a.k.a. F

• Syntax of types, contexts, kinds

(14)

Adding kinds to System F – a.k.a. F

(15)

Adding kinds to System F – a.k.a. F

(16)

Adding kinds to System F – a.k.a. F

(17)

Adding kinds to System F – a.k.a. F

(18)

Higher-Order Existentials

• F with existential types has some interesting uses

• Example: Abstract data type for pairs

want to hide choice of Pair type constructor

(19)

Higher-Order Existentials

• Example, continued

Using the Pair ADT:

(20)

Higher-Order Existentials, formally

(21)

Algorithmic Type-Checking for F

• Kinding relation is easily decidable (syntax- directed)

• T-Eq must be removed, similarly to T-Sub in the system with subtyping

• Two critical points for the now missing T-Eq rule:

First premise of T-App and T-TApp requires type to be of a specific form

In the second premise of T-App we must match two types

(22)

Algorithmic Type-Checking for F

Idea: Equivalence checking by normalization

Normalization = Reduction to normal form

In our case: Use directed variant of type equivalence relation, reduce until normal form reached

In practical languages, a slightly weaker form of

equivalence checking is used: Normalization to Weak Head Normal Form (WHNF)

A term is in WHNF if its top-level constructor is not reducible

i.e. stop if top-level constructor is not an application

Referenzen

ÄHNLICHE DOKUMENTE

True_def: True ≡ ((λ x :: bool.. Foundations of Higher-Order Logic 3.3 Conservative Extension of Theories. The nature

• Bertrand Russell found paradox in Frege’s system and proposed the Ramified Theory of Types.. • Wrote Principia Mathematica with Whitehead, an attempt at developing basic

They have also shown that the transformation to linkless normal form involves only a singly-exponential blowup in concept size and that subsumption can be carried out in linear

Similarly, there can be transitions that do not pop and/or push a symbol off/to the stack. Deterministic variants of PDAs are strictly less

B7.1 Context-free Grammars B7.2 Chomsky Normal Form B7.3 Push-Down Automata B7.4 Summary.. Gabriele R¨ oger (University of Basel) Theory of Computer Science March 29, 2021 2

B7.1 Context-free Grammars B7.2 Chomsky Normal Form B7.3 Push-Down Automata B7.4 Summary... B7.1

Context-free Grammars and ε-Rules Chomsky Normal Form Push-Down Automata Summary.. Context-free Grammars

C5.1 Context-free Grammars and ε-Rules C5.2 Chomsky Normal Form.. C5.3 Push-Down Automata