Skip to content

Commit e3e2fa1

Browse files
KJ-10 Reduce usage isElabIndependent predicate to default elab cooling
1 parent 8252da5 commit e3e2fa1

1 file changed

Lines changed: 41 additions & 29 deletions

File tree

semantics/elaborate-blocks.k

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ syntax K ::= "setEncloser" "(" TypedExp //Evaluates to source object
6666
"," K //The enclosing object
6767
")" [strict(1,3)]
6868

69-
//@Computes into an expression of the form elabRes('QThis(QualClass)::QualClass),
70-
//@where QualClass is searched in the enclosing context of the first argument,
71-
//@being a subclass of the second one.
69+
/*@ Computes into an expression of the form elabRes('QThis(QualClass)::QualClass),
70+
where QualClass is searched in the enclosing context of the first argument,
71+
being a subclass of the second one.
72+
Or elabRes(noValue) if no suitable result is found
73+
*/
7274
syntax K ::= "getElabResQThisSubclassOf" "(" ClassType //The context class in which qualifier is searched for.
7375
"," ClassType //Qualifier should be subclass of this class
7476
")" [strict(2)]
@@ -248,23 +250,21 @@ rule [elabStaticEnd]:
248250

249251
//@Adds a new empty layer to <elabEnv>
250252
syntax K ::= "addElabEnv"
251-
252-
//@Removes the last layer from <elabEnv>
253-
syntax K ::= "removeLastElabEnv"
254-
255-
//@Adds params to the <elabEnv>.
256-
syntax K ::= "elabParams" "(" KList ")"
257-
258253
rule [addElabEnv]:
259254
<k> addElabEnv => . ...</k>
260255
<elabEnv> . => ListItem(stEnv(.Map)) ...</elabEnv>
261256
<localTypes> . => ListItem(stEnv(.Map)) ...</localTypes>
262257

258+
//@Removes the last layer from <elabEnv>
259+
syntax K ::= "removeLastElabEnv"
263260
rule [removeLastElabEnv]:
264261
<k> removeLastElabEnv => . ...</k>
265262
<elabEnv> ListItem(_) => . ...</elabEnv>
266263
<localTypes> ListItem(_) => . ...</localTypes>
267264

265+
//@Adds params to the <elabEnv>.
266+
syntax K ::= "elabParams" "(" KList ")"
267+
268268
rule [elabParams]:
269269
<k> elabParams((paramImpl(T:Type, X:Id) => .KList) ,,_) ...</k>
270270
<elabEnv> ListItem(stEnv((. => X |-> T) _)) ...</elabEnv>
@@ -330,6 +330,8 @@ rule isElabNaked(K:K) =>
330330
All KLabels that can be part of a code block during elaboration phase,
331331
except those members of customElabChildren or isElabNaked groups.
332332
*/
333+
//todo: maybe statements should be processed by default rules as they are now, but expressions should not.
334+
//Let's start from expressions.
333335
syntax K ::= "defaultElabChildren" "(" KLabel ")" [function]
334336
rule defaultElabChildren(KL:KLabel) =>
335337
(KL ==KLabel 'ListWrap)
@@ -554,10 +556,9 @@ rule [elabCoolExpressionOrKResult]:
554556
when
555557
isElabIndependent(ElabIndep)
556558
557-
rule [elabCoolIndependent]:
558-
(ElabIndep:K => .) ~> elab(_:KLabel(_,, (CHOLE => ElabIndep),, _))
559-
when
560-
isElabIndependent(ElabIndep)
559+
/*Was KR:ElabIndependent before, but in fact only KResult may stay free in this position.*/
560+
rule [elabCoolKResult]:
561+
(KR:KResult => .) ~> elab(_:KLabel(_,, (CHOLE => KR),, _))
561562
562563
/*@ The default algorithm of transforming the term from elab to elabRes, when the children were completely elaborated.
563564
Deletes elabRes wrappers from children. This algorithm is activated when the following conditions apply:
@@ -584,26 +585,30 @@ rule [elabDisposeEnd]:
584585
when
585586
notBool isExpressionLabel(KL) andBool haveNoElabRes(Ks)
586587
587-
//@ Required by the rule [elabQNewInstance-resolve-class]
588-
rule [elabDisposeCoolIndependent]:
589-
(ElabIndep:K => .) ~> elabDispose(_:KLabel(_,, (CHOLE => ElabIndep),, _))
590-
when
591-
isElabIndependent(ElabIndep)
592-
593-
syntax K ::= "isElabIndependent" "(" K ")" [function]
594-
rule isElabIndependent(K:K) =>
595-
(isKResult(K) ==K true)
596-
orBool (isTypedExp(K) ==K true)
597-
598-
//@ Computes to true if the given argument is a list of elaboration results, false otherwise.
588+
/*@ Computes to true if the given argument is a list of elaboration results, false otherwise.
589+
An elaborated result is either:
590+
- KResult
591+
- TypedExp
592+
- elabRes(...)
593+
*/
599594
syntax K ::= "isElab" "(" KList ")" [function]
600595
rule isElab(K:K,, Ks:KList)
601-
=> ((getKLabel(K) ==KLabel 'elabRes`(_`)) orBool (isElabIndependent(K) ==K true))
596+
=> ((getKLabel(K) ==KLabel 'elabRes`(_`)) orBool (isTypedExp(K) ==K true) orBool (isKResult(K) ==K true))
602597
andBool isElab(Ks)
603598

604599
rule isElab(.KList) => true
605600

606-
//@ Computes to true if the given argument is a list of terms with none being elab-result only, false otherwise.
601+
/*@ Either of:
602+
- KResult
603+
- TypedExp
604+
Used in defaut cooling rules of elaboration phase, to distinguish between elborated statements and elaborated
605+
expressions. When expressions are cooled they are unwrapped fron thei elabRes() wrapper.
606+
*/
607+
syntax K ::= "isElabIndependent" "(" K ")" [function]
608+
rule isElabIndependent(K:K)
609+
=> (isKResult(K) ==K true) orBool (isTypedExp(K) ==K true)
610+
611+
//@ True if given KList have no terms of the form elabRes(...), false otherwise.
607612
syntax K ::= "haveNoElabRes" "(" KList ")" [function]
608613
rule haveNoElabRes(K:K,, Ks:KList)
609614
=> (getKLabel(K) =/=KLabel 'elabRes`(_`)) andBool haveNoElabRes(Ks)
@@ -637,6 +642,8 @@ rule [elabCatch]:
637642
<elabEnv> (. => ListItem(StEnvK)) ListItem(StEnvK:K) ...</elabEnv>
638643
<localTypes> (. => ListItem(LocTypesK)) ListItem(LocTypesK:K) ...</localTypes>
639644
645+
//here we need elabDispose because the block content is a list.
646+
//We could move the elabdospose logic to ListWrap instead of having it here.
640647
rule [elabDisposeBlockForOrCatch]:
641648
<k> elab(KL:KLabel(ElabResL:KList)) => removeLastElabEnv ~> elabDispose(KL(ElabResL)) ...</k>
642649
when
@@ -1219,11 +1226,16 @@ rule [existsClass]:
12191226
</k>
12201227
<namesToClasses>... Pack:PackageId |-> mapWrap(NamesToClasses:Map) ...</namesToClasses>
12211228
1229+
/*@ All new instance creation expressions are converted into qualified ones - 'QNewInstance, duringelaboration phase.
1230+
For instance inner classes, the qualifier will be a valid expression for the qualifier. For other classes qualifier
1231+
will be noValue.
1232+
*/
12221233
rule [elab-QNewInstance]:
12231234
elab('QNewInstance(Qual:K,, Arg2:K,, T:RefType,, Arg4:K,, elabRes(ActualArgsList:K),, 'None(.KList)))
12241235
=> elabRes('QNewInstance(Qual,, Arg2,, T,, Arg4,, ActualArgsList,, 'None(.KList)) :: T)
12251236
when
1226-
isElabIndependent(Qual)
1237+
//warning: cannot use ==Bool in the first expression - looks like isTypedExp(Qual) will not be computed
1238+
(isTypedExp(Qual) ==K true) orBool (Qual ==K noValue)
12271239

12281240
rule [elabInstanceOf]:
12291241
elabDispose('InstanceOf(TE:TypedExp,, RT2:RefType)) => elabRes('InstanceOf(TE,, RT2) :: bool)

0 commit comments

Comments
 (0)