Debian Codegraph @

src/Language/TreeCaching.v

raw
1 (** * Tree Caching for PHOAS Expressions *)
2 (** The naive encoding of PHOAS passes that need to produce both
3 [expr]-like output and data-like output simultaneously involves
4 exponential blowup.
5
6 This file allows caching of results (and/or intermediates) of a
7 data-producing PHOAS pass in a tree structure that mimics the
8 PHOAS expression so that a subsequent pass can consume this tree
9 and a PHOAS expression to produce a new expression.
10
11 More concretely, suppose we are trying to write a pass that is
12 [expr var1 * expr var2 -> A * expr var3]. We can define an
13 [expr]-like-tree-structure that (a) doesn't use higher-order
14 things for [Abs] nodes, and (b) stores [A] at every node. Then we
15 can write a pass that is [expr var1 * expr var2 -> A * tree-of-A]
16 and then [expr var1 * expr var2 * tree-of-A -> expr var3] such
17 that we incur only linear overhead.
18
19 See also
20 %\href{https://github.com/mit-plv/fiat-crypto/issues/1604#issuecomment-1553341559}{mit-plv/fiat-crypto\#1604 with option (2)}%
21 #<a href=https://github.com/mit-plv/fiat-crypto/issues/1604##issuecomment-1553341559">mit-plv/fiat-crypto##1604 with option (2)</a>#
22 and
23 %\href{https://github.com/mit-plv/fiat-crypto/issues/1761}{mit-plv/fiat-crypto\#1761}%
24 #<a href=https://github.com/mit-plv/fiat-crypto/issues/1761#">mit-plv/fiat-crypto##1761</a>#. *)
25
26 Require Import Rewriter.Language.Language.
27
28 Module Compilers.
29 Export Language.Compilers.
30 Local Set Boolean Equality Schemes.
31 Local Set Decidable Equality Schemes.
32
33 Module tree_nd.
34 Section with_result.
35 Context {base_type : Type}.
36 Local Notation type := (type base_type).
37 Context {ident : type -> Type}
38 {result : Type}.
39 Local Notation expr := (@expr.expr base_type ident).
40
41 Inductive tree : Type :=
42 | Ident (r : result) : tree
43 | Var (r : result) : tree
44 | Abs (r : result) (f : option tree) : tree
45 | App (r : result) (f : option tree) (x : option tree) : tree
46 | LetIn (r : result) (x : option tree) (f : option tree) : tree
47 .
48 End with_result.
49 Global Arguments tree result : clear implicits, assert.
50 End tree_nd.
51
52 Module tree.
53 Section with_result.
54 Context {base_type : Type}.
55 Local Notation type := (type base_type).
56 Context {ident : type -> Type}
57 {result : type -> Type}.
58 Local Notation expr := (@expr.expr base_type ident).
59
60 Inductive tree : type -> Type :=
61 | Ident {t} (r : result t) : tree t
62 | Var {t} (r : result t) : tree t
63 | Abs {s d} (r : result (s -> d)) (f : option (tree d)) : tree (s -> d)
64 | App {s d} (r : result d) (f : option (tree (s -> d))) (x : option (tree s)) : tree d
65 | LetIn {A B} (r : result B) (x : option (tree A)) (f : option (tree B)) : tree B
66 .
67 End with_result.
68 Global Arguments tree {base_type} {result} t, {base_type} result t : assert.
69 End tree.
70 End Compilers.