using typings as types
play

Using Typings as Types Casper Bach Poulsen TU Delft, The - PowerPoint PPT Presentation

Using Typings as Types Casper Bach Poulsen TU Delft, The Netherlands Peter Mosses, Neil Sculthorpe Swansea University, UK NWPT 2015, Reykjavk, Iceland, October 2015 1 Computations and values computation states 2 Computations and values


  1. Using Typings as Types Casper Bach Poulsen TU Delft, The Netherlands Peter Mosses, Neil Sculthorpe Swansea University, UK NWPT 2015, Reykjavík, Iceland, October 2015 1

  2. Computations and values computation states 2

  3. Computations and values computation states values 2

  4. Computations and values computation states values 2

  5. Computations and values computation states abstraction (procedural) body 2

  6. Computations and values computation states abstraction (procedural) body 2

  7. An example of dynamic scope ( λ f. ( λ y. f 1) 2) ( λ x. x + y ) let f = ( λ x. x + y ) in let y = 2 in f 1 ( { y : int } ` ( int ! int )) int 3

  8. Static scope M, N ::= n | x | ( M + N ) | ( λ x.M ) | ( M N ) σ , τ ::= int | t | ( σ → τ ) A x [ { x : σ } ` M : τ A ` M : ( σ ! τ ) A ` N : σ A ` ( λ x.M ) : ( σ ! τ ) A ` ( M N ) : τ A ` M : τ A ✓ A 0 A 0 ` M : τ 4

  9. Dynamic scope M, N ::= n | x | ( M + N ) | ( λ x.M ) | ( M N ) σ , τ ::= int | t | ( σ ! τ ) | ( A ` τ ) | ( σ ^ τ ) A ` M : τ ( A ( x )= σ ) ; ` ( λ x.M ) : ( A x ` ( σ ! τ )) 5

  10. Dynamic scope M, N ::= n | x | ( M + N ) | ( λ x.M ) | ( M N ) σ , τ ::= int | t | ( σ ! τ ) | ( A ` τ ) | ( σ ^ τ ) A x [ { x : σ } ` M : τ ; ` ( λ x.M ) : ( A x ` ( σ ! τ )) A ` M : ( A 0 ` ( σ ! τ )) A ` N : σ ( A ^ A 0 ) ` ( M N ) : τ A ` M : σ ( A ` σ ) < : ( A 0 ` τ ) A 0 ` M : τ 5

  11. Explicit closures M ::= · · · | close ( M ) A ` M : ( A 0 ` ( σ ! τ )) ( A ^ A 0 ) ` close ( M ) : ( ; ` ( σ ! τ )) A x [ { x : σ } ` M : τ ; ` ( λ x.M ) : ( A x ` ( σ ! τ )) ; ^ A x ` close ( λ x.M ) : ( ; ` ( σ ! τ )) A ` M : ( ; ` ( σ ! τ )) A ` N : σ ( A ^ ; ) ` ( M N ) : τ 6

  12. Some subtyping rules σ 0 < : σ τ < : τ 0 ( A x ∪ { x : τ } ) < : A x ( σ → τ ) < : ( σ 0 → τ 0 ) A 0 < : A σ < : τ A x < : A 0 τ < : τ 0 x ( A ` σ ) < : ( A 0 ` τ ) ( A x ∪ { x : τ } ) < : ( A 0 x ∪ { x : τ 0 } ) 7

  13. Related work Implicit Parameters: Dynamic Scoping with Static Types Jeffrey R. Lewis* Mark B. Shields* Erik Meijert John Launchbury* *Oregon Graduate Institute of Science & Technology [POPL 2000] tuniversity of Utrecht A Polymorphic Modal Type System for Lisp-Like Multi-Staged Languages ∗ It is on line 478 of one thousand lines of code, and it is 5 Abstract Ik-Soon Kim Kwangkeun Yi Cristiano Calcagno levels deep in the recursion. You have basically two choices. Seoul National University Seoul National University Imperial College You can define a global named width, and use it on line 478, This paper introduces a language feature, called implicit pa- [POPL 2006] or you can add an extra parameter to nearly every function rameters, that provides dynamically scoped variables within in the pretty printer and percolate width up through all the a statically-typed Hindley-Milner framework. Implicit pa- levels of recursion. Neither choice is very satisfactory. I SO L ATE : A Type System for Self-recursion rameters are lexically distinct from regular identifiers, and are bound by a special with construct whose scope is dy- All this fuss is especially annoying because the change that namic, rather than static as with let. Implicit parameters you wish to make is conceptually rather small, yet imple- are treated by the type system as parameters that are not menting it will require a significant change to the program. Ravi Chugh explicitly declared, but are inferred from their use. What you would really like to do is get the best of both- make the definition parameterized, but not have to thread [ESOP 2015] We present implicit parameters within a small call-by-name the additional parameter through all that code. What you X-calculus. We give a type system, a type inference algo- would like to use is an implicit parameter. rithm, and several semantics. We also explore implicit pa- 8 rameters in the wider settings of call-by-need languages with With the system proposed in this paper, you only need to overloading, and call-by-value languages with effects. As a change line 478, the place where the display width is checked witness to the former, we have implemented implicit param- (and perhaps a handful of type signatures-this is discussed eters as an extension of Haskell within the Hugs interpreter, in Section 5.4). The rest of the pretty printer will remain which we use to present several motivating examples. completely unaffected. The idea is to introduce a parameter to the program whose presence is inferred, rather than the programmer having to spell it out everywhere. 1 A Scenario: Pretty Printing To introduce an implicit parameter, we change line 478 as follows: You have just finished writing the perfect pretty printer. It takes as input a document to be laid out, and produces a . . . if i >= ?width then . . . string. The ? is an annotation on an identifier that indicates an pretty :: Dot -> String implicit parameter. After this small change, when we ask what the type of pretty is again, the answer is now: You have done the hard part-your code is lovely, concise and modular, and your pretty printer produces output that pretty :: (?width :: Int) => Dot -> String is somehow even prettier than anything you would bother to do by hand. You’re thinking: JFP: Functional Pearl. This means that pretty is a function from Dot to String with an implicit parameter named width, of type Int. All But, there are just a few fussy details left. we had to do was ase the implicit parameter, and its pres- For example, you were not focusing on the unimportant de- ence was inferred. tails, so you hard-coded the width of the display to be 78 The most striking difference between implicit and regular characters. The annoying thing is that the check to see if explicit parameters is that once an implicit parameter is in- have exceeded the display width is buried deep within YOU troduced, it is propagated automatically. In other words, the code. when a function with implicit parameters is called, its im- . . . if i >= 78 then . . plicit parameters are inherited by the caller. If we examine the definition of pretty, we find that it is defined in terms of a function worker, which is itself implicitly parameterized permission to make digital or hard copies of all or part ofthis work for by ?uidth. USC is granted witllout fee provided that copies PersOXll Or &SSrOOnl are not nn& or distributed for prolit or commercial advantage a$ld that pretty d = worker d Cl copies bar this notice and the full citation on the first page. ~l‘o cC,py worker :: (?width :: Int) => @henvise, to republish, to post on servers or to redistribute to lists, Dot -> CDocl -> String requires prior specific permission and/or a fee. POPL 2000 Boston MA USA W-Wght ACM 2000 l-58113-t25-9/00/1...$5.00 108

  14. Typings as types M : ( A ` τ ) A ` M : τ A x [ { x : σ } ` M : τ M : ( A x [ { x : σ } ` τ ) A ` ( λ x.M ) : ( σ ! τ ) ( λ x.M ) : ( A ` ( σ ! τ )) A ` M : ( σ ! τ ) A ` N : σ A ` ( M N ) : τ M : ( A ` ( σ ! τ )) N : ( A ` σ ) ( M N ) : ( A ` τ ) 9

  15. Conclusion Novel type system ‣ motivated by considering abstractions as values ‣ applicable to general dynamic scope ‣ exploits typings and intersection types Future work ‣ meta-theory (soundness, …) ‣ generalise to include stores, exceptions, … ‣ achieve modularity - implicit propagation of omitted type features 10

  16. Appendix 11

Recommend


More recommend