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

« back to all changes in this revision

Viewing changes to src/full/Agda/TypeChecking/Patterns/Match.hs

  • Committer: Package Import Robot
  • Author(s): Iain Lane, d5cf60f
  • Date: 2015-05-20 13:08:33 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20150520130833-cdcmhagwsouna237
Tags: 2.4.2.2-2
[d5cf60f] Depend on ${shlibs:Depends}, to get libc (& maybe other) deps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
{-# LANGUAGE CPP #-}
2
 
{-# LANGUAGE DeriveFunctor #-}
 
1
{-# LANGUAGE CPP                 #-}
 
2
{-# LANGUAGE DeriveFunctor       #-}
3
3
{-# LANGUAGE ScopedTypeVariables #-}
4
4
 
5
5
module Agda.TypeChecking.Patterns.Match where
21
21
import Agda.Utils.Size
22
22
import Agda.Utils.Tuple
23
23
 
24
 
#include "../../undefined.h"
 
24
#include "undefined.h"
25
25
import Agda.Utils.Impossible
26
26
 
27
27
-- | If matching is inconclusive (@DontKnow@) we want to know whether
28
28
--   it is due to a particular meta variable.
29
 
data Match a = Yes Simplification [a] | No | DontKnow (Maybe MetaId)
30
 
  deriving (Functor)
 
29
data Match a = Yes Simplification [a]
 
30
             | No
 
31
             | DontKnow (Maybe MetaId)
 
32
  deriving Functor
31
33
 
32
34
instance Monoid (Match a) where
33
35
    mempty = Yes mempty []
35
37
    Yes s us   `mappend` Yes s' vs        = Yes (s `mappend` s') (us ++ vs)
36
38
    Yes _ _    `mappend` No               = No
37
39
    Yes _ _    `mappend` DontKnow m       = DontKnow m
38
 
    No         `mappend` _                = No
 
40
    No         `mappend` _                = No
39
41
 
40
42
    -- Nothing means blocked by a variable.  In this case no instantiation of
41
43
    -- meta-variables will make progress.
43
45
 
44
46
    -- One could imagine DontKnow _ `mappend` No = No, but would break the
45
47
    -- equivalence to case-trees.
46
 
    DontKnow m `mappend` _                = DontKnow m
 
48
    DontKnow m `mappend` _                = DontKnow m
47
49
 
48
50
-- | Instead of 'zipWithM', we need to use this lazy version
49
51
--   of combining pattern matching computations.
141
143
    w <- reduceB' v
142
144
    let arg' = arg $> ignoreBlocking w
143
145
    case ignoreSharing <$> w of
144
 
        NotBlocked (Lit l')
145
 
            | l == l'          -> return (Yes YesSimplification [] , arg')
146
 
            | otherwise        -> return (No                       , arg')
147
 
        NotBlocked (MetaV x _) -> return (DontKnow $ Just x        , arg')
148
 
        Blocked x _            -> return (DontKnow $ Just x        , arg')
149
 
        _                      -> return (DontKnow Nothing         , arg')
 
146
        NotBlocked (Lit l')
 
147
            | l == l'          -> return (Yes YesSimplification [] , arg')
 
148
            | otherwise        -> return (No                       , arg')
 
149
        NotBlocked (MetaV x _) -> return (DontKnow $ Just x        , arg')
 
150
        Blocked x _            -> return (DontKnow $ Just x        , arg')
 
151
        _                      -> return (DontKnow Nothing         , arg')
150
152
 
151
153
{- Andreas, 2012-04-02 NO LONGER UP-TO-DATE
152
154
matchPattern (Arg h' r' (ConP c _ ps))     (Arg h Irrelevant v) = do
153
155
          -- Andreas, 2010-09-07 matching a record constructor against
154
156
          -- something irrelevant will just continue matching against
155
157
          -- irrelevant stuff
156
 
                (m, vs) <- matchPatterns ps $
 
158
                (m, vs) <- matchPatterns ps $
157
159
                  repeat $ Arg NotHidden Irrelevant $ DontCare __IMPOSSIBLE__
158
 
                return (m, Arg h Irrelevant $ Con c vs)
 
160
                return (m, Arg h Irrelevant $ Con c vs)
159
161
-}
160
162
 
161
163
  -- Case record pattern: always succeed!
162
164
  -- This case is necessary if we want to use the clauses before
163
165
  -- record pattern translation (e.g., in type-checking definitions by copatterns).
164
 
  (ConP con@(ConHead c ds) Just{} ps, arg@(Arg info v))
 
166
  (ConP con@(ConHead c _ ds) Just{} ps, arg@(Arg info v))
165
167
     -- precondition: con actually comes with the record fields
166
168
     | size ds == size ps -> mapSnd (Arg info . Con con) <$> do
167
169
         matchPatterns ps $ for ds $ \ d -> Arg info $ v `applyE` [Proj d]
170
172
 
171
173
  -- Case data constructor pattern.
172
174
  (ConP c _ ps, Arg info v) ->
173
 
    do  w <- traverse constructorForm =<< reduceB' v
 
175
    do  w <- traverse constructorForm =<< reduceB' v
174
176
        -- Unfold delayed (corecursive) definitions one step. This is
175
177
        -- only necessary if c is a coinductive constructor, but
176
178
        -- 1) it does not hurt to do it all the time, and
185
187
                   -- unfolded (due to open public).
186
188
               _ -> return w
187
189
        let v = ignoreBlocking w
188
 
        case ignoreSharing <$> w of
 
190
        case ignoreSharing <$> w of
189
191
 
190
192
{- Andreas, 2013-10-27 the following considered HARMFUL:
191
193
          -- Andreas, 2010-09-07 matching a record constructor against
193
195
          -- irrelevant stuff
194
196
          -- NotBlocked (Sort Prop)
195
197
          _  | isIrrelevant info -> do
196
 
                (m, vs) <- matchPatterns ps $
 
198
                (m, vs) <- matchPatterns ps $
197
199
                  repeat $ setRelevance Irrelevant $ defaultArg $ Sort Prop
198
200
                    -- repeat looks very bad here (non-termination!)
199
 
                return (m, Arg info $ Con c vs)
 
201
                return (m, Arg info $ Con c vs)
200
202
-}
201
 
          NotBlocked (Con c' vs)
202
 
            | c == c'            -> do
203
 
                (m, vs) <- yesSimplification <$> matchPatterns ps vs
204
 
                return (m, Arg info $ Con c' vs)
205
 
            | otherwise           -> return (No, Arg info v) -- NOTE: v the reduced thing(shadowing!). Andreas, 2013-07-03
206
 
          NotBlocked (MetaV x vs) -> return (DontKnow $ Just x, Arg info v)
207
 
          Blocked x _             -> return (DontKnow $ Just x, Arg info v)
 
203
          NotBlocked (Con c' vs)
 
204
            | c == c'            -> do
 
205
                (m, vs) <- yesSimplification <$> matchPatterns ps vs
 
206
                return (m, Arg info $ Con c' vs)
 
207
            | otherwise           -> return (No, Arg info v) -- NOTE: v the reduced thing(shadowing!). Andreas, 2013-07-03
 
208
          NotBlocked (MetaV x vs) -> return (DontKnow $ Just x, Arg info v)
 
209
          Blocked x _             -> return (DontKnow $ Just x, Arg info v)
208
210
          _                       -> return (DontKnow Nothing, Arg info v)
209
211
 
 
212
-- ASR (08 November 2014). The type of the function could be
 
213
--
 
214
-- @(Match Term, [I.Arg Term]) -> (Match Term, [I.Arg Term])@.
 
215
yesSimplification :: (Match a, b) -> (Match a, b)
210
216
yesSimplification (Yes _ vs, us) = (Yes YesSimplification vs, us)
211
217
yesSimplification r              = r