Advances in Type Systems for Functional Logic Programming Enrique - - PowerPoint PPT Presentation

advances in type systems for functional logic programming
SMART_READER_LITE
LIVE PREVIEW

Advances in Type Systems for Functional Logic Programming Enrique - - PowerPoint PPT Presentation

Advances in Type Systems for Functional Logic Programming Advances in Type Systems for Functional Logic Programming Enrique Mart n Mart n Proyecto Fin de M aster en Programaci on y Tecnolog a Software M aster en


slide-1
SLIDE 1

Advances in Type Systems for Functional Logic Programming

Advances in Type Systems for Functional Logic Programming

Enrique Mart´ ın Mart´ ın

Proyecto Fin de M´ aster en Programaci´

  • n y Tecnolog´

ıa Software M´ aster en Investigaci´

  • n en Inform´

atica Facultad de Inform´ atica UCM

9 de Julio 2009

slide-2
SLIDE 2

Advances in Type Systems for Functional Logic Programming Introduction

This work

We propose a type system tackling two orthogonal aspects:

1) A naive application of a Damas & Milner type system in a language with HO patterns may produce problems from the point of view of types [GHR01].

2) There are various grades of polymorphism for bound variables in let expressions.

slide-3
SLIDE 3

Advances in Type Systems for Functional Logic Programming Introduction

Motivation and informal description

slide-4
SLIDE 4

Advances in Type Systems for Functional Logic Programming Introduction

HO patterns

Higher order pattern: pattern with partial application of constructors or functions

Examples: id, map id, snd X, and true Example ([GHR01] & Curry mailing list) snd :: A → B → B unpack :: (A → A) → B snd X Y → Y unpack (snd X) → X cast :: A → B and :: bool → bool → bool cast X → unpack (snd X) and true X → X and false X → false

slide-5
SLIDE 5

Advances in Type Systems for Functional Logic Programming Introduction

HO patterns (II)

The expression and (cast 0) true : bool is well-typed, since (cast 0) : bool. If we apply the rules of cast and unpack we obtain: and (cast 0) true cast − → and (unpack (snd 0)) true

unpack

− → and 0 true and 0 true is ill-typed ⇓ Well-typed programs can go wrong!

slide-6
SLIDE 6

Advances in Type Systems for Functional Logic Programming Introduction

HO patterns (III) Where is the problem?

In function unpack. Knowing the type of its pattern (snd X) does not provide any information about the type of its subpattern X. Damas & Milner type systems treat this “opacity” as polymorphism, and this is the reason why they infer a type (A → A) → B for unpack.

slide-7
SLIDE 7

Advances in Type Systems for Functional Logic Programming Introduction

HO patterns (IV) How to solve this situation?

a) Solution of [GHR01]: opaque patterns are forbidden. t is

  • paque iff it contains f t1 . . . tn s.t. f : τn → τ and

FTV (τn) FTV (τ). Examples: With snd : A → B → B

slide-8
SLIDE 8

Advances in Type Systems for Functional Logic Programming Introduction

HO patterns (IV) How to solve this situation?

a) Solution of [GHR01]: opaque patterns are forbidden. t is

  • paque iff it contains f t1 . . . tn s.t. f : τn → τ and

FTV (τn) FTV (τ). Examples: With snd : A → B → B snd true ✘

slide-9
SLIDE 9

Advances in Type Systems for Functional Logic Programming Introduction

HO patterns (IV) How to solve this situation?

a) Solution of [GHR01]: opaque patterns are forbidden. t is

  • paque iff it contains f t1 . . . tn s.t. f : τn → τ and

FTV (τn) FTV (τ). Examples: With snd : A → B → B snd true ✘ snd zero ✘

slide-10
SLIDE 10

Advances in Type Systems for Functional Logic Programming Introduction

HO patterns (IV) How to solve this situation?

a) Solution of [GHR01]: opaque patterns are forbidden. t is

  • paque iff it contains f t1 . . . tn s.t. f : τn → τ and

FTV (τn) FTV (τ). Examples: With snd : A → B → B snd true ✘ snd zero ✘ snd X

slide-11
SLIDE 11

Advances in Type Systems for Functional Logic Programming Introduction

HO patterns (V)

b) Our solution: Making a distinction between transparent and

  • paque variables. A variable of a pattern is transparent if its

type is univocally fixed by the type of the pattern, and

  • paque otherwise.

We reject only opaque variables when they appear in the rest

  • f the expression, i.e., when they are critical.

In the example, X is opaque in (snd X) and appears in the right-hand side of the rule for unpack, so the program will be rejected. However, patterns like snd true or snd zero have not any

  • paque variable, so they would be accepted.
slide-12
SLIDE 12

Advances in Type Systems for Functional Logic Programming Introduction

Another generalization for free

Another generalization for free We allow opaque data constructors, i.e., constructors with type τn → τ s.t. FTV (τn) FTV (τ).

Example: cont : α → container

These data constructors are not allowed in existing FP or FLP systems. In our framework we treat them the same way as HO patterns.

slide-13
SLIDE 13

Advances in Type Systems for Functional Logic Programming Introduction

Opaque data constructors are useful

data container = cont A % cont : A → container code :: container -> string code (cont false) = "000" code (cont true) = "001" code (cont zero) = "010" code (cont (succ X)) = "011" ++ code (cont X) This program is not legal in [GHR01] or FLP systems.

slide-14
SLIDE 14

Advances in Type Systems for Functional Logic Programming Introduction

Our second contribution: polymorphism of let expressions

There exist different grades of polymorphism for variables in let expressions. Different implementations have different grades of polymorphism. They do not usually document their choice. We explicitly formalize 3 kinds of let expressions let t = e1 in e2 in our type system.

slide-15
SLIDE 15

Advances in Type Systems for Functional Logic Programming Introduction

Monomorphic lets

letm t = e1 in e2 All the variables in t have a monomorphic type. Systems: Clean 2.0, T OY 2.3.11, PAKCS 1.9.1, KICS 0.81893. Examples: let F = id in (F 0, F 4) ✔ let F = id in (F true, F 0) ✘ let [F, G] = [id, id] in (F true, F 0, G 0, G false) ✘

1monomorphic where expressions

slide-16
SLIDE 16

Advances in Type Systems for Functional Logic Programming Introduction

Polymorphic lets

letp t = e1 in e2 All the variables in the pattern have a polymorphic type. Systems: Hugs Sept. 2006, Ocaml 3.10.2, F# Sept. 2008. Examples: let F = id in (F 0, F 4) ✔ let F = id in (F true, F 0) ✔ let [F, G] = [id, id] in (F true, F 0, G 0, G false) ✔

slide-17
SLIDE 17

Advances in Type Systems for Functional Logic Programming Introduction

“Mixed” lets

letpm t = e1 in e2 The polymorphism depends on the form of the pattern: a) If the pattern is a single variable, it is polymorphic. b) If the pattern is compound, the variables are monomorphic. Systems: GHC 6.8.2, Standard ML of New Jersey 110.67, Curry M¨ unster 0.9.11. Examples: let F = id in (F 0, F 4) ✔ let F = id in (F true, F 0) ✔ let [F, G] = [id, id] in (F true, F 0, G 0, G false) ✘

slide-18
SLIDE 18

Advances in Type Systems for Functional Logic Programming Type System

Formal aspects

slide-19
SLIDE 19

Advances in Type Systems for Functional Logic Programming Type System

Type system

We have divided the type system into two parts: a) A basic typing relation ⊢ to give a type to an expression. b) An extended typing relation ⊢• that uses the previous and also checks the absence of critical variables.

slide-20
SLIDE 20

Advances in Type Systems for Functional Logic Programming Type System

Basic typing relation ⊢ A ⊢ e : τ

A - set of assumptions over symbols : {si : σi} e - expression τ - simple type

slide-21
SLIDE 21

Advances in Type Systems for Functional Logic Programming Type System

Basic typing relation ⊢: rules (I)

[ID] A ⊢ s : τ if s ∈ DC ∪ FS ∪ DV ∧ (s : σ) ∈ A ∧ σ ≻ τ [APP] A ⊢ e1 : τ1 → τ A ⊢ e2 : τ1 A ⊢ e1e2 : τ [Λ] A ⊕ {Xi : τi} ⊢ t : τt A ⊕ {Xi : τi} ⊢ e : τ A ⊢ λt.e : τt → τ if {Xi} = var(t)

slide-22
SLIDE 22

Advances in Type Systems for Functional Logic Programming Type System

Basic typing relation ⊢: rules (II)

[LETm] A ⊕ {Xi : τi} ⊢ t : τt A ⊢ e1 : τt A ⊕ {Xi : τi} ⊢ e2 : τ2 A ⊢ letm t = e1 in e2 : τ2 if {Xi} = var(t) [LETp] A ⊕ {Xi : τi} ⊢ t : τt A ⊢ e1 : τt A ⊕ {Xi : Gen(τi, A)} ⊢ e2 : τ2 A ⊢ letp t = e1 in e2 : τ2 if {Xi} = var(t)

Gen(τ, A) is the closure or generalization of τ wrt. A Formally: Gen(τ, A) = ∀αi.τ where {αi} = FTV (τ) FTV (A).

slide-23
SLIDE 23

Advances in Type Systems for Functional Logic Programming Type System

Basic typing relation ⊢: rules (III)

[LETX

pm]

A ⊢ e1 : τ1 A ⊕ {X : Gen(τ1, A)} ⊢ e2 : τ2 A ⊢ letpm X = e1 in e2 : τ2 [LETh

pm]

A ⊕ {Xi : τi} ⊢ h t1 . . . tn : τt A ⊢ e1 : τt A ⊕ {Xi : τi} ⊢ e2 : τ2 A ⊢ letpm h t1 . . . tn = e1 in e2 : τ2 if {Xi} = var(t1 . . . tn) ∧ h ∈ DC ∪ FS

slide-24
SLIDE 24

Advances in Type Systems for Functional Logic Programming Type System

Opaque and critical variables

Opaque variable “A variable of a pattern is opaque if its type is not univocally fixed by the type of the pattern.” Critical variable “A variable is critical if it is opaque in a pattern of a lambda or let expression and appears in the rest of the expression.” Examples:

X is opaque snd X X is not opaque in snd [X, true] X is critical in λ(snd X).X X is not critical in λ(snd X).true

slide-25
SLIDE 25

Advances in Type Systems for Functional Logic Programming Type System

Extended typing relation ⊢• A ⊢• e : τ

A - set of assumptions over symbols e - expression τ - simple type Forbids only expressions with critical variables.

[P] A ⊢ e : τ A ⊢• e : τ if critV arA(e) = ∅

slide-26
SLIDE 26

Advances in Type Systems for Functional Logic Programming Type System

Subject reduction

Well-typed program (wtA(P)). P is well-typed wrt. A iff for every rule f t1 . . . tn → e ∈ P:

a) A ⊢• λt1 . . . tn.e : τ b) τ is a variant of A(f)

Let-rewriting semantics [LRS08] Theorem (Subject reduction) If A ⊢• e : τ and wtA(P) and P ⊢ e →l e′ then A ⊢• e′ : τ.

slide-27
SLIDE 27

Advances in Type Systems for Functional Logic Programming Type Inference

Type inference for expressions A e : τ|π A • e : τ|π

A - set of assumptions e - expression τ - most general type for e π - minimum type substitution which is needed to apply to A in

  • rder to derive a type for e

Intuition: a) wherever type derivation guesses types, type inference introduces fresh type variables. b) wherever type derivation forces equality of types, type inference computes a mgu. c) infer a variant for symbols.

slide-28
SLIDE 28

Advances in Type Systems for Functional Logic Programming Type Inference

Properties of the type inference for expressions

Theorem (Soundness of type inference) A e : τ|π = ⇒ Aπ ⊢ e : τ A • e : τ|π = ⇒ Aπ ⊢• e : τ Theorem (Completeness of wrt. ⊢) If Aπ′ ⊢ e : τ ′ then ∃τ, π, π′′. A e : τ|π ∧ Aππ′′ = Aπ′ ∧ τπ′′ = τ ′. Theorem (Maximality of •) a) •Πe

A ≡ {π ∈ T Subst | ∃τ ∈ SType. Aπ ⊢• e : τ} has a

maximum element ⇐ ⇒ ∃τg ∈ SType, πg ∈ T Subst. A • e : τg|πg. b) If Aπ′ ⊢• e : τ ′ and A • e : τ|π then exists a type substitution π′′ such that Aπ′ = Aππ′′ and τ ′ = τπ′′.

slide-29
SLIDE 29

Advances in Type Systems for Functional Logic Programming Type Inference

Type inference for Programs

Block inference. Procedure B to infer types for programs without explicit type declarations. B(A, {rule1, . . . , rulem}) = π Theorem (Soundness of B) If B(A, P) = π then wtAπ(P). Theorem (Maximality of B) If wtAπ′(P) and B(A, P) = π then ∃π′′ such that Aπ′ = Aππ′′. Stratified inference. Divides the program in mutually dependent blocks, infer types and generalizes.

slide-30
SLIDE 30

Advances in Type Systems for Functional Logic Programming Conclusions and Future Work

Conclusions (I)

We propose a type system tackling the type problem of HO patterns and the variety of polymorphism for let expressions. A paper containing the results of this work was presented in the 18th International Workshop on Functional and (Constraint) Logic Programming (WFLP) 2009, held in Brasilia on June 28; and it has been accepted to appear in the associated LNCS volume. We have a prototype of the stratified type inference for programs ready to be integrated in the T OY compiler.

slide-31
SLIDE 31

Advances in Type Systems for Functional Logic Programming Conclusions and Future Work

Conclusions (II)

When compared to [GHR01] we make the following contributions: More liberality in the treatment of opaque patterns. Consideration of local pattern bindings and λ-abstractions. Formalization of different possibilities for polymorphism in local bindings. Subject reduction is proved wrt. a small-step operational semantics (let-rewriting), closer to real computations. Algorithms for inferring types for programs without explicit type declarations.

slide-32
SLIDE 32

Advances in Type Systems for Functional Logic Programming Conclusions and Future Work

Future Work

Integrate the type inference for programs into the T OY compiler. Generalize the subject reduction property to narrowing, using let-narrowing reductions. Study the possible relation between opacity and existential types or GADTs. Extend the type system to support type classes and extra variables in the right-hand sides.

slide-33
SLIDE 33

Advances in Type Systems for Functional Logic Programming Conclusions and Future Work

End

slide-34
SLIDE 34

Advances in Type Systems for Functional Logic Programming

J.C. Gonz´ alez-Moreno, T. Hortal´ a-Gonz´ alez, and Rodr´ ıguez-Artalejo, M. Polymorphic types in functional logic programming. In Journal of Functional and Logic Programming, volume 2001/S01, pages 1–71, 2001. Special issue of selected papers contributed to the International Symposium on Functional and Logic Programming (FLOPS’99). F.J. L´

  • pez-Fraguas, J. Rodr´

ıguez-Hortal´ a, and

  • J. S´

anchez-Hern´ andez. Rewriting and call-time choice: the HO case. In Proc. 9th International Symposium on Functional and Logic Programming (FLOPS’08), volume 4989 of LNCS, pages 147–162. Springer, 2008.