on master help sign in
Fix parameter propagation
commitf98d1383e2dd8c06e6d1fa8a6d76f9e6e439e28e authorEdwin Brady <ecb10@st-andrews.ac.uk> dateTue 18 Feb 2014, 11:05 (13 years ago) parentac446bd
8 files changed+106−63
libs/base/Control/Isomorphism.idr+5−4 libs/base/Control/Monad/State.idr+1−1 libs/base/Data/Vect/Quantifiers.idr+4−4 libs/effects/Effects.idr+3−3 libs/neweffects/Effects.idr+2−2 src/Idris/ElabDecls.hs+86−45 src/Idris/ElabTerm.hs+1−1 test/proof002/Reflect.idr+4−3
libs/base/Control/Isomorphism.idr+5−4
@@ -35,10 +35,10 @@ isoSym (MkIso to from toFrom fromTo) = MkIso from to fromTo toFrom
3535
3636eitherComm : Iso (Either a b) (Either b a)
3737eitherComm = MkIso swap swap swapSwap swapSwap
38− where swap : Either a b -> Either b a
38+ where swap : Either a' b' -> Either b' a' -- a & b are parameters, so fixed!
3939 swap (Left x) = Right x
4040 swap (Right x) = Left x
41− swapSwap : (e : Either a b) -> swap (swap e) = e
41+ swapSwap : (e : Either a' b') -> swap (swap e) = e
4242 swapSwap (Left x) = refl
4343 swapSwap (Right x) = refl
4444
@@ -105,9 +105,10 @@ eitherCongRight i = eitherCong isoRefl i
105105-- Isomorphisms over products
106106pairComm : Iso (a, b) (b, a)
107107pairComm = MkIso swap swap swapSwap swapSwap
108− where swap : (a, b) -> (b, a)
108+ where swap : (a', b') -> (b', a')
109109 swap (x, y) = (y, x)
110− swapSwap : (x : (a, b)) -> swap (swap x) = x
110+
111+ swapSwap : (x : (a', b')) -> swap (swap x) = x
111112 swapSwap (x, y) = refl
112113
113114pairAssoc : Iso (a, (b, c)) ((a, b), c)
libs/base/Control/Monad/State.idr+1−1
@@ -14,7 +14,7 @@ record StateT : Type -> (Type -> Type) -> Type -> Type where
1414
1515instance Functor f => Functor (StateT s f) where
1616 map f (ST g) = ST (\st => map (mapFst f) (g st)) where
17− mapFst : (a -> x) -> (a, b) -> (x, b)
17+ mapFst : (a -> x) -> (a, s) -> (x, s)
1818 mapFst fn (a, b) = (fn a, b)
1919
2020instance Monad f => Applicative (StateT s f) where
libs/base/Data/Vect/Quantifiers.idr+4−4
@@ -14,10 +14,10 @@ anyElim f _ (There p) = f p
1414
1515any : {P : a -> Type} -> ((x : a) -> Dec (P x)) -> (xs : Vect n a) -> Dec (Any P xs)
1616any _ Nil = No anyNilAbsurd
17−any {P} p (x::xs) with (p x)
17+any p (x::xs) with (p x)
1818 | Yes prf = Yes (Here prf)
1919 | No prf =
20− case any {P} p xs of
20+ case any p xs of
2121 Yes prf' => Yes (There prf')
2222 No prf' => No (anyElim prf' prf)
2323
@@ -39,9 +39,9 @@ notAllThere np (_ :: ps) = np ps
3939
4040all : {P : a -> Type} -> ((x : a) -> Dec (P x)) -> (xs : Vect n a) -> Dec (All P xs)
4141all _ Nil = Yes Nil
42−all {P} d (x::xs) with (d x)
42+all d (x::xs) with (d x)
4343 | No prf = No (notAllHere prf)
4444 | Yes prf =
45− case all {P} d xs of
45+ case all d xs of
4646 Yes prf' => Yes (prf :: prf')
4747 No prf' => No (notAllThere prf')
libs/effects/Effects.idr+3−3
@@ -209,8 +209,8 @@ execEff : Env m xs -> (p : EffElem e res xs) ->
209209 (Env m (updateResTy xs p eff) -> a -> m t) -> m t
210210execEff (val :: env) Here eff' k
211211 = handle val eff' (\res, v => k (res :: env) v)
212−execEff {e} {a} {b} (val :: env) (There p) eff k
213− = execEff {e} {a} {b} env p eff (\env', v => k (val :: env') v)
212+execEff {e} {res} {b} {a} (val :: env) (There p) eff k
213+ = execEff {e} {res} {b} {a} env p eff (\env', v => k (val :: env') v)
214214
215215private
216216testEff : Env m xs -> (p : EffElem e (Either l r) xs) ->
@@ -224,7 +224,7 @@ testEff (val :: env) (There p) lk rk
224224 (\envk => rk (val :: envk))
225225
226226private
227−testEffLbl : {x : lbl} ->
227+testEffLbl : {x : lblTy} ->
228228 Env m xs -> (p : EffElem e (LRes x (Either l r)) xs) ->
229229 (Env m (updateResTyImm xs p (LRes x l)) -> m b) ->
230230 (Env m (updateResTyImm xs p (LRes x r)) -> m b) ->
libs/neweffects/Effects.idr+2−2
@@ -208,8 +208,8 @@ execEff : Env m xs -> (p : EffElem e res xs) ->
208208execEff (val :: env) Here eff' k
209209 = handle val eff' (\v, res => k v (res :: env))
210210-- FIXME: Teach the elaborator to propagate parameters here
211−execEff {a} {res} {resk} (val :: env) (There p) eff k
212− = execEff {a} {res} {resk} env p eff (\v, env' => k v (val :: env'))
211+execEff {e} {a} {res} {resk} (val :: env) (There p) eff k
212+ = execEff {e} {a} {res} {resk} env p eff (\v, env' => k v (val :: env'))
213213
214214-- Q: Instead of m b, implement as StateT (Env m xs') m b, so that state
215215-- updates can be propagated even through failing computations?
src/Idris/ElabDecls.hs+86−45
@@ -76,12 +76,12 @@ elabType' norm info syn doc fc opts n ty' = {- let ty' = piBind (params info) ty
7676 ctxt <- getContext
7777 i <- getIState
7878
79− logLvl 3 $ show n ++ " pre-type " ++ showTmImpls ty'
79+ logLvl 1 $ show n ++ " pre-type " ++ showTmImpls ty'
8080 ty' <- addUsingConstraints syn fc ty'
8181 ty' <- implicit syn n ty'
8282
8383 let ty = addImpl i ty'
84− logLvl 2 $ show n ++ " type " ++ showTmImpls ty
84+ logLvl 1 $ show n ++ " type " ++ showTmImpls ty
8585 ((tyT, defer, is), log) <-
8686 tclift $ elaborate ctxt n (TType (UVal 0)) []
8787 (errAt "type of " n (erun fc (build i info False [] n ty)))
@@ -662,7 +662,7 @@ elabProvider info syn fc n ty tm
662662 tm <- getProvided fc rhs'
663663
664664 -- Finally add a top-level definition of the provided term
665− elabClauses info fc [] n [PClause fc n (PRef fc n) [] (delab i tm) []]
665+ elabClauses info fc [] n [PClause fc n (PApp fc (PRef fc n) []) [] (delab i tm) []]
666666 logLvl 1 $ "Elaborated provider " ++ show n ++ " as: " ++ show tm
667667
668668 where isTType :: TT Name -> Bool
@@ -1266,6 +1266,68 @@ checkPossible info fc tcgen fname lhs_in
12661266 ((P _ x _, _), (P _ y _, _)) -> x == y
12671267 _ -> False
12681268
1269+getFixedInType i env (PExp _ _ _ _ : is) (Bind n (Pi t) sc)
1270+ = getFixedInType i (n : env) is (instantiate (P Bound n t) sc)
1271+getFixedInType i env (_ : is) (Bind n (Pi t) sc)
1272+ = getFixedInType i (n : env) is (instantiate (P Bound n t) sc)
1273+getFixedInType i env is tm@(App f a)
1274+ | (P _ tn _, args) <- unApply tm
1275+ = case lookupCtxt tn (idris_datatypes i) of
1276+ [t] -> nub $ paramNames args env (param_pos t) ++
1277+ getFixedInType i env is f ++
1278+ getFixedInType i env is a
1279+ [] -> nub $ getFixedInType i env is f ++
1280+ getFixedInType i env is a
1281+ | otherwise = nub $ getFixedInType i env is f ++
1282+ getFixedInType i env is a
1283+getFixedInType i _ _ _ = []
1284+
1285+getFlexInType i env (Bind n (Pi t) sc)
1286+ = nub $ getFlexInType i env t ++
1287+ getFlexInType i (n : env) (instantiate (P Bound n t) sc)
1288+getFlexInType i env tm@(App f a)
1289+ | (P _ tn _, args) <- unApply tm
1290+ = case lookupCtxt tn (idris_datatypes i) of
1291+ [t] -> nub $ paramNames args env [x | x <- [0..length args],
1292+ not (x `elem` param_pos t)]
1293+ ++ getFlexInType i env f ++
1294+ getFlexInType i env a
1295+ [] -> nub $ getFlexInType i env f ++
1296+ getFlexInType i env a
1297+ | otherwise = nub $ getFlexInType i env f ++
1298+ getFlexInType i env a
1299+getFlexInType i _ _ = []
1300+
1301+-- Treat a name as a parameter if it appears in parameter positions in
1302+-- types, and never in a non-parameter position in a type.
1303+
1304+getParamsInType i env ps t = let fix = getFixedInType i env ps t
1305+ flex = getFlexInType i env t in
1306+ [x | x <- fix, not (x `elem` flex)]
1307+
1308+paramNames args env [] = []
1309+paramNames args env (p : ps)
1310+ | length args > p = case args!!p of
1311+ P _ n _ -> if n `elem` env
1312+ then n : paramNames args env ps
1313+ else paramNames args env ps
1314+ _ -> paramNames args env ps
1315+ | otherwise = paramNames args env ps
1316+
1317+propagateParams :: [Name] -> Type -> PTerm -> PTerm
1318+propagateParams ps t tm@(PApp _ (PRef fc n) args)
1319+ = PApp fc (PRef fc n) (addP t args)
1320+ where addP (Bind n _ sc) (t : ts)
1321+ | Placeholder <- getTm t,
1322+ n `elem` ps,
1323+ not (n `elem` allNamesIn tm)
1324+ = t { getTm = PRef fc n } : addP sc ts
1325+ addP (Bind n _ sc) (t : ts) = t : addP sc ts
1326+ addP _ ts = ts
1327+propagateParams ps t (PRef fc n)
1328+ = PApp fc (PRef fc n) (map (\x -> pimp x (PRef fc x) True) ps)
1329+propagateParams ps t x = x
1330+
12691331elabClause :: ElabInfo -> FnOpts -> (Int, PClause) ->
12701332 Idris (Either Term (Term, Term))
12711333elabClause info opts (_, PClause fc fname lhs_in [] PImpossible [])
@@ -1292,10 +1354,12 @@ elabClause info opts (cnum, PClause fc fname lhs_in withs rhs_in whereblock)
12921354 [t] -> t
12931355 _ -> []
12941356 let params = getParamsInType i [] fn_is fn_ty
1295− let lhs = stripUnmatchable i $
1296− addImplPat i (propagateParams params (stripLinear i lhs_in))
1297− logLvl 5 ("LHS: " ++ show fc ++ " " ++ showTmImpls lhs)
1298− logLvl 4 ("Fixed parameters: " ++ show params ++ " from " ++ show (fn_ty, fn_is))
1357+ let flex = getFlexInType i [] fn_ty
1358+ let lhs = mkLHSapp $ stripUnmatchable i $
1359+ propagateParams params fn_ty (addImplPat i (stripLinear i lhs_in))
1360+ logLvl 5 ("LHS: " ++ show fc ++ " " ++ show lhs)
1361+ logLvl 4 ("Fixed parameters: " ++ show (flex, params) ++ " from " ++ show lhs_in ++
1362+ "\n" ++ show (fn_ty, fn_is))
12991363
13001364 (((lhs', dlhs, []), probs, inj), _) <-
13011365 tclift $ elaborate ctxt (sMN 0 "patLHS") infP []
@@ -1342,7 +1406,7 @@ elabClause info opts (cnum, PClause fc fname lhs_in withs rhs_in whereblock)
13421406 -- Elaborate those with a type *before* RHS, those without *after*
13431407 let (wbefore, wafter) = sepBlocks wb
13441408
1345− logLvl 5 $ "Where block:\n " ++ show wbefore ++ "\n" ++ show wafter
1409+ logLvl 1 $ "Where block:\n " ++ show wbefore ++ "\n" ++ show wafter
13461410 mapM_ (elabDecl' EAll info) wbefore
13471411 -- Now build the RHS, using the type of the LHS as the goal.
13481412 i <- getIState -- new implicits from where block
@@ -1355,7 +1419,7 @@ elabClause info opts (cnum, PClause fc fname lhs_in withs rhs_in whereblock)
13551419 ((rhs', defer, is, probs), _) <-
13561420 tclift $ elaborate ctxt (sMN 0 "patRHS") clhsty []
13571421 (do pbinds ist lhs_tm
1358− mapM_ setinj inj
1422+ mapM_ setinj (nub (params ++ inj))
13591423 setNextName
13601424 (_, _, is) <- errAt "right hand side of " fname
13611425 (erun fc (build i info False opts fname rhs))
@@ -1415,6 +1479,9 @@ elabClause info opts (cnum, PClause fc fname lhs_in withs rhs_in whereblock)
14151479 addErrRev (crhs, clhs)
14161480 return $ Right (clhs, crhs)
14171481 where
1482+ mkLHSapp t@(PRef _ _) = trace ("APP " ++ show t) $ PApp fc t []
1483+ mkLHSapp t = t
1484+
14181485 decorate (NS x ns)
14191486 = NS (SN (WhereN cnum fname x)) ns -- ++ [show cnum])
14201487-- = NS (UN ('#':show x)) (ns ++ [show cnum, show fname])
@@ -1447,40 +1514,6 @@ elabClause info opts (cnum, PClause fc fname lhs_in withs rhs_in whereblock)
14471514 -- _ -> MN i (show n)) . l
14481515 }
14491516
1450− getParamsInType i env (PExp _ _ _ _ : is) (Bind n (Pi t) sc)
1451− = getParamsInType i env is (instantiate (P Bound n t) sc)
1452− getParamsInType i env (_ : is) (Bind n (Pi t) sc)
1453− = getParamsInType i (n : env) is (instantiate (P Bound n t) sc)
1454− getParamsInType i env is tm@(App f a)
1455− | (P _ tn _, args) <- unApply tm
1456− = case lookupCtxt tn (idris_datatypes i) of
1457− [t] -> nub $ paramNames args env (param_pos t) ++
1458− getParamsInType i env is f ++
1459− getParamsInType i env is a
1460− [] -> nub $ getParamsInType i env is f ++
1461− getParamsInType i env is a
1462− | otherwise = nub $ getParamsInType i env is f ++
1463− getParamsInType i env is a
1464− getParamsInType i _ _ _ = []
1465−
1466− paramNames args env [] = []
1467− paramNames args env (p : ps)
1468− | length args > p = case args!!p of
1469− P _ n _ -> if n `elem` env
1470− then n : paramNames args env ps
1471− else paramNames args env ps
1472− _ -> paramNames args env ps
1473− | otherwise = paramNames args env ps
1474−
1475− propagateParams :: [Name] -> PTerm -> PTerm
1476− propagateParams ps (PApp _ (PRef fc n) args)
1477− = PApp fc (PRef fc n) (map addP args)
1478− where addP imp@(PImp _ _ _ _ Placeholder _)
1479− | pname imp `elem` ps = imp { getTm = PRef fc (pname imp) }
1480− addP t = t
1481− propagateParams ps (PRef fc n)
1482− = PApp fc (PRef fc n) (map (\x -> pimp x (PRef fc x) True) ps)
1483− propagateParams ps x = x
14841517
14851518 -- if a hole is just an argument/result of a case block, treat it as
14861519 -- the unit type. Hack to help elaborate case in do blocks.
@@ -1508,8 +1541,16 @@ elabClause info opts (_, PWith fc fname lhs_in withs wval_in withblock)
15081541 -- Build the LHS as an "Infer", and pull out its type and
15091542 -- pattern bindings
15101543 i <- getIState
1511− let lhs = addImplPat i (stripLinear i lhs_in)
1512− logLvl 5 ("LHS: " ++ showTmImpls lhs)
1544+ -- get the parameters first, to pass through to any where block
1545+ let fn_ty = case lookupTy fname (tt_ctxt i) of
1546+ [t] -> t
1547+ _ -> error "Can't happen (elabClause function type)"
1548+ let fn_is = case lookupCtxt fname (idris_implicits i) of
1549+ [t] -> t
1550+ _ -> []
1551+ let params = getParamsInType i [] fn_is fn_ty
1552+ let lhs = propagateParams params fn_ty (addImplPat i (stripLinear i lhs_in))
1553+ logLvl 1 ("LHS: " ++ show lhs)
15131554 ((lhs', dlhs, []), _) <-
15141555 tclift $ elaborate ctxt (sMN 0 "patLHS") infP []
15151556 (errAt "left hand side of with in " fname
src/Idris/ElabTerm.hs+1−1
@@ -611,7 +611,7 @@ elab ist info pattern opts fn tm
611611 elab' ina (PUnifyLog t) = do unifyLog True
612612 elab' ina t
613613 unifyLog False
614− elab' ina x = fail $ "Unelaboratable syntactic form " ++ show x
614+ elab' ina x = fail $ "Unelaboratable syntactic form " ++ showTmImpls x
615615
616616 isScr :: PTerm -> (Name, Binder Term) -> (Name, (Bool, Binder Term))
617617 isScr (PRef _ n) (n', b) = (n', (n == n', b))
test/proof002/Reflect.idr+4−3
@@ -40,9 +40,10 @@ using (xs : List a, ys : List a, G : List (List a))
4040 expr_r (Var i) = (_ ** (RApp RNil i, refl))
4141 expr_r (App ex ey) = let (xl ** (xr, xprf)) = expr_r ex in
4242 let (yl ** (yr, yprf)) = expr_r ey in
43− appRExpr _ _ xr yr xprf yprf
43+ appRExpr _ _ xr yr xprf yprf
4444 where
45− appRExpr : (xs', ys' : List a) ->
45+ appRExpr : (xs', ys' : List a) ->
46+ {G : List (List a)} -> {xs, ys : List a} ->
4647 RExpr G xs -> RExpr G ys -> (xs' = xs) -> (ys' = ys) ->
4748 (ws' ** (RExpr G ws', xs' ++ ys' = ws'))
4849 appRExpr x' y' rxs (RApp e i) xprf yprf
@@ -173,7 +174,7 @@ Reflect.appRExpr1 = proof {
173174 rewrite sym xprf;
174175 rewrite sym yprf;
175176 rewrite prf;
176− rewrite sym (appendAssociative xs xs2 ys1);
177+ rewrite sym (appendAssociative xs1 xs2 ys1);
177178 trivial;
178179}
179180