Skip to content

Commit

Permalink
Expected type should be TGradual so that partof returns "PartialMatch".
Browse files Browse the repository at this point in the history
  • Loading branch information
CuppoJava committed Mar 9, 2023
1 parent 1230757 commit 11f852d
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions compiler/type-gen-equations.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ defn gen-equations (c:TComm) -> TComm :
match(c) :

(c:TDef) :
val value-ctxt = ExpCtxt(or-top(type(c) as Type))
val value-ctxt = ExpCtxt(or-gradual(type(c) as Type))
val new-value = gen-equations(value(c), value-ctxt)
add-equal?(type(c) as Type, type!(new-value))
TDef(info(c), n(c), type(c), new-value)
Expand All @@ -335,7 +335,7 @@ defn gen-equations (c:TComm) -> TComm :

;If inferred types, then generate equations.
if inferred? :
val value-ctxt = ExpCtxt(TTop())
val value-ctxt = ExpCtxt(TGradual())
val new-value = gen-equations(value(c), value-ctxt)
val arity = length(ns(c))
for (v in types(c), i in 0 to false) do :
Expand All @@ -352,7 +352,7 @@ defn gen-equations (c:TComm) -> TComm :
(c:TDefVar) :
val new-value = match(value(c)) :
(v:TExp) :
val value-ctxt = ExpCtxt(or-top(type(c) as Type))
val value-ctxt = ExpCtxt(or-gradual(type(c) as Type))
val new-v = gen-equations(v, value-ctxt)
add-super?(type(c) as Type, type!(new-v))
new-v
Expand All @@ -365,7 +365,7 @@ defn gen-equations (c:TComm) -> TComm :
for (a in args(c), farg in a1(c)) do :
set-type(a, type-in-body(farg))
;Generate equations for body.
val body-ctxt = ExpCtxt(or-top(a2!(c)))
val body-ctxt = ExpCtxt(or-gradual(a2!(c)))
val new-body = gen-equations(body(c), body-ctxt)
add-equal?(a2!(c), type!(new-body))
sub-body(c, new-body)
Expand All @@ -374,7 +374,7 @@ defn gen-equations (c:TComm) -> TComm :
gen-method(c, None())

(c:TInit) :
val new-exp = gen-equations(exp(c), ExpCtxt(TTop()))
val new-exp = gen-equations(exp(c), ExpCtxt(TGradual()))
TInit(info(c), new-exp)

(c:TLDef) :
Expand Down Expand Up @@ -433,7 +433,7 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :

(e:TLet) :
val new-type = gen-var(n(e), ntype(e))
val value-ctxt = ExpCtxt(or-top(ntype(e)))
val value-ctxt = ExpCtxt(or-gradual(ntype(e)))
val new-value = gen-equations(value(e), value-ctxt)
add-equal?(new-type, type!(new-value))
val new-body = gen-equations(body(e), ctxt)
Expand All @@ -444,7 +444,7 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :
val new-type = gen-var(n(e), ntype(e))
val new-value = match(value(e)) :
(v:TExp) :
val value-ctxt = ExpCtxt(or-top(ntype(e)))
val value-ctxt = ExpCtxt(or-gradual(ntype(e)))
val new-v = gen-equations(v, value-ctxt)
add-super?(new-type, type!(new-v))
new-v
Expand All @@ -466,7 +466,7 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :
;If inferred types, then generate equations.
if inferred? :
val new-types = to-tuple(seq(gen-var, ns(e), ntypes(e))) as Tuple<TUVar>
val new-value = gen-equations(value(e), ExpCtxt(TTop()))
val new-value = gen-equations(value(e), ExpCtxt(TGradual()))
val new-body = gen-equations(body(e), ctxt)
val arity = length(ns(e))
for (v in new-types, i in 0 to false) do :
Expand Down Expand Up @@ -525,7 +525,7 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :
;Case: When return type is inferred, and the current context is ExpCtxt(T).
; Then context is ExpCtxt(R) where R is inferred from 'T'.
;Case: When return type is inferred, and the current context is CallCtxt.
; Then context is ExpCtxt(TTop()).
; Then context is ExpCtxt(expected(ctxt)).
val body-ctxt = match(a2(e), ctxt) :
(a2:Type, ctxt) :
ExpCtxt(a2)
Expand All @@ -552,14 +552,14 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :
TMulti(info(e), new-type, ctxt, new-funcs)

(e:TSeq) :
val new-a = gen-equations(a(e), ExpCtxt(TTop()))
val new-a = gen-equations(a(e), ExpCtxt(TGradual()))
val new-b = gen-equations(b(e), ctxt)
TSeq(info(e), type!(new-b), ctxt, new-a, new-b)

(e:TMatch) :
;Analyze the arguments first.
val new-args = for a in args(e) map :
gen-equations(a, ExpCtxt(TTop()))
gen-equations(a, ExpCtxt(TGradual()))

;For each branch, compute the body.
val new-branches = for b in branches(e) map :
Expand Down Expand Up @@ -613,7 +613,7 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :
r

(e:TCast) :
val new-exp = gen-equations(exp(e), ExpCtxt(TTop()))
val new-exp = gen-equations(exp(e), ExpCtxt(TGradual()))
TCast(info(e), targ(e), ctxt, new-exp, targ(e))

(e:TUpCast) :
Expand All @@ -622,10 +622,10 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :

(e:TSet) :
;Retrieve the ref of the variable.
val new-ref = annotate-type(ref(e), ExpCtxt(TTop()))
val new-ref = annotate-type(ref(e), ExpCtxt(TGradual()))

;Process the value.
val value-ctxt = ExpCtxt(or-top(type(new-ref)))
val value-ctxt = ExpCtxt(or-gradual(type(new-ref)))
val new-value = gen-equations(value(e), value-ctxt)

;Generate the super constraint.
Expand Down Expand Up @@ -669,7 +669,7 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :

(e:TPrim) :
val result-type = fresh-tuvar()
val new-args = map(gen-equations{_, ExpCtxt(TTop())}, args(e))
val new-args = map(gen-equations{_, ExpCtxt(TGradual())}, args(e))
add-equation $ PrimEqn(n(result-type), op(e), map(type!, new-args))
TPrim(info(e), result-type, ctxt, op(e), new-args)

Expand Down Expand Up @@ -706,7 +706,7 @@ defn gen-equations (e:TExp, ctxt:Context) -> TExp :
add-equation $ InferEqn(n(v), TupleLoc(arity,i), expected(ctxt), true)
map(ExpCtxt, vars)
(ctxt:CallCtxt) :
to-tuple(repeat(ExpCtxt(TTop()), arity))
to-tuple(repeat(ExpCtxt(TGradual()), arity))
;Generate equations for expressions.
val new-exps = to-tuple $
for (e in exps(e), ctxt in exp-ctxts) seq :
Expand Down Expand Up @@ -773,7 +773,7 @@ defn gen-method (m:TDefmethod, thist:Maybe<Type>) -> TDefmethod :
set-type(a, type-in-body(farg))

;Compute the body in the right context.
val body-ctxt = ExpCtxt(or-top(new-a2))
val body-ctxt = ExpCtxt(or-gradual(new-a2))
val new-body = gen-equations(body(m), body-ctxt)
add-equal?(new-a2, type!(new-body))

Expand Down Expand Up @@ -1046,7 +1046,7 @@ defn gen-equations (e:LSExp,
(ctxt:LSExpCtxt) :
ExpCtxt(hs-type-operation(ToHistanza(), expected(ctxt)))
(ctxt:LSBlankExpCtxt|LSBadCtxt) :
ExpCtxt(TTop())
ExpCtxt(TGradual())
(ctxt:LSAddrCtxt|LSCallCtxt) :
fatal("LSFn is not a valid expression within a %_ context." % [object-type(ctxt)])
val new-e = gen-function-reference(ref(e) as TRef|TMix, exp-ctxt)
Expand Down Expand Up @@ -1463,9 +1463,9 @@ defn unsatisfiable-svar () -> SVar :
defn unsatisfiable-uvart () -> UVarT :
UVarT(unsatisfiable-var())

;Return a gradual when false.
;Return a gradual when false or TUVar.
defn or-gradual (f:False) : TGradual()
defn or-gradual (t:Type) : t
defn or-gradual (t:Type) : TGradual() when t is TUVar else t
defn or-gradual (a:FArg<Type|False>) : sub-type(a, or-gradual(type(a)))

;Return a type variable when false.
Expand All @@ -1476,11 +1476,6 @@ defn or-var (t:Type) : t
defn or-lsvar (f:False) : fresh-uvart()
defn or-lsvar (t:LSType) : t

;Return a Top when false or TUVar.
;Used for generating contexts.
defn or-top (f:False) : TTop()
defn or-top (t:Type) : TTop() when t is TUVar else t

;Generate a type variable for 't', and also
;set it in the type environment.
defn gen-var (n:Int, t:Type|False) -> Type :
Expand Down

0 comments on commit 11f852d

Please sign in to comment.