visitors unchained
play

Visitors Unchained Using visitors to traverse abstract syntax with - PowerPoint PPT Presentation

Visitors Unchained Using visitors to traverse abstract syntax with binding Franois Pottier ICFP 2017, Oxford September 5, 2017 Visitors Unchained Using visitors to traverse abstract syntax with binding OBJECTS Franois Pottier AT ICFP!?


  1. Visitors Unchained Using visitors to traverse abstract syntax with binding François Pottier ICFP 2017, Oxford September 5, 2017

  2. Visitors Unchained Using visitors to traverse abstract syntax with binding OBJECTS François Pottier AT ICFP!? ICFP 2017, Oxford September 5, 2017

  3. Visitors Unchained Using visitors to traverse abstract syntax with binding OBJECTS François Pottier AT ICFP!? BINDERS, AGAIN!? ICFP 2017, Oxford September 5, 2017

  4. Boilerplate alert! Manipulating abstract syntax with binding can be a chore . Whenever one creates a new language, one must typically implement: ◮ substitution , α -equivalence , free names , – (all representations) ◮ opening / closing , shifting , – (some representations) ◮ converting between representations... This boilerplate code is known as nameplate (Cheney). It is large , boring , error-prone . It does not seem easily reusable, as it is datatype-specific .

  5. Fighting boilerplate! In this talk & paper: A way of getting rid of nameplate , in OCaml , which ◮ supports multiple representations of names and conversions between them, ◮ supports complex binding constructs , ◮ is modular and open-ended , that is, user-extensible, ◮ relies on as little code generation as possible. Based on a combination of auto-generated visitors and library code. ◮ visitors , an OCaml syntax extension (released); ◮ AlphaLib , an OCaml library (at a preliminary stage).

  6. Wait! Isn’t this a solved problem already? Several Haskell libraries address this problem: FreshLib, Unbound, Bound... They exploit Haskell’s support for generic programming (SYB, RepLib, ...) Cool stuff can be done in Coq (Schäfer et al. , 2015) and Agda (Allais et al. , 2017). In the OCaml world: ◮ C α ml (F .P ., 2005), an ad hoc code generator; monolithic, inflexible. ◮ Yallop ports SYB to MetaOCaml+implicits: next talk! The way of the future? – this talk: making do with vanilla OCaml .

  7. Visitors

  8. Generating a “map” visitor, in a nutshell Annotating a type definition with [@@deriving visitors { ... }] ... expr = type | EConst of int | EAdd of expr * expr [ @@deriving visitors { variety = "map" }]

  9. Generating a “map” visitor, in a nutshell Annotating a type definition with [@@deriving visitors { ... }] ... expr = type | EConst of int | EAdd of expr * expr [ @@deriving visitors { variety = "map" }] ... causes a visitor class to be auto-generated: class virtual [’self] map = object (self : ’self) inherit [_] VisitorsRuntime .map method visit_EConst env c0 = let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 c1 = method let r0 = self#visit_expr env c0 in let r1 = self#visit_expr env c1 in EAdd (r0 , r1) visit_expr env this = method this match with | EConst c0 -> self# visit_EConst env c0 | EAdd (c0 , c1) -> self# visit_EAdd env c0 c1 end

  10. Generating a “map” visitor, in a nutshell Annotating a type definition with [@@deriving visitors { ... }] ... expr = type | EConst of int | EAdd of expr * expr [ @@deriving visitors { variety = "map" }] ... causes a visitor class to be auto-generated: class virtual [’self] map = object (self : ’self) inherit [_] VisitorsRuntime .map method visit_EConst env c0 = let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 c1 = method let r0 = self#visit_expr env c0 in let r1 = self#visit_expr env c1 in EAdd (r0 , r1) visit_expr env this = method this one method per match with | EConst c0 -> data type self# visit_EConst env c0 | EAdd (c0 , c1) -> self# visit_EAdd env c0 c1 end

  11. Generating a “map” visitor, in a nutshell Annotating a type definition with [@@deriving visitors { ... }] ... expr = type | EConst of int | EAdd of expr * expr [ @@deriving visitors { variety = "map" }] ... causes a visitor class to be auto-generated: class virtual [’self] map = object (self : ’self) inherit [_] VisitorsRuntime .map method visit_EConst env c0 = let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 c1 = method let r0 = self#visit_expr env c0 in let r1 = self#visit_expr env c1 in EAdd (r0 , r1) visit_expr env this = method this one method per match with | EConst c0 -> data constructor self# visit_EConst env c0 | EAdd (c0 , c1) -> self# visit_EAdd env c0 c1 end

  12. Generating a “map” visitor, in a nutshell Annotating a type definition with [@@deriving visitors { ... }] ... expr = type | EConst of int | EAdd of expr * expr [ @@deriving visitors { variety = "map" }] ... causes a visitor class to be auto-generated: class virtual [’self] map = object (self : ’self) inherit [_] VisitorsRuntime .map method visit_EConst env c0 = let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 c1 = method let r0 = self#visit_expr env c0 in let r1 = self#visit_expr env c1 in EAdd (r0 , r1) visit_expr env this = method this default behavior match with | EConst c0 -> is to rebuild a tree self# visit_EConst env c0 | EAdd (c0 , c1) -> self# visit_EAdd env c0 c1 end

  13. Generating a “map” visitor, in a nutshell Annotating a type definition with [@@deriving visitors { ... }] ... expr = type | EConst of int | EAdd of expr * expr [ @@deriving visitors { variety = "map" }] ... causes a visitor class to be auto-generated: class virtual [’self] map = object (self : ’self) inherit [_] VisitorsRuntime .map method visit_EConst env c0 = let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 c1 = method let r0 = self#visit_expr env c0 in let r1 = self#visit_expr env c1 in EAdd (r0 , r1) visit_expr env this = method this an environment match with | EConst c0 -> is pushed down self# visit_EConst env c0 | EAdd (c0 , c1) -> self# visit_EAdd env c0 c1 end

  14. Using a “map” visitor, in a nutshell Inherit a visitor class and override one or more methods: optimize : expr -> expr = let let v = object (self) inherit [_] map method ! visit_EAdd env e1 e2 = self# visit_expr env e1 ,self# visit_expr env e2 match with | EConst 0, e (* 0 + e = e *) | e, EConst 0 -> e (* e + 0 = e *) | e1 , e2 -> EAdd (e1 , e2) end in v # visit_expr () No changes to this code are needed when more expression forms are added.

  15. Visiting preexisting / parameterized types Integers and lists can be visited, too. a preexisting type expr = type | EConst of int | EAdd of expr list [ @@deriving visitors { variety = "map" }] virtual [’self] map = object (self : ’self) class inherit [_] VisitorsRuntime .map visit_EConst env c0 = method let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 = method let r0 = self#visit_list (self# visit_expr) env c0 in EAdd r0 method visit_expr env this = ... end A visitor can be “taught” to traverse a data structure!

  16. Visiting preexisting / parameterized types Integers and lists can be visited, too. visitor method is inherited expr = type | EConst of int | EAdd of expr list [ @@deriving visitors { variety = "map" }] virtual [’self] map = object (self : ’self) class inherit [_] VisitorsRuntime .map visit_EConst env c0 = method let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 = method let r0 = self#visit_list (self# visit_expr) env c0 in EAdd r0 method visit_expr env this = ... end A visitor can be “taught” to traverse a data structure!

  17. Visiting preexisting / parameterized types Integers and lists can be visited, too. a preexisting parameterized type expr = type | EConst of int | EAdd of expr list [ @@deriving visitors { variety = "map" }] virtual [’self] map = object (self : ’self) class inherit [_] VisitorsRuntime .map visit_EConst env c0 = method let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 = method let r0 = self#visit_list (self# visit_expr) env c0 in EAdd r0 method visit_expr env this = ... end A visitor can be “taught” to traverse a data structure!

  18. Visiting preexisting / parameterized types Integers and lists can be visited, too. inherited visitor method is passed a visitor function expr = type | EConst of int | EAdd of expr list [ @@deriving visitors { variety = "map" }] virtual [’self] map = object (self : ’self) class inherit [_] VisitorsRuntime .map visit_EConst env c0 = method let r0 = self#visit_int env c0 in EConst r0 visit_EAdd env c0 = method let r0 = self#visit_list (self# visit_expr) env c0 in EAdd r0 method visit_expr env this = ... end A visitor can be “taught” to traverse a data structure!

  19. Visitors – summary Although they follow fixed patterns, visitors are quite versatile . They are customizable and composable . More fun with visitors: ◮ visitors for open data types and their fixed points (link); ◮ visitors for hash-consed data structures (link); ◮ iterators out of visitors (link). In the remainder of this talk: ◮ Traversing abstract syntax with binding .

  20. Visitors Unchained

  21. Traversing syntax with binding For modularity , it seems desirable to distinguish three concerns: 1. Describing a binding construct . 2. Describing an operation on terms. ◮ usually specific of one representation of names and binders, ◮ sometimes specific of two such representations, e.g., conversions . 3. The end user should be insulated from this complexity. – 1 & 2 are part of AlphaLib .

  22. end user binder maestro operations specialist

  23. The end user

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