Skip to content

Commit 9f91e0c

Browse files
committed
[ new ] inlining let-bound expressions used at most one
1 parent 2189e05 commit 9f91e0c

4 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module Generic.Semantics.Elaboration.LetCounter where
2+
3+
import Level as L
4+
open import Size
5+
open import Agda.Builtin.Equality
6+
open import Agda.Builtin.Bool
7+
open import Data.Product
8+
import Data.Product.Categorical.Right as PC
9+
open import Data.List.Base
10+
open import Data.List.Relation.Unary.All as All
11+
open import Data.List.Relation.Unary.All.Properties
12+
open import Function
13+
14+
open import var
15+
open import varlike
16+
open import indexed
17+
open import environment using (Kripke; th^Var; ε; _∙_; extend)
18+
open import Generic.Syntax
19+
import Generic.Syntax.LetCounter as LetCounter
20+
open LetCounter hiding (Let)
21+
import Generic.Syntax.LetBinder as LetBinder
22+
open import Generic.Semantics
23+
open import Generic.Semantics.Syntactic
24+
25+
module _ {I : Set} {d : Desc I} where
26+
27+
module PCR: List I} = PC L.zero (rawMonoid Γ)
28+
29+
Counted : I ─Scoped I ─Scoped
30+
Counted T i Γ = T i Γ × Count Γ
31+
32+
count : {σ Γ} e ⟦ e ⟧ (Kripke Var (Counted (Tm (d `+ LetCounter.Let) ∞))) σ Γ
33+
Counted (⟦ e ⟧ (Scope (Tm (d `+ LetCounter.Let) ∞))) σ Γ
34+
count (`σ A e) (a , t) = map₁ (a ,_) (count (e a) t)
35+
count (`X Δ j e) (kr , t) =
36+
let (r , cr) = reify vl^Var Δ j kr
37+
(u , cu) = count e t
38+
in (r , u) , merge (++⁻ʳ Δ cr) cu
39+
count (`∎ eq) t = t , zeros
40+
41+
LetCount : Sem (d `+ LetBinder.Let) Var (Counted (Tm (d `+ LetCounter.Let) ∞))
42+
Sem.th^𝓥 LetCount = th^Var
43+
Sem.var LetCount = λ v `var v , fromVar v
44+
Sem.alg LetCount = λ where
45+
(true , t) map₁ (`con ∘′ (true ,_)) (count d t)
46+
(false , στ , (e , ce) , tct , refl)
47+
let (t , ct) = tct extend (ε ∙ z)
48+
e-usage = All.head ct
49+
in `con (false , e-usage , στ , e , t , refl)
50+
, -- if e (the let-bound expression) is not used in t (the body of the let)
51+
-- we can ignore its contribution to the count:
52+
(case e-usage of λ where
53+
zero All.tail ct
54+
_ merge ce (All.tail ct))
55+
56+
annotate : {σ Γ} Tm (d `+ LetBinder.Let) ∞ σ Γ Tm (d `+ LetCounter.Let) ∞ σ Γ
57+
annotate = proj₁ ∘′ Sem.sem LetCount (base vl^Var)
58+
59+
Inline : Sem (d `+ LetCounter.Let) (Tm (d `+ LetBinder.Let) ∞)
60+
(Tm (d `+ LetBinder.Let) ∞)
61+
Sem.th^𝓥 Inline = th^Tm
62+
Sem.var Inline = id
63+
Sem.alg Inline = λ where
64+
(true , t) `con (true , fmap d (reify vl^Tm) t)
65+
(false , many , στ , e , b , eq) `con (false , στ , e , b extend (ε ∙ `var z) , eq)
66+
(false , _ , στ , e , b , refl) b (base vl^Var) (ε ∙ e)
67+
68+
inline : {σ Γ} Tm (d `+ LetCounter.Let) ∞ σ Γ Tm (d `+ LetBinder.Let) ∞ σ Γ
69+
inline = Sem.sem Inline (base vl^Tm)
70+
71+
inline-affine : {σ Γ} Tm (d `+ LetBinder.Let) ∞ σ Γ Tm (d `+ LetBinder.Let) ∞ σ Γ
72+
inline-affine = inline ∘′ annotate

src/Generic/Syntax/LetBinder.agda

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ module _ {I : Set} where
1818

1919
pattern `IN' e t = (_ , e , t , refl)
2020
pattern `IN e t = `con (`IN' e t)
21+
22+
module _ {I : Set} {d : Desc I} where
23+
24+
embed : {i σ} [ Tm d i σ ⟶ Tm (d `+ Let) i σ ]
25+
embed = map^Tm (MkDescMorphism (true ,_))

src/Generic/Syntax/LetBinders.agda

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
module Generic.Syntax.LetBinders where
22

3+
open import Data.Bool
34
open import Data.Product
45
open import Agda.Builtin.List
56
open import Agda.Builtin.Equality
67
open import Function
78

9+
open import indexed
810
open import Generic.Syntax
911

1012
module _ {I : Set} where
1113

1214
Lets : Desc I
1315
Lets = `σ (List I × I) $ uncurry $ λ Δ σ
1416
`Xs Δ (`X Δ σ (`∎ σ))
17+
18+
module _ {I : Set} {d : Desc I} where
19+
20+
embed : {i σ} [ Tm d i σ ⟶ Tm (d `+ Lets) i σ ]
21+
embed = map^Tm (MkDescMorphism (true ,_))

src/Generic/Syntax/LetCounter.agda

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Generic.Syntax.LetCounter where
2+
3+
open import Algebra
4+
open import Data.Bool
5+
open import Data.Product
6+
open import Data.List.Relation.Unary.All
7+
open import Agda.Builtin.List
8+
open import Agda.Builtin.Equality
9+
open import Function
10+
11+
open import indexed
12+
open import var
13+
open import Generic.Syntax
14+
15+
import Generic.Syntax.LetBinder as LetBinder
16+
17+
data Counter : Set where
18+
zero : Counter
19+
one : Counter
20+
many : Counter
21+
22+
_+_ : Counter Counter Counter
23+
zero + n = n
24+
m + zero = m
25+
_ + _ = many
26+
27+
module _ {I : Set} where
28+
29+
Count : List I Set
30+
Count = All (λ _ Counter)
31+
32+
zeros : [ Count ]
33+
zeros = tabulate (λ _ zero)
34+
35+
fromVar : {i} [ Var i ⟶ Count ]
36+
fromVar z = one ∷ zeros
37+
fromVar (s v) = zero ∷ fromVar v
38+
39+
merge : [ Count ⟶ Count ⟶ Count ]
40+
merge = curry (zipWith (uncurry _+_))
41+
42+
rawMonoid : List I RawMonoid _ _
43+
rawMonoid Γ = record
44+
{ Carrier = Count Γ
45+
; _≈_ = _≡_
46+
; _∙_ = merge
47+
; ε = tabulate (λ _ zero)
48+
}
49+
50+
module _ {I : Set} where
51+
52+
Let : Desc I
53+
Let = `σ Counter $ λ _ LetBinder.Let
54+
55+
pattern `IN' e t = (_ , e , t , refl)
56+
pattern `IN e t = `con (`IN' e t)
57+
58+
module _ {I : Set} {d : Desc I} where
59+
60+
embed : {i σ} [ Tm d i σ ⟶ Tm (d `+ Let) i σ ]
61+
embed = map^Tm (MkDescMorphism (true ,_))

0 commit comments

Comments
 (0)