Skip to content

Commit 0e5c797

Browse files
committed
Preserve more type annotations in shallow.mc
1 parent b4a24d6 commit 0e5c797

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/stdlib/mexpr/shallow-patterns.mc

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ lang ShallowBase = Ast + NamedPat
281281
-> Expr
282282
-> Expr
283283
sem lowerToExpr scrutinee branches = | fallthrough ->
284-
-- TODO(vipa, 2022-08-12): Deduplicate the branches, put them in let-expressions before
285284
match
286285
mapAccumL
287286
(lam acc. lam branch. match branch with (pat, expr) in
@@ -292,12 +291,13 @@ lang ShallowBase = Ast + NamedPat
292291
errorSingle [info] (join ["Inconsistent pattern; '", nameGetStr name, "' is not always bound."]) in
293292
let callF = lam nameMap.
294293
let lookup = lam n. mapLookupOrElse (lam. inconsistentError (infoPat pat) n) n nameMap in
295-
app_
296-
(appSeq_
297-
(nvar_ fName)
298-
(map
299-
(lam n. nvar_ (lookup n)) names))
300-
unit_ in
294+
withType (tyTm expr)
295+
(app_
296+
(appSeq_
297+
(nvar_ fName)
298+
(map
299+
(lam n. nvar_ (lookup n)) names))
300+
unit_) in
301301
(acc, (pat, callF)))
302302
[]
303303
branches
@@ -443,7 +443,8 @@ lang ShallowInt = ShallowBase + IntPat
443443
| PatInt x -> _ssingleton (SPatInt {val = x.val, info = x.info})
444444

445445
sem mkMatch scrutinee t e =
446-
| SPatInt i -> match_ (nvar_ scrutinee) (withTypePat tyint_ (withInfoPat i.info (pint_ i.val))) t e
446+
| SPatInt i ->
447+
withType (tyTm t) (match_ (nvar_ scrutinee) (withTypePat tyint_ (withInfoPat i.info (pint_ i.val))) t e)
447448

448449
sem shallowCmp =
449450
| (SPatInt l, SPatInt r) -> subi l.val r.val
@@ -464,7 +465,8 @@ lang ShallowChar = ShallowBase + CharPat
464465
| PatChar x -> _ssingleton (SPatChar {val = x.val, info = x.info})
465466

466467
sem mkMatch scrutinee t e =
467-
| SPatChar v -> match_ (nvar_ scrutinee) (withTypePat tychar_ (withInfoPat v.info (pchar_ v.val))) t e
468+
| SPatChar v ->
469+
withType (tyTm t) (match_ (nvar_ scrutinee) (withTypePat tychar_ (withInfoPat v.info (pchar_ v.val))) t e)
468470

469471
sem shallowCmp =
470472
| (SPatChar l, SPatChar r) -> cmpChar l.val r.val
@@ -485,7 +487,8 @@ lang ShallowBool = ShallowBase + BoolPat
485487
| PatBool x -> _ssingleton (SPatBool {val = x.val, info = x.info})
486488

487489
sem mkMatch scrutinee t e =
488-
| SPatBool v -> match_ (nvar_ scrutinee) (withTypePat tybool_ (withInfoPat v.info (pbool_ v.val))) t e
490+
| SPatBool v ->
491+
withType (tyTm t) (match_ (nvar_ scrutinee) (withTypePat tybool_ (withInfoPat v.info (pbool_ v.val))) t e)
489492

490493
sem shallowCmp =
491494
| (SPatBool {val = true}, SPatBool {val = true}) -> 0
@@ -520,7 +523,7 @@ lang ShallowRecord = ShallowBase + RecordPat + RecordTypeAst + PrettyPrint
520523
, ty = x.ty
521524
, info = x.info
522525
} in
523-
withInfo x.info (match_ (nvar_ scrutinee) pat t never_)
526+
withInfo x.info (withType (tyTm t) (match_ (nvar_ scrutinee) pat t (withType (tyTm t) never_)))
524527

525528
sem shallowIsInfallible =
526529
| SPatRecord _ -> true
@@ -539,7 +542,7 @@ let _getSliceName
539542
modref slices (mapInsert margins name (deref slices));
540543
name
541544

542-
lang ShallowSeq = ShallowBase + SeqTotPat + SeqEdgePat
545+
lang ShallowSeq = ShallowBase + SeqTotPat + SeqEdgePat + SeqTypeAst
543546
syn SPat =
544547
| SPatSeqTot {elements : [Name], slices : Ref (Map (Int, Int) Name), ty : Type, info : Info}
545548
-- NOTE(vipa, 2022-05-26): The translation strategy used matches
@@ -667,12 +670,14 @@ lang ShallowSeq = ShallowBase + SeqTotPat + SeqEdgePat
667670
subsequence_ (nvar_ scrutinee) (int_ n) (int_ (subi (length x.elements) (addi n m)))
668671
end
669672
in nulet_ name expr) in
670-
match_ (nvar_ scrutinee)
671-
(withInfoPat x.info (withTypePat x.ty (pseqtot_ (map npvar_ x.elements))))
672-
(bindall_ slices t)
673-
e
673+
withType (tyTm t)
674+
(match_ (nvar_ scrutinee)
675+
(withInfoPat x.info (withTypePat x.ty (pseqtot_ (map npvar_ x.elements))))
676+
(bindall_ slices t)
677+
e)
674678
| SPatSeqGE x ->
675-
let letFrom_ = lam n. lam i. nulet_ n (get_ (nvar_ scrutinee) i) in
679+
match unwrapType x.ty with TySeq {ty = elemTy} in
680+
let letFrom_ = lam n. lam i. nulet_ n (withType elemTy (get_ (nvar_ scrutinee) i)) in
676681
let pres = mapi
677682
(lam i. lam n. letFrom_ n (int_ i))
678683
(deref x.prefix) in
@@ -703,9 +708,10 @@ lang ShallowSeq = ShallowBase + SeqTotPat + SeqEdgePat
703708
end
704709
in nulet_ name expr) in
705710
let len = if deref needLen then [nulet_ lenName (length_ (nvar_ scrutinee))] else [] in
706-
match_ (nvar_ scrutinee) (withInfoPat x.info (withTypePat x.ty (pseqedgew_ (make x.minLength pvarw_) [])))
707-
(bindall_ (join [pres, len, slices, posts]) t)
708-
e
711+
withType (tyTm t)
712+
(match_ (nvar_ scrutinee) (withInfoPat x.info (withTypePat x.ty (pseqedgew_ (make x.minLength pvarw_) [])))
713+
(bindall_ (join [pres, len, slices, posts]) t)
714+
e)
709715

710716
sem shallowIsInfallible =
711717
| SPatSeqGE x -> eqi x.minLength 0
@@ -729,9 +735,10 @@ lang ShallowCon = ShallowBase + DataPat
729735
| PatCon x -> _ssingleton (SPatCon {conName = x.ident, subName = nameSym "carried", ty = x.ty, info = x.info})
730736

731737
sem mkMatch scrutinee t e =
732-
| SPatCon x -> match_ (nvar_ scrutinee)
733-
(withTypePat x.ty (withInfoPat x.info (npcon_ x.conName (npvar_ x.subName))))
734-
t e
738+
| SPatCon x -> withType (tyTm t)
739+
(match_ (nvar_ scrutinee)
740+
(withTypePat x.ty (withInfoPat x.info (npcon_ x.conName (npvar_ x.subName))))
741+
t e)
735742

736743
sem shallowCmp =
737744
| (SPatCon l, SPatCon r) -> nameCmp l.conName r.conName

0 commit comments

Comments
 (0)