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
39-- Data to pass to recursively called elaborators; e.g. for where blocks,
40-- paramaterised declarations, etc.
42data ElabInfo = EInfo { params :: [(Name, PTerm)],
43 inblock :: Ctxt [Name], -- names in the block, and their params
44 liftname :: Name -> Name,
45 namespace :: Maybe [String] }
47toplevel = EInfo [] emptyContext id Nothing
49eInfoNames :: ElabInfo -> [Name]
50eInfoNames info = map fst (params info) ++ M.keys (inblock info)
52data IOption = IOption { opt_logLevel :: Int,
54 opt_typeintype :: Bool,
56 opt_showimp :: Bool, -- ^^ show implicits
57 opt_errContext :: Bool,
62 opt_codegen :: Codegen,
63 opt_outputTy :: OutputType,
64 opt_ibcsubdir :: FilePath,
65 opt_importdirs :: [FilePath],
69 opt_cmdline :: [Opt], -- remember whole command line
71 opt_autoSolve :: Bool -- ^ automatically apply "solve" tactic in prover
75defaultOpts = IOption { opt_logLevel = 0
76 , opt_typecase = False
77 , opt_typeintype = False
80 , opt_errContext = False
83 , opt_nobanner = False
86 , opt_outputTy = Executable
94 , opt_autoSolve = True
97data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
99-- | The output mode in use
100data OutputMode = RawOutput | IdeSlave Integer deriving Show
102-- | How wide is the console?
103data ConsoleWidth = InfinitelyWide -- ^ Have pretty-printer assume that lines should not be broken
104 | ColsWide Int -- ^ Manually specified - must be positive
105 | AutomaticWidth -- ^ Attempt to determine width, or 80 otherwise
107-- | The global state used in the Idris monad
108data IState = IState {
109 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
110 idris_constraints :: [(UConstraint, FC)],
111 -- ^ A list of universe constraints and their corresponding source locations
112 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
113 idris_implicits :: Ctxt [PArg],
114 idris_statics :: Ctxt [Bool],
115 idris_classes :: Ctxt ClassInfo,
116 idris_dsls :: Ctxt DSL,
117 idris_optimisation :: Ctxt OptInfo,
118 idris_datatypes :: Ctxt TypeInfo,
119 idris_namehints :: Ctxt [Name],
120 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
121 -- ^ list of lhs/rhs, and a list of missing clauses
122 idris_flags :: Ctxt [FnOpt],
123 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
124 idris_calledgraph :: Ctxt [Name],
125 idris_docstrings :: Ctxt (Docstring, [(Name, Docstring)]),
126 idris_tyinfodata :: Ctxt TIData,
127 idris_totcheck :: [(FC, Name)], -- names to check totality on
128 idris_defertotcheck :: [(FC, Name)], -- names to check at the end
129 idris_options :: IOption,
131 idris_lineapps :: [((FilePath, Int), PTerm)],
132 -- ^ Full application LHS on source line
133 idris_metavars :: [(Name, (Maybe Name, Int, Bool))], -- ^ The currently defined but not proven metavariables
134 idris_coercions :: [Name],
135 idris_transforms :: [(Term, Term)],
136 idris_errRev :: [(Term, Term)],
137 syntax_rules :: [Syntax],
138 syntax_keywords :: [String],
139 imported :: [FilePath], -- ^ The imported modules
140 idris_scprims :: [(Name, (Int, PrimFn))],
141 idris_objs :: [(Codegen, FilePath)],
142 idris_libs :: [(Codegen, String)],
143 idris_cgflags :: [(Codegen, String)],
144 idris_hdrs :: [(Codegen, String)],
145 idris_imported :: [FilePath], -- ^ Imported ibc file names
146 proof_list :: [(Name, [String])],
148 parserWarnings :: [(FC, Err)],
149 lastParse :: Maybe Name,
150 indent_stack :: [Int],
151 brace_stack :: [Maybe Int],
152 lastTokenSpan :: Maybe FC, -- ^ What was the span of the latest token parsed?
153 hide_list :: [(Name, Maybe Accessibility)],
154 default_access :: Accessibility,
155 default_total :: Bool,
156 ibc_write :: [IBCWrite],
157 compiled_so :: Maybe String,
158 idris_dynamic_libs :: [DynamicLib],
159 idris_language_extensions :: [LanguageExt],
160 idris_outputmode :: OutputMode,
161 idris_colourRepl :: Bool,
162 idris_colourTheme :: ColourTheme,
163 idris_outh :: Handle,
164 idris_errorhandlers :: [Name], -- ^ Global error handlers
165 idris_nameIdx :: (Int, Ctxt (Int, Name)),
166 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
167 module_aliases :: M.Map [T.Text] [T.Text],
168 idris_consolewidth :: ConsoleWidth -- ^ How many chars wide is the console?
171data SizeChange = Smaller | Same | Bigger | Unknown
174deriving instance Binary SizeChange
175deriving instance NFData SizeChange
178type SCGEntry = (Name, [Maybe (Int, SizeChange)])
180data CGInfo = CGInfo { argsdef :: [Name],
181 calls :: [(Name, [[Name]])],
187deriving instance Binary CGInfo
188deriving instance NFData CGInfo
191primDefs = [sUN "unsafePerformPrimIO",
192 sUN "mkLazyForeignPrim",
196-- information that needs writing for the current module's .ibc file
197data IBCWrite = IBCFix FixDecl
201 | IBCInstance Bool Name Name
209 | IBCObj Codegen FilePath
210 | IBCLib Codegen String
211 | IBCCGFlag Codegen String
213 | IBCHeader Codegen String
214 | IBCAccess Name Accessibility
215 | IBCMetaInformation Name MetaInformation
216 | IBCTotal Name Totality
217 | IBCFlags Name [FnOpt]
218 | IBCTrans (Term, Term)
219 | IBCErrRev (Term, Term)
223 | IBCDef Name -- i.e. main context
224 | IBCNameHint (Name, Name)
225 | IBCLineApp FilePath Int PTerm
226 | IBCErrorHandler Name
227 | IBCFunctionErrorHandler Name Name Name
230-- | The initial state for the compiler
232idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
233 emptyContext emptyContext emptyContext emptyContext
234 emptyContext emptyContext emptyContext emptyContext
235 emptyContext emptyContext
236 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
237 [] [] Nothing [] Nothing [] [] Nothing [] Hidden False [] Nothing [] [] RawOutput
238 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
241-- | The monad for the main REPL - reading and processing files and updating
242-- global state (hence the IO inner monad).
243--type Idris = WriterT [Either String (IO ())] (State IState a))
244type Idris = StateT IState (ErrorT Err IO)
246-- Commands in the REPL
265 | ChangeDirectory FilePath
268 | Compile Codegen String
273 | AddProof (Maybe Name)
285 | DynamicLink FilePath
290 | CaseSplitAt Bool Int Name
291 | AddClauseFrom Bool Int Name
292 | AddProofClauseFrom Bool Int Name
293 | AddMissing Bool Int Name
294 | MakeWith Bool Int Name
295 | DoProofSearch Bool Int Name [Name]
299 | SetColour ColourType IdrisColour
303 | SetConsoleWidth ConsoleWidth
306data Opt = Filename String
318 | NoBuiltins -- only for the really primitive stuff!
344 | OutputTy OutputType
345 | Extension LanguageExt
346 | InterpretScript String
347 | TargetTriple String
352 | AutoWidth -- ^ Automatically adjust terminal width
353 | AutoSolve -- ^ Automatically issue "solve" tactic in interactive prover
356-- Parsed declarations
358data Fixity = Infixl { prec :: Int }
359 | Infixr { prec :: Int }
360 | InfixN { prec :: Int }
361 | PrefixN { prec :: Int }
364deriving instance Binary Fixity
365deriving instance NFData Fixity
368instance Show Fixity where
369 show (Infixl i) = "infixl " ++ show i
370 show (Infixr i) = "infixr " ++ show i
371 show (InfixN i) = "infix " ++ show i
372 show (PrefixN i) = "prefix " ++ show i
374data FixDecl = Fix Fixity String
377instance Show FixDecl where
378 show (Fix f s) = show f ++ " " ++ s
381deriving instance Binary FixDecl
382deriving instance NFData FixDecl
385instance Ord FixDecl where
386 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
389data Static = Static | Dynamic
392deriving instance Binary Static
393deriving instance NFData Static
396-- Mark bindings with their explicitness, and laziness
397data Plicity = Imp { pargopts :: [ArgOpt],
400 | Exp { pargopts :: [ArgOpt],
403 | Constraint { pargopts :: [ArgOpt],
405 | TacImp { pargopts :: [ArgOpt],
410plazy :: Plicity -> Bool
411plazy tm = Lazy `elem` pargopts tm
414deriving instance Binary Plicity
415deriving instance NFData Plicity
418impl = Imp [Lazy] Dynamic False
419expl = Exp [] Dynamic False
420expl_param = Exp [] Dynamic True
421constraint = Constraint [] Dynamic
422tacimpl t = TacImp [] Dynamic t
424data FnOpt = Inlinable -- always evaluate when simplifying
425 | TotalFn | PartialFn | CoveringFn
426 | Coinductive | AssertTotal
427 | Dictionary -- type class dictionary, eval only when
428 -- a function argument, and further evaluation resutls
429 | Implicit -- implicit coercion
430 | CExport String -- export, with a C name
431 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
432 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
433 | Reflection -- a reflecting function, compile-time only
434 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
437deriving instance Binary FnOpt
438deriving instance NFData FnOpt
443inlinable :: FnOpts -> Bool
444inlinable = elem Inlinable
446dictionary :: FnOpts -> Bool
447dictionary = elem Dictionary
450-- | Data declaration options
451data DataOpt = Codata -- Set if the the data-type is coinductive
452 | DefaultEliminator -- Set if an eliminator should be generated for data type
456type DataOpts = [DataOpt]
458-- | Type provider - what to provide
459data ProvideWhat = ProvTerm -- ^ only allow providing terms
460 | ProvPostulate -- ^ only allow postulates
461 | ProvAny -- ^ either is ok
464-- | Top-level declarations such as compiler directives, definitions,
465-- datatypes and typeclasses.
467 = PFix FC Fixity [String] -- ^ Fixity declaration
468 | PTy Docstring [(Name, Docstring)] SyntaxInfo FC FnOpts Name t -- ^ Type declaration
469 | PPostulate Docstring SyntaxInfo FC FnOpts Name t -- ^ Postulate
470 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
471 | PCAF FC Name t -- ^ Top level constant
472 | PData Docstring [(Name, Docstring)] SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
473 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
474 | PNamespace String [PDecl' t] -- ^ New namespace
475 | PRecord Docstring SyntaxInfo FC Name t Docstring Name t -- ^ Record declaration
476 | PClass Docstring SyntaxInfo FC
479 [(Name, t)] -- parameters
480 [PDecl' t] -- declarations
481 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
482 -- class name, parameters, method declarations
483 | PInstance SyntaxInfo FC [t] -- constraints
486 t -- full instance type
487 (Maybe Name) -- explicit name
489 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
490 -- class name, parameters, full instance type, optional explicit name, and definitions
491 | PDSL Name (DSL' t) -- ^ DSL declaration
492 | PSyntax FC Syntax -- ^ Syntax definition
493 | PMutual FC [PDecl' t] -- ^ Mutual block
494 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
495 | PProvider SyntaxInfo FC ProvideWhat Name t t -- ^ Type provider. The first t is the type, the second is the term
496 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
497 -- bool is True, lhs and rhs must be convertible
500deriving instance Binary PDecl'
501deriving instance NFData PDecl'
504-- For elaborator state
505type ElabD a = Elab' [PDecl] a
507-- | One clause of a top-level definition. Term arguments to constructors are:
509-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
511-- 2. The list of extra 'with' patterns
513-- 3. The right-hand side
515-- 4. The where block (PDecl' t)
517data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
518 | PWith FC Name t [t] t [PDecl' t]
519 | PClauseR FC [t] t [PDecl' t]
520 | PWithR FC [t] t [PDecl' t]
523deriving instance Binary PClause'
524deriving instance NFData PClause'
528data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
529 d_tcon :: t, -- ^ Type constructor
530 d_cons :: [(Docstring, [(Name, Docstring)], Name, t, FC, [Name])] -- ^ Constructors
532 -- ^ Data declaration
533 | PLaterdecl { d_name :: Name, d_tcon :: t }
534 -- ^ "Placeholder" for data whose constructors are defined later
537deriving instance Binary PData'
538deriving instance NFData PData'
541-- Handy to get a free function for applying PTerm -> PTerm functions
542-- across a program, by deriving Functor
544type PDecl = PDecl' PTerm
545type PData = PData' PTerm
546type PClause = PClause' PTerm
548-- get all the names declared in a decl
550declared :: PDecl -> [Name]
551declared (PFix _ _ _) = []
552declared (PTy _ _ _ _ _ n t) = [n]
553declared (PPostulate _ _ _ _ n t) = [n]
554declared (PClauses _ _ n _) = [] -- not a declaration
555declared (PCAF _ n _) = [n]
556declared (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
557 where fstt (_, _, a, _, _, _) = a
558declared (PData _ _ _ _ _ (PLaterdecl n _)) = [n]
559declared (PParams _ _ ds) = concatMap declared ds
560declared (PNamespace _ ds) = concatMap declared ds
561declared (PRecord _ _ _ n _ _ c _) = [n, c]
562declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
563declared (PInstance _ _ _ _ _ _ _ _) = []
564declared (PDSL n _) = [n]
565declared (PSyntax _ _) = []
566declared (PMutual _ ds) = concatMap declared ds
567declared (PDirective _) = []
569-- get the names declared, not counting nested parameter blocks
570tldeclared :: PDecl -> [Name]
571tldeclared (PFix _ _ _) = []
572tldeclared (PTy _ _ _ _ _ n t) = [n]
573tldeclared (PPostulate _ _ _ _ n t) = [n]
574tldeclared (PClauses _ _ n _) = [] -- not a declaration
575tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
576tldeclared (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
577 where fstt (_, _, a, _, _, _) = a
578tldeclared (PParams _ _ ds) = []
579tldeclared (PMutual _ ds) = concatMap tldeclared ds
580tldeclared (PNamespace _ ds) = concatMap tldeclared ds
581tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
582tldeclared (PInstance _ _ _ _ _ _ _ _) = []
585defined :: PDecl -> [Name]
586defined (PFix _ _ _) = []
587defined (PTy _ _ _ _ _ n t) = []
588defined (PPostulate _ _ _ _ n t) = []
589defined (PClauses _ _ n _) = [n] -- not a declaration
590defined (PCAF _ n _) = [n]
591defined (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
592 where fstt (_, _, a, _, _, _) = a
593defined (PData _ _ _ _ _ (PLaterdecl n _)) = []
594defined (PParams _ _ ds) = concatMap defined ds
595defined (PNamespace _ ds) = concatMap defined ds
596defined (PRecord _ _ _ n _ _ c _) = [n, c]
597defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
598defined (PInstance _ _ _ _ _ _ _ _) = []
599defined (PDSL n _) = [n]
600defined (PSyntax _ _) = []
601defined (PMutual _ ds) = concatMap defined ds
602defined (PDirective _) = []
605updateN :: [(Name, Name)] -> Name -> Name
606updateN ns n | Just n' <- lookup n ns = n'
609updateNs :: [(Name, Name)] -> PTerm -> PTerm
611updateNs ns t = mapPT updateRef t
612 where updateRef (PRef fc f) = PRef fc (updateN ns f)
615-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
617-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
618-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
619-- where updateCNs ns (PClause n l ts r ds)
620-- = PClause (updateN ns n) (fmap (updateNs ns) l)
621-- (map (fmap (updateNs ns)) ts)
622-- (fmap (updateNs ns) r)
623-- (map (updateDNs ns) ds)
626data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show)
628-- | High level language terms
629data PTerm = PQuote Raw
631 | PInferRef FC Name -- ^ A name to be defined later
633 | PLam Name PTerm PTerm
634 | PPi Plicity Name PTerm PTerm
635 | PLet Name PTerm PTerm PTerm
636 | PTyped PTerm PTerm -- ^ Term with explicit type
637 | PApp FC PTerm [PArg]
638 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
639 | PMatchApp FC Name -- ^ Make an application by type matching
640 | PCase FC PTerm [(PTerm, PTerm)]
646 | PRewrite FC PTerm PTerm (Maybe PTerm)
647 | PPair FC PunInfo PTerm PTerm
648 | PDPair FC PunInfo PTerm PTerm PTerm
649 | PAlternative Bool [PTerm] -- True if only one may work
650 | PHidden PTerm -- ^ Irrelevant or hidden pattern
652 | PGoal FC PTerm Name PTerm
659 | PProof [PTactic] -- ^ Proof script
660 | PTactics [PTactic] -- ^ As PProof, but no auto solving
661 | PElabError Err -- ^ Error to report on elaboration
662 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
663 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
664 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
665 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
666 | PNoImplicits PTerm -- ^ never run implicit converions on the term
671deriving instance Binary PTerm
672deriving instance NFData PTerm
675mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
676mapPT f t = f (mpt t) where
677 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
678 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
679 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
680 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
682 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
683 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
684 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
685 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
686 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
687 mpt (PPair fc p l r) = PPair fc p (mapPT f l) (mapPT f r)
688 mpt (PDPair fc p l t r) = PDPair fc p (mapPT f l) (mapPT f t) (mapPT f r)
689 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
690 mpt (PHidden t) = PHidden (mapPT f t)
691 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
692 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
693 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
694 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
695 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
696 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
697 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
701data PTactic' t = Intro [Name] | Intros | Focus Name
702 | Refine Name [Bool] | Rewrite t
706 | LetTac Name t | LetTacTy Name t t
707 | Exact t | Compute | Trivial | TCInstance
708 | ProofSearch (Maybe Name) [Name]
711 | ProofState | ProofTerm | Undo
712 | Try (PTactic' t) (PTactic' t)
713 | TSeq (PTactic' t) (PTactic' t)
714 | ApplyTactic t -- see Language.Reflection module
718 | GoalType String (PTactic' t)
722 deriving (Show, Eq, Functor)
724deriving instance Binary PTactic'
725deriving instance NFData PTactic'
727instance Sized a => Sized (PTactic' a) where
728 size (Intro nms) = 1 + size nms
730 size (Focus nm) = 1 + size nm
731 size (Refine nm bs) = 1 + size nm + length bs
732 size (Rewrite t) = 1 + size t
733 size (Induction t) = 1 + size t
734 size (LetTac nm t) = 1 + size nm + size t
735 size (Exact t) = 1 + size t
743 size (Try l r) = 1 + size l + size r
744 size (TSeq l r) = 1 + size l + size r
745 size (ApplyTactic t) = 1 + size t
746 size (Reflect t) = 1 + size t
747 size (Fill t) = 1 + size t
751type PTactic = PTactic' PTerm
753data PDo' t = DoExp FC t
755 | DoBindP FC t t [(t,t)]
758 deriving (Eq, Functor)
760deriving instance Binary PDo'
761deriving instance NFData PDo'
764instance Sized a => Sized (PDo' a) where
765 size (DoExp fc t) = 1 + size fc + size t
766 size (DoBind fc nm t) = 1 + size fc + size nm + size t
767 size (DoBindP fc l r alts) = 1 + size fc + size l + size r + size alts
768 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
769 size (DoLetP fc l r) = 1 + size fc + size l + size r
773-- The priority gives a hint as to elaboration order. Best to elaborate
774-- things early which will help give a more concrete type to other
775-- variables, e.g. a before (interpTy a).
777data PArg' t = PImp { priority :: Int,
778 machine_inf :: Bool, -- true if the machine inferred it
780 pname :: Name, getTm :: t }
781 | PExp { priority :: Int,
785 | PConstraint { priority :: Int,
789 | PTacImplicit { priority :: Int,
794 deriving (Show, Eq, Functor)
796data ArgOpt = Lazy | HideDisplay
799lazyarg :: PArg' t -> Bool
800lazyarg tm = Lazy `elem` argopts tm
802instance Sized a => Sized (PArg' a) where
803 size (PImp p _ l nm trm) = 1 + size nm + size trm
804 size (PExp p l nm trm) = 1 + size nm + size trm
805 size (PConstraint p l nm trm) = 1 + size nm +size nm + size trm
806 size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
809deriving instance Binary PArg'
810deriving instance NFData PArg'
813pimp n t mach = PImp 1 mach [Lazy] n t
814pexp t = PExp 1 [] (sMN 0 "arg") t
815pconst t = PConstraint 1 [] (sMN 0 "carg") t
816ptacimp n s t = PTacImplicit 2 [Lazy] n s t
818type PArg = PArg' PTerm
822data ClassInfo = CI { instanceName :: Name,
823 class_methods :: [(Name, (FnOpts, PTerm))],
824 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
825 class_default_superclasses :: [PDecl],
826 class_params :: [Name],
827 class_instances :: [Name] }
830deriving instance Binary ClassInfo
831deriving instance NFData ClassInfo
834-- Type inference data
836data TIData = TIPartial -- ^ a function with a partially defined type
837 | TISolution [Term] -- ^ possible solutions to a metavariable in a type
840-- An argument is conditionally forceable iff its forceability
841-- depends on the collapsibility of the whole type.
842data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
845deriving instance Binary Forceability
846deriving instance NFData Forceability
849data OptInfo = Optimise { collapsible :: Bool,
851 -- The following should actually be (IntMap Forceability)
852 -- but the corresponding Binary instance seems to be broken.
853 -- Let's store a list and convert it to IntMap whenever needed.
854 forceable :: [(Int, Forceability)],
858deriving instance Binary OptInfo
859deriving instance NFData OptInfo
863data TypeInfo = TI { con_names :: [Name],
865 data_opts :: DataOpts,
869deriving instance Binary TypeInfo
870deriving instance NFData TypeInfo
873-- Syntactic sugar info
875data DSL' t = DSL { dsl_bind :: t,
880 index_first :: Maybe t,
881 index_next :: Maybe t,
882 dsl_lambda :: Maybe t,
885 deriving (Show, Functor)
887deriving instance Binary DSL'
888deriving instance NFData DSL'
893data SynContext = PatternSyntax | TermSyntax | AnySyntax
896deriving instance Binary SynContext
897deriving instance NFData SynContext
900data Syntax = Rule [SSymbol] PTerm SynContext
903deriving instance Binary Syntax
904deriving instance NFData Syntax
907data SSymbol = Keyword Name
914deriving instance Binary SSymbol
915deriving instance NFData SSymbol
918initDSL = DSL (PRef f (sUN ">>="))
919 (PRef f (sUN "return"))
921 (PRef f (sUN "pure"))
927 where f = fileFC "(builtin)"
929data Using = UImplicit Name PTerm
930 | UConstraint Name [Name]
933deriving instance Binary Using
934deriving instance NFData Using
937data SyntaxInfo = Syn { using :: [Using],
938 syn_params :: [(Name, PTerm)],
939 syn_namespace :: [String],
941 decoration :: Name -> Name,
943 implicitAllowed :: Bool,
947deriving instance NFData SyntaxInfo
948deriving instance Binary SyntaxInfo
951defaultSyntax = Syn [] [] [] [] id False False initDSL
953expandNS :: SyntaxInfo -> Name -> Name
954expandNS syn n@(NS _ _) = n
955expandNS syn n = case syn_namespace syn of
960-- For inferring types of things
964inferTy = sMN 0 "__Infer"
965inferCon = sMN 0 "__infer"
966inferDecl = PDatadecl inferTy
968 [(emptyDocstring, [], inferCon, PPi impl (sMN 0 "iType") PType (
969 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
970 (PRef bi inferTy)), bi, [])]
973infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
974infP = P (TCon 6 0) inferTy (TType (UVal 0))
976getInferTerm, getInferType :: Term -> Term
977getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
978getInferTerm (App (App _ _) tm) = tm
979getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
981getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
982 where toTy (Lam t) = Pi t
983 toTy (PVar t) = PVTy t
985getInferType (App (App _ ty) _) = ty
989-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
991primNames = [unitTy, unitCon,
992 falseTy, pairTy, pairCon,
993 eqTy, eqCon, inferTy, inferCon]
995unitTy = sMN 0 "__Unit"
996unitCon = sMN 0 "__II"
997unitDecl = PDatadecl unitTy PType
998 [(emptyDocstring, [], unitCon, PRef bi unitTy, bi, [])]
999unitOpts = [DefaultEliminator]
1001falseTy = sMN 0 "__False"
1002falseDecl = PDatadecl falseTy PType []
1005pairTy = sMN 0 "__Pair"
1006pairCon = sMN 0 "__MkPair"
1007pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
1008 [(emptyDocstring, [], pairCon, PPi impl (n "A") PType (
1009 PPi impl (n "B") PType (
1010 PPi expl (n "a") (PRef bi (n "A")) (
1011 PPi expl (n "b") (PRef bi (n "B"))
1012 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
1013 pexp (PRef bi (n "B"))])))), bi, [])]
1019eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
1020 (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
1022 [(emptyDocstring, [], eqCon, PPi impl (n "A") PType (
1023 PPi impl (n "x") (PRef bi (n "A"))
1024 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
1025 pimp (n "B") Placeholder False,
1026 pexp (PRef bi (n "x")),
1027 pexp (PRef bi (n "x"))])), bi, [])]
1031elimName = sUN "__Elim"
1032elimMethElimTy = sUN "__elimTy"
1033elimMethElim = sUN "elim"
1034elimDecl = PClass (parseDocstring . T.pack $ "Type class for eliminators") defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
1035 [PTy emptyDocstring [] defaultSyntax bi [TotalFn] elimMethElimTy PType,
1036 PTy emptyDocstring [] defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
1038-- Defined in builtins.idr
1039sigmaTy = sUN "Exists"
1040existsCon = sUN "Ex_intro"
1042piBind :: [(Name, PTerm)] -> PTerm -> PTerm
1043piBind = piBindp expl
1045piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
1047piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
1050-- Pretty-printing declarations and terms
1052-- These "show" instances render to an absurdly wide screen because inserted line breaks
1053-- could interfere with interactive editing, which calls "show".
1055instance Show PTerm where
1056 showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
1058instance Show PDecl where
1059 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
1061instance Show PClause where
1062 showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
1064instance Show PData where
1065 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
1067instance Pretty PTerm OutputAnnotation where
1068 pretty = prettyImp False
1070-- | Colourise annotations according to an Idris state. It ignores the names
1071-- in the annotation, as there's no good way to show extended information on a
1073consoleDecorate :: IState -> OutputAnnotation -> String -> String
1074consoleDecorate ist _ | not (idris_colourRepl ist) = id
1075consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1076 in colouriseData theme
1077consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1078 in colouriseType theme
1079consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1080consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1081consoleDecorate ist AnnKeyword = colouriseKeyword (idris_colourTheme ist)
1082consoleDecorate ist (AnnName n _ _ _) = let ctxt = tt_ctxt ist
1083 theme = idris_colourTheme ist
1085 _ | isDConName n ctxt -> colouriseData theme
1086 _ | isFnName n ctxt -> colouriseFun theme
1087 _ | isTConName n ctxt -> colouriseType theme
1088 _ | otherwise -> id -- don't colourise unknown names
1089consoleDecorate ist (AnnFC _) = id
1090consoleDecorate ist (AnnTextFmt fmt) = Idris.Colours.colourise (colour fmt)
1091 where colour BoldText = IdrisColour Nothing True False True False
1092 colour UnderlineText = IdrisColour Nothing True True False False
1093 colour ItalicText = IdrisColour Nothing True False False True
1095-- | Pretty-print a high-level closed Idris term
1096prettyImp :: Bool -- ^^ whether to show implicits
1097 -> PTerm -- ^^ the term to pretty-print
1098 -> Doc OutputAnnotation
1099prettyImp impl = pprintPTerm impl [] []
1101-- | Pretty-print a high-level Idris term in some bindings context
1102pprintPTerm :: Bool -- ^^ whether to show implicits
1103 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1104 -> [Name] -- ^^ names to always show in pi, even if not used
1105 -> PTerm -- ^^ the term to pretty-print
1106 -> Doc OutputAnnotation
1107pprintPTerm impl bnd docArgs = prettySe 10 bnd
1109 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1110 prettySe p bnd (PQuote r) =
1111 text "![" <> pretty r <> text "]"
1112 prettySe p bnd (PPatvar fc n) = pretty n
1114 | Just str <- slist p bnd e = str
1115 | Just n <- snat p e = annotate AnnConstData (text (show n))
1116 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1117 prettySe p bnd (PLam n ty sc) =
1118 bracket p 2 . group . align . hang 2 $
1119 text "\\" <> bindingOf n False <+> text "=>" <$>
1120 prettySe 10 ((n, False):bnd) sc
1121 prettySe p bnd (PLet n ty v sc) =
1123 kwd "let" <+> bindingOf n False <+> text "=" </>
1124 prettySe 10 bnd v <+> kwd "in" </>
1125 prettySe 10 ((n, False):bnd) sc
1126 prettySe p bnd (PPi (Exp l s _) n ty sc)
1127 | n `elem` allNamesIn sc || impl || n `elem` docArgs =
1128 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1129 bracket p 2 . group $
1130 enclose open rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1131 st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
1133 bracket p 2 . group $
1134 group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
1138 Static -> text "[static]" <> space
1140 prettySe p bnd (PPi (Imp l s _) n ty sc)
1142 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1144 open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1145 st <> text "->" </> prettySe 10 ((n, True):bnd) sc
1146 | otherwise = prettySe 10 ((n, True):bnd) sc
1150 Static -> text "[static]" <> space
1152 prettySe p bnd (PPi (Constraint _ _) n ty sc) =
1154 prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
1155 prettySe p bnd (PPi (TacImp _ _ s) n ty sc) =
1157 lbrace <> kwd "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1158 rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
1159 prettySe p bnd (PApp _ (PRef _ f) args) -- normal names, no explicit args
1160 | UN nm <- basename f
1161 , not impl && null (getExps args) = if isAlpha (thead nm)
1162 then prettyName impl bnd f
1163 else enclose lparen rparen $
1164 prettyName impl bnd f
1165 prettySe p bnd (PAppBind _ (PRef _ f) [])
1166 | not impl = text "!" <> prettyName impl bnd f
1167 prettySe p bnd (PApp _ (PRef _ op) args)
1168 | UN nm <- basename op
1170 (not impl) && (not $ isAlpha (thead nm)) =
1171 case getExps args of
1172 [] -> enclose lparen rparen opName
1173 [x] -> group (enclose lparen rparen opName <$> group (prettySe 0 bnd x))
1174 [l,r] -> bracket p 1 $ inFix l r
1175 (l:r:rest) -> bracket p 1 $
1176 enclose lparen rparen (inFix l r) <+>
1177 align (group (vsep (map (prettyArgSe bnd) rest)))
1178 where opName = prettyName impl bnd op
1179 inFix l r = align . group $
1180 (prettySe 1 bnd l <+> opName) <$> group (prettySe 0 bnd r)
1181 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1182 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1183 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1184 prettySe p bnd (PApp _ f as) =
1185 let args = getExps as
1186 fp = prettySe 1 bnd f
1188 bracket p 1 . group $
1192 else fp <+> align (vsep (map (prettyArgS bnd) as))
1195 else fp <+> align (vsep (map (prettyArgSe bnd) args))
1196 prettySe p bnd (PCase _ scr opts) =
1197 kwd "case" <+> prettySe 10 bnd scr <+> kwd "of" <> prettyBody
1199 prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1201 sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1202 prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1203 prettySe p bnd (PRefl _ _) = annName eqCon $ text "refl"
1204 prettySe p bnd (PResolveTC _) = text "resolvetc"
1205 prettySe p bnd (PTrue _ IsType) = annName unitTy $ text "()"
1206 prettySe p bnd (PTrue _ IsTerm) = annName unitCon $ text "()"
1207 prettySe p bnd (PTrue _ TypeOrTerm) = text "()"
1208 prettySe p bnd (PFalse _) = annName falseTy $ text "_|_"
1209 prettySe p bnd (PEq _ l r) =
1210 bracket p 2 . align . group $
1211 prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
1212 where eq = annName eqTy (text "=")
1213 prettySe p bnd (PRewrite _ l r _) =
1215 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1216 prettySe p bnd (PTyped l r) =
1217 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1218 prettySe p bnd pair@(PPair _ pun _ _) -- flatten tuples to the right, like parser
1219 | Just elts <- pairElts pair = enclose (ann lparen) (ann rparen) .
1220 align . group . vsep . punctuate (ann comma) $
1221 map (prettySe 10 bnd) elts
1222 where ann = case pun of
1224 IsType -> annName pairTy
1225 IsTerm -> annName pairCon
1226 prettySe p bnd (PDPair _ TypeOrTerm l t r) =
1227 lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1228 prettySe p bnd (PDPair _ IsType (PRef _ n) t r) =
1229 annName sigmaTy lparen <>
1230 bindingOf n False <+>
1231 annName sigmaTy (text "**") <+>
1232 prettySe 10 ((n, False):bnd) r <>
1233 annName sigmaTy rparen
1234 prettySe p bnd (PDPair _ IsType l t r) =
1235 annName sigmaTy lparen <>
1236 prettySe 10 bnd l <+>
1237 annName sigmaTy (text "**") <+>
1238 prettySe 10 bnd r <>
1239 annName sigmaTy rparen
1240 prettySe p bnd (PDPair _ IsTerm l t r) =
1241 annName existsCon lparen <>
1242 prettySe 10 bnd l <+>
1243 annName existsCon (text "**") <+>
1244 prettySe 10 bnd r <>
1245 annName existsCon rparen
1246 prettySe p bnd (PAlternative a as) =
1247 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1250 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1251 prettySe p bnd PType = annotate AnnConstType $ text "Type"
1252 prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1253 where annot (AType _) = AnnConstType
1254 annot StrType = AnnConstType
1255 annot PtrType = AnnConstType
1256 annot VoidType = AnnConstType
1257 annot _ = AnnConstData
1258 -- XXX: add pretty for tactics
1259 prettySe p bnd (PProof ts) =
1260 text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1261 prettySe p bnd (PTactics ts) =
1262 text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1263 prettySe p bnd (PMetavar n) = text "?" <> pretty n
1264 prettySe p bnd (PReturn f) = kwd "return"
1265 prettySe p bnd PImpossible = kwd "impossible"
1266 prettySe p bnd Placeholder = text "_"
1267 prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1268 prettySe p bnd (PElabError s) = pretty s
1270 prettySe p bnd _ = text "test"
1272 prettyArgS bnd (PImp _ _ _ n tm) = prettyArgSi bnd (n, tm)
1273 prettyArgS bnd (PExp _ _ _ tm) = prettyArgSe bnd tm
1274 prettyArgS bnd (PConstraint _ _ _ tm) = prettyArgSc bnd tm
1275 prettyArgS bnd (PTacImplicit _ _ n _ tm) = prettyArgSti bnd (n, tm)
1277 prettyArgSe bnd arg = prettySe 0 bnd arg
1278 prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1279 prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1280 prettyArgSti bnd (n, val) = lbrace <> kwd "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1282 annName :: Name -> Doc OutputAnnotation -> Doc OutputAnnotation
1283 annName n = annotate (AnnName n Nothing Nothing Nothing)
1285 basename :: Name -> Name
1286 basename (NS n _) = basename n
1289 slist' p bnd (PApp _ (PRef _ nil) _)
1290 | not impl && nsroot nil == sUN "Nil" = Just []
1291 slist' p bnd (PRef _ nil)
1292 | not impl && nsroot nil == sUN "Nil" = Just []
1293 slist' p bnd (PApp _ (PRef _ cons) args)
1294 | nsroot cons == sUN "::",
1295 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1297 Just tl' <- slist' p bnd tl
1300 isImp (PImp {}) = True
1302 slist' _ _ tm = Nothing
1304 slist p bnd e | Just es <- slist' p bnd e = Just $
1305 case es of [] -> annotate AnnConstData $ text "[]"
1306 [x] -> enclose left right . group $
1308 xs -> (enclose left right .
1309 align . group . vsep .
1311 map (prettySe p bnd)) xs
1312 where left = (annotate AnnConstData (text "["))
1313 right = (annotate AnnConstData (text "]"))
1314 comma = (annotate AnnConstData (text ","))
1315 slist _ _ _ = Nothing
1317 pairElts :: PTerm -> Maybe [PTerm]
1318 pairElts (PPair _ _ x y) | Just elts <- pairElts y = Just (x:elts)
1319 | otherwise = Just [x, y]
1320 pairElts _ = Nothing
1322 natns = "Prelude.Nat."
1325 | show z == (natns++"Z") || show z == "Z" = Just 0
1326 snat p (PApp _ s [PExp {getTm=n}])
1327 | show s == (natns++"S") || show s == "S",
1332 bracket outer inner doc
1333 | inner > outer = lparen <> doc <> rparen
1336 kwd = annotate AnnKeyword . text
1338-- | Pretty-printer helper for the binding site of a name
1339bindingOf :: Name -- ^^ the bound name
1340 -> Bool -- ^^ whether the name is implicit
1341 -> Doc OutputAnnotation
1342bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1344-- | Pretty-printer helper for names that attaches the correct annotations
1345prettyName :: Bool -- ^^ whether to show namespaces
1346 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1347 -> Name -- ^^ the name to pprint
1348 -> Doc OutputAnnotation
1349prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1350 | otherwise = annotate (AnnName n Nothing Nothing Nothing) (text (strName n))
1351 where strName (UN n) = T.unpack n
1352 strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1353 | otherwise = strName n
1354 strName (MN i s) = T.unpack s
1355 strName other = show other
1358showCImp :: Bool -> PClause -> Doc OutputAnnotation
1359showCImp impl (PClause _ n l ws r w)
1360 = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1361 <+> text "where" <+> text (show w)
1364 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1365showCImp impl (PWith _ n l ws r w)
1366 = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1367 <+> braces (text (show w))
1370 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1373showDImp :: Bool -> PData -> Doc OutputAnnotation
1374showDImp impl (PDatadecl n ty cons)
1375 = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1376 (indent 2 $ vsep (map (\ (_, _, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1378showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1379showDecls i ds = vsep (map (showDeclImp i) ds)
1381showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1382showDeclImp i (PTy _ _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1383showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1384 indent 2 (vsep (map (showCImp i) cs))
1385showDeclImp _ (PData _ _ _ _ _ d) = showDImp True d
1386showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1387showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1388showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1389showDeclImp i (PClass _ _ _ cs n ps ds)
1390 = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1391showDeclImp i (PInstance _ _ cs n _ t _ ds)
1392 = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1393showDeclImp _ _ = text "..."
1394-- showDeclImp (PImport i) = "import " ++ i
1396instance Show (Doc OutputAnnotation) where
1397 show = flip (displayS . renderCompact) ""
1399getImps :: [PArg] -> [(Name, PTerm)]
1401getImps (PImp _ _ _ n tm : xs) = (n, tm) : getImps xs
1402getImps (_ : xs) = getImps xs
1404getExps :: [PArg] -> [PTerm]
1406getExps (PExp _ _ _ tm : xs) = tm : getExps xs
1407getExps (_ : xs) = getExps xs
1409getConsts :: [PArg] -> [PTerm]
1411getConsts (PConstraint _ _ _ tm : xs) = tm : getConsts xs
1412getConsts (_ : xs) = getConsts xs
1414getAll :: [PArg] -> [PTerm]
1419showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1420 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1421 -> Bool -- ^^ whether to show implicits
1422 -> Bool -- ^^ whether to colourise
1423 -> Name -- ^^ the term to show
1425showName ist bnd impl colour n = case ist of
1426 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1427 Nothing -> showbasic n
1428 where name = if impl then show n else showbasic n
1429 showbasic n@(UN _) = showCG n
1430 showbasic (MN _ s) = str s
1431 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1432 showbasic (SN s) = show s
1434 colourise n t = let ctxt' = fmap tt_ctxt ist in
1437 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1438 else colouriseBound t name
1439 | isDConName n ctxt -> colouriseData t name
1440 | isFnName n ctxt -> colouriseFun t name
1441 | isTConName n ctxt -> colouriseType t name
1442 -- The assumption is that if a name is not bound and does not exist in the
1443 -- global context, then we're somewhere in which implicit info has been lost
1444 -- (like error messages). Thus, unknown vars are colourised as implicits.
1445 | otherwise -> colouriseImplicit t name
1447showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1448 -> PTerm -- ^^ the term to show
1450showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
1452-- | Show a term with implicits, no colours
1453showTmImpls :: PTerm -> String
1454showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1457instance Sized PTerm where
1458 size (PQuote rawTerm) = size rawTerm
1459 size (PRef fc name) = size name
1460 size (PLam name ty bdy) = 1 + size ty + size bdy
1461 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1462 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1463 size (PTyped trm ty) = 1 + size trm + size ty
1464 size (PApp fc name args) = 1 + size args
1465 size (PAppBind fc name args) = 1 + size args
1466 size (PCase fc trm bdy) = 1 + size trm + size bdy
1467 size (PTrue fc _) = 1
1468 size (PFalse fc) = 1
1469 size (PRefl fc _) = 1
1470 size (PResolveTC fc) = 1
1471 size (PEq fc left right) = 1 + size left + size right
1472 size (PRewrite fc left right _) = 1 + size left + size right
1473 size (PPair fc _ left right) = 1 + size left + size right
1474 size (PDPair fs _ left ty right) = 1 + size left + size ty + size right
1475 size (PAlternative a alts) = 1 + size alts
1476 size (PHidden hidden) = size hidden
1477 size (PUnifyLog tm) = size tm
1478 size (PDisamb _ tm) = size tm
1479 size (PNoImplicits tm) = size tm
1481 size (PConstant const) = 1 + size const
1482 size Placeholder = 1
1483 size (PDoBlock dos) = 1 + size dos
1484 size (PIdiom fc term) = 1 + size term
1485 size (PReturn fc) = 1
1486 size (PMetavar name) = 1
1487 size (PProof tactics) = size tactics
1488 size (PElabError err) = size err
1489 size PImpossible = 1
1491getPArity :: PTerm -> Int
1492getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1495-- Return all names, free or globally bound, in the given term.
1497allNamesIn :: PTerm -> [Name]
1498allNamesIn tm = nub $ ni [] tm
1499 where -- TODO THINK added niTacImp, but is it right?
1501 | not (n `elem` env) = [n]
1502 ni env (PPatvar _ n) = [n]
1503 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1504 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1505 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1506 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1507 ni env (PPi p n ty sc) = niTacImp env p ++ ni env ty ++ ni (n:env) sc
1508 ni env (PHidden tm) = ni env tm
1509 ni env (PEq _ l r) = ni env l ++ ni env r
1510 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1511 ni env (PTyped l r) = ni env l ++ ni env r
1512 ni env (PPair _ _ l r) = ni env l ++ ni env r
1513 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1514 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1515 ni env (PAlternative a ls) = concatMap (ni env) ls
1516 ni env (PUnifyLog tm) = ni env tm
1517 ni env (PDisamb _ tm) = ni env tm
1518 ni env (PNoImplicits tm) = ni env tm
1521 niTacImp env (TacImp _ _ scr) = ni env scr
1524-- Return all names defined in binders in the given term
1525boundNamesIn :: PTerm -> [Name]
1526boundNamesIn tm = nub $ ni tm
1527 where -- TODO THINK Added niTacImp, but is it right?
1528 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1529 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1530 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1531 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1532 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1533 ni (PPi p n ty sc) = niTacImp p ++ (n : (ni ty ++ ni sc))
1534 ni (PEq _ l r) = ni l ++ ni r
1535 ni (PRewrite _ l r _) = ni l ++ ni r
1536 ni (PTyped l r) = ni l ++ ni r
1537 ni (PPair _ _ l r) = ni l ++ ni r
1538 ni (PDPair _ _ (PRef _ n) t r) = ni t ++ ni r
1539 ni (PDPair _ _ l t r) = ni l ++ ni t ++ ni r
1540 ni (PAlternative a as) = concatMap (ni) as
1541 ni (PHidden tm) = ni tm
1542 ni (PUnifyLog tm) = ni tm
1543 ni (PDisamb _ tm) = ni tm
1544 ni (PNoImplicits tm) = ni tm
1547 niTacImp (TacImp _ _ scr) = ni scr
1550-- Return names which are free in the given term.
1551namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1552namesIn uvars ist tm = nub $ ni [] tm
1555 | not (n `elem` env)
1556 = case lookupTy n (tt_ctxt ist) of
1558 _ -> if n `elem` (map fst uvars) then [n] else []
1559 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1560 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1561 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1562 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1563 ni env (PPi p n ty sc) = niTacImp env p ++ ni env ty ++ ni (n:env) sc
1564 ni env (PEq _ l r) = ni env l ++ ni env r
1565 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1566 ni env (PTyped l r) = ni env l ++ ni env r
1567 ni env (PPair _ _ l r) = ni env l ++ ni env r
1568 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1569 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1570 ni env (PAlternative a as) = concatMap (ni env) as
1571 ni env (PHidden tm) = ni env tm
1572 ni env (PUnifyLog tm) = ni env tm
1573 ni env (PDisamb _ tm) = ni env tm
1574 ni env (PNoImplicits tm) = ni env tm
1577 niTacImp env (TacImp _ _ scr) = ni env scr
1580-- Return which of the given names are used in the given term.
1582usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1583usedNamesIn vars ist tm = nub $ ni [] tm
1584 where -- TODO THINK added niTacImp, but is it right?
1586 | n `elem` vars && not (n `elem` env)
1587 = case lookupTy n (tt_ctxt ist) of
1590 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1591 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1592 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1593 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1594 ni env (PPi p n ty sc) = niTacImp env p ++ ni env ty ++ ni (n:env) sc
1595 ni env (PEq _ l r) = ni env l ++ ni env r
1596 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1597 ni env (PTyped l r) = ni env l ++ ni env r
1598 ni env (PPair _ _ l r) = ni env l ++ ni env r
1599 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1600 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1601 ni env (PAlternative a as) = concatMap (ni env) as
1602 ni env (PHidden tm) = ni env tm
1603 ni env (PUnifyLog tm) = ni env tm
1604 ni env (PDisamb _ tm) = ni env tm
1605 ni env (PNoImplicits tm) = ni env tm
1608 niTacImp env (TacImp _ _ scr) = ni env scr
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