@@ -808,6 +808,11 @@ instance Eq NameType where
808808 TCon _ a == TCon _ b = (a == b) -- ignore tag
809809 _ == _ = False
810810
811+data AppStatus n = Complete
812+ | MaybeHoles
813+ | Holes [n]
814+ deriving (Eq, Ord, Functor, Data, Typeable, Show)
815+
811816-- | Terms in the core language. The type parameter is the type of
812817-- identifiers used for bindings and explicit named references;
813818-- usually we use @TT 'Name'@.
@@ -816,7 +821,7 @@ data TT n = P NameType n (TT n) -- ^ named references with type
816821 -- Pure Type Systems Formalized)
817822 | V !Int -- ^ a resolved de Bruijn-indexed variable
818823 | Bind n !(Binder (TT n)) (TT n) -- ^ a binding
819− | App !(TT n) (TT n) -- ^ function, function type, arg
824+ | App (AppStatus n) !(TT n) (TT n) -- ^ function, function type, arg
820825 | Constant Const -- ^ constant
821826 | Proj (TT n) !Int -- ^ argument projection; runtime only
822827 -- (-1) is a special case for 'subtract one from BI'
@@ -852,7 +857,7 @@ instance TermSize (TT Name) where
852857 termsize n (Bind n' b sc)
853858 = let rn = if n == n' then sMN 0 "noname" else n in
854859 termsize rn sc
855− termsize n (App f a) = termsize n f + termsize n a
860+ termsize n (App _ f a) = termsize n f + termsize n a
856861 termsize n (Proj t i) = termsize n t
857862 termsize n _ = 1
858863
@@ -860,7 +865,7 @@ instance Sized a => Sized (TT a) where
860865 size (P name n trm) = 1 + size name + size n + size trm
861866 size (V v) = 1
862867 size (Bind nm binder bdy) = 1 + size nm + size binder + size bdy
863− size (App l r) = 1 + size l + size r
868+ size (App _ l r) = 1 + size l + size r
864869 size (Constant c) = size c
865870 size Erased = 1
866871 size (TType u) = 1 + size u
@@ -881,8 +886,8 @@ instance Eq n => Eq (TT n) where
881886 (==) (P xt x _) (P yt y _) = x == y
882887 (==) (V x) (V y) = x == y
883888 (==) (Bind _ xb xs) (Bind _ yb ys) = xs == ys && xb == yb
884− (==) (App fx ax) (App fy ay) = ax == ay && fx == fy
885− (==) (TType _) (TType _) = True -- deal with constraints later
889+ (==) (App _ fx ax) (App _ fy ay) = ax == ay && fx == fy
890+ (==) (TType _) (TType _) = True -- deal with constraints later
886891 (==) (Constant x) (Constant y) = x == y
887892 (==) (Proj x i) (Proj y j) = x == y && i == j
888893 (==) Erased _ = True
@@ -898,15 +903,15 @@ isInjective :: TT n -> Bool
898903isInjective (P (DCon _ _ _) _ _) = True
899904isInjective (P (TCon _ _) _ _) = True
900905isInjective (Constant _) = True
901−isInjective (TType x) = True
906+isInjective (TType x) = True
902907isInjective (Bind _ (Pi _ _ _) sc) = True
903−isInjective (App f a) = isInjective f
908+isInjective (App _ f a) = isInjective f
904909isInjective _ = False
905910
906911-- | Count the number of instances of a de Bruijn index in a term
907912vinstances :: Int -> TT n -> Int
908913vinstances i (V x) | i == x = 1
909−vinstances i (App f a) = vinstances i f + vinstances i a
914+vinstances i (App _ f a) = vinstances i f + vinstances i a
910915vinstances i (Bind x b sc) = instancesB b + vinstances (i + 1) sc
911916 where instancesB (Let t v) = vinstances i v
912917 instancesB _ = 0
@@ -918,7 +923,7 @@ instantiate e = subst 0 where
918923 subst i (P nt x ty) = P nt x (subst i ty)
919924 subst i (V x) | i == x = e
920925 subst i (Bind x b sc) = Bind x (fmap (subst i) b) (subst (i+1) sc)
921− subst i (App f a) = App (subst i f) (subst i a)
926+ subst i (App s f a) = App s (subst i f) (subst i a)
922927 subst i (Proj x idx) = Proj (subst i x) idx
923928 subst i t = t
924929
@@ -931,7 +936,7 @@ substV x tm = dropV 0 (instantiate x tm) where
931936 dropV i (V x) | x > i = V (x - 1)
932937 | otherwise = V x
933938 dropV i (Bind x b sc) = Bind x (fmap (dropV i) b) (dropV (i+1) sc)
934− dropV i (App f a) = App (dropV i f) (dropV i a)
939+ dropV i (App s f a) = App s (dropV i f) (dropV i a)
935940 dropV i (Proj x idx) = Proj (dropV i x) idx
936941 dropV i t = t
937942
@@ -942,7 +947,7 @@ explicitNames (Bind x b sc) = let b' = fmap explicitNames b in
942947 Bind x b'
943948 (explicitNames (instantiate
944949 (P Bound x (binderTy b')) sc))
945−explicitNames (App f a) = App (explicitNames f) (explicitNames a)
950+explicitNames (App s f a) = App s (explicitNames f) (explicitNames a)
946951explicitNames (Proj x idx) = Proj (explicitNames x) idx
947952explicitNames t = t
948953
@@ -956,7 +961,7 @@ pToV' n i (Bind x b sc)
956961-- resolve names from the *outer* scope which may happen to have the same id.
957962 | n == x = Bind x (fmap (pToV' n i) b) sc
958963 | otherwise = Bind x (fmap (pToV' n i) b) (pToV' n (i+1) sc)
959−pToV' n i (App f a) = App (pToV' n i f) (pToV' n i a)
964+pToV' n i (App s f a) = App s (pToV' n i f) (pToV' n i a)
960965pToV' n i (Proj t idx) = Proj (pToV' n i t) idx
961966pToV' n i t = t
962967
@@ -967,7 +972,7 @@ addBinder t = ab 0 t
967972 ab top (V i) | i >= top = V (i + 1)
968973 | otherwise = V i
969974 ab top (Bind x b sc) = Bind x (fmap (ab top) b) (ab (top + 1) sc)
970− ab top (App f a) = App (ab top f) (ab top a)
975+ ab top (App s f a) = App s (ab top f) (ab top a)
971976 ab top (Proj t idx) = Proj (ab top t) idx
972977 ab top t = t
973978
@@ -986,14 +991,14 @@ vToP = vToP' [] where
986991 P Bound n (binderTy b)
987992 vToP' env (Bind n b sc) = let b' = fmap (vToP' env) b in
988993 Bind n b' (vToP' ((n, b'):env) sc)
989− vToP' env (App f a) = App (vToP' env f) (vToP' env a)
994+ vToP' env (App s f a) = App s (vToP' env f) (vToP' env a)
990995 vToP' env t = t
991996
992997-- | Replace every non-free reference to the name of a binding in
993998-- the given term with a de Bruijn index.
994999finalise :: Eq n => TT n -> TT n
9951000finalise (Bind x b sc) = Bind x (fmap finalise b) (pToV x (finalise sc))
996−finalise (App f a) = App (finalise f) (finalise a)
1001+finalise (App s f a) = App s (finalise f) (finalise a)
9971002finalise t = t
9981003
9991004-- Once we've finished checking everything about a term we no longer need
@@ -1001,7 +1006,7 @@ finalise t = t
10011006
10021007pEraseType :: TT n -> TT n
10031008pEraseType (P nt t _) = P nt t Erased
1004−pEraseType (App f a) = App (pEraseType f) (pEraseType a)
1009+pEraseType (App s f a) = App s (pEraseType f) (pEraseType a)
10051010pEraseType (Bind n b sc) = Bind n (fmap pEraseType b) (pEraseType sc)
10061011pEraseType t = t
10071012
@@ -1028,9 +1033,9 @@ subst n v tm = fst $ subst' 0 tm
10281033 = let (b', ub) = substB' i b
10291034 (sc', usc) = subst' (i+1) sc in
10301035 if ub || usc then (Bind x b' sc', True) else (t, False)
1031− subst' i t@(App f a) = let (f', uf) = subst' i f
1032− (a', ua) = subst' i a in
1033− if uf || ua then (App f' a', True) else (t, False)
1036+ subst' i t@(App s f a) = let (f', uf) = subst' i f
1037+ (a', ua) = subst' i a in
1038+ if uf || ua then (App s f' a', True) else (t, False)
10341039 subst' i t@(Proj x idx) = let (x', u) = subst' i x in
10351040 if u then (Proj x' idx, u) else (t, False)
10361041 subst' i t = (t, False)
@@ -1055,7 +1060,7 @@ psubst n v tm = s' 0 tm where
10551060 s' i (P _ x _) | n == x = v
10561061 s' i (Bind x b sc) | n == x = Bind x (fmap (s' i) b) sc
10571062 | otherwise = Bind x (fmap (s' i) b) (s' (i+1) sc)
1058− s' i (App f a) = App (s' i f) (s' i a)
1063+ s' i (App st f a) = App st (s' i f) (s' i a)
10591064 s' i (Proj t idx) = Proj (s' i t) idx
10601065 s' i t = t
10611066
@@ -1073,7 +1078,7 @@ substTerm :: Eq n => TT n {-^ Old term -} ->
10731078 -> TT n
10741079substTerm old new = st where
10751080 st t | t == old = new
1076− st (App f a) = App (st f) (st a)
1081+ st (App s f a) = App s (st f) (st a)
10771082 st (Bind x b sc) = Bind x (fmap st b) (st sc)
10781083 st t = t
10791084
@@ -1087,7 +1092,7 @@ occurrences n t = execState (no' 0 t) 0
10871092 where noB' i (Let t v) = do no' i t; no' i v
10881093 noB' i (Guess t v) = do no' i t; no' i v
10891094 noB' i b = no' i (binderTy b)
1090− no' i (App f a) = do no' i f; no' i a
1095+ no' i (App _ f a) = do no' i f; no' i a
10911096 no' i (Proj x _) = no' i x
10921097 no' i _ = return ()
10931098
@@ -1101,7 +1106,7 @@ noOccurrence n t = no' 0 t
11011106 where noB' i (Let t v) = no' i t && no' i v
11021107 noB' i (Guess t v) = no' i t && no' i v
11031108 noB' i b = no' i (binderTy b)
1104− no' i (App f a) = no' i f && no' i a
1109+ no' i (App _ f a) = no' i f && no' i a
11051110 no' i (Proj x _) = no' i x
11061111 no' i _ = True
11071112
@@ -1113,7 +1118,7 @@ freeNames t = nub $ freeNames' t
11131118 freeNames' (Bind n (Let t v) sc) = freeNames' v ++ (freeNames' sc \\ [n])
11141119 ++ freeNames' t
11151120 freeNames' (Bind n b sc) = freeNames' (binderTy b) ++ (freeNames' sc \\ [n])
1116− freeNames' (App f a) = freeNames' f ++ freeNames' a
1121+ freeNames' (App _ f a) = freeNames' f ++ freeNames' a
11171122 freeNames' (Proj x i) = freeNames' x
11181123 freeNames' _ = []
11191124
@@ -1125,14 +1130,14 @@ arity _ = 0
11251130-- | Deconstruct an application; returns the function and a list of arguments
11261131unApply :: TT n -> (TT n, [TT n])
11271132unApply t = ua [] t where
1128− ua args (App f a) = ua (a:args) f
1133+ ua args (App _ f a) = ua (a:args) f
11291134 ua args t = (t, args)
11301135
11311136-- | Returns a term representing the application of the first argument
11321137-- (a function) to every element of the second argument.
11331138mkApp :: TT n -> [TT n] -> TT n
11341139mkApp f [] = f
1135−mkApp f (a:as) = mkApp (App f a) as
1140+mkApp f (a:as) = mkApp (App MaybeHoles f a) as
11361141
11371142unList :: Term -> Maybe [Term]
11381143unList tm = case unApply tm of
@@ -1173,7 +1178,7 @@ safeForgetEnv env (Bind n b sc)
11731178 (safeForgetEnv env v)
11741179 safeForgetEnvB env b = do ty' <- safeForgetEnv env (binderTy b)
11751180 Just $ fmap (\_ -> ty') b
1176−safeForgetEnv env (App f a) = liftM2 RApp (safeForgetEnv env f) (safeForgetEnv env a)
1181+safeForgetEnv env (App _ f a) = liftM2 RApp (safeForgetEnv env f) (safeForgetEnv env a)
11771182safeForgetEnv env (Constant c) = Just $ RConstant c
11781183safeForgetEnv env (TType i) = Just RType
11791184safeForgetEnv env (UType u) = Just $ RUType u
@@ -1226,7 +1231,7 @@ uniqueBinders ns = ubSet (fromList ns) where
12261231 = let n' = uniqueNameSet n ns
12271232 ns' = insert n' ns in
12281233 Bind n' (fmap (ubSet ns') b) (ubSet ns' sc)
1229− ubSet ns (App f a) = App (ubSet ns f) (ubSet ns a)
1234+ ubSet ns (App s f a) = App s (ubSet ns f) (ubSet ns a)
12301235 ubSet ns t = t
12311236
12321237
@@ -1319,7 +1324,7 @@ prettyEnv env t = prettyEnv' env t False
13191324 bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
13201325 prettySe p env (Bind n b sc) debug =
13211326 bracket p 2 $ prettySb env n b debug <> prettySe 10 ((n, b):env) sc debug
1322− prettySe p env (App f a) debug =
1327+ prettySe p env (App _ f a) debug =
13231328 bracket p 1 $ prettySe 1 env f debug <+> prettySe 0 env a debug
13241329 prettySe p env (Proj x i) debug =
13251330 prettySe 1 env x debug <+> text ("!" ++ show i)
@@ -1363,7 +1368,7 @@ showEnv' env t dbg = se 10 env t where
13631368 where arrow (TType _) = " -> "
13641369 arrow u = " [" ++ show u ++ "] -> "
13651370 se p env (Bind n b sc) = bracket p 2 $ sb env n b ++ se 10 ((n,b):env) sc
1366− se p env (App f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
1371+ se p env (App _ f a) = bracket p 1 $ se 1 env f ++ " " ++ se 0 env a
13671372 se p env (Proj x i) = se 1 env x ++ "!" ++ show i
13681373 se p env (Constant c) = show c
13691374 se p env Erased = "[__]"
@@ -1388,9 +1393,9 @@ showEnv' env t dbg = se 10 env t where
13881393 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
13891394 | otherwise = str
13901395
1391−-- | Check whether a term has any holes in it - impure if so
1396+-- | Check whether a term has any hole bindings in it - impure if so
13921397pureTerm :: TT Name -> Bool
1393−pureTerm (App f a) = pureTerm f && pureTerm a
1398+pureTerm (App _ f a) = pureTerm f && pureTerm a
13941399pureTerm (Bind n b sc) = notClassName n && pureBinder b && pureTerm sc where
13951400 pureBinder (Hole _) = False
13961401 pureBinder (Guess _ _) = False
@@ -1406,7 +1411,7 @@ pureTerm _ = True
14061411weakenTm :: Int -> TT n -> TT n
14071412weakenTm i t = wk i 0 t
14081413 where wk i min (V x) | x >= min = V (i + x)
1409− wk i m (App f a) = App (wk i m f) (wk i m a)
1414+ wk i m (App s f a) = App s (wk i m f) (wk i m a)
14101415 wk i m (Bind x b sc) = Bind x (wkb i m b) (wk i (m + 1) sc)
14111416 wk i m t = t
14121417 wkb i m t = fmap (wk i m) t
@@ -1431,7 +1436,7 @@ weakenTmEnv i = map (\ (n, b) -> (n, fmap (weakenTm i) b))
14311436orderPats :: Term -> Term
14321437orderPats tm = op [] tm
14331438 where
1434− op [] (App f a) = App f (op [] a) -- for Infer terms
1439+ op [] (App s f a) = App s f (op [] a) -- for Infer terms
14351440
14361441 op ps (Bind n (PVar t) sc) = op ((n, PVar t) : ps) sc
14371442 op ps (Bind n (Hole t) sc) = op ((n, Hole t) : ps) sc
@@ -1456,7 +1461,7 @@ refsIn (Bind n b t) = nub $ nb b ++ refsIn t
14561461 where nb (Let t v) = nub (refsIn t) ++ nub (refsIn v)
14571462 nb (Guess t v) = nub (refsIn t) ++ nub (refsIn v)
14581463 nb t = refsIn (binderTy t)
1459−refsIn (App f a) = nub (refsIn f ++ refsIn a)
1464+refsIn (App s f a) = nub (refsIn f ++ refsIn a)
14601465refsIn _ = []
14611466
14621467-- Make sure all the pattern bindings are as far out as possible
@@ -1493,9 +1498,9 @@ liftPats tm = let (tm', ps) = runState (getPats tm) [] in
14931498 return (Bind n (Hole t') sc')
14941499
14951500
1496− getPats (App f a) = do f' <- getPats f
1497− a' <- getPats a
1498− return (App f' a')
1501+ getPats (App s f a) = do f' <- getPats f
1502+ a' <- getPats a
1503+ return (App s f' a')
14991504 getPats t = return t
15001505
15011506allTTNames :: Eq n => TT n -> [n]
@@ -1505,7 +1510,7 @@ allTTNames = nub . allNamesIn
15051510 where nb (Let t v) = allNamesIn t ++ allNamesIn v
15061511 nb (Guess t v) = allNamesIn t ++ allNamesIn v
15071512 nb t = allNamesIn (binderTy t)
1508− allNamesIn (App f a) = allNamesIn f ++ allNamesIn a
1513+ allNamesIn (App _ f a) = allNamesIn f ++ allNamesIn a
15091514 allNamesIn _ = []
15101515
15111516
@@ -1528,7 +1533,7 @@ pprintTT bound tm = pp startPrec bound tm
15281533 | otherwise = text ("{{{V" ++ show i ++ "}}}")
15291534 pp p bound (Bind n b sc) = ppb p bound n b $
15301535 pp startPrec (n:bound) sc
1531− pp p bound (App tm1 tm2) =
1536+ pp p bound (App _ tm1 tm2) =
15321537 bracket p appPrec . group . hang 2 $
15331538 pp appPrec bound tm1 <> line <>
15341539 pp (appPrec + 1) bound tm2