Skip to content

Commit 20345ab

Browse files
committed
fix test
1 parent c081b71 commit 20345ab

File tree

10 files changed

+34
-35
lines changed

10 files changed

+34
-35
lines changed

src/main/scala/com/github/tgeng/archon/core/ir/builtins.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ object Builtins:
113113
val EffectsRetainSimpleLinearQn: QualifiedName = BuiltinEffects / "retainSimpleLinear"
114114
val TotalQn: QualifiedName = BuiltinEffects / "total"
115115
val DivQn: QualifiedName = BuiltinEffects / "div"
116+
val NdetQn: QualifiedName = BuiltinEffects / "ndet"
116117
val MaybeDivQn: QualifiedName = BuiltinEffects / "mdiv"
117118

118119
val BuiltinLevel: QualifiedName = Builtin / "level"
@@ -134,9 +135,8 @@ object Builtins:
134135
throw new IllegalArgumentException(s"Conflicting definitions: $conflictingDefs")
135136
elaborateAll(builtins)(using Context.empty)(using SimpleSignature())
136137

137-
import CTerm.*
138-
import PreDeclaration.*
139-
import Usage.*
138+
import CTerm.*
139+
import PreDeclaration.* import Usage.*
140140
import VTerm.*
141141

142142
private def binding(name: Name, ty: VTerm, usage: VTerm = uAny): Binding[CTerm] =

src/main/scala/com/github/tgeng/archon/core/ir/conversion.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def checkIsConvertible
284284
op2 @ OperationCall(effInstance2, name2, args2),
285285
) if name1 == name2 =>
286286
val effConstraint = checkIsConvertible(effInstance1, effInstance2, Some(EffectsType()))
287-
val (qn, tArgs) = inferType(effInstance1)._1 match
287+
val (qn, tArgs) = inferType(effInstance1)._2 match
288288
case EffectInstanceType(eff, _) => eff
289289
case _ => throw ComplexOperationCall(op2)
290290
val operation = Σ.getOperation(qn, name1)

src/main/scala/com/github/tgeng/archon/core/ir/irError.scala

+1-10
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ enum IrError(val Γ: Context, e: Throwable | Null = null) extends Exception(e):
4949
case MissingDefinition(qn: QualifiedName) extends IrError(Context.empty)
5050
case MissingField(name: Name, qn: QualifiedName) extends IrError(Context.empty)
5151
case MissingFieldsInCoPattern(clause: PreClause)(using Γ: Context) extends IrError(Γ)
52-
case MissingOperation(name: Name, qn: QualifiedName) extends IrError(Context.empty)
5352
case MissingUserCoPattern(clause: PreClause)(using Γ: Context) extends IrError(Γ)
5453
case NonEmptyType(ty: VTerm, source: PreClause)(using Γ: Context) extends IrError(Γ)
5554
case NotCConvertible(sub: CTerm, sup: CTerm, ty: Option[CTerm])(using Γ: Context)
@@ -60,12 +59,6 @@ enum IrError(val Γ: Context, e: Throwable | Null = null) extends Exception(e):
6059
case NotDataTypeType(tm: CTerm)(using Γ: Context) extends IrError(Γ)
6160
case NotLevelType(tm: VTerm)(using Γ: Context) extends IrError(Γ)
6261
case NotEffectSubsumption(sub: VTerm, sup: VTerm)(using Γ: Context) extends IrError(Γ)
63-
case NotEqDecidabilitySubsumption(sub: VTerm, sup: VTerm)(using Γ: Context) extends IrError(Γ)
64-
case NotHandlerTypeSubsumption(sub: VTerm, sup: VTerm)(using Γ: Context) extends IrError(Γ)
65-
case NotEqDecidableDueToConstructor
66-
(qn: QualifiedName, conName: Name, badComponentIndex: Nat)
67-
(using Γ: Context) extends IrError(Γ)
68-
case NotEqDecidableType(ty: VTerm)(using Γ: Context) extends IrError(Γ)
6962
case NotLevelSubsumption(sub: VTerm, sup: VTerm)(using Γ: Context) extends IrError(Γ)
7063
case NotTypeError(tm: VTerm)(using Γ: Context) extends IrError(Γ)
7164
case NotUsageSubsumption(sub: VTerm, sup: VTerm)(using Γ: Context) extends IrError(Γ)
@@ -94,13 +87,11 @@ enum IrError(val Γ: Context, e: Throwable | Null = null) extends Exception(e):
9487
(using Γ: Context) extends IrError(Γ, cause)
9588
case DataLevelCannotDependOnIndexArgument(preData: PreDeclaration.PreData)(using Γ: Context)
9689
extends IrError(Γ)
97-
case DataEqDecidabilityCannotDependOnIndexArgument
98-
(preData: PreDeclaration.PreData)
99-
(using Γ: Context) extends IrError(Γ)
10090
case DuplicatedDeclaration(qn: QualifiedName) extends IrError(Context.empty)
10191
case UnableToFindUsageMeetDuringUnification(lowerBounds: Set[VTerm])(using Γ: Context)
10292
extends IrError(Γ)
10393
case InternalIrError(message: String)(using Γ: Context) extends IrError(Γ)
10494
case ComplexOperationCall(call: CTerm.OperationCall)(using Γ: Context) extends IrError(Γ)
10595
case ExpectEffectInstance(badEffInstance: VTerm)(using Γ: Context) extends IrError(Γ)
96+
case ExpectEffectInstanceTypeSubsumption(sub: VTerm, sup: VTerm)(using Γ: Context) extends IrError(Γ)
10697
override def getMessage: String = verbosePPrinter.apply(this).plainText

src/main/scala/com/github/tgeng/archon/core/ir/reduction.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,9 @@ extension (v: VTerm)
557557
operands.map((k, v) => dfs(k.normalized, retainSimpleLinearOnly || v)).toSeq
558558
val newLiterals = (literalsAndOperands.flatMap { case (l, _) => l } ++ literal)
559559
.filter(effInstance =>
560-
val (qn, args) = inferType(effInstance)._1 match
560+
val (qn, args) = inferType(effInstance)._2 match
561561
case EffectInstanceType(eff, _) => eff
562-
case _ => throw IllegalStateException("bad type")
562+
case t => throw IllegalStateException(s"Expect EffectInstanceType but got $t")
563563
if retainSimpleLinearOnly then {
564564
val effect = Σ.getEffect(qn)
565565
effect.continuationUsage
@@ -634,9 +634,9 @@ given Reducible[CTerm] with
634634
(using signature: Signature)
635635
(using TypingContext)
636636
: CTerm =
637-
// TODO[P0]: if the reduced term contains `Continuation` or `HandlerKey`, then throw away the
638-
// reduced term and simply return t as it is. This is needed because `Continuation` is too hard
639-
// to be lowered.
637+
// TODO[P1]: if the reduced term contains `Continuation` or `EffectInstance`, then throw away
638+
// the reduced term and simply return t as it is. This is needed because `Continuation` is too
639+
// hard to be lowered.
640640
StackMachine(mutable.ArrayBuffer()).run(t).withSourceInfo(t.sourceInfo)
641641

642642
object Reducible:

src/main/scala/com/github/tgeng/archon/core/ir/subtyping.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.github.tgeng.archon.common.WrapPolicy.*
66
import com.github.tgeng.archon.core.common.*
77
import com.github.tgeng.archon.core.ir.CTerm.*
88
import com.github.tgeng.archon.core.ir.Declaration.*
9-
import com.github.tgeng.archon.core.ir.HandlerType.{Complex, Simple}
109
import com.github.tgeng.archon.core.ir.IrError.*
1110
import com.github.tgeng.archon.core.ir.PrettyPrinter.pprint
1211
import com.github.tgeng.archon.core.ir.ResolvedMetaVariable.*
@@ -28,7 +27,6 @@ def checkIsSubtype
2827
(using ctx: TypingContext)
2928
: Set[Constraint] = debugSubsumption("checkIsSubtype", sub, sup):
3029
check2(sub, sup):
31-
// TODO[P0]: add logic for EffectInstanceType
3230
case (sub, sup) if sub == sup => Set.empty
3331
case (sub: VTerm, u @ RUnsolved(_, _, constraint, _, _)) =>
3432
ctx.adaptForMetaVariable(u, sub):
@@ -96,6 +94,13 @@ def checkIsSubtype
9694
case _ => throw NotVSubtype(sub, sup)
9795
case (LevelType(ub1), LevelType(ub2)) =>
9896
if ub1 <= ub2 then Set.empty else throw NotVSubtype(sub, sup)
97+
case (
98+
EffectInstanceType((qn1, args1), handlerConstraint1),
99+
EffectInstanceType((qn2, args2), handlerConstraint2),
100+
) =>
101+
if handlerConstraint1 > handlerConstraint2 || qn1 != qn2 then
102+
throw ExpectEffectInstanceTypeSubsumption(sub, sup)
103+
else checkAreConvertible(args1, args2, Σ.getEffect(qn1).context.toList)
99104
case _ => checkIsConvertible(sub, sup, None)
100105

101106
/** Preconditions: sub and sup are both types

src/main/scala/com/github/tgeng/archon/core/ir/term.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,12 @@ enum VTerm(val sourceInfo: SourceInfo) extends SourceInfoOwner[VTerm]:
327327
(literal: LevelOrder, maxOperands: SeqMap[VTerm, /* level offset */ Nat])
328328
(using sourceInfo: SourceInfo) extends VTerm(sourceInfo)
329329

330-
// TODO[P0]: rename to EffectInstanceType
331330
case EffectInstanceType
332-
(effect: Eff, handlerConstraint: HandlerConstraint)
331+
(eff: Eff, handlerConstraint: HandlerConstraint)
333332
(using sourceInfo: SourceInfo) extends VTerm(sourceInfo)
334333

335-
// TODO[P0]: rename to EffectInstance
336334
case EffectInstance
337-
(effect: Eff, handlerConstraint: HandlerConstraint, handlerKey: HandlerKey = HandlerKey())
335+
(eff: Eff, handlerConstraint: HandlerConstraint, handlerKey: HandlerKey = HandlerKey())
338336
extends VTerm(SourceInfo.SiEmpty)
339337

340338
/** Automatically derived term, aka, `_` in Agda-like languages. During type checking, this is
@@ -455,7 +453,9 @@ object VTerm:
455453
val uAny: VTerm = VTerm.UsageLiteral(Usage.UAny)
456454
val div: EffectInstance =
457455
EffectInstance((Builtins.DivQn, Nil), HandlerConstraint(Usage.U0, HandlerType.Simple))
458-
val globalKeys: Set[EffectInstance] = Set(div)
456+
val ndet: EffectInstance =
457+
EffectInstance((Builtins.NdetQn, Nil), HandlerConstraint(Usage.U1, HandlerType.Simple))
458+
val globalKeys: Set[EffectInstance] = Set(div, ndet)
459459

460460
/** Marker of a computation that surely diverges. Computation with this effect will not be
461461
* executed by the type checker.

src/main/scala/com/github/tgeng/archon/core/ir/typing.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ def inferType
179179
case EffectInstanceType(eff, handlerConstraint) =>
180180
val newTm = EffectInstanceType(checkEff(eff), handlerConstraint)
181181
(newTm, Type(newTm))
182-
case EffectInstance(_, _, _) => ???
182+
case t @ EffectInstance(eff, handlerConstraint, _) =>
183+
// There is no need to check eff because EffectInstance is only created by reduction.
184+
(t, EffectInstanceType(eff, handlerConstraint))
183185
case LevelType(strictUpperBound) =>
184186
(LevelType(strictUpperBound), Type(LevelType(strictUpperBound)))
185187
case Level(literal, maxOperands) =>
@@ -440,7 +442,7 @@ def inferType
440442
val args = checkTypes(uncheckedArgs, record.context.map(_._1).toList)
441443
(RecordType(qn, args, effects), CType(tm, Total()))
442444
case OperationCall(effInstance, name, uncheckedArgs) =>
443-
val (qn, uncheckedTArgs) = inferType(effInstance)._1 match
445+
val (qn, uncheckedTArgs) = inferType(effInstance)._2 match
444446
case EffectInstanceType(eff, _) => eff
445447
case _ =>
446448
throw IllegalStateException(
@@ -511,6 +513,7 @@ def checkType
511513
(using Σ: Signature)
512514
(using ctx: TypingContext)
513515
: CTerm = debugCheckType(tm, ty):
516+
// TODO[P0]: using something of effect instance type but not as an effect instance should yield ndet effect.
514517
tm match
515518
case Force(v) =>
516519
val checkedV = checkType(v, U(ty))
@@ -943,7 +946,7 @@ private def getEffectsContinuationUsage
943946
val usage = ctx.withMetaResolved(effects.normalized):
944947
case Effects(effectInstances, operands) =>
945948
val literalUsages = effectInstances.map { effectInstance =>
946-
inferType(effectInstance)._1 match
949+
inferType(effectInstance)._2 match
947950
case EffectInstanceType(_, handlerConstraint) =>
948951
UsageLiteral(handlerConstraint.continuationUsage)
949952
case _ => throw IllegalStateException("type error")

src/main/scala/com/github/tgeng/archon/core/ir/usages.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def collectUsages
194194
collectArgsUsages(args, record.context.map(_._1).toList) +
195195
collectUsages(effects, Some(EffectsType()))
196196
case OperationCall(effectInstance, name, args) =>
197-
val (qn, tArgs) = inferType(effectInstance)._1 match
197+
val (qn, tArgs) = inferType(effectInstance)._2 match
198198
case EffectInstanceType(eff, _) => eff
199199
case _ =>
200200
throw IllegalStateException(

src/main/scala/com/github/tgeng/archon/core/ir/visitor.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ trait TermVisitor[C, R]:
121121
(using ctx: C)
122122
(using Σ: Signature)
123123
: R =
124-
visitEff(instanceType.effect)
124+
visitEff(instanceType.eff)
125125

126126
def visitEffectInstance(instance: VTerm.EffectInstance)(using ctx: C)(using Σ: Signature): R =
127-
visitEff(instance.effect)
127+
visitEff(instance.eff)
128128

129129
def visitAuto(auto: Auto)(using ctx: C)(using Σ: Signature): R = combine()
130130

@@ -655,13 +655,13 @@ trait Transformer[C]:
655655
(using Σ: Signature)
656656
: VTerm =
657657
EffectInstanceType(
658-
transformEff(instanceType.effect),
658+
transformEff(instanceType.eff),
659659
instanceType.handlerConstraint,
660660
)
661661

662662
def transformEffectInstance(instance: EffectInstance)(using ctx: C)(using Σ: Signature): VTerm =
663663
EffectInstance(
664-
transformEff(instance.effect),
664+
transformEff(instance.eff),
665665
instance.handlerConstraint,
666666
instance.handlerKey,
667667
)

src/test/resources/com/github/tgeng/archon/core/ir/testing/tterm/t_declaration_spec/record_stream/translation_output.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ List(
4848
bodyTy = RecordType(
4949
qn = qn"test.CStream",
5050
args = List(),
51-
effects = Effects(literal = Set(), unionOperands = SeqMap()) @ "ε"
51+
effects = Effects(handlerKeys = Set(), unionOperands = SeqMap()) @ "ε"
5252
) @ "CStream",
5353
effects = Collapse(cTm = Def(qn = qn"archon.builtin.effects.total") @ "ε") @ "ε"
5454
) @ "Eq Nat m (head self) -> CStream",

0 commit comments

Comments
 (0)