Skip to content

Commit 704a252

Browse files
fabianhjrgallais
authored andcommitted
[ fix #55 ] Propagate linear context from Definition to Clauses
1 parent d9318a2 commit 704a252

File tree

8 files changed

+47
-9
lines changed

8 files changed

+47
-9
lines changed

src/TTImp/Elab/Local.idr

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,25 @@ localHelper {vars} nest env nestdecls_in func
3333
= do est <- get EST
3434
let f = defining est
3535
defs <- get Ctxt
36-
let vis = case !(lookupCtxtExact (Resolved (defining est)) (gamma defs)) of
37-
Just gdef => visibility gdef
38-
Nothing => Public
36+
gdef <- lookupCtxtExact (Resolved (defining est)) (gamma defs)
37+
let vis = maybe Public visibility gdef
38+
let mult = maybe linear GlobalDef.multiplicity gdef
39+
3940
-- If the parent function is public, the nested definitions need
4041
-- to be public too
41-
let nestdecls =
42+
let nestdeclsVis =
4243
if vis == Public
4344
then map setPublic nestdecls_in
4445
else nestdecls_in
4546

46-
let defNames = definedInBlock emptyNS nestdecls
47+
-- If the parent function is erased, then the nested definitions
48+
-- will be erased too
49+
let nestdeclsMult =
50+
if mult == erased
51+
then map setErased nestdeclsVis
52+
else nestdeclsVis
53+
54+
let defNames = definedInBlock emptyNS nestdeclsMult
4755
names' <- traverse (applyEnv f)
4856
(nub defNames) -- binding names must be unique
4957
-- fixes bug #115
@@ -60,7 +68,7 @@ localHelper {vars} nest env nestdecls_in func
6068
-- everything
6169
let oldhints = localHints defs
6270

63-
let nestdecls = map (updateName nest') nestdecls
71+
let nestdecls = map (updateName nest') nestdeclsMult
6472
log "elab.def.local" 20 $ show nestdecls
6573

6674
traverse_ (processDecl [] nest' env') nestdecls
@@ -134,6 +142,14 @@ localHelper {vars} nest env nestdecls_in func
134142
= INamespace fc ps (map setPublic decls)
135143
setPublic d = d
136144

145+
setErased : ImpDecl -> ImpDecl
146+
setErased (IClaim fc _ v opts ty) = IClaim fc erased v opts ty
147+
setErased (IParameters fc ps decls)
148+
= IParameters fc ps (map setErased decls)
149+
setErased (INamespace fc ps decls)
150+
= INamespace fc ps (map setErased decls)
151+
setErased d = d
152+
137153
export
138154
checkLocal : {vars : _} ->
139155
{auto c : Ref Ctxt Defs} ->

src/TTImp/ProcessDef.idr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ processDef opts nest env fc n_in cs_in
854854

855855
-- Dynamically rebind default totality requirement to this function's totality requirement
856856
-- and use this requirement when processing `with` blocks
857+
log "declare.def" 5 $ "Traversing clauses of " ++ show n ++ " with mult " ++ show mult
857858
let treq = fromMaybe !getDefaultTotalityOption (findSetTotal (flags gdef))
858859
cs <- withTotality treq $
859860
traverse (checkClause mult (visibility gdef) treq

src/TTImp/ProcessType.idr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import TTImp.TTImp
1919
import TTImp.Utils
2020

2121
import Data.List
22+
import Data.Strings
2223
import Libraries.Data.NameMap
2324

2425
%default covering
@@ -267,7 +268,7 @@ processType {vars} eopts nest env fc rig vis opts (MkImpTy tfc nameFC n_in ty_ra
267268
addNameLoc nameFC n
268269

269270
log "declare.type" 1 $ "Processing " ++ show n
270-
log "declare.type" 5 $ "Checking type decl " ++ show n ++ " : " ++ show ty_raw
271+
log "declare.type" 5 $ unwords ["Checking type decl:", show rig, show n, ":", show ty_raw]
271272
idx <- resolveName n
272273

273274
-- Check 'n' is undefined

src/TTImp/TTImp.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ mutual
360360

361361
export
362362
Show ImpDecl where
363-
show (IClaim _ _ _ opts ty) = show opts ++ " " ++ show ty
363+
show (IClaim _ c _ opts ty) = show opts ++ " " ++ show c ++ " " ++ show ty
364364
show (IData _ _ d) = show d
365365
show (IDef _ n cs) = "(%def " ++ show n ++ " " ++ show cs ++ ")"
366366
show (IParameters _ ps ds)

tests/Main.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ idrisTestsLinear = MkTestPool "Quantities" []
107107
["linear001", "linear002", "linear003", -- "linear004" -- disabled due to requiring linearity subtyping
108108
"linear005", "linear006", "linear007", "linear008",
109109
"linear009", "linear010", "linear011", "linear012",
110-
"linear013"]
110+
"linear013", "linear014"]
111111

112112
idrisTestsLiterate : TestPool
113113
idrisTestsLiterate = MkTestPool "Literate programming" []

tests/idris2/linear014/Issue55.idr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
0
2+
foo : (0 b : Bool) -> Bool
3+
foo b = b
4+
5+
0
6+
bar : Bool
7+
bar = q
8+
where
9+
q : Bool
10+
q = foo True
11+
12+
0
13+
baz : Bool
14+
baz = let q : Bool
15+
q = foo True in
16+
q

tests/idris2/linear014/expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1/1: Building Issue55 (Issue55.idr)

tests/idris2/linear014/run

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$1 -c Issue55.idr
2+
3+
rm -r build

0 commit comments

Comments
 (0)