@@ -252,7 +252,7 @@ elab ist info emode opts fn tm
252252 ptm <- get_term
253253 when pattern $
254254 -- Look for Rig1 (linear) pattern bindings
255− do let pnms = findLinear ist [] ptm
255+ do let pnms = findLinear Rig1 ist [] ptm
256256 update_term (setLinear pnms)
257257 where
258258 pattern = emode == ELHS || emode == EImpossible
@@ -695,7 +695,7 @@ elab ist info emode opts fn tm
695695 elabE ec' fc sc
696696 solve
697697 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)
699699 = do attack
700700 ivs <- get_implementations
701701 tyn <- getNameFrom (sMN 0 "letty")
@@ -703,7 +703,7 @@ elab ist info emode opts fn tm
703703 valn <- getNameFrom (sMN 0 "letval")
704704 claim valn (Var tyn)
705705 explicit valn
706− letbind n (Var tyn) (Var valn)
706+ letbind n rig (Var tyn) (Var valn)
707707 addPSname n
708708 case ty of
709709 Placeholder -> return ()
@@ -730,7 +730,7 @@ elab ist info emode opts fn tm
730730 -- HACK: If the name leaks into its type, it may leak out of
731731 -- scope outside, so substitute in the outer scope.
732732 expandLet n (case lookupBinder n env of
733− Just (Let t v) -> v
733+ Just (Let rig t v) -> v
734734 other -> error ("Value not a let binding: " ++ show other))
735735 solve
736736 highlightSource nfc (AnnBoundName n False)
@@ -741,7 +741,7 @@ elab ist info emode opts fn tm
741741 claim tyn RType
742742 valn <- getNameFrom (sMN 0 "letval")
743743 claim valn (Var tyn)
744− letbind n (Var tyn) (Var valn)
744+ letbind n RigW (Var tyn) (Var valn)
745745 focus valn
746746 elabE (ina { e_inarg = True, e_intype = True }) (Just fc) (PApp fc r [pexp (delab ist rty)])
747747 env <- get_env
@@ -991,11 +991,13 @@ elab ist info emode opts fn tm
991991 -- When building the metavar application, leave out the unique
992992 -- names which have been used elsewhere in the term, since we
993993 -- won't be able to use them in the resulting application.
994+ env <- get_env
994995 let unique_used = getUniqueUsed (tt_ctxt ist) ptm
996+ let lin_used = getLinearUsed (tt_ctxt ist) ptm
995997 let n' = metavarName (namespace info) n
996998 attack
997999 psns <- getPSnames
998− n' <- defer unique_used n'
1000+ n' <- defer unique_used lin_used n'
9991001 solve
10001002 highlightSource nfc (AnnName n' (Just MetavarOutput) Nothing Nothing)
10011003 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
10161018 valn <- getNameFrom (sMN 0 "scval")
10171019 scvn <- getNameFrom (sMN 0 "scvar")
10181020 claim valn (Var tyn)
1019− letbind scvn (Var tyn) (Var valn)
1021+ env <- get_env
1022+
1023+ let scrnames = allNamesIn scr
1024+ letbind scvn (letrig scrnames env) (Var tyn) (Var valn)
10201025
10211026 -- Start filling in the scrutinee type, if we can work one
10221027 -- out from the case options
@@ -1047,13 +1052,15 @@ elab ist info emode opts fn tm
10471052 let inOpts = (filter (/= scvn) (map fstEnv args)) \\ (concatMap (\x -> allNamesIn (snd x)) opts)
10481053
10491054 let argsDropped = filter (\t -> isUnique envU t || isNotLift args t)
1050− (nub $ allNamesIn scr ++ inApp ptm ++
1055+ (nub $ scrnames ++ inApp ptm ++
10511056 inOpts)
1057+ let lin_used = getLinearUsed (tt_ctxt ist) ptm
10521058
10531059 let args' = filter (\(n, _, _) -> n `notElem` argsDropped) args
10541060
1061+ -- trace (show lin_used ++ "\n" ++ show args ++ "\n" ++ show ptm) attack
10551062 attack
1056− cname' <- defer argsDropped (mkN (mkCaseName fc fn))
1063+ cname' <- defer argsDropped lin_used (mkN (mkCaseName fc fn))
10571064 solve
10581065
10591066 -- if the scrutinee is one of the 'args' in env, we should
@@ -1075,6 +1082,17 @@ elab ist info emode opts fn tm
10751082 xs@(_:_) -> sNS n xs
10761083 _ -> n
10771084
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
1090+
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
1095+
10781096 getScrType [] = Nothing
10791097 getScrType (f : os) = maybe (getScrType os) Just (getAppType f)
10801098
@@ -1092,7 +1110,7 @@ elab ist info emode opts fn tm
10921110
10931111 inApp (P _ n _) = [n]
10941112 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
10961114 inApp (Bind n (Guess _ v) sc) = inApp v ++ inApp sc
10971115 inApp (Bind n b sc) = inApp sc
10981116 inApp _ = []
@@ -1163,7 +1181,7 @@ elab ist info emode opts fn tm
11631181 -- Let-bind the result of elaborating the contained term, so that
11641182 -- the hole doesn't disappear
11651183 nTm <- getNameFrom (sMN 0 "quotedTerm")
1166− letbind nTm (Var qTy) (Var qTm)
1184+ letbind nTm RigW (Var qTy) (Var qTm)
11671185
11681186 -- Fill out the goal type, if relevant
11691187 case goalt of
@@ -1290,7 +1308,7 @@ elab ist info emode opts fn tm
12901308 -- In order to intercept the elaborated value, we need to
12911309 -- let-bind it.
12921310 attack
1293− letbind n g (Var n')
1311+ letbind n RigW g (Var n')
12941312 focus n'
12951313 elab' ina fc tm
12961314 env <- get_env
@@ -1319,8 +1337,10 @@ elab ist info emode opts fn tm
13191337 delayElab pri t
13201338 = updateAux (\e -> e { delayed_elab = delayed_elab e ++ [(pri, t)] })
13211339
1340+ -- If the variable in the environment is the scrutinee of the case,
1341+ -- and has multiplicity W, keep it available
13221342 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))
13241344 isScr _ (n', _, b) = (n', (False, b))
13251345
13261346 caseBlock :: FC -> Name
@@ -1332,8 +1352,8 @@ elab ist info emode opts fn tm
13321352 map (mkClause args) opts
13331353
13341354 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
1336− -- otherwise match on the new argument we're adding.
1355+ -- 'True'. If the scrutinee is available in the environment,
1356+ -- match on that otherwise match on the new argument we're adding.
13371357 findScr ((n, (True, t)) : xs)
13381358 = (n, (True, t)) : scrName n xs
13391359 findScr [(n, (_, t))] = [(n, (True, t))]
@@ -1721,7 +1741,7 @@ collectDeferred top casenames ctxt tm = cd [] tm
17211741 t' <- cd ((n, b) : env) t
17221742 return (Bind n b' t')
17231743 where
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)
17251745 cdb (Guess t v) = liftM2 Guess (cd env t) (cd env v)
17261746 cdb b = do ty' <- cd env (binderTy b)
17271747 return (b { binderTy = ty' })
@@ -1737,7 +1757,7 @@ case_ ind autoSolve ist fn tm = do
17371757 valn <- getNameFrom (sMN 0 "ival")
17381758 claim valn (Var tyn)
17391759 letn <- getNameFrom (sMN 0 "irule")
1740− letbind letn (Var tyn) (Var valn)
1760+ letbind letn RigW (Var tyn) (Var valn)
17411761 focus valn
17421762 elab ist toplevel ERHS [] (sMN 0 "tac") tm
17431763 env <- get_env
@@ -2115,7 +2135,7 @@ runElabAction info ist fc env tm ns = do tm' <- eval tm
21152135 n' <- reifyTTName n
21162136 ty' <- reifyRaw ty
21172137 tm' <- reifyRaw tm
2118− letbind n' ty' tm'
2138+ letbind n' RigW ty' tm'
21192139 returnUnit
21202140 | n == tacN "Prim__Compute"
21212141 = do ~[] <- tacTmArgs 0 tac args; compute ; returnUnit
@@ -2270,9 +2290,10 @@ runElabAction info ist fc env tm ns = do tm' <- eval tm
22702290 ptm <- get_term
22712291 -- See documentation above in the elab case for PMetavar
22722292 let unique_used = getUniqueUsed ctxt ptm
2293+ let lin_used = getLinearUsed ctxt ptm
22732294 let mvn = metavarName ns n'
22742295 attack
2275− defer unique_used mvn
2296+ defer unique_used lin_used mvn
22762297 solve
22772298 returnUnit
22782299 | n == tacN "Prim__Fixity"
@@ -2377,7 +2398,7 @@ runTac autoSolve ist perhapsFC fn tac
23772398 valn <- getNameFrom (sMN 0 "eqval")
23782399 claim valn (Var tyn)
23792400 letn <- getNameFrom (sMN 0 "equiv_val")
2380− letbind letn (Var tyn) (Var valn)
2401+ letbind letn RigW (Var tyn) (Var valn)
23812402 focus tyn
23822403 elab ist toplevel ERHS [] (sMN 0 "tac") tm
23832404 focus valn
@@ -2390,7 +2411,7 @@ runTac autoSolve ist perhapsFC fn tac
23902411 valn <- getNameFrom (sMN 0 "rval")
23912412 claim valn (Var tyn)
23922413 letn <- getNameFrom (sMN 0 "rewrite_rule")
2393− letbind letn (Var tyn) (Var valn)
2414+ letbind letn RigW (Var tyn) (Var valn)
23942415 focus valn
23952416 elab ist toplevel ERHS [] (sMN 0 "tac") tm
23962417 rewrite (Var letn)
@@ -2406,7 +2427,7 @@ runTac autoSolve ist perhapsFC fn tac
24062427 valn <- getNameFrom (sMN 0 "letval")
24072428 claim valn (Var tyn)
24082429 letn <- unique_hole n
2409− letbind letn (Var tyn) (Var valn)
2430+ letbind letn RigW (Var tyn) (Var valn)
24102431 focus valn
24112432 elab ist toplevel ERHS [] (sMN 0 "tac") tm
24122433 when autoSolve solveAll
@@ -2417,7 +2438,7 @@ runTac autoSolve ist perhapsFC fn tac
24172438 valn <- getNameFrom (sMN 0 "letval")
24182439 claim valn (Var tyn)
24192440 letn <- unique_hole n
2420− letbind letn (Var tyn) (Var valn)
2441+ letbind letn RigW (Var tyn) (Var valn)
24212442 focus tyn
24222443 elab ist toplevel ERHS [] (sMN 0 "tac") ty
24232444 focus valn
@@ -2443,7 +2464,7 @@ runTac autoSolve ist perhapsFC fn tac
24432464 script <- getNameFrom (sMN 0 "script")
24442465 claim script scriptTy
24452466 scriptvar <- getNameFrom (sMN 0 "scriptvar" )
2446− letbind scriptvar scriptTy (Var script)
2467+ letbind scriptvar RigW scriptTy (Var script)
24472468 focus script
24482469 elab ist toplevel ERHS [] (sMN 0 "tac") tm
24492470 (script', _) <- get_type_val (Var scriptvar)
@@ -2475,7 +2496,7 @@ runTac autoSolve ist perhapsFC fn tac
24752496 script <- getNameFrom (sMN 0 "script")
24762497 claim script scriptTy
24772498 scriptvar <- getNameFrom (sMN 0 "scriptvar" )
2478− letbind scriptvar scriptTy (Var script)
2499+ letbind scriptvar RigW scriptTy (Var script)
24792500 focus script
24802501 ptm <- get_term
24812502 env <- get_env
@@ -2506,7 +2527,7 @@ runTac autoSolve ist perhapsFC fn tac
25062527 valn <- getNameFrom (sMN 0 "letval")
25072528 claim valn (Var tyn)
25082529 letn <- getNameFrom (sMN 0 "letvar")
2509− letbind letn (Var tyn) (Var valn)
2530+ letbind letn RigW (Var tyn) (Var valn)
25102531 focus valn
25112532 elab ist toplevel ERHS [] (sMN 0 "tac") v
25122533 (value, _) <- get_type_val (Var letn)
@@ -2520,7 +2541,7 @@ runTac autoSolve ist perhapsFC fn tac
25202541 valn <- getNameFrom (sMN 0 "letval")
25212542 claim valn (Var tyn)
25222543 letn <- getNameFrom (sMN 0 "letvar")
2523− letbind letn (Var tyn) (Var valn)
2544+ letbind letn RigW (Var tyn) (Var valn)
25242545 focus valn
25252546 elab ist toplevel ERHS [] (sMN 0 "tac") v
25262547 (value, _) <- get_type_val (Var letn)