~ubuntu-branches/ubuntu/wily/agda/wily-proposed

« back to all changes in this revision

Viewing changes to src/full/Agda/TypeChecking/Reduce.hs

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-08-05 06:38:12 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140805063812-io8e77niomivhd49
Tags: 2.4.0.2-1
* [6e140ac] Imported Upstream version 2.4.0.2
* [2049fc8] Update Build-Depends to match control
* [93dc4d4] Install the new primitives
* [e48f40f] Fix typo dev→doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
{-# LANGUAGE CPP, PatternGuards,
2
 
             TypeSynonymInstances, FlexibleInstances #-}
 
1
{-# LANGUAGE CPP #-}
 
2
{-# LANGUAGE FlexibleInstances #-}
 
3
{-# LANGUAGE PatternGuards #-}
 
4
{-# LANGUAGE TupleSections #-}
 
5
{-# LANGUAGE TypeSynonymInstances #-}
3
6
 
4
7
module Agda.TypeChecking.Reduce where
5
8
 
6
9
import Prelude hiding (mapM)
7
 
import Control.Monad.State hiding (mapM)
8
10
import Control.Monad.Reader hiding (mapM)
9
 
import Control.Monad.Error hiding (mapM)
10
11
import Control.Applicative
 
12
 
11
13
import Data.List as List hiding (sort)
12
 
import qualified Data.Map as Map
 
14
import Data.Maybe
13
15
import Data.Map (Map)
14
 
import qualified Data.Set as Set
15
16
import Data.Traversable
16
17
import Data.Hashable
17
18
 
18
19
import Agda.Syntax.Position
19
 
import Agda.Syntax.Common
 
20
import Agda.Syntax.Common hiding (Arg, Dom, NamedArg, ArgInfo)
20
21
import Agda.Syntax.Internal
21
22
import Agda.Syntax.Scope.Base (Scope)
22
23
import Agda.Syntax.Literal
23
24
 
24
 
import Agda.TypeChecking.Monad
25
 
import Agda.TypeChecking.Monad.Context
26
 
import Agda.TypeChecking.Monad.Builtin
 
25
import Agda.TypeChecking.Monad hiding ( underAbstraction_, enterClosure, isInstantiatedMeta
 
26
                                      , reportSDoc, reportSLn, getConstInfo
 
27
                                      , lookupMeta )
 
28
import qualified Agda.TypeChecking.Monad as TCM
 
29
import Agda.TypeChecking.Monad.Builtin hiding (getPrimitive, constructorForm)
27
30
import Agda.TypeChecking.Substitute
28
 
import Agda.TypeChecking.Free
 
31
import Agda.TypeChecking.CompiledClause
29
32
import Agda.TypeChecking.EtaContract
30
 
import Agda.TypeChecking.CompiledClause
31
33
import {-# SOURCE #-} Agda.TypeChecking.Pretty
32
34
 
33
 
import {-# SOURCE #-} Agda.TypeChecking.Level
34
35
import {-# SOURCE #-} Agda.TypeChecking.Patterns.Match
35
36
import {-# SOURCE #-} Agda.TypeChecking.CompiledClause.Match
36
37
 
 
38
import Agda.TypeChecking.Reduce.Monad
 
39
 
37
40
import Agda.Utils.Monad
38
41
import Agda.Utils.HashMap (HashMap)
39
 
import qualified Agda.Utils.HashMap as HMap
 
42
import Agda.Utils.Maybe
 
43
import Agda.Utils.Monad
 
44
import Agda.Utils.Tuple
40
45
 
41
46
#include "../undefined.h"
42
47
import Agda.Utils.Impossible
43
48
 
44
 
traceFun :: String -> TCM a -> TCM a
45
 
traceFun s m = do
46
 
  reportSLn "tc.inst" 100 $ "[ " ++ s
47
 
  x <- m
48
 
  reportSLn "tc.inst" 100 $ "]"
49
 
  return x
50
 
 
51
 
traceFun' :: Show a => String -> TCM a -> TCM a
52
 
traceFun' s m = do
53
 
  reportSLn "tc.inst" 100 $ "[ " ++ s
54
 
  x <- m
55
 
  reportSLn "tc.inst" 100 $ "  result = " ++ show x ++ "\n]"
56
 
  return x
 
49
instantiate :: Instantiate a => a -> TCM a
 
50
instantiate = runReduceM . instantiate'
 
51
 
 
52
instantiateFull :: InstantiateFull a => a -> TCM a
 
53
instantiateFull = runReduceM . instantiateFull'
 
54
 
 
55
reduce :: Reduce a => a -> TCM a
 
56
reduce = runReduceM . reduce'
 
57
 
 
58
reduceB :: Reduce a => a -> TCM (Blocked a)
 
59
reduceB = runReduceM . reduceB'
 
60
 
 
61
normalise :: Normalise a => a -> TCM a
 
62
normalise = runReduceM . normalise'
 
63
 
 
64
simplify :: Simplify a => a -> TCM a
 
65
simplify = runReduceM . simplify'
57
66
 
58
67
-- | Instantiate something.
59
68
--   Results in an open meta variable or a non meta.
60
69
--   Doesn't do any reduction, and preserves blocking tags (when blocking meta
61
70
--   is uninstantiated).
62
71
class Instantiate t where
63
 
    instantiate :: t -> TCM t
 
72
    instantiate' :: t -> ReduceM t
64
73
 
65
74
instance Instantiate Term where
66
 
  instantiate t@(MetaV x args) = do
 
75
  instantiate' t@(MetaV x es) = do
67
76
    mi <- mvInstantiation <$> lookupMeta x
68
77
    case mi of
69
 
      InstV a                        -> instantiate $ a `apply` args
70
 
      Open                           -> return t
71
 
      OpenIFS                        -> return t
72
 
      BlockedConst _                 -> return t
73
 
      PostponedTypeCheckingProblem _ -> return t
74
 
      InstS _                        -> __IMPOSSIBLE__
75
 
  instantiate (Level l) = levelTm <$> instantiate l
76
 
  instantiate (Sort s) = sortTm <$> instantiate s
77
 
  instantiate v@Shared{} = updateSharedTerm instantiate v
78
 
  instantiate t = return t
 
78
      InstV a                          -> instantiate' $ a `applyE` es
 
79
      Open                             -> return t
 
80
      OpenIFS                          -> return t
 
81
      BlockedConst _                   -> return t
 
82
      PostponedTypeCheckingProblem _ _ -> return t
 
83
      InstS _                          -> __IMPOSSIBLE__
 
84
  instantiate' (Level l) = levelTm <$> instantiate' l
 
85
  instantiate' (Sort s) = sortTm <$> instantiate' s
 
86
  instantiate' v@Shared{} =
 
87
    __IMPOSSIBLE__ -- updateSharedTerm instantiate' v
 
88
  instantiate' t = return t
79
89
 
80
90
instance Instantiate Level where
81
 
  instantiate (Max as) = levelMax <$> instantiate as
 
91
  instantiate' (Max as) = levelMax <$> instantiate' as
82
92
 
83
93
instance Instantiate PlusLevel where
84
 
  instantiate l@ClosedLevel{} = return l
85
 
  instantiate (Plus n a) = Plus n <$> instantiate a
 
94
  instantiate' l@ClosedLevel{} = return l
 
95
  instantiate' (Plus n a) = Plus n <$> instantiate' a
86
96
 
87
97
instance Instantiate LevelAtom where
88
 
  instantiate l = case l of
 
98
  instantiate' l = case l of
89
99
    MetaLevel m vs -> do
90
 
      v <- instantiate (MetaV m vs)
 
100
      v <- instantiate' (MetaV m vs)
91
101
      case ignoreSharing v of
92
102
        MetaV m vs -> return $ MetaLevel m vs
93
103
        _          -> return $ UnreducedLevel v
94
 
    UnreducedLevel l -> UnreducedLevel <$> instantiate l
 
104
    UnreducedLevel l -> UnreducedLevel <$> instantiate' l
95
105
    _ -> return l
96
106
 
97
107
instance Instantiate a => Instantiate (Blocked a) where
98
 
  instantiate v@NotBlocked{} = return v
99
 
  instantiate v@(Blocked x u) = do
 
108
  instantiate' v@NotBlocked{} = return v
 
109
  instantiate' v@(Blocked x u) = do
100
110
    mi <- mvInstantiation <$> lookupMeta x
101
111
    case mi of
102
 
      InstV _                        -> notBlocked <$> instantiate u
103
 
      Open                           -> return v
104
 
      OpenIFS                        -> return v
105
 
      BlockedConst _                 -> return v
106
 
      PostponedTypeCheckingProblem _ -> return v
107
 
      InstS _                        -> __IMPOSSIBLE__
 
112
      InstV _                          -> notBlocked <$> instantiate' u
 
113
      Open                             -> return v
 
114
      OpenIFS                          -> return v
 
115
      BlockedConst _                   -> return v
 
116
      PostponedTypeCheckingProblem _ _ -> return v
 
117
      InstS _                          -> __IMPOSSIBLE__
108
118
 
109
119
instance Instantiate Type where
110
 
    instantiate (El s t) = El <$> instantiate s <*> instantiate t
 
120
    instantiate' (El s t) = El <$> instantiate' s <*> instantiate' t
111
121
 
112
122
instance Instantiate Sort where
113
 
  instantiate s = case s of
114
 
    Type l -> levelSort <$> instantiate l
 
123
  instantiate' s = case s of
 
124
    Type l -> levelSort <$> instantiate' l
115
125
    _      -> return s
116
126
 
117
127
instance Instantiate Elim where
118
 
  instantiate (Apply v) = Apply <$> instantiate v
119
 
  instantiate (Proj f)  = pure $ Proj f
 
128
  instantiate' (Apply v) = Apply <$> instantiate' v
 
129
  instantiate' (Proj f)  = pure $ Proj f
120
130
 
121
131
instance Instantiate t => Instantiate (Abs t) where
122
 
  instantiate = traverse instantiate
 
132
  instantiate' = traverse instantiate'
123
133
 
124
134
instance Instantiate t => Instantiate (Arg t) where
125
 
    instantiate = traverse instantiate
 
135
    instantiate' = traverse instantiate'
126
136
 
127
137
instance Instantiate t => Instantiate (Dom t) where
128
 
    instantiate = traverse instantiate
 
138
    instantiate' = traverse instantiate'
129
139
 
130
140
instance Instantiate t => Instantiate [t] where
131
 
    instantiate = traverse instantiate
 
141
    instantiate' = traverse instantiate'
132
142
 
133
143
instance (Instantiate a, Instantiate b) => Instantiate (a,b) where
134
 
    instantiate (x,y) = (,) <$> instantiate x <*> instantiate y
 
144
    instantiate' (x,y) = (,) <$> instantiate' x <*> instantiate' y
135
145
 
136
146
 
137
147
instance (Instantiate a, Instantiate b,Instantiate c) => Instantiate (a,b,c) where
138
 
    instantiate (x,y,z) = (,,) <$> instantiate x <*> instantiate y <*> instantiate z
 
148
    instantiate' (x,y,z) = (,,) <$> instantiate' x <*> instantiate' y <*> instantiate' z
139
149
 
140
150
instance Instantiate a => Instantiate (Closure a) where
141
 
    instantiate cl = do
142
 
        x <- enterClosure cl instantiate
 
151
    instantiate' cl = do
 
152
        x <- enterClosure cl instantiate'
143
153
        return $ cl { clValue = x }
144
154
 
145
155
instance Instantiate Telescope where
146
 
  instantiate tel = return tel
 
156
  instantiate' tel = return tel
147
157
 
148
158
instance Instantiate Constraint where
149
 
  instantiate (ValueCmp cmp t u v) = do
150
 
    (t,u,v) <- instantiate (t,u,v)
 
159
  instantiate' (ValueCmp cmp t u v) = do
 
160
    (t,u,v) <- instantiate' (t,u,v)
151
161
    return $ ValueCmp cmp t u v
152
 
  instantiate (ElimCmp cmp t v as bs) =
153
 
    ElimCmp cmp <$> instantiate t <*> instantiate v <*> instantiate as <*> instantiate bs
154
 
  instantiate (LevelCmp cmp u v)   = uncurry (LevelCmp cmp) <$> instantiate (u,v)
155
 
  instantiate (TypeCmp cmp a b)    = uncurry (TypeCmp cmp) <$> instantiate (a,b)
156
 
  instantiate (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp)  <$> instantiate (tela,telb)
157
 
  instantiate (SortCmp cmp a b)    = uncurry (SortCmp cmp) <$> instantiate (a,b)
158
 
  instantiate (Guarded c pid)      = Guarded <$> instantiate c <*> pure pid
159
 
  instantiate (UnBlock m)          = return $ UnBlock m
160
 
  instantiate (FindInScope m args) = FindInScope m <$> mapM instantiate args
161
 
  instantiate (IsEmpty r t)        = IsEmpty r <$> instantiate t
 
162
  instantiate' (ElimCmp cmp t v as bs) =
 
163
    ElimCmp cmp <$> instantiate' t <*> instantiate' v <*> instantiate' as <*> instantiate' bs
 
164
  instantiate' (LevelCmp cmp u v)   = uncurry (LevelCmp cmp) <$> instantiate' (u,v)
 
165
  instantiate' (TypeCmp cmp a b)    = uncurry (TypeCmp cmp) <$> instantiate' (a,b)
 
166
  instantiate' (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp)  <$> instantiate' (tela,telb)
 
167
  instantiate' (SortCmp cmp a b)    = uncurry (SortCmp cmp) <$> instantiate' (a,b)
 
168
  instantiate' (Guarded c pid)      = Guarded <$> instantiate' c <*> pure pid
 
169
  instantiate' (UnBlock m)          = return $ UnBlock m
 
170
  instantiate' (FindInScope m args) = FindInScope m <$> mapM instantiate' args
 
171
  instantiate' (IsEmpty r t)        = IsEmpty r <$> instantiate' t
162
172
 
163
173
instance (Ord k, Instantiate e) => Instantiate (Map k e) where
164
 
    instantiate = traverse instantiate
 
174
    instantiate' = traverse instantiate'
165
175
 
166
176
 
167
177
---------------------------------------------------------------------------
177
187
    NotBlocked _           -> unblocked (ignoreBlocking t)
178
188
 
179
189
ifBlockedType :: MonadTCM tcm => Type -> (MetaId -> Type -> tcm a) -> (Type -> tcm a) -> tcm a
180
 
ifBlockedType t blocked unblocked = do
181
 
  t <- liftTCM $ reduceB t
182
 
  case ignoreSharing . unEl <$> t of
183
 
    Blocked m _            -> blocked m (ignoreBlocking t)
184
 
    NotBlocked (MetaV m _) -> blocked m (ignoreBlocking t)
185
 
    NotBlocked _           -> unblocked (ignoreBlocking t)
 
190
ifBlockedType (El s t) blocked unblocked =
 
191
  ifBlocked t (\ m v -> blocked m $ El s v) (\ v -> unblocked $ El s v)
186
192
 
187
193
class Reduce t where
188
 
    reduce  :: t -> TCM t
189
 
    reduceB :: t -> TCM (Blocked t)
 
194
    reduce'  :: t -> ReduceM t
 
195
    reduceB' :: t -> ReduceM (Blocked t)
190
196
 
191
 
    reduce  t = ignoreBlocking <$> reduceB t
192
 
    reduceB t = notBlocked <$> reduce t
 
197
    reduce'  t = ignoreBlocking <$> reduceB' t
 
198
    reduceB' t = notBlocked <$> reduce' t
193
199
 
194
200
instance Reduce Type where
195
 
    reduce  (El s t) = El s <$> reduce t
196
 
    reduceB (El s t) = fmap (El s) <$> reduceB t
 
201
    reduce'  (El s t) = El s <$> reduce' t
 
202
    reduceB' (El s t) = fmap (El s) <$> reduceB' t
197
203
 
198
204
instance Reduce Sort where
199
 
    reduce s = {-# SCC "reduce<Sort>" #-}
200
 
      ifM (not <$> hasUniversePolymorphism) (red s)
201
 
      $ red =<< instantiateFull s
 
205
    reduce' s = {-# SCC "reduce'<Sort>" #-}
 
206
      ifNotM hasUniversePolymorphism (red s) $ {- else -} red =<< instantiateFull' s
202
207
      where
203
208
        red s = do
204
 
          s <- instantiate s
 
209
          s <- instantiate' s
205
210
          case s of
206
211
            DLub s1 s2 -> do
207
 
              s <- dLub <$> reduce s1 <*> reduce s2
 
212
              s <- dLub <$> reduce' s1 <*> reduce' s2
208
213
              case s of
209
214
                DLub{}  -> return s
210
 
                _       -> reduce s   -- TODO: not so nice
 
215
                _       -> reduce' s   -- TODO: not so nice
211
216
            Prop       -> return s
212
 
            Type s'    -> levelSort <$> reduce s'
 
217
            Type s'    -> levelSort <$> reduce' s'
213
218
            Inf        -> return Inf
214
219
 
215
220
instance Reduce Elim where
216
 
  reduce (Apply v) = Apply <$> reduce v
217
 
  reduce (Proj f)  = pure $ Proj f
 
221
  reduce' (Apply v) = Apply <$> reduce' v
 
222
  reduce' (Proj f)  = pure $ Proj f
218
223
 
219
224
instance Reduce Level where
220
 
  reduce  (Max as) = levelMax <$> mapM reduce as
221
 
  reduceB (Max as) = fmap levelMax . traverse id <$> traverse reduceB as
 
225
  reduce'  (Max as) = levelMax <$> mapM reduce' as
 
226
  reduceB' (Max as) = fmap levelMax . traverse id <$> traverse reduceB' as
222
227
 
223
228
instance Reduce PlusLevel where
224
 
  reduceB l@ClosedLevel{} = return $ notBlocked l
225
 
  reduceB (Plus n l) = fmap (Plus n) <$> reduceB l
 
229
  reduceB' l@ClosedLevel{} = return $ notBlocked l
 
230
  reduceB' (Plus n l) = fmap (Plus n) <$> reduceB' l
226
231
 
227
232
instance Reduce LevelAtom where
228
 
  reduceB l = case l of
 
233
  reduceB' l = case l of
229
234
    MetaLevel m vs   -> fromTm (MetaV m vs)
230
235
    NeutralLevel v   -> return $ NotBlocked $ NeutralLevel v
231
236
    BlockedLevel m v ->
233
238
    UnreducedLevel v -> fromTm v
234
239
    where
235
240
      fromTm v = do
236
 
        bv <- reduceB v
 
241
        bv <- reduceB' v
237
242
        let v = ignoreBlocking bv
238
243
        case ignoreSharing <$> bv of
239
244
          NotBlocked (MetaV m vs) -> return $ NotBlocked $ MetaLevel m vs
242
247
 
243
248
 
244
249
instance (Subst t, Reduce t) => Reduce (Abs t) where
245
 
  reduce b@(Abs x _) = Abs x <$> underAbstraction_ b reduce
246
 
  reduce (NoAbs x v) = NoAbs x <$> reduce v
 
250
  reduce' b@(Abs x _) = Abs x <$> underAbstraction_ b reduce'
 
251
  reduce' (NoAbs x v) = NoAbs x <$> reduce' v
247
252
 
248
253
-- Lists are never blocked
249
254
instance Reduce t => Reduce [t] where
250
 
    reduce = traverse reduce
 
255
    reduce' = traverse reduce'
251
256
 
252
257
instance Reduce t => Reduce (Arg t) where
253
 
    reduce a@(Arg h Irrelevant t) = return a  -- Don't reduce irr. args!?
254
 
    reduce a                      = traverse reduce a
 
258
    reduce' a = case getRelevance a of
 
259
                 Irrelevant -> return a             -- Don't reduce' irr. args!?
 
260
                 _          -> traverse reduce' a
255
261
 
256
 
    reduceB t = traverse id <$> traverse reduceB t
 
262
    reduceB' t = traverse id <$> traverse reduceB' t
257
263
 
258
264
instance Reduce t => Reduce (Dom t) where
259
 
    reduce = traverse reduce
260
 
    reduceB t = traverse id <$> traverse reduceB t
 
265
    reduce' = traverse reduce'
 
266
    reduceB' t = traverse id <$> traverse reduceB' t
261
267
 
262
268
-- Tuples are never blocked
263
269
instance (Reduce a, Reduce b) => Reduce (a,b) where
264
 
    reduce (x,y)  = (,) <$> reduce x <*> reduce y
 
270
    reduce' (x,y)  = (,) <$> reduce' x <*> reduce' y
265
271
 
266
272
instance (Reduce a, Reduce b,Reduce c) => Reduce (a,b,c) where
267
 
    reduce (x,y,z) = (,,) <$> reduce x <*> reduce y <*> reduce z
 
273
    reduce' (x,y,z) = (,,) <$> reduce' x <*> reduce' y <*> reduce' z
268
274
 
269
275
instance Reduce Term where
270
 
  reduceB v = {-# SCC "reduce<Term>" #-} do
271
 
    v <- instantiate v
 
276
  reduceB' v = {-# SCC "reduce'<Term>" #-} do
 
277
    v <- instantiate' v
 
278
    let done = return $ notBlocked v
272
279
    case v of
273
280
--    Andreas, 2012-11-05 not reducing meta args does not destroy anything
274
281
--    and seems to save 2% sec on the standard library
275
 
      MetaV x args -> return $ notBlocked v
276
 
      Def f args   -> unfoldDefinition False reduceB (Def f []) f args
277
 
      Con c args   -> do
278
 
          -- Constructors can reduce when they come from an
 
282
--      MetaV x args -> notBlocked . MetaV x <$> reduce' args
 
283
      MetaV x es -> done
 
284
      Def f es   -> unfoldDefinitionE False reduceB' (Def f []) f es
 
285
      Con c args -> do
 
286
          -- Constructors can reduce' when they come from an
279
287
          -- instantiated module.
280
 
          v <- unfoldDefinition False reduceB (Con c []) c args
 
288
          v <- unfoldDefinition False reduceB' (Con c []) (conName c) args
281
289
          traverse reduceNat v
282
 
      Sort s   -> fmap sortTm <$> reduceB s
283
 
      Level l  -> fmap levelTm <$> reduceB l
284
 
      Pi _ _   -> return $ notBlocked v
285
 
      Lit _    -> return $ notBlocked v
286
 
      Var _ _  -> return $ notBlocked v
287
 
      Lam _ _  -> return $ notBlocked v
288
 
      DontCare _ -> return $ notBlocked v
289
 
      Shared{}   -> updateSharedTermF reduceB v
 
290
      Sort s   -> fmap sortTm <$> reduceB' s
 
291
      Level l  -> ifM (elem LevelReductions <$> asks envAllowedReductions)
 
292
                    {- then -} (fmap levelTm <$> reduceB' l)
 
293
                    {- else -} done
 
294
      -- Level l  -> fmap levelTm <$> reduceB' l
 
295
      Pi _ _   -> done
 
296
      Lit _    -> done
 
297
      Var _ _  -> done
 
298
      Lam _ _  -> done
 
299
      DontCare _ -> done
 
300
      Shared{}   -> __IMPOSSIBLE__ -- updateSharedTermF reduceB' v
290
301
    where
291
302
      -- NOTE: reduceNat can traverse the entire term.
292
 
      reduceNat v@Shared{} = updateSharedTerm reduceNat v
 
303
      reduceNat v@Shared{} = __IMPOSSIBLE__ -- updateSharedTerm reduceNat v
293
304
      reduceNat v@(Con c []) = do
294
305
        mz  <- getBuiltin' builtinZero
295
306
        case v of
296
307
          _ | Just v == mz  -> return $ Lit $ LitInt (getRange c) 0
297
308
          _                 -> return v
298
 
      reduceNat v@(Con c [Arg NotHidden Relevant w]) = do
 
309
      reduceNat v@(Con c [a]) | notHidden a && isRelevant a = do
299
310
        ms  <- fmap ignoreSharing <$> getBuiltin' builtinSuc
300
311
        case v of
301
 
          _ | Just (Con c []) == ms -> inc <$> reduce w
 
312
          _ | Just (Con c []) == ms -> inc <$> reduce' (unArg a)
302
313
          _                         -> return v
303
314
          where
304
315
            inc w = case ignoreSharing w of
305
316
              Lit (LitInt r n) -> Lit (LitInt (fuseRange c r) $ n + 1)
306
 
              _                -> Con c [Arg NotHidden Relevant w]
 
317
              _                -> Con c [defaultArg w]
307
318
      reduceNat v = return v
308
319
 
309
320
-- | If the first argument is 'True', then a single delayed clause may
310
321
-- be unfolded.
311
322
unfoldDefinition ::
312
 
  Bool -> (Term -> TCM (Blocked Term)) ->
313
 
  Term -> QName -> Args -> TCM (Blocked Term)
314
 
unfoldDefinition unfoldDelayed keepGoing v0 f args =
 
323
  Bool -> (Term -> ReduceM (Blocked Term)) ->
 
324
  Term -> QName -> Args -> ReduceM (Blocked Term)
 
325
unfoldDefinition b keepGoing v f args = snd <$> do
 
326
  unfoldDefinition' b (\ t -> (NoSimplification,) <$> keepGoing t) v f $
 
327
    map Apply args
 
328
 
 
329
unfoldDefinitionE ::
 
330
  Bool -> (Term -> ReduceM (Blocked Term)) ->
 
331
  Term -> QName -> Elims -> ReduceM (Blocked Term)
 
332
unfoldDefinitionE b keepGoing v f es = snd <$>
 
333
  unfoldDefinition' b (\ t -> (NoSimplification,) <$> keepGoing t) v f es
 
334
 
 
335
unfoldDefinition' ::
 
336
  Bool -> (Term -> ReduceM (Simplification, Blocked Term)) ->
 
337
  Term -> QName -> Elims -> ReduceM (Simplification, Blocked Term)
 
338
unfoldDefinition' unfoldDelayed keepGoing v0 f es =
315
339
  {-# SCC "reduceDef" #-} do
316
340
  info <- getConstInfo f
317
 
  case theDef info of
 
341
  let def = theDef info
 
342
      v   = v0 `applyE` es
 
343
  case def of
318
344
    Constructor{conSrcCon = c} ->
319
 
      return $ notBlocked $ Con (c `withRangeOf` f) args
 
345
      retSimpl $ notBlocked $ Con (c `withRangeOf` f) [] `applyE` es
320
346
    Primitive{primAbstr = ConcreteDef, primName = x, primClauses = cls} -> do
321
 
      pf <- getPrimitive x
322
 
      reducePrimitive x v0 f args pf (defDelayed info)
323
 
                      (maybe [] id cls) (defCompiled info)
324
 
    _  -> reduceNormal v0 f (map notReduced args) (defDelayed info)
 
347
      pf <- fromMaybe __IMPOSSIBLE__ <$> getPrimitive' x
 
348
      reducePrimitive x v0 f es pf (defDelayed info) (defNonterminating info)
 
349
                      cls (defCompiled info)
 
350
    _  -> do
 
351
      allowed <- asks envAllowedReductions
 
352
{-
 
353
      -- case f is projection-like:
 
354
      if isProperProjection def then
 
355
        if ProjectionReductions `elem` allowed then do
 
356
          -- we cannot call elimView right away, since it calls back to reduce'
 
357
          -- get rid of projection if possible
 
358
          (simpl, w) <- onlyReduceProjections $ do
 
359
            reduceNormal (retSimpl <=< reduceB') v0 f (map notReduced args)
 
360
              (defDelayed info) (defNonterminating info)
 
361
              (defClauses info) (defCompiled info)
 
362
          -- Now @w@ should not have any reducible projection in the head.
 
363
          -- By not allowing reentrace (dontReduceProjections),
 
364
          -- we can now call elimView without risk of circularity.
 
365
          case w of
 
366
            Blocked{} -> return (simpl, w)
 
367
            NotBlocked w' -> do
 
368
              ev <- dontReduceProjections $ elimView w'
 
369
              case ev of
 
370
                DefElim f es -> performedSimplification' simpl $ do
 
371
                  reduceDefElim f es
 
372
                _ -> return (simpl, w)
 
373
        else retSimpl $ notBlocked v
 
374
       -- case f is not a projection:
 
375
       else if FunctionReductions `elem` allowed then
 
376
        -- proceed as before, without calling elimView
 
377
-}
 
378
      if FunctionReductions `elem` allowed ||
 
379
         (isJust (isProjection_ def) && ProjectionReductions `elem` allowed)  -- includes projection-like
 
380
       then
 
381
        reduceNormalE keepGoing v0 f (map notReduced es)
 
382
                       (defDelayed info) (defNonterminating info)
325
383
                       (defClauses info) (defCompiled info)
 
384
        else retSimpl $ notBlocked v
 
385
 
326
386
  where
327
 
    reducePrimitive x v0 f args pf delayed cls mcc
328
 
        | n < ar    = return $ notBlocked $ v0 `apply` args -- not fully applied
329
 
        | otherwise = {-# SCC "reducePrimitive" #-} do
330
 
            let (args1,args2) = genericSplitAt ar args
331
 
            r <- def args1
332
 
            case r of
333
 
              NoReduction args1' ->
334
 
                if null cls then
335
 
                  return $ apply (Def f []) <$> traverse id (map mredToBlocked args1' ++ map notBlocked args2)
336
 
                else
337
 
                  reduceNormal v0 f (args1' ++
338
 
                                     map notReduced args2)
339
 
                               delayed cls mcc
340
 
              YesReduction v ->
341
 
                keepGoing $ v `apply` args2
342
 
        where
343
 
            n   = genericLength args
344
 
            ar  = primFunArity pf
345
 
            def = primFunImplementation pf
346
 
            mredToBlocked :: MaybeReduced a -> Blocked a
347
 
            mredToBlocked (MaybeRed NotReduced  x) = notBlocked x
348
 
            mredToBlocked (MaybeRed (Reduced b) x) = x <$ b
349
 
 
350
 
    reduceNormal :: Term -> QName -> [MaybeReduced (Arg Term)] -> Delayed -> [Clause] -> Maybe CompiledClauses -> TCM (Blocked Term)
351
 
    reduceNormal v0 f args delayed def mcc = {-# SCC "reduceNormal" #-} do
352
 
        case def of
353
 
          _ | Delayed <- delayed,
354
 
              not unfoldDelayed -> defaultResult
355
 
          [] -> defaultResult -- no definition for head
356
 
          cls -> do
357
 
            ev <- maybe (appDef' v0 cls args)
358
 
                        (\cc -> appDef v0 cc args) mcc
 
387
    retSimpl v = (,v) <$> getSimplification
 
388
{-
 
389
    reduceDefElim :: QName -> [Elim] -> ReduceM (Simplification, Blocked Term)
 
390
    reduceDefElim f es = do
 
391
      info <- getConstInfo f
 
392
      reduceNormalE keepGoing (Def f []) f (map notReduced es)
 
393
                       (defDelayed info) (defNonterminating info)
 
394
                       (defClauses info) (defCompiled info)
 
395
-}
 
396
 
 
397
    reducePrimitive x v0 f es pf delayed nonterminating cls mcc
 
398
      | genericLength es < ar
 
399
                  = retSimpl $ notBlocked $ v0 `applyE` es -- not fully applied
 
400
      | otherwise = {-# SCC "reducePrimitive" #-} do
 
401
          let (es1,es2) = genericSplitAt ar es
 
402
              args1     = fromMaybe __IMPOSSIBLE__ $ mapM isApplyElim es1
 
403
          r <- primFunImplementation pf args1
 
404
          case r of
 
405
            NoReduction args1' -> do
 
406
              let es1' = map (fmap Apply) args1'
 
407
              if null cls then do
 
408
                retSimpl $ applyE (Def f []) <$> do
 
409
                  traverse id $
 
410
                    map mredToBlocked es1' ++ map notBlocked es2
 
411
               else
 
412
                reduceNormalE keepGoing v0 f
 
413
                             (es1' ++ map notReduced es2)
 
414
                             delayed nonterminating cls mcc
 
415
            YesReduction simpl v -> performedSimplification' simpl $
 
416
              keepGoing $ v `applyE` es2
 
417
      where
 
418
          ar  = primFunArity pf
 
419
          mredToBlocked :: MaybeReduced a -> Blocked a
 
420
          mredToBlocked (MaybeRed NotReduced  x) = notBlocked x
 
421
          mredToBlocked (MaybeRed (Reduced b) x) = x <$ b
 
422
 
 
423
{-
 
424
    reduceNormal ::  (Term -> ReduceM (Simplification, Blocked Term)) -> Term -> QName -> [MaybeReduced (Arg Term)] -> Delayed -> Bool -> [Clause] -> Maybe CompiledClauses -> ReduceM (Simplification, Blocked Term)
 
425
    reduceNormal keepGoing v0 f args = reduceNormalE keepGoing v0 f $ map (fmap Apply) args
 
426
-}
 
427
 
 
428
    reduceNormalE :: (Term -> ReduceM (Simplification, Blocked Term)) -> Term -> QName -> [MaybeReduced Elim] -> Delayed -> Bool -> [Clause] -> Maybe CompiledClauses -> ReduceM (Simplification, Blocked Term)
 
429
    reduceNormalE keepGoing v0 f es delayed nonterminating def mcc = {-# SCC "reduceNormal" #-} do
 
430
      case def of
 
431
        _ | nonterminating -> defaultResult
 
432
        _ | Delayed <- delayed,
 
433
            not unfoldDelayed -> defaultResult
 
434
        [] -> defaultResult -- no definition for head
 
435
{- OBSOLETE
 
436
        -- stop here if we only want to reduce' (proper) projections
 
437
        -- but the symbol @f@ is not one
 
438
        cls -> ifM (asks envOnlyReduceProjections `and2M` do not . maybe False projProper <$> isProjection f) defaultResult $ do
 
439
-}
 
440
{-
 
441
        cls -> allowAllReductions $ do
 
442
            -- In subterms, we allow all reductions.
 
443
-}
 
444
        cls -> do
 
445
            ev <- appDefE_ f v0 cls mcc es
359
446
            case ev of
360
447
              NoReduction v -> do
361
 
                reportSDoc "tc.reduce" 90 $ vcat
362
 
                  [ text "*** tried to reduce " <+> prettyTCM f
363
 
                  , text "    args    " <+> prettyTCM (map (unArg . ignoreReduced) args)
364
 
                  , text "    stuck on" <+> prettyTCM (ignoreBlocking v) ]
365
 
                return v
366
 
              YesReduction v -> do
367
 
                reportSDoc "tc.reduce" 90 $ vcat
368
 
                  [ text "*** reduced definition: " <+> prettyTCM f
369
 
                  ]
370
 
                reportSDoc "tc.reduce" 100 $ text "    result" <+> prettyTCM v $$
371
 
                                             text "    raw   " <+> text (show v)
 
448
                traceSDoc "tc.reduce'" 90 (vcat
 
449
                  [ text "*** tried to reduce' " <+> prettyTCM f
 
450
                  , text "    es =  " <+> sep (map (prettyTCM . ignoreReduced) es)
 
451
--                  [ text "*** tried to reduce' " <+> prettyTCM vfull
 
452
                  , text "    stuck on" <+> prettyTCM (ignoreBlocking v) ]) $ do
 
453
                retSimpl v
 
454
              YesReduction simpl v -> performedSimplification' simpl $ do
 
455
                traceSDoc "tc.reduce'"  90 (text "*** reduced definition: " <+> prettyTCM f) $ do
 
456
                traceSDoc "tc.reduce'"  95 (text "    result" <+> prettyTCM v) $ do
 
457
                traceSDoc "tc.reduce'" 100 (text "    raw   " <+> text (show v)) $ do
372
458
                keepGoing v
373
 
      where defaultResult = return $ notBlocked $ v0 `apply` (map ignoreReduced args)
 
459
      where defaultResult = retSimpl $ notBlocked $ vfull
 
460
            vfull         = v0 `applyE` map ignoreReduced es
 
461
--      where defaultResult = retSimpl $ notBlocked $ v0 `apply` (map ignoreReduced args)
374
462
 
375
463
-- | Reduce a non-primitive definition if it is a copy linking to another def.
376
464
reduceDefCopy :: QName -> Args -> TCM (Reduced () Term)
377
465
reduceDefCopy f vs = do
378
 
  info <- getConstInfo f
 
466
  info <- TCM.getConstInfo f
379
467
  if (defCopy info) then reduceDef_ info f vs else return $ NoReduction ()
380
468
 
381
469
-- | Reduce a non-primitive definition once unless it is delayed.
382
470
reduceDef :: QName -> Args -> TCM (Reduced () Term)
383
471
reduceDef f vs = do
384
 
  info <- getConstInfo f
 
472
  info <- TCM.getConstInfo f
385
473
  reduceDef_ info f vs
386
474
 
387
475
reduceDef_ :: Definition -> QName -> Args -> TCM (Reduced () Term)
391
478
      args = map notReduced vs
392
479
      cls  = (defClauses info)
393
480
      mcc  = (defCompiled info)
394
 
      delayed = (defDelayed info)
395
 
  case delayed of
396
 
    Delayed -> return $ NoReduction ()
397
 
    NotDelayed -> do
398
 
      ev <- maybe (appDef' v0 cls args)
399
 
                  (\cc -> appDef v0 cc args) mcc
 
481
  if (defDelayed info == Delayed) || (defNonterminating info)
 
482
   then return $ NoReduction ()
 
483
   else do
 
484
      ev <- runReduceM $ appDef_ f v0 cls mcc args
400
485
      case ev of
401
 
        YesReduction t    -> return $ YesReduction t
402
 
        NoReduction args' -> return $ NoReduction ()
403
 
 
 
486
        YesReduction simpl t -> return $ YesReduction simpl t
 
487
        NoReduction args'    -> return $ NoReduction ()
 
488
 
 
489
-- | Reduce simple (single clause) definitions.
 
490
reduceHead :: Term -> TCM (Blocked Term)
 
491
reduceHead = runReduceM . reduceHead'
 
492
 
 
493
reduceHead' :: Term -> ReduceM (Blocked Term)
 
494
reduceHead' v = do -- ignoreAbstractMode $ do
 
495
  -- Andreas, 2013-02-18 ignoreAbstractMode leads to information leakage
 
496
  -- see Issue 796
 
497
 
 
498
  -- first, possibly rewrite literal v to constructor form
 
499
  v <- constructorForm v
 
500
  reportSDoc "tc.inj.reduce" 30 $ text "reduceHead" <+> prettyTCM v
 
501
  case ignoreSharing v of
 
502
    Def f es -> do
 
503
 
 
504
      abstractMode <- envAbstractMode <$> ask
 
505
      isAbstract <- treatAbstractly f
 
506
      reportSLn "tc.inj.reduce" 50 $
 
507
        "reduceHead: we are in " ++ show abstractMode++ "; " ++ show f ++
 
508
        " is treated " ++ if isAbstract then "abstractly" else "concretely"
 
509
 
 
510
      let v0  = Def f []
 
511
          red = unfoldDefinitionE False reduceHead' v0 f es
 
512
      def <- theDef <$> getConstInfo f
 
513
      case def of
 
514
        -- Andreas, 2012-11-06 unfold aliases (single clause terminating functions)
 
515
        -- see test/succeed/Issue747
 
516
        -- We restrict this to terminating functions to not make the
 
517
        -- type checker loop here on non-terminating functions.
 
518
        -- see test/fail/TerminationInfiniteRecord
 
519
        Function{ funClauses = [ _ ], funDelayed = NotDelayed, funTerminates = Just True } -> do
 
520
          reportSLn "tc.inj.reduce" 50 $ "reduceHead: head " ++ show f ++ " is Function"
 
521
          red
 
522
        Datatype{ dataClause = Just _ } -> red
 
523
        Record{ recClause = Just _ }    -> red
 
524
        _                               -> return $ notBlocked v
 
525
    _ -> return $ notBlocked v
 
526
 
 
527
-- | Apply a definition using the compiled clauses, or fall back to
 
528
--   ordinary clauses if no compiled clauses exist.
 
529
appDef_ :: QName -> Term -> [Clause] -> Maybe CompiledClauses -> MaybeReducedArgs -> ReduceM (Reduced (Blocked Term) Term)
 
530
appDef_ f v0 cls mcc args = appDefE_ f v0 cls mcc $ map (fmap Apply) args
 
531
 
 
532
appDefE_ :: QName -> Term -> [Clause] -> Maybe CompiledClauses -> MaybeReducedElims -> ReduceM (Reduced (Blocked Term) Term)
 
533
appDefE_ f v0 cls mcc args =
 
534
  local (\ e -> e { envAppDef = Just f }) $
 
535
  maybe (appDefE' v0 cls args)
 
536
        (\cc -> appDefE v0 cc args) mcc
 
537
 
 
538
 
 
539
-- | Apply a defined function to it's arguments, using the compiled clauses.
404
540
--   The original term is the first argument applied to the third.
405
 
appDef :: Term -> CompiledClauses -> MaybeReducedArgs -> TCM (Reduced (Blocked Term) Term)
406
 
appDef v cc args = liftTCM $ do
407
 
  r <- matchCompiled cc args
 
541
appDef :: Term -> CompiledClauses -> MaybeReducedArgs -> ReduceM (Reduced (Blocked Term) Term)
 
542
appDef v cc args = appDefE v cc $ map (fmap Apply) args
 
543
 
 
544
appDefE :: Term -> CompiledClauses -> MaybeReducedElims -> ReduceM (Reduced (Blocked Term) Term)
 
545
appDefE v cc es = do
 
546
  r <- matchCompiledE cc es
408
547
  case r of
409
 
    YesReduction t    -> return $ YesReduction t
410
 
    NoReduction args' -> return $ NoReduction $ fmap (apply v) args'
411
 
 
412
 
appDef' :: Term -> [Clause] -> MaybeReducedArgs -> TCM (Reduced (Blocked Term) Term)
413
 
appDef' _ [] _ = {- ' -} __IMPOSSIBLE__
414
 
appDef' v cls@(Clause {clausePats = ps} : _) args
415
 
  | m < n     = return $ NoReduction $ notBlocked $ v `apply` map ignoreReduced args
 
548
    YesReduction simpl t -> return $ YesReduction simpl t
 
549
    NoReduction es'      -> return $ NoReduction $ applyE v <$> es'
 
550
 
 
551
-- | Apply a defined function to it's arguments, using the original clauses.
 
552
appDef' :: Term -> [Clause] -> MaybeReducedArgs -> ReduceM (Reduced (Blocked Term) Term)
 
553
appDef' v cls args = appDefE' v cls $ map (fmap Apply) args
 
554
 
 
555
{- OLD.  With varying function arity, check for underapplication is UNSOUND.
 
556
appDefE' :: Term -> [Clause] -> MaybeReducedElims -> ReduceM (Reduced (Blocked Term) Term)
 
557
appDefE' _ [] _ = {- ' -} __IMPOSSIBLE__
 
558
appDefE' v cls@(Clause {clausePats = ps} : _) es
 
559
    -- case underapplied: no reduction
 
560
  | m < n     = return $ NoReduction $ notBlocked $ v `applyE` map ignoreReduced es
416
561
  | otherwise = do
417
 
    let (args0, args1) = splitAt n args
418
 
    r <- goCls cls (map ignoreReduced args0)
 
562
    let (es0, es1) = splitAt n es
 
563
    r <- goCls cls (map ignoreReduced es0)
419
564
    case r of
420
 
      YesReduction u -> return $ YesReduction $ u `apply` map ignoreReduced args1
421
 
      NoReduction v  -> return $ NoReduction $ (`apply` map ignoreReduced args1) <$> v
 
565
      YesReduction simpl u -> return $ YesReduction simpl $ u `applyE` map ignoreReduced es1
 
566
      NoReduction v        -> return $ NoReduction $ (`applyE` map ignoreReduced es1) <$> v
422
567
  where
423
568
 
424
569
    n = genericLength ps
425
 
    m = genericLength args
426
 
 
427
 
    goCls :: [Clause] -> Args -> TCM (Reduced (Blocked Term) Term)
428
 
    goCls [] args = typeError $ IncompletePatternMatching v args
429
 
    goCls (cl@(Clause { clausePats = pats
430
 
                      , clauseBody = body }) : cls) args = do
431
 
        (m, args) <- matchPatterns pats args
432
 
        case m of
433
 
            No            -> goCls cls args
434
 
            DontKnow Nothing  -> return $ NoReduction $ notBlocked $ v `apply` args
435
 
            DontKnow (Just m) -> return $ NoReduction $ blocked m $ v `apply` args
436
 
            Yes args'
437
 
              | hasBody body  -> return $ YesReduction (
438
 
                  -- TODO: let matchPatterns also return the reduced forms
439
 
                  -- of the original arguments!
440
 
                  app args' body)
441
 
              | otherwise         -> return $ NoReduction $ notBlocked $ v `apply` args
442
 
 
443
 
    hasBody (Body _) = True
444
 
    hasBody NoBody   = False
445
 
    hasBody (Bind b) = hasBody (unAbs b)
446
 
 
447
 
    app []               (Body v') = v'
448
 
    app (arg : args) (Bind b)  = app args $ absApp b arg -- CBN
449
 
    app  _                NoBody   = __IMPOSSIBLE__
450
 
    app (_ : _)  (Body _)  = __IMPOSSIBLE__
451
 
    app []               (Bind _)  = __IMPOSSIBLE__
452
 
 
 
570
    m = genericLength es
 
571
-}
 
572
appDefE' :: Term -> [Clause] -> MaybeReducedElims -> ReduceM (Reduced (Blocked Term) Term)
 
573
appDefE' v cls es = goCls cls $ map ignoreReduced es
 
574
  where
 
575
    goCls :: [Clause] -> [Elim] -> ReduceM (Reduced (Blocked Term) Term)
 
576
    goCls cl es = do
 
577
      reportSLn "tc.reduce'" 95 $ "Reduce.goCls tries reduction, #clauses = " ++ show (length cl)
 
578
      let cantReduce es = return $ NoReduction $ notBlocked $ v `applyE` es
 
579
      case cl of
 
580
        -- Andreas, 2013-10-26  In case of an incomplete match,
 
581
        -- we just do not reduce'.  This allows adding single function
 
582
        -- clauses after they have been type-checked, to type-check
 
583
        -- the remaining clauses (see Issue 907).
 
584
        [] -> cantReduce es -- WAS: typeError $ IncompletePatternMatching v es
 
585
        cl @ Clause { clauseBody = body } : cls -> do
 
586
          let pats = namedClausePats cl
 
587
              n    = length pats
 
588
          -- if clause is underapplied, skip to next clause
 
589
          if length es < n then goCls cls es else do
 
590
            let (es0, es1) = splitAt n es
 
591
            (m, es0) <- matchCopatterns pats es0
 
592
            es <- return $ es0 ++ es1
 
593
            case m of
 
594
              No                -> goCls cls es
 
595
              DontKnow Nothing  -> cantReduce es
 
596
              DontKnow (Just m) -> return $ NoReduction $ blocked m $ v `applyE` es
 
597
              Yes simpl vs -- vs is the subst. for the variables bound in body
 
598
                | isJust (getBody body)  -- clause has body?
 
599
                                -> return $ YesReduction simpl $
 
600
                    -- TODO: let matchPatterns also return the reduced forms
 
601
                    -- of the original arguments!
 
602
                    -- Andreas, 2013-05-19 isn't this done now?
 
603
                    app vs body EmptyS `applyE` es1
 
604
                | otherwise     -> cantReduce es
 
605
 
 
606
 
 
607
    -- NEW version, building an explicit substitution from arguments
 
608
    -- and executing it using parallel substitution.
 
609
    -- Calculating the de Bruijn indices: ;-) for the Bind case
 
610
    --   Simply-typed version
 
611
    --   (we are not interested in types, only in de Bruijn indices here)
 
612
    --   Γ ⊢ σ : Δ
 
613
    --   Γ ⊢ v : A
 
614
    --   Γ ⊢ (σ,v) : Δ.A
 
615
    --   Δ ⊢ λ b : A → B
 
616
    --   Δ.A ⊢ b : B
 
617
    app :: [Term] -> ClauseBody -> Substitution -> Term
 
618
    app []       (Body v)           sigma = applySubst sigma v
 
619
--    app (v : vs) (Bind b) sigma = app es (absBody b) (v :# sigma) -- CBN
 
620
    app (v : vs) (Bind (Abs   _ b)) sigma = app vs b (v :# sigma) -- CBN
 
621
    app (v : vs) (Bind (NoAbs _ b)) sigma = app vs b sigma
 
622
    app  _        NoBody            sigma = __IMPOSSIBLE__
 
623
    app (_ : _)  (Body _)           sigma = __IMPOSSIBLE__
 
624
    app []       (Bind _)           sigma = __IMPOSSIBLE__
 
625
 
 
626
{- OLD version, one substitution after another
 
627
    app :: [Elim] -> ClauseBody -> Term
 
628
    app []           (Body v') = v'
 
629
    app (Proj f : es)    b         = app es b
 
630
    app (Apply arg : es) (Bind b)  = app es $ absApp b $ unArg arg -- CBN
 
631
    app  _            NoBody   = __IMPOSSIBLE__
 
632
    app (_ : _)      (Body _)  = __IMPOSSIBLE__
 
633
    app []           (Bind _)  = __IMPOSSIBLE__
 
634
-}
453
635
 
454
636
instance Reduce a => Reduce (Closure a) where
455
 
    reduce cl = do
456
 
        x <- enterClosure cl reduce
 
637
    reduce' cl = do
 
638
        x <- enterClosure cl reduce'
457
639
        return $ cl { clValue = x }
458
640
 
459
641
instance Reduce Telescope where
460
 
  reduce tel = return tel
 
642
  reduce' tel = return tel
461
643
 
462
644
instance Reduce Constraint where
463
 
  reduce (ValueCmp cmp t u v) = do
464
 
    (t,u,v) <- reduce (t,u,v)
 
645
  reduce' (ValueCmp cmp t u v) = do
 
646
    (t,u,v) <- reduce' (t,u,v)
465
647
    return $ ValueCmp cmp t u v
466
 
  reduce (ElimCmp cmp t v as bs) =
467
 
    ElimCmp cmp <$> reduce t <*> reduce v <*> reduce as <*> reduce bs
468
 
  reduce (LevelCmp cmp u v)    = uncurry (LevelCmp cmp) <$> reduce (u,v)
469
 
  reduce (TypeCmp cmp a b)     = uncurry (TypeCmp cmp) <$> reduce (a,b)
470
 
  reduce (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp)  <$> reduce (tela,telb)
471
 
  reduce (SortCmp cmp a b)     = uncurry (SortCmp cmp) <$> reduce (a,b)
472
 
  reduce (Guarded c pid)       = Guarded <$> reduce c <*> pure pid
473
 
  reduce (UnBlock m)           = return $ UnBlock m
474
 
  reduce (FindInScope m cands) = FindInScope m <$> mapM reduce cands
475
 
  reduce (IsEmpty r t)         = IsEmpty r <$> reduce t
 
648
  reduce' (ElimCmp cmp t v as bs) =
 
649
    ElimCmp cmp <$> reduce' t <*> reduce' v <*> reduce' as <*> reduce' bs
 
650
  reduce' (LevelCmp cmp u v)    = uncurry (LevelCmp cmp) <$> reduce' (u,v)
 
651
  reduce' (TypeCmp cmp a b)     = uncurry (TypeCmp cmp) <$> reduce' (a,b)
 
652
  reduce' (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp)  <$> reduce' (tela,telb)
 
653
  reduce' (SortCmp cmp a b)     = uncurry (SortCmp cmp) <$> reduce' (a,b)
 
654
  reduce' (Guarded c pid)       = Guarded <$> reduce' c <*> pure pid
 
655
  reduce' (UnBlock m)           = return $ UnBlock m
 
656
  reduce' (FindInScope m cands) = FindInScope m <$> mapM reduce' cands
 
657
  reduce' (IsEmpty r t)         = IsEmpty r <$> reduce' t
476
658
 
477
659
instance (Ord k, Reduce e) => Reduce (Map k e) where
478
 
    reduce = traverse reduce
 
660
    reduce' = traverse reduce'
 
661
 
 
662
---------------------------------------------------------------------------
 
663
-- * Simplification
 
664
---------------------------------------------------------------------------
 
665
 
 
666
-- | Only unfold definitions if this leads to simplification
 
667
--   which means that a constructor/literal pattern is matched.
 
668
class Simplify t where
 
669
  simplify' :: t -> ReduceM t
 
670
 
 
671
instance Simplify Term where
 
672
  simplify' v = do
 
673
    v <- instantiate' v
 
674
    case v of
 
675
      Def f vs   -> do
 
676
        let keepGoing v = (,NotBlocked v) <$> getSimplification
 
677
        (simpl, v) <- unfoldDefinition' False keepGoing (Def f []) f vs
 
678
        reportSDoc "tc.simplify'" 20 $
 
679
          text ("simplify': unfolding definition returns " ++ show simpl)
 
680
            <+> prettyTCM (ignoreBlocking v)
 
681
        case simpl of
 
682
          YesSimplification -> simplifyBlocked' v -- Dangerous, but if @simpl@ then @v /= Def f vs@
 
683
          NoSimplification  -> Def f <$> simplify' vs
 
684
      MetaV x vs -> MetaV x  <$> simplify' vs
 
685
      Con c vs   -> Con c    <$> simplify' vs
 
686
      Sort s     -> sortTm   <$> simplify' s
 
687
      Level l    -> levelTm  <$> simplify' l
 
688
      Pi a b     -> Pi       <$> simplify' a <*> simplify' b
 
689
      Lit l      -> return v
 
690
      Var i vs   -> Var i    <$> simplify' vs
 
691
      Lam h v    -> Lam h    <$> simplify' v
 
692
      DontCare v -> dontCare <$> simplify' v
 
693
      Shared{}   -> __IMPOSSIBLE__ -- updateSharedTerm simplify' v
 
694
 
 
695
simplifyBlocked' :: Simplify t => Blocked t -> ReduceM t
 
696
simplifyBlocked' (Blocked _ t)  = return t
 
697
simplifyBlocked' (NotBlocked t) = simplify' t
 
698
 
 
699
instance Simplify Type where
 
700
    simplify' (El s t) = El <$> simplify' s <*> simplify' t
 
701
 
 
702
instance Simplify Elim where
 
703
  simplify' (Apply v) = Apply <$> simplify' v
 
704
  simplify' (Proj f)  = pure $ Proj f
 
705
 
 
706
instance Simplify Sort where
 
707
    simplify' s = do
 
708
      case s of
 
709
        DLub s1 s2 -> dLub <$> simplify' s1 <*> simplify' s2
 
710
        Type s     -> levelSort <$> simplify' s
 
711
        Prop       -> return s
 
712
        Inf        -> return s
 
713
 
 
714
instance Simplify Level where
 
715
  simplify' (Max as) = levelMax <$> simplify' as
 
716
 
 
717
instance Simplify PlusLevel where
 
718
  simplify' l@ClosedLevel{} = return l
 
719
  simplify' (Plus n l) = Plus n <$> simplify' l
 
720
 
 
721
instance Simplify LevelAtom where
 
722
  simplify' l = do
 
723
    l <- instantiate' l
 
724
    case l of
 
725
      MetaLevel m vs   -> MetaLevel m <$> simplify' vs
 
726
      BlockedLevel m v -> BlockedLevel m <$> simplify' v
 
727
      NeutralLevel v   -> NeutralLevel   <$> simplify' v -- ??
 
728
      UnreducedLevel v -> UnreducedLevel <$> simplify' v -- ??
 
729
 
 
730
instance (Subst t, Simplify t) => Simplify (Abs t) where
 
731
    simplify' a@(Abs x _) = Abs x <$> underAbstraction_ a simplify'
 
732
    simplify' (NoAbs x v) = NoAbs x <$> simplify' v
 
733
 
 
734
instance Simplify t => Simplify (Arg t) where
 
735
    simplify' = traverse simplify'
 
736
 
 
737
instance Simplify t => Simplify (Named name t) where
 
738
    simplify' = traverse simplify'
 
739
 
 
740
instance Simplify t => Simplify (Dom t) where
 
741
    simplify' = traverse simplify'
 
742
 
 
743
instance Simplify t => Simplify [t] where
 
744
    simplify' = traverse simplify'
 
745
 
 
746
instance (Ord k, Simplify e) => Simplify (Map k e) where
 
747
    simplify' = traverse simplify'
 
748
 
 
749
instance Simplify a => Simplify (Maybe a) where
 
750
    simplify' = traverse simplify'
 
751
 
 
752
instance (Simplify a, Simplify b) => Simplify (a,b) where
 
753
    simplify' (x,y) = (,) <$> simplify' x <*> simplify' y
 
754
 
 
755
instance (Simplify a, Simplify b, Simplify c) => Simplify (a,b,c) where
 
756
    simplify' (x,y,z) =
 
757
        do  (x,(y,z)) <- simplify' (x,(y,z))
 
758
            return (x,y,z)
 
759
 
 
760
instance Simplify a => Simplify (Closure a) where
 
761
    simplify' cl = do
 
762
        x <- enterClosure cl simplify'
 
763
        return $ cl { clValue = x }
 
764
 
 
765
instance (Subst a, Simplify a) => Simplify (Tele a) where
 
766
  simplify' EmptyTel        = return EmptyTel
 
767
  simplify' (ExtendTel a b) = uncurry ExtendTel <$> simplify' (a, b)
 
768
 
 
769
instance Simplify ProblemConstraint where
 
770
  simplify' (PConstr pid c) = PConstr pid <$> simplify' c
 
771
 
 
772
instance Simplify Constraint where
 
773
  simplify' (ValueCmp cmp t u v) = do
 
774
    (t,u,v) <- simplify' (t,u,v)
 
775
    return $ ValueCmp cmp t u v
 
776
  simplify' (ElimCmp cmp t v as bs) =
 
777
    ElimCmp cmp <$> simplify' t <*> simplify' v <*> simplify' as <*> simplify' bs
 
778
  simplify' (LevelCmp cmp u v)    = uncurry (LevelCmp cmp) <$> simplify' (u,v)
 
779
  simplify' (TypeCmp cmp a b)     = uncurry (TypeCmp cmp) <$> simplify' (a,b)
 
780
  simplify' (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp) <$> simplify' (tela,telb)
 
781
  simplify' (SortCmp cmp a b)     = uncurry (SortCmp cmp) <$> simplify' (a,b)
 
782
  simplify' (Guarded c pid)       = Guarded <$> simplify' c <*> pure pid
 
783
  simplify' (UnBlock m)           = return $ UnBlock m
 
784
  simplify' (FindInScope m cands) = FindInScope m <$> mapM simplify' cands
 
785
  simplify' (IsEmpty r t)         = IsEmpty r <$> simplify' t
 
786
 
 
787
instance Simplify Bool where
 
788
  simplify' = return
 
789
 
 
790
instance Simplify Pattern where
 
791
  simplify' p = case p of
 
792
    VarP _       -> return p
 
793
    LitP _       -> return p
 
794
    ConP c mt ps -> ConP c <$> simplify' mt <*> simplify' ps
 
795
    DotP v       -> DotP <$> simplify' v
 
796
    ProjP _      -> return p
 
797
 
 
798
instance Simplify ClauseBody where
 
799
    simplify' (Body   t) = Body   <$> simplify' t
 
800
    simplify' (Bind   b) = Bind   <$> simplify' b
 
801
    simplify'  NoBody   = return NoBody
 
802
 
 
803
instance Simplify DisplayForm where
 
804
  simplify' (Display n ps v) = Display n <$> simplify' ps <*> return v
 
805
 
479
806
 
480
807
---------------------------------------------------------------------------
481
808
-- * Normalisation
482
809
---------------------------------------------------------------------------
483
810
 
484
811
class Normalise t where
485
 
    normalise :: t -> TCM t
 
812
    normalise' :: t -> ReduceM t
486
813
 
487
814
instance Normalise Sort where
488
 
    normalise s = do
489
 
      s <- reduce s
 
815
    normalise' s = do
 
816
      s <- reduce' s
490
817
      case s of
491
 
        DLub s1 s2 -> dLub <$> normalise s1 <*> normalise s2
 
818
        DLub s1 s2 -> dLub <$> normalise' s1 <*> normalise' s2
492
819
        Prop       -> return s
493
 
        Type s     -> levelSort <$> normalise s
 
820
        Type s     -> levelSort <$> normalise' s
494
821
        Inf        -> return Inf
495
822
 
496
823
instance Normalise Type where
497
 
    normalise (El s t) = El <$> normalise s <*> normalise t
 
824
    normalise' (El s t) = El <$> normalise' s <*> normalise' t
498
825
 
499
826
instance Normalise Term where
500
 
    normalise v =
501
 
        do  v <- reduce v
 
827
    normalise' v =
 
828
        do  v <- reduce' v
502
829
            case v of
503
 
                Var n vs    -> Var n <$> normalise vs
504
 
                Con c vs    -> Con c <$> normalise vs
505
 
                Def f vs    -> Def f <$> normalise vs
506
 
                MetaV x vs  -> MetaV x <$> normalise vs
 
830
                Var n vs    -> Var n <$> normalise' vs
 
831
                Con c vs    -> Con c <$> normalise' vs
 
832
                Def f vs    -> Def f <$> normalise' vs
 
833
                MetaV x vs  -> MetaV x <$> normalise' vs
507
834
                Lit _       -> return v
508
 
                Level l     -> levelTm <$> normalise l
509
 
                Lam h b     -> Lam h <$> normalise b
510
 
                Sort s      -> sortTm <$> normalise s
511
 
                Pi a b      -> uncurry Pi <$> normalise (a,b)
512
 
                Shared{}    -> updateSharedTerm normalise v
 
835
                Level l     -> levelTm <$> normalise' l
 
836
                Lam h b     -> Lam h <$> normalise' b
 
837
                Sort s      -> sortTm <$> normalise' s
 
838
                Pi a b      -> uncurry Pi <$> normalise' (a,b)
 
839
                Shared{}    -> __IMPOSSIBLE__ -- updateSharedTerm normalise' v
513
840
                DontCare _  -> return v
514
841
 
515
842
instance Normalise Elim where
516
 
  normalise (Apply v) = Apply <$> normalise v
517
 
  normalise (Proj f)  = pure $ Proj f
 
843
  normalise' (Apply v) = Apply <$> normalise' v
 
844
  normalise' (Proj f)  = pure $ Proj f
518
845
 
519
846
instance Normalise Level where
520
 
  normalise (Max as) = levelMax <$> normalise as
 
847
  normalise' (Max as) = levelMax <$> normalise' as
521
848
 
522
849
instance Normalise PlusLevel where
523
 
  normalise l@ClosedLevel{} = return l
524
 
  normalise (Plus n l) = Plus n <$> normalise l
 
850
  normalise' l@ClosedLevel{} = return l
 
851
  normalise' (Plus n l) = Plus n <$> normalise' l
525
852
 
526
853
instance Normalise LevelAtom where
527
 
  normalise l = do
528
 
    l <- reduce l
 
854
  normalise' l = do
 
855
    l <- reduce' l
529
856
    case l of
530
 
      MetaLevel m vs   -> MetaLevel m <$> normalise vs
531
 
      BlockedLevel m v -> BlockedLevel m <$> normalise v
532
 
      NeutralLevel v   -> NeutralLevel <$> normalise v
 
857
      MetaLevel m vs   -> MetaLevel m <$> normalise' vs
 
858
      BlockedLevel m v -> BlockedLevel m <$> normalise' v
 
859
      NeutralLevel v   -> NeutralLevel <$> normalise' v
533
860
      UnreducedLevel{} -> __IMPOSSIBLE__    -- I hope
534
861
 
535
862
instance Normalise ClauseBody where
536
 
    normalise (Body   t) = Body   <$> normalise t
537
 
    normalise (Bind   b) = Bind   <$> normalise b
538
 
    normalise  NoBody    = return NoBody
 
863
    normalise' (Body   t) = Body   <$> normalise' t
 
864
    normalise' (Bind   b) = Bind   <$> normalise' b
 
865
    normalise'  NoBody   = return NoBody
539
866
 
540
867
instance (Subst t, Normalise t) => Normalise (Abs t) where
541
 
    normalise a@(Abs x _) = Abs x <$> underAbstraction_ a normalise
542
 
    normalise (NoAbs x v) = NoAbs x <$> normalise v
 
868
    normalise' a@(Abs x _) = Abs x <$> underAbstraction_ a normalise'
 
869
    normalise' (NoAbs x v) = NoAbs x <$> normalise' v
543
870
 
544
871
instance Normalise t => Normalise (Arg t) where
545
 
    normalise a@(Arg h Irrelevant t) = return a -- Andreas, 2012-04-02: Do not normalize irrelevant terms!?
546
 
    normalise a = traverse normalise a
 
872
    normalise' a | isIrrelevant a = return a -- Andreas, 2012-04-02: Do not normalize irrelevant terms!?
 
873
                | otherwise                       = traverse normalise' a
 
874
 
 
875
instance Normalise t => Normalise (Named name t) where
 
876
    normalise' = traverse normalise'
547
877
 
548
878
instance Normalise t => Normalise (Dom t) where
549
 
    normalise = traverse normalise
 
879
    normalise' = traverse normalise'
550
880
 
551
881
instance Normalise t => Normalise [t] where
552
 
    normalise = traverse normalise
 
882
    normalise' = traverse normalise'
553
883
 
554
884
instance (Normalise a, Normalise b) => Normalise (a,b) where
555
 
    normalise (x,y) = (,) <$> normalise x <*> normalise y
 
885
    normalise' (x,y) = (,) <$> normalise' x <*> normalise' y
556
886
 
557
887
instance (Normalise a, Normalise b, Normalise c) => Normalise (a,b,c) where
558
 
    normalise (x,y,z) =
559
 
        do  (x,(y,z)) <- normalise (x,(y,z))
 
888
    normalise' (x,y,z) =
 
889
        do  (x,(y,z)) <- normalise' (x,(y,z))
560
890
            return (x,y,z)
561
891
 
562
892
instance Normalise a => Normalise (Closure a) where
563
 
    normalise cl = do
564
 
        x <- enterClosure cl normalise
 
893
    normalise' cl = do
 
894
        x <- enterClosure cl normalise'
565
895
        return $ cl { clValue = x }
566
896
 
567
897
instance (Subst a, Normalise a) => Normalise (Tele a) where
568
 
  normalise EmptyTel        = return EmptyTel
569
 
  normalise (ExtendTel a b) = uncurry ExtendTel <$> normalise (a, b)
 
898
  normalise' EmptyTel        = return EmptyTel
 
899
  normalise' (ExtendTel a b) = uncurry ExtendTel <$> normalise' (a, b)
570
900
 
571
901
instance Normalise ProblemConstraint where
572
 
  normalise (PConstr pid c) = PConstr pid <$> normalise c
 
902
  normalise' (PConstr pid c) = PConstr pid <$> normalise' c
573
903
 
574
904
instance Normalise Constraint where
575
 
  normalise (ValueCmp cmp t u v) = do
576
 
    (t,u,v) <- normalise (t,u,v)
 
905
  normalise' (ValueCmp cmp t u v) = do
 
906
    (t,u,v) <- normalise' (t,u,v)
577
907
    return $ ValueCmp cmp t u v
578
 
  normalise (ElimCmp cmp t v as bs) =
579
 
    ElimCmp cmp <$> normalise t <*> normalise v <*> normalise as <*> normalise bs
580
 
  normalise (LevelCmp cmp u v)    = uncurry (LevelCmp cmp) <$> normalise (u,v)
581
 
  normalise (TypeCmp cmp a b)     = uncurry (TypeCmp cmp) <$> normalise (a,b)
582
 
  normalise (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp) <$> normalise (tela,telb)
583
 
  normalise (SortCmp cmp a b)     = uncurry (SortCmp cmp) <$> normalise (a,b)
584
 
  normalise (Guarded c pid)       = Guarded <$> normalise c <*> pure pid
585
 
  normalise (UnBlock m)           = return $ UnBlock m
586
 
  normalise (FindInScope m cands) = FindInScope m <$> mapM normalise cands
587
 
  normalise (IsEmpty r t)         = IsEmpty r <$> normalise t
 
908
  normalise' (ElimCmp cmp t v as bs) =
 
909
    ElimCmp cmp <$> normalise' t <*> normalise' v <*> normalise' as <*> normalise' bs
 
910
  normalise' (LevelCmp cmp u v)    = uncurry (LevelCmp cmp) <$> normalise' (u,v)
 
911
  normalise' (TypeCmp cmp a b)     = uncurry (TypeCmp cmp) <$> normalise' (a,b)
 
912
  normalise' (TelCmp a b cmp tela telb) = uncurry (TelCmp a b cmp) <$> normalise' (tela,telb)
 
913
  normalise' (SortCmp cmp a b)     = uncurry (SortCmp cmp) <$> normalise' (a,b)
 
914
  normalise' (Guarded c pid)       = Guarded <$> normalise' c <*> pure pid
 
915
  normalise' (UnBlock m)           = return $ UnBlock m
 
916
  normalise' (FindInScope m cands) = FindInScope m <$> mapM normalise' cands
 
917
  normalise' (IsEmpty r t)         = IsEmpty r <$> normalise' t
 
918
 
 
919
instance Normalise Bool where
 
920
  normalise' = return
588
921
 
589
922
instance Normalise Pattern where
590
 
  normalise p = case p of
 
923
  normalise' p = case p of
591
924
    VarP _       -> return p
592
925
    LitP _       -> return p
593
 
    ConP c mt ps -> ConP c <$> normalise mt <*> normalise ps
594
 
    DotP v       -> DotP <$> normalise v
 
926
    ConP c mt ps -> ConP c <$> normalise' mt <*> normalise' ps
 
927
    DotP v       -> DotP <$> normalise' v
 
928
    ProjP _      -> return p
595
929
 
596
930
instance Normalise DisplayForm where
597
 
  normalise (Display n ps v) = Display n <$> normalise ps <*> return v
 
931
  normalise' (Display n ps v) = Display n <$> normalise' ps <*> return v
598
932
 
599
933
instance (Ord k, Normalise e) => Normalise (Map k e) where
600
 
    normalise = traverse normalise
 
934
    normalise' = traverse normalise'
601
935
 
602
936
instance Normalise a => Normalise (Maybe a) where
603
 
    normalise = traverse normalise
 
937
    normalise' = traverse normalise'
604
938
 
605
939
---------------------------------------------------------------------------
606
940
-- * Full instantiation
607
941
---------------------------------------------------------------------------
608
942
 
 
943
-- STALE: Full instantiatiation = normalisation [ instantiate' / reduce' ]
609
944
-- How can we express this? We need higher order classes!
610
945
 
 
946
-- | @instantiateFull'@ 'instantiate's metas everywhere (and recursively)
 
947
--   but does not 'reduce'.
611
948
class InstantiateFull t where
612
 
    instantiateFull :: t -> TCM t
 
949
    instantiateFull' :: t -> ReduceM t
613
950
 
614
951
instance InstantiateFull Name where
615
 
    instantiateFull = return
 
952
    instantiateFull' = return
616
953
 
617
954
instance InstantiateFull Sort where
618
 
    instantiateFull s = do
619
 
        s <- instantiate s
 
955
    instantiateFull' s = do
 
956
        s <- instantiate' s
620
957
        case s of
621
 
            Type n     -> levelSort <$> instantiateFull n
 
958
            Type n     -> levelSort <$> instantiateFull' n
622
959
            Prop       -> return s
623
 
            DLub s1 s2 -> dLub <$> instantiateFull s1 <*> instantiateFull s2
 
960
            DLub s1 s2 -> dLub <$> instantiateFull' s1 <*> instantiateFull' s2
624
961
            Inf        -> return s
625
962
 
626
963
instance InstantiateFull Type where
627
 
    instantiateFull (El s t) =
628
 
      El <$> instantiateFull s <*> instantiateFull t
 
964
    instantiateFull' (El s t) =
 
965
      El <$> instantiateFull' s <*> instantiateFull' t
629
966
 
630
967
instance InstantiateFull Term where
631
 
    instantiateFull v = etaOnce =<< do -- Andreas, 2010-11-12 DONT ETA!! eta-reduction breaks subject reduction
 
968
    instantiateFull' v = etaOnce =<< do -- Andreas, 2010-11-12 DONT ETA!! eta-reduction breaks subject reduction
632
969
-- but removing etaOnce now breaks everything
633
 
      v <- instantiate v
 
970
      v <- instantiate' v
634
971
      case v of
635
 
          Var n vs    -> Var n <$> instantiateFull vs
636
 
          Con c vs    -> Con c <$> instantiateFull vs
637
 
          Def f vs    -> Def f <$> instantiateFull vs
638
 
          MetaV x vs  -> MetaV x <$> instantiateFull vs
 
972
          Var n vs    -> Var n <$> instantiateFull' vs
 
973
          Con c vs    -> Con c <$> instantiateFull' vs
 
974
          Def f vs    -> Def f <$> instantiateFull' vs
 
975
          MetaV x vs  -> MetaV x <$> instantiateFull' vs
639
976
          Lit _       -> return v
640
 
          Level l     -> levelTm <$> instantiateFull l
641
 
          Lam h b     -> Lam h <$> instantiateFull b
642
 
          Sort s      -> sortTm <$> instantiateFull s
643
 
          Pi a b      -> uncurry Pi <$> instantiateFull (a,b)
644
 
          Shared{}    -> updateSharedTerm instantiateFull v
645
 
          DontCare v  -> DontCare <$> instantiateFull v
 
977
          Level l     -> levelTm <$> instantiateFull' l
 
978
          Lam h b     -> Lam h <$> instantiateFull' b
 
979
          Sort s      -> sortTm <$> instantiateFull' s
 
980
          Pi a b      -> uncurry Pi <$> instantiateFull' (a,b)
 
981
          Shared{}    -> __IMPOSSIBLE__ -- updateSharedTerm instantiateFull' v
 
982
          DontCare v  -> dontCare <$> instantiateFull' v
646
983
 
647
984
instance InstantiateFull Level where
648
 
  instantiateFull (Max as) = levelMax <$> instantiateFull as
 
985
  instantiateFull' (Max as) = levelMax <$> instantiateFull' as
649
986
 
650
987
instance InstantiateFull PlusLevel where
651
 
  instantiateFull l@ClosedLevel{} = return l
652
 
  instantiateFull (Plus n l) = Plus n <$> instantiateFull l
 
988
  instantiateFull' l@ClosedLevel{} = return l
 
989
  instantiateFull' (Plus n l) = Plus n <$> instantiateFull' l
653
990
 
654
991
instance InstantiateFull LevelAtom where
655
 
  instantiateFull l = case l of
 
992
  instantiateFull' l = case l of
656
993
    MetaLevel m vs -> do
657
 
      v <- instantiateFull (MetaV m vs)
 
994
      v <- instantiateFull' (MetaV m vs)
658
995
      case ignoreSharing v of
659
996
        MetaV m vs -> return $ MetaLevel m vs
660
997
        _          -> return $ UnreducedLevel v
661
 
    NeutralLevel v -> NeutralLevel <$> instantiateFull v
 
998
    NeutralLevel v -> NeutralLevel <$> instantiateFull' v
662
999
    BlockedLevel m v ->
663
1000
      ifM (isInstantiatedMeta m)
664
 
          (UnreducedLevel <$> instantiateFull v)
665
 
          (BlockedLevel m <$> instantiateFull v)
666
 
    UnreducedLevel v -> UnreducedLevel <$> instantiateFull v
 
1001
          (UnreducedLevel <$> instantiateFull' v)
 
1002
          (BlockedLevel m <$> instantiateFull' v)
 
1003
    UnreducedLevel v -> UnreducedLevel <$> instantiateFull' v
 
1004
 
 
1005
instance InstantiateFull Bool where
 
1006
    instantiateFull' = return
667
1007
 
668
1008
instance InstantiateFull Pattern where
669
 
    instantiateFull v@VarP{}       = return v
670
 
    instantiateFull (DotP t)       = DotP <$> instantiateFull t
671
 
    instantiateFull (ConP n mt ps) = ConP n <$> instantiateFull mt <*> instantiateFull ps
672
 
    instantiateFull l@LitP{}       = return l
 
1009
    instantiateFull' v@VarP{}       = return v
 
1010
    instantiateFull' (DotP t)       = DotP <$> instantiateFull' t
 
1011
    instantiateFull' (ConP n mt ps) = ConP n <$> instantiateFull' mt <*> instantiateFull' ps
 
1012
    instantiateFull' l@LitP{}       = return l
 
1013
    instantiateFull' p@ProjP{}      = return p
673
1014
 
674
1015
instance InstantiateFull ClauseBody where
675
 
    instantiateFull (Body   t) = Body   <$> instantiateFull t
676
 
    instantiateFull (Bind   b) = Bind   <$> instantiateFull b
677
 
    instantiateFull  NoBody    = return NoBody
 
1016
    instantiateFull' (Body   t) = Body   <$> instantiateFull' t
 
1017
    instantiateFull' (Bind   b) = Bind   <$> instantiateFull' b
 
1018
    instantiateFull'  NoBody    = return NoBody
678
1019
 
679
1020
instance (Subst t, InstantiateFull t) => InstantiateFull (Abs t) where
680
 
    instantiateFull a@(Abs x _) = Abs x <$> underAbstraction_ a instantiateFull
681
 
    instantiateFull (NoAbs x a) = NoAbs x <$> instantiateFull a
 
1021
    instantiateFull' a@(Abs x _) = Abs x <$> underAbstraction_ a instantiateFull'
 
1022
    instantiateFull' (NoAbs x a) = NoAbs x <$> instantiateFull' a
682
1023
 
683
1024
instance InstantiateFull t => InstantiateFull (Arg t) where
684
 
    instantiateFull = traverse instantiateFull
 
1025
    instantiateFull' = traverse instantiateFull'
 
1026
 
 
1027
instance InstantiateFull t => InstantiateFull (Named name t) where
 
1028
    instantiateFull' = traverse instantiateFull'
685
1029
 
686
1030
instance InstantiateFull t => InstantiateFull (Dom t) where
687
 
    instantiateFull = traverse instantiateFull
 
1031
    instantiateFull' = traverse instantiateFull'
688
1032
 
689
1033
instance InstantiateFull t => InstantiateFull [t] where
690
 
    instantiateFull = traverse instantiateFull
 
1034
    instantiateFull' = traverse instantiateFull'
691
1035
 
692
1036
instance (InstantiateFull a, InstantiateFull b) => InstantiateFull (a,b) where
693
 
    instantiateFull (x,y) = (,) <$> instantiateFull x <*> instantiateFull y
 
1037
    instantiateFull' (x,y) = (,) <$> instantiateFull' x <*> instantiateFull' y
694
1038
 
695
1039
instance (InstantiateFull a, InstantiateFull b, InstantiateFull c) => InstantiateFull (a,b,c) where
696
 
    instantiateFull (x,y,z) =
697
 
        do  (x,(y,z)) <- instantiateFull (x,(y,z))
 
1040
    instantiateFull' (x,y,z) =
 
1041
        do  (x,(y,z)) <- instantiateFull' (x,(y,z))
698
1042
            return (x,y,z)
699
1043
 
700
1044
instance InstantiateFull a => InstantiateFull (Closure a) where
701
 
    instantiateFull cl = do
702
 
        x <- enterClosure cl instantiateFull
 
1045
    instantiateFull' cl = do
 
1046
        x <- enterClosure cl instantiateFull'
703
1047
        return $ cl { clValue = x }
704
1048
 
705
1049
instance InstantiateFull ProblemConstraint where
706
 
  instantiateFull (PConstr p c) = PConstr p <$> instantiateFull c
 
1050
  instantiateFull' (PConstr p c) = PConstr p <$> instantiateFull' c
707
1051
 
708
1052
instance InstantiateFull Constraint where
709
 
  instantiateFull c = case c of
 
1053
  instantiateFull' c = case c of
710
1054
    ValueCmp cmp t u v -> do
711
 
      (t,u,v) <- instantiateFull (t,u,v)
 
1055
      (t,u,v) <- instantiateFull' (t,u,v)
712
1056
      return $ ValueCmp cmp t u v
713
1057
    ElimCmp cmp t v as bs ->
714
 
      ElimCmp cmp <$> instantiateFull t <*> instantiateFull v <*> instantiateFull as <*> instantiateFull bs
715
 
    LevelCmp cmp u v    -> uncurry (LevelCmp cmp) <$> instantiateFull (u,v)
716
 
    TypeCmp cmp a b     -> uncurry (TypeCmp cmp) <$> instantiateFull (a,b)
717
 
    TelCmp a b cmp tela telb -> uncurry (TelCmp a b cmp) <$> instantiateFull (tela,telb)
718
 
    SortCmp cmp a b     -> uncurry (SortCmp cmp) <$> instantiateFull (a,b)
719
 
    Guarded c pid       -> Guarded <$> instantiateFull c <*> pure pid
 
1058
      ElimCmp cmp <$> instantiateFull' t <*> instantiateFull' v <*> instantiateFull' as <*> instantiateFull' bs
 
1059
    LevelCmp cmp u v    -> uncurry (LevelCmp cmp) <$> instantiateFull' (u,v)
 
1060
    TypeCmp cmp a b     -> uncurry (TypeCmp cmp) <$> instantiateFull' (a,b)
 
1061
    TelCmp a b cmp tela telb -> uncurry (TelCmp a b cmp) <$> instantiateFull' (tela,telb)
 
1062
    SortCmp cmp a b     -> uncurry (SortCmp cmp) <$> instantiateFull' (a,b)
 
1063
    Guarded c pid       -> Guarded <$> instantiateFull' c <*> pure pid
720
1064
    UnBlock m           -> return $ UnBlock m
721
 
    FindInScope m cands -> FindInScope m <$> mapM instantiateFull cands
722
 
    IsEmpty r t         -> IsEmpty r <$> instantiateFull t
 
1065
    FindInScope m cands -> FindInScope m <$> mapM instantiateFull' cands
 
1066
    IsEmpty r t         -> IsEmpty r <$> instantiateFull' t
723
1067
 
724
1068
instance InstantiateFull Elim where
725
 
  instantiateFull (Apply v) = Apply <$> instantiateFull v
726
 
  instantiateFull (Proj f)  = pure $ Proj f
 
1069
  instantiateFull' (Apply v) = Apply <$> instantiateFull' v
 
1070
  instantiateFull' (Proj f)  = pure $ Proj f
727
1071
 
728
1072
instance (Ord k, InstantiateFull e) => InstantiateFull (Map k e) where
729
 
    instantiateFull = traverse instantiateFull
 
1073
    instantiateFull' = traverse instantiateFull'
730
1074
 
731
1075
instance (Eq k, Hashable k, InstantiateFull e) => InstantiateFull (HashMap k e) where
732
 
    instantiateFull = traverse instantiateFull
 
1076
    instantiateFull' = traverse instantiateFull'
733
1077
 
734
1078
instance InstantiateFull ModuleName where
735
 
    instantiateFull = return
 
1079
    instantiateFull' = return
736
1080
 
737
1081
instance InstantiateFull Scope where
738
 
    instantiateFull = return
 
1082
    instantiateFull' = return
739
1083
 
740
1084
instance InstantiateFull Signature where
741
 
  instantiateFull (Sig a b) = uncurry Sig <$> instantiateFull (a, b)
 
1085
  instantiateFull' (Sig a b) = uncurry Sig <$> instantiateFull' (a, b)
742
1086
 
743
1087
instance InstantiateFull Section where
744
 
  instantiateFull (Section tel n) = flip Section n <$> instantiateFull tel
 
1088
  instantiateFull' (Section tel n) = flip Section n <$> instantiateFull' tel
745
1089
 
746
1090
instance (Subst a, InstantiateFull a) => InstantiateFull (Tele a) where
747
 
  instantiateFull EmptyTel = return EmptyTel
748
 
  instantiateFull (ExtendTel a b) = uncurry ExtendTel <$> instantiateFull (a, b)
 
1091
  instantiateFull' EmptyTel = return EmptyTel
 
1092
  instantiateFull' (ExtendTel a b) = uncurry ExtendTel <$> instantiateFull' (a, b)
749
1093
 
750
1094
instance InstantiateFull Char where
751
 
    instantiateFull = return
 
1095
    instantiateFull' = return
752
1096
 
753
1097
instance InstantiateFull Definition where
754
 
    instantiateFull (Defn rel x t pol occ df i c d) = do
755
 
      (t, (df, d)) <- instantiateFull (t, (df, d))
 
1098
    instantiateFull' (Defn rel x t pol occ df i c d) = do
 
1099
      (t, (df, d)) <- instantiateFull' (t, (df, d))
756
1100
      return $ Defn rel x t pol occ df i c d
757
1101
 
758
1102
instance InstantiateFull a => InstantiateFull (Open a) where
759
 
  instantiateFull (OpenThing n a) = OpenThing n <$> instantiateFull a
 
1103
  instantiateFull' (OpenThing n a) = OpenThing n <$> instantiateFull' a
760
1104
 
761
1105
instance InstantiateFull DisplayForm where
762
 
  instantiateFull (Display n ps v) = uncurry (Display n) <$> instantiateFull (ps, v)
 
1106
  instantiateFull' (Display n ps v) = uncurry (Display n) <$> instantiateFull' (ps, v)
763
1107
 
764
1108
instance InstantiateFull DisplayTerm where
765
 
  instantiateFull (DTerm v)        = DTerm <$> instantiateFull v
766
 
  instantiateFull (DDot  v)        = DDot  <$> instantiateFull v
767
 
  instantiateFull (DCon c vs)      = DCon c <$> instantiateFull vs
768
 
  instantiateFull (DDef c vs)      = DDef c <$> instantiateFull vs
769
 
  instantiateFull (DWithApp vs ws) = uncurry DWithApp <$> instantiateFull (vs, ws)
 
1109
  instantiateFull' (DTerm v)       = DTerm <$> instantiateFull' v
 
1110
  instantiateFull' (DDot  v)       = DDot  <$> instantiateFull' v
 
1111
  instantiateFull' (DCon c vs)     = DCon c <$> instantiateFull' vs
 
1112
  instantiateFull' (DDef c vs)     = DDef c <$> instantiateFull' vs
 
1113
  instantiateFull' (DWithApp v vs ws) = uncurry3 DWithApp <$> instantiateFull' (v, vs, ws)
770
1114
 
771
1115
instance InstantiateFull Defn where
772
 
    instantiateFull d = case d of
 
1116
    instantiateFull' d = case d of
773
1117
      Axiom{} -> return d
774
1118
      Function{ funClauses = cs, funCompiled = cc, funInv = inv } -> do
775
 
        (cs, cc, inv) <- instantiateFull (cs, cc, inv)
 
1119
        (cs, cc, inv) <- instantiateFull' (cs, cc, inv)
776
1120
        return $ d { funClauses = cs, funCompiled = cc, funInv = inv }
777
1121
      Datatype{ dataSort = s, dataClause = cl } -> do
778
 
        s  <- instantiateFull s
779
 
        cl <- instantiateFull cl
 
1122
        s  <- instantiateFull' s
 
1123
        cl <- instantiateFull' cl
780
1124
        return $ d { dataSort = s, dataClause = cl }
781
1125
      Record{ recConType = t, recClause = cl, recTel = tel } -> do
782
 
        t   <- instantiateFull t
783
 
        cl  <- instantiateFull cl
784
 
        tel <- instantiateFull tel
 
1126
        t   <- instantiateFull' t
 
1127
        cl  <- instantiateFull' cl
 
1128
        tel <- instantiateFull' tel
785
1129
        return $ d { recConType = t, recClause = cl, recTel = tel }
786
1130
      Constructor{} -> return d
787
1131
      Primitive{ primClauses = cs } -> do
788
 
        cs <- instantiateFull cs
 
1132
        cs <- instantiateFull' cs
789
1133
        return $ d { primClauses = cs }
790
1134
 
791
1135
instance InstantiateFull FunctionInverse where
792
 
  instantiateFull NotInjective = return NotInjective
793
 
  instantiateFull (Inverse inv) = Inverse <$> instantiateFull inv
 
1136
  instantiateFull' NotInjective = return NotInjective
 
1137
  instantiateFull' (Inverse inv) = Inverse <$> instantiateFull' inv
794
1138
 
795
1139
instance InstantiateFull a => InstantiateFull (WithArity a) where
796
 
  instantiateFull (WithArity n a) = WithArity n <$> instantiateFull a
 
1140
  instantiateFull' (WithArity n a) = WithArity n <$> instantiateFull' a
797
1141
 
798
1142
instance InstantiateFull a => InstantiateFull (Case a) where
799
 
  instantiateFull (Branches cs ls m) =
800
 
    Branches <$> instantiateFull cs
801
 
             <*> instantiateFull ls
802
 
             <*> instantiateFull m
 
1143
  instantiateFull' (Branches cs ls m) =
 
1144
    Branches <$> instantiateFull' cs
 
1145
             <*> instantiateFull' ls
 
1146
             <*> instantiateFull' m
803
1147
 
804
1148
instance InstantiateFull CompiledClauses where
805
 
  instantiateFull Fail        = return Fail
806
 
  instantiateFull (Done m t)  = Done m <$> instantiateFull t
807
 
  instantiateFull (Case n bs) = Case n <$> instantiateFull bs
 
1149
  instantiateFull' Fail        = return Fail
 
1150
  instantiateFull' (Done m t)  = Done m <$> instantiateFull' t
 
1151
  instantiateFull' (Case n bs) = Case n <$> instantiateFull' bs
808
1152
 
809
1153
instance InstantiateFull Clause where
810
 
    instantiateFull (Clause r tel perm ps b) =
811
 
       Clause r <$> instantiateFull tel
 
1154
    instantiateFull' (Clause r tel perm ps b t) =
 
1155
       Clause r <$> instantiateFull' tel
812
1156
       <*> return perm
813
 
       <*> instantiateFull ps
814
 
       <*> instantiateFull b
 
1157
       <*> instantiateFull' ps
 
1158
       <*> instantiateFull' b
 
1159
       <*> instantiateFull' t
815
1160
 
816
1161
instance InstantiateFull Interface where
817
 
    instantiateFull (Interface ms mod scope inside
 
1162
    instantiateFull' (Interface h ms mod scope inside
818
1163
                               sig b hsImports highlighting pragmas patsyns) =
819
 
        Interface ms mod scope inside
820
 
            <$> instantiateFull sig
821
 
            <*> instantiateFull b
 
1164
        Interface h ms mod scope inside
 
1165
            <$> instantiateFull' sig
 
1166
            <*> instantiateFull' b
822
1167
            <*> return hsImports
823
1168
            <*> return highlighting
824
1169
            <*> return pragmas
825
1170
            <*> return patsyns
826
1171
 
827
1172
instance InstantiateFull a => InstantiateFull (Builtin a) where
828
 
    instantiateFull (Builtin t) = Builtin <$> instantiateFull t
829
 
    instantiateFull (Prim x)    = Prim <$> instantiateFull x
 
1173
    instantiateFull' (Builtin t) = Builtin <$> instantiateFull' t
 
1174
    instantiateFull' (Prim x)   = Prim <$> instantiateFull' x
830
1175
 
831
1176
instance InstantiateFull QName where
832
 
  instantiateFull = return
 
1177
  instantiateFull' = return
833
1178
 
834
1179
instance InstantiateFull a => InstantiateFull (Maybe a) where
835
 
  instantiateFull = mapM instantiateFull
836
 
 
837
 
 
838
 
{- DUPLICATE of Telescope.telView
839
 
 
840
 
telViewM :: Type -> TCM TelView
841
 
telViewM t = do
842
 
  t <- reduce t -- also instantiates meta if in head position
843
 
  case ignoreSharing $ unEl t of
844
 
    Pi a b -> absV a (absName b) <$> telViewM (absBody b)
845
 
    _      -> return $ TelV EmptyTel t
846
 
  where
847
 
    absV a x (TelV tel t) = TelV (ExtendTel a (Abs x tel)) t
848
 
-}
 
1180
  instantiateFull' = mapM instantiateFull'
 
1181