Skip to content

Commit eddf4bf

Browse files
committed
[ refactor ] Use ConTag for represetaion constructor in case tree
1 parent 8791b40 commit eddf4bf

File tree

14 files changed

+178
-143
lines changed

14 files changed

+178
-143
lines changed

src/Compiler/CompileExpr.idr

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,27 +209,22 @@ mutual
209209
Name -> List (CaseAlt vars) ->
210210
Core (List (CConAlt vars))
211211
conCases n [] = pure []
212-
conCases {vars} n (ConCase x tag args sc :: ns)
213-
= do defs <- get Ctxt
214-
Just gdef <- lookupCtxtExact x (gamma defs)
212+
conCases {vars} n (ConCase tag args sc :: ns)
213+
= do let n = conName tag
214+
defs <- get Ctxt
215+
Just gdef <- lookupCtxtExact n (gamma defs)
215216
| Nothing => -- primitive type match
216-
do xn <- getFullName x
217+
do xn <- getFullName n
217218
pure $ MkConAlt xn TYCON Nothing args !(toCExpTree n sc)
218219
:: !(conCases n ns)
219220
case (definition gdef) of
220221
DCon _ arity (Just pos) => conCases n ns -- skip it
221-
_ => do xn <- getFullName x
222+
_ => do xn <- getFullName n
222223
let (args' ** sub)
223224
= mkDropSubst 0 (eraseArgs gdef) vars args
224225
sc' <- toCExpTree n sc
225226
ns' <- conCases n ns
226-
if dcon (definition gdef)
227-
then pure $ MkConAlt xn !(dconFlag xn) (Just tag) args' (shrinkCExp sub sc') :: ns'
228-
else pure $ MkConAlt xn !(dconFlag xn) Nothing args' (shrinkCExp sub sc') :: ns'
229-
where
230-
dcon : Def -> Bool
231-
dcon (DCon {}) = True
232-
dcon _ = False
227+
pure $ MkConAlt xn !(dconFlag xn) (tagOrder tag) args' (shrinkCExp sub sc') :: ns'
233228
conCases n (_ :: ns) = conCases n ns
234229

235230
constCases : {vars : _} ->
@@ -257,9 +252,9 @@ mutual
257252
getNewType fc scr n [] = pure Nothing
258253
getNewType fc scr n (DefaultCase sc :: ns)
259254
= pure $ Nothing
260-
getNewType {vars} fc scr n (ConCase x tag args sc :: ns)
255+
getNewType {vars} fc scr n (ConCase tag args sc :: ns)
261256
= do defs <- get Ctxt
262-
case !(lookupDefExact x (gamma defs)) of
257+
case !(lookupDefExact (conName tag) (gamma defs)) of
263258
-- If the flag is False, we still take the
264259
-- default, but need to evaluate the scrutinee of the
265260
-- case anyway - if the data structure contains a %World,
@@ -332,7 +327,7 @@ mutual
332327
{auto c : Ref Ctxt Defs} ->
333328
Name -> CaseTree vars ->
334329
Core (CExp vars)
335-
toCExpTree' n (Case _ x scTy alts@(ConCase _ _ _ _ :: _))
330+
toCExpTree' n (Case _ x scTy alts@(ConCase {} :: _))
336331
= let fc = getLoc scTy in
337332
do Nothing <- getNewType fc (CLocal fc x) n alts
338333
| Just def => pure def

src/Core/Binary.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import public Libraries.Utils.Binary
2525
||| version number if you're changing the version more than once in the same day.
2626
export
2727
ttcVersion : Int
28-
ttcVersion = 2025_12_06_00
28+
ttcVersion = 2025_12_08_00
2929

3030
export
3131
checkTTCVersion : String -> Int -> Int -> Core ()

src/Core/Case/CaseBuilder.idr

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import Libraries.Data.List.LengthMatch
2121
import Libraries.Data.List01
2222
import Libraries.Data.List01.Quantifiers
2323

24-
import Decidable.Equality
25-
2624
import Libraries.Text.PrettyPrint.Prettyprinter
2725

2826
%default covering
@@ -364,24 +362,14 @@ partition phase (x :: xs) with (partition phase xs)
364362
VarClause => VarClauses [x] NoClauses
365363

366364
data ConType : Type where
367-
CName : Name -> (tag : Int) -> ConType
365+
CName : ConTag -> ConType
368366
CDelay : ConType
369367
CConst : Constant -> ConType
370368

371-
conTypeEq : (x, y : ConType) -> Maybe (x = y)
372-
conTypeEq (CName x tag) (CName x' tag')
373-
= do Refl <- nameEq x x'
374-
case decEq tag tag' of
375-
Yes Refl => Just Refl
376-
No contra => Nothing
377-
conTypeEq CDelay CDelay = Just Refl
378-
conTypeEq (CConst x) (CConst y) = (\xy => cong CConst xy) <$> constantEq x y
379-
conTypeEq _ _ = Nothing
380-
381369
data Group : List Name -> -- pattern variables still to process
382370
Scoped where
383371
ConGroup : {newargs : _} ->
384-
Name -> (tag : Int) ->
372+
ConTag ->
385373
List01 True (PatClause (newargs ++ todo) (newargs ++ vars)) ->
386374
Group todo vars
387375
DelayGroup : {tyarg, valarg : _} ->
@@ -393,14 +381,14 @@ data Group : List Name -> -- pattern variables still to process
393381

394382
covering
395383
{vars : _} -> {todo : _} -> Show (Group todo vars) where
396-
show (ConGroup c t cs) = "Con " ++ show c ++ ": " ++ show cs
384+
show (ConGroup tag cs) = "Con " ++ show (conName tag) ++ ": " ++ show cs
397385
show (DelayGroup cs) = "Delay: " ++ show cs
398386
show (ConstGroup c cs) = "Const " ++ show c ++ ": " ++ show cs
399387

400388
data GroupMatch : ConType -> List Pat -> Group todo vars -> Type where
401-
ConMatch : {tag : Int} -> LengthMatch ps newargs ->
402-
GroupMatch (CName n tag) ps
403-
(ConGroup {newargs} n tag (MkPatClause pvs pats pid rhs :: rest))
389+
ConMatch : {tag : ConTag} -> LengthMatch ps newargs ->
390+
GroupMatch (CName tag) ps
391+
(ConGroup {newargs} tag (MkPatClause pvs pats pid rhs :: rest))
404392
DelayMatch : GroupMatch CDelay []
405393
(DelayGroup {tyarg} {valarg} (MkPatClause pvs pats pid rhs :: rest))
406394
ConstMatch : GroupMatch (CConst c) []
@@ -409,13 +397,13 @@ data GroupMatch : ConType -> List Pat -> Group todo vars -> Type where
409397

410398
checkGroupMatch : (c : ConType) -> (ps : List Pat) -> (g : Group todo vars) ->
411399
GroupMatch c ps g
412-
checkGroupMatch (CName x tag) ps (ConGroup {newargs} x' tag' (MkPatClause pvs pats pid rhs :: rest))
400+
checkGroupMatch (CName tag) ps (ConGroup {newargs} tag' (MkPatClause pvs pats pid rhs :: rest))
413401
= case checkLengthMatch ps newargs of
414402
Nothing => NoMatch
415-
Just prf => case (nameEq x x', decEq tag tag') of
416-
(Just Refl, Yes Refl) => ConMatch prf
403+
Just prf => case conTagEq tag tag' of
404+
Just Refl => ConMatch prf
417405
_ => NoMatch
418-
checkGroupMatch (CName x tag) ps _ = NoMatch
406+
checkGroupMatch (CName tag) ps _ = NoMatch
419407
checkGroupMatch CDelay [] (DelayGroup (MkPatClause pvs pats pid rhs :: rest))
420408
= DelayMatch
421409
checkGroupMatch (CConst c) [] (ConstGroup c' (MkPatClause pvs pats pid rhs :: rest))
@@ -511,7 +499,7 @@ groupCons fc fn pvars (x :: xs) {isCons = p :: ps}
511499
= foldlC (uncurry . gc) !(gc [] x p) $ pushIn xs ps
512500
where
513501
addConG : {vars', todo' : _} ->
514-
Name -> (tag : Int) ->
502+
ConTag ->
515503
List Pat -> NamedPats todo' vars' ->
516504
Int -> (rhs : Term vars') ->
517505
(acc : List01 ne (Group todo' vars')) ->
@@ -520,13 +508,13 @@ groupCons fc fn pvars (x :: xs) {isCons = p :: ps}
520508
-- add new pattern arguments for each of that constructor's arguments.
521509
-- The type of 'ConGroup' ensures that we refer to the arguments by
522510
-- the same name in each of the clauses
523-
addConG n tag pargs pats pid rhs []
524-
= do cty <- if n == UN (Basic "->")
511+
addConG tag pargs pats pid rhs []
512+
= do cty <- if tag == TConTag (UN $ Basic "->")
525513
then pure $ NBind fc (MN "_" 0) (Pi fc top Explicit (MkNFClosure defaultOpts (mkEnv fc vars') (NType fc (MN "top" 0)))) $
526514
(\d, a => pure $ NBind fc (MN "_" 1) (Pi fc top Explicit (MkNFClosure defaultOpts (mkEnv fc vars') (NErased fc Placeholder)))
527515
(\d, a => pure $ NType fc (MN "top" 0)))
528516
else do defs <- get Ctxt
529-
Just t <- lookupTyExact n (gamma defs)
517+
Just t <- lookupTyExact (conName tag) (gamma defs)
530518
| Nothing => pure (NErased fc Placeholder)
531519
nf defs (mkEnv fc vars') (embed t)
532520
(patnames ** (l, newargs)) <- nextNames fc "e" pargs (Just cty)
@@ -535,20 +523,20 @@ groupCons fc fn pvars (x :: xs) {isCons = p :: ps}
535523
let pats' = updatePatNames (updateNames (zip patnames pargs))
536524
(weakenNs l pats)
537525
let clause = MkPatClause pvars (newargs ++ pats') pid (weakenNs l rhs)
538-
pure [ConGroup n tag [clause]]
539-
addConG n tag pargs pats pid rhs (g :: gs) with (checkGroupMatch (CName n tag) pargs g)
540-
addConG n tag pargs pats pid rhs
541-
(ConGroup n tag (MkPatClause pvars ps tid tm :: rest) :: gs) | ConMatch {newargs} lprf
526+
pure [ConGroup tag [clause]]
527+
addConG tag pargs pats pid rhs (g :: gs) with (checkGroupMatch (CName tag) pargs g)
528+
addConG tag pargs pats pid rhs
529+
(ConGroup tag (MkPatClause pvars ps tid tm :: rest) :: gs) | ConMatch {newargs} lprf
542530
= do let newps = newPats pargs lprf ps
543531
let l = mkSizeOf newargs
544532
let pats' = updatePatNames (updateNames (zip newargs pargs))
545533
(weakenNs l pats)
546534
let newclause = MkPatClause pvars (newps ++ pats') pid (weakenNs l rhs)
547535
-- put the new clause at the end of the group, since we
548536
-- match the clauses top to bottom.
549-
pure $ ConGroup n tag (MkPatClause pvars ps tid tm :: rest ++ [newclause]) :: gs
550-
addConG n tag pargs pats pid rhs (g :: gs) | NoMatch
551-
= (g ::) <$> addConG n tag pargs pats pid rhs gs
537+
pure $ ConGroup tag (MkPatClause pvars ps tid tm :: rest ++ [newclause]) :: gs
538+
addConG tag pargs pats pid rhs (g :: gs) | NoMatch
539+
= (g ::) <$> addConG tag pargs pats pid rhs gs
552540

553541
-- This rather ugly special case is to deal with laziness, where Delay
554542
-- is like a constructor, but with a special meaning that it forces
@@ -613,14 +601,14 @@ groupCons fc fn pvars (x :: xs) {isCons = p :: ps}
613601
= addGroup p pprf pats pid (substName n (Local fc (Just True) idx pprf) rhs) acc
614602
addGroup (PCon cfc n t a pargs) pprf pats pid rhs acc
615603
= if a == length pargs
616-
then addConG n t pargs pats pid rhs acc
604+
then addConG (DConTag n t) pargs pats pid rhs acc
617605
else throw (CaseCompile cfc fn (NotFullyApplied n))
618606
addGroup (PTyCon cfc n a pargs) pprf pats pid rhs acc
619607
= if a == length pargs
620-
then addConG n 0 pargs pats pid rhs acc
608+
then addConG (TConTag n) pargs pats pid rhs acc
621609
else throw (CaseCompile cfc fn (NotFullyApplied n))
622610
addGroup (PArrow _ _ s t) pprf pats pid rhs acc
623-
= addConG (UN $ Basic "->") 0 [s, t] pats pid rhs acc
611+
= addConG (TConTag $ UN $ Basic "->") [s, t] pats pid rhs acc
624612
-- Go inside the delay; we'll flag the case as needing to force its
625613
-- scrutinee (need to check in 'caseGroups below)
626614
addGroup (PDelay _ _ pty parg) pprf pats pid rhs acc
@@ -919,10 +907,10 @@ mutual
919907
where
920908
altGroups : forall ne. List01 ne (Group todo vars) -> Core (List (CaseAlt vars))
921909
altGroups [] = pure $ toList $ DefaultCase <$> errorCase
922-
altGroups (ConGroup {newargs} cn tag rest :: cs)
910+
altGroups (ConGroup {newargs} tag rest :: cs)
923911
= do crest <- match fc fn phase rest (map (weakenNs (mkSizeOf newargs)) errorCase)
924912
cs' <- altGroups cs
925-
pure (ConCase cn tag newargs crest :: cs')
913+
pure (ConCase tag newargs crest :: cs')
926914
altGroups (DelayGroup {tyarg} {valarg} rest :: cs)
927915
= do crest <- match fc fn phase rest (map (weakenNs (mkSizeOf [tyarg, valarg])) errorCase)
928916
cs' <- altGroups cs
@@ -1150,7 +1138,7 @@ simpleCase fc phase fn ty clauses
11501138

11511139
mutual
11521140
findReachedAlts : CaseAlt ns' -> List Int
1153-
findReachedAlts (ConCase _ _ _ t) = findReached t
1141+
findReachedAlts (ConCase _ _ t) = findReached t
11541142
findReachedAlts (DelayCase _ _ t) = findReached t
11551143
findReachedAlts (ConstCase _ t) = findReached t
11561144
findReachedAlts (DefaultCase t) = findReached t
@@ -1195,7 +1183,7 @@ identifyUnreachableDefaults fc defs nfty cs
11951183

11961184
dropRep : List (CaseAlt vars) -> SortedSet Int -> (List (CaseAlt vars), SortedSet Int)
11971185
dropRep [] extra = ([], extra)
1198-
dropRep (c@(ConCase n t args sc) :: rest) extra
1186+
dropRep (c@(ConCase t args sc) :: rest) extra
11991187
-- assumption is that there's no defaultcase in 'rest' because
12001188
-- we've just removed it
12011189
= let (filteredClauses, extraCases) = partition (not . tagIs t) rest
@@ -1225,11 +1213,11 @@ findExtraDefaults fc defs (Case idx el ty altsIn)
12251213
pure (Prelude.toList extraCases ++ extraCases')
12261214
where
12271215
findExtraAlts : CaseAlt vars -> Core (List Int)
1228-
findExtraAlts (ConCase x tag args ctree) = findExtraDefaults fc defs ctree
1229-
findExtraAlts (DelayCase x arg ctree) = findExtraDefaults fc defs ctree
1230-
findExtraAlts (ConstCase x ctree) = findExtraDefaults fc defs ctree
1216+
findExtraAlts (ConCase _ _ ctree) = findExtraDefaults fc defs ctree
1217+
findExtraAlts (DelayCase _ _ ctree) = findExtraDefaults fc defs ctree
1218+
findExtraAlts (ConstCase _ ctree) = findExtraDefaults fc defs ctree
12311219
-- already handled defaults by elaborating them to all possible cons
1232-
findExtraAlts (DefaultCase ctree) = pure []
1220+
findExtraAlts (DefaultCase _) = pure []
12331221

12341222
findExtraDefaults fc defs ctree = pure []
12351223

src/Core/Case/CaseTree.idr

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,40 @@ import Libraries.Data.NameMap
1111
import Libraries.Text.PrettyPrint.Prettyprinter
1212
import Libraries.Data.List.SizeOf
1313

14+
import Decidable.Equality
15+
1416
%default covering
1517

18+
public export
19+
data ConTag = DConTag Name Int
20+
| TConTag Name
21+
22+
export
23+
conName : ConTag -> Name
24+
conName (DConTag n _) = n
25+
conName (TConTag n) = n
26+
27+
export
28+
tagOrder : ConTag -> Maybe Int
29+
tagOrder (DConTag _ i) = Just i
30+
tagOrder (TConTag {}) = Nothing
31+
32+
export
33+
conTagEq : (x, y : ConTag) -> Maybe (x = y)
34+
conTagEq (DConTag n i) (DConTag n' i')
35+
= do Refl <- nameEq n n'
36+
case decEq i i' of
37+
Yes Refl => Just Refl
38+
No contra => Nothing
39+
conTagEq (TConTag n) (TConTag n') = (\xy => cong TConTag xy) <$> nameEq n n'
40+
conTagEq _ _ = Nothing
41+
42+
export
43+
Eq ConTag where
44+
DConTag _ i == DConTag _ i' = i == i'
45+
TConTag n == TConTag n' = n == n'
46+
_ == _ = False
47+
1648
mutual
1749
||| Case trees in A-normal forms
1850
||| i.e. we may only dispatch on variables, not expressions
@@ -37,7 +69,7 @@ mutual
3769
public export
3870
data CaseAlt : Scoped where
3971
||| Constructor for a data type; bind the arguments and subterms.
40-
ConCase : Name -> (tag : Int) -> (args : List Name) ->
72+
ConCase : ConTag -> (args : List Name) ->
4173
CaseTree (Scope.addInner vars args) -> CaseAlt vars
4274
||| Lazy match for the Delay type use for codata types
4375
DelayCase : (ty : Name) -> (arg : Name) ->
@@ -60,7 +92,7 @@ mutual
6092
measure Impossible = 0
6193

6294
measureAlts : CaseAlt vars -> Nat
63-
measureAlts (ConCase x tag args y) = 1 + (measure y)
95+
measureAlts (ConCase tag args y) = 1 + (measure y)
6496
measureAlts (DelayCase ty arg x) = 1 + (measure x)
6597
measureAlts (ConstCase x y) = 1 + (measure y)
6698
measureAlts (DefaultCase x) = 1 + (measure x)
@@ -85,12 +117,12 @@ mutual
85117

86118
export
87119
StripNamespace (CaseAlt vars) where
88-
trimNS ns (ConCase x tag args t) = ConCase x tag args (trimNS ns t)
120+
trimNS ns (ConCase tag args t) = ConCase tag args (trimNS ns t)
89121
trimNS ns (DelayCase ty arg t) = DelayCase ty arg (trimNS ns t)
90122
trimNS ns (ConstCase x t) = ConstCase x (trimNS ns t)
91123
trimNS ns (DefaultCase t) = DefaultCase (trimNS ns t)
92124

93-
restoreNS ns (ConCase x tag args t) = ConCase x tag args (restoreNS ns t)
125+
restoreNS ns (ConCase tag args t) = ConCase tag args (restoreNS ns t)
94126
restoreNS ns (DelayCase ty arg t) = DelayCase ty arg (restoreNS ns t)
95127
restoreNS ns (ConstCase x t) = ConstCase x (restoreNS ns t)
96128
restoreNS ns (DefaultCase t) = DefaultCase (restoreNS ns t)
@@ -141,8 +173,8 @@ showCT indent (STerm i tm) = "[" ++ show i ++ "] " ++ show tm
141173
showCT indent (Unmatched msg) = "Error: " ++ show msg
142174
showCT indent Impossible = "Impossible"
143175

144-
showCA indent (ConCase n tag args sc)
145-
= showSep " " (map show (n :: args)) ++ " => " ++
176+
showCA indent (ConCase tag args sc)
177+
= showSep " " (map show (conName tag :: args)) ++ " => " ++
146178
showCT indent sc
147179
showCA indent (DelayCase _ arg sc)
148180
= "Delay " ++ show arg ++ " => " ++ showCT indent sc
@@ -174,8 +206,8 @@ mutual
174206
eqTree _ _ = False
175207

176208
eqAlt : CaseAlt vs -> CaseAlt vs' -> Bool
177-
eqAlt (ConCase n t args tree) (ConCase n' t' args' tree')
178-
= n == n' && eqTree tree tree' -- assume arities match, since name does
209+
eqAlt (ConCase t args tree) (ConCase t' args' tree')
210+
= t == t' && eqTree tree tree' -- assume arities match, since name does
179211
eqAlt (DelayCase _ _ tree) (DelayCase _ _ tree')
180212
= eqTree tree tree'
181213
eqAlt (ConstCase c tree) (ConstCase c' tree')
@@ -227,8 +259,8 @@ mutual
227259
SizeOf ns ->
228260
CaseAlt (outer ++ inner) ->
229261
CaseAlt (outer ++ (ns ++ inner))
230-
insertCaseAltNames p q (ConCase x tag args ct)
231-
= ConCase x tag args
262+
insertCaseAltNames p q (ConCase tag args ct)
263+
= ConCase tag args
232264
(rewrite appendAssociative args outer (ns ++ inner) in
233265
insertCaseNames (mkSizeOf args + p) q {inner}
234266
(rewrite sym (appendAssociative args outer inner) in
@@ -252,7 +284,7 @@ getNames add ns sc = getSet ns sc
252284
where
253285
mutual
254286
getAltSet : NameMap Bool -> CaseAlt vs -> NameMap Bool
255-
getAltSet ns (ConCase n t args sc) = getSet ns sc
287+
getAltSet ns (ConCase t args sc) = getSet ns sc
256288
getAltSet ns (DelayCase t a sc) = getSet ns sc
257289
getAltSet ns (ConstCase i sc) = getSet ns sc
258290
getAltSet ns (DefaultCase sc) = getSet ns sc

src/Core/Case/CaseTree/Pretty.idr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ namespace Raw
2626
prettyTree (Unmatched msg) = "Error:" <++> pretty0 msg
2727
prettyTree Impossible = "Impossible"
2828

29-
prettyAlt (ConCase n tag args sc)
30-
= hsep (annotate (DCon (Just n)) (pretty0 n) :: map pretty0 args)
29+
prettyAlt (ConCase tag args sc)
30+
= let n = conName tag in
31+
hsep (annotate (DCon (Just n)) (pretty0 n) :: map pretty0 args)
3132
<++> fatArrow
3233
<+> let sc = prettyTree sc in
3334
Union (spaces 1 <+> sc) (nest 2 (hardline <+> sc))
@@ -78,7 +79,8 @@ namespace Resugared
7879
prettyTree env (Unmatched msg) = pure ("Error:" <++> pretty0 msg)
7980
prettyTree env Impossible = pure "Impossible"
8081

81-
prettyAlt env (ConCase n tag args sc) = do
82+
prettyAlt env (ConCase tag args sc) = do
83+
let n = conName tag
8284
con <- prettyName n
8385
sc <- prettyTree (mkEnvOnto emptyFC args env) sc
8486
pure $ hsep (annotate (DCon (Just n)) con :: map pretty0 args)

0 commit comments

Comments
 (0)