on ec97cac help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
2461 lines 110 kB history raw
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 DeriveDataTypeable, TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
6import Idris.Core.TT
7import Idris.Core.Evaluate
8import Idris.Core.Elaborate hiding (Tactic(..))
9import Idris.Core.Typecheck
10import Idris.Docstrings
11import IRTS.Lang
12import IRTS.CodegenCommon
13import Util.Pretty
14import Util.DynamicLinker
16import Idris.Colours
18import System.Console.Haskeline
19import System.IO
21import Prelude hiding ((<$>))
23import Control.Applicative ((<|>))
25import Control.Monad.Trans.State.Strict
26import Control.Monad.Trans.Except
27import qualified Control.Monad.Trans.Class as Trans (lift)
29import Data.Data (Data)
30import Data.Function (on)
31import Data.Generics.Uniplate.Data (universe, children)
32import Data.List hiding (group)
33import Data.Char
34import qualified Data.Map.Strict as M
35import qualified Data.Text as T
36import Data.Either
37import qualified Data.Set as S
38import Data.Word (Word)
39import Data.Maybe (fromMaybe, mapMaybe, maybeToList)
40import Data.Traversable (Traversable)
41import Data.Typeable
42import Data.Foldable (Foldable)
44import Debug.Trace
46import Text.PrettyPrint.Annotated.Leijen
48data ElabWhat = ETypes | EDefns | EAll
49 deriving (Show, Eq)
51-- Data to pass to recursively called elaborators; e.g. for where blocks,
52-- paramaterised declarations, etc.
54-- rec_elabDecl is used to pass the top level elaborator into other elaborators,
55-- so that we can have mutually recursive elaborators in separate modules without
56-- having to muck about with cyclic modules.
57data ElabInfo = EInfo { params :: [(Name, PTerm)],
58 inblock :: Ctxt [Name], -- names in the block, and their params
59 liftname :: Name -> Name,
60 namespace :: Maybe [String],
61 elabFC :: Maybe FC,
62 rec_elabDecl :: ElabWhat -> ElabInfo -> PDecl ->
63 Idris () }
65toplevel :: ElabInfo
66toplevel = EInfo [] emptyContext id Nothing Nothing (\_ _ _ -> fail "Not implemented")
68eInfoNames :: ElabInfo -> [Name]
69eInfoNames info = map fst (params info) ++ M.keys (inblock info)
71data IOption = IOption {
72 opt_logLevel :: Int
73 , opt_logcats :: [LogCat] -- ^ List of logging categories.
74 , opt_typecase :: Bool
75 , opt_typeintype :: Bool
76 , opt_coverage :: Bool
77 , opt_showimp :: Bool -- ^ show implicits
78 , opt_errContext :: Bool
79 , opt_repl :: Bool
80 , opt_verbose :: Bool
81 , opt_nobanner :: Bool
82 , opt_quiet :: Bool
83 , opt_codegen :: Codegen
84 , opt_outputTy :: OutputType
85 , opt_ibcsubdir :: FilePath
86 , opt_importdirs :: [FilePath]
87 , opt_triple :: String
88 , opt_cpu :: String
89 , opt_cmdline :: [Opt] -- remember whole command line
90 , opt_origerr :: Bool
91 , opt_autoSolve :: Bool -- ^ automatically apply "solve" tactic in prover
92 , opt_autoImport :: [FilePath] -- ^ e.g. Builtins+Prelude
93 , opt_optimise :: [Optimisation]
94 , opt_printdepth :: Maybe Int
95 , opt_evaltypes :: Bool -- ^ normalise types in :t
96 , opt_desugarnats :: Bool
97 , opt_autoimpls :: Bool
98 } deriving (Show, Eq)
100defaultOpts = IOption { opt_logLevel = 0
101 , opt_logcats = []
102 , opt_typecase = False
103 , opt_typeintype = False
104 , opt_coverage = True
105 , opt_showimp = False
106 , opt_errContext = False
107 , opt_repl = True
108 , opt_verbose = True
109 , opt_nobanner = False
110 , opt_quiet = False
111 , opt_codegen = Via "c"
112 , opt_outputTy = Executable
113 , opt_ibcsubdir = ""
114 , opt_importdirs = []
115 , opt_triple = ""
116 , opt_cpu = ""
117 , opt_cmdline = []
118 , opt_origerr = False
119 , opt_autoSolve = True
120 , opt_autoImport = []
121 , opt_optimise = defaultOptimise
122 , opt_printdepth = Just 5000
123 , opt_evaltypes = True
124 , opt_desugarnats = False
125 , opt_autoimpls = True
126 }
128data PPOption = PPOption {
129 ppopt_impl :: Bool -- ^^ whether to show implicits
130 , ppopt_desugarnats :: Bool
131 , ppopt_pinames :: Bool -- ^^ whether to show names in pi bindings
132 , ppopt_depth :: Maybe Int
133} deriving (Show)
135data Optimisation = PETransform -- partial eval and associated transforms
136 deriving (Show, Eq)
138defaultOptimise = [PETransform]
140-- | Pretty printing options with default verbosity.
141defaultPPOption :: PPOption
142defaultPPOption = PPOption { ppopt_impl = False,
143 ppopt_desugarnats = False,
144 ppopt_pinames = False,
145 ppopt_depth = Just 200 }
147-- | Pretty printing options with the most verbosity.
148verbosePPOption :: PPOption
149verbosePPOption = PPOption { ppopt_impl = True,
150 ppopt_desugarnats = True,
151 ppopt_pinames = True,
152 ppopt_depth = Just 200 }
154-- | Get pretty printing options from the big options record.
155ppOption :: IOption -> PPOption
156ppOption opt = PPOption {
157 ppopt_impl = opt_showimp opt,
158 ppopt_pinames = False,
159 ppopt_depth = opt_printdepth opt,
160 ppopt_desugarnats = opt_desugarnats opt
163-- | Get pretty printing options from an idris state record.
164ppOptionIst :: IState -> PPOption
165ppOptionIst = ppOption . idris_options
167data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
169-- | The output mode in use
170data OutputMode = RawOutput Handle -- ^ Print user output directly to the handle
171 | IdeMode Integer Handle -- ^ Send IDE output for some request ID to the handle
172 deriving Show
174-- | How wide is the console?
175data ConsoleWidth = InfinitelyWide -- ^ Have pretty-printer assume that lines should not be broken
176 | ColsWide Int -- ^ Manually specified - must be positive
177 | AutomaticWidth -- ^ Attempt to determine width, or 80 otherwise
178 deriving (Show, Eq)
181-- | The global state used in the Idris monad
182data IState = IState {
183 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
184 idris_constraints :: S.Set ConstraintFC,
185 -- ^ A list of universe constraints and their corresponding source locations
186 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
187 idris_implicits :: Ctxt [PArg],
188 idris_statics :: Ctxt [Bool],
189 idris_classes :: Ctxt ClassInfo,
190 idris_records :: Ctxt RecordInfo,
191 idris_dsls :: Ctxt DSL,
192 idris_optimisation :: Ctxt OptInfo,
193 idris_datatypes :: Ctxt TypeInfo,
194 idris_namehints :: Ctxt [Name],
195 idris_patdefs :: Ctxt ([([(Name, Term)], Term, Term)], [PTerm]), -- not exported
196 -- ^ list of lhs/rhs, and a list of missing clauses
197 idris_flags :: Ctxt [FnOpt],
198 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
199 idris_docstrings :: Ctxt (Docstring DocTerm, [(Name, Docstring DocTerm)]),
200 idris_moduledocs :: Ctxt (Docstring DocTerm),
201 -- ^ module documentation is saved in a special MN so the context
202 -- mechanism can be used for disambiguation
203 idris_tyinfodata :: Ctxt TIData,
204 idris_fninfo :: Ctxt FnInfo,
205 idris_transforms :: Ctxt [(Term, Term)],
206 idris_autohints :: Ctxt [Name],
207 idris_totcheck :: [(FC, Name)], -- names to check totality on
208 idris_defertotcheck :: [(FC, Name)], -- names to check at the end
209 idris_totcheckfail :: [(FC, String)],
210 idris_options :: IOption,
211 idris_name :: Int,
212 idris_lineapps :: [((FilePath, Int), PTerm)],
213 -- ^ Full application LHS on source line
214 idris_metavars :: [(Name, (Maybe Name, Int, [Name], Bool, Bool))],
215 -- ^ The currently defined but not proven metavariables. The Int
216 -- is the number of vars to display as a context, the Maybe Name
217 -- is its top-level function, the [Name] is the list of local variables
218 -- available for proof search and the Bools are whether :p is
219 -- allowed, and whether the variable is definable at all
220 -- (Metavariables are not definable if they are applied in a term which
221 -- still has hole bindings)
222 idris_coercions :: [Name],
223 idris_errRev :: [(Term, Term)],
224 syntax_rules :: SyntaxRules,
225 syntax_keywords :: [String],
226 imported :: [FilePath], -- ^ The imported modules
227 idris_scprims :: [(Name, (Int, PrimFn))],
228 idris_objs :: [(Codegen, FilePath)],
229 idris_libs :: [(Codegen, String)],
230 idris_cgflags :: [(Codegen, String)],
231 idris_hdrs :: [(Codegen, String)],
232 idris_imported :: [(FilePath, Bool)], -- ^ Imported ibc file names, whether public
233 proof_list :: [(Name, (Bool, [String]))],
234 errSpan :: Maybe FC,
235 parserWarnings :: [(FC, Err)],
236 lastParse :: Maybe Name,
237 indent_stack :: [Int],
238 brace_stack :: [Maybe Int],
239 lastTokenSpan :: Maybe FC, -- ^ What was the span of the latest token parsed?
240 idris_parsedSpan :: Maybe FC,
241 hide_list :: Ctxt Accessibility,
242 default_access :: Accessibility,
243 default_total :: Bool,
244 ibc_write :: [IBCWrite],
245 compiled_so :: Maybe String,
246 idris_dynamic_libs :: [DynamicLib],
247 idris_language_extensions :: [LanguageExt],
248 idris_outputmode :: OutputMode,
249 idris_colourRepl :: Bool,
250 idris_colourTheme :: ColourTheme,
251 idris_errorhandlers :: [Name], -- ^ Global error handlers
252 idris_nameIdx :: (Int, Ctxt (Int, Name)),
253 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
254 module_aliases :: M.Map [T.Text] [T.Text],
255 idris_consolewidth :: ConsoleWidth, -- ^ How many chars wide is the console?
256 idris_postulates :: S.Set Name,
257 idris_externs :: S.Set (Name, Int),
258 idris_erasureUsed :: [(Name, Int)], -- ^ Function/constructor name, argument position is used
259 idris_whocalls :: Maybe (M.Map Name [Name]),
260 idris_callswho :: Maybe (M.Map Name [Name]),
261 idris_repl_defs :: [Name], -- ^ List of names that were defined in the repl, and can be re-/un-defined
262 elab_stack :: [(Name, Bool)], -- ^ Stack of names currently being elaborated, Bool set if it's an instance
263 -- (instances appear twice; also as a funtion name)
264 idris_symbols :: M.Map Name Name, -- ^ Symbol table (preserves sharing of names)
265 idris_exports :: [Name], -- ^ Functions with ExportList
266 idris_highlightedRegions :: [(FC, OutputAnnotation)], -- ^ Highlighting information to output
267 idris_parserHighlights :: [(FC, OutputAnnotation)], -- ^ Highlighting information from the parser
268 idris_deprecated :: Ctxt String, -- ^ Deprecated names and explanation
269 idris_inmodule :: S.Set Name, -- ^ Names defined in current module
270 idris_ttstats :: M.Map Term (Int, Term)
271 }
273-- Required for parsers library, and therefore trifecta
274instance Show IState where
275 show = const "{internal state}"
277data SizeChange = Smaller | Same | Bigger | Unknown
278 deriving (Show, Eq)
279{-!
280deriving instance Binary SizeChange
281deriving instance NFData SizeChange
282!-}
284type SCGEntry = (Name, [Maybe (Int, SizeChange)])
285type UsageReason = (Name, Int) -- fn_name, its_arg_number
287data CGInfo = CGInfo { calls :: [Name],
288 scg :: [SCGEntry],
289 usedpos :: [(Int, [UsageReason])]
290 }
291 deriving Show
292{-!
293deriving instance Binary CGInfo
294deriving instance NFData CGInfo
295!-}
297primDefs = [sUN "unsafePerformPrimIO",
298 sUN "mkLazyForeignPrim",
299 sUN "mkForeignPrim",
300 sUN "void"]
302-- information that needs writing for the current module's .ibc file
303data IBCWrite = IBCFix FixDecl
304 | IBCImp Name
305 | IBCStatic Name
306 | IBCClass Name
307 | IBCRecord Name
308 | IBCInstance Bool Bool Name Name
309 | IBCDSL Name
310 | IBCData Name
311 | IBCOpt Name
312 | IBCMetavar Name
313 | IBCSyntax Syntax
314 | IBCKeyword String
315 | IBCImport (Bool, FilePath) -- True = import public
316 | IBCImportDir FilePath
317 | IBCObj Codegen FilePath
318 | IBCLib Codegen String
319 | IBCCGFlag Codegen String
320 | IBCDyLib String
321 | IBCHeader Codegen String
322 | IBCAccess Name Accessibility
323 | IBCMetaInformation Name MetaInformation
324 | IBCTotal Name Totality
325 | IBCFlags Name [FnOpt]
326 | IBCFnInfo Name FnInfo
327 | IBCTrans Name (Term, Term)
328 | IBCErrRev (Term, Term)
329 | IBCCG Name
330 | IBCDoc Name
331 | IBCCoercion Name
332 | IBCDef Name -- i.e. main context
333 | IBCNameHint (Name, Name)
334 | IBCLineApp FilePath Int PTerm
335 | IBCErrorHandler Name
336 | IBCFunctionErrorHandler Name Name Name
337 | IBCPostulate Name
338 | IBCExtern (Name, Int)
339 | IBCTotCheckErr FC String
340 | IBCParsedRegion FC
341 | IBCModDocs Name -- ^ The name is the special name used to track module docs
342 | IBCUsage (Name, Int)
343 | IBCExport Name
344 | IBCAutoHint Name Name
345 | IBCDeprecate Name String
346 deriving Show
348-- | The initial state for the compiler
349idrisInit :: IState
350idrisInit = IState initContext S.empty []
351 emptyContext emptyContext emptyContext emptyContext
352 emptyContext emptyContext emptyContext emptyContext
353 emptyContext emptyContext emptyContext emptyContext
354 emptyContext emptyContext emptyContext emptyContext
355 emptyContext
356 [] [] [] defaultOpts 6 [] [] [] [] emptySyntaxRules [] [] [] [] [] [] []
357 [] [] Nothing [] Nothing [] [] Nothing Nothing emptyContext Private False [] Nothing [] []
358 (RawOutput stdout) True defaultTheme [] (0, emptyContext) emptyContext M.empty
359 AutomaticWidth S.empty S.empty [] Nothing Nothing [] [] M.empty [] [] []
360 emptyContext S.empty M.empty
363-- | The monad for the main REPL - reading and processing files and updating
364-- global state (hence the IO inner monad).
365--type Idris = WriterT [Either String (IO ())] (State IState a))
366type Idris = StateT IState (ExceptT Err IO)
368catchError :: Idris a -> (Err -> Idris a) -> Idris a
369catchError = liftCatch catchE
371throwError :: Err -> Idris a
372throwError = Trans.lift . throwE
374-- Commands in the REPL
376data Codegen = Via String
377-- | ViaC
378-- | ViaJava
379-- | ViaNode
380-- | ViaJavaScript
381-- | ViaLLVM
382 | Bytecode
383 deriving (Show, Eq)
384{-!
385deriving instance NFData Codegen
386!-}
388data HowMuchDocs = FullDocs | OverviewDocs
390-- | REPL commands
391data Command = Quit
392 | Help
393 | Eval PTerm
394 | NewDefn [PDecl] -- ^ Each 'PDecl' should be either a type declaration (at most one) or a clause defining the same name.
395 | Undefine [Name]
396 | Check PTerm
397 | Core PTerm
398 | DocStr (Either Name Const) HowMuchDocs
399 | TotCheck Name
400 | Reload
401 | Watch
402 | Load FilePath (Maybe Int) -- up to maximum line number
403 | ChangeDirectory FilePath
404 | ModImport String
405 | Edit
406 | Compile Codegen String
407 | Execute PTerm
408 | ExecVal PTerm
409 | Metavars
410 | Prove Bool Name -- ^ If false, use prover, if true, use elab shell
411 | AddProof (Maybe Name)
412 | RmProof Name
413 | ShowProof Name
414 | Proofs
415 | Universes
416 | LogLvl Int
417 | LogCategory [LogCat]
418 | Spec PTerm
419 | WHNF PTerm
420 | TestInline PTerm
421 | Defn Name
422 | Missing Name
423 | DynamicLink FilePath
424 | ListDynamic
425 | Pattelab PTerm
426 | Search [String] PTerm
427 | CaseSplitAt Bool Int Name
428 | AddClauseFrom Bool Int Name
429 | AddProofClauseFrom Bool Int Name
430 | AddMissing Bool Int Name
431 | MakeWith Bool Int Name
432 | MakeCase Bool Int Name
433 | MakeLemma Bool Int Name
434 | DoProofSearch Bool -- update file
435 Bool -- recursive search
436 Int -- depth
437 Name -- top level name
438 [Name] -- hints
439 | SetOpt Opt
440 | UnsetOpt Opt
441 | NOP
442 | SetColour ColourType IdrisColour
443 | ColourOn
444 | ColourOff
445 | ListErrorHandlers
446 | SetConsoleWidth ConsoleWidth
447 | SetPrinterDepth (Maybe Int)
448 | Apropos [String] String
449 | WhoCalls Name
450 | CallsWho Name
451 | Browse [String]
452 | MakeDoc String -- IdrisDoc
453 | Warranty
454 | PrintDef Name
455 | PPrint OutputFmt Int PTerm
456 | TransformInfo Name
457 -- Debugging commands
458 | DebugInfo Name
459 | DebugUnify PTerm PTerm
461data OutputFmt = HTMLOutput | LaTeXOutput
463-- | Recognised logging categories for the Idris compiler.
464--
465-- @TODO add in sub categories.
466data LogCat = IParse
467 | IElab
468 | ICodeGen
469 | IErasure
470 | ICoverage
471 | IIBC
472 deriving (Show, Eq, Ord)
474strLogCat :: LogCat -> String
475strLogCat IParse = "parser"
476strLogCat IElab = "elab"
477strLogCat ICodeGen = "codegen"
478strLogCat IErasure = "erasure"
479strLogCat ICoverage = "coverage"
480strLogCat IIBC = "ibc"
482codegenCats :: [LogCat]
483codegenCats = [ICodeGen]
485parserCats :: [LogCat]
486parserCats = [IParse]
488elabCats :: [LogCat]
489elabCats = [IElab]
491loggingCatsStr :: String
492loggingCatsStr = unlines
493 [ (strLogCat IParse)
494 , (strLogCat IElab)
495 , (strLogCat ICodeGen)
496 , (strLogCat IErasure)
497 , (strLogCat ICoverage)
498 , (strLogCat IIBC)
499 ]
501data Opt = Filename String
502 | Quiet
503 | NoBanner
504 | ColourREPL Bool
505 | Idemode
506 | IdemodeSocket
507 | ShowLibs
508 | ShowLibdir
509 | ShowIncs
510 | ShowPkgs
511 | ShowLoggingCats
512 | NoBasePkgs
513 | NoPrelude
514 | NoBuiltins -- only for the really primitive stuff!
515 | NoREPL
516 | OLogging Int
517 | OLogCats [LogCat]
518 | Output String
519 | Interface
520 | TypeCase
521 | TypeInType
522 | DefaultTotal
523 | DefaultPartial
524 | WarnPartial
525 | WarnReach
526 | EvalTypes
527 | NoCoverage
528 | ErrContext
529 | ShowImpl
530 | Verbose
531 | Port String -- REPL TCP port
532 | IBCSubDir String
533 | ImportDir String
534 | PkgBuild String
535 | PkgInstall String
536 | PkgClean String
537 | PkgCheck String
538 | PkgREPL String
539 | PkgMkDoc String -- IdrisDoc
540 | PkgTest String
541 | PkgIndex FilePath
542 | WarnOnly
543 | Pkg String
544 | BCAsm String
545 | DumpDefun String
546 | DumpCases String
547 | UseCodegen Codegen
548 | CodegenArgs String
549 | OutputTy OutputType
550 | Extension LanguageExt
551 | InterpretScript String
552 | EvalExpr String
553 | TargetTriple String
554 | TargetCPU String
555 | OptLevel Int
556 | AddOpt Optimisation
557 | RemoveOpt Optimisation
558 | Client String
559 | ShowOrigErr
560 | AutoWidth -- ^ Automatically adjust terminal width
561 | AutoSolve -- ^ Automatically issue "solve" tactic in old-style interactive prover
562 | UseConsoleWidth ConsoleWidth
563 | DumpHighlights
564 | DesugarNats
565 | NoElimDeprecationWarnings -- ^ Don't show deprecation warnings for %elim
566 | NoOldTacticDeprecationWarnings -- ^ Don't show deprecation warnings for old-style tactics
567 deriving (Show, Eq)
569data ElabShellCmd = EQED | EAbandon | EUndo | EProofState | EProofTerm
570 | EEval PTerm | ECheck PTerm | ESearch PTerm
571 | EDocStr (Either Name Const)
572 deriving (Show, Eq)
574-- Parsed declarations
576data Fixity = Infixl { prec :: Int }
577 | Infixr { prec :: Int }
578 | InfixN { prec :: Int }
579 | PrefixN { prec :: Int }
580 deriving Eq
581{-!
582deriving instance Binary Fixity
583deriving instance NFData Fixity
584!-}
586instance Show Fixity where
587 show (Infixl i) = "infixl " ++ show i
588 show (Infixr i) = "infixr " ++ show i
589 show (InfixN i) = "infix " ++ show i
590 show (PrefixN i) = "prefix " ++ show i
592data FixDecl = Fix Fixity String
593 deriving Eq
595instance Show FixDecl where
596 show (Fix f s) = show f ++ " " ++ s
598{-!
599deriving instance Binary FixDecl
600deriving instance NFData FixDecl
601!-}
603instance Ord FixDecl where
604 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
607data Static = Static | Dynamic
608 deriving (Show, Eq, Data, Typeable)
609{-!
610deriving instance Binary Static
611deriving instance NFData Static
612!-}
614-- Mark bindings with their explicitness, and laziness
615data Plicity = Imp { pargopts :: [ArgOpt],
616 pstatic :: Static,
617 pparam :: Bool,
618 pscoped :: Maybe ImplicitInfo -- Nothing, if top level
619 }
620 | Exp { pargopts :: [ArgOpt],
621 pstatic :: Static,
622 pparam :: Bool } -- this is a param (rather than index)
623 | Constraint { pargopts :: [ArgOpt],
624 pstatic :: Static }
625 | TacImp { pargopts :: [ArgOpt],
626 pstatic :: Static,
627 pscript :: PTerm }
628 deriving (Show, Eq, Data, Typeable)
630{-!
631deriving instance Binary Plicity
632deriving instance NFData Plicity
633!-}
635is_scoped :: Plicity -> Maybe ImplicitInfo
636is_scoped (Imp _ _ _ s) = s
637is_scoped _ = Nothing
639impl = Imp [] Dynamic False (Just (Impl False True))
641forall_imp = Imp [] Dynamic False (Just (Impl False False))
642forall_constraint = Imp [] Dynamic False (Just (Impl True False))
644expl = Exp [] Dynamic False
645expl_param = Exp [] Dynamic True
647constraint = Constraint [] Static
649tacimpl t = TacImp [] Dynamic t
651data FnOpt = Inlinable -- always evaluate when simplifying
652 | TotalFn | PartialFn | CoveringFn
653 | Coinductive | AssertTotal
654 | Dictionary -- type class dictionary, eval only when
655 -- a function argument, and further evaluation resutls
656 | Implicit -- implicit coercion
657 | NoImplicit -- do not apply implicit coercions
658 | CExport String -- export, with a C name
659 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
660 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
661 | Reflection -- a reflecting function, compile-time only
662 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
663 | Constructor -- Data constructor type
664 | AutoHint -- use in auto implicit search
665 | PEGenerated -- generated by partial evaluator
666 deriving (Show, Eq)
667{-!
668deriving instance Binary FnOpt
669deriving instance NFData FnOpt
670!-}
672type FnOpts = [FnOpt]
674inlinable :: FnOpts -> Bool
675inlinable = elem Inlinable
677dictionary :: FnOpts -> Bool
678dictionary = elem Dictionary
681-- | Type provider - what to provide
682data ProvideWhat' t = ProvTerm t t -- ^ the first is the goal type, the second is the term
683 | ProvPostulate t -- ^ goal type must be Type, so only term
684 deriving (Show, Eq, Functor)
686type ProvideWhat = ProvideWhat' PTerm
688-- | Top-level declarations such as compiler directives, definitions,
689-- datatypes and typeclasses.
690data PDecl' t
691 = PFix FC Fixity [String] -- ^ Fixity declaration
692 | PTy (Docstring (Either Err t)) [(Name, Docstring (Either Err t))] SyntaxInfo FC FnOpts Name FC t -- ^ Type declaration (last FC is precise name location)
693 | PPostulate Bool -- external def if true
694 (Docstring (Either Err t)) SyntaxInfo FC FC FnOpts Name t -- ^ Postulate, second FC is precise name location
695 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
696 | PCAF FC Name t -- ^ Top level constant
697 | PData (Docstring (Either Err t)) [(Name, Docstring (Either Err t))] SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
698 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
699 | PNamespace String FC [PDecl' t]
700 -- ^ New namespace, where FC is accurate location of the
701 -- namespace in the file
702 | PRecord (Docstring (Either Err t)) SyntaxInfo FC DataOpts
703 Name -- Record name
704 FC -- Record name precise location
705 [(Name, FC, Plicity, t)] -- Parameters, where FC is precise name span
706 [(Name, Docstring (Either Err t))] -- Param Docs
707 [(Maybe (Name, FC), Plicity, t, Maybe (Docstring (Either Err t)))] -- Fields
708 (Maybe (Name, FC)) -- Optional constructor name and location
709 (Docstring (Either Err t)) -- Constructor doc
710 SyntaxInfo -- Constructor SyntaxInfo
711 -- ^ Record declaration
712 | PClass (Docstring (Either Err t)) SyntaxInfo FC
713 [(Name, t)] -- constraints
714 Name -- class name
715 FC -- accurate location of class name
716 [(Name, FC, t)] -- parameters and precise locations
717 [(Name, Docstring (Either Err t))] -- parameter docstrings
718 [(Name, FC)] -- determining parameters and precise locations
719 [PDecl' t] -- declarations
720 (Maybe (Name, FC)) -- instance constructor name and location
721 (Docstring (Either Err t)) -- instance constructor docs
722 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
723 -- class name, class name location, parameters, method declarations, optional constructor name
724 | PInstance
725 (Docstring (Either Err t)) -- Instance docs
726 [(Name, Docstring (Either Err t))] -- Parameter docs
727 SyntaxInfo
728 FC [(Name, t)] -- constraints
729 Accessibility
730 FnOpts
731 Name -- class
732 FC -- precise location of class
733 [t] -- parameters
734 t -- full instance type
735 (Maybe Name) -- explicit name
736 [PDecl' t]
737 -- ^ Instance declaration: arguments are documentation, syntax info, source
738 -- location, constraints, class name, parameters, full instance
739 -- type, optional explicit name, and definitions
740 | PDSL Name (DSL' t) -- ^ DSL declaration
741 | PSyntax FC Syntax -- ^ Syntax definition
742 | PMutual FC [PDecl' t] -- ^ Mutual block
743 | PDirective Directive -- ^ Compiler directive.
744 | PProvider (Docstring (Either Err t)) SyntaxInfo FC FC (ProvideWhat' t) Name -- ^ Type provider. The first t is the type, the second is the term. The second FC is precise highlighting location.
745 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
746 -- bool is True, lhs and rhs must be convertible
747 | PRunElabDecl FC t [String] -- ^ FC is decl-level, for errors, and
748 -- Strings represent the namespace
749 deriving Functor
750{-!
751deriving instance Binary PDecl'
752deriving instance NFData PDecl'
753!-}
755-- | The set of source directives
756data Directive = DLib Codegen String |
757 DLink Codegen String |
758 DFlag Codegen String |
759 DInclude Codegen String |
760 DHide Name |
761 DFreeze Name |
762 DAccess Accessibility |
763 DDefault Bool |
764 DLogging Integer |
765 DDynamicLibs [String] |
766 DNameHint Name FC [(Name, FC)] |
767 DErrorHandlers Name FC Name FC [(Name, FC)] |
768 DLanguage LanguageExt |
769 DDeprecate Name String |
770 DAutoImplicits Bool |
771 DUsed FC Name Name
773-- | A set of instructions for things that need to happen in IState
774-- after a term elaboration when there's been reflected elaboration.
775data RDeclInstructions = RTyDeclInstrs Name FC [PArg] Type
776 | RClausesInstrs Name [([(Name, Term)], Term, Term)]
777 | RAddInstance Name Name
778 | RDatatypeDeclInstrs Name [PArg]
779 | RDatatypeDefnInstrs Name Type [(Name, [PArg], Type)]
780 -- ^ Datatype, constructors
782-- | For elaborator state
783data EState = EState {
784 case_decls :: [(Name, PDecl)],
785 delayed_elab :: [(Int, Elab' EState ())],
786 new_tyDecls :: [RDeclInstructions],
787 highlighting :: [(FC, OutputAnnotation)]
788 }
790initEState :: EState
791initEState = EState [] [] [] []
793type ElabD a = Elab' EState a
795highlightSource :: FC -> OutputAnnotation -> ElabD ()
796highlightSource fc annot =
797 updateAux (\aux -> aux { highlighting = (fc, annot) : highlighting aux })
799-- | One clause of a top-level definition. Term arguments to constructors are:
800--
801-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
802--
803-- 2. The list of extra 'with' patterns
804--
805-- 3. The right-hand side
806--
807-- 4. The where block (PDecl' t)
809data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
810 | PWith FC Name t [t] t (Maybe (Name, FC)) [PDecl' t]
811 | PClauseR FC [t] t [PDecl' t]
812 | PWithR FC [t] t (Maybe (Name, FC)) [PDecl' t]
813 deriving Functor
814{-!
815deriving instance Binary PClause'
816deriving instance NFData PClause'
817!-}
819-- | Data declaration
820data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
821 d_name_fc :: FC, -- ^ The precise location of the type constructor name
822 d_tcon :: t, -- ^ Type constructor
823 d_cons :: [(Docstring (Either Err PTerm), [(Name, Docstring (Either Err PTerm))], Name, FC, t, FC, [Name])] -- ^ Constructors
824 }
825 -- ^ Data declaration
826 | PLaterdecl { d_name :: Name, d_name_fc :: FC, d_tcon :: t }
827 -- ^ "Placeholder" for data whose constructors are defined later
828 deriving Functor
830-- | Transform the FCs in a PData and its associated terms. The first
831-- function transforms the general-purpose FCs, and the second transforms
832-- those that are used for semantic source highlighting, so they can be
833-- treated specially.
834mapPDataFC :: (FC -> FC) -> (FC -> FC) -> PData -> PData
835mapPDataFC f g (PDatadecl n nfc tycon ctors) =
836 PDatadecl n (g nfc) (mapPTermFC f g tycon) (map ctorFC ctors)
837 where ctorFC (doc, argDocs, n, nfc, ty, fc, ns) =
838 (doc, argDocs, n, g nfc, mapPTermFC f g ty, f fc, ns)
839mapPDataFC f g (PLaterdecl n nfc tycon) =
840 PLaterdecl n (g nfc) (mapPTermFC f g tycon)
841{-!
842deriving instance Binary PData'
843deriving instance NFData PData'
844!-}
846-- Handy to get a free function for applying PTerm -> PTerm functions
847-- across a program, by deriving Functor
849type PDecl = PDecl' PTerm
850type PData = PData' PTerm
851type PClause = PClause' PTerm
853-- | Transform the FCs in a PTerm. The first function transforms the
854-- general-purpose FCs, and the second transforms those that are used
855-- for semantic source highlighting, so they can be treated specially.
856mapPDeclFC :: (FC -> FC) -> (FC -> FC) -> PDecl -> PDecl
857mapPDeclFC f g (PFix fc fixity ops) =
858 PFix (f fc) fixity ops
859mapPDeclFC f g (PTy doc argdocs syn fc opts n nfc ty) =
860 PTy doc argdocs syn (f fc) opts n (g nfc) (mapPTermFC f g ty)
861mapPDeclFC f g (PPostulate ext doc syn fc nfc opts n ty) =
862 PPostulate ext doc syn (f fc) (g nfc) opts n (mapPTermFC f g ty)
864mapPDeclFC f g (PClauses fc opts n clauses) =
865 PClauses (f fc) opts n (map (fmap (mapPTermFC f g)) clauses)
866mapPDeclFC f g (PCAF fc n tm) = PCAF (f fc) n (mapPTermFC f g tm)
867mapPDeclFC f g (PData doc argdocs syn fc opts dat) =
868 PData doc argdocs syn (f fc) opts (mapPDataFC f g dat)
869mapPDeclFC f g (PParams fc params decls) =
870 PParams (f fc)
871 (map (\(n, ty) -> (n, mapPTermFC f g ty)) params)
872 (map (mapPDeclFC f g) decls)
873mapPDeclFC f g (PNamespace ns fc decls) =
874 PNamespace ns (f fc) (map (mapPDeclFC f g) decls)
875mapPDeclFC f g (PRecord doc syn fc opts n nfc params paramdocs fields ctor ctorDoc syn') =
876 PRecord doc syn (f fc) opts n (g nfc)
877 (map (\(pn, pnfc, plic, ty) -> (pn, g pnfc, plic, mapPTermFC f g ty)) params)
878 paramdocs
879 (map (\(fn, plic, ty, fdoc) -> (fmap (\(fn', fnfc) -> (fn', g fnfc)) fn,
880 plic, mapPTermFC f g ty, fdoc))
881 fields)
882 (fmap (\(ctorN, ctorNFC) -> (ctorN, g ctorNFC)) ctor)
883 ctorDoc
884 syn'
885mapPDeclFC f g (PClass doc syn fc constrs n nfc params paramDocs det body ctor ctorDoc) =
886 PClass doc syn (f fc)
887 (map (\(constrn, constr) -> (constrn, mapPTermFC f g constr)) constrs)
888 n (g nfc) (map (\(n, nfc, pty) -> (n, g nfc, mapPTermFC f g pty)) params)
889 paramDocs (map (\(dn, dnfc) -> (dn, g dnfc)) det)
890 (map (mapPDeclFC f g) body)
891 (fmap (\(n, nfc) -> (n, g nfc)) ctor)
892 ctorDoc
893mapPDeclFC f g (PInstance doc paramDocs syn fc constrs cn acc opts cnfc params instTy instN body) =
894 PInstance doc paramDocs syn (f fc)
895 (map (\(constrN, constrT) -> (constrN, mapPTermFC f g constrT)) constrs)
896 cn acc opts (g cnfc) (map (mapPTermFC f g) params)
897 (mapPTermFC f g instTy)
898 instN
899 (map (mapPDeclFC f g) body)
900mapPDeclFC f g (PDSL n dsl) = PDSL n (fmap (mapPTermFC f g) dsl)
901mapPDeclFC f g (PSyntax fc syn) = PSyntax (f fc) $
902 case syn of
903 Rule syms tm ctxt -> Rule syms (mapPTermFC f g tm) ctxt
904 DeclRule syms decls -> DeclRule syms (map (mapPDeclFC f g) decls)
905mapPDeclFC f g (PMutual fc decls) =
906 PMutual (f fc) (map (mapPDeclFC f g) decls)
907mapPDeclFC f g (PDirective d) =
908 PDirective $
909 case d of
910 DNameHint n nfc ns ->
911 DNameHint n (g nfc) (map (\(hn, hnfc) -> (hn, g hnfc)) ns)
912 DErrorHandlers n nfc n' nfc' ns ->
913 DErrorHandlers n (g nfc) n' (g nfc') $
914 map (\(an, anfc) -> (an, g anfc)) ns
915mapPDeclFC f g (PProvider doc syn fc nfc what n) =
916 PProvider doc syn (f fc) (g nfc) (fmap (mapPTermFC f g) what) n
917mapPDeclFC f g (PTransform fc safe l r) =
918 PTransform (f fc) safe (mapPTermFC f g l) (mapPTermFC f g r)
919mapPDeclFC f g (PRunElabDecl fc script ns) =
920 PRunElabDecl (f fc) (mapPTermFC f g script) ns
922-- | Get all the names declared in a declaration
923declared :: PDecl -> [Name]
924declared (PFix _ _ _) = []
925declared (PTy _ _ _ _ _ n fc t) = [n]
926declared (PPostulate _ _ _ _ _ _ n t) = [n]
927declared (PClauses _ _ n _) = [] -- not a declaration
928declared (PCAF _ n _) = [n]
929declared (PData _ _ _ _ _ (PDatadecl n _ _ ts)) = n : map fstt ts
930 where fstt (_, _, a, _, _, _, _) = a
931declared (PData _ _ _ _ _ (PLaterdecl n _ _)) = [n]
932declared (PParams _ _ ds) = concatMap declared ds
933declared (PNamespace _ _ ds) = concatMap declared ds
934declared (PRecord _ _ _ _ n _ _ _ _ cn _ _) = n : map fst (maybeToList cn)
935declared (PClass _ _ _ _ n _ _ _ _ ms cn cd) = n : (map fst (maybeToList cn) ++ concatMap declared ms)
936declared (PInstance _ _ _ _ _ _ _ _ _ _ _ _ _) = []
937declared (PDSL n _) = [n]
938declared (PSyntax _ _) = []
939declared (PMutual _ ds) = concatMap declared ds
940declared (PDirective _) = []
941declared _ = []
943-- get the names declared, not counting nested parameter blocks
944tldeclared :: PDecl -> [Name]
945tldeclared (PFix _ _ _) = []
946tldeclared (PTy _ _ _ _ _ n _ t) = [n]
947tldeclared (PPostulate _ _ _ _ _ _ n t) = [n]
948tldeclared (PClauses _ _ n _) = [] -- not a declaration
949tldeclared (PRecord _ _ _ _ n _ _ _ _ cn _ _) = n : map fst (maybeToList cn)
950tldeclared (PData _ _ _ _ _ (PDatadecl n _ _ ts)) = n : map fstt ts
951 where fstt (_, _, a, _, _, _, _) = a
952tldeclared (PParams _ _ ds) = []
953tldeclared (PMutual _ ds) = concatMap tldeclared ds
954tldeclared (PNamespace _ _ ds) = concatMap tldeclared ds
955tldeclared (PClass _ _ _ _ n _ _ _ _ ms cn _) = n : (map fst (maybeToList cn) ++ concatMap tldeclared ms)
956tldeclared (PInstance _ _ _ _ _ _ _ _ _ _ _ _ _) = []
957tldeclared _ = []
959defined :: PDecl -> [Name]
960defined (PFix _ _ _) = []
961defined (PTy _ _ _ _ _ n _ t) = []
962defined (PPostulate _ _ _ _ _ _ n t) = []
963defined (PClauses _ _ n _) = [n] -- not a declaration
964defined (PCAF _ n _) = [n]
965defined (PData _ _ _ _ _ (PDatadecl n _ _ ts)) = n : map fstt ts
966 where fstt (_, _, a, _, _, _, _) = a
967defined (PData _ _ _ _ _ (PLaterdecl n _ _)) = []
968defined (PParams _ _ ds) = concatMap defined ds
969defined (PNamespace _ _ ds) = concatMap defined ds
970defined (PRecord _ _ _ _ n _ _ _ _ cn _ _) = n : map fst (maybeToList cn)
971defined (PClass _ _ _ _ n _ _ _ _ ms cn _) = n : (map fst (maybeToList cn) ++ concatMap defined ms)
972defined (PInstance _ _ _ _ _ _ _ _ _ _ _ _ _) = []
973defined (PDSL n _) = [n]
974defined (PSyntax _ _) = []
975defined (PMutual _ ds) = concatMap defined ds
976defined (PDirective _) = []
977defined _ = []
979updateN :: [(Name, Name)] -> Name -> Name
980updateN ns n | Just n' <- lookup n ns = n'
981updateN _ n = n
983updateNs :: [(Name, Name)] -> PTerm -> PTerm
984updateNs [] t = t
985updateNs ns t = mapPT updateRef t
986 where updateRef (PRef fc fcs f) = PRef fc fcs (updateN ns f)
987 updateRef t = t
989-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
990-- updateDNs [] t = t
991-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
992-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
993-- where updateCNs ns (PClause n l ts r ds)
994-- = PClause (updateN ns n) (fmap (updateNs ns) l)
995-- (map (fmap (updateNs ns)) ts)
996-- (fmap (updateNs ns) r)
997-- (map (updateDNs ns) ds)
998-- updateDNs ns c = c
1000data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show, Data, Typeable)
1002-- | High level language terms
1003data PTerm = PQuote Raw -- ^ Inclusion of a core term into the high-level language
1004 | PRef FC [FC] Name -- ^ A reference to a variable. The FC is its precise source location for highlighting. The list of FCs is a collection of additional highlighting locations.
1005 | PInferRef FC [FC] Name -- ^ A name to be defined later
1006 | PPatvar FC Name -- ^ A pattern variable
1007 | PLam FC Name FC PTerm PTerm -- ^ A lambda abstraction. Second FC is name span.
1008 | PPi Plicity Name FC PTerm PTerm -- ^ (n : t1) -> t2, where the FC is for the precise location of the variable
1009 | PLet FC Name FC PTerm PTerm PTerm -- ^ A let binding (second FC is precise name location)
1010 | PTyped PTerm PTerm -- ^ Term with explicit type
1011 | PApp FC PTerm [PArg] -- ^ e.g. IO (), List Char, length x
1012 | PAppImpl PTerm [ImplicitInfo] -- ^ Implicit argument application (introduced during elaboration only)
1013 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
1014 | PMatchApp FC Name -- ^ Make an application by type matching
1015 | PIfThenElse FC PTerm PTerm PTerm -- ^ Conditional expressions - elaborated to an overloading of ifThenElse
1016 | PCase FC PTerm [(PTerm, PTerm)] -- ^ A case expression. Args are source location, scrutinee, and a list of pattern/RHS pairs
1017 | PTrue FC PunInfo -- ^ Unit type..?
1018 | PResolveTC FC -- ^ Solve this dictionary by type class resolution
1019 | PRewrite FC PTerm PTerm (Maybe PTerm) -- ^ "rewrite" syntax, with optional result type
1020 | PPair FC [FC] PunInfo PTerm PTerm -- ^ A pair (a, b) and whether it's a product type or a pair (solved by elaboration). The list of FCs is its punctuation.
1021 | PDPair FC [FC] PunInfo PTerm PTerm PTerm -- ^ A dependent pair (tm : a ** b) and whether it's a sigma type or a pair that inhabits one (solved by elaboration). The [FC] is its punctuation.
1022 | PAs FC Name PTerm -- ^ @-pattern, valid LHS only
1023 | PAlternative [(Name, Name)] PAltType [PTerm] -- ^ (| A, B, C|). Includes unapplied unique name mappings for mkUniqueNames.
1024 | PHidden PTerm -- ^ Irrelevant or hidden pattern
1025 | PType FC -- ^ 'Type' type
1026 | PUniverse Universe -- ^ Some universe
1027 | PGoal FC PTerm Name PTerm -- ^ quoteGoal, used for %reflection functions
1028 | PConstant FC Const -- ^ Builtin types
1029 | Placeholder -- ^ Underscore
1030 | PDoBlock [PDo] -- ^ Do notation
1031 | PIdiom FC PTerm -- ^ Idiom brackets
1032 | PReturn FC
1033 | PMetavar FC Name -- ^ A metavariable, ?name, and its precise location
1034 | PProof [PTactic] -- ^ Proof script
1035 | PTactics [PTactic] -- ^ As PProof, but no auto solving
1036 | PElabError Err -- ^ Error to report on elaboration
1037 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
1038 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
1039 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
1040 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
1041 | PNoImplicits PTerm -- ^ never run implicit converions on the term
1042 | PQuasiquote PTerm (Maybe PTerm) -- ^ `(Term [: Term])
1043 | PUnquote PTerm -- ^ ~Term
1044 | PQuoteName Name Bool FC -- ^ `{n} where the FC is the precise highlighting for the name in particular. If the Bool is False, then it's `{{n}} and the name won't be resolved.
1045 | PRunElab FC PTerm [String] -- ^ %runElab tm - New-style proof script. Args are location, script, enclosing namespace.
1046 | PConstSugar FC PTerm -- ^ A desugared constant. The FC is a precise source location that will be used to highlight it later.
1047 deriving (Eq, Data, Typeable)
1049data PAltType = ExactlyOne Bool -- ^ flag sets whether delay is allowed
1050 | FirstSuccess
1051 | TryImplicit
1052 deriving (Eq, Data, Typeable)
1054-- | Transform the FCs in a PTerm. The first function transforms the
1055-- general-purpose FCs, and the second transforms those that are used
1056-- for semantic source highlighting, so they can be treated specially.
1057mapPTermFC :: (FC -> FC) -> (FC -> FC) -> PTerm -> PTerm
1058mapPTermFC f g (PQuote q) = PQuote q
1059mapPTermFC f g (PRef fc fcs n) = PRef (g fc) (map g fcs) n
1060mapPTermFC f g (PInferRef fc fcs n) = PInferRef (g fc) (map g fcs) n
1061mapPTermFC f g (PPatvar fc n) = PPatvar (g fc) n
1062mapPTermFC f g (PLam fc n fc' t1 t2) = PLam (f fc) n (g fc') (mapPTermFC f g t1) (mapPTermFC f g t2)
1063mapPTermFC f g (PPi plic n fc t1 t2) = PPi plic n (g fc) (mapPTermFC f g t1) (mapPTermFC f g t2)
1064mapPTermFC f g (PLet fc n fc' t1 t2 t3) = PLet (f fc) n (g fc') (mapPTermFC f g t1) (mapPTermFC f g t2) (mapPTermFC f g t3)
1065mapPTermFC f g (PTyped t1 t2) = PTyped (mapPTermFC f g t1) (mapPTermFC f g t2)
1066mapPTermFC f g (PApp fc t args) = PApp (f fc) (mapPTermFC f g t) (map (fmap (mapPTermFC f g)) args)
1067mapPTermFC f g (PAppImpl t1 impls) = PAppImpl (mapPTermFC f g t1) impls
1068mapPTermFC f g (PAppBind fc t args) = PAppBind (f fc) (mapPTermFC f g t) (map (fmap (mapPTermFC f g)) args)
1069mapPTermFC f g (PMatchApp fc n) = PMatchApp (f fc) n
1070mapPTermFC f g (PIfThenElse fc t1 t2 t3) = PIfThenElse (f fc) (mapPTermFC f g t1) (mapPTermFC f g t2) (mapPTermFC f g t3)
1071mapPTermFC f g (PCase fc t cases) = PCase (f fc) (mapPTermFC f g t) (map (\(l,r) -> (mapPTermFC f g l, mapPTermFC f g r)) cases)
1072mapPTermFC f g (PTrue fc info) = PTrue (f fc) info
1073mapPTermFC f g (PResolveTC fc) = PResolveTC (f fc)
1074mapPTermFC f g (PRewrite fc t1 t2 t3) = PRewrite (f fc) (mapPTermFC f g t1) (mapPTermFC f g t2) (fmap (mapPTermFC f g) t3)
1075mapPTermFC f g (PPair fc hls info t1 t2) = PPair (f fc) (map g hls) info (mapPTermFC f g t1) (mapPTermFC f g t2)
1076mapPTermFC f g (PDPair fc hls info t1 t2 t3) = PDPair (f fc) (map g hls) info (mapPTermFC f g t1) (mapPTermFC f g t2) (mapPTermFC f g t3)
1077mapPTermFC f g (PAs fc n t) = PAs (f fc) n (mapPTermFC f g t)
1078mapPTermFC f g (PAlternative ns ty ts) = PAlternative ns ty (map (mapPTermFC f g) ts)
1079mapPTermFC f g (PHidden t) = PHidden (mapPTermFC f g t)
1080mapPTermFC f g (PType fc) = PType (f fc)
1081mapPTermFC f g (PUniverse u) = PUniverse u
1082mapPTermFC f g (PGoal fc t1 n t2) = PGoal (f fc) (mapPTermFC f g t1) n (mapPTermFC f g t2)
1083mapPTermFC f g (PConstant fc c) = PConstant (f fc) c
1084mapPTermFC f g Placeholder = Placeholder
1085mapPTermFC f g (PDoBlock dos) = PDoBlock (map mapPDoFC dos)
1086 where mapPDoFC (DoExp fc t) = DoExp (f fc) (mapPTermFC f g t)
1087 mapPDoFC (DoBind fc n nfc t) = DoBind (f fc) n (g nfc) (mapPTermFC f g t)
1088 mapPDoFC (DoBindP fc t1 t2 alts) =
1089 DoBindP (f fc) (mapPTermFC f g t1) (mapPTermFC f g t2) (map (\(l,r)-> (mapPTermFC f g l, mapPTermFC f g r)) alts)
1090 mapPDoFC (DoLet fc n nfc t1 t2) = DoLet (f fc) n (g nfc) (mapPTermFC f g t1) (mapPTermFC f g t2)
1091 mapPDoFC (DoLetP fc t1 t2) = DoLetP (f fc) (mapPTermFC f g t1) (mapPTermFC f g t2)
1092mapPTermFC f g (PIdiom fc t) = PIdiom (f fc) (mapPTermFC f g t)
1093mapPTermFC f g (PReturn fc) = PReturn (f fc)
1094mapPTermFC f g (PMetavar fc n) = PMetavar (g fc) n
1095mapPTermFC f g (PProof tacs) = PProof (map (fmap (mapPTermFC f g)) tacs)
1096mapPTermFC f g (PTactics tacs) = PTactics (map (fmap (mapPTermFC f g)) tacs)
1097mapPTermFC f g (PElabError err) = PElabError err
1098mapPTermFC f g PImpossible = PImpossible
1099mapPTermFC f g (PCoerced t) = PCoerced (mapPTermFC f g t)
1100mapPTermFC f g (PDisamb msg t) = PDisamb msg (mapPTermFC f g t)
1101mapPTermFC f g (PUnifyLog t) = PUnifyLog (mapPTermFC f g t)
1102mapPTermFC f g (PNoImplicits t) = PNoImplicits (mapPTermFC f g t)
1103mapPTermFC f g (PQuasiquote t1 t2) = PQuasiquote (mapPTermFC f g t1) (fmap (mapPTermFC f g) t2)
1104mapPTermFC f g (PUnquote t) = PUnquote (mapPTermFC f g t)
1105mapPTermFC f g (PRunElab fc tm ns) = PRunElab (f fc) (mapPTermFC f g tm) ns
1106mapPTermFC f g (PConstSugar fc tm) = PConstSugar (g fc) (mapPTermFC f g tm)
1107mapPTermFC f g (PQuoteName n x fc) = PQuoteName n x (g fc)
1109{-!
1110dg instance Binary PTerm
1111deriving instance NFData PTerm
1112!-}
1114mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
1115mapPT f t = f (mpt t) where
1116 mpt (PLam fc n nfc t s) = PLam fc n nfc (mapPT f t) (mapPT f s)
1117 mpt (PPi p n nfc t s) = PPi p n nfc (mapPT f t) (mapPT f s)
1118 mpt (PLet fc n nfc ty v s) = PLet fc n nfc (mapPT f ty) (mapPT f v) (mapPT f s)
1119 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s) (fmap (mapPT f) g)
1120 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
1121 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
1122 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
1123 mpt (PIfThenElse fc c t e) = PIfThenElse fc (mapPT f c) (mapPT f t) (mapPT f e)
1124 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
1125 mpt (PPair fc hls p l r) = PPair fc hls p (mapPT f l) (mapPT f r)
1126 mpt (PDPair fc hls p l t r) = PDPair fc hls p (mapPT f l) (mapPT f t) (mapPT f r)
1127 mpt (PAlternative ns a as) = PAlternative ns a (map (mapPT f) as)
1128 mpt (PHidden t) = PHidden (mapPT f t)
1129 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
1130 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
1131 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
1132 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
1133 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
1134 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
1135 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
1136 mpt x = x
1139data PTactic' t = Intro [Name] | Intros | Focus Name
1140 | Refine Name [Bool] | Rewrite t | DoUnify
1141 | Induction t
1142 | CaseTac t
1143 | Equiv t
1144 | Claim Name t
1145 | Unfocus
1146 | MatchRefine Name
1147 | LetTac Name t | LetTacTy Name t t
1148 | Exact t | Compute | Trivial | TCInstance
1149 | ProofSearch Bool Bool Int (Maybe Name)
1150 [Name] -- allowed local names
1151 [Name] -- hints
1152 -- ^ the bool is whether to search recursively
1153 | Solve
1154 | Attack
1155 | ProofState | ProofTerm | Undo
1156 | Try (PTactic' t) (PTactic' t)
1157 | TSeq (PTactic' t) (PTactic' t)
1158 | ApplyTactic t -- see Language.Reflection module
1159 | ByReflection t
1160 | Reflect t
1161 | Fill t
1162 | GoalType String (PTactic' t)
1163 | TCheck t
1164 | TEval t
1165 | TDocStr (Either Name Const)
1166 | TSearch t
1167 | Skip
1168 | TFail [ErrorReportPart]
1169 | Qed | Abandon
1170 | SourceFC
1171 deriving (Show, Eq, Functor, Foldable, Traversable, Data, Typeable)
1172{-!
1173deriving instance Binary PTactic'
1174deriving instance NFData PTactic'
1175!-}
1176instance Sized a => Sized (PTactic' a) where
1177 size (Intro nms) = 1 + size nms
1178 size Intros = 1
1179 size (Focus nm) = 1 + size nm
1180 size (Refine nm bs) = 1 + size nm + length bs
1181 size (Rewrite t) = 1 + size t
1182 size (Induction t) = 1 + size t
1183 size (LetTac nm t) = 1 + size nm + size t
1184 size (Exact t) = 1 + size t
1185 size Compute = 1
1186 size Trivial = 1
1187 size Solve = 1
1188 size Attack = 1
1189 size ProofState = 1
1190 size ProofTerm = 1
1191 size Undo = 1
1192 size (Try l r) = 1 + size l + size r
1193 size (TSeq l r) = 1 + size l + size r
1194 size (ApplyTactic t) = 1 + size t
1195 size (Reflect t) = 1 + size t
1196 size (Fill t) = 1 + size t
1197 size Qed = 1
1198 size Abandon = 1
1199 size Skip = 1
1200 size (TFail ts) = 1 + size ts
1201 size SourceFC = 1
1202 size DoUnify = 1
1203 size (CaseTac x) = 1 + size x
1204 size (Equiv t) = 1 + size t
1205 size (Claim x y) = 1 + size x + size y
1206 size Unfocus = 1
1207 size (MatchRefine x) = 1 + size x
1208 size (LetTacTy x y z) = 1 + size x + size y + size z
1209 size TCInstance = 1
1211type PTactic = PTactic' PTerm
1213data PDo' t = DoExp FC t
1214 | DoBind FC Name FC t -- ^ second FC is precise name location
1215 | DoBindP FC t t [(t,t)]
1216 | DoLet FC Name FC t t -- ^ second FC is precise name location
1217 | DoLetP FC t t
1218 deriving (Eq, Functor, Data, Typeable)
1219{-!
1220deriving instance Binary PDo'
1221deriving instance NFData PDo'
1222!-}
1224instance Sized a => Sized (PDo' a) where
1225 size (DoExp fc t) = 1 + size fc + size t
1226 size (DoBind fc nm nfc t) = 1 + size fc + size nm + size nfc + size t
1227 size (DoBindP fc l r alts) = 1 + size fc + size l + size r + size alts
1228 size (DoLet fc nm nfc l r) = 1 + size fc + size nm + size l + size r
1229 size (DoLetP fc l r) = 1 + size fc + size l + size r
1231type PDo = PDo' PTerm
1233-- The priority gives a hint as to elaboration order. Best to elaborate
1234-- things early which will help give a more concrete type to other
1235-- variables, e.g. a before (interpTy a).
1237data PArg' t = PImp { priority :: Int,
1238 machine_inf :: Bool, -- true if the machine inferred it
1239 argopts :: [ArgOpt],
1240 pname :: Name,
1241 getTm :: t }
1242 | PExp { priority :: Int,
1243 argopts :: [ArgOpt],
1244 pname :: Name,
1245 getTm :: t }
1246 | PConstraint { priority :: Int,
1247 argopts :: [ArgOpt],
1248 pname :: Name,
1249 getTm :: t }
1250 | PTacImplicit { priority :: Int,
1251 argopts :: [ArgOpt],
1252 pname :: Name,
1253 getScript :: t,
1254 getTm :: t }
1255 deriving (Show, Eq, Functor, Data, Typeable)
1257data ArgOpt = AlwaysShow | HideDisplay | InaccessibleArg | UnknownImp
1258 deriving (Show, Eq, Data, Typeable)
1260instance Sized a => Sized (PArg' a) where
1261 size (PImp p _ l nm trm) = 1 + size nm + size trm
1262 size (PExp p l nm trm) = 1 + size nm + size trm
1263 size (PConstraint p l nm trm) = 1 + size nm +size nm + size trm
1264 size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
1266{-!
1267deriving instance Binary PArg'
1268deriving instance NFData PArg'
1269!-}
1271pimp n t mach = PImp 1 mach [] n t
1272pexp t = PExp 1 [] (sMN 0 "arg") t
1273pconst t = PConstraint 1 [] (sMN 0 "carg") t
1274ptacimp n s t = PTacImplicit 2 [] n s t
1276type PArg = PArg' PTerm
1278-- | Get the highest FC in a term, if one exists
1279highestFC :: PTerm -> Maybe FC
1280highestFC (PQuote _) = Nothing
1281highestFC (PRef fc _ _) = Just fc
1282highestFC (PInferRef fc _ _) = Just fc
1283highestFC (PPatvar fc _) = Just fc
1284highestFC (PLam fc _ _ _ _) = Just fc
1285highestFC (PPi _ _ _ _ _) = Nothing
1286highestFC (PLet fc _ _ _ _ _) = Just fc
1287highestFC (PTyped tm ty) = highestFC tm <|> highestFC ty
1288highestFC (PApp fc _ _) = Just fc
1289highestFC (PAppBind fc _ _) = Just fc
1290highestFC (PMatchApp fc _) = Just fc
1291highestFC (PCase fc _ _) = Just fc
1292highestFC (PIfThenElse fc _ _ _) = Just fc
1293highestFC (PTrue fc _) = Just fc
1294highestFC (PResolveTC fc) = Just fc
1295highestFC (PRewrite fc _ _ _) = Just fc
1296highestFC (PPair fc _ _ _ _) = Just fc
1297highestFC (PDPair fc _ _ _ _ _) = Just fc
1298highestFC (PAs fc _ _) = Just fc
1299highestFC (PAlternative _ _ args) =
1300 case mapMaybe highestFC args of
1301 [] -> Nothing
1302 (fc:_) -> Just fc
1303highestFC (PHidden _) = Nothing
1304highestFC (PType fc) = Just fc
1305highestFC (PUniverse _) = Nothing
1306highestFC (PGoal fc _ _ _) = Just fc
1307highestFC (PConstant fc _) = Just fc
1308highestFC Placeholder = Nothing
1309highestFC (PDoBlock lines) =
1310 case map getDoFC lines of
1311 [] -> Nothing
1312 (fc:_) -> Just fc
1313 where
1314 getDoFC (DoExp fc t) = fc
1315 getDoFC (DoBind fc nm nfc t) = fc
1316 getDoFC (DoBindP fc l r alts) = fc
1317 getDoFC (DoLet fc nm nfc l r) = fc
1318 getDoFC (DoLetP fc l r) = fc
1320highestFC (PIdiom fc _) = Just fc
1321highestFC (PReturn fc) = Just fc
1322highestFC (PMetavar fc _) = Just fc
1323highestFC (PProof _) = Nothing
1324highestFC (PTactics _) = Nothing
1325highestFC (PElabError _) = Nothing
1326highestFC PImpossible = Nothing
1327highestFC (PCoerced tm) = highestFC tm
1328highestFC (PDisamb _ opts) = highestFC opts
1329highestFC (PUnifyLog tm) = highestFC tm
1330highestFC (PNoImplicits tm) = highestFC tm
1331highestFC (PQuasiquote _ _) = Nothing
1332highestFC (PUnquote tm) = highestFC tm
1333highestFC (PQuoteName _ _ fc) = Just fc
1334highestFC (PRunElab fc _ _) = Just fc
1335highestFC (PConstSugar fc _) = Just fc
1336highestFC (PAppImpl t _) = highestFC t
1338-- Type class data
1340data ClassInfo = CI { instanceCtorName :: Name,
1341 class_methods :: [(Name, (FnOpts, PTerm))],
1342 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
1343 class_default_superclasses :: [PDecl],
1344 class_params :: [Name],
1345 class_instances :: [(Name, Bool)], -- the Bool is whether to include in instance search, so named instances are excluded
1346 class_determiners :: [Int] }
1347 deriving Show
1348{-!
1349deriving instance Binary ClassInfo
1350deriving instance NFData ClassInfo
1351!-}
1353-- Record data
1354data RecordInfo = RI { record_parameters :: [(Name,PTerm)],
1355 record_constructor :: Name,
1356 record_projections :: [Name] }
1357 deriving Show
1359-- Type inference data
1361data TIData = TIPartial -- ^ a function with a partially defined type
1362 | TISolution [Term] -- ^ possible solutions to a metavariable in a type
1363 deriving Show
1365-- | Miscellaneous information about functions
1366data FnInfo = FnInfo { fn_params :: [Int] }
1367 deriving Show
1368{-!
1369deriving instance Binary FnInfo
1370deriving instance NFData FnInfo
1371!-}
1373data OptInfo = Optimise { inaccessible :: [(Int,Name)], -- includes names for error reporting
1374 detaggable :: Bool }
1375 deriving Show
1376{-!
1377deriving instance Binary OptInfo
1378deriving instance NFData OptInfo
1379!-}
1381-- | Syntactic sugar info
1382data DSL' t = DSL { dsl_bind :: t,
1383 dsl_return :: t,
1384 dsl_apply :: t,
1385 dsl_pure :: t,
1386 dsl_var :: Maybe t,
1387 index_first :: Maybe t,
1388 index_next :: Maybe t,
1389 dsl_lambda :: Maybe t,
1390 dsl_let :: Maybe t,
1391 dsl_pi :: Maybe t
1393 deriving (Show, Functor)
1394{-!
1395deriving instance Binary DSL'
1396deriving instance NFData DSL'
1397!-}
1399type DSL = DSL' PTerm
1401data SynContext = PatternSyntax | TermSyntax | AnySyntax
1402 deriving Show
1403{-!
1404deriving instance Binary SynContext
1405deriving instance NFData SynContext
1406!-}
1408data Syntax = Rule [SSymbol] PTerm SynContext
1409 | DeclRule [SSymbol] [PDecl]
1410 deriving Show
1412syntaxNames :: Syntax -> [Name]
1413syntaxNames (Rule syms _ _) = mapMaybe ename syms
1414 where ename (Keyword n) = Just n
1415 ename _ = Nothing
1416syntaxNames (DeclRule syms _) = mapMaybe ename syms
1417 where ename (Keyword n) = Just n
1418 ename _ = Nothing
1420syntaxSymbols :: Syntax -> [SSymbol]
1421syntaxSymbols (Rule ss _ _) = ss
1422syntaxSymbols (DeclRule ss _) = ss
1423{-!
1424deriving instance Binary Syntax
1425deriving instance NFData Syntax
1426!-}
1428data SSymbol = Keyword Name
1429 | Symbol String
1430 | Binding Name
1431 | Expr Name
1432 | SimpleExpr Name
1433 deriving (Show, Eq)
1436{-!
1437deriving instance Binary SSymbol
1438deriving instance NFData SSymbol
1439!-}
1441newtype SyntaxRules = SyntaxRules { syntaxRulesList :: [Syntax] }
1443emptySyntaxRules :: SyntaxRules
1444emptySyntaxRules = SyntaxRules []
1446updateSyntaxRules :: [Syntax] -> SyntaxRules -> SyntaxRules
1447updateSyntaxRules rules (SyntaxRules sr) = SyntaxRules newRules
1448 where
1449 newRules = sortBy (ruleSort `on` syntaxSymbols) (rules ++ sr)
1451 ruleSort [] [] = EQ
1452 ruleSort [] _ = LT
1453 ruleSort _ [] = GT
1454 ruleSort (s1:ss1) (s2:ss2) =
1455 case symCompare s1 s2 of
1456 EQ -> ruleSort ss1 ss2
1457 r -> r
1459 -- Better than creating Ord instance for SSymbol since
1460 -- in general this ordering does not really make sense.
1461 symCompare (Keyword n1) (Keyword n2) = compare n1 n2
1462 symCompare (Keyword _) _ = LT
1463 symCompare (Symbol _) (Keyword _) = GT
1464 symCompare (Symbol s1) (Symbol s2) = compare s1 s2
1465 symCompare (Symbol _) _ = LT
1466 symCompare (Binding _) (Keyword _) = GT
1467 symCompare (Binding _) (Symbol _) = GT
1468 symCompare (Binding b1) (Binding b2) = compare b1 b2
1469 symCompare (Binding _) _ = LT
1470 symCompare (Expr _) (Keyword _) = GT
1471 symCompare (Expr _) (Symbol _) = GT
1472 symCompare (Expr _) (Binding _) = GT
1473 symCompare (Expr e1) (Expr e2) = compare e1 e2
1474 symCompare (Expr _) _ = LT
1475 symCompare (SimpleExpr _) (Keyword _) = GT
1476 symCompare (SimpleExpr _) (Symbol _) = GT
1477 symCompare (SimpleExpr _) (Binding _) = GT
1478 symCompare (SimpleExpr _) (Expr _) = GT
1479 symCompare (SimpleExpr e1) (SimpleExpr e2) = compare e1 e2
1481initDSL = DSL (PRef f [] (sUN ">>="))
1482 (PRef f [] (sUN "return"))
1483 (PRef f [] (sUN "<*>"))
1484 (PRef f [] (sUN "pure"))
1485 Nothing
1486 Nothing
1487 Nothing
1488 Nothing
1489 Nothing
1490 Nothing
1491 where f = fileFC "(builtin)"
1493data Using = UImplicit Name PTerm
1494 | UConstraint Name [Name]
1495 deriving (Show, Eq, Data, Typeable)
1496{-!
1497deriving instance Binary Using
1498deriving instance NFData Using
1499!-}
1501data SyntaxInfo = Syn { using :: [Using],
1502 syn_params :: [(Name, PTerm)],
1503 syn_namespace :: [String],
1504 no_imp :: [Name],
1505 imp_methods :: [Name], -- class methods. When expanding
1506 -- implicits, these should be expanded even under
1507 -- binders
1508 decoration :: Name -> Name,
1509 inPattern :: Bool,
1510 implicitAllowed :: Bool,
1511 maxline :: Maybe Int,
1512 mut_nesting :: Int,
1513 dsl_info :: DSL,
1514 syn_in_quasiquote :: Int }
1515 deriving Show
1516{-!
1517deriving instance NFData SyntaxInfo
1518deriving instance Binary SyntaxInfo
1519!-}
1521defaultSyntax = Syn [] [] [] [] [] id False False Nothing 0 initDSL 0
1523expandNS :: SyntaxInfo -> Name -> Name
1524expandNS syn n@(NS _ _) = n
1525expandNS syn n = case syn_namespace syn of
1526 [] -> n
1527 xs -> sNS n xs
1530-- For inferring types of things
1532bi = fileFC "builtin"
1534inferTy = sMN 0 "__Infer"
1535inferCon = sMN 0 "__infer"
1536inferDecl = PDatadecl inferTy NoFC
1537 (PType bi)
1538 [(emptyDocstring, [], inferCon, NoFC, PPi impl (sMN 0 "iType") NoFC (PType bi) (
1539 PPi expl (sMN 0 "ival") NoFC (PRef bi [] (sMN 0 "iType"))
1540 (PRef bi [] inferTy)), bi, [])]
1541inferOpts = []
1543infTerm t = PApp bi (PRef bi [] inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
1544infP = P (TCon 6 0) inferTy (TType (UVal 0))
1546getInferTerm, getInferType :: Term -> Term
1547getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
1548getInferTerm (App _ (App _ _ _) tm) = tm
1549getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
1551getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
1552 where toTy (Lam t) = Pi Nothing t (TType (UVar 0))
1553 toTy (PVar t) = PVTy t
1554 toTy b = b
1555getInferType (App _ (App _ _ ty) _) = ty
1559-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign
1561primNames = [inferTy, inferCon]
1563unitTy = sUN "Unit"
1564unitCon = sUN "MkUnit"
1566falseDoc = fmap (const $ Msg "") . parseDocstring . T.pack $
1567 "The empty type, also known as the trivially false proposition." ++
1568 "\n\n" ++
1569 "Use `void` or `absurd` to prove anything if you have a variable " ++
1570 "of type `Void` in scope."
1571falseTy = sUN "Void"
1573pairTy = sNS (sUN "Pair") ["Builtins"]
1574pairCon = sNS (sUN "MkPair") ["Builtins"]
1576upairTy = sNS (sUN "UPair") ["Builtins"]
1577upairCon = sNS (sUN "MkUPair") ["Builtins"]
1579eqTy = sUN "="
1580eqCon = sUN "Refl"
1581eqDoc = fmap (const (Left $ Msg "")) . parseDocstring . T.pack $
1582 "The propositional equality type. A proof that `x` = `y`." ++
1583 "\n\n" ++
1584 "To use such a proof, pattern-match on it, and the two equal things will " ++
1585 "then need to be the _same_ pattern." ++
1586 "\n\n" ++
1587 "**Note**: Idris's equality type is potentially _heterogeneous_, which means that it " ++
1588 "is possible to state equalities between values of potentially different " ++
1589 "types. However, Idris will attempt the homogeneous case unless it fails to typecheck." ++
1590 "\n\n" ++
1591 "You may need to use `(~=~)` to explicitly request heterogeneous equality."
1593eqDecl = PDatadecl eqTy NoFC (piBindp impl [(n "A", PType bi), (n "B", PType bi)]
1594 (piBind [(n "x", PRef bi [] (n "A")), (n "y", PRef bi [] (n "B"))]
1595 (PType bi)))
1596 [(reflDoc, reflParamDoc,
1597 eqCon, NoFC, PPi impl (n "A") NoFC (PType bi) (
1598 PPi impl (n "x") NoFC (PRef bi [] (n "A"))
1599 (PApp bi (PRef bi [] eqTy) [pimp (n "A") Placeholder False,
1600 pimp (n "B") Placeholder False,
1601 pexp (PRef bi [] (n "x")),
1602 pexp (PRef bi [] (n "x"))])), bi, [])]
1603 where n a = sUN a
1604 reflDoc = annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $
1605 "A proof that `x` in fact equals `x`. To construct this, you must have already " ++
1606 "shown that both sides are in fact equal."
1607 reflParamDoc = [(n "A", annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $ "the type at which the equality is proven"),
1608 (n "x", annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $ "the element shown to be equal to itself.")]
1610eqParamDoc = [(n "A", annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $ "the type of the left side of the equality"),
1611 (n "B", annotCode (const (Left $ Msg "")) . parseDocstring . T.pack $ "the type of the right side of the equality")
1613 where n a = sUN a
1615eqOpts = []
1617-- | The special name to be used in the module documentation context -
1618-- not for use in the main definition context. The namespace around it
1619-- will determine the module to which the docs adhere.
1620modDocName :: Name
1621modDocName = sMN 0 "ModuleDocs"
1623-- Defined in builtins.idr
1624sigmaTy = sNS (sUN "DPair") ["Builtins"]
1625sigmaCon = sNS (sUN "MkDPair") ["Builtins"]
1627piBind :: [(Name, PTerm)] -> PTerm -> PTerm
1628piBind = piBindp expl
1630piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
1631piBindp p [] t = t
1632piBindp p ((n, ty):ns) t = PPi p n NoFC ty (piBindp p ns t)
1635-- Pretty-printing declarations and terms
1637-- These "show" instances render to an absurdly wide screen because inserted line breaks
1638-- could interfere with interactive editing, which calls "show".
1640instance Show PTerm where
1641 showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp defaultPPOption) tm
1643instance Show PDecl where
1644 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp verbosePPOption) d
1646instance Show PClause where
1647 showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp verbosePPOption) c
1649instance Show PData where
1650 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp defaultPPOption) d
1652instance Pretty PTerm OutputAnnotation where
1653 pretty = prettyImp defaultPPOption
1655-- | Colourise annotations according to an Idris state. It ignores the names
1656-- in the annotation, as there's no good way to show extended information on a
1657-- terminal.
1658annotationColour :: IState -> OutputAnnotation -> Maybe IdrisColour
1659annotationColour ist _ | not (idris_colourRepl ist) = Nothing
1660annotationColour ist (AnnConst c) =
1661 let theme = idris_colourTheme ist
1662 in Just $ if constIsType c
1663 then typeColour theme
1664 else dataColour theme
1665annotationColour ist (AnnData _ _) = Just $ dataColour (idris_colourTheme ist)
1666annotationColour ist (AnnType _ _) = Just $ typeColour (idris_colourTheme ist)
1667annotationColour ist (AnnBoundName _ True) = Just $ implicitColour (idris_colourTheme ist)
1668annotationColour ist (AnnBoundName _ False) = Just $ boundVarColour (idris_colourTheme ist)
1669annotationColour ist AnnKeyword = Just $ keywordColour (idris_colourTheme ist)
1670annotationColour ist (AnnName n _ _ _) =
1671 let ctxt = tt_ctxt ist
1672 theme = idris_colourTheme ist
1673 in case () of
1674 _ | isDConName n ctxt -> Just $ dataColour theme
1675 _ | isFnName n ctxt -> Just $ functionColour theme
1676 _ | isTConName n ctxt -> Just $ typeColour theme
1677 _ | isPostulateName n ist -> Just $ postulateColour theme
1678 _ | otherwise -> Nothing -- don't colourise unknown names
1679annotationColour ist (AnnTextFmt fmt) = Just (colour fmt)
1680 where colour BoldText = IdrisColour Nothing True False True False
1681 colour UnderlineText = IdrisColour Nothing True True False False
1682 colour ItalicText = IdrisColour Nothing True False False True
1683annotationColour ist _ = Nothing
1686-- | Colourise annotations according to an Idris state. It ignores the names
1687-- in the annotation, as there's no good way to show extended
1688-- information on a terminal. Note that strings produced this way will
1689-- not be coloured on Windows, so the use of the colour rendering
1690-- functions in Idris.Output is to be preferred.
1691consoleDecorate :: IState -> OutputAnnotation -> String -> String
1692consoleDecorate ist ann = maybe id colourise (annotationColour ist ann)
1694isPostulateName :: Name -> IState -> Bool
1695isPostulateName n ist = S.member n (idris_postulates ist)
1697-- | Pretty-print a high-level closed Idris term with no information about precedence/associativity
1698prettyImp :: PPOption -- ^^ pretty printing options
1699 -> PTerm -- ^^ the term to pretty-print
1700 -> Doc OutputAnnotation
1701prettyImp impl = pprintPTerm impl [] [] []
1703-- | Serialise something to base64 using its Binary instance.
1705-- | Do the right thing for rendering a term in an IState
1706prettyIst :: IState -> PTerm -> Doc OutputAnnotation
1707prettyIst ist = pprintPTerm (ppOptionIst ist) [] [] (idris_infixes ist)
1709-- | Pretty-print a high-level Idris term in some bindings context with infix info
1710pprintPTerm :: PPOption -- ^^ pretty printing options
1711 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1712 -> [Name] -- ^^ names to always show in pi, even if not used
1713 -> [FixDecl] -- ^^ Fixity declarations
1714 -> PTerm -- ^^ the term to pretty-print
1715 -> Doc OutputAnnotation
1716pprintPTerm ppo bnd docArgs infixes = prettySe (ppopt_depth ppo) startPrec bnd
1717 where
1718 startPrec = 0
1719 funcAppPrec = 10
1721 prettySe :: Maybe Int -> Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1722 prettySe d p bnd (PQuote r) =
1723 text "![" <> pretty r <> text "]"
1724 prettySe d p bnd (PPatvar fc n) = pretty n
1725 prettySe d p bnd e
1726 | Just str <- slist d p bnd e = depth d $ str
1727 | Just n <- snat ppo d p e = depth d $ annotate (AnnData "Nat" "") (text (show n))
1728 prettySe d p bnd (PRef fc _ n) = prettyName True (ppopt_impl ppo) bnd n
1729 prettySe d p bnd (PLam fc n nfc ty sc) =
1730 depth d . bracket p startPrec . group . align . hang 2 $
1731 text "\\" <> prettyBindingOf n False <+> text "=>" <$>
1732 prettySe (decD d) startPrec ((n, False):bnd) sc
1733 prettySe d p bnd (PLet fc n nfc ty v sc) =
1734 depth d . bracket p startPrec . group . align $
1735 kwd "let" <+> (group . align . hang 2 $ prettyBindingOf n False <+> text "=" <$> prettySe (decD d) startPrec bnd v) </>
1736 kwd "in" <+> (group . align . hang 2 $ prettySe (decD d) startPrec ((n, False):bnd) sc)
1737 prettySe d p bnd (PPi (Exp l s _) n _ ty sc)
1738 | n `elem` allNamesIn sc || ppopt_impl ppo && uname n || n `elem` docArgs
1739 || ppopt_pinames ppo && uname n =
1740 depth d . bracket p startPrec . group $
1741 enclose lparen rparen (group . align $ prettyBindingOf n False <+> colon <+> prettySe (decD d) startPrec bnd ty) <+>
1742 st <> text "->" <$> prettySe (decD d) startPrec ((n, False):bnd) sc
1743 | otherwise =
1744 depth d . bracket p startPrec . group $
1745 group (prettySe (decD d) (startPrec + 1) bnd ty <+> st) <> text "->" <$>
1746 group (prettySe (decD d) startPrec ((n, False):bnd) sc)
1747 where
1748 uname (UN n) = case str n of
1749 ('_':_) -> False
1750 _ -> True
1751 uname _ = False
1753 st =
1754 case s of
1755 Static -> text "%static" <> space
1756 _ -> empty
1757 prettySe d p bnd (PPi (Imp l s _ fa) n _ ty sc)
1758 | ppopt_impl ppo =
1759 depth d . bracket p startPrec $
1760 lbrace <> prettyBindingOf n True <+> colon <+> prettySe (decD d) startPrec bnd ty <> rbrace <+>
1761 st <> text "->" </> prettySe (decD d) startPrec ((n, True):bnd) sc
1762 | otherwise = depth d $ prettySe (decD d) startPrec ((n, True):bnd) sc
1763 where
1764 st =
1765 case s of
1766 Static -> text "%static" <> space
1767 _ -> empty
1768 prettySe d p bnd (PPi (Constraint _ _) n _ ty sc) =
1769 depth d . bracket p startPrec $
1770 prettySe (decD d) (startPrec + 1) bnd ty <+> text "=>" </> prettySe (decD d) startPrec ((n, True):bnd) sc
1771 prettySe d p bnd (PPi (TacImp _ _ (PTactics [ProofSearch{}])) n _ ty sc) =
1772 lbrace <> kwd "auto" <+> pretty n <+> colon <+> prettySe (decD d) startPrec bnd ty <>
1773 rbrace <+> text "->" </> prettySe (decD d) startPrec ((n, True):bnd) sc
1774 prettySe d p bnd (PPi (TacImp _ _ s) n _ ty sc) =
1775 depth d . bracket p startPrec $
1776 lbrace <> kwd "default" <+> prettySe (decD d) (funcAppPrec + 1) bnd s <+> pretty n <+> colon <+> prettySe (decD d) startPrec bnd ty <>
1777 rbrace <+> text "->" </> prettySe (decD d) startPrec ((n, True):bnd) sc
1778 -- This case preserves the behavior of the former constructor PEq.
1779 -- It should be removed if feasible when the pretty-printing of infix
1780 -- operators in general is improved.
1781 prettySe d p bnd (PApp _ (PRef _ _ n) [lt, rt, l, r])
1782 | n == eqTy, ppopt_impl ppo =
1783 depth d . bracket p eqPrec $
1784 enclose lparen rparen eq <+>
1785 align (group (vsep (map (prettyArgS (decD d) bnd)
1786 [lt, rt, l, r])))
1787 | n == eqTy =
1788 depth d . bracket p eqPrec . align . group $
1789 prettyTerm (getTm l) <+> eq <$> group (prettyTerm (getTm r))
1790 where eq = annName eqTy (text "=")
1791 eqPrec = startPrec
1792 prettyTerm = prettySe (decD d) (eqPrec + 1) bnd
1793 prettySe d p bnd (PApp _ (PRef _ _ f) args) -- normal names, no explicit args
1794 | UN nm <- basename f
1795 , not (ppopt_impl ppo) && null (getShowArgs args) =
1796 prettyName True (ppopt_impl ppo) bnd f
1797 prettySe d p bnd (PAppBind _ (PRef _ _ f) [])
1798 | not (ppopt_impl ppo) = text "!" <> prettyName True (ppopt_impl ppo) bnd f
1799 prettySe d p bnd (PApp _ (PRef _ _ op) args) -- infix operators
1800 | UN nm <- basename op
1801 , not (tnull nm) &&
1802 (not (ppopt_impl ppo)) && (not $ isAlpha (thead nm)) =
1803 case getShowArgs args of
1804 [] -> opName True
1805 [x] -> group (opName True <$> group (prettySe (decD d) startPrec bnd (getTm x)))
1806 [l,r] -> let precedence = maybe (startPrec - 1) prec f
1807 in depth d . bracket p precedence $ inFix (getTm l) (getTm r)
1808 (l@(PExp{}) : r@(PExp{}) : rest) ->
1809 depth d . bracket p funcAppPrec $
1810 enclose lparen rparen (inFix (getTm l) (getTm r)) <+>
1811 align (group (vsep (map (prettyArgS d bnd) rest)))
1812 as -> opName True <+> align (vsep (map (prettyArgS d bnd) as))
1813 where opName isPrefix = prettyName isPrefix (ppopt_impl ppo) bnd op
1814 f = getFixity (opStr op)
1815 left = case f of
1816 Nothing -> funcAppPrec + 1
1817 Just (Infixl p') -> p'
1818 Just f' -> prec f' + 1
1819 right = case f of
1820 Nothing -> funcAppPrec + 1
1821 Just (Infixr p') -> p'
1822 Just f' -> prec f' + 1
1823 inFix l r = align . group $
1824 (prettySe (decD d) left bnd l <+> opName False) <$>
1825 group (prettySe (decD d) right bnd r)
1826 prettySe d p bnd (PApp _ hd@(PRef fc _ f) [tm]) -- symbols, like 'foo
1827 | PConstant NoFC (Idris.Core.TT.Str str) <- getTm tm,
1828 f == sUN "Symbol_" = annotate (AnnType ("'" ++ str) ("The symbol " ++ str)) $
1829 char '\'' <> prettySe (decD d) startPrec bnd (PRef fc [] (sUN str))
1830 prettySe d p bnd (PApp _ f as) = -- Normal prefix applications
1831 let args = getShowArgs as
1832 fp = prettySe (decD d) (startPrec + 1) bnd f
1833 shownArgs = if ppopt_impl ppo then as else args
1834 in
1835 depth d . bracket p funcAppPrec . group $
1836 if null shownArgs
1837 then fp
1838 else fp <+> align (vsep (map (prettyArgS d bnd) shownArgs))
1839 prettySe d p bnd (PCase _ scr cases) =
1840 align $ kwd "case" <+> prettySe (decD d) startPrec bnd scr <+> kwd "of" <$>
1841 depth d (indent 2 (vsep (map ppcase cases)))
1842 where
1843 ppcase (l, r) = let prettyCase = prettySe (decD d) startPrec
1844 ([(n, False) | n <- vars l] ++ bnd)
1845 in nest nestingSize $
1846 prettyCase l <+> text "=>" <+> prettyCase r
1847 -- Warning: this is a bit of a hack. At this stage, we don't have the
1848 -- global context, so we can't determine which names are constructors,
1849 -- which are types, and which are pattern variables on the LHS of the
1850 -- case pattern. We use the heuristic that names without a namespace
1851 -- are patvars, because right now case blocks in PTerms are always
1852 -- delaborated from TT before being sent to the pretty-printer. If they
1853 -- start getting printed directly, THIS WILL BREAK.
1854 -- Potential solution: add a list of known patvars to the cases in
1855 -- PCase, and have the delaborator fill it out, kind of like the pun
1856 -- disambiguation on PDPair.
1857 vars tm = filter noNS (allNamesIn tm)
1858 noNS (NS _ _) = False
1859 noNS _ = True
1861 prettySe d p bnd (PIfThenElse _ c t f) =
1862 depth d . bracket p funcAppPrec . group . align . hang 2 . vsep $
1863 [ kwd "if" <+> prettySe (decD d) startPrec bnd c
1864 , kwd "then" <+> prettySe (decD d) startPrec bnd t
1865 , kwd "else" <+> prettySe (decD d) startPrec bnd f
1867 prettySe d p bnd (PHidden tm) = text "." <> prettySe (decD d) funcAppPrec bnd tm
1868 prettySe d p bnd (PResolveTC _) = kwd "%instance"
1869 prettySe d p bnd (PTrue _ IsType) = annName unitTy $ text "()"
1870 prettySe d p bnd (PTrue _ IsTerm) = annName unitCon $ text "()"
1871 prettySe d p bnd (PTrue _ TypeOrTerm) = text "()"
1872 prettySe d p bnd (PRewrite _ l r _) =
1873 depth d . bracket p startPrec $
1874 text "rewrite" <+> prettySe (decD d) (startPrec + 1) bnd l <+> text "in" <+> prettySe (decD d) startPrec bnd r
1875 prettySe d p bnd (PTyped l r) =
1876 lparen <> prettySe (decD d) startPrec bnd l <+> colon <+> prettySe (decD d) startPrec bnd r <> rparen
1877 prettySe d p bnd pair@(PPair _ _ pun _ _) -- flatten tuples to the right, like parser
1878 | Just elts <- pairElts pair = depth d . enclose (ann lparen) (ann rparen) .
1879 align . group . vsep . punctuate (ann comma) $
1880 map (prettySe (decD d) startPrec bnd) elts
1881 where ann = case pun of
1882 TypeOrTerm -> id
1883 IsType -> annName pairTy
1884 IsTerm -> annName pairCon
1885 prettySe d p bnd (PDPair _ _ pun l t r) =
1886 depth d $
1887 annotated lparen <>
1888 left <+>
1889 annotated (text "**") <+>
1890 prettySe (decD d) startPrec (addBinding bnd) r <>
1891 annotated rparen
1892 where annotated = case pun of
1893 IsType -> annName sigmaTy
1894 IsTerm -> annName sigmaCon
1895 TypeOrTerm -> id
1896 (left, addBinding) = case (l, pun) of
1897 (PRef _ _ n, IsType) -> (bindingOf n False <+> text ":" <+> prettySe (decD d) startPrec bnd t, ((n, False) :))
1898 _ -> (prettySe (decD d) startPrec bnd l, id)
1899 prettySe d p bnd (PAlternative ns a as) =
1900 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1901 where
1902 prettyAs =
1903 foldr ((\ l r -> l <+> text "," <+> r) .
1904 depth d . prettySe (decD d) startPrec bnd)
1905 empty as
1906 prettySe d p bnd (PType _) = annotate (AnnType "Type" "The type of types") $ text "Type"
1907 prettySe d p bnd (PUniverse u) = annotate (AnnType (show u) "The type of unique types") $ text (show u)
1908 prettySe d p bnd (PConstant _ c) = annotate (AnnConst c) (text (show c))
1909 -- XXX: add pretty for tactics
1910 prettySe d p bnd (PProof ts) =
1911 kwd "proof" <+> lbrace <> ellipsis <> rbrace
1912 prettySe d p bnd (PTactics ts) =
1913 kwd "tactics" <+> lbrace <> ellipsis <> rbrace
1914 prettySe d p bnd (PMetavar _ n) = annotate (AnnName n (Just MetavarOutput) Nothing Nothing) $ text "?" <> pretty n
1915 prettySe d p bnd (PReturn f) = kwd "return"
1916 prettySe d p bnd PImpossible = kwd "impossible"
1917 prettySe d p bnd Placeholder = text "_"
1918 prettySe d p bnd (PDoBlock dos) =
1919 bracket p startPrec $
1920 kwd "do" <+> align (vsep (map (group . align . hang 2) (ppdo bnd dos)))
1921 where ppdo bnd (DoExp _ tm:dos) = prettySe (decD d) startPrec bnd tm : ppdo bnd dos
1922 ppdo bnd (DoBind _ bn _ tm : dos) =
1923 (prettyBindingOf bn False <+> text "<-" <+>
1924 group (align (hang 2 (prettySe (decD d) startPrec bnd tm)))) :
1925 ppdo ((bn, False):bnd) dos
1926 ppdo bnd (DoBindP _ _ _ _ : dos) = -- ok because never made by delab
1927 text "no pretty printer for pattern-matching do binding" :
1928 ppdo bnd dos
1929 ppdo bnd (DoLet _ ln _ ty v : dos) =
1930 (kwd "let" <+> prettyBindingOf ln False <+>
1931 (if ty /= Placeholder
1932 then colon <+> prettySe (decD d) startPrec bnd ty <+> text "="
1933 else text "=") <+>
1934 group (align (hang 2 (prettySe (decD d) startPrec bnd v)))) :
1935 ppdo ((ln, False):bnd) dos
1936 ppdo bnd (DoLetP _ _ _ : dos) = -- ok because never made by delab
1937 text "no pretty printer for pattern-matching do binding" :
1938 ppdo bnd dos
1939 ppdo _ [] = []
1940 prettySe d p bnd (PCoerced t) = prettySe d p bnd t
1941 prettySe d p bnd (PElabError s) = pretty s
1942 -- Quasiquote pprinting ignores bound vars
1943 prettySe d p bnd (PQuasiquote t Nothing) = text "`(" <> prettySe (decD d) p [] t <> text ")"
1944 prettySe d p bnd (PQuasiquote t (Just g)) = text "`(" <> prettySe (decD d) p [] t <+> colon <+> prettySe (decD d) p [] g <> text ")"
1945 prettySe d p bnd (PUnquote t) = text "~" <> prettySe (decD d) p bnd t
1946 prettySe d p bnd (PQuoteName n res _) = text start <> prettyName True (ppopt_impl ppo) bnd n <> text end
1947 where start = if res then "`{" else "`{{"
1948 end = if res then "}" else "}}"
1949 prettySe d p bnd (PRunElab _ tm _) =
1950 bracket p funcAppPrec . group . align . hang 2 $
1951 text "%runElab" <$>
1952 prettySe (decD d) funcAppPrec bnd tm
1953 prettySe d p bnd (PConstSugar fc tm) = prettySe d p bnd tm -- should never occur, but harmless
1954 prettySe d p bnd _ = text "missing pretty-printer for term"
1956 prettyBindingOf :: Name -> Bool -> Doc OutputAnnotation
1957 prettyBindingOf n imp = annotate (AnnBoundName n imp) (text (display n))
1958 where display (UN n) = T.unpack n
1959 display (MN _ n) = T.unpack n
1960 -- If a namespace is specified on a binding form, we'd better show it regardless of the implicits settings
1961 display (NS n ns) = (intercalate "." . map T.unpack . reverse) ns ++ "." ++ display n
1962 display n = show n
1964 prettyArgS d bnd (PImp _ _ _ n tm) = prettyArgSi d bnd (n, tm)
1965 prettyArgS d bnd (PExp _ _ _ tm) = prettyArgSe d bnd tm
1966 prettyArgS d bnd (PConstraint _ _ _ tm) = prettyArgSc d bnd tm
1967 prettyArgS d bnd (PTacImplicit _ _ n _ tm) = prettyArgSti d bnd (n, tm)
1969 prettyArgSe d bnd arg = prettySe d (funcAppPrec + 1) bnd arg
1970 prettyArgSi d bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe (decD d) startPrec bnd val <> rbrace
1971 prettyArgSc d bnd val = lbrace <> lbrace <> prettySe (decD d) startPrec bnd val <> rbrace <> rbrace
1972 prettyArgSti d bnd (n, val) = lbrace <> kwd "auto" <+> pretty n <+> text "=" <+> prettySe (decD d) startPrec bnd val <> rbrace
1974 annName :: Name -> Doc OutputAnnotation -> Doc OutputAnnotation
1975 annName n = annotate (AnnName n Nothing Nothing Nothing)
1977 opStr :: Name -> String
1978 opStr (NS n _) = opStr n
1979 opStr (UN n) = T.unpack n
1981 slist' :: Maybe Int -> Int -> [(Name, Bool)] -> PTerm -> Maybe [Doc OutputAnnotation]
1982 slist' (Just d) _ _ _ | d <= 0 = Nothing
1983 slist' d _ _ e
1984 | containsHole e = Nothing
1985 slist' d p bnd (PApp _ (PRef _ _ nil) _)
1986 | not (ppopt_impl ppo) && nsroot nil == sUN "Nil" = Just []
1987 slist' d p bnd (PRef _ _ nil)
1988 | not (ppopt_impl ppo) && nsroot nil == sUN "Nil" = Just []
1989 slist' d p bnd (PApp _ (PRef _ _ cons) args)
1990 | nsroot cons == sUN "::",
1991 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1992 all isImp imps,
1993 Just tl' <- slist' (decD d) p bnd tl
1994 = Just (prettySe d startPrec bnd hd : tl')
1995 where
1996 isImp (PImp {}) = True
1997 isImp _ = False
1998 slist' _ _ _ tm = Nothing
2000 slist d p bnd e | Just es <- slist' d p bnd e = Just $
2001 case es of
2002 [] -> annotate (AnnData "" "") $ text "[]"
2003 [x] -> enclose left right . group $ x
2004 xs -> enclose left right .
2005 align . group . vsep .
2006 punctuate comma $ xs
2007 where left = (annotate (AnnData "" "") (text "["))
2008 right = (annotate (AnnData "" "") (text "]"))
2009 comma = (annotate (AnnData "" "") (text ","))
2010 slist _ _ _ _ = Nothing
2012 pairElts :: PTerm -> Maybe [PTerm]
2013 pairElts (PPair _ _ _ x y) | Just elts <- pairElts y = Just (x:elts)
2014 | otherwise = Just [x, y]
2015 pairElts _ = Nothing
2017 natns = "Prelude.Nat."
2019 snat :: PPOption -> Maybe Int -> Int -> PTerm -> Maybe Integer
2020 snat ppo d p e
2021 | ppopt_desugarnats ppo = Nothing
2022 | otherwise = snat' d p e
2023 where
2024 snat' :: Maybe Int -> Int -> PTerm -> Maybe Integer
2025 snat' (Just x) _ _ | x <= 0 = Nothing
2026 snat' d p (PRef _ _ z)
2027 | show z == (natns++"Z") || show z == "Z" = Just 0
2028 snat' d p (PApp _ s [PExp {getTm=n}])
2029 | show s == (natns++"S") || show s == "S",
2030 Just n' <- snat' (decD d) p n
2031 = Just $ 1 + n'
2032 snat' _ _ _ = Nothing
2034 bracket outer inner doc
2035 | outer > inner = lparen <> doc <> rparen
2036 | otherwise = doc
2038 ellipsis = text "..."
2040 depth Nothing = id
2041 depth (Just d) = if d <= 0 then const ellipsis else id
2043 decD = fmap (\x -> x - 1)
2045 kwd = annotate AnnKeyword . text
2047 fixities :: M.Map String Fixity
2048 fixities = M.fromList [(s, f) | (Fix f s) <- infixes]
2050 getFixity :: String -> Maybe Fixity
2051 getFixity = flip M.lookup fixities
2053-- | Strip away namespace information
2054basename :: Name -> Name
2055basename (NS n _) = basename n
2056basename n = n
2058-- | Determine whether a name was the one inserted for a hole or
2059-- guess by the delaborator
2060isHoleName :: Name -> Bool
2061isHoleName (UN n) = n == T.pack "[__]"
2062isHoleName _ = False
2064-- | Check whether a PTerm has been delaborated from a Term containing a Hole or Guess
2065containsHole :: PTerm -> Bool
2066containsHole pterm = or [isHoleName n | PRef _ _ n <- take 1000 $ universe pterm]
2068-- | Pretty-printer helper for names that attaches the correct annotations
2069prettyName
2070 :: Bool -- ^^ whether the name should be parenthesised if it is an infix operator
2071 -> Bool -- ^^ whether to show namespaces
2072 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
2073 -> Name -- ^^ the name to pprint
2074 -> Doc OutputAnnotation
2075prettyName infixParen showNS bnd n
2076 | (MN _ s) <- n, isPrefixOf "_" $ T.unpack s = text "_"
2077 | (UN n') <- n, T.unpack n' == "_" = text "_"
2078 | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) fullName
2079 | otherwise = annotate (AnnName n Nothing Nothing Nothing) fullName
2080 where fullName = text nameSpace <> parenthesise (text (baseName n))
2081 baseName (UN n) = T.unpack n
2082 baseName (NS n ns) = baseName n
2083 baseName (MN i s) = T.unpack s
2084 baseName other = show other
2085 nameSpace = case n of
2086 (NS n' ns) -> if showNS then (concatMap (++ ".") . map T.unpack . reverse) ns else ""
2087 _ -> ""
2088 isInfix = case baseName n of
2089 "" -> False
2090 (c : _) -> not (isAlpha c)
2091 parenthesise = if isInfix && infixParen then enclose lparen rparen else id
2094showCImp :: PPOption -> PClause -> Doc OutputAnnotation
2095showCImp ppo (PClause _ n l ws r w)
2096 = prettyImp ppo l <+> showWs ws <+> text "=" <+> prettyImp ppo r
2097 <+> text "where" <+> text (show w)
2098 where
2099 showWs [] = empty
2100 showWs (x : xs) = text "|" <+> prettyImp ppo x <+> showWs xs
2101showCImp ppo (PWith _ n l ws r pn w)
2102 = prettyImp ppo l <+> showWs ws <+> text "with" <+> prettyImp ppo r
2103 <+> braces (text (show w))
2104 where
2105 showWs [] = empty
2106 showWs (x : xs) = text "|" <+> prettyImp ppo x <+> showWs xs
2109showDImp :: PPOption -> PData -> Doc OutputAnnotation
2110showDImp ppo (PDatadecl n nfc ty cons)
2111 = text "data" <+> text (show n) <+> colon <+> prettyImp ppo ty <+> text "where" <$>
2112 (indent 2 $ vsep (map (\ (_, _, n, _, t, _, _) -> pipe <+> prettyName True False [] n <+> colon <+> prettyImp ppo t) cons))
2114showDecls :: PPOption -> [PDecl] -> Doc OutputAnnotation
2115showDecls o ds = vsep (map (showDeclImp o) ds)
2117showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
2118showDeclImp o (PTy _ _ _ _ _ n _ t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp o t
2119showDeclImp o (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
2120 indent 2 (vsep (map (showCImp o) cs))
2121showDeclImp o (PData _ _ _ _ _ d) = showDImp o { ppopt_impl = True } d
2122showDeclImp o (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls o ps <> line)
2123showDeclImp o (PNamespace n fc ps) = text "namespace" <+> text n <> braces (line <> showDecls o ps <> line)
2124showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
2125showDeclImp o (PClass _ _ _ cs n _ ps _ _ ds _ _)
2126 = text "interface" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls o ds
2127showDeclImp o (PInstance _ _ _ _ cs acc _ n _ _ t _ ds)
2128 = text "implementation" <+> text (show cs) <+> text (show n) <+> prettyImp o t <> line <> showDecls o ds
2129showDeclImp _ _ = text "..."
2130-- showDeclImp (PImport o) = "import " ++ o
2132getImps :: [PArg] -> [(Name, PTerm)]
2133getImps [] = []
2134getImps (PImp _ _ _ n tm : xs) = (n, tm) : getImps xs
2135getImps (_ : xs) = getImps xs
2137getExps :: [PArg] -> [PTerm]
2138getExps [] = []
2139getExps (PExp _ _ _ tm : xs) = tm : getExps xs
2140getExps (_ : xs) = getExps xs
2142getShowArgs :: [PArg] -> [PArg]
2143getShowArgs [] = []
2144getShowArgs (e@(PExp _ _ _ tm) : xs) = e : getShowArgs xs
2145getShowArgs (e : xs) | AlwaysShow `elem` argopts e = e : getShowArgs xs
2146 | PImp _ _ _ _ tm <- e
2147 , containsHole tm = e : getShowArgs xs
2148getShowArgs (_ : xs) = getShowArgs xs
2150getConsts :: [PArg] -> [PTerm]
2151getConsts [] = []
2152getConsts (PConstraint _ _ _ tm : xs) = tm : getConsts xs
2153getConsts (_ : xs) = getConsts xs
2155getAll :: [PArg] -> [PTerm]
2156getAll = map getTm
2159-- | Show Idris name
2160showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
2161 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
2162 -> PPOption -- ^^ pretty printing options
2163 -> Bool -- ^^ whether to colourise
2164 -> Name -- ^^ the term to show
2165 -> String
2166showName ist bnd ppo colour n = case ist of
2167 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
2168 Nothing -> showbasic n
2169 where name = if ppopt_impl ppo then show n else showbasic n
2170 showbasic n@(UN _) = showCG n
2171 showbasic (MN i s) = str s
2172 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
2173 showbasic (SN s) = show s
2174 fst3 (x, _, _) = x
2175 colourise n t = let ctxt' = fmap tt_ctxt ist in
2176 case ctxt' of
2177 Nothing -> name
2178 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
2179 else colouriseBound t name
2180 | isDConName n ctxt -> colouriseData t name
2181 | isFnName n ctxt -> colouriseFun t name
2182 | isTConName n ctxt -> colouriseType t name
2183 -- The assumption is that if a name is not bound and does not exist in the
2184 -- global context, then we're somewhere in which implicit info has been lost
2185 -- (like error messages). Thus, unknown vars are colourised as implicits.
2186 | otherwise -> colouriseImplicit t name
2188showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
2189 -> PTerm -- ^^ the term to show
2190 -> String
2191showTm ist = displayDecorated (consoleDecorate ist) .
2192 renderPretty 0.8 100000 .
2193 prettyImp (ppOptionIst ist)
2195-- | Show a term with implicits, no colours
2196showTmImpls :: PTerm -> String
2197showTmImpls = flip (displayS . renderCompact . prettyImp verbosePPOption) ""
2199-- | Show a term with specific options
2200showTmOpts :: PPOption -> PTerm -> String
2201showTmOpts opt = flip (displayS . renderPretty 1.0 10000000 . prettyImp opt) ""
2204instance Sized PTerm where
2205 size (PQuote rawTerm) = size rawTerm
2206 size (PRef fc _ name) = size name
2207 size (PLam fc name _ ty bdy) = 1 + size ty + size bdy
2208 size (PPi plicity name fc ty bdy) = 1 + size ty + size fc + size bdy
2209 size (PLet fc name nfc ty def bdy) = 1 + size ty + size def + size bdy
2210 size (PTyped trm ty) = 1 + size trm + size ty
2211 size (PApp fc name args) = 1 + size args
2212 size (PAppBind fc name args) = 1 + size args
2213 size (PCase fc trm bdy) = 1 + size trm + size bdy
2214 size (PIfThenElse fc c t f) = 1 + sum (map size [c, t, f])
2215 size (PTrue fc _) = 1
2216 size (PResolveTC fc) = 1
2217 size (PRewrite fc left right _) = 1 + size left + size right
2218 size (PPair fc _ _ left right) = 1 + size left + size right
2219 size (PDPair fs _ _ left ty right) = 1 + size left + size ty + size right
2220 size (PAlternative _ a alts) = 1 + size alts
2221 size (PHidden hidden) = size hidden
2222 size (PUnifyLog tm) = size tm
2223 size (PDisamb _ tm) = size tm
2224 size (PNoImplicits tm) = size tm
2225 size (PType _) = 1
2226 size (PUniverse _) = 1
2227 size (PConstant fc const) = 1 + size fc + size const
2228 size Placeholder = 1
2229 size (PDoBlock dos) = 1 + size dos
2230 size (PIdiom fc term) = 1 + size term
2231 size (PReturn fc) = 1
2232 size (PMetavar _ name) = 1
2233 size (PProof tactics) = size tactics
2234 size (PElabError err) = size err
2235 size PImpossible = 1
2236 size _ = 0
2238getPArity :: PTerm -> Int
2239getPArity (PPi _ _ _ _ sc) = 1 + getPArity sc
2240getPArity _ = 0
2242-- Return all names, free or globally bound, in the given term.
2244allNamesIn :: PTerm -> [Name]
2245allNamesIn tm = nub $ ni 0 [] tm
2246 where -- TODO THINK added niTacImp, but is it right?
2247 ni 0 env (PRef _ _ n)
2248 | not (n `elem` env) = [n]
2249 ni 0 env (PPatvar _ n) = [n]
2250 ni 0 env (PApp _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
2251 ni 0 env (PAppBind _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
2252 ni 0 env (PCase _ c os) = ni 0 env c ++ concatMap (ni 0 env . snd) os
2253 ni 0 env (PIfThenElse _ c t f) = ni 0 env c ++ ni 0 env t ++ ni 0 env f
2254 ni 0 env (PLam fc n _ ty sc) = ni 0 env ty ++ ni 0 (n:env) sc
2255 ni 0 env (PPi p n _ ty sc) = niTacImp 0 env p ++ ni 0 env ty ++ ni 0 (n:env) sc
2256 ni 0 env (PLet _ n _ ty val sc) = ni 0 env ty ++ ni 0 env val ++ ni 0 (n:env) sc
2257 ni 0 env (PHidden tm) = ni 0 env tm
2258 ni 0 env (PRewrite _ l r _) = ni 0 env l ++ ni 0 env r
2259 ni 0 env (PTyped l r) = ni 0 env l ++ ni 0 env r
2260 ni 0 env (PPair _ _ _ l r) = ni 0 env l ++ ni 0 env r
2261 ni 0 env (PDPair _ _ _ (PRef _ _ n) Placeholder r) = n : ni 0 env r
2262 ni 0 env (PDPair _ _ _ (PRef _ _ n) t r) = ni 0 env t ++ ni 0 (n:env) r
2263 ni 0 env (PDPair _ _ _ l t r) = ni 0 env l ++ ni 0 env t ++ ni 0 env r
2264 ni 0 env (PAlternative ns a ls) = concatMap (ni 0 env) ls
2265 ni 0 env (PUnifyLog tm) = ni 0 env tm
2266 ni 0 env (PDisamb _ tm) = ni 0 env tm
2267 ni 0 env (PNoImplicits tm) = ni 0 env tm
2269 ni i env (PQuasiquote tm ty) = ni (i+1) env tm ++ maybe [] (ni i env) ty
2270 ni i env (PUnquote tm) = ni (i - 1) env tm
2271 ni i env tm = concatMap (ni i env) (children tm)
2273 niTacImp i env (TacImp _ _ scr) = ni i env scr
2274 niTacImp _ _ _ = []
2277-- Return all names defined in binders in the given term
2278boundNamesIn :: PTerm -> [Name]
2279boundNamesIn tm = S.toList (ni 0 S.empty tm)
2280 where -- TODO THINK Added niTacImp, but is it right?
2281 ni :: Int -> S.Set Name -> PTerm -> S.Set Name
2282 ni 0 set (PApp _ f as) = niTms 0 (ni 0 set f) (map getTm as)
2283 ni 0 set (PAppBind _ f as) = niTms 0 (ni 0 set f) (map getTm as)
2284 ni 0 set (PCase _ c os) = niTms 0 (ni 0 set c) (map snd os)
2285 ni 0 set (PIfThenElse _ c t f) = niTms 0 set [c, t, f]
2286 ni 0 set (PLam fc n _ ty sc) = S.insert n $ ni 0 (ni 0 set ty) sc
2287 ni 0 set (PLet fc n nfc ty val sc) = S.insert n $ ni 0 (ni 0 (ni 0 set ty) val) sc
2288 ni 0 set (PPi p n _ ty sc) = niTacImp 0 (S.insert n $ ni 0 (ni 0 set ty) sc) p
2289 ni 0 set (PRewrite _ l r _) = ni 0 (ni 0 set l) r
2290 ni 0 set (PTyped l r) = ni 0 (ni 0 set l) r
2291 ni 0 set (PPair _ _ _ l r) = ni 0 (ni 0 set l) r
2292 ni 0 set (PDPair _ _ _ (PRef _ _ n) t r) = ni 0 (ni 0 set t) r
2293 ni 0 set (PDPair _ _ _ l t r) = ni 0 (ni 0 (ni 0 set l) t) r
2294 ni 0 set (PAlternative ns a as) = niTms 0 set as
2295 ni 0 set (PHidden tm) = ni 0 set tm
2296 ni 0 set (PUnifyLog tm) = ni 0 set tm
2297 ni 0 set (PDisamb _ tm) = ni 0 set tm
2298 ni 0 set (PNoImplicits tm) = ni 0 set tm
2300 ni i set (PQuasiquote tm ty) = ni (i + 1) set tm `S.union` maybe S.empty (ni i set) ty
2301 ni i set (PUnquote tm) = ni (i - 1) set tm
2303 ni i set tm = foldr (S.union . ni i set) set (children tm)
2305 niTms :: Int -> S.Set Name -> [PTerm] -> S.Set Name
2306 niTms i set [] = set
2307 niTms i set (x : xs) = niTms i (ni i set x) xs
2309 niTacImp i set (TacImp _ _ scr) = ni i set scr
2310 niTacImp i set _ = set
2312-- Return names which are valid implicits in the given term (type).
2313implicitNamesIn :: [Name] -> IState -> PTerm -> [Name]
2314implicitNamesIn uvars ist tm
2315 = let (imps, fns) = execState (ni 0 [] tm) ([], []) in
2316 nub imps \\ nub fns
2317 where
2318 addImp n = do (imps, fns) <- get
2319 put (n : imps, fns)
2320 addFn n = do (imps, fns) <- get
2321 put (imps, n: fns)
2323 notCAF [] = False
2324 notCAF (PExp _ _ _ _ : _) = True
2325 notCAF (_ : xs) = notCAF xs
2327 notHidden (n, _) = case getAccessibility n of
2328 Hidden -> False
2329 Private -> False
2330 _ -> True
2332 getAccessibility n
2333 = case lookupDefAccExact n False (tt_ctxt ist) of
2334 Just (n,t) -> t
2335 _ -> Public
2337 ni 0 env (PRef _ _ n@(NS _ _))
2338 | not (n `elem` env)
2339 -- Never implicitly bind if there's a namespace
2340 = addFn n
2341 ni 0 env (PRef _ _ n)
2342 | not (n `elem` env) && implicitable n || n `elem` uvars = addImp n
2343 ni 0 env (PApp _ f@(PRef _ _ n) as)
2344 | n `elem` uvars = do ni 0 env f
2345 mapM_ (ni 0 env . getTm) as
2346 | otherwise = do case lookupTy n (tt_ctxt ist) of
2347 [] -> return ()
2348 _ -> addFn n
2349 mapM_ (ni 0 env . getTm) as
2350 ni 0 env (PApp _ f as) = do ni 0 env f
2351 mapM_ (ni 0 env . getTm) as
2352 ni 0 env (PAppBind _ f as) = do ni 0 env f
2353 mapM_ (ni 0 env . getTm) as
2354 ni 0 env (PCase _ c os) = do ni 0 env c
2355 -- names in 'os', not counting the names bound in the cases
2356 mapM_ (ni 0 env . snd) os
2357 (imps, fns) <- get
2358 put ([] ,[])
2359 mapM_ (ni 0 env . fst) os
2360 (impsfst, _) <- get
2361 put (nub imps \\ nub impsfst, fns)
2362 ni 0 env (PIfThenElse _ c t f) = mapM_ (ni 0 env) [c, t, f]
2363 ni 0 env (PLam fc n _ ty sc) = do ni 0 env ty; ni 0 (n:env) sc
2364 ni 0 env (PPi p n _ ty sc) = do ni 0 env ty; ni 0 (n:env) sc
2365 ni 0 env (PRewrite _ l r _) = do ni 0 env l; ni 0 env r
2366 ni 0 env (PTyped l r) = do ni 0 env l; ni 0 env r
2367 ni 0 env (PPair _ _ _ l r) = do ni 0 env l; ni 0 env r
2368 ni 0 env (PDPair _ _ _ (PRef _ _ n) t r) = do ni 0 env t; ni 0 (n:env) r
2369 ni 0 env (PDPair _ _ _ l t r) = do ni 0 env l
2370 ni 0 env t
2371 ni 0 env r
2372 ni 0 env (PAlternative ns a as) = mapM_ (ni 0 env) as
2373 ni 0 env (PHidden tm) = ni 0 env tm
2374 ni 0 env (PUnifyLog tm) = ni 0 env tm
2375 ni 0 env (PDisamb _ tm) = ni 0 env tm
2376 ni 0 env (PNoImplicits tm) = ni 0 env tm
2378 ni i env (PQuasiquote tm ty) = do ni (i + 1) env tm
2379 maybe (return ()) (ni i env) ty
2380 ni i env (PUnquote tm) = ni (i - 1) env tm
2382 ni i env tm = mapM_ (ni i env) (children tm)
2384-- Return names which are free in the given term.
2385namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
2386namesIn uvars ist tm = nub $ ni 0 [] tm
2387 where
2388 ni 0 env (PRef _ _ n)
2389 | not (n `elem` env)
2390 = case lookupTy n (tt_ctxt ist) of
2391 [] -> [n]
2392 _ -> [n | n `elem` (map fst uvars)]
2393 ni 0 env (PApp _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
2394 ni 0 env (PAppBind _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
2395 ni 0 env (PCase _ c os) = ni 0 env c ++
2396 -- names in 'os', not counting the names bound in the cases
2397 (nub (concatMap (ni 0 env . snd) os)
2398 \\ nub (concatMap (ni 0 env . fst) os))
2399 ni 0 env (PIfThenElse _ c t f) = concatMap (ni 0 env) [c, t, f]
2400 ni 0 env (PLam fc n nfc ty sc) = ni 0 env ty ++ ni 0 (n:env) sc
2401 ni 0 env (PPi p n _ ty sc) = niTacImp 0 env p ++ ni 0 env ty ++ ni 0 (n:env) sc
2402 ni 0 env (PRewrite _ l r _) = ni 0 env l ++ ni 0 env r
2403 ni 0 env (PTyped l r) = ni 0 env l ++ ni 0 env r
2404 ni 0 env (PPair _ _ _ l r) = ni 0 env l ++ ni 0 env r
2405 ni 0 env (PDPair _ _ _ (PRef _ _ n) t r) = ni 0 env t ++ ni 0 (n:env) r
2406 ni 0 env (PDPair _ _ _ l t r) = ni 0 env l ++ ni 0 env t ++ ni 0 env r
2407 ni 0 env (PAlternative ns a as) = concatMap (ni 0 env) as
2408 ni 0 env (PHidden tm) = ni 0 env tm
2409 ni 0 env (PUnifyLog tm) = ni 0 env tm
2410 ni 0 env (PDisamb _ tm) = ni 0 env tm
2411 ni 0 env (PNoImplicits tm) = ni 0 env tm
2413 ni i env (PQuasiquote tm ty) = ni (i + 1) env tm ++ maybe [] (ni i env) ty
2414 ni i env (PUnquote tm) = ni (i - 1) env tm
2416 ni i env tm = concatMap (ni i env) (children tm)
2418 niTacImp i env (TacImp _ _ scr) = ni i env scr
2419 niTacImp _ _ _ = []
2421-- Return which of the given names are used in the given term.
2423usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
2424usedNamesIn vars ist tm = nub $ ni 0 [] tm
2425 where -- TODO THINK added niTacImp, but is it right?
2426 ni 0 env (PRef _ _ n)
2427 | n `elem` vars && not (n `elem` env)
2428 = case lookupDefExact n (tt_ctxt ist) of
2429 Nothing -> [n]
2430 _ -> []
2431 ni 0 env (PApp _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
2432 ni 0 env (PAppBind _ f as) = ni 0 env f ++ concatMap (ni 0 env . getTm) as
2433 ni 0 env (PCase _ c os) = ni 0 env c ++ concatMap (ni 0 env . snd) os
2434 ni 0 env (PIfThenElse _ c t f) = concatMap (ni 0 env) [c, t, f]
2435 ni 0 env (PLam fc n _ ty sc) = ni 0 env ty ++ ni 0 (n:env) sc
2436 ni 0 env (PPi p n _ ty sc) = niTacImp 0 env p ++ ni 0 env ty ++ ni 0 (n:env) sc
2437 ni 0 env (PRewrite _ l r _) = ni 0 env l ++ ni 0 env r
2438 ni 0 env (PTyped l r) = ni 0 env l ++ ni 0 env r
2439 ni 0 env (PPair _ _ _ l r) = ni 0 env l ++ ni 0 env r
2440 ni 0 env (PDPair _ _ _ (PRef _ _ n) t r) = ni 0 env t ++ ni 0 (n:env) r
2441 ni 0 env (PDPair _ _ _ l t r) = ni 0 env l ++ ni 0 env t ++ ni 0 env r
2442 ni 0 env (PAlternative ns a as) = concatMap (ni 0 env) as
2443 ni 0 env (PHidden tm) = ni 0 env tm
2444 ni 0 env (PUnifyLog tm) = ni 0 env tm
2445 ni 0 env (PDisamb _ tm) = ni 0 env tm
2446 ni 0 env (PNoImplicits tm) = ni 0 env tm
2448 ni i env (PQuasiquote tm ty) = ni (i + 1) env tm ++ maybe [] (ni i env) ty
2449 ni i env (PUnquote tm) = ni (i - 1) env tm
2451 ni i env tm = concatMap (ni i env) (children tm)
2453 niTacImp i env (TacImp _ _ scr) = ni i env scr
2454 niTacImp _ _ _ = []
2456-- Return the list of inaccessible (= dotted) positions for a name.
2457getErasureInfo :: IState -> Name -> [Int]
2458getErasureInfo ist n =
2459 case lookupCtxtExact n (idris_optimisation ist) of
2460 Just (Optimise inacc detagg) -> map fst inacc
2461 Nothing -> []