Skip to content

Commit ea02836

Browse files
committed
Uniquify all Sprite preterms before elaboartion
This commit α-renames all variables in a separate step prior elaboration in order to avoid name clashes as demonstrated by `#testAlphaClash`. This is similar to PR #197 but differ in several ways: * perform renaming as a separate step, not as part of [Chk-Lam]. * renames all lets, not just function parameter binds * renames everything in single top-down traversal. Fixes #139 (well, not quite yet)
1 parent 52614b9 commit ea02836

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+555
-4
lines changed

src/SpriteLang-Tests/BoolGaloisConnectionTest.class.st

-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ BoolGaloisConnectionTest >> testAlphaClash [
9393
so must end up in the same UNSAFE result.
9494
However, this breaks, see issue #139.
9595
"
96-
self skip. "Remove this skip after #139 is resolved."
9796
self proveUnsafe: '
9897
⟦val b2i : b:bool => int[i | ((i===0) not <=> b) & ((i===0)|(i===1)) ] ⟧
9998
let b2i = (b) => {

src/SpriteLang/ARef.class.st

+12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ ARef >> tsubstGoA: outerT tVar: outerA [
9999
x -> (t rSortToRType tsubstGo: outerT tVar: outerA) rTypeToRSort ])
100100
]
101101

102+
{ #category : #'α-renaming' }
103+
ARef >> uniq2: α [
104+
| α′ |
105+
106+
α′ := α.
107+
arArgs do:[:a|α′ := α′ extMap: a key to: a key uniq].
108+
109+
^self class
110+
arArgs: (arArgs collect:[:a|(α′ at: a key) -> (a value uniq2: α′)])
111+
arPred: (arPred uniq2: α′)
112+
]
113+
102114
{ #category : #'as yet unclassified' }
103115
ARef >>+ rhs [
104116
| xts1 p1 xts2 p2 su |

src/SpriteLang/Alt.class.st

+8
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ Alt >> expr [
5959
Alt >> expr: anObject [
6060
expr := anObject
6161
]
62+
63+
{ #category : #'α-renaming' }
64+
Alt >> uniq2: α [
65+
^self class
66+
daCon: daCon
67+
binds: (binds collect: [ :each | each uniq2: α])
68+
expr: (expr uniq2: α)
69+
]

src/SpriteLang/EAnn.class.st

+10
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ EAnn >> synth: Γ [
7272
c := expr check: Γ rtype: t.
7373
^{ c . t }
7474
]
75+
76+
{ #category : #'α-renaming' }
77+
EAnn >> uniq2: α [
78+
79+
^self class
80+
expr: (expr uniq2: α)
81+
ann: (ann uniq2: α)
82+
83+
84+
]

src/SpriteLang/ECase.class.st

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ ECase >> goSubsTyExpr: su [
5858
])
5959
]
6060

61+
{ #category : #'α-renaming' }
62+
ECase >> uniq2: α [
63+
| x′ alts′ |
64+
x′ := α at: x ifAbsent:[x].
65+
alts′ := alts collect: [ :each | each uniq2: α ].
66+
^self class
67+
x: x′ alts: alts′
68+
69+
]
70+
6171
{ #category : #accessing }
6272
ECase >> x [
6373
^ x

src/SpriteLang/ECon.class.st

+5
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ ECon >> synthImm: Γ [
5959
ECon >> toFX [
6060
^self shouldBeImplemented
6161
]
62+
63+
{ #category : #'α-renaming' }
64+
ECon >> uniq2: α [
65+
"Nothing to do"
66+
]

src/SpriteLang/EFun.class.st

+13
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,16 @@ EFun >> renameTy: ty metric: m [
9292
t′′_m′′ := expr renameTy: t′ metric: m′. t′′ := t′′_m′′ key. m′′ := t′′_m′′ value.
9393
^(TFun x: x s: s′ t: t′′) -> m′′
9494
]
95+
96+
{ #category : #'α-renaming' }
97+
EFun >> uniq2: α [
98+
| id id′ α′ |
99+
100+
id := bind id.
101+
id′ := α at: id ifAbsent:[id uniq].
102+
α′ := α extMap: id to: id′.
103+
104+
^self class
105+
bind: (bind uniq2: α′)
106+
expr: (expr uniq2: α′)
107+
]

src/SpriteLang/EIf.class.st

+9
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ EIf >> trueE [
7777
EIf >> trueE: anObject [
7878
trueE := anObject
7979
]
80+
81+
{ #category : #'α-renaming' }
82+
EIf >> uniq2: α [
83+
^self class
84+
cond: (cond uniq2: α)
85+
trueE: (trueE uniq2: α)
86+
falseE: (falseE uniq2: α).
87+
88+
]

src/SpriteLang/EImm.class.st

+11
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,14 @@ EImm >> synth: Γ [
5151
t := imm synthImm: Γ.
5252
^{ HCstr cTrue . t }
5353
]
54+
55+
{ #category : #'as yet unclassified' }
56+
EImm >> uniq [
57+
"Nothing to do. This is only called on 'sentinel' EImm terminating list of ELet decls"
58+
]
59+
60+
{ #category : #'α-renaming' }
61+
EImm >> uniq2: α [
62+
^self class imm: (imm uniq2: α)
63+
64+
]

src/SpriteLang/ELet.class.st

+35
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,38 @@ ELet >> goSubsTyExpr: su [
7979
ELet >> gtChildren [
8080
^{decl . expr}
8181
]
82+
83+
{ #category : #'α-renaming' }
84+
ELet >> uniq [
85+
"α-rename all variables ensuring all have unique names.
86+
Returns a new instance."
87+
88+
| α decl′ expr′ |
89+
90+
α := αRenames empty.
91+
decl′ := decl class bind: decl bind expr: (decl expr uniq2: α).
92+
expr′ := expr uniq.
93+
94+
^self class
95+
decl: decl′
96+
expr: expr′
97+
98+
99+
100+
101+
]
102+
103+
{ #category : #'α-renaming' }
104+
ELet >> uniq2: α [
105+
| id id′ α′ decl′ expr′ |
106+
107+
id := decl bind id.
108+
id′:= α at: id ifAbsent:[id uniq].
109+
α′ := α extMap: id to: id′.
110+
111+
decl′ := decl class bind: (decl bind uniq2: α′) expr: (decl expr uniq2: α)."<-- CERTAINLY NOT α!!!"
112+
expr′ := expr uniq2: α′.
113+
114+
^self class decl: decl′ expr: expr′
115+
116+
]

src/SpriteLang/ERApp.class.st

+7
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ ERApp >> synth: Γ [
4141
s′ := s rinst.
4242
^{ c . s′ }
4343
]
44+
45+
{ #category : #'α-renaming' }
46+
ERApp >> uniq2: α [
47+
^self class
48+
expr: (expr uniq2: α)
49+
50+
]

src/SpriteLang/ETApp.class.st

+8
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ ETApp >> synth: Γ [
5555
tt := Γ refresh: rtype.
5656
^{ ce . te type tsubst: tt tVar: te var }
5757
]
58+
59+
{ #category : #'α-renaming' }
60+
ETApp >> uniq2: α [
61+
^self class
62+
expr: (expr uniq2: α)
63+
rtype: (rtype uniq2: α)
64+
65+
]

src/SpriteLang/ETLam.class.st

+5
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,8 @@ ETLam >> tvar [
6464
ETLam >> tvar: anObject [
6565
tvar := anObject
6666
]
67+
68+
{ #category : #'α-renaming' }
69+
ETLam >> uniq2: α [
70+
self shouldBeImplemented
71+
]

src/SpriteLang/EVar.extension.st

+9
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ synthImm :: Env -> SrcImm -> CG RType
5353
t := Γ getEnvDamit: sym.
5454
^t singleton: sym
5555
]
56+
57+
{ #category : #'*SpriteLang' }
58+
EVar >> uniq2: α [
59+
| sym′ |
60+
61+
sym′ := α at: sym ifAbsent:[ ^ self ].
62+
^self class of: sym′
63+
64+
]

src/SpriteLang/Expr.extension.st

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ predRType p = TBase TBool (known $ F.predReft p)
1111
r: self "F."predReft known
1212
]
1313

14+
{ #category : #'*SpriteLang' }
15+
Expr >> uniq2: α [
16+
"α-rename all variables. Returns a copy of receiver with all names unique."
17+
18+
| self|
19+
20+
self:= self.
21+
α keysAndValuesDo: [ :orig :uniq |
22+
self:= selfrename: orig to: uniq.
23+
].
24+
^ self
25+
26+
27+
]
28+
1429
{ #category : #'*SpriteLang' }
1530
Expr >> ΛpredReft [
1631
^self predReft known

src/SpriteLang/HPred.extension.st

+8
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ smash p = [p]
4747
HPred >> subsAR: p ar: ar [
4848
^self "No κ-apps can happen, because those have been refactored to RefVarApps"
4949
]
50+
51+
{ #category : #'*SpriteLang' }
52+
HPred >> uniq2: α [
53+
"α-rename all variables. Returns a copy of receiver with all names unique."
54+
55+
self shouldBeImplemented
56+
57+
]

src/SpriteLang/HPredAnd.extension.st

+6
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ HPredAnd >> subs: p ar: ar [
2323
HPredAnd >> subsAR: aString ar: anARef [
2424
^HPredAnd of: (ps collect: [ :each | each subsAR: aString ar: anARef ])
2525
]
26+
27+
{ #category : #'*SpriteLang' }
28+
HPredAnd >> uniq2: α [
29+
^self class of: (ps collect: [:e|e uniq2: α])
30+
31+
]

src/SpriteLang/HReft.extension.st

+6
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ HReft >> predExprsGo [
1414
HReft >> subs: p ar: ar [
1515
^self
1616
]
17+
18+
{ #category : #'*SpriteLang' }
19+
HReft >> uniq2: α [
20+
^self class expr: (expr uniq2: α)
21+
22+
]

src/SpriteLang/KnownReft.class.st

+11
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,14 @@ KnownReft >> symbol: anObject [
163163
KnownReft >> syms [
164164
^{symbol}, expr syms
165165
]
166+
167+
{ #category : #'α-renaming' }
168+
KnownReft >> uniq2: α [
169+
| symbol′ α′ expr′ |
170+
171+
symbol′ := α at: symbol ifAbsent:[symbol uniq].
172+
α′ := α extMap: symbol to: symbol′.
173+
expr′ := expr uniq2: α′.
174+
175+
^self class symbol: symbol′ expr: expr′
176+
]

src/SpriteLang/MetricExpression.class.st

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ MetricExpression >> subst: su [
5959
^self class source: replacement sym
6060
]
6161

62+
{ #category : #'α-renaming' }
63+
MetricExpression >> uniq2: α [
64+
| source′ |
65+
66+
source′ := α at: source ifAbsent:[^self].
67+
^self class source: source′
68+
69+
70+
]
71+
6272
{ #category : #'as yet unclassified' }
6373
MetricExpression >> wfExpr: γ sort: t [
6474
"

src/SpriteLang/Prog.class.st

+5-3
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ Prog >> solve [
8282

8383
{ #category : #verification }
8484
Prog >> vcgen [
85-
| env eL cstr query |
85+
| expr′ env expr′′ cstr query |
86+
87+
expr′ := expr uniq.
8688
env := ΓContext empEnv: meas typs: data.
87-
eL := expr elaborate: env.
89+
expr′′ := expr elaborate: env.
8890
CGState reset.
89-
cstr := eL check: env rtype: TInt instance bTrue.
91+
cstr := expr′′ check: env rtype: TInt instance bTrue.
9092
query := HornQuery new.
9193
cstr addToQuery: query.
9294
CGState current cgInfo cgiKVars do: [ :kVar | kVar addToQuery: query ].

src/SpriteLang/RType.class.st

+18
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ closeType :: [RVar] -> [(F.Symbol, RType)] -> RType -> RType
137137
^rvParams generalize
138138
]
139139

140+
{ #category : #'α-renaming' }
141+
RType >> collectαRenames: α [
142+
"Create (but not apply!) all neccessary α-renames
143+
and return updated renames. See SpriteAnn >> #uniq2: why
144+
this is needed."
145+
146+
^ self subclassResponsibility
147+
148+
149+
]
150+
140151
{ #category : #polymorphism }
141152
RType >> dispatchUnify: t1 [
142153
"The 'normal' case where we just fall through to unifyLL:."
@@ -516,6 +527,13 @@ unify :: F.SrcSpan -> RType -> RType -> ElabM RType
516527
^t2 dispatchUnify: self
517528
]
518529

530+
{ #category : #'α-renaming' }
531+
RType >> uniq2: α [
532+
"α-rename all variables. Returns a copy of receiver with all names unique."
533+
534+
self subclassResponsibility.
535+
]
536+
519537
{ #category : #'as yet unclassified' }
520538
RType >> ≺ [ t
521539
self subclassResponsibility

src/SpriteLang/RVar.class.st

+10
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,13 @@ RVar >> tsubstGoP: t′ tVar: a [
122122
rvArgs: newRvArgs;
123123
yourself
124124
]
125+
126+
{ #category : #'α-renaming' }
127+
RVar >> uniq2: α [
128+
| rvName′ |
129+
130+
rvName′ := α at: rvName ifAbsent:[^self].
131+
^self class
132+
rvName: rvName′
133+
rvArgs: (rvArgs collect:[:e|e uniq2: α])
134+
]

src/SpriteLang/RefVarApp.extension.st

+7
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ go (H.Var k xs) | k == p ...
2727
θ := yts zip: xs with: [ :y_☐ :x | y_☐ key -> x ].
2828
^pr substs: θ
2929
]
30+
31+
{ #category : #'*SpriteLang' }
32+
RefVarApp >> uniq2: α [
33+
^self class
34+
var:at: var ifAbsent:[var])
35+
args: (args collect:[:argat: arg ifAbsent:[arg]])
36+
]

src/SpriteLang/Refl.class.st

+7
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ Class {
33
#superclass : #SpriteAnn,
44
#category : #SpriteLang
55
}
6+
7+
{ #category : #'α-renaming' }
8+
Refl >> uniq2: α [
9+
10+
self shouldBeImplemented
11+
12+
]

0 commit comments

Comments
 (0)