@@ -807,6 +807,11 @@ instance Eq NameType where
807807 TCon _ a == TCon _ b = (a == b) -- ignore tag
808808 _ == _ = False
809809
810+data AppStatus n = Complete
811+ | MaybeHoles
812+ | Holes [n]
813+ deriving (Eq, Ord, Functor, Data, Typeable, Show)
814+
810815-- | Terms in the core language. The type parameter is the type of
811816-- identifiers used for bindings and explicit named references;
812817-- usually we use @TT 'Name'@.
@@ -815,7 +820,7 @@ data TT n = P NameType n (TT n) -- ^ named references with type
815820 -- Pure Type Systems Formalized)
816821 | V !Int -- ^ a resolved de Bruijn-indexed variable
817822 | Bind n !(Binder (TT n)) (TT n) -- ^ a binding
818− | App !(TT n) (TT n) -- ^ function, function type, arg
823+ | App (AppStatus n) !(TT n) (TT n) -- ^ function, function type, arg
819824 | Constant Const -- ^ constant
820825 | Proj (TT n) !Int -- ^ argument projection; runtime only
821826 -- (-1) is a special case for 'subtract one from BI'
@@ -851,7 +856,7 @@ instance TermSize (TT Name) where
851856 termsize n (Bind n' b sc)
852857 = let rn = if n == n' then sMN 0 "noname" else n in
853858 termsize rn sc
854− termsize n (App f a) = termsize n f + termsize n a
859+ termsize n (App _ f a) = termsize n f + termsize n a
855860 termsize n (Proj t i) = termsize n t
856861 termsize n _ = 1
857862
@@ -859,7 +864,7 @@ instance Sized a => Sized (TT a) where
859864 size (P name n trm) = 1 + size name + size n + size trm
860865 size (V v) = 1
861866 size (Bind nm binder bdy) = 1 + size nm + size binder + size bdy
862− size (App l r) = 1 + size l + size r
867+ size (App _ l r) = 1 + size l + size r
863868 size (Constant c) = size c
864869 size Erased = 1
865870 size (TType u) = 1 + size u
@@ -880,8 +885,8 @@ instance Eq n => Eq (TT n) where
880885 (==) (P xt x _) (P yt y _) = x == y
881886 (==) (V x) (V y) = x == y
882887 (==) (Bind _ xb xs) (Bind _ yb ys) = xs == ys && xb == yb
883− (==) (App fx ax) (App fy ay) = ax == ay && fx == fy
884− (==) (TType _) (TType _) = True -- deal with constraints later
888+ (==) (App _ fx ax) (App _ fy ay) = ax == ay && fx == fy
889+ (==) (TType _) (TType _) = True -- deal with constraints later
885890 (==) (Constant x) (Constant y) = x == y
886891 (==) (Proj x i) (Proj y j) = x == y && i == j
887892 (==) Erased _ = True
@@ -897,15 +902,15 @@ isInjective :: TT n -> Bool
897902isInjective (P (DCon _ _ _) _ _) = True
898903isInjective (P (TCon _ _) _ _) = True
899904isInjective (Constant _) = True
900−isInjective (TType x) = True
905+isInjective (TType x) = True
901906isInjective (Bind _ (Pi _ _ _) sc) = True
902−isInjective (App f a) = isInjective f
907+isInjective (App _ f a) = isInjective f
903908isInjective _ = False
904909
905910-- | Count the number of instances of a de Bruijn index in a term
906911vinstances :: Int -> TT n -> Int
907912vinstances i (V x) | i == x = 1
908−vinstances i (App f a) = vinstances i f + vinstances i a
913+vinstances i (App _ f a) = vinstances i f + vinstances i a
909914vinstances i (Bind x b sc) = instancesB b + vinstances (i + 1) sc
910915 where instancesB (Let t v) = vinstances i v
911916 instancesB _ = 0
@@ -917,7 +922,7 @@ instantiate e = subst 0 where
917922 subst i (P nt x ty) = P nt x (subst i ty)
918923 subst i (V x) | i == x = e
919924 subst i (Bind x b sc) = Bind x (fmap (subst i) b) (subst (i+1) sc)
920− subst i (App f a) = App (subst i f) (subst i a)
925+ subst i (App s f a) = App s (subst i f) (subst i a)
921926 subst i (Proj x idx) = Proj (subst i x) idx
922927 subst i t = t
923928
@@ -930,7 +935,7 @@ substV x tm = dropV 0 (instantiate x tm) where
930935 dropV i (V x) | x > i = V (x - 1)
931936 | otherwise = V x
932937 dropV i (Bind x b sc) = Bind x (fmap (dropV i) b) (dropV (i+1) sc)
933− dropV i (App f a) = App (dropV i f) (dropV i a)
938+ dropV i (App s f a) = App s (dropV i f) (dropV i a)
934939 dropV i (Proj x idx) = Proj (dropV i x) idx
935940 dropV i t = t
936941
@@ -941,7 +946,7 @@ explicitNames (Bind x b sc) = let b' = fmap explicitNames b in
941946 Bind x b'
942947 (explicitNames (instantiate
943948 (P Bound x (binderTy b')) sc))
944−explicitNames (App f a) = App (explicitNames f) (explicitNames a)
949+explicitNames (App s f a) = App s (explicitNames f) (explicitNames a)
945950explicitNames (Proj x idx) = Proj (explicitNames x) idx
946951explicitNames t = t
947952
@@ -955,7 +960,7 @@ pToV' n i (Bind x b sc)
955960-- resolve names from the *outer* scope which may happen to have the same id.
956961 | n == x = Bind x (fmap (pToV' n i) b) sc
957962 | otherwise = Bind x (fmap (pToV' n i) b) (pToV' n (i+1) sc)
958−pToV' n i (App f a) = App (pToV' n i f) (pToV' n i a)
963+pToV' n i (App s f a) = App s (pToV' n i f) (pToV' n i a)
959964pToV' n i (Proj t idx) = Proj (pToV' n i t) idx
960965pToV' n i t = t
961966
@@ -966,7 +971,7 @@ addBinder t = ab 0 t
966971 ab top (V i) | i >= top = V (i + 1)
967972 | otherwise = V i
968973 ab top (Bind x b sc) = Bind x (fmap (ab top) b) (ab (top + 1) sc)
969− ab top (App f a) = App (ab top f) (ab top a)
974+ ab top (App s f a) = App s (ab top f) (ab top a)
970975 ab top (Proj t idx) = Proj (ab top t) idx
971976 ab top t = t
972977
@@ -985,14 +990,14 @@ vToP = vToP' [] where
985990 P Bound n (binderTy b)
986991 vToP' env (Bind n b sc) = let b' = fmap (vToP' env) b in
987992 Bind n b' (vToP' ((n, b'):env) sc)
988− vToP' env (App f a) = App (vToP' env f) (vToP' env a)
993+ vToP' env (App s f a) = App s (vToP' env f) (vToP' env a)
989994 vToP' env t = t
990995
991996-- | Replace every non-free reference to the name of a binding in
992997-- the given term with a de Bruijn index.
993998finalise :: Eq n => TT n -> TT n
994999finalise (Bind x b sc) = Bind x (fmap finalise b) (pToV x (finalise sc))
995−finalise (App f a) = App (finalise f) (finalise a)
1000+finalise (App s f a) = App s (finalise f) (finalise a)
9961001finalise t = t
9971002
9981003-- Once we've finished checking everything about a term we no longer need
@@ -1000,7 +1005,7 @@ finalise t = t
10001005
10011006pEraseType :: TT n -> TT n
10021007pEraseType (P nt t _) = P nt t Erased
1003−pEraseType (App f a) = App (pEraseType f) (pEraseType a)
1008+pEraseType (App s f a) = App s (pEraseType f) (pEraseType a)
10041009pEraseType (Bind n b sc) = Bind n (fmap pEraseType b) (pEraseType sc)
10051010pEraseType t = t
10061011
@@ -1027,9 +1032,9 @@ subst n v tm = fst $ subst' 0 tm
10271032 = let (b', ub) = substB' i b
10281033 (sc', usc) = subst' (i+1) sc in
10291034 if ub || usc then (Bind x b' sc', True) else (t, False)
1030− subst' i t@(App f a) = let (f', uf) = subst' i f
1031− (a', ua) = subst' i a in
1032− if uf || ua then (App f' a', True) else (t, False)
1035+ subst' i t@(App s f a) = let (f', uf) = subst' i f
1036+ (a', ua) = subst' i a in
1037+ if uf || ua then (App s f' a', True) else (t, False)
10331038 subst' i t@(Proj x idx) = let (x', u) = subst' i x in
10341039 if u then (Proj x' idx, u) else (t, False)
10351040 subst' i t = (t, False)
@@ -1054,7 +1059,7 @@ psubst n v tm = s' 0 tm where
10541059 s' i (P _ x _) | n == x = v
10551060 s' i (Bind x b sc) | n == x = Bind x (fmap (s' i) b) sc
10561061 | otherwise = Bind x (fmap (s' i) b) (s' (i+1) sc)
1057− s' i (App f a) = App (s' i f) (s' i a)
1062+ s' i (App st f a) = App st (s' i f) (s' i a)
10581063 s' i (Proj t idx) = Proj (s' i t) idx
10591064 s' i t = t
10601065
@@ -1072,7 +1077,7 @@ substTerm :: Eq n => TT n {-^ Old term -} ->
10721077 -> TT n
10731078substTerm old new = st where
10741079 st t | t == old = new
1075− st (App f a) = App (st f) (st a)
1080+ st (App s f a) = App s (st f) (st a)
10761081 st (Bind x b sc) = Bind x (fmap st b) (st sc)
10771082 st t = t
10781083
@@ -1086,7 +1091,7 @@ occurrences n t = execState (no' 0 t) 0
10861091 where noB' i (Let t v) = do no' i t; no' i v
10871092 noB' i (Guess t v) = do no' i t; no' i v
10881093 noB' i b = no' i (binderTy b)
1089− no' i (App f a) = do no' i f; no' i a
1094+ no' i (App _ f a) = do no' i f; no' i a
10901095 no' i (Proj x _) = no' i x
10911096 no' i _ = return ()
10921097
@@ -1100,7 +1105,7 @@ noOccurrence n t = no' 0 t
11001105 where noB' i (Let t v) = no' i t && no' i v
11011106 noB' i (Guess t v) = no' i t && no' i v
11021107 noB' i b = no' i (binderTy b)
1103− no' i (App f a) = no' i f && no' i a
1108+ no' i (App _ f a) = no' i f && no' i a
11041109 no' i (Proj x _) = no' i x
11051110 no' i _ = True
11061111
@@ -1112,7 +1117,7 @@ freeNames t = nub $ freeNames' t
11121117 freeNames' (Bind n (Let t v) sc) = freeNames' v ++ (freeNames' sc \\ [n])
11131118 ++ freeNames' t
11141119 freeNames' (Bind n b sc) = freeNames' (binderTy b) ++ (freeNames' sc \\ [n])
1115− freeNames' (App f a) = freeNames' f ++ freeNames' a
1120+ freeNames' (App _ f a) = freeNames' f ++ freeNames' a
11161121 freeNames' (Proj x i) = freeNames' x
11171122 freeNames' _ = []
11181123
@@ -1124,14 +1129,14 @@ arity _ = 0
11241129-- | Deconstruct an application; returns the function and a list of arguments
11251130unApply :: TT n -> (TT n, [TT n])
11261131unApply t = ua [] t where
1127− ua args (App f a) = ua (a:args) f
1132+ ua args (App _ f a) = ua (a:args) f
11281133 ua args t = (t, args)
11291134
11301135-- | Returns a term representing the application of the first argument
11311136-- (a function) to every element of the second argument.
11321137mkApp :: TT n -> [TT n] -> TT n
11331138mkApp f [] = f
1134−mkApp f (a:as) = mkApp (App f a) as
1139+mkApp f (a:as) = mkApp (App MaybeHoles f a) as
11351140
11361141unList :: Term -> Maybe [Term]
11371142unList tm = case unApply tm of
@@ -1172,7 +1177,7 @@ safeForgetEnv env (Bind n b sc)
11721177 (safeForgetEnv env v)
11731178 safeForgetEnvB env b = do ty' <- safeForgetEnv env (binderTy b)
11741179 Just $ fmap (\_ -> ty') b
1175−safeForgetEnv env (App f a) = liftM2 RApp (safeForgetEnv env f) (safeForgetEnv env a)
1180+safeForgetEnv env (App _ f a) = liftM2 RApp (safeForgetEnv env f) (safeForgetEnv env a)
11761181safeForgetEnv env (Constant c) = Just $ RConstant c
11771182safeForgetEnv env (TType i) = Just RType
11781183safeForgetEnv env (UType u) = Just $ RUType u
@@ -1225,7 +1230,7 @@ uniqueBinders ns = ubSet (fromList ns) where
12251230 = let n' = uniqueNameSet n ns
12261231 ns' = insert n' ns in
12271232 Bind n' (fmap (ubSet ns') b) (ubSet ns' sc)
1228− ubSet ns (App f a) = App (ubSet ns f) (ubSet ns a)
1233+ ubSet ns (App s f a) = App s (ubSet ns f) (ubSet ns a)
12291234 ubSet ns t = t
12301235
12311236
@@ -1318,7 +1323,7 @@ prettyEnv env t = prettyEnv' env t False
13181323 bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
13191324 prettySe p env (Bind n b sc) debug =
13201325 bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
1321− prettySe p env (App f a) debug =
1326+ prettySe p env (App _ f a) debug =
13221327 bracket p 1 $ prettySe 1 env f debug <+> prettySe 0 env a debug
13231328 prettySe p env (Proj x i) debug =
13241329 prettySe 1 env x debug <+> text ("!" ++ show i)
@@ -1362,7 +1367,7 @@ showEnv' env t dbg = se 10 env t where
13621367 where arrow (TType _) = " -> "
13631368 arrow u = " [" ++ show u ++ "] -> "
13641369 se p env (Bind n b sc) = bracket p 2 $ sb env n b ++ se 10 ((n,b):env) sc
1365− se p env (App f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
1370+ se p env (App _ f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
13661371 se p env (Proj x i) = se 1 env x ++ "!" ++ show i
13671372 se p env (Constant c) = show c
13681373 se p env Erased = "[__]"
@@ -1387,9 +1392,9 @@ showEnv' env t dbg = se 10 env t where
13871392 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
13881393 | otherwise = str
13891394
1390−-- | Check whether a term has any holes in it - impure if so
1395+-- | Check whether a term has any hole bindings in it - impure if so
13911396pureTerm :: TT Name -> Bool
1392−pureTerm (App f a) = pureTerm f && pureTerm a
1397+pureTerm (App _ f a) = pureTerm f && pureTerm a
13931398pureTerm (Bind n b sc) = notClassName n && pureBinder b && pureTerm sc where
13941399 pureBinder (Hole _) = False
13951400 pureBinder (Guess _ _) = False
@@ -1405,7 +1410,7 @@ pureTerm _ = True
14051410weakenTm :: Int -> TT n -> TT n
14061411weakenTm i t = wk i 0 t
14071412 where wk i min (V x) | x >= min = V (i + x)
1408− wk i m (App f a) = App (wk i m f) (wk i m a)
1413+ wk i m (App s f a) = App s (wk i m f) (wk i m a)
14091414 wk i m (Bind x b sc) = Bind x (wkb i m b) (wk i (m + 1) sc)
14101415 wk i m t = t
14111416 wkb i m t = fmap (wk i m) t
@@ -1430,7 +1435,7 @@ weakenTmEnv i = map (\ (n, b) -> (n, fmap (weakenTm i) b))
14301435orderPats :: Term -> Term
14311436orderPats tm = op [] tm
14321437 where
1433− op [] (App f a) = App f (op [] a) -- for Infer terms
1438+ op [] (App s f a) = App s f (op [] a) -- for Infer terms
14341439
14351440 op ps (Bind n (PVar t) sc) = op ((n, PVar t) : ps) sc
14361441 op ps (Bind n (Hole t) sc) = op ((n, Hole t) : ps) sc
@@ -1455,7 +1460,7 @@ refsIn (Bind n b t) = nub $ nb b ++ refsIn t
14551460 where nb (Let t v) = nub (refsIn t) ++ nub (refsIn v)
14561461 nb (Guess t v) = nub (refsIn t) ++ nub (refsIn v)
14571462 nb t = refsIn (binderTy t)
1458−refsIn (App f a) = nub (refsIn f ++ refsIn a)
1463+refsIn (App s f a) = nub (refsIn f ++ refsIn a)
14591464refsIn _ = []
14601465
14611466-- Make sure all the pattern bindings are as far out as possible
@@ -1492,9 +1497,9 @@ liftPats tm = let (tm', ps) = runState (getPats tm) [] in
14921497 return (Bind n (Hole t') sc')
14931498
14941499
1495− getPats (App f a) = do f' <- getPats f
1496− a' <- getPats a
1497− return (App f' a')
1500+ getPats (App s f a) = do f' <- getPats f
1501+ a' <- getPats a
1502+ return (App s f' a')
14981503 getPats t = return t
14991504
15001505allTTNames :: Eq n => TT n -> [n]
@@ -1504,7 +1509,7 @@ allTTNames = nub . allNamesIn
15041509 where nb (Let t v) = allNamesIn t ++ allNamesIn v
15051510 nb (Guess t v) = allNamesIn t ++ allNamesIn v
15061511 nb t = allNamesIn (binderTy t)
1507− allNamesIn (App f a) = allNamesIn f ++ allNamesIn a
1512+ allNamesIn (App _ f a) = allNamesIn f ++ allNamesIn a
15081513 allNamesIn _ = []
15091514
15101515
@@ -1527,7 +1532,7 @@ pprintTT bound tm = pp startPrec bound tm
15271532 | otherwise = text ("{{{V" ++ show i ++ "}}}")
15281533 pp p bound (Bind n b sc) = ppb p bound n b $
15291534 pp startPrec (n:bound) sc
1530− pp p bound (App tm1 tm2) =
1535+ pp p bound (App _ tm1 tm2) =
15311536 bracket p appPrec . group . hang 2 $
15321537 pp appPrec bound tm1 <> line <>
15331538 pp (appPrec + 1) bound tm2