@@ -252,7 +252,7 @@ elab ist info emode opts fn tm
252 ptm <- get_term
252 ptm <- get_term
253 when pattern $
253 when pattern $
254 -- Look for Rig1 (linear) pattern bindings
254 -- Look for Rig1 (linear) pattern bindings
255− do let pnms = findLinear ist [] ptm
255+ do let pnms = findLinear Rig1 ist [] ptm
256 update_term (setLinear pnms)
256 update_term (setLinear pnms)
258 pattern = emode == ELHS || emode == EImpossible
258 pattern = emode == ELHS || emode == EImpossible
@@ -695,7 +695,7 @@ elab ist info emode opts fn tm
695 elabE ec' fc sc
695 elabE ec' fc sc
697 highlightSource nfc (AnnBoundName n False)
697 highlightSource nfc (AnnBoundName n False)
698− elab' ina _ tm@(PLet fc n nfc ty val sc)
698+ elab' ina _ tm@(PLet fc rig n nfc ty val sc)
699 = do attack
699 = do attack
700 ivs <- get_implementations
700 ivs <- get_implementations
701 tyn <- getNameFrom (sMN 0 "letty")
701 tyn <- getNameFrom (sMN 0 "letty")
@@ -703,7 +703,7 @@ elab ist info emode opts fn tm
703 valn <- getNameFrom (sMN 0 "letval")
703 valn <- getNameFrom (sMN 0 "letval")
704 claim valn (Var tyn)
704 claim valn (Var tyn)
705 explicit valn
705 explicit valn
706− letbind n (Var tyn) (Var valn)
706+ letbind n rig (Var tyn) (Var valn)
707 addPSname n
707 addPSname n
708 case ty of
708 case ty of
709 Placeholder -> return ()
709 Placeholder -> return ()
@@ -730,7 +730,7 @@ elab ist info emode opts fn tm
730 -- HACK: If the name leaks into its type, it may leak out of
730 -- HACK: If the name leaks into its type, it may leak out of
731 -- scope outside, so substitute in the outer scope.
731 -- scope outside, so substitute in the outer scope.
732 expandLet n (case lookupBinder n env of
732 expandLet n (case lookupBinder n env of
733− Just (Let t v) -> v
733+ Just (Let rig t v) -> v
734 other -> error ("Value not a let binding: " ++ show other))
734 other -> error ("Value not a let binding: " ++ show other))
736 highlightSource nfc (AnnBoundName n False)
736 highlightSource nfc (AnnBoundName n False)
@@ -741,7 +741,7 @@ elab ist info emode opts fn tm
741 claim tyn RType
741 claim tyn RType
742 valn <- getNameFrom (sMN 0 "letval")
742 valn <- getNameFrom (sMN 0 "letval")
743 claim valn (Var tyn)
743 claim valn (Var tyn)
744− letbind n (Var tyn) (Var valn)
744+ letbind n RigW (Var tyn) (Var valn)
745 focus valn
745 focus valn
746 elabE (ina { e_inarg = True, e_intype = True }) (Just fc) (PApp fc r [pexp (delab ist rty)])
746 elabE (ina { e_inarg = True, e_intype = True }) (Just fc) (PApp fc r [pexp (delab ist rty)])
747 env <- get_env
747 env <- get_env
@@ -991,11 +991,13 @@ elab ist info emode opts fn tm
991 -- When building the metavar application, leave out the unique
991 -- When building the metavar application, leave out the unique
992 -- names which have been used elsewhere in the term, since we
992 -- names which have been used elsewhere in the term, since we
993 -- won't be able to use them in the resulting application.
993 -- won't be able to use them in the resulting application.
994 let unique_used = getUniqueUsed (tt_ctxt ist) ptm
995 let unique_used = getUniqueUsed (tt_ctxt ist) ptm
996+ let lin_used = getLinearUsed (tt_ctxt ist) ptm
995 let n' = metavarName (namespace info) n
997 let n' = metavarName (namespace info) n
997 psns <- getPSnames
999 psns <- getPSnames
998− n' <- defer unique_used n'
1000+ n' <- defer unique_used lin_used n'
1000 highlightSource nfc (AnnName n' (Just MetavarOutput) Nothing Nothing)
1002 highlightSource nfc (AnnName n' (Just MetavarOutput) Nothing Nothing)
1001 elab' ina fc (PProof ts) = do compute; mapM_ (runTac True ist (elabFC info) fn) ts
1003 elab' ina fc (PProof ts) = do compute; mapM_ (runTac True ist (elabFC info) fn) ts
@@ -1016,7 +1018,10 @@ elab ist info emode opts fn tm
1016 valn <- getNameFrom (sMN 0 "scval")
1018 valn <- getNameFrom (sMN 0 "scval")
1017 scvn <- getNameFrom (sMN 0 "scvar")
1019 scvn <- getNameFrom (sMN 0 "scvar")
1018 claim valn (Var tyn)
1020 claim valn (Var tyn)
1023+ let scrnames = allNamesIn scr
1019− letbind scvn (Var tyn) (Var valn)
1024+ letbind scvn (letrig scrnames env) (Var tyn) (Var valn)
1021 -- Start filling in the scrutinee type, if we can work one
1026 -- Start filling in the scrutinee type, if we can work one
1022 -- out from the case options
1027 -- out from the case options
@@ -1047,13 +1052,15 @@ elab ist info emode opts fn tm
1047 let inOpts = (filter (/= scvn) (map fstEnv args)) \\ (concatMap (\x -> allNamesIn (snd x)) opts)
1052 let inOpts = (filter (/= scvn) (map fstEnv args)) \\ (concatMap (\x -> allNamesIn (snd x)) opts)
1049 let argsDropped = filter (\t -> isUnique envU t || isNotLift args t)
1054 let argsDropped = filter (\t -> isUnique envU t || isNotLift args t)
1050− (nub $ allNamesIn scr ++ inApp ptm ++
1055+ (nub $ scrnames ++ inApp ptm ++
1057+ let lin_used = getLinearUsed (tt_ctxt ist) ptm
1053 let args' = filter (\(n, _, _) -> n `notElem` argsDropped) args
1059 let args' = filter (\(n, _, _) -> n `notElem` argsDropped) args
1061+ -- trace (show lin_used ++ "\n" ++ show args ++ "\n" ++ show ptm) attack
1056− cname' <- defer argsDropped (mkN (mkCaseName fc fn))
1063+ cname' <- defer argsDropped lin_used (mkN (mkCaseName fc fn))
1059 -- if the scrutinee is one of the 'args' in env, we should
1066 -- if the scrutinee is one of the 'args' in env, we should
@@ -1075,6 +1082,17 @@ elab ist info emode opts fn tm
1075 xs@(_:_) -> sNS n xs
1082 xs@(_:_) -> sNS n xs
1085+ -- If any variables in the scrutinee are in the environment with
1086+ -- multiplicity other than RigW, let bind the scrutinee variable
1087+ -- with the smallest multiplicity
1088+ letrig ns [] = RigW
1089+ letrig ns env = letrig' Rig1 ns env
1091+ letrig' def ns [] = def
1092+ letrig' def ns ((n, r, _) : env)
1093+ | n `elem` ns = letrig' (rigMult def r) ns env
1094+ | otherwise = letrig' def ns env
1078 getScrType [] = Nothing
1096 getScrType [] = Nothing
1079 getScrType (f : os) = maybe (getScrType os) Just (getAppType f)
1097 getScrType (f : os) = maybe (getScrType os) Just (getAppType f)
@@ -1092,7 +1110,7 @@ elab ist info emode opts fn tm
1093 inApp (P _ n _) = [n]
1111 inApp (P _ n _) = [n]
1094 inApp (App _ f a) = inApp f ++ inApp a
1112 inApp (App _ f a) = inApp f ++ inApp a
1095− inApp (Bind n (Let _ v) sc) = inApp v ++ inApp sc
1113+ inApp (Bind n (Let _ _ v) sc) = inApp v ++ inApp sc
1096 inApp (Bind n (Guess _ v) sc) = inApp v ++ inApp sc
1114 inApp (Bind n (Guess _ v) sc) = inApp v ++ inApp sc
1097 inApp (Bind n b sc) = inApp sc
1115 inApp (Bind n b sc) = inApp sc
1098 inApp _ = []
1116 inApp _ = []
@@ -1163,7 +1181,7 @@ elab ist info emode opts fn tm
1163 -- Let-bind the result of elaborating the contained term, so that
1181 -- Let-bind the result of elaborating the contained term, so that
1164 -- the hole doesn't disappear
1182 -- the hole doesn't disappear
1165 nTm <- getNameFrom (sMN 0 "quotedTerm")
1183 nTm <- getNameFrom (sMN 0 "quotedTerm")
1166− letbind nTm (Var qTy) (Var qTm)
1184+ letbind nTm RigW (Var qTy) (Var qTm)
1168 -- Fill out the goal type, if relevant
1186 -- Fill out the goal type, if relevant
1169 case goalt of
1187 case goalt of
@@ -1290,7 +1308,7 @@ elab ist info emode opts fn tm
1290 -- In order to intercept the elaborated value, we need to
1308 -- In order to intercept the elaborated value, we need to
1291 -- let-bind it.
1309 -- let-bind it.
1293− letbind n g (Var n')
1311+ letbind n RigW g (Var n')
1294 focus n'
1312 focus n'
1295 elab' ina fc tm
1313 elab' ina fc tm
1296 env <- get_env
1314 env <- get_env
@@ -1319,8 +1337,10 @@ elab ist info emode opts fn tm
1319 delayElab pri t
1337 delayElab pri t
1320 = updateAux (\e -> e { delayed_elab = delayed_elab e ++ [(pri, t)] })
1338 = updateAux (\e -> e { delayed_elab = delayed_elab e ++ [(pri, t)] })
1340+ -- If the variable in the environment is the scrutinee of the case,
1341+ -- and has multiplicity W, keep it available
1322 isScr :: PTerm -> (Name, RigCount, Binder Term) -> (Name, (Bool, Binder Term))
1342 isScr :: PTerm -> (Name, RigCount, Binder Term) -> (Name, (Bool, Binder Term))
1323− isScr (PRef _ _ n) (n', _, b) = (n', (n == n', b))
1343+ isScr (PRef _ _ n) (n', RigW, b) = (n', (n == n', b))
1324 isScr _ (n', _, b) = (n', (False, b))
1344 isScr _ (n', _, b) = (n', (False, b))
1326 caseBlock :: FC -> Name
1346 caseBlock :: FC -> Name
@@ -1332,8 +1352,8 @@ elab ist info emode opts fn tm
1332 map (mkClause args) opts
1352 map (mkClause args) opts
1334 where -- Find the variable we want as the scrutinee and mark it as
1354 where -- Find the variable we want as the scrutinee and mark it as
1335− -- 'True'. If the scrutinee is in the environment, match on that
1355+ -- 'True'. If the scrutinee is available in the environment,
1336− -- otherwise match on the new argument we're adding.
1356+ -- match on that otherwise match on the new argument we're adding.
1337 findScr ((n, (True, t)) : xs)
1357 findScr ((n, (True, t)) : xs)
1338 = (n, (True, t)) : scrName n xs
1358 = (n, (True, t)) : scrName n xs
1339 findScr [(n, (_, t))] = [(n, (True, t))]
1359 findScr [(n, (_, t))] = [(n, (True, t))]
@@ -1721,7 +1741,7 @@ collectDeferred top casenames ctxt tm = cd [] tm
1721 t' <- cd ((n, b) : env) t
1741 t' <- cd ((n, b) : env) t
1722 return (Bind n b' t')
1742 return (Bind n b' t')
1724− cdb (Let t v) = liftM2 Let (cd env t) (cd env v)
1744+ cdb (Let rig t v) = liftM2 (Let rig) (cd env t) (cd env v)
1725 cdb (Guess t v) = liftM2 Guess (cd env t) (cd env v)
1745 cdb (Guess t v) = liftM2 Guess (cd env t) (cd env v)
1726 cdb b = do ty' <- cd env (binderTy b)
1746 cdb b = do ty' <- cd env (binderTy b)
1727 return (b { binderTy = ty' })
1747 return (b { binderTy = ty' })
@@ -1737,7 +1757,7 @@ case_ ind autoSolve ist fn tm = do
1737 valn <- getNameFrom (sMN 0 "ival")
1757 valn <- getNameFrom (sMN 0 "ival")
1738 claim valn (Var tyn)
1758 claim valn (Var tyn)
1739 letn <- getNameFrom (sMN 0 "irule")
1759 letn <- getNameFrom (sMN 0 "irule")
1740− letbind letn (Var tyn) (Var valn)
1760+ letbind letn RigW (Var tyn) (Var valn)
1741 focus valn
1761 focus valn
1742 elab ist toplevel ERHS [] (sMN 0 "tac") tm
1762 elab ist toplevel ERHS [] (sMN 0 "tac") tm
1743 env <- get_env
1763 env <- get_env
@@ -2115,7 +2135,7 @@ runElabAction info ist fc env tm ns = do tm' <- eval tm
2115 n' <- reifyTTName n
2135 n' <- reifyTTName n
2116 ty' <- reifyRaw ty
2136 ty' <- reifyRaw ty
2117 tm' <- reifyRaw tm
2137 tm' <- reifyRaw tm
2118− letbind n' ty' tm'
2138+ letbind n' RigW ty' tm'
2119 returnUnit
2139 returnUnit
2120 | n == tacN "Prim__Compute"
2140 | n == tacN "Prim__Compute"
2121 = do ~[] <- tacTmArgs 0 tac args; compute ; returnUnit
2141 = do ~[] <- tacTmArgs 0 tac args; compute ; returnUnit
@@ -2270,9 +2290,10 @@ runElabAction info ist fc env tm ns = do tm' <- eval tm
2270 ptm <- get_term
2290 ptm <- get_term
2271 -- See documentation above in the elab case for PMetavar
2291 -- See documentation above in the elab case for PMetavar
2272 let unique_used = getUniqueUsed ctxt ptm
2292 let unique_used = getUniqueUsed ctxt ptm
2293+ let lin_used = getLinearUsed ctxt ptm
2273 let mvn = metavarName ns n'
2294 let mvn = metavarName ns n'
2275− defer unique_used mvn
2296+ defer unique_used lin_used mvn
2277 returnUnit
2298 returnUnit
2278 | n == tacN "Prim__Fixity"
2299 | n == tacN "Prim__Fixity"
@@ -2377,7 +2398,7 @@ runTac autoSolve ist perhapsFC fn tac
2377 valn <- getNameFrom (sMN 0 "eqval")
2398 valn <- getNameFrom (sMN 0 "eqval")
2378 claim valn (Var tyn)
2399 claim valn (Var tyn)
2379 letn <- getNameFrom (sMN 0 "equiv_val")
2400 letn <- getNameFrom (sMN 0 "equiv_val")
2380− letbind letn (Var tyn) (Var valn)
2401+ letbind letn RigW (Var tyn) (Var valn)
2381 focus tyn
2402 focus tyn
2382 elab ist toplevel ERHS [] (sMN 0 "tac") tm
2403 elab ist toplevel ERHS [] (sMN 0 "tac") tm
2383 focus valn
2404 focus valn
@@ -2390,7 +2411,7 @@ runTac autoSolve ist perhapsFC fn tac
2390 valn <- getNameFrom (sMN 0 "rval")
2411 valn <- getNameFrom (sMN 0 "rval")
2391 claim valn (Var tyn)
2412 claim valn (Var tyn)
2392 letn <- getNameFrom (sMN 0 "rewrite_rule")
2413 letn <- getNameFrom (sMN 0 "rewrite_rule")
2393− letbind letn (Var tyn) (Var valn)
2414+ letbind letn RigW (Var tyn) (Var valn)
2394 focus valn
2415 focus valn
2395 elab ist toplevel ERHS [] (sMN 0 "tac") tm
2416 elab ist toplevel ERHS [] (sMN 0 "tac") tm
2396 rewrite (Var letn)
2417 rewrite (Var letn)
@@ -2406,7 +2427,7 @@ runTac autoSolve ist perhapsFC fn tac
2406 valn <- getNameFrom (sMN 0 "letval")
2427 valn <- getNameFrom (sMN 0 "letval")
2407 claim valn (Var tyn)
2428 claim valn (Var tyn)
2408 letn <- unique_hole n
2429 letn <- unique_hole n
2409− letbind letn (Var tyn) (Var valn)
2430+ letbind letn RigW (Var tyn) (Var valn)
2410 focus valn
2431 focus valn
2411 elab ist toplevel ERHS [] (sMN 0 "tac") tm
2432 elab ist toplevel ERHS [] (sMN 0 "tac") tm
2412 when autoSolve solveAll
2433 when autoSolve solveAll
@@ -2417,7 +2438,7 @@ runTac autoSolve ist perhapsFC fn tac
2417 valn <- getNameFrom (sMN 0 "letval")
2438 valn <- getNameFrom (sMN 0 "letval")
2418 claim valn (Var tyn)
2439 claim valn (Var tyn)
2419 letn <- unique_hole n
2440 letn <- unique_hole n
2420− letbind letn (Var tyn) (Var valn)
2441+ letbind letn RigW (Var tyn) (Var valn)
2421 focus tyn
2442 focus tyn
2422 elab ist toplevel ERHS [] (sMN 0 "tac") ty
2443 elab ist toplevel ERHS [] (sMN 0 "tac") ty
2423 focus valn
2444 focus valn
@@ -2443,7 +2464,7 @@ runTac autoSolve ist perhapsFC fn tac
2443 script <- getNameFrom (sMN 0 "script")
2464 script <- getNameFrom (sMN 0 "script")
2444 claim script scriptTy
2465 claim script scriptTy
2445 scriptvar <- getNameFrom (sMN 0 "scriptvar" )
2466 scriptvar <- getNameFrom (sMN 0 "scriptvar" )
2446− letbind scriptvar scriptTy (Var script)
2467+ letbind scriptvar RigW scriptTy (Var script)
2447 focus script
2468 focus script
2448 elab ist toplevel ERHS [] (sMN 0 "tac") tm
2469 elab ist toplevel ERHS [] (sMN 0 "tac") tm
2449 (script', _) <- get_type_val (Var scriptvar)
2470 (script', _) <- get_type_val (Var scriptvar)
@@ -2475,7 +2496,7 @@ runTac autoSolve ist perhapsFC fn tac
2475 script <- getNameFrom (sMN 0 "script")
2496 script <- getNameFrom (sMN 0 "script")
2476 claim script scriptTy
2497 claim script scriptTy
2477 scriptvar <- getNameFrom (sMN 0 "scriptvar" )
2498 scriptvar <- getNameFrom (sMN 0 "scriptvar" )
2478− letbind scriptvar scriptTy (Var script)
2499+ letbind scriptvar RigW scriptTy (Var script)
2479 focus script
2500 focus script
2480 ptm <- get_term
2501 ptm <- get_term
2481 env <- get_env
2502 env <- get_env
@@ -2506,7 +2527,7 @@ runTac autoSolve ist perhapsFC fn tac
2506 valn <- getNameFrom (sMN 0 "letval")
2527 valn <- getNameFrom (sMN 0 "letval")
2507 claim valn (Var tyn)
2528 claim valn (Var tyn)
2508 letn <- getNameFrom (sMN 0 "letvar")
2529 letn <- getNameFrom (sMN 0 "letvar")
2509− letbind letn (Var tyn) (Var valn)
2530+ letbind letn RigW (Var tyn) (Var valn)
2510 focus valn
2531 focus valn
2511 elab ist toplevel ERHS [] (sMN 0 "tac") v
2532 elab ist toplevel ERHS [] (sMN 0 "tac") v
2512 (value, _) <- get_type_val (Var letn)
2533 (value, _) <- get_type_val (Var letn)
@@ -2520,7 +2541,7 @@ runTac autoSolve ist perhapsFC fn tac
2520 valn <- getNameFrom (sMN 0 "letval")
2541 valn <- getNameFrom (sMN 0 "letval")
2521 claim valn (Var tyn)
2542 claim valn (Var tyn)
2522 letn <- getNameFrom (sMN 0 "letvar")
2543 letn <- getNameFrom (sMN 0 "letvar")
2523− letbind letn (Var tyn) (Var valn)
2544+ letbind letn RigW (Var tyn) (Var valn)
2524 focus valn
2545 focus valn
2525 elab ist toplevel ERHS [] (sMN 0 "tac") v
2546 elab ist toplevel ERHS [] (sMN 0 "tac") v
2526 (value, _) <- get_type_val (Var letn)
2547 (value, _) <- get_type_val (Var letn)