@@ -294,14 +294,21 @@ data PDo' t = DoExp FC t
295type PDo = PDo' PTerm
295type PDo = PDo' PTerm
297+-- The priority gives a hint as to elaboration order. Best to elaborate
298+-- things early which will help give a more concrete type to other
299+-- variables, e.g. a before (interpTy a).
301+data PArg' t = PImp { priority :: Int,
297−data PArg' t = PImp { lazyarg :: Bool, pname :: Name, getTm :: t }
302+ lazyarg :: Bool, pname :: Name, getTm :: t }
303+ | PExp { priority :: Int,
298− | PExp { lazyarg :: Bool, getTm :: t }
304+ lazyarg :: Bool, getTm :: t }
305+ | PConstraint { priority :: Int,
299− | PConstraint { lazyarg :: Bool, getTm :: t }
306+ lazyarg :: Bool, getTm :: t }
300 deriving (Show, Eq, Functor)
307 deriving (Show, Eq, Functor)
302−pimp = PImp False
309+pimp = PImp 0 False
303−pexp = PExp False
310+pexp = PExp 0 False
304−pconst = PConstraint False
311+pconst = PConstraint 0 False
306type PArg = PArg' PTerm
313type PArg = PArg' PTerm
@@ -370,17 +377,17 @@ showDImp impl (PDatadecl n ty cons)
371getImps :: [PArg] -> [(Name, PTerm)]
378getImps :: [PArg] -> [(Name, PTerm)]
372getImps [] = []
379getImps [] = []
373−getImps (PImp _ n tm : xs) = (n, tm) : getImps xs
380+getImps (PImp _ _ n tm : xs) = (n, tm) : getImps xs
374getImps (_ : xs) = getImps xs
381getImps (_ : xs) = getImps xs
376getExps :: [PArg] -> [PTerm]
383getExps :: [PArg] -> [PTerm]
377getExps [] = []
384getExps [] = []
378−getExps (PExp _ tm : xs) = tm : getExps xs
385+getExps (PExp _ _ tm : xs) = tm : getExps xs
379getExps (_ : xs) = getExps xs
386getExps (_ : xs) = getExps xs
381getConsts :: [PArg] -> [PTerm]
388getConsts :: [PArg] -> [PTerm]
382getConsts [] = []
389getConsts [] = []
383−getConsts (PConstraint _ tm : xs) = tm : getConsts xs
390+getConsts (PConstraint _ _ tm : xs) = tm : getConsts xs
384getConsts (_ : xs) = getConsts xs
391getConsts (_ : xs) = getConsts xs
386getAll :: [PArg] -> [PTerm]
393getAll :: [PArg] -> [PTerm]
@@ -420,10 +427,9 @@ showImp impl tm = se 10 tm where
420 = let [l, r] = getExps args in
427 = let [l, r] = getExps args in
421 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
428 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
422 se p (PApp _ f as)
429 se p (PApp _ f as)
423− = let (imps, cs, args) = (getImps as, getConsts as, getExps as) in
430+ = let args = getExps as in
424− bracket p 1 $ se 1 f ++ concatMap scArg cs
425− ++ (if impl then concatMap siArg imps else "")
431+ bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
426− ++ concatMap seArg args
432+ else concatMap seArg args
427 se p (PHidden tm) = "." ++ se 0 tm
433 se p (PHidden tm) = "." ++ se 0 tm
428 se p (PRefl _) = "refl"
434 se p (PRefl _) = "refl"
429 se p (PResolveTC _) = "resolvetc"
435 se p (PResolveTC _) = "resolvetc"
@@ -438,6 +444,10 @@ showImp impl tm = se 10 tm where
438 se p (PMetavar n) = "?" ++ show n
444 se p (PMetavar n) = "?" ++ show n
439 se p Placeholder = "_"
445 se p Placeholder = "_"
447+ sArg (PImp _ _ n tm) = siArg (n, tm)
448+ sArg (PExp _ _ tm) = seArg tm
449+ sArg (PConstraint _ _ tm) = scArg tm
441 seArg arg = " " ++ se 0 arg
451 seArg arg = " " ++ se 0 arg
442 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
452 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
443 scArg val = " {{" ++ se 10 val ++ "}}"
453 scArg val = " {{" ++ se 10 val ++ "}}"
@@ -592,6 +602,33 @@ expandParamsD dec ps ns (PClauses fc n cs)
592 (map (expandParamsD dec ps ns) ds)
602 (map (expandParamsD dec ps ns) ds)
593expandParamsD dec ps ns d = d
603expandParamsD dec ps ns d = d
605+-- Calculate a priority for a type, for deciding elaboration order
606+-- * if it's just a type variable or concrete type, do it early (0)
607+-- * if there's only type variables and injective constructors, do it next (1)
608+-- * if there's a function type, next (2)
609+-- * finally, everything else (3)
611+getPriority :: IState -> PTerm -> Int
612+getPriority i tm = pri tm
615+ case lookupP n (tt_ctxt i) of
616+ Just (P (DCon _ _) _ _) -> 1
617+ Just (P (TCon _ _) _ _) -> 1
618+ Just (P Ref _ _) -> 3
619+ Nothing -> 0 -- must be locally bound, if it's not an error...
620+ pri (PPi _ _ x y) = max 4 (max (pri x) (pri y))
624+ pri (PEq _ l r) = max 1 (max (pri l) (pri r))
625+ pri (PApp _ f as) = max 1 (max (pri f) (foldr max 0 (map (pri.getTm) as)))
626+ pri (PPair _ l r) = max 1 (max (pri l) (pri r))
627+ pri (PDPair _ l r) = max 1 (max (pri l) (pri r))
628+ pri (PConstant _) = 0
595-- Dealing with implicit arguments
632-- Dealing with implicit arguments
597-- Add implicit Pi bindings for any names in the term which appear in an
634-- Add implicit Pi bindings for any names in the term which appear in an
@@ -631,7 +668,7 @@ implicitise syn ist tm
631 imps top env (PPi (Imp l _) n ty sc)
668 imps top env (PPi (Imp l _) n ty sc)
632 = do let isn = nub (namesIn ist ty) `dropAll` [n]
669 = do let isn = nub (namesIn ist ty) `dropAll` [n]
633 (decls , ns) <- get
670 (decls , ns) <- get
634− put (PImp l n ty : decls,
671+ put (PImp (getPriority ist ty) l n ty : decls,
635 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
672 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
636 imps True (n:env) sc
673 imps True (n:env) sc
637 imps top env (PPi (Exp l _) n ty sc)
674 imps top env (PPi (Exp l _) n ty sc)
@@ -639,7 +676,7 @@ implicitise syn ist tm
639 (PRef _ x) -> namesIn ist sc `dropAll` [n]
676 (PRef _ x) -> namesIn ist sc `dropAll` [n]
641 (decls, ns) <- get -- ignore decls in HO types
678 (decls, ns) <- get -- ignore decls in HO types
642− put (PExp l ty : decls,
679+ put (PExp (getPriority ist ty) l ty : decls,
643 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
680 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
644 imps True (n:env) sc
681 imps True (n:env) sc
645 imps top env (PPi (Constraint l _) n ty sc)
682 imps top env (PPi (Constraint l _) n ty sc)
@@ -647,7 +684,7 @@ implicitise syn ist tm
647 (PRef _ x) -> namesIn ist sc `dropAll` [n]
684 (PRef _ x) -> namesIn ist sc `dropAll` [n]
649 (decls, ns) <- get -- ignore decls in HO types
686 (decls, ns) <- get -- ignore decls in HO types
650− put (PConstraint l ty : decls,
687+ put (PConstraint 10 l ty : decls,
651 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
688 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
652 imps True (n:env) sc
689 imps True (n:env) sc
653 imps top env (PEq _ l r)
690 imps top env (PEq _ l r)
@@ -723,21 +760,21 @@ aiFn ist fc f as
723 Nothing -> mkPApp fc 1 (PRef fc f) as
760 Nothing -> mkPApp fc 1 (PRef fc f) as
725 insertImpl :: [PArg] -> [PArg] -> [PArg]
762 insertImpl :: [PArg] -> [PArg] -> [PArg]
726− insertImpl (PExp l ty : ps) (PExp _ tm : given) =
763+ insertImpl (PExp p l ty : ps) (PExp _ _ tm : given) =
727− PExp l tm : insertImpl ps given
764+ PExp p l tm : insertImpl ps given
728− insertImpl (PConstraint l ty : ps) (PConstraint _ tm : given) =
765+ insertImpl (PConstraint p l ty : ps) (PConstraint _ _ tm : given) =
729− PConstraint l tm : insertImpl ps given
766+ PConstraint p l tm : insertImpl ps given
730− insertImpl (PConstraint l ty : ps) given =
767+ insertImpl (PConstraint p l ty : ps) given =
731− PConstraint l (PResolveTC fc) : insertImpl ps given
768+ PConstraint p l (PResolveTC fc) : insertImpl ps given
732− insertImpl (PImp l n ty : ps) given =
769+ insertImpl (PImp p l n ty : ps) given =
733 case find n given [] of
770 case find n given [] of
734− Just (tm, given') -> PImp l n tm : insertImpl ps given'
771+ Just (tm, given') -> PImp p l n tm : insertImpl ps given'
735− Nothing -> PImp l n Placeholder : insertImpl ps given
772+ Nothing -> PImp p l n Placeholder : insertImpl ps given
736 insertImpl expected [] = []
773 insertImpl expected [] = []
737 insertImpl [] given = given
774 insertImpl [] given = given
739 find n [] acc = Nothing
776 find n [] acc = Nothing
740− find n (PImp _ n' t : gs) acc
777+ find n (PImp _ _ n' t : gs) acc
741 | n == n' = Just (t, reverse acc ++ gs)
778 | n == n' = Just (t, reverse acc ++ gs)
742 find n (g : gs) acc = find n gs (g : acc)
779 find n (g : gs) acc = find n gs (g : acc)