1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
7import Idris.Core.Evaluate
8import Idris.Core.Elaborate hiding (Tactic(..))
9import Idris.Core.Typecheck
10import Idris.Docstrings
12import IRTS.CodegenCommon
14import Util.DynamicLinker
20import System.Console.Haskeline
23import Control.Monad.Trans.State.Strict
24import Control.Monad.Trans.Error
26import Data.List hiding (group)
28import qualified Data.Map as M
29import qualified Data.Text as T
30import qualified Data.Map as M
32import qualified Data.Set as S
33import Data.Word (Word)
37import Text.PrettyPrint.Annotated.Leijen
39data IOption = IOption { opt_logLevel :: Int,
41 opt_typeintype :: Bool,
43 opt_showimp :: Bool, -- ^^ show implicits
44 opt_errContext :: Bool,
49 opt_codegen :: Codegen,
50 opt_outputTy :: OutputType,
51 opt_ibcsubdir :: FilePath,
52 opt_importdirs :: [FilePath],
56 opt_cmdline :: [Opt], -- remember whole command line
58 opt_autoSolve :: Bool -- ^ automatically apply "solve" tactic in prover
62defaultOpts = IOption { opt_logLevel = 0
63 , opt_typecase = False
64 , opt_typeintype = False
67 , opt_errContext = False
70 , opt_nobanner = False
73 , opt_outputTy = Executable
81 , opt_autoSolve = True
84data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
86-- | The output mode in use
87data OutputMode = RawOutput | IdeSlave Integer deriving Show
89-- | How wide is the console?
90data ConsoleWidth = InfinitelyWide -- ^ Have pretty-printer assume that lines should not be broken
91 | ColsWide Int -- ^ Manually specified - must be positive
92 | AutomaticWidth -- ^ Attempt to determine width, or 80 otherwise
94-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
95-- without typechecking).
96-- This will include all the functions and data declarations, plus fixity declarations
99-- | The global state used in the Idris monad
100data IState = IState {
101 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
102 idris_constraints :: [(UConstraint, FC)],
103 -- ^ A list of universe constraints and their corresponding source locations
104 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
105 idris_implicits :: Ctxt [PArg],
106 idris_statics :: Ctxt [Bool],
107 idris_classes :: Ctxt ClassInfo,
108 idris_dsls :: Ctxt DSL,
109 idris_optimisation :: Ctxt OptInfo,
110 idris_datatypes :: Ctxt TypeInfo,
111 idris_namehints :: Ctxt [Name],
112 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
113 -- ^ list of lhs/rhs, and a list of missing clauses
114 idris_flags :: Ctxt [FnOpt],
115 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
116 idris_calledgraph :: Ctxt [Name],
117 idris_docstrings :: Ctxt (Docstring, [(Name, Docstring)]),
118 idris_tyinfodata :: Ctxt TIData,
119 idris_totcheck :: [(FC, Name)], -- names to check totality on
120 idris_defertotcheck :: [(FC, Name)], -- names to check at the end
121 idris_options :: IOption,
123 idris_lineapps :: [((FilePath, Int), PTerm)],
124 -- ^ Full application LHS on source line
125 idris_metavars :: [(Name, (Maybe Name, Int, Bool))], -- ^ The currently defined but not proven metavariables
126 idris_coercions :: [Name],
127 idris_transforms :: [(Term, Term)],
128 idris_errRev :: [(Term, Term)],
129 syntax_rules :: [Syntax],
130 syntax_keywords :: [String],
131 imported :: [FilePath], -- ^ The imported modules
132 idris_scprims :: [(Name, (Int, PrimFn))],
133 idris_objs :: [(Codegen, FilePath)],
134 idris_libs :: [(Codegen, String)],
135 idris_cgflags :: [(Codegen, String)],
136 idris_hdrs :: [(Codegen, String)],
137 proof_list :: [(Name, [String])],
139 parserWarnings :: [(FC, Err)],
140 lastParse :: Maybe Name,
141 indent_stack :: [Int],
142 brace_stack :: [Maybe Int],
143 lastTokenSpan :: Maybe FC, -- ^ What was the span of the latest token parsed?
144 hide_list :: [(Name, Maybe Accessibility)],
145 default_access :: Accessibility,
146 default_total :: Bool,
147 ibc_write :: [IBCWrite],
148 compiled_so :: Maybe String,
149 idris_dynamic_libs :: [DynamicLib],
150 idris_language_extensions :: [LanguageExt],
151 idris_outputmode :: OutputMode,
152 idris_colourRepl :: Bool,
153 idris_colourTheme :: ColourTheme,
154 idris_outh :: Handle,
155 idris_errorhandlers :: [Name], -- ^ Global error handlers
156 idris_nameIdx :: (Int, Ctxt (Int, Name)),
157 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
158 module_aliases :: M.Map [T.Text] [T.Text],
159 idris_consolewidth :: ConsoleWidth -- ^ How many chars wide is the console?
162data SizeChange = Smaller | Same | Bigger | Unknown
165deriving instance Binary SizeChange
166deriving instance NFData SizeChange
169type SCGEntry = (Name, [Maybe (Int, SizeChange)])
171data CGInfo = CGInfo { argsdef :: [Name],
172 calls :: [(Name, [[Name]])],
178deriving instance Binary CGInfo
179deriving instance NFData CGInfo
182primDefs = [sUN "unsafePerformPrimIO",
183 sUN "mkLazyForeignPrim",
187-- information that needs writing for the current module's .ibc file
188data IBCWrite = IBCFix FixDecl
192 | IBCInstance Bool Name Name
200 | IBCObj Codegen FilePath
201 | IBCLib Codegen String
202 | IBCCGFlag Codegen String
204 | IBCHeader Codegen String
205 | IBCAccess Name Accessibility
206 | IBCMetaInformation Name MetaInformation
207 | IBCTotal Name Totality
208 | IBCFlags Name [FnOpt]
209 | IBCTrans (Term, Term)
210 | IBCErrRev (Term, Term)
214 | IBCDef Name -- i.e. main context
215 | IBCNameHint (Name, Name)
216 | IBCLineApp FilePath Int PTerm
217 | IBCErrorHandler Name
218 | IBCFunctionErrorHandler Name Name Name
221-- | The initial state for the compiler
223idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
224 emptyContext emptyContext emptyContext emptyContext
225 emptyContext emptyContext emptyContext emptyContext
226 emptyContext emptyContext
227 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
228 [] Nothing [] Nothing [] [] Nothing [] Hidden False [] Nothing [] [] RawOutput
229 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
232-- | The monad for the main REPL - reading and processing files and updating
233-- global state (hence the IO inner monad).
234--type Idris = WriterT [Either String (IO ())] (State IState a))
235type Idris = StateT IState (ErrorT Err IO)
237-- Commands in the REPL
256 | ChangeDirectory FilePath
259 | Compile Codegen String
264 | AddProof (Maybe Name)
276 | DynamicLink FilePath
281 | CaseSplitAt Bool Int Name
282 | AddClauseFrom Bool Int Name
283 | AddProofClauseFrom Bool Int Name
284 | AddMissing Bool Int Name
285 | MakeWith Bool Int Name
286 | DoProofSearch Bool Int Name [Name]
290 | SetColour ColourType IdrisColour
294 | SetConsoleWidth ConsoleWidth
296 | MakeDoc String -- IdrisDoc
298data Opt = Filename String
310 | NoBuiltins -- only for the really primitive stuff!
330 | PkgMkDoc String -- IdrisDoc
337 | OutputTy OutputType
338 | Extension LanguageExt
339 | InterpretScript String
340 | TargetTriple String
345 | AutoWidth -- ^ Automatically adjust terminal width
346 | AutoSolve -- ^ Automatically issue "solve" tactic in interactive prover
349-- Parsed declarations
351data Fixity = Infixl { prec :: Int }
352 | Infixr { prec :: Int }
353 | InfixN { prec :: Int }
354 | PrefixN { prec :: Int }
357deriving instance Binary Fixity
358deriving instance NFData Fixity
361instance Show Fixity where
362 show (Infixl i) = "infixl " ++ show i
363 show (Infixr i) = "infixr " ++ show i
364 show (InfixN i) = "infix " ++ show i
365 show (PrefixN i) = "prefix " ++ show i
367data FixDecl = Fix Fixity String
370instance Show FixDecl where
371 show (Fix f s) = show f ++ " " ++ s
374deriving instance Binary FixDecl
375deriving instance NFData FixDecl
378instance Ord FixDecl where
379 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
382data Static = Static | Dynamic
385deriving instance Binary Static
386deriving instance NFData Static
389-- Mark bindings with their explicitness, and laziness
390data Plicity = Imp { pargopts :: [ArgOpt],
393 | Exp { pargopts :: [ArgOpt],
396 | Constraint { pargopts :: [ArgOpt],
398 | TacImp { pargopts :: [ArgOpt],
403plazy :: Plicity -> Bool
404plazy tm = Lazy `elem` pargopts tm
407deriving instance Binary Plicity
408deriving instance NFData Plicity
411impl = Imp [Lazy] Dynamic False
412expl = Exp [] Dynamic False
413expl_param = Exp [] Dynamic True
414constraint = Constraint [] Dynamic
415tacimpl t = TacImp [] Dynamic t
417data FnOpt = Inlinable -- always evaluate when simplifying
418 | TotalFn | PartialFn | CoveringFn
419 | Coinductive | AssertTotal
420 | Dictionary -- type class dictionary, eval only when
421 -- a function argument, and further evaluation resutls
422 | Implicit -- implicit coercion
423 | CExport String -- export, with a C name
424 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
425 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
426 | Reflection -- a reflecting function, compile-time only
427 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
430deriving instance Binary FnOpt
431deriving instance NFData FnOpt
436inlinable :: FnOpts -> Bool
437inlinable = elem Inlinable
439dictionary :: FnOpts -> Bool
440dictionary = elem Dictionary
443-- | Data declaration options
444data DataOpt = Codata -- Set if the the data-type is coinductive
445 | DefaultEliminator -- Set if an eliminator should be generated for data type
449type DataOpts = [DataOpt]
451-- | Type provider - what to provide
452data ProvideWhat = ProvTerm -- ^ only allow providing terms
453 | ProvPostulate -- ^ only allow postulates
454 | ProvAny -- ^ either is ok
457-- | Top-level declarations such as compiler directives, definitions,
458-- datatypes and typeclasses.
460 = PFix FC Fixity [String] -- ^ Fixity declaration
461 | PTy Docstring [(Name, Docstring)] SyntaxInfo FC FnOpts Name t -- ^ Type declaration
462 | PPostulate Docstring SyntaxInfo FC FnOpts Name t -- ^ Postulate
463 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
464 | PCAF FC Name t -- ^ Top level constant
465 | PData Docstring [(Name, Docstring)] SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
466 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
467 | PNamespace String [PDecl' t] -- ^ New namespace
468 | PRecord Docstring SyntaxInfo FC Name t Docstring Name t -- ^ Record declaration
469 | PClass Docstring SyntaxInfo FC
472 [(Name, t)] -- parameters
473 [PDecl' t] -- declarations
474 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
475 -- class name, parameters, method declarations
476 | PInstance SyntaxInfo FC [t] -- constraints
479 t -- full instance type
480 (Maybe Name) -- explicit name
482 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
483 -- class name, parameters, full instance type, optional explicit name, and definitions
484 | PDSL Name (DSL' t) -- ^ DSL declaration
485 | PSyntax FC Syntax -- ^ Syntax definition
486 | PMutual FC [PDecl' t] -- ^ Mutual block
487 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
488 | PProvider SyntaxInfo FC ProvideWhat Name t t -- ^ Type provider. The first t is the type, the second is the term
489 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
490 -- bool is True, lhs and rhs must be convertible
493deriving instance Binary PDecl'
494deriving instance NFData PDecl'
497-- For elaborator state
498type ElabD a = Elab' [PDecl] a
500-- | One clause of a top-level definition. Term arguments to constructors are:
502-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
504-- 2. The list of extra 'with' patterns
506-- 3. The right-hand side
508-- 4. The where block (PDecl' t)
510data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
511 | PWith FC Name t [t] t [PDecl' t]
512 | PClauseR FC [t] t [PDecl' t]
513 | PWithR FC [t] t [PDecl' t]
516deriving instance Binary PClause'
517deriving instance NFData PClause'
521data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
522 d_tcon :: t, -- ^ Type constructor
523 d_cons :: [(Docstring, [(Name, Docstring)], Name, t, FC, [Name])] -- ^ Constructors
525 -- ^ Data declaration
526 | PLaterdecl { d_name :: Name, d_tcon :: t }
527 -- ^ "Placeholder" for data whose constructors are defined later
530deriving instance Binary PData'
531deriving instance NFData PData'
534-- Handy to get a free function for applying PTerm -> PTerm functions
535-- across a program, by deriving Functor
537type PDecl = PDecl' PTerm
538type PData = PData' PTerm
539type PClause = PClause' PTerm
541-- get all the names declared in a decl
543declared :: PDecl -> [Name]
544declared (PFix _ _ _) = []
545declared (PTy _ _ _ _ _ n t) = [n]
546declared (PPostulate _ _ _ _ n t) = [n]
547declared (PClauses _ _ n _) = [] -- not a declaration
548declared (PCAF _ n _) = [n]
549declared (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
550 where fstt (_, _, a, _, _, _) = a
551declared (PData _ _ _ _ _ (PLaterdecl n _)) = [n]
552declared (PParams _ _ ds) = concatMap declared ds
553declared (PNamespace _ ds) = concatMap declared ds
554declared (PRecord _ _ _ n _ _ c _) = [n, c]
555declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
556declared (PInstance _ _ _ _ _ _ _ _) = []
557declared (PDSL n _) = [n]
558declared (PSyntax _ _) = []
559declared (PMutual _ ds) = concatMap declared ds
560declared (PDirective _) = []
562-- get the names declared, not counting nested parameter blocks
563tldeclared :: PDecl -> [Name]
564tldeclared (PFix _ _ _) = []
565tldeclared (PTy _ _ _ _ _ n t) = [n]
566tldeclared (PPostulate _ _ _ _ n t) = [n]
567tldeclared (PClauses _ _ n _) = [] -- not a declaration
568tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
569tldeclared (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
570 where fstt (_, _, a, _, _, _) = a
571tldeclared (PParams _ _ ds) = []
572tldeclared (PMutual _ ds) = concatMap tldeclared ds
573tldeclared (PNamespace _ ds) = concatMap tldeclared ds
574tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
575tldeclared (PInstance _ _ _ _ _ _ _ _) = []
578defined :: PDecl -> [Name]
579defined (PFix _ _ _) = []
580defined (PTy _ _ _ _ _ n t) = []
581defined (PPostulate _ _ _ _ n t) = []
582defined (PClauses _ _ n _) = [n] -- not a declaration
583defined (PCAF _ n _) = [n]
584defined (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
585 where fstt (_, _, a, _, _, _) = a
586defined (PData _ _ _ _ _ (PLaterdecl n _)) = []
587defined (PParams _ _ ds) = concatMap defined ds
588defined (PNamespace _ ds) = concatMap defined ds
589defined (PRecord _ _ _ n _ _ c _) = [n, c]
590defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
591defined (PInstance _ _ _ _ _ _ _ _) = []
592defined (PDSL n _) = [n]
593defined (PSyntax _ _) = []
594defined (PMutual _ ds) = concatMap defined ds
595defined (PDirective _) = []
598updateN :: [(Name, Name)] -> Name -> Name
599updateN ns n | Just n' <- lookup n ns = n'
602updateNs :: [(Name, Name)] -> PTerm -> PTerm
604updateNs ns t = mapPT updateRef t
605 where updateRef (PRef fc f) = PRef fc (updateN ns f)
608-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
610-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
611-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
612-- where updateCNs ns (PClause n l ts r ds)
613-- = PClause (updateN ns n) (fmap (updateNs ns) l)
614-- (map (fmap (updateNs ns)) ts)
615-- (fmap (updateNs ns) r)
616-- (map (updateDNs ns) ds)
619data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show)
621-- | High level language terms
622data PTerm = PQuote Raw
624 | PInferRef FC Name -- ^ A name to be defined later
626 | PLam Name PTerm PTerm
627 | PPi Plicity Name PTerm PTerm
628 | PLet Name PTerm PTerm PTerm
629 | PTyped PTerm PTerm -- ^ Term with explicit type
630 | PApp FC PTerm [PArg]
631 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
632 | PMatchApp FC Name -- ^ Make an application by type matching
633 | PCase FC PTerm [(PTerm, PTerm)]
639 | PRewrite FC PTerm PTerm (Maybe PTerm)
640 | PPair FC PunInfo PTerm PTerm
641 | PDPair FC PunInfo PTerm PTerm PTerm
642 | PAlternative Bool [PTerm] -- True if only one may work
643 | PHidden PTerm -- ^ Irrelevant or hidden pattern
645 | PGoal FC PTerm Name PTerm
652 | PProof [PTactic] -- ^ Proof script
653 | PTactics [PTactic] -- ^ As PProof, but no auto solving
654 | PElabError Err -- ^ Error to report on elaboration
655 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
656 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
657 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
658 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
659 | PNoImplicits PTerm -- ^ never run implicit converions on the term
664deriving instance Binary PTerm
665deriving instance NFData PTerm
668mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
669mapPT f t = f (mpt t) where
670 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
671 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
672 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
673 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
675 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
676 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
677 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
678 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
679 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
680 mpt (PPair fc p l r) = PPair fc p (mapPT f l) (mapPT f r)
681 mpt (PDPair fc p l t r) = PDPair fc p (mapPT f l) (mapPT f t) (mapPT f r)
682 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
683 mpt (PHidden t) = PHidden (mapPT f t)
684 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
685 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
686 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
687 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
688 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
689 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
690 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
694data PTactic' t = Intro [Name] | Intros | Focus Name
695 | Refine Name [Bool] | Rewrite t
699 | LetTac Name t | LetTacTy Name t t
700 | Exact t | Compute | Trivial | TCInstance
701 | ProofSearch (Maybe Name) [Name]
704 | ProofState | ProofTerm | Undo
705 | Try (PTactic' t) (PTactic' t)
706 | TSeq (PTactic' t) (PTactic' t)
707 | ApplyTactic t -- see Language.Reflection module
711 | GoalType String (PTactic' t)
715 deriving (Show, Eq, Functor)
717deriving instance Binary PTactic'
718deriving instance NFData PTactic'
720instance Sized a => Sized (PTactic' a) where
721 size (Intro nms) = 1 + size nms
723 size (Focus nm) = 1 + size nm
724 size (Refine nm bs) = 1 + size nm + length bs
725 size (Rewrite t) = 1 + size t
726 size (Induction t) = 1 + size t
727 size (LetTac nm t) = 1 + size nm + size t
728 size (Exact t) = 1 + size t
736 size (Try l r) = 1 + size l + size r
737 size (TSeq l r) = 1 + size l + size r
738 size (ApplyTactic t) = 1 + size t
739 size (Reflect t) = 1 + size t
740 size (Fill t) = 1 + size t
744type PTactic = PTactic' PTerm
746data PDo' t = DoExp FC t
748 | DoBindP FC t t [(t,t)]
751 deriving (Eq, Functor)
753deriving instance Binary PDo'
754deriving instance NFData PDo'
757instance Sized a => Sized (PDo' a) where
758 size (DoExp fc t) = 1 + size fc + size t
759 size (DoBind fc nm t) = 1 + size fc + size nm + size t
760 size (DoBindP fc l r alts) = 1 + size fc + size l + size r + size alts
761 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
762 size (DoLetP fc l r) = 1 + size fc + size l + size r
766-- The priority gives a hint as to elaboration order. Best to elaborate
767-- things early which will help give a more concrete type to other
768-- variables, e.g. a before (interpTy a).
770data PArg' t = PImp { priority :: Int,
771 machine_inf :: Bool, -- true if the machine inferred it
773 pname :: Name, getTm :: t }
774 | PExp { priority :: Int,
777 | PConstraint { priority :: Int,
780 | PTacImplicit { priority :: Int,
785 deriving (Show, Eq, Functor)
787data ArgOpt = Lazy | HideDisplay
790lazyarg :: PArg' t -> Bool
791lazyarg tm = Lazy `elem` argopts tm
793instance Sized a => Sized (PArg' a) where
794 size (PImp p _ l nm trm) = 1 + size nm + size trm
795 size (PExp p l trm) = 1 + size trm
796 size (PConstraint p l trm) = 1 + size trm
797 size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
800deriving instance Binary PArg'
801deriving instance NFData PArg'
804pimp n t mach = PImp 1 mach [Lazy] n t
806pconst t = PConstraint 1 [] t
807ptacimp n s t = PTacImplicit 2 [Lazy] n s t
809type PArg = PArg' PTerm
813data ClassInfo = CI { instanceName :: Name,
814 class_methods :: [(Name, (FnOpts, PTerm))],
815 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
816 class_default_superclasses :: [PDecl],
817 class_params :: [Name],
818 class_instances :: [Name] }
821deriving instance Binary ClassInfo
822deriving instance NFData ClassInfo
825-- Type inference data
827data TIData = TIPartial -- ^ a function with a partially defined type
828 | TISolution [Term] -- ^ possible solutions to a metavariable in a type
831-- An argument is conditionally forceable iff its forceability
832-- depends on the collapsibility of the whole type.
833data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
836deriving instance Binary Forceability
837deriving instance NFData Forceability
840data OptInfo = Optimise { collapsible :: Bool,
842 -- The following should actually be (IntMap Forceability)
843 -- but the corresponding Binary instance seems to be broken.
844 -- Let's store a list and convert it to IntMap whenever needed.
845 forceable :: [(Int, Forceability)],
849deriving instance Binary OptInfo
850deriving instance NFData OptInfo
854data TypeInfo = TI { con_names :: [Name],
856 data_opts :: DataOpts,
860deriving instance Binary TypeInfo
861deriving instance NFData TypeInfo
864-- Syntactic sugar info
866data DSL' t = DSL { dsl_bind :: t,
871 index_first :: Maybe t,
872 index_next :: Maybe t,
873 dsl_lambda :: Maybe t,
876 deriving (Show, Functor)
878deriving instance Binary DSL'
879deriving instance NFData DSL'
884data SynContext = PatternSyntax | TermSyntax | AnySyntax
887deriving instance Binary SynContext
888deriving instance NFData SynContext
891data Syntax = Rule [SSymbol] PTerm SynContext
894deriving instance Binary Syntax
895deriving instance NFData Syntax
898data SSymbol = Keyword Name
905deriving instance Binary SSymbol
906deriving instance NFData SSymbol
909initDSL = DSL (PRef f (sUN ">>="))
910 (PRef f (sUN "return"))
912 (PRef f (sUN "pure"))
918 where f = fileFC "(builtin)"
920data Using = UImplicit Name PTerm
921 | UConstraint Name [Name]
924deriving instance Binary Using
925deriving instance NFData Using
928data SyntaxInfo = Syn { using :: [Using],
929 syn_params :: [(Name, PTerm)],
930 syn_namespace :: [String],
932 decoration :: Name -> Name,
934 implicitAllowed :: Bool,
938deriving instance NFData SyntaxInfo
939deriving instance Binary SyntaxInfo
942defaultSyntax = Syn [] [] [] [] id False False initDSL
944expandNS :: SyntaxInfo -> Name -> Name
945expandNS syn n@(NS _ _) = n
946expandNS syn n = case syn_namespace syn of
951-- For inferring types of things
955inferTy = sMN 0 "__Infer"
956inferCon = sMN 0 "__infer"
957inferDecl = PDatadecl inferTy
959 [(emptyDocstring, [], inferCon, PPi impl (sMN 0 "iType") PType (
960 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
961 (PRef bi inferTy)), bi, [])]
964infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
965infP = P (TCon 6 0) inferTy (TType (UVal 0))
967getInferTerm, getInferType :: Term -> Term
968getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
969getInferTerm (App (App _ _) tm) = tm
970getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
972getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
973 where toTy (Lam t) = Pi t
974 toTy (PVar t) = PVTy t
976getInferType (App (App _ ty) _) = ty
980-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
982primNames = [unitTy, unitCon,
983 falseTy, pairTy, pairCon,
984 eqTy, eqCon, inferTy, inferCon]
986unitTy = sMN 0 "__Unit"
987unitCon = sMN 0 "__II"
988unitDecl = PDatadecl unitTy PType
989 [(emptyDocstring, [], unitCon, PRef bi unitTy, bi, [])]
990unitOpts = [DefaultEliminator]
992falseTy = sMN 0 "__False"
993falseDecl = PDatadecl falseTy PType []
996pairTy = sMN 0 "__Pair"
997pairCon = sMN 0 "__MkPair"
998pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
999 [(emptyDocstring, [], pairCon, PPi impl (n "A") PType (
1000 PPi impl (n "B") PType (
1001 PPi expl (n "a") (PRef bi (n "A")) (
1002 PPi expl (n "b") (PRef bi (n "B"))
1003 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
1004 pexp (PRef bi (n "B"))])))), bi, [])]
1010eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
1011 (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
1013 [(emptyDocstring, [], eqCon, PPi impl (n "A") PType (
1014 PPi impl (n "x") (PRef bi (n "A"))
1015 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
1016 pimp (n "B") Placeholder False,
1017 pexp (PRef bi (n "x")),
1018 pexp (PRef bi (n "x"))])), bi, [])]
1022elimName = sUN "__Elim"
1023elimMethElimTy = sUN "__elimTy"
1024elimMethElim = sUN "elim"
1025elimDecl = PClass (parseDocstring . T.pack $ "Type class for eliminators") defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
1026 [PTy emptyDocstring [] defaultSyntax bi [TotalFn] elimMethElimTy PType,
1027 PTy emptyDocstring [] defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
1029-- Defined in builtins.idr
1030sigmaTy = sUN "Exists"
1031existsCon = sUN "Ex_intro"
1033piBind :: [(Name, PTerm)] -> PTerm -> PTerm
1034piBind = piBindp expl
1036piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
1038piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
1041-- Pretty-printing declarations and terms
1043-- These "show" instances render to an absurdly wide screen because inserted line breaks
1044-- could interfere with interactive editing, which calls "show".
1046instance Show PTerm where
1047 showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
1049instance Show PDecl where
1050 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
1052instance Show PClause where
1053 showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
1055instance Show PData where
1056 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
1058instance Pretty PTerm OutputAnnotation where
1059 pretty = prettyImp False
1061-- | Colourise annotations according to an Idris state. It ignores the names
1062-- in the annotation, as there's no good way to show extended information on a
1064consoleDecorate :: IState -> OutputAnnotation -> String -> String
1065consoleDecorate ist _ | not (idris_colourRepl ist) = id
1066consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1067 in colouriseData theme
1068consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1069 in colouriseType theme
1070consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1071consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1072consoleDecorate ist AnnKeyword = colouriseKeyword (idris_colourTheme ist)
1073consoleDecorate ist (AnnName n _ _) = let ctxt = tt_ctxt ist
1074 theme = idris_colourTheme ist
1076 _ | isDConName n ctxt -> colouriseData theme
1077 _ | isFnName n ctxt -> colouriseFun theme
1078 _ | isTConName n ctxt -> colouriseType theme
1079 _ | otherwise -> id -- don't colourise unknown names
1080consoleDecorate ist (AnnFC _) = id
1081consoleDecorate ist (AnnTextFmt fmt) = Idris.Colours.colourise (colour fmt)
1082 where colour BoldText = IdrisColour Nothing True False True False
1083 colour UnderlineText = IdrisColour Nothing True True False False
1084 colour ItalicText = IdrisColour Nothing True False False True
1086-- | Pretty-print a high-level closed Idris term
1087prettyImp :: Bool -- ^^ whether to show implicits
1088 -> PTerm -- ^^ the term to pretty-print
1089 -> Doc OutputAnnotation
1090prettyImp impl = pprintPTerm impl [] []
1092-- | Pretty-print a high-level Idris term in some bindings context
1093pprintPTerm :: Bool -- ^^ whether to show implicits
1094 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1095 -> [Name] -- ^^ names to always show in pi, even if not used
1096 -> PTerm -- ^^ the term to pretty-print
1097 -> Doc OutputAnnotation
1098pprintPTerm impl bnd docArgs = prettySe 10 bnd
1100 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1101 prettySe p bnd (PQuote r) =
1102 text "![" <> pretty r <> text "]"
1103 prettySe p bnd (PPatvar fc n) = pretty n
1105 | Just str <- slist p bnd e = str
1106 | Just n <- snat p e = annotate AnnConstData (text (show n))
1107 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1108 prettySe p bnd (PLam n ty sc) =
1109 bracket p 2 . group . align . hang 2 $
1110 text "\\" <> bindingOf n False <+> text "=>" <$>
1111 prettySe 10 ((n, False):bnd) sc
1112 prettySe p bnd (PLet n ty v sc) =
1114 kwd "let" <+> bindingOf n False <+> text "=" </>
1115 prettySe 10 bnd v <+> kwd "in" </>
1116 prettySe 10 ((n, False):bnd) sc
1117 prettySe p bnd (PPi (Exp l s _) n ty sc)
1118 | n `elem` allNamesIn sc || impl || n `elem` docArgs =
1119 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1120 bracket p 2 . group $
1121 enclose open rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1122 st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
1124 bracket p 2 . group $
1125 group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
1129 Static -> text "[static]" <> space
1131 prettySe p bnd (PPi (Imp l s _) n ty sc)
1133 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1135 open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1136 st <> text "->" </> prettySe 10 ((n, True):bnd) sc
1137 | otherwise = prettySe 10 ((n, True):bnd) sc
1141 Static -> text "[static]" <> space
1143 prettySe p bnd (PPi (Constraint _ _) n ty sc) =
1145 prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
1146 prettySe p bnd (PPi (TacImp _ _ s) n ty sc) =
1148 lbrace <> kwd "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1149 rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
1150 prettySe p bnd (PApp _ (PRef _ f) args) -- normal names, no explicit args
1151 | UN nm <- basename f
1152 , not impl && null (getExps args) = if isAlpha (thead nm)
1153 then prettyName impl bnd f
1154 else enclose lparen rparen $
1155 prettyName impl bnd f
1156 prettySe p bnd (PAppBind _ (PRef _ f) [])
1157 | not impl = text "!" <> prettyName impl bnd f
1158 prettySe p bnd (PApp _ (PRef _ op) args)
1159 | UN nm <- basename op
1161 (not impl) && (not $ isAlpha (thead nm)) =
1162 case getExps args of
1163 [] -> enclose lparen rparen opName
1164 [x] -> group (enclose lparen rparen opName <$> group (prettySe 0 bnd x))
1165 [l,r] -> bracket p 1 $ inFix l r
1166 (l:r:rest) -> bracket p 1 $
1167 enclose lparen rparen (inFix l r) <+>
1168 align (group (vsep (map (prettyArgSe bnd) rest)))
1169 where opName = prettyName impl bnd op
1170 inFix l r = align . group $
1171 (prettySe 1 bnd l <+> opName) <$> group (prettySe 0 bnd r)
1172 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1173 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1174 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1175 prettySe p bnd (PApp _ f as) =
1176 let args = getExps as
1177 fp = prettySe 1 bnd f
1179 bracket p 1 . group $
1183 else fp <+> align (vsep (map (prettyArgS bnd) as))
1186 else fp <+> align (vsep (map (prettyArgSe bnd) args))
1187 prettySe p bnd (PCase _ scr opts) =
1188 kwd "case" <+> prettySe 10 bnd scr <+> kwd "of" <> prettyBody
1190 prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1192 sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1193 prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1194 prettySe p bnd (PRefl _ _) = annotate (AnnName eqCon Nothing Nothing) $ text "refl"
1195 prettySe p bnd (PResolveTC _) = text "resolvetc"
1196 prettySe p bnd (PTrue _ IsType) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1197 prettySe p bnd (PTrue _ IsTerm) = annotate (AnnName unitCon Nothing Nothing) $ text "()"
1198 prettySe p bnd (PTrue _ TypeOrTerm) = text "()"
1199 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1200 prettySe p bnd (PEq _ l r) =
1201 bracket p 2 . align . group $
1202 prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
1203 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1204 prettySe p bnd (PRewrite _ l r _) =
1206 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1207 prettySe p bnd (PTyped l r) =
1208 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1209 prettySe p bnd pair@(PPair _ pun _ _) -- flatten tuples to the right, like parser
1210 | Just elts <- pairElts pair = enclose (ann lparen) (ann rparen) .
1211 align . group . vsep . punctuate (ann comma) $
1212 map (prettySe 10 bnd) elts
1213 where ann = case pun of
1215 IsType -> annotate (AnnName pairTy Nothing Nothing)
1216 IsTerm -> annotate (AnnName pairCon Nothing Nothing)
1217 prettySe p bnd (PDPair _ TypeOrTerm l t r) =
1218 lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1219 prettySe p bnd (PDPair _ IsType (PRef _ n) t r) =
1220 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1221 bindingOf n False <+>
1222 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1223 prettySe 10 ((n, False):bnd) r <>
1224 annotate (AnnName sigmaTy Nothing Nothing) rparen
1225 prettySe p bnd (PDPair _ IsType l t r) =
1226 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1227 prettySe 10 bnd l <+>
1228 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1229 prettySe 10 bnd r <>
1230 annotate (AnnName sigmaTy Nothing Nothing) rparen
1231 prettySe p bnd (PDPair _ IsTerm l t r) =
1232 annotate (AnnName existsCon Nothing Nothing) lparen <>
1233 prettySe 10 bnd l <+>
1234 annotate (AnnName existsCon Nothing Nothing) (text "**") <+>
1235 prettySe 10 bnd r <>
1236 annotate (AnnName existsCon Nothing Nothing) rparen
1237 prettySe p bnd (PAlternative a as) =
1238 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1241 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1242 prettySe p bnd PType = annotate AnnConstType $ text "Type"
1243 prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1244 where annot (AType _) = AnnConstType
1245 annot StrType = AnnConstType
1246 annot PtrType = AnnConstType
1247 annot VoidType = AnnConstType
1248 annot _ = AnnConstData
1249 -- XXX: add pretty for tactics
1250 prettySe p bnd (PProof ts) =
1251 text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1252 prettySe p bnd (PTactics ts) =
1253 text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1254 prettySe p bnd (PMetavar n) = text "?" <> pretty n
1255 prettySe p bnd (PReturn f) = kwd "return"
1256 prettySe p bnd PImpossible = kwd "impossible"
1257 prettySe p bnd Placeholder = text "_"
1258 prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1259 prettySe p bnd (PElabError s) = pretty s
1261 prettySe p bnd _ = text "test"
1263 prettyArgS bnd (PImp _ _ _ n tm) = prettyArgSi bnd (n, tm)
1264 prettyArgS bnd (PExp _ _ tm) = prettyArgSe bnd tm
1265 prettyArgS bnd (PConstraint _ _ tm) = prettyArgSc bnd tm
1266 prettyArgS bnd (PTacImplicit _ _ n _ tm) = prettyArgSti bnd (n, tm)
1268 prettyArgSe bnd arg = prettySe 0 bnd arg
1269 prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1270 prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1271 prettyArgSti bnd (n, val) = lbrace <> kwd "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1273 basename :: Name -> Name
1274 basename (NS n _) = basename n
1277 slist' p bnd (PApp _ (PRef _ nil) _)
1278 | not impl && nsroot nil == sUN "Nil" = Just []
1279 slist' p bnd (PRef _ nil)
1280 | not impl && nsroot nil == sUN "Nil" = Just []
1281 slist' p bnd (PApp _ (PRef _ cons) args)
1282 | nsroot cons == sUN "::",
1283 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1285 Just tl' <- slist' p bnd tl
1288 isImp (PImp {}) = True
1290 slist' _ _ tm = Nothing
1292 slist p bnd e | Just es <- slist' p bnd e = Just $
1293 case es of [] -> annotate AnnConstData $ text "[]"
1294 [x] -> enclose left right . group $
1296 xs -> (enclose left right .
1297 align . group . vsep .
1299 map (prettySe p bnd)) xs
1300 where left = (annotate AnnConstData (text "["))
1301 right = (annotate AnnConstData (text "]"))
1302 comma = (annotate AnnConstData (text ","))
1303 slist _ _ _ = Nothing
1305 pairElts :: PTerm -> Maybe [PTerm]
1306 pairElts (PPair _ _ x y) | Just elts <- pairElts y = Just (x:elts)
1307 | otherwise = Just [x, y]
1308 pairElts _ = Nothing
1310 natns = "Prelude.Nat."
1313 | show z == (natns++"Z") || show z == "Z" = Just 0
1314 snat p (PApp _ s [PExp {getTm=n}])
1315 | show s == (natns++"S") || show s == "S",
1320 bracket outer inner doc
1321 | inner > outer = lparen <> doc <> rparen
1324 kwd = annotate AnnKeyword . text
1326-- | Pretty-printer helper for the binding site of a name
1327bindingOf :: Name -- ^^ the bound name
1328 -> Bool -- ^^ whether the name is implicit
1329 -> Doc OutputAnnotation
1330bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1332-- | Pretty-printer helper for names that attaches the correct annotations
1333prettyName :: Bool -- ^^ whether to show namespaces
1334 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1335 -> Name -- ^^ the name to pprint
1336 -> Doc OutputAnnotation
1337prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1338 | otherwise = annotate (AnnName n Nothing Nothing) (text (strName n))
1339 where strName (UN n) = T.unpack n
1340 strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1341 | otherwise = strName n
1342 strName (MN i s) = T.unpack s
1343 strName other = show other
1346showCImp :: Bool -> PClause -> Doc OutputAnnotation
1347showCImp impl (PClause _ n l ws r w)
1348 = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1349 <+> text "where" <+> text (show w)
1352 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1353showCImp impl (PWith _ n l ws r w)
1354 = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1355 <+> braces (text (show w))
1358 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1361showDImp :: Bool -> PData -> Doc OutputAnnotation
1362showDImp impl (PDatadecl n ty cons)
1363 = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1364 (indent 2 $ vsep (map (\ (_, _, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1366showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1367showDecls i ds = vsep (map (showDeclImp i) ds)
1369showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1370showDeclImp i (PTy _ _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1371showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1372 indent 2 (vsep (map (showCImp i) cs))
1373showDeclImp _ (PData _ _ _ _ _ d) = showDImp True d
1374showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1375showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1376showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1377showDeclImp i (PClass _ _ _ cs n ps ds)
1378 = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1379showDeclImp i (PInstance _ _ cs n _ t _ ds)
1380 = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1381showDeclImp _ _ = text "..."
1382-- showDeclImp (PImport i) = "import " ++ i
1384instance Show (Doc OutputAnnotation) where
1385 show = flip (displayS . renderCompact) ""
1387getImps :: [PArg] -> [(Name, PTerm)]
1389getImps (PImp _ _ _ n tm : xs) = (n, tm) : getImps xs
1390getImps (_ : xs) = getImps xs
1392getExps :: [PArg] -> [PTerm]
1394getExps (PExp _ _ tm : xs) = tm : getExps xs
1395getExps (_ : xs) = getExps xs
1397getConsts :: [PArg] -> [PTerm]
1399getConsts (PConstraint _ _ tm : xs) = tm : getConsts xs
1400getConsts (_ : xs) = getConsts xs
1402getAll :: [PArg] -> [PTerm]
1407showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1408 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1409 -> Bool -- ^^ whether to show implicits
1410 -> Bool -- ^^ whether to colourise
1411 -> Name -- ^^ the term to show
1413showName ist bnd impl colour n = case ist of
1414 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1415 Nothing -> showbasic n
1416 where name = if impl then show n else showbasic n
1417 showbasic n@(UN _) = showCG n
1418 showbasic (MN _ s) = str s
1419 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1420 showbasic (SN s) = show s
1422 colourise n t = let ctxt' = fmap tt_ctxt ist in
1425 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1426 else colouriseBound t name
1427 | isDConName n ctxt -> colouriseData t name
1428 | isFnName n ctxt -> colouriseFun t name
1429 | isTConName n ctxt -> colouriseType t name
1430 -- The assumption is that if a name is not bound and does not exist in the
1431 -- global context, then we're somewhere in which implicit info has been lost
1432 -- (like error messages). Thus, unknown vars are colourised as implicits.
1433 | otherwise -> colouriseImplicit t name
1435showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1436 -> PTerm -- ^^ the term to show
1438showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
1440-- | Show a term with implicits, no colours
1441showTmImpls :: PTerm -> String
1442showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1445instance Sized PTerm where
1446 size (PQuote rawTerm) = size rawTerm
1447 size (PRef fc name) = size name
1448 size (PLam name ty bdy) = 1 + size ty + size bdy
1449 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1450 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1451 size (PTyped trm ty) = 1 + size trm + size ty
1452 size (PApp fc name args) = 1 + size args
1453 size (PAppBind fc name args) = 1 + size args
1454 size (PCase fc trm bdy) = 1 + size trm + size bdy
1455 size (PTrue fc _) = 1
1456 size (PFalse fc) = 1
1457 size (PRefl fc _) = 1
1458 size (PResolveTC fc) = 1
1459 size (PEq fc left right) = 1 + size left + size right
1460 size (PRewrite fc left right _) = 1 + size left + size right
1461 size (PPair fc _ left right) = 1 + size left + size right
1462 size (PDPair fs _ left ty right) = 1 + size left + size ty + size right
1463 size (PAlternative a alts) = 1 + size alts
1464 size (PHidden hidden) = size hidden
1465 size (PUnifyLog tm) = size tm
1466 size (PDisamb _ tm) = size tm
1467 size (PNoImplicits tm) = size tm
1469 size (PConstant const) = 1 + size const
1470 size Placeholder = 1
1471 size (PDoBlock dos) = 1 + size dos
1472 size (PIdiom fc term) = 1 + size term
1473 size (PReturn fc) = 1
1474 size (PMetavar name) = 1
1475 size (PProof tactics) = size tactics
1476 size (PElabError err) = size err
1477 size PImpossible = 1
1479getPArity :: PTerm -> Int
1480getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1483-- Return all names, free or globally bound, in the given term.
1485allNamesIn :: PTerm -> [Name]
1486allNamesIn tm = nub $ ni [] tm
1489 | not (n `elem` env) = [n]
1490 ni env (PPatvar _ n) = [n]
1491 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1492 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1493 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1494 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1495 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1496 ni env (PHidden tm) = ni env tm
1497 ni env (PEq _ l r) = ni env l ++ ni env r
1498 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1499 ni env (PTyped l r) = ni env l ++ ni env r
1500 ni env (PPair _ _ l r) = ni env l ++ ni env r
1501 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1502 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1503 ni env (PAlternative a ls) = concatMap (ni env) ls
1504 ni env (PUnifyLog tm) = ni env tm
1505 ni env (PDisamb _ tm) = ni env tm
1506 ni env (PNoImplicits tm) = ni env tm
1509-- Return all names defined in binders in the given term
1510boundNamesIn :: PTerm -> [Name]
1511boundNamesIn tm = nub $ ni tm
1513 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1514 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1515 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1516 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1517 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1518 ni (PPi _ n ty sc) = n : (ni ty ++ ni sc)
1519 ni (PEq _ l r) = ni l ++ ni r
1520 ni (PRewrite _ l r _) = ni l ++ ni r
1521 ni (PTyped l r) = ni l ++ ni r
1522 ni (PPair _ _ l r) = ni l ++ ni r
1523 ni (PDPair _ _ (PRef _ n) t r) = ni t ++ ni r
1524 ni (PDPair _ _ l t r) = ni l ++ ni t ++ ni r
1525 ni (PAlternative a as) = concatMap (ni) as
1526 ni (PHidden tm) = ni tm
1527 ni (PUnifyLog tm) = ni tm
1528 ni (PDisamb _ tm) = ni tm
1529 ni (PNoImplicits tm) = ni tm
1532-- Return names which are free in the given term.
1533namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1534namesIn uvars ist tm = nub $ ni [] tm
1537 | not (n `elem` env)
1538 = case lookupTy n (tt_ctxt ist) of
1540 _ -> if n `elem` (map fst uvars) then [n] else []
1541 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1542 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1543 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1544 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1545 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1546 ni env (PEq _ l r) = ni env l ++ ni env r
1547 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1548 ni env (PTyped l r) = ni env l ++ ni env r
1549 ni env (PPair _ _ l r) = ni env l ++ ni env r
1550 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1551 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1552 ni env (PAlternative a as) = concatMap (ni env) as
1553 ni env (PHidden tm) = ni env tm
1554 ni env (PUnifyLog tm) = ni env tm
1555 ni env (PDisamb _ tm) = ni env tm
1556 ni env (PNoImplicits tm) = ni env tm
1559-- Return which of the given names are used in the given term.
1561usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1562usedNamesIn vars ist tm = nub $ ni [] tm
1565 | n `elem` vars && not (n `elem` env)
1566 = case lookupTy n (tt_ctxt ist) of
1569 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1570 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1571 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1572 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1573 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1574 ni env (PEq _ l r) = ni env l ++ ni env r
1575 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1576 ni env (PTyped l r) = ni env l ++ ni env r
1577 ni env (PPair _ _ l r) = ni env l ++ ni env r
1578 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1579 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1580 ni env (PAlternative a as) = concatMap (ni env) as
1581 ni env (PHidden tm) = ni env tm
1582 ni env (PUnifyLog tm) = ni env tm
1583 ni env (PDisamb _ tm) = ni env tm
1584 ni env (PNoImplicits tm) = ni env tm
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list