26
30
import Control.Applicative
27
31
import Control.Monad.Reader hiding (mapM)
28
32
import Control.Monad.Error hiding (mapM)
34
import Data.Foldable (Foldable, traverse_)
30
35
import Data.Traversable (mapM, traverse)
31
36
import Data.List ((\\), nub, foldl')
32
37
import qualified Data.Map as Map
34
40
import Agda.Syntax.Concrete as C hiding (topLevelModuleName)
41
import Agda.Syntax.Concrete.Generic
35
42
import Agda.Syntax.Concrete.Operators
36
43
import Agda.Syntax.Abstract as A
37
import Agda.Syntax.Abstract.Copatterns
38
44
import Agda.Syntax.Position
39
import Agda.Syntax.Common
45
import Agda.Syntax.Common hiding (Arg, Dom, NamedArg, ArgInfo)
46
import qualified Agda.Syntax.Common as Common
40
47
import Agda.Syntax.Info
41
48
import Agda.Syntax.Concrete.Definitions as C
42
import Agda.Syntax.Concrete.Pretty
43
import Agda.Syntax.Abstract.Pretty
44
49
import Agda.Syntax.Fixity
45
50
import Agda.Syntax.Notation
46
51
import Agda.Syntax.Scope.Base
50
54
import Agda.TypeChecking.Monad.Base (TypeError(..), Call(..), typeError,
51
55
TCErr(..), extendlambdaname)
52
import Agda.TypeChecking.Monad.Trace (traceCall, traceCallCPS, setCurrentRange)
56
import Agda.TypeChecking.Monad.Benchmark (billTo, billTop, reimburseTop)
57
import qualified Agda.TypeChecking.Monad.Benchmark as Bench
58
import Agda.TypeChecking.Monad.Trace (traceCall, setCurrentRange)
53
59
import Agda.TypeChecking.Monad.State
60
import Agda.TypeChecking.Monad.MetaVars (registerInteractionPoint)
54
61
import Agda.TypeChecking.Monad.Options
62
import Agda.TypeChecking.Monad.Env (insideDotPattern, isInsideDotPattern)
64
import Agda.Interaction.FindFile (checkModuleName)
65
-- import Agda.Interaction.Imports -- for type-checking in ghci
56
66
import {-# SOURCE #-} Agda.Interaction.Imports (scopeCheckImport)
57
67
import Agda.Interaction.Options
69
import Agda.Utils.FileName
70
import Agda.Utils.Functor
71
import Agda.Utils.Fresh
72
import Agda.Utils.List
59
73
import Agda.Utils.Monad
60
import Agda.Utils.Tuple
61
import Agda.Utils.List
62
import Agda.Utils.Fresh
63
74
import Agda.Utils.Pretty
65
76
#include "../../undefined.h"
138
144
recordConstructorType :: [NiceDeclaration] -> C.Expr
139
145
recordConstructorType fields = build fs
147
-- drop all declarations after the last field declaration
141
148
fs = reverse $ dropWhile notField $ reverse fields
143
150
notField NiceField{} = False
144
151
notField _ = True
146
build (NiceField r f _ _ x (Arg h rel e) : fs) =
147
C.Pi [C.TypedBindings r $ Arg h rel (C.TBind r [BName x f] e)] $ build fs
153
-- Andreas, 2013-11-08
154
-- Turn @open public@ into just @open@, since we cannot have an
155
-- @open public@ in a @let@. Fixes issue 532.
156
build (NiceOpen r m dir@ImportDirective{ publicOpen = True } : fs) =
157
build (NiceOpen r m dir{ publicOpen = False } : fs)
159
build (NiceModuleMacro r p x modapp open dir@ImportDirective{ publicOpen = True } : fs) =
160
build (NiceModuleMacro r p x modapp open dir{ publicOpen = False } : fs)
162
build (NiceField r f _ _ x (Common.Arg info e) : fs) =
163
C.Pi [C.TypedBindings r $ Common.Arg info (C.TBind r [mkBoundName x f] e)] $ build fs
148
164
where r = getRange x
149
build (d : fs) = C.Let noRange [killRange $ notSoNiceDeclaration d] $
165
build (d : fs) = C.Let (getRange d) [notSoNiceDeclaration d] $
151
167
build [] = C.SetN noRange 0 -- todo: nicer
170
-- | @checkModuleApplication modapp m0 x dir = return (modapp', renD, renM)@
172
-- @m0@ is the new (abstract) module name and
173
-- @x@ its concret form (used for error messages).
174
checkModuleApplication
175
:: C.ModuleApplication
179
-> ScopeM (A.ModuleApplication, Ren A.QName, Ren ModuleName)
153
181
checkModuleApplication (C.SectionApp _ tel e) m0 x dir' =
182
-- For the following, set the current module to be m0.
154
183
withCurrentModule m0 $ do
184
-- check that expression @e@ is of the form @m args@
155
185
(m, args) <- case appView e of
156
186
AppView (Ident m) args -> return (m, args)
157
187
_ -> notAModuleExpr e
189
-- scope check the telescope (introduces bindings!)
159
190
tel' <- toAbstract tel
160
(m1,args') <- toAbstract (OldModuleName m
163
s <- getNamedScope m1
191
-- scope the old module name, the module args
192
(m1,args') <- toAbstract (OldModuleName m, args)
164
193
-- Drop constructors (OnlyQualified) if there are arguments. The record constructor
165
194
-- isn't properly in the record module, so copying it will lead to badness.
166
195
let noRecConstr | null args = id
167
196
| otherwise = removeOnlyQualified
168
(s', (renM, renD)) <- copyScope m0 . noRecConstr =<< getNamedScope m1
197
-- Copy the scope associated with m and take the parts actually imported.
198
(s', (renM, renD)) <- copyScope m m0 . noRecConstr =<< getNamedScope m1
169
199
s' <- applyImportDirectiveM (C.QName x) dir' s'
200
-- Set the current scope to @s'@
170
201
modifyCurrentScope $ const s'
171
202
printScope "mod.inst" 20 "copied source module"
172
203
reportSLn "scope.mod.inst" 30 $ "renamings:\n " ++ show renD ++ "\n " ++ show renM
173
204
return ((A.SectionApp tel' m1 args'), renD, renM)
174
206
checkModuleApplication (C.RecordModuleIFS _ recN) m0 x dir' =
175
207
withCurrentModule m0 $ do
176
208
m1 <- toAbstract $ OldModuleName recN
177
209
s <- getNamedScope m1
178
(s', (renM, renD)) <- copyScope m0 s
210
(s', (renM, renD)) <- copyScope recN m0 s
179
211
s' <- applyImportDirectiveM recN dir' s'
180
212
modifyCurrentScope $ const s'
182
214
printScope "mod.inst" 20 "copied record module"
183
215
return ((A.RecordModuleIFS m1), renD, renM)
217
-- | @checkModuleMacro mkApply range access concreteName modapp open dir@
219
-- Preserves local variables.
222
:: (ModuleInfo -> ModuleName -> A.ModuleApplication -> Ren A.QName -> Ren ModuleName -> a)
226
-> C.ModuleApplication
185
230
checkModuleMacro apply r p x modapp open dir = withLocalVars $ do
186
231
notPublicWithoutOpen open dir
408
454
-- Expressions ------------------------------------------------------------
410
456
-- | Peel off 'C.HiddenArg' and represent it as an 'NamedArg'.
411
mkNamedArg :: C.Expr -> NamedArg C.Expr
412
mkNamedArg (C.HiddenArg _ e) = Arg Hidden Relevant e
413
mkNamedArg (C.InstanceArg _ e) = Arg Instance Relevant e
414
mkNamedArg e = Arg NotHidden Relevant $ unnamed e
457
mkNamedArg :: C.Expr -> C.NamedArg C.Expr
458
mkNamedArg (C.HiddenArg _ e) = Common.Arg (setHiding Hidden defaultArgInfo) e
459
mkNamedArg (C.InstanceArg _ e) = Common.Arg (setHiding Instance defaultArgInfo) e
460
mkNamedArg e = Common.Arg defaultArgInfo $ unnamed e
416
462
-- | Peel off 'C.HiddenArg' and represent it as an 'Arg', throwing away any name.
417
mkArg' :: Relevance -> C.Expr -> Arg C.Expr
418
mkArg' r (C.HiddenArg _ e) = Arg Hidden r $ namedThing e
419
mkArg' r (C.InstanceArg _ e) = Arg Instance r $ namedThing e
420
mkArg' r e = Arg NotHidden r e
463
mkArg' :: C.ArgInfo -> C.Expr -> C.Arg C.Expr
464
mkArg' info (C.HiddenArg _ e) = Common.Arg (setHiding Hidden info) $ namedThing e
465
mkArg' info (C.InstanceArg _ e) = Common.Arg (setHiding Instance info) $ namedThing e
466
mkArg' info e = Common.Arg (setHiding NotHidden info) e
422
468
-- | By default, arguments are @Relevant@.
423
mkArg :: C.Expr -> Arg C.Expr
469
mkArg :: C.Expr -> C.Arg C.Expr
424
470
-- mkArg (C.Dot _ e) = mkArg' Irrelevant e
425
mkArg e = mkArg' Relevant e
471
mkArg e = mkArg' defaultArgInfo e
428
474
-- | Parse a possibly dotted C.Expr as A.Expr. Bool = True if dotted.
633
688
C.DontCare e -> A.DontCare <$> toAbstract e
635
690
instance ToAbstract C.LamBinding A.LamBinding where
636
toAbstract (C.DomainFree h rel x) = A.DomainFree h rel <$> toAbstract (NewName x)
637
toAbstract (C.DomainFull tb) = A.DomainFull <$> toAbstract tb
691
toAbstract (C.DomainFree info x) = A.DomainFree <$> toAbstract info <*> toAbstract (NewName x)
692
toAbstract (C.DomainFull tb) = A.DomainFull <$> toAbstract tb
639
makeDomainFull :: C.LamBinding -> C.LamBinding
640
makeDomainFull b@C.DomainFull{} = b
641
makeDomainFull (C.DomainFree h rel x) =
642
C.DomainFull $ C.TypedBindings r $ Arg h rel $ C.TBind r [x] $ C.Underscore r Nothing
694
makeDomainFull :: C.LamBinding -> C.TypedBindings
695
makeDomainFull (C.DomainFull b) = b
696
makeDomainFull (C.DomainFree info x) =
697
C.TypedBindings r $ Common.Arg info $ C.TBind r [x] $ C.Underscore r Nothing
643
698
where r = getRange x
644
700
instance ToAbstract C.TypedBindings A.TypedBindings where
645
701
toAbstract (C.TypedBindings r bs) = A.TypedBindings r <$> toAbstract bs
649
705
t' <- toAbstractCtx TopCtx t
650
706
xs' <- toAbstract (map NewName xs)
651
707
return $ A.TBind r xs' t'
652
toAbstract (C.TNoBind e) = do
653
e <- toAbstractCtx TopCtx e
708
toAbstract (C.TLet r ds) = do
709
ds' <- toAbstract (LetDefs ds)
710
return $ A.TLet r ds'
712
-- | Scope check a module (top level function).
719
-> ScopeM [A.Declaration]
720
-> ScopeM [A.Declaration]
721
scopeCheckNiceModule r p name tel checkDs
722
| telHasOpenStms tel = do
723
-- Andreas, 2013-12-10:
724
-- If the module telescope contains open statements,
725
-- add an extra anonymous module around the current one.
726
-- Otherwise, the open statements would create
727
-- identifiers in the parent scope of the current module.
728
-- But open statements in the module telescope should
729
-- only affect the current module!
730
scopeCheckNiceModule noRange p noName_ [] $
731
scopeCheckNiceModule_
734
scopeCheckNiceModule_
736
-- The actual workhorse:
737
scopeCheckNiceModule_ = do
739
-- Check whether we are dealing with an anonymous module.
740
-- This corresponds to a Coq/LEGO section.
741
(name, p, open) <- do
742
if isNoName name then do
743
(i :: NameId) <- fresh
744
return (C.NoName (getRange name) i, PrivateAccess, True)
745
else return (name, p, False)
747
-- Check and bind the module, using the supplied check for its contents.
748
aname <- toAbstract (NewModuleName name)
750
scopeCheckModule r (C.QName name) aname tel checkDs
751
bindModule p name aname
753
-- If the module was anonymous open it public.
755
openModule_ (C.QName name) $
756
defaultImportDir { publicOpen = True }
759
-- | Check whether a telescope has open declarations.
760
telHasOpenStms :: C.Telescope -> Bool
761
telHasOpenStms = any isOpenBinds
763
isOpenBinds (C.TypedBindings _ tb) = isOpenBind $ unArg tb
764
isOpenBind C.TBind{} = False
765
isOpenBind (C.TLet _ ds) = any isOpen ds
766
isOpen (C.ModuleMacro _ _ _ DoOpen _) = True
767
isOpen C.Open{} = True
768
isOpen C.Import{} = __IMPOSSIBLE__
769
isOpen (C.Mutual _ ds) = any isOpen ds
770
isOpen (C.Abstract _ ds) = any isOpen ds
771
isOpen (C.Private _ ds) = any isOpen ds
775
telHasLetStms :: C.Telescope -> Bool
776
telHasLetStms = any isLetBinds
778
isLetBinds (C.TypedBindings _ tb) = isLetBind $ unArg tb
779
isLetBind C.TBind{} = False
780
isLetBind C.TLet{} = True
783
-- | We for now disallow let-bindings in @data@ and @record@ telescopes.
784
-- This due "nested datatypes"; there is no easy interpretation of
786
-- data D (A : Set) (open M A) (b : B) : Set where
787
-- c : D (A × A) b → D A b
789
-- where @B@ is brought in scope by @open M A@.
791
class EnsureNoLetStms a where
792
ensureNoLetStms :: a -> ScopeM ()
794
{- From ghc 7.2, there is LANGUAGE DefaultSignatures
795
default ensureNoLetStms :: Foldable t => t a -> ScopeM ()
796
ensureNoLetStms = traverse_ ensureNoLetStms
799
instance EnsureNoLetStms C.TypedBinding where
802
C.TLet{} -> typeError $ IllegalLetInTelescope tb
803
C.TBind{} -> return ()
805
instance EnsureNoLetStms a => EnsureNoLetStms (LamBinding' a) where
806
ensureNoLetStms = traverse_ ensureNoLetStms
808
instance EnsureNoLetStms a => EnsureNoLetStms (TypedBindings' a) where
809
ensureNoLetStms = traverse_ ensureNoLetStms
811
instance EnsureNoLetStms a => EnsureNoLetStms [a] where
812
ensureNoLetStms = traverse_ ensureNoLetStms
656
815
-- | Returns the scope inside the checked module.
657
scopeCheckModule :: Range -> C.QName -> A.ModuleName -> C.Telescope -> [C.Declaration] ->
658
ScopeM (ScopeInfo, [A.Declaration])
659
scopeCheckModule r x qm tel ds = do
818
-> C.QName -- ^ The concrete name of the module.
819
-> A.ModuleName -- ^ The abstract name of the module.
820
-> C.Telescope -- ^ The module telescope.
821
-> ScopeM [A.Declaration] -- ^ The code for checking the module contents.
822
-> ScopeM (ScopeInfo, [A.Declaration])
823
scopeCheckModule r x qm tel checkDs = do
660
824
printScope "module" 20 $ "checking module " ++ show x
661
res <- withCurrentModule qm $ do
663
-- qm <- getCurrentModule
664
printScope "module" 20 $ "inside module " ++ show x
666
tel <- toAbstract tel
667
ds <- (:[]) . A.Section info (qm `withRangesOfQ` x) tel <$>
825
-- Andreas, 2013-12-10: Telescope does not live in the new module
826
-- but its parent, so check it before entering the new module.
827
-- This is important for Nicolas Pouillard's open parametrized modules
828
-- statements inside telescopes.
829
res <- withLocalVars $ do
830
tel <- toAbstract tel
831
withCurrentModule qm $ do
833
-- qm <- getCurrentModule
834
printScope "module" 20 $ "inside module " ++ show x
669
836
scope <- getScope
837
return (scope, [ A.Section info (qm `withRangesOfQ` x) tel ds ])
672
839
-- Binding is done by the caller
673
840
printScope "module" 20 $ "after module " ++ show x
691
864
-- Top-level declarations are always (import|open)* module
692
865
instance ToAbstract (TopLevel [C.Declaration]) TopLevelInfo where
693
toAbstract (TopLevel ds) = case splitAt (length ds - 1) ds of
694
(ds', [C.Module r m tel ds]) -> do
866
toAbstract (TopLevel file ds) =
867
-- A file is a bunch of preliminary decls (imports etc.)
868
-- plus a single module decl.
869
case splitAt (length ds - 1) ds of
870
(ds', [C.Module r m0 tel ds]) -> do
871
-- If the module name is _ compute the name from the file path
873
then return $ C.QName $ C.Name noRange [Id $ stringToRawName $ rootName file]
875
-- Andreas, 2014-03-28 Issue 1078
876
-- We need to check the module name against the file name here.
877
-- Otherwise one could sneak in a lie and confuse the scope
879
checkModuleName (C.toTopLevelModuleName m0) file
695
881
setTopLevelModule m
696
882
am <- toAbstract (NewModuleQName m)
697
883
ds' <- toAbstract ds'
698
(scope0, ds) <- scopeCheckModule r m am tel ds
884
(scope0, ds) <- scopeCheckModule r m am tel $ toAbstract ds
699
885
scope <- getScope
700
886
return $ TopLevelInfo (ds' ++ ds) scope scope0
701
887
_ -> __IMPOSSIBLE__
726
912
instance ToAbstract LetDef [A.LetBinding] where
727
913
toAbstract (LetDef d) =
729
NiceMutual _ _ d@[C.FunSig _ fx _ rel _ x t, C.FunDef _ _ _ abstract _ _ [cl]] ->
915
NiceMutual _ _ d@[C.FunSig _ fx _ info _ x t, C.FunDef _ _ _ abstract _ _ [cl]] ->
730
916
do when (abstract == AbstractDef) $ do
731
917
typeError $ GenericError $ "abstract not allowed in let expressions"
732
918
e <- letToAbstract cl
733
919
t <- toAbstract t
734
x <- toAbstract (NewName $ C.BName x fx)
735
return [ A.LetBind (LetRange $ getRange d) rel x t e ]
920
x <- toAbstract (NewName $ mkBoundName x fx)
921
info <- toAbstract info
922
return [ A.LetBind (LetRange $ getRange d) info x t e ]
737
924
-- irrefutable let binding, like (x , y) = rhs
738
NiceFunClause r PublicAccess ConcreteDef termCheck d@(C.FunClause (C.LHS p [] [] []) (C.RHS rhs) NoWhere) -> do
739
rhs <- toAbstract rhs
742
checkPatternLinearity [p]
744
return [ A.LetPatBind (LetRange r) p rhs ]
925
NiceFunClause r PublicAccess ConcreteDef termCheck d@(C.FunClause lhs@(C.LHS p [] [] []) (C.RHS rhs) NoWhere) -> do
926
mp <- setCurrentRange (getRange p) $ (Right <$> parsePattern p) `catchError` (return . Left)
929
rhs <- toAbstract rhs
931
checkPatternLinearity [p]
933
return [ A.LetPatBind (LetRange r) p rhs ]
934
-- It's not a record pattern, so it should be a prefix left-hand side
936
case definedName p of
937
Nothing -> throwError err
938
Just x -> toAbstract $ LetDef $ NiceMutual r termCheck
939
[ C.FunSig r defaultFixity' PublicAccess defaultArgInfo termCheck x (C.Underscore (getRange x) Nothing)
940
, C.FunDef r __IMPOSSIBLE__ __IMPOSSIBLE__ ConcreteDef __IMPOSSIBLE__ __IMPOSSIBLE__
941
[C.Clause x lhs (C.RHS rhs) NoWhere []]
944
definedName (C.IdentP (C.QName x)) = Just x
945
definedName C.IdentP{} = Nothing
946
definedName (C.RawAppP _ (p : _)) = definedName p
947
definedName (C.ParenP _ p) = definedName p
948
definedName C.WildP{} = Nothing -- for instance let _ + x = x in ... (not allowed)
949
definedName C.AbsurdP{} = Nothing
950
definedName C.AsP{} = Nothing
951
definedName C.DotP{} = Nothing
952
definedName C.LitP{} = Nothing
953
definedName C.HiddenP{} = __IMPOSSIBLE__
954
definedName C.InstanceP{} = __IMPOSSIBLE__
955
definedName C.RawAppP{} = __IMPOSSIBLE__
956
definedName C.AppP{} = __IMPOSSIBLE__
957
definedName C.OpAppP{} = __IMPOSSIBLE__
746
959
-- You can't open public in a let
747
960
NiceOpen r x dirs | not (C.publicOpen dirs) -> do
780
993
letToAbstract _ = notAValidLetBinding d
782
995
-- Named patterns not allowed in let definitions
783
lambda e (Arg h rel (Named Nothing (A.VarP x))) = return $ A.Lam i (A.DomainFree h rel x) e
996
lambda e (Common.Arg info (Named Nothing (A.VarP x))) =
997
return $ A.Lam i (A.DomainFree info x) e
785
999
i = ExprRange (fuseRange x e)
786
lambda e (Arg h rel (Named Nothing (A.WildP i))) =
1000
lambda e (Common.Arg info (Named Nothing (A.WildP i))) =
787
1001
do x <- freshNoName (getRange i)
788
return $ A.Lam i' (A.DomainFree h rel x) e
1002
return $ A.Lam i' (A.DomainFree info x) e
790
1004
i' = ExprRange (fuseRange i e)
791
1005
lambda _ _ = notAValidLetBinding d
1007
newtype Blind a = Blind { unBlind :: a }
1009
instance ToAbstract (Blind a) (Blind a) where
793
1012
-- The only reason why we return a list is that open declarations disappears.
794
1013
-- For every other declaration we get a singleton list.
795
1014
instance ToAbstract NiceDeclaration A.Declaration where
798
1017
traceCall (ScopeCheckDeclaration d) $
1020
-- Axiom (actual postulate)
802
1021
C.Axiom r f p rel x t -> do
803
1022
-- check that we do not postulate in --safe mode
804
1023
clo <- commandLineOptions
805
1024
when (optSafe clo) (typeError (SafeFlagPostulate x))
806
1025
-- check the postulate
807
toAbstractNiceAxiom d
1026
toAbstractNiceAxiom A.NoFunSig d
810
1029
C.NiceField r f p a x t -> do
811
1030
unless (p == PublicAccess) $ typeError $ GenericError "Record fields can not be private"
812
t' <- toAbstractCtx TopCtx t
1031
-- Interaction points for record fields have already been introduced
1032
-- when checking the type of the record constructor.
1033
-- To avoid introducing interaction points (IP) twice, we turn
1034
-- all question marks to underscores. (See issue 1138.)
1035
let maskIP (C.QuestionMark r _) = C.Underscore r Nothing
1037
t' <- toAbstractCtx TopCtx $ mapExpr maskIP t
813
1038
y <- freshAbstractQName f x
814
1039
irrProj <- optIrrelevantProjections <$> pragmaOptions
815
unless (argRelevance t == Irrelevant && not irrProj) $
1040
unless (isIrrelevant t && not irrProj) $
816
1041
-- Andreas, 2010-09-24: irrelevant fields are not in scope
817
1042
-- this ensures that projections out of irrelevant fields cannot occur
818
1043
-- Ulf: unless you turn on --irrelevant-projections
831
1056
ds' <- toAbstract ds
832
1057
return [ A.Mutual (MutualInfo termCheck r) ds' ]
834
C.NiceRecSig r f a x ls t -> withLocalVars $ do
835
let toTypeBinding :: C.LamBinding -> C.TypedBindings
836
toTypeBinding b = case makeDomainFull b of
839
ls' <- toAbstract (map toTypeBinding ls)
1059
C.NiceRecSig r f a x ls t -> do
1062
ls' <- toAbstract (map makeDomainFull ls)
840
1063
x' <- freshAbstractQName f x
841
1064
bindName a DefName x x'
842
1065
t' <- toAbstract t
843
1066
return [ A.RecSig (mkDefInfo x f a ConcreteDef r) x' ls' t' ]
844
1068
C.NiceDataSig r f a x ls t -> withLocalVars $ do
845
1069
printScope "scope.data.sig" 20 ("checking DataSig for " ++ show x)
846
let toTypeBinding :: C.LamBinding -> C.TypedBindings
847
toTypeBinding b = case makeDomainFull b of
850
ls' <- toAbstract (map toTypeBinding ls)
1071
ls' <- toAbstract (map makeDomainFull ls)
851
1072
x' <- freshAbstractQName f x
852
1073
{- -- Andreas, 2012-01-16: remember number of parameters
853
1074
bindName a (DataName (length ls)) x x' -}
920
1147
printScope "rec" 15 "record complete"
921
1148
return [ A.RecDef (mkDefInfo x f PublicAccess a r) x' ind cm' pars contel afields ]
923
-- Andreas, 2012-10-30 anonymous modules are like Coq sections
924
NiceModule r p a (C.QName name) tel ds ->
925
traceCall (ScopeCheckDeclaration $ NiceModule r p a (C.QName name) tel []) $ do
926
(name, p, isSection) <- if not (C.isNoName name)
927
then return (name, p, False)
929
(i :: NameId) <- fresh
930
return (C.NoName (getRange name) i, PrivateAccess, True)
931
aname <- toAbstract (NewModuleName name)
932
ds <- snd <$> scopeCheckModule r (C.QName name) aname tel ds
933
bindModule p name aname
934
-- if the module was anonymous open it public
936
openModule_ (C.QName name) $
937
defaultImportDir { publicOpen = True }
940
NiceModule _ _ _ C.Qual{} _ _ -> __IMPOSSIBLE__
942
NiceModuleMacro r p a x modapp open dir ->
1150
NiceModule r p a x@(C.QName name) tel ds ->
1151
traceCall (ScopeCheckDeclaration $ NiceModule r p a x tel []) $ do
1152
scopeCheckNiceModule r p name tel $ toAbstract ds
1154
NiceModule _ _ _ m@C.Qual{} _ _ ->
1155
typeError $ GenericError $ "Local modules cannot have qualified names"
1157
NiceModuleMacro r p x modapp open dir ->
943
1158
checkModuleMacro Apply r p x modapp open dir
945
1160
NiceOpen r x dir -> do
1013
1227
NicePatternSyn r fx n as p -> do
1014
1228
reportSLn "scope.pat" 10 $ "found nice pattern syn: " ++ show r
1016
isparameterised <- not . null <$> getLocalVars
1017
when isparameterised $ typeError $ NotSupported
1018
"pattern synonym in parameterised module"
1020
1230
y <- freshAbstractQName fx n
1021
1231
bindName PublicAccess PatternSynName n y
1022
defn <- withLocalVars $ do
1023
p' <- killRange <$> (toAbstract =<< toAbstract =<< parsePatternSyn p)
1024
as' <- mapM (\a -> unVarName =<< resolveName (C.QName a)) as
1232
defn@(as, p) <- withLocalVars $ do
1233
p <- toAbstract =<< toAbstract =<< parsePatternSyn p
1234
as <- (traverse . mapM) (unVarName <=< resolveName . C.QName) as
1235
as <- (map . fmap) unBlind <$> toAbstract ((map . fmap) Blind as)
1026
1237
modifyPatternSyns (Map.insert y defn)
1238
return [A.PatternSynDef y as p] -- only for highlighting
1028
1239
where unVarName (VarName a) = return a
1029
1240
unVarName _ = typeError $ UnusedVariableInPatternSynonym
1032
1243
-- checking postulate or type sig. without checking safe flag
1033
toAbstractNiceAxiom (C.Axiom r f p rel x t) = do
1244
toAbstractNiceAxiom funSig (C.Axiom r f p info x t) = do
1034
1245
t' <- toAbstractCtx TopCtx t
1035
1246
y <- freshAbstractQName f x
1247
info <- toAbstract info
1036
1248
bindName p DefName x y
1037
return [ A.Axiom (mkDefInfo x f p ConcreteDef r) rel y t' ]
1038
toAbstractNiceAxiom _ = __IMPOSSIBLE__
1249
return [ A.Axiom funSig (mkDefInfo x f p ConcreteDef r) info y t' ]
1250
toAbstractNiceAxiom _ _ = __IMPOSSIBLE__
1041
1253
data IsRecordCon = YesRec | NoRec
1293
1531
toAbstract (A.DefP i x as) = A.DefP i x <$> mapM toAbstract as
1294
1532
toAbstract (A.WildP i) = return $ A.WildP i
1295
1533
toAbstract (A.AsP i x p) = A.AsP i x <$> toAbstract p
1296
toAbstract (A.DotP i e) = A.DotP i <$> toAbstract e
1534
toAbstract (A.DotP i e) = A.DotP i <$> insideDotPattern (toAbstract e)
1297
1535
toAbstract (A.AbsurdP i) = return $ A.AbsurdP i
1298
1536
toAbstract (A.LitP l) = return $ A.LitP l
1299
1537
toAbstract (A.ImplicitP i) = return $ A.ImplicitP i
1300
toAbstract (A.PatternSynP i x as) = do
1301
p <- lookupPatternSyn x
1302
as' <- mapM toAbstract as
1303
instPatternSyn p as'
1305
instPatternSyn :: A.PatternSynDefn -> [NamedArg A.Pattern] -> ScopeM A.Pattern
1306
instPatternSyn (ns, p) as
1307
| length ns == length as = return $ substPattern s $ setRange (getRange i) p
1308
| otherwise = typeError $ PatternSynonymArityMismatch x
1310
s = zipWith' (\n a -> (n, namedThing (unArg a))) ns as
1538
toAbstract (A.PatternSynP i x as) = A.PatternSynP i x <$> mapM toAbstract as
1313
1540
instance ToAbstract C.Pattern (A.Pattern' C.Expr) where
1373
1600
-- | Turn an operator application into abstract syntax. Make sure to record the
1374
1601
-- right precedences for the various arguments.
1375
toAbstractOpApp :: C.QName -> [OpApp C.Expr] -> ScopeM A.Expr
1602
toAbstractOpApp :: C.QName -> [C.NamedArg (OpApp C.Expr)] -> ScopeM A.Expr
1376
1603
toAbstractOpApp op es = do
1377
1604
f <- getFixity op
1378
1605
let (_,_,parts) = oldToNewNotation $ (op, f)
1379
1606
op <- toAbstract (OldQName op)
1380
1607
foldl' app op <$> left (theFixity f) [p | p <- parts, not (isBindingHole p)] es
1382
app e arg = A.App (ExprRange (fuseRange e arg)) e
1383
$ Arg NotHidden Relevant $ unnamed arg
1609
app e arg = A.App (ExprRange (fuseRange e arg)) e (setArgColors [] arg)
1611
toAbsOpArg cxt = traverse $ traverse $ toAbstractOpArg cxt
1385
1613
left f (IdPart _ : xs) es = inside f xs es
1386
1614
left f (_ : xs) (e : es) = do
1387
e <- toAbstractOpArg (LeftOperandCtx f) e
1615
e <- toAbsOpArg (LeftOperandCtx f) e
1388
1616
es <- inside f xs es
1389
1617
return (e : es)
1390
1618
left f (_ : _) [] = __IMPOSSIBLE__
1391
1619
left f [] _ = __IMPOSSIBLE__
1393
inside f [x] es = right f x es
1394
inside f (IdPart _ : xs) es = inside f xs es
1621
inside f [x] es = right f x es
1622
inside f (IdPart _ : xs) es = inside f xs es
1395
1623
inside f (_ : xs) (e : es) = do
1396
e <- toAbstractOpArg InsideOperandCtx e
1624
e <- toAbsOpArg InsideOperandCtx e
1397
1625
es <- inside f xs es
1398
1626
return (e : es)
1399
1627
inside _ (_ : _) [] = __IMPOSSIBLE__