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

« back to all changes in this revision

Viewing changes to src/full/Agda/TypeChecking/Test/Generators.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 FlexibleInstances #-}
 
1
{-# LANGUAGE CPP                    #-}
 
2
{-# LANGUAGE FlexibleInstances      #-}
3
3
{-# LANGUAGE FunctionalDependencies #-}
4
 
{-# LANGUAGE MultiParamTypeClasses #-}
5
 
{-# LANGUAGE TypeSynonymInstances #-}
6
 
{-# LANGUAGE UndecidableInstances #-}
 
4
{-# LANGUAGE MultiParamTypeClasses  #-}
 
5
{-# LANGUAGE TypeSynonymInstances   #-}
 
6
{-# LANGUAGE UndecidableInstances   #-}
7
7
 
8
8
module Agda.TypeChecking.Test.Generators where
9
9
 
25
25
import Agda.Utils.TestHelpers
26
26
import qualified Agda.Utils.VarSet as Set
27
27
 
28
 
#include "../../undefined.h"
 
28
#include "undefined.h"
29
29
import Agda.Utils.Impossible
30
30
 
31
31
data TermConfiguration = TermConf
32
 
      { tcDefinedNames     :: [QName]
 
32
      { tcDefinedNames     :: [QName]
33
33
      , tcConstructorNames :: [QName]
34
34
      , tcProjectionNames  :: [QName]
35
 
      , tcFreeVariables    :: [Nat]
36
 
      , tcLiterals         :: UseLiterals
37
 
      , tcFrequencies      :: Frequencies
38
 
      , tcFixSize          :: Maybe Int
39
 
        -- ^ Maximum size of the generated element. When @Nothing@ this value
40
 
        --   is initialized from the 'Test.QuickCheck.size' parameter.
41
 
      , tcIsType           :: Bool
42
 
        -- ^ When this is true no lambdas, literals, or constructors are
43
 
        --   generated
 
35
      , tcFreeVariables    :: [Nat]
 
36
      , tcLiterals         :: UseLiterals
 
37
      , tcFrequencies      :: Frequencies
 
38
      , tcFixSize          :: Maybe Int
 
39
        -- ^ Maximum size of the generated element. When @Nothing@ this value
 
40
        --   is initialized from the 'Test.QuickCheck.size' parameter.
 
41
      , tcIsType           :: Bool
 
42
        -- ^ When this is true no lambdas, literals, or constructors are
 
43
        --   generated
44
44
      }
45
45
  deriving Show
46
46
 
56
56
      { varFreq :: Int
57
57
      , defFreq :: Int
58
58
      , conFreq :: Int
59
 
      , litFreq  :: Int
 
59
      , litFreq  :: Int
60
60
      , sortFreq :: Int
61
 
      , lamFreq  :: Int
62
 
      , piFreq   :: Int
63
 
      , funFreq  :: Int
 
61
      , lamFreq  :: Int
 
62
      , piFreq   :: Int
 
63
      , funFreq  :: Int
64
64
      }
65
65
  deriving Show
66
66
 
84
84
 
85
85
defaultFrequencies :: Frequencies
86
86
defaultFrequencies = Freqs
87
 
      { termFreqs   = TermFreqs   { varFreq = 24, defFreq = 8, conFreq = 8, litFreq = 1, sortFreq = 2, lamFreq = 10, piFreq = 5, funFreq = 5 }
 
87
      { termFreqs   = TermFreqs   { varFreq = 24, defFreq = 8, conFreq = 8, litFreq = 1, sortFreq = 2, lamFreq = 10, piFreq = 5, funFreq = 5 }
88
88
      , elimFreqs   = ElimFreqs   { applyFreq = 9, projFreq = 1 }
89
89
      , hiddenFreqs = HiddenFreqs { hiddenFreq = 1, notHiddenFreq = 5 }
90
 
      , sortFreqs   = SortFreqs   { setFreqs = [3, 1], propFreq = 1 }
 
90
      , sortFreqs   = SortFreqs   { setFreqs = [3, 1], propFreq = 1 }
91
91
      }
92
92
 
93
93
noProp :: TermConfiguration -> TermConfiguration
94
94
noProp conf = conf { tcFrequencies = fq { sortFreqs = sfq { propFreq = 0 } } }
95
95
  where
96
 
    fq  = tcFrequencies conf
97
 
    sfq = sortFreqs fq
 
96
    fq  = tcFrequencies conf
 
97
    sfq = sortFreqs fq
98
98
 
99
99
data UseLiterals = UseLit
100
100
      { useLitInt    :: Bool
138
138
  , tcProjectionNames  = projs
139
139
  , tcFreeVariables    = List.sort $ List.nub vs
140
140
  , tcFrequencies      = defaultFrequencies
141
 
  , tcLiterals         = noLiterals
142
 
  , tcFixSize          = Nothing
143
 
  , tcIsType           = False
 
141
  , tcLiterals         = noLiterals
 
142
  , tcFixSize          = Nothing
 
143
  , tcIsType           = False
144
144
  }
145
145
  where
146
146
    (defs, cons, projs) = flip evalState 0 $ do
150
150
    mkName s = do
151
151
      n <- tick
152
152
      return $ QName { qnameModule = MName []
153
 
                     , qnameName   = Name
154
 
                        { nameId          = NameId n 1
155
 
                        , nameConcrete    = C.Name noRange [C.Id s]
156
 
                        , nameBindingSite = noRange
157
 
                        , nameFixity      = defaultFixity'
158
 
                        }
159
 
                      }
 
153
                     , qnameName   = Name
 
154
                        { nameId          = NameId n 1
 
155
                        , nameConcrete    = C.Name noRange [C.Id s]
 
156
                        , nameBindingSite = noRange
 
157
                        , nameFixity      = defaultFixity'
 
158
                        }
 
159
                      }
160
160
 
161
161
class GenC a where
162
162
  genC :: TermConfiguration -> Gen a
163
163
 
164
 
newtype YesType a   = YesType   { unYesType :: a     }
165
 
newtype NoType  a   = NoType    { unNoType  :: a     }
166
 
newtype VarName     = VarName   { unVarName :: Nat   }
167
 
newtype DefName     = DefName   { unDefName :: QName }
168
 
newtype ConName     = ConName   { unConName :: ConHead }
 
164
newtype YesType a   = YesType   { unYesType :: a     }
 
165
newtype NoType  a   = NoType    { unNoType  :: a     }
 
166
newtype VarName     = VarName   { unVarName :: Nat   }
 
167
newtype DefName     = DefName   { unDefName :: QName }
 
168
newtype ConName     = ConName   { unConName :: ConHead }
169
169
newtype ProjName    = ProjName  { unProjName :: QName }
170
170
newtype SizedList a = SizedList { unSizedList :: [a] }
171
171
 
194
194
  genC conf = frequency [ (hideF, return Hidden), (nohideF, return NotHidden) ]
195
195
    where
196
196
      HiddenFreqs {hiddenFreq = hideF, notHiddenFreq = nohideF } =
197
 
        hiddenFreqs $ tcFrequencies conf
 
197
        hiddenFreqs $ tcFrequencies conf
198
198
 
199
199
instance (GenC c, GenC a) => GenC (Common.Arg c a) where
200
200
  genC conf = (\ (h, a) -> Arg (setHiding h defaultArgInfo) a) <$> genC conf
211
211
                        , (projF, Proj . unProjName <$> genC conf) ]
212
212
    where
213
213
      ElimFreqs {applyFreq = applyF, projFreq = projF } =
214
 
        elimFreqs $ tcFrequencies conf
 
214
        elimFreqs $ tcFrequencies conf
215
215
 
216
216
instance GenC DefName where
217
217
  genC conf = DefName  <$> do elements $ tcDefinedNames conf
245
245
 
246
246
instance GenC Literal where
247
247
  genC conf = oneof (concat $ zipWith gen useLits
248
 
              [ uncurry LitInt    <$> genC conf
249
 
              , uncurry LitFloat  <$> genC conf
250
 
              , uncurry LitString <$> genC conf
251
 
              , uncurry LitChar   <$> genC conf
252
 
              ]
253
 
           )
 
248
              [ uncurry LitInt    <$> genC conf
 
249
              , uncurry LitFloat  <$> genC conf
 
250
              , uncurry LitString <$> genC conf
 
251
              , uncurry LitChar   <$> genC conf
 
252
              ]
 
253
           )
254
254
    where
255
255
      useLits = map ($ tcLiterals conf) [ useLitInt, useLitFloat, useLitString, useLitChar ]
256
256
 
270
270
  genC conf = case tcFixSize conf of
271
271
      Nothing -> sized $ \n -> genC $ fixSizeConf n conf
272
272
      Just n | n <= 0    -> genLeaf
273
 
             | otherwise -> frequency
274
 
        [ (varF, genVar $ genElims conf)
275
 
        , (defF, genDef $ genElims conf)
276
 
        , (conF, genCon $ genArgs conf)
277
 
        , (litF,  Lit <$> genC conf)
278
 
        , (sortF, Sort <$> genC conf)
279
 
        , (lamF,  genLam)
280
 
        , (piF,   genPi)
281
 
        ]
 
273
             | otherwise -> frequency
 
274
        [ (varF, genVar $ genElims conf)
 
275
        , (defF, genDef $ genElims conf)
 
276
        , (conF, genCon $ genArgs conf)
 
277
        , (litF,  Lit <$> genC conf)
 
278
        , (sortF, Sort <$> genC conf)
 
279
        , (lamF,  genLam)
 
280
        , (piF,   genPi)
 
281
        ]
282
282
    where
283
283
      defs    = tcDefinedNames conf
284
284
      cons    = tcConstructorNames conf
288
288
      useLits = map ($ tcLiterals conf) [ useLitInt, useLitFloat, useLitString, useLitChar ]
289
289
 
290
290
      varF  | null vars = 0
291
 
            | otherwise = freq (varFreq . termFreqs)
 
291
            | otherwise = freq (varFreq . termFreqs)
292
292
      defF  | null defs = 0
293
 
            | otherwise = freq (defFreq . termFreqs)
 
293
            | otherwise = freq (defFreq . termFreqs)
294
294
      conF  | null cons || isType = 0
295
 
            | otherwise           = freq (conFreq . termFreqs)
 
295
            | otherwise           = freq (conFreq . termFreqs)
296
296
      litF  | or useLits && not isType = freq (litFreq . termFreqs)
297
 
            | otherwise             = 0
 
297
            | otherwise             = 0
298
298
      lamF  | isType    = 0
299
 
            | otherwise = freq (lamFreq  . termFreqs)
 
299
            | otherwise = freq (lamFreq  . termFreqs)
300
300
      sortF = freq (sortFreq . termFreqs)
301
301
      piF   = freq (piFreq   . termFreqs)
302
302
 
311
311
      genDef args = Def <$> elements defs <*> args
312
312
 
313
313
      genCon :: Gen Args -> Gen Term
314
 
      genCon args = Con <$> (flip ConHead [] <$> elements cons) <*> args
 
314
      genCon args = Con <$> ((\ c -> ConHead c Inductive []) <$> elements cons) <*> args
315
315
 
316
316
      genLeaf :: Gen Term
317
317
      genLeaf = frequency
318
 
        [ (varF, genVar $ return [])
319
 
        , (defF, genDef $ return [])
320
 
        , (conF, genCon $ return [])
321
 
        , (litF,  Lit  <$> genC conf)
322
 
        , (sortF, Sort <$> genC conf)
323
 
        ]
 
318
        [ (varF, genVar $ return [])
 
319
        , (defF, genDef $ return [])
 
320
        , (conF, genCon $ return [])
 
321
        , (litF,  Lit  <$> genC conf)
 
322
        , (sortF, Sort <$> genC conf)
 
323
        ]
324
324
 
325
325
-- | Only generates default configurations. Names and free variables varies.
326
326
genConf :: Gen TermConfiguration
353
353
  noShrink (NoType x) = noShrink x
354
354
 
355
355
instance ShrinkC a b => ShrinkC [a] [b] where
356
 
  noShrink        = map noShrink
 
356
  noShrink        = map noShrink
357
357
  shrinkC conf xs = noShrink (removeChunks xs) ++ shrinkOne xs
358
358
   where
359
359
    -- Code stolen from Test.QuickCheck.Arbitrary
395
395
  noShrink = unDefName
396
396
 
397
397
instance ShrinkC ConName ConHead where
398
 
  shrinkC conf (ConName (ConHead{conName = c})) = map (flip ConHead []) $ takeWhile (/= c) $ tcConstructorNames conf
 
398
  shrinkC conf (ConName (ConHead{conName = c})) = map (\ c -> ConHead c Inductive []) $ takeWhile (/= c) $ tcConstructorNames conf
399
399
  noShrink = unConName
400
400
 
401
401
instance ShrinkC Literal Literal where
402
402
  shrinkC _ (LitInt _ 0) = []
403
 
  shrinkC conf l         = LitInt noRange 0 : case l of
 
403
  shrinkC conf l         = LitInt noRange 0 : case l of
404
404
      LitInt    r n -> LitInt    r <$> shrink n
405
405
      LitString r s -> LitString r <$> shrinkC conf s
406
406
      LitChar   r c -> LitChar   r <$> shrinkC conf c
410
410
 
411
411
instance ShrinkC Char Char where
412
412
  shrinkC _ 'a' = []
413
 
  shrinkC _ _   = ['a']
 
413
  shrinkC _ _   = ['a']
414
414
  noShrink = id
415
415
 
416
416
instance ShrinkC Hiding Hiding where
453
453
  noShrink = id
454
454
 
455
455
instance ShrinkC Telescope Telescope where
456
 
  shrinkC conf EmptyTel          = []
 
456
  shrinkC conf EmptyTel          = []
457
457
  shrinkC conf (ExtendTel a tel) =
458
458
    killAbs tel : (uncurry ExtendTel <$> shrinkC conf (a, tel))
459
459
  noShrink = id
465
465
instance ShrinkC Term Term where
466
466
  shrinkC conf (DontCare _)  = []
467
467
  shrinkC conf (Sort Prop) = []
468
 
  shrinkC conf t           = filter validType $ case ignoreSharing t of
 
468
  shrinkC conf t           = filter validType $ case ignoreSharing t of
469
469
    Var i es     -> map unArg (argsFromElims es) ++
470
 
                    (uncurry Var <$> shrinkC conf (VarName i, NoType es))
 
470
                    (uncurry Var <$> shrinkC conf (VarName i, NoType es))
471
471
    Def d es     -> map unArg (argsFromElims es) ++
472
 
                    (uncurry Def <$> shrinkC conf (DefName d, NoType es))
 
472
                    (uncurry Def <$> shrinkC conf (DefName d, NoType es))
473
473
    Con c args   -> map unArg args ++
474
 
                    (uncurry Con <$> shrinkC conf (ConName c, NoType args))
475
 
    Lit l        -> Lit <$> shrinkC conf l
 
474
                    (uncurry Con <$> shrinkC conf (ConName c, NoType args))
 
475
    Lit l        -> Lit <$> shrinkC conf l
476
476
    Level l      -> [] -- TODO
477
477
    Lam info b   -> killAbs b : ((\(h,x) -> Lam (setHiding h defaultArgInfo) x)
478
478
                                 <$> shrinkC conf (argInfoHiding info, b))
479
479
    Pi a b       -> unEl (unDom a) : unEl (killAbs b) :
480
 
                    (uncurry Pi <$> shrinkC conf (a, b))
 
480
                    (uncurry Pi <$> shrinkC conf (a, b))
481
481
    Sort s       -> Sort <$> shrinkC conf s
482
482
    MetaV m es   -> map unArg (argsFromElims es) ++
483
483
                    (MetaV m <$> shrinkC conf (NoType es))
484
484
    DontCare _   -> []
485
485
    Shared{}     -> __IMPOSSIBLE__
 
486
    ExtLam _ _   -> __IMPOSSIBLE__
486
487
    where
487
488
      validType t
488
 
        | not (tcIsType conf) = True
489
 
        | otherwise         = case t of
490
 
            Con _ _ -> False
491
 
            Lam _ _ -> False
492
 
            Lit _         -> False
493
 
            _     -> True
 
489
        | not (tcIsType conf) = True
 
490
        | otherwise         = case t of
 
491
            Con _ _ -> False
 
492
            Lam _ _ -> False
 
493
            Lit _         -> False
 
494
            _     -> True
494
495
  noShrink = id
495
496
 
496
497
killAbs :: KillVar a => Abs a -> a
502
503
 
503
504
instance KillVar Term where
504
505
  killVar i t = case ignoreSharing t of
505
 
    Var j args | j == i    -> DontCare (Var j [])
506
 
               | j >  i    -> Var (j - 1) $ killVar i args
507
 
               | otherwise -> Var j       $ killVar i args
508
 
    Def c args             -> Def c       $ killVar i args
509
 
    Con c args             -> Con c       $ killVar i args
510
 
    Lit l                  -> Lit l
 
506
    Var j args | j == i    -> DontCare (Var j [])
 
507
               | j >  i    -> Var (j - 1) $ killVar i args
 
508
               | otherwise -> Var j       $ killVar i args
 
509
    Def c args             -> Def c       $ killVar i args
 
510
    Con c args             -> Con c       $ killVar i args
 
511
    Lit l                  -> Lit l
511
512
    Level l                -> Level l -- TODO
512
 
    Sort s                 -> Sort s
513
 
    Lam h b                -> Lam h       $ killVar i b
514
 
    Pi a b                 -> uncurry Pi  $ killVar i (a, b)
515
 
    MetaV m args           -> MetaV m     $ killVar i args
 
513
    Sort s                 -> Sort s
 
514
    Lam h b                -> Lam h       $ killVar i b
 
515
    Pi a b                 -> uncurry Pi  $ killVar i (a, b)
 
516
    MetaV m args           -> MetaV m     $ killVar i args
516
517
    DontCare mv            -> DontCare    $ killVar i mv
517
518
    Shared{}               -> __IMPOSSIBLE__
 
519
    ExtLam _ _             -> __IMPOSSIBLE__
518
520
 
519
521
instance KillVar Type where
520
522
  killVar i (El s t) = El s $ killVar i t
521
523
 
522
524
instance KillVar Telescope where
523
 
  killVar i EmptyTel          = EmptyTel
 
525
  killVar i EmptyTel          = EmptyTel
524
526
  killVar i (ExtendTel a tel) = uncurry ExtendTel $ killVar i (a, tel)
525
527
 
526
528
instance KillVar a => KillVar (Elim' a) where