advances in type systems for functional logic programming
play

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


  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´ on y Tecnolog´ ıa Software M´ aster en Investigaci´ on en Inform´ atica Facultad de Inform´ atica UCM 9 de Julio 2009

  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.

  3. Advances in Type Systems for Functional Logic Programming Introduction Motivation and informal description

  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

  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 unpack − → and ( unpack ( snd 0)) true − → and 0 true and 0 true is ill-typed ⇓ Well-typed programs can go wrong!

  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 .

  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 opaque iff it contains f t 1 . . . t n s.t. f : τ n → τ and FTV ( τ n ) � FTV ( τ ) . Examples: With snd : A → B → B

  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 opaque iff it contains f t 1 . . . t n s.t. f : τ n → τ and FTV ( τ n ) � FTV ( τ ) . Examples: With snd : A → B → B snd true ✘

  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 opaque iff it contains f t 1 . . . t n s.t. f : τ n → τ and FTV ( τ n ) � FTV ( τ ) . Examples: With snd : A → B → B snd true ✘ snd zero ✘

  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 opaque iff it contains f t 1 . . . t n s.t. f : τ n → τ and FTV ( τ n ) � FTV ( τ ) . Examples: With snd : A → B → B snd true ✘ snd zero ✘ ✘ snd X

  11. Advances in Type Systems for Functional Logic Programming Introduction HO patterns (V) b) Our solution: Making a distinction between transparent and opaque variables. A variable of a pattern is transparent if its type is univocally fixed by the type of the pattern, and opaque otherwise. We reject only opaque variables when they appear in the rest of 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 opaque variable, so they would be accepted.

  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.

  13. Advances in Type Systems for Functional Logic Programming Introduction Opaque data constructors are useful % cont : A → container data container = cont A 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.

  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 = e 1 in e 2 in our type system.

  15. Advances in Type Systems for Functional Logic Programming Introduction Monomorphic lets let m t = e 1 in e 2 All the variables in t have a monomorphic type. Systems: Clean 2.0, T OY 2.3.1 1 , 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 ) ✘ 1 monomorphic where expressions

  16. Advances in Type Systems for Functional Logic Programming Introduction Polymorphic lets let p t = e 1 in e 2 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 ) ✔

  17. Advances in Type Systems for Functional Logic Programming Introduction “Mixed” lets let pm t = e 1 in e 2 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 ) ✘

  18. Advances in Type Systems for Functional Logic Programming Type System Formal aspects

  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.

  20. Advances in Type Systems for Functional Logic Programming Type System Basic typing relation ⊢ A ⊢ e : τ A - set of assumptions over symbols : { s i : σ i } e - expression τ - simple type

  21. Advances in Type Systems for Functional Logic Programming Type System Basic typing relation ⊢ : rules (I) if s ∈ DC ∪ FS ∪ DV [ID] ∧ ( s : σ ) ∈ A ∧ σ ≻ τ A ⊢ s : τ A ⊢ e 1 : τ 1 → τ A ⊢ e 2 : τ 1 [APP] A ⊢ e 1 e 2 : τ A ⊕ { X i : τ i } ⊢ t : τ t [ Λ ] A ⊕ { X i : τ i } ⊢ e : τ if { X i } = var ( t ) A ⊢ λt.e : τ t → τ

  22. Advances in Type Systems for Functional Logic Programming Type System Basic typing relation ⊢ : rules (II) A ⊕ { X i : τ i } ⊢ t : τ t A ⊢ e 1 : τ t [LET m ] if { X i } = var ( t ) A ⊕ { X i : τ i } ⊢ e 2 : τ 2 A ⊢ let m t = e 1 in e 2 : τ 2 A ⊕ { X i : τ i } ⊢ t : τ t A ⊢ e 1 : τ t [LET p ] if { X i } = var ( t ) A ⊕ { X i : Gen ( τ i , A ) } ⊢ e 2 : τ 2 A ⊢ let p t = e 1 in e 2 : τ 2 Gen ( τ, A ) is the closure or generalization of τ wrt. A Formally: Gen ( τ, A ) = ∀ α i .τ where { α i } = FTV ( τ ) � FTV ( A ) .

  23. Advances in Type Systems for Functional Logic Programming Type System Basic typing relation ⊢ : rules (III) A ⊢ e 1 : τ 1 [LET X A ⊕ { X : Gen ( τ 1 , A ) } ⊢ e 2 : τ 2 pm ] A ⊢ let pm X = e 1 in e 2 : τ 2 A ⊕ { X i : τ i } ⊢ h t 1 . . . t n : τ t A ⊢ e 1 : τ t if { X i } = var ( t 1 . . . t n ) [LET h pm ] A ⊕ { X i : τ i } ⊢ e 2 : τ 2 ∧ h ∈ DC ∪ FS A ⊢ let pm h t 1 . . . t n = e 1 in e 2 : τ 2

  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 critical in λ ( snd X ) .X X is not opaque in snd [ X, true ] X is not critical in λ ( snd X ) .true

  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 . A ⊢ e : τ [P] if critV ar A ( e ) = ∅ A ⊢ • e : τ

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend