probabilistic termination by monadic affine sized typing
play

Probabilistic Termination by Monadic Affine Sized Typing Ugo dal - PowerPoint PPT Presentation

Probabilistic Termination by Monadic Affine Sized Typing Ugo dal Lago Charles Grellois FOCUS Team INRIA & University of Bologna ELICA Meeting, Bologna October 6, 2016 dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine


  1. Probabilistic Termination by Monadic Affine Sized Typing Ugo dal Lago Charles Grellois FOCUS Team – INRIA & University of Bologna ELICA Meeting, Bologna October 6, 2016 dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 1 / 26

  2. Motivations Probabilistic programming languages are important in computer science: modeling uncertainty, cryptography, machine learning, AI. . . Quantitative notion of termination: almost-sure termination (AST) AST has been studied for imperative programs in the last years. . . . . . but what about the functional probabilistic languages? We introduce a monadic, affine sized type system sound for AST. dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 2 / 26

  3. Sized types: the deterministic case Simply-typed λ -calculus is strongly normalizing (SN). No longer true with the letrec construction. . . Sized types: a decidable extension of the simple type system ensuring SN for λ -terms with letrec. See notably: Hughes-Pareto-Sabry 1996, Proving the correctness of reactive systems using sized types , Barthe-Frade-Gim´ enez-Pinto-Uustalu 2004, Type-based termination of recursive definitions . dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 3 / 26

  4. Sized types: the deterministic case � � � ∞ � � Sizes: s , r ::= i s + size comparison inducing subtyping. Notably � ∞ = ∞ . Idea: � i is 0, Nat � � i is 0 or S 0, Nat . . . Nat ∞ is any natural number. Often denoted simply Nat. The same for lists,. . . dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 4 / 26

  5. Sized types: the deterministic case � � � ∞ � � Sizes: s , r ::= i s + size comparison inducing subtyping. Notably � ∞ = ∞ . Fixpoint rule: Γ , f : Nat i → σ ⊢ M : Nat i → σ [ i / � � i ] i pos σ = M : Nat s → σ [ i / s ] Γ ⊢ letrec f Typable = ⇒ SN. Proof using reducibility candidates. Decidable type inference. dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 4 / 26

  6. Sized types: example in the deterministic case From Barthe et al. (op. cit.): Size decreases during recursive calls ⇒ SN. dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 5 / 26

  7. A probabilistic λ -calculus � � � � V V � let x = M in N � M ⊕ p N M , N , . . . ::= V � � case V of { S → W | 0 → Z } � � � � � 0 � S V � λ x . M � letrec f V , W , Z , . . . ::= x = V Formulation equivalent to λ -calculus with ⊕ p , but constrained for technical reasons (Let normal form) Restriction to base type Nat for simplicity, but can be extended to general inductive datatypes (as in sized types) dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 6 / 26

  8. A probabilistic λ -calculus: operational semantics � ( M [ x / V ]) 1 � let x = V in M → v � ( M [ x / V ]) 1 � → v ( λ x . M ) V � � �� 1 � � � � c − → c − → → v (letrec f = V ) W V [ f / (letrec f = V )] W dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 7 / 26

  9. A probabilistic λ -calculus: operational semantics � ( W V ) 1 � case S V of { S → W | 0 → Z } → v � ( Z ) 1 � case 0 of { S → W | 0 → Z } → v dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 8 / 26

  10. A probabilistic λ -calculus: operational semantics � M p , N 1 − p � M ⊕ p N → v � � � L p i � i ∈ I → v M i � � � (let x = L i in N ) p i � i ∈ I → v let x = M in N dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 9 / 26

  11. A probabilistic λ -calculus: operational semantics � � � VD M p j � j ∈ J D = + D V ∀ j ∈ J , M j → v E j j �� � D → v j ∈ J p j · E j + D V For D a distribution of terms: � �� �� � D ⇛ n [ [ D ] ] = sup n ∈ N D n v D n where ⇛ n v is → n v followed by projection on values. � M 1 � We let [ [ M ] ] = [ [ ] ]. M is AST iff � [ [ M ] ] = 1. dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 10 / 26

  12. Random walks as probabilistic terms Biased random walk: � � �� � � 0 → 0 M bias = letrec f = λ x . case x of S → λ y . f ( y ) ⊕ 2 3 ( f (S S y ))) n ¯ Unbiased random walk: � � �� � � 0 → 0 M unb = letrec f = λ x . case x of S → λ y . f ( y ) ⊕ 1 2 ( f (S S y ))) n ¯ � � [ [ M bias ] ] = [ [ M unb ] ] = 1 Capture this in a sized type system? dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 11 / 26

  13. Another term We also want to capture terms as: � � M nat = letrec f = λ x . x ⊕ 1 2 S ( f x ) 0 of semantics � � 1 1 1 2 , (S 0) 4 , (S S 0) 8 , . . . [ [ M nat ] ] = (0) summing to 1. dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 12 / 26

  14. Beyond SN terms, towards distribution types First idea: extend the sized type system with: Γ ⊢ M : σ Γ ⊢ N : σ Choice Γ ⊢ M ⊕ p N : σ and “unify” types of M and N by subtyping. Kind of product interpretation of ⊕ : we can’t capture more than SN. . . dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 13 / 26

  15. Beyond SN terms, towards distribution types First idea: extend the sized type system with: Γ ⊢ M : σ Γ ⊢ N : σ Choice Γ ⊢ M ⊕ p N : σ and “unify” types of M and N by subtyping. We can’t type M bias nor M unb in a way decreasing the size (essential for letrec): we get at best � i → Nat ∞ ⊢ λ y . f ( y ) ⊕ 2 i → Nat ∞ � � f : Nat 3 ( f (S S y ))) : Nat and can’t use a variation of the letrec rule on that. dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 13 / 26

  16. Beyond SN terms, towards distribution types We will use distribution types, built as follows: Γ | Θ ⊢ M : µ Γ | Ψ ⊢ N : ν { | µ | } = { | ν | } Choice Γ | Θ ⊕ p Ψ ⊢ M ⊕ p N : µ ⊕ p ν Now � � � � 1 � Nat i → Nat ∞ � 2 � 3 i → Nat ∞ 3 , � f : Nat ⊢ i → Nat ∞ � λ y . f ( y ) ⊕ 2 3 ( f (S S y ))) : Nat dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 14 / 26

  17. Beyond SN terms, towards distribution types We will use distribution types, built as follows: Γ | Θ ⊢ M : µ Γ | Ψ ⊢ N : ν { | µ | } = { | ν | } Choice Γ | Θ ⊕ p Ψ ⊢ M ⊕ p N : µ ⊕ p ν Similarly: � � � � 1 � Nat i → Nat ∞ � 1 � 2 i → Nat ∞ 2 , � f : Nat ⊢ i → Nat ∞ � λ y . f ( y ) ⊕ 1 2 ( f (S S y ))) : Nat dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 14 / 26

  18. Designing the fixpoint rule { | Γ | } = Nat i / ∈ Γ and i positive in ν ?????????????????????????????????????????????? � � � i → ν [ � (Nat s j → ν [ s j / i ]) p j � j ∈ J � Γ | f : ⊢ V : Nat i / i ] LetRec = V : Nat r → ν [ r / i ] Γ | ∅ ⊢ letrec f dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 15 / 26

  19. Designing the fixpoint rule { | Γ | } = Nat i / ∈ Γ and i positive in ν � j ∈ J p j ( | s j | ) < 1 � � � i → ν [ � (Nat s j → ν [ s j / i ]) p j � � j ∈ J Γ | f : ⊢ V : Nat i / i ] LetRec = V : Nat r → ν [ r / i ] Γ | ∅ ⊢ letrec f would allow to type � � �� � � 0 → 0 M bias = letrec f = λ x . case x of S → λ y . f ( y ) ⊕ 2 3 ( f (S S y ))) n ¯ dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 15 / 26

  20. Designing the fixpoint rule { | Γ | } = Nat i / ∈ Γ and i positive in ν � ) < 1 or � j ∈ J p j ( | s j | j ∈ J p j < 1 � � � � i → ν [ � (Nat s j → ν [ s j / i ]) p j � j ∈ J Γ | f : ⊢ V : Nat i / i ] LetRec = V : Nat r → ν [ r / i ] Γ | ∅ ⊢ letrec f would allow to type M nat too: � � M nat = letrec f = λ x . x ⊕ 1 2 S ( f x ) 0 dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 15 / 26

  21. Designing the fixpoint rule { | Γ | } = Nat i / ∈ Γ and i positive in ν � ) < 1 or � j ∈ J p j ( | s j | j ∈ J p j < 1 � � � i → ν [ � (Nat s j → ν [ s j / i ]) p j � � j ∈ J Γ | f : ⊢ V : Nat i / i ] LetRec = V : Nat r → ν [ r / i ] Γ | ∅ ⊢ letrec f But how to cope with � � �� � � 0 → 0 M unb = letrec f = λ x . case x of S → λ y . f ( y ) ⊕ 1 2 ( f (S S y ))) n ¯ dal Lago & Grellois (INRIA & U. Bologna) Monadic Affine Sized Typing Oct 6, 2016 15 / 26

Recommend


More recommend