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]])],
184 usedpos :: [(Int, [(Name, Int)])] } -- (used_arg#, [(using_fn, its_arg#)])
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],
402 pparam :: Bool } -- this is a param (rather than index)
403 | Constraint { pargopts :: [ArgOpt],
405 | TacImp { pargopts :: [ArgOpt],
411deriving instance Binary Plicity
412deriving instance NFData Plicity
415impl = Imp [] Dynamic False
416expl = Exp [] Dynamic False
417expl_param = Exp [] Dynamic True
418constraint = Constraint [] Dynamic
419tacimpl t = TacImp [] Dynamic t
421data FnOpt = Inlinable -- always evaluate when simplifying
422 | TotalFn | PartialFn | CoveringFn
423 | Coinductive | AssertTotal
424 | Dictionary -- type class dictionary, eval only when
425 -- a function argument, and further evaluation resutls
426 | Implicit -- implicit coercion
427 | CExport String -- export, with a C name
428 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
429 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
430 | Reflection -- a reflecting function, compile-time only
431 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
434deriving instance Binary FnOpt
435deriving instance NFData FnOpt
440inlinable :: FnOpts -> Bool
441inlinable = elem Inlinable
443dictionary :: FnOpts -> Bool
444dictionary = elem Dictionary
447-- | Data declaration options
448data DataOpt = Codata -- Set if the the data-type is coinductive
449 | DefaultEliminator -- Set if an eliminator should be generated for data type
453type DataOpts = [DataOpt]
455-- | Type provider - what to provide
456data ProvideWhat = ProvTerm -- ^ only allow providing terms
457 | ProvPostulate -- ^ only allow postulates
458 | ProvAny -- ^ either is ok
461-- | Top-level declarations such as compiler directives, definitions,
462-- datatypes and typeclasses.
464 = PFix FC Fixity [String] -- ^ Fixity declaration
465 | PTy Docstring [(Name, Docstring)] SyntaxInfo FC FnOpts Name t -- ^ Type declaration
466 | PPostulate Docstring SyntaxInfo FC FnOpts Name t -- ^ Postulate
467 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
468 | PCAF FC Name t -- ^ Top level constant
469 | PData Docstring [(Name, Docstring)] SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
470 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
471 | PNamespace String [PDecl' t] -- ^ New namespace
472 | PRecord Docstring SyntaxInfo FC Name t Docstring Name t -- ^ Record declaration
473 | PClass Docstring SyntaxInfo FC
476 [(Name, t)] -- parameters
477 [PDecl' t] -- declarations
478 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
479 -- class name, parameters, method declarations
480 | PInstance SyntaxInfo FC [t] -- constraints
483 t -- full instance type
484 (Maybe Name) -- explicit name
486 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
487 -- class name, parameters, full instance type, optional explicit name, and definitions
488 | PDSL Name (DSL' t) -- ^ DSL declaration
489 | PSyntax FC Syntax -- ^ Syntax definition
490 | PMutual FC [PDecl' t] -- ^ Mutual block
491 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
492 | PProvider SyntaxInfo FC ProvideWhat Name t t -- ^ Type provider. The first t is the type, the second is the term
493 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
494 -- bool is True, lhs and rhs must be convertible
497deriving instance Binary PDecl'
498deriving instance NFData PDecl'
501-- For elaborator state
502type ElabD a = Elab' [PDecl] a
504-- | One clause of a top-level definition. Term arguments to constructors are:
506-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
508-- 2. The list of extra 'with' patterns
510-- 3. The right-hand side
512-- 4. The where block (PDecl' t)
514data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
515 | PWith FC Name t [t] t [PDecl' t]
516 | PClauseR FC [t] t [PDecl' t]
517 | PWithR FC [t] t [PDecl' t]
520deriving instance Binary PClause'
521deriving instance NFData PClause'
525data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
526 d_tcon :: t, -- ^ Type constructor
527 d_cons :: [(Docstring, [(Name, Docstring)], Name, t, FC, [Name])] -- ^ Constructors
529 -- ^ Data declaration
530 | PLaterdecl { d_name :: Name, d_tcon :: t }
531 -- ^ "Placeholder" for data whose constructors are defined later
534deriving instance Binary PData'
535deriving instance NFData PData'
538-- Handy to get a free function for applying PTerm -> PTerm functions
539-- across a program, by deriving Functor
541type PDecl = PDecl' PTerm
542type PData = PData' PTerm
543type PClause = PClause' PTerm
545-- get all the names declared in a decl
547declared :: PDecl -> [Name]
548declared (PFix _ _ _) = []
549declared (PTy _ _ _ _ _ n t) = [n]
550declared (PPostulate _ _ _ _ n t) = [n]
551declared (PClauses _ _ n _) = [] -- not a declaration
552declared (PCAF _ n _) = [n]
553declared (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
554 where fstt (_, _, a, _, _, _) = a
555declared (PData _ _ _ _ _ (PLaterdecl n _)) = [n]
556declared (PParams _ _ ds) = concatMap declared ds
557declared (PNamespace _ ds) = concatMap declared ds
558declared (PRecord _ _ _ n _ _ c _) = [n, c]
559declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
560declared (PInstance _ _ _ _ _ _ _ _) = []
561declared (PDSL n _) = [n]
562declared (PSyntax _ _) = []
563declared (PMutual _ ds) = concatMap declared ds
564declared (PDirective _) = []
566-- get the names declared, not counting nested parameter blocks
567tldeclared :: PDecl -> [Name]
568tldeclared (PFix _ _ _) = []
569tldeclared (PTy _ _ _ _ _ n t) = [n]
570tldeclared (PPostulate _ _ _ _ n t) = [n]
571tldeclared (PClauses _ _ n _) = [] -- not a declaration
572tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
573tldeclared (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
574 where fstt (_, _, a, _, _, _) = a
575tldeclared (PParams _ _ ds) = []
576tldeclared (PMutual _ ds) = concatMap tldeclared ds
577tldeclared (PNamespace _ ds) = concatMap tldeclared ds
578tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
579tldeclared (PInstance _ _ _ _ _ _ _ _) = []
582defined :: PDecl -> [Name]
583defined (PFix _ _ _) = []
584defined (PTy _ _ _ _ _ n t) = []
585defined (PPostulate _ _ _ _ n t) = []
586defined (PClauses _ _ n _) = [n] -- not a declaration
587defined (PCAF _ n _) = [n]
588defined (PData _ _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
589 where fstt (_, _, a, _, _, _) = a
590defined (PData _ _ _ _ _ (PLaterdecl n _)) = []
591defined (PParams _ _ ds) = concatMap defined ds
592defined (PNamespace _ ds) = concatMap defined ds
593defined (PRecord _ _ _ n _ _ c _) = [n, c]
594defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
595defined (PInstance _ _ _ _ _ _ _ _) = []
596defined (PDSL n _) = [n]
597defined (PSyntax _ _) = []
598defined (PMutual _ ds) = concatMap defined ds
599defined (PDirective _) = []
602updateN :: [(Name, Name)] -> Name -> Name
603updateN ns n | Just n' <- lookup n ns = n'
606updateNs :: [(Name, Name)] -> PTerm -> PTerm
608updateNs ns t = mapPT updateRef t
609 where updateRef (PRef fc f) = PRef fc (updateN ns f)
612-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
614-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
615-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
616-- where updateCNs ns (PClause n l ts r ds)
617-- = PClause (updateN ns n) (fmap (updateNs ns) l)
618-- (map (fmap (updateNs ns)) ts)
619-- (fmap (updateNs ns) r)
620-- (map (updateDNs ns) ds)
623data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show)
625-- | High level language terms
626data PTerm = PQuote Raw
628 | PInferRef FC Name -- ^ A name to be defined later
630 | PLam Name PTerm PTerm
631 | PPi Plicity Name PTerm PTerm
632 | PLet Name PTerm PTerm PTerm
633 | PTyped PTerm PTerm -- ^ Term with explicit type
634 | PApp FC PTerm [PArg]
635 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
636 | PMatchApp FC Name -- ^ Make an application by type matching
637 | PCase FC PTerm [(PTerm, PTerm)]
643 | PRewrite FC PTerm PTerm (Maybe PTerm)
644 | PPair FC PunInfo PTerm PTerm
645 | PDPair FC PunInfo PTerm PTerm PTerm
646 | PAlternative Bool [PTerm] -- True if only one may work
647 | PHidden PTerm -- ^ Irrelevant or hidden pattern
649 | PGoal FC PTerm Name PTerm
656 | PProof [PTactic] -- ^ Proof script
657 | PTactics [PTactic] -- ^ As PProof, but no auto solving
658 | PElabError Err -- ^ Error to report on elaboration
659 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
660 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
661 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
662 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
663 | PNoImplicits PTerm -- ^ never run implicit converions on the term
668deriving instance Binary PTerm
669deriving instance NFData PTerm
672mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
673mapPT f t = f (mpt t) where
674 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
675 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
676 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
677 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
679 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
680 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
681 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
682 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
683 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
684 mpt (PPair fc p l r) = PPair fc p (mapPT f l) (mapPT f r)
685 mpt (PDPair fc p l t r) = PDPair fc p (mapPT f l) (mapPT f t) (mapPT f r)
686 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
687 mpt (PHidden t) = PHidden (mapPT f t)
688 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
689 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
690 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
691 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
692 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
693 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
694 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
698data PTactic' t = Intro [Name] | Intros | Focus Name
699 | Refine Name [Bool] | Rewrite t
703 | LetTac Name t | LetTacTy Name t t
704 | Exact t | Compute | Trivial | TCInstance
705 | ProofSearch (Maybe Name) [Name]
708 | ProofState | ProofTerm | Undo
709 | Try (PTactic' t) (PTactic' t)
710 | TSeq (PTactic' t) (PTactic' t)
711 | ApplyTactic t -- see Language.Reflection module
715 | GoalType String (PTactic' t)
719 deriving (Show, Eq, Functor)
721deriving instance Binary PTactic'
722deriving instance NFData PTactic'
724instance Sized a => Sized (PTactic' a) where
725 size (Intro nms) = 1 + size nms
727 size (Focus nm) = 1 + size nm
728 size (Refine nm bs) = 1 + size nm + length bs
729 size (Rewrite t) = 1 + size t
730 size (Induction t) = 1 + size t
731 size (LetTac nm t) = 1 + size nm + size t
732 size (Exact t) = 1 + size t
740 size (Try l r) = 1 + size l + size r
741 size (TSeq l r) = 1 + size l + size r
742 size (ApplyTactic t) = 1 + size t
743 size (Reflect t) = 1 + size t
744 size (Fill t) = 1 + size t
748type PTactic = PTactic' PTerm
750data PDo' t = DoExp FC t
752 | DoBindP FC t t [(t,t)]
755 deriving (Eq, Functor)
757deriving instance Binary PDo'
758deriving instance NFData PDo'
761instance Sized a => Sized (PDo' a) where
762 size (DoExp fc t) = 1 + size fc + size t
763 size (DoBind fc nm t) = 1 + size fc + size nm + size t
764 size (DoBindP fc l r alts) = 1 + size fc + size l + size r + size alts
765 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
766 size (DoLetP fc l r) = 1 + size fc + size l + size r
770-- The priority gives a hint as to elaboration order. Best to elaborate
771-- things early which will help give a more concrete type to other
772-- variables, e.g. a before (interpTy a).
774data PArg' t = PImp { priority :: Int,
775 machine_inf :: Bool, -- true if the machine inferred it
777 pname :: Name, getTm :: t }
778 | PExp { priority :: Int,
782 | PConstraint { priority :: Int,
786 | PTacImplicit { priority :: Int,
791 deriving (Show, Eq, Functor)
793data ArgOpt = HideDisplay | InaccessibleArg
796instance Sized a => Sized (PArg' a) where
797 size (PImp p _ l nm trm) = 1 + size nm + size trm
798 size (PExp p l nm trm) = 1 + size nm + size trm
799 size (PConstraint p l nm trm) = 1 + size nm +size nm + size trm
800 size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
803deriving instance Binary PArg'
804deriving instance NFData PArg'
807pimp n t mach = PImp 1 mach [] n t
808pexp t = PExp 1 [] (sMN 0 "arg") t
809pconst t = PConstraint 1 [] (sMN 0 "carg") t
810ptacimp n s t = PTacImplicit 2 [] n s t
812type PArg = PArg' PTerm
816data ClassInfo = CI { instanceName :: Name,
817 class_methods :: [(Name, (FnOpts, PTerm))],
818 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
819 class_default_superclasses :: [PDecl],
820 class_params :: [Name],
821 class_instances :: [Name] }
824deriving instance Binary ClassInfo
825deriving instance NFData ClassInfo
828-- Type inference data
830data TIData = TIPartial -- ^ a function with a partially defined type
831 | TISolution [Term] -- ^ possible solutions to a metavariable in a type
834-- An argument is conditionally forceable iff its forceability
835-- depends on the collapsibility of the whole type.
836data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
839deriving instance Binary Forceability
840deriving instance NFData Forceability
843data OptInfo = Optimise { collapsible :: Bool,
845 -- The following should actually be (IntMap Forceability)
846 -- but the corresponding Binary instance seems to be broken.
847 -- Let's store a list and convert it to IntMap whenever needed.
848 forceable :: [(Int, Forceability)],
850 inaccessible :: [(Int,Name)] } -- arg name for error reporting
853deriving instance Binary OptInfo
854deriving instance NFData OptInfo
858data TypeInfo = TI { con_names :: [Name],
860 data_opts :: DataOpts,
864deriving instance Binary TypeInfo
865deriving instance NFData TypeInfo
868-- Syntactic sugar info
870data DSL' t = DSL { dsl_bind :: t,
875 index_first :: Maybe t,
876 index_next :: Maybe t,
877 dsl_lambda :: Maybe t,
880 deriving (Show, Functor)
882deriving instance Binary DSL'
883deriving instance NFData DSL'
888data SynContext = PatternSyntax | TermSyntax | AnySyntax
891deriving instance Binary SynContext
892deriving instance NFData SynContext
895data Syntax = Rule [SSymbol] PTerm SynContext
898deriving instance Binary Syntax
899deriving instance NFData Syntax
902data SSymbol = Keyword Name
909deriving instance Binary SSymbol
910deriving instance NFData SSymbol
913initDSL = DSL (PRef f (sUN ">>="))
914 (PRef f (sUN "return"))
916 (PRef f (sUN "pure"))
922 where f = fileFC "(builtin)"
924data Using = UImplicit Name PTerm
925 | UConstraint Name [Name]
928deriving instance Binary Using
929deriving instance NFData Using
932data SyntaxInfo = Syn { using :: [Using],
933 syn_params :: [(Name, PTerm)],
934 syn_namespace :: [String],
936 decoration :: Name -> Name,
938 implicitAllowed :: Bool,
942deriving instance NFData SyntaxInfo
943deriving instance Binary SyntaxInfo
946defaultSyntax = Syn [] [] [] [] id False False initDSL
948expandNS :: SyntaxInfo -> Name -> Name
949expandNS syn n@(NS _ _) = n
950expandNS syn n = case syn_namespace syn of
955-- For inferring types of things
959inferTy = sMN 0 "__Infer"
960inferCon = sMN 0 "__infer"
961inferDecl = PDatadecl inferTy
963 [(emptyDocstring, [], inferCon, PPi impl (sMN 0 "iType") PType (
964 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
965 (PRef bi inferTy)), bi, [])]
968infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
969infP = P (TCon 6 0) inferTy (TType (UVal 0))
971getInferTerm, getInferType :: Term -> Term
972getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
973getInferTerm (App (App _ _) tm) = tm
974getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
976getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
977 where toTy (Lam t) = Pi t
978 toTy (PVar t) = PVTy t
980getInferType (App (App _ ty) _) = ty
984-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
986primNames = [unitTy, unitCon,
987 falseTy, pairTy, pairCon,
988 eqTy, eqCon, inferTy, inferCon]
990unitTy = sMN 0 "__Unit"
991unitCon = sMN 0 "__II"
992unitDecl = PDatadecl unitTy PType
993 [(emptyDocstring, [], unitCon, PRef bi unitTy, bi, [])]
994unitOpts = [DefaultEliminator]
996falseTy = sMN 0 "__False"
997falseDecl = PDatadecl falseTy PType []
1000pairTy = sMN 0 "__Pair"
1001pairCon = sMN 0 "__MkPair"
1002pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
1003 [(emptyDocstring, [], pairCon, PPi impl (n "A") PType (
1004 PPi impl (n "B") PType (
1005 PPi expl (n "a") (PRef bi (n "A")) (
1006 PPi expl (n "b") (PRef bi (n "B"))
1007 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
1008 pexp (PRef bi (n "B"))])))), bi, [])]
1014eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
1015 (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
1017 [(emptyDocstring, [], eqCon, PPi impl (n "A") PType (
1018 PPi impl (n "x") (PRef bi (n "A"))
1019 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
1020 pimp (n "B") Placeholder False,
1021 pexp (PRef bi (n "x")),
1022 pexp (PRef bi (n "x"))])), bi, [])]
1026elimName = sUN "__Elim"
1027elimMethElimTy = sUN "__elimTy"
1028elimMethElim = sUN "elim"
1029elimDecl = PClass (parseDocstring . T.pack $ "Type class for eliminators") defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
1030 [PTy emptyDocstring [] defaultSyntax bi [TotalFn] elimMethElimTy PType,
1031 PTy emptyDocstring [] defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
1033-- Defined in builtins.idr
1034sigmaTy = sUN "Exists"
1035existsCon = sUN "Ex_intro"
1037piBind :: [(Name, PTerm)] -> PTerm -> PTerm
1038piBind = piBindp expl
1040piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
1042piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
1045-- Pretty-printing declarations and terms
1047-- These "show" instances render to an absurdly wide screen because inserted line breaks
1048-- could interfere with interactive editing, which calls "show".
1050instance Show PTerm where
1051 showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
1053instance Show PDecl where
1054 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
1056instance Show PClause where
1057 showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
1059instance Show PData where
1060 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
1062instance Pretty PTerm OutputAnnotation where
1063 pretty = prettyImp False
1065-- | Colourise annotations according to an Idris state. It ignores the names
1066-- in the annotation, as there's no good way to show extended information on a
1068consoleDecorate :: IState -> OutputAnnotation -> String -> String
1069consoleDecorate ist _ | not (idris_colourRepl ist) = id
1070consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1071 in colouriseData theme
1072consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1073 in colouriseType theme
1074consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1075consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1076consoleDecorate ist AnnKeyword = colouriseKeyword (idris_colourTheme ist)
1077consoleDecorate ist (AnnName n _ _ _) = let ctxt = tt_ctxt ist
1078 theme = idris_colourTheme ist
1080 _ | isDConName n ctxt -> colouriseData theme
1081 _ | isFnName n ctxt -> colouriseFun theme
1082 _ | isTConName n ctxt -> colouriseType theme
1083 _ | otherwise -> id -- don't colourise unknown names
1084consoleDecorate ist (AnnFC _) = id
1085consoleDecorate ist (AnnTextFmt fmt) = Idris.Colours.colourise (colour fmt)
1086 where colour BoldText = IdrisColour Nothing True False True False
1087 colour UnderlineText = IdrisColour Nothing True True False False
1088 colour ItalicText = IdrisColour Nothing True False False True
1090-- | Pretty-print a high-level closed Idris term
1091prettyImp :: Bool -- ^^ whether to show implicits
1092 -> PTerm -- ^^ the term to pretty-print
1093 -> Doc OutputAnnotation
1094prettyImp impl = pprintPTerm impl [] []
1096-- | Pretty-print a high-level Idris term in some bindings context
1097pprintPTerm :: Bool -- ^^ whether to show implicits
1098 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1099 -> [Name] -- ^^ names to always show in pi, even if not used
1100 -> PTerm -- ^^ the term to pretty-print
1101 -> Doc OutputAnnotation
1102pprintPTerm impl bnd docArgs = prettySe 10 bnd
1104 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1105 prettySe p bnd (PQuote r) =
1106 text "![" <> pretty r <> text "]"
1107 prettySe p bnd (PPatvar fc n) = pretty n
1109 | Just str <- slist p bnd e = str
1110 | Just n <- snat p e = annotate AnnConstData (text (show n))
1111 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1112 prettySe p bnd (PLam n ty sc) =
1113 bracket p 2 . group . align . hang 2 $
1114 text "\\" <> bindingOf n False <+> text "=>" <$>
1115 prettySe 10 ((n, False):bnd) sc
1116 prettySe p bnd (PLet n ty v sc) =
1118 kwd "let" <+> bindingOf n False <+> text "=" </>
1119 prettySe 10 bnd v <+> kwd "in" </>
1120 prettySe 10 ((n, False):bnd) sc
1121 prettySe p bnd (PPi (Exp l s _) n ty sc)
1122 | n `elem` allNamesIn sc || impl || n `elem` docArgs =
1123 bracket p 2 . group $
1124 enclose lparen rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1125 st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
1127 bracket p 2 . group $
1128 group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
1132 Static -> text "[static]" <> space
1134 prettySe p bnd (PPi (Imp l s _) n ty sc)
1137 lparen <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1138 st <> text "->" </> prettySe 10 ((n, True):bnd) sc
1139 | otherwise = prettySe 10 ((n, True):bnd) sc
1143 Static -> text "[static]" <> space
1145 prettySe p bnd (PPi (Constraint _ _) n ty sc) =
1147 prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
1148 prettySe p bnd (PPi (TacImp _ _ s) n ty sc) =
1150 lbrace <> kwd "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1151 rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
1152 prettySe p bnd (PApp _ (PRef _ f) args) -- normal names, no explicit args
1153 | UN nm <- basename f
1154 , not impl && null (getExps args) = if isAlpha (thead nm)
1155 then prettyName impl bnd f
1156 else enclose lparen rparen $
1157 prettyName impl bnd f
1158 prettySe p bnd (PAppBind _ (PRef _ f) [])
1159 | not impl = text "!" <> prettyName impl bnd f
1160 prettySe p bnd (PApp _ (PRef _ op) args)
1161 | UN nm <- basename op
1163 (not impl) && (not $ isAlpha (thead nm)) =
1164 case getExps args of
1165 [] -> enclose lparen rparen opName
1166 [x] -> group (enclose lparen rparen opName <$> group (prettySe 0 bnd x))
1167 [l,r] -> bracket p 1 $ inFix l r
1168 (l:r:rest) -> bracket p 1 $
1169 enclose lparen rparen (inFix l r) <+>
1170 align (group (vsep (map (prettyArgSe bnd) rest)))
1171 where opName = prettyName impl bnd op
1172 inFix l r = align . group $
1173 (prettySe 1 bnd l <+> opName) <$> group (prettySe 0 bnd r)
1174 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1175 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1176 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1177 prettySe p bnd (PApp _ f as) =
1178 let args = getExps as
1179 fp = prettySe 1 bnd f
1181 bracket p 1 . group $
1185 else fp <+> align (vsep (map (prettyArgS bnd) as))
1188 else fp <+> align (vsep (map (prettyArgSe bnd) args))
1189 prettySe p bnd (PCase _ scr opts) =
1190 kwd "case" <+> prettySe 10 bnd scr <+> kwd "of" <> prettyBody
1192 prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1194 sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1195 prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1196 prettySe p bnd (PRefl _ _) = annName eqCon $ text "refl"
1197 prettySe p bnd (PResolveTC _) = text "resolvetc"
1198 prettySe p bnd (PTrue _ IsType) = annName unitTy $ text "()"
1199 prettySe p bnd (PTrue _ IsTerm) = annName unitCon $ text "()"
1200 prettySe p bnd (PTrue _ TypeOrTerm) = text "()"
1201 prettySe p bnd (PFalse _) = annName falseTy $ text "_|_"
1202 prettySe p bnd (PEq _ l r) =
1203 bracket p 2 . align . group $
1204 prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
1205 where eq = annName eqTy (text "=")
1206 prettySe p bnd (PRewrite _ l r _) =
1208 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1209 prettySe p bnd (PTyped l r) =
1210 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1211 prettySe p bnd pair@(PPair _ pun _ _) -- flatten tuples to the right, like parser
1212 | Just elts <- pairElts pair = enclose (ann lparen) (ann rparen) .
1213 align . group . vsep . punctuate (ann comma) $
1214 map (prettySe 10 bnd) elts
1215 where ann = case pun of
1217 IsType -> annName pairTy
1218 IsTerm -> annName pairCon
1219 prettySe p bnd (PDPair _ TypeOrTerm l t r) =
1220 lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1221 prettySe p bnd (PDPair _ IsType (PRef _ n) t r) =
1222 annName sigmaTy lparen <>
1223 bindingOf n False <+>
1224 annName sigmaTy (text "**") <+>
1225 prettySe 10 ((n, False):bnd) r <>
1226 annName sigmaTy rparen
1227 prettySe p bnd (PDPair _ IsType l t r) =
1228 annName sigmaTy lparen <>
1229 prettySe 10 bnd l <+>
1230 annName sigmaTy (text "**") <+>
1231 prettySe 10 bnd r <>
1232 annName sigmaTy rparen
1233 prettySe p bnd (PDPair _ IsTerm l t r) =
1234 annName existsCon lparen <>
1235 prettySe 10 bnd l <+>
1236 annName existsCon (text "**") <+>
1237 prettySe 10 bnd r <>
1238 annName existsCon rparen
1239 prettySe p bnd (PAlternative a as) =
1240 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1243 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1244 prettySe p bnd PType = annotate AnnConstType $ text "Type"
1245 prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1246 where annot (AType _) = AnnConstType
1247 annot StrType = AnnConstType
1248 annot PtrType = AnnConstType
1249 annot VoidType = AnnConstType
1250 annot _ = AnnConstData
1251 -- XXX: add pretty for tactics
1252 prettySe p bnd (PProof ts) =
1253 text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1254 prettySe p bnd (PTactics ts) =
1255 text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1256 prettySe p bnd (PMetavar n) = text "?" <> pretty n
1257 prettySe p bnd (PReturn f) = kwd "return"
1258 prettySe p bnd PImpossible = kwd "impossible"
1259 prettySe p bnd Placeholder = text "_"
1260 prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1261 prettySe p bnd (PElabError s) = pretty s
1263 prettySe p bnd _ = text "test"
1265 prettyArgS bnd (PImp _ _ _ n tm) = prettyArgSi bnd (n, tm)
1266 prettyArgS bnd (PExp _ _ _ tm) = prettyArgSe bnd tm
1267 prettyArgS bnd (PConstraint _ _ _ tm) = prettyArgSc bnd tm
1268 prettyArgS bnd (PTacImplicit _ _ n _ tm) = prettyArgSti bnd (n, tm)
1270 prettyArgSe bnd arg = prettySe 0 bnd arg
1271 prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1272 prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1273 prettyArgSti bnd (n, val) = lbrace <> kwd "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1275 annName :: Name -> Doc OutputAnnotation -> Doc OutputAnnotation
1276 annName n = annotate (AnnName n Nothing Nothing Nothing)
1278 basename :: Name -> Name
1279 basename (NS n _) = basename n
1282 slist' p bnd (PApp _ (PRef _ nil) _)
1283 | not impl && nsroot nil == sUN "Nil" = Just []
1284 slist' p bnd (PRef _ nil)
1285 | not impl && nsroot nil == sUN "Nil" = Just []
1286 slist' p bnd (PApp _ (PRef _ cons) args)
1287 | nsroot cons == sUN "::",
1288 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1290 Just tl' <- slist' p bnd tl
1293 isImp (PImp {}) = True
1295 slist' _ _ tm = Nothing
1297 slist p bnd e | Just es <- slist' p bnd e = Just $
1298 case es of [] -> annotate AnnConstData $ text "[]"
1299 [x] -> enclose left right . group $
1301 xs -> (enclose left right .
1302 align . group . vsep .
1304 map (prettySe p bnd)) xs
1305 where left = (annotate AnnConstData (text "["))
1306 right = (annotate AnnConstData (text "]"))
1307 comma = (annotate AnnConstData (text ","))
1308 slist _ _ _ = Nothing
1310 pairElts :: PTerm -> Maybe [PTerm]
1311 pairElts (PPair _ _ x y) | Just elts <- pairElts y = Just (x:elts)
1312 | otherwise = Just [x, y]
1313 pairElts _ = Nothing
1315 natns = "Prelude.Nat."
1318 | show z == (natns++"Z") || show z == "Z" = Just 0
1319 snat p (PApp _ s [PExp {getTm=n}])
1320 | show s == (natns++"S") || show s == "S",
1325 bracket outer inner doc
1326 | inner > outer = lparen <> doc <> rparen
1329 kwd = annotate AnnKeyword . text
1331-- | Pretty-printer helper for the binding site of a name
1332bindingOf :: Name -- ^^ the bound name
1333 -> Bool -- ^^ whether the name is implicit
1334 -> Doc OutputAnnotation
1335bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1337-- | Pretty-printer helper for names that attaches the correct annotations
1338prettyName :: Bool -- ^^ whether to show namespaces
1339 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1340 -> Name -- ^^ the name to pprint
1341 -> Doc OutputAnnotation
1342prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1343 | otherwise = annotate (AnnName n Nothing Nothing Nothing) (text (strName n))
1344 where strName (UN n) = T.unpack n
1345 strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1346 | otherwise = strName n
1347 strName (MN i s) = T.unpack s
1348 strName other = show other
1351showCImp :: Bool -> PClause -> Doc OutputAnnotation
1352showCImp impl (PClause _ n l ws r w)
1353 = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1354 <+> text "where" <+> text (show w)
1357 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1358showCImp impl (PWith _ n l ws r w)
1359 = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1360 <+> braces (text (show w))
1363 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1366showDImp :: Bool -> PData -> Doc OutputAnnotation
1367showDImp impl (PDatadecl n ty cons)
1368 = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1369 (indent 2 $ vsep (map (\ (_, _, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1371showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1372showDecls i ds = vsep (map (showDeclImp i) ds)
1374showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1375showDeclImp i (PTy _ _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1376showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1377 indent 2 (vsep (map (showCImp i) cs))
1378showDeclImp _ (PData _ _ _ _ _ d) = showDImp True d
1379showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1380showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1381showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1382showDeclImp i (PClass _ _ _ cs n ps ds)
1383 = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1384showDeclImp i (PInstance _ _ cs n _ t _ ds)
1385 = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1386showDeclImp _ _ = text "..."
1387-- showDeclImp (PImport i) = "import " ++ i
1389instance Show (Doc OutputAnnotation) where
1390 show = flip (displayS . renderCompact) ""
1392getImps :: [PArg] -> [(Name, PTerm)]
1394getImps (PImp _ _ _ n tm : xs) = (n, tm) : getImps xs
1395getImps (_ : xs) = getImps xs
1397getExps :: [PArg] -> [PTerm]
1399getExps (PExp _ _ _ tm : xs) = tm : getExps xs
1400getExps (_ : xs) = getExps xs
1402getConsts :: [PArg] -> [PTerm]
1404getConsts (PConstraint _ _ _ tm : xs) = tm : getConsts xs
1405getConsts (_ : xs) = getConsts xs
1407getAll :: [PArg] -> [PTerm]
1412showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1413 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1414 -> Bool -- ^^ whether to show implicits
1415 -> Bool -- ^^ whether to colourise
1416 -> Name -- ^^ the term to show
1418showName ist bnd impl colour n = case ist of
1419 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1420 Nothing -> showbasic n
1421 where name = if impl then show n else showbasic n
1422 showbasic n@(UN _) = showCG n
1423 showbasic (MN _ s) = str s
1424 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1425 showbasic (SN s) = show s
1427 colourise n t = let ctxt' = fmap tt_ctxt ist in
1430 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1431 else colouriseBound t name
1432 | isDConName n ctxt -> colouriseData t name
1433 | isFnName n ctxt -> colouriseFun t name
1434 | isTConName n ctxt -> colouriseType t name
1435 -- The assumption is that if a name is not bound and does not exist in the
1436 -- global context, then we're somewhere in which implicit info has been lost
1437 -- (like error messages). Thus, unknown vars are colourised as implicits.
1438 | otherwise -> colouriseImplicit t name
1440showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1441 -> PTerm -- ^^ the term to show
1443showTm ist = displayDecorated (consoleDecorate ist) .
1444 renderPretty 0.8 100000 .
1445 prettyImp (opt_showimp (idris_options ist))
1447-- | Show a term with implicits, no colours
1448showTmImpls :: PTerm -> String
1449showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1452instance Sized PTerm where
1453 size (PQuote rawTerm) = size rawTerm
1454 size (PRef fc name) = size name
1455 size (PLam name ty bdy) = 1 + size ty + size bdy
1456 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1457 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1458 size (PTyped trm ty) = 1 + size trm + size ty
1459 size (PApp fc name args) = 1 + size args
1460 size (PAppBind fc name args) = 1 + size args
1461 size (PCase fc trm bdy) = 1 + size trm + size bdy
1462 size (PTrue fc _) = 1
1463 size (PFalse fc) = 1
1464 size (PRefl fc _) = 1
1465 size (PResolveTC fc) = 1
1466 size (PEq fc left right) = 1 + size left + size right
1467 size (PRewrite fc left right _) = 1 + size left + size right
1468 size (PPair fc _ left right) = 1 + size left + size right
1469 size (PDPair fs _ left ty right) = 1 + size left + size ty + size right
1470 size (PAlternative a alts) = 1 + size alts
1471 size (PHidden hidden) = size hidden
1472 size (PUnifyLog tm) = size tm
1473 size (PDisamb _ tm) = size tm
1474 size (PNoImplicits tm) = size tm
1476 size (PConstant const) = 1 + size const
1477 size Placeholder = 1
1478 size (PDoBlock dos) = 1 + size dos
1479 size (PIdiom fc term) = 1 + size term
1480 size (PReturn fc) = 1
1481 size (PMetavar name) = 1
1482 size (PProof tactics) = size tactics
1483 size (PElabError err) = size err
1484 size PImpossible = 1
1486getPArity :: PTerm -> Int
1487getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1490-- Return all names, free or globally bound, in the given term.
1492allNamesIn :: PTerm -> [Name]
1493allNamesIn tm = nub $ ni [] tm
1494 where -- TODO THINK added niTacImp, but is it right?
1496 | not (n `elem` env) = [n]
1497 ni env (PPatvar _ n) = [n]
1498 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1499 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1500 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1501 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1502 ni env (PPi p n ty sc) = niTacImp env p ++ ni env ty ++ ni (n:env) sc
1503 ni env (PHidden tm) = ni env tm
1504 ni env (PEq _ l r) = ni env l ++ ni env r
1505 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1506 ni env (PTyped l r) = ni env l ++ ni env r
1507 ni env (PPair _ _ l r) = ni env l ++ ni env r
1508 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1509 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1510 ni env (PAlternative a ls) = concatMap (ni env) ls
1511 ni env (PUnifyLog tm) = ni env tm
1512 ni env (PDisamb _ tm) = ni env tm
1513 ni env (PNoImplicits tm) = ni env tm
1516 niTacImp env (TacImp _ _ scr) = ni env scr
1519-- Return all names defined in binders in the given term
1520boundNamesIn :: PTerm -> [Name]
1521boundNamesIn tm = nub $ ni tm
1522 where -- TODO THINK Added niTacImp, but is it right?
1523 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1524 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1525 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1526 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1527 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1528 ni (PPi p n ty sc) = niTacImp p ++ (n : (ni ty ++ ni sc))
1529 ni (PEq _ l r) = ni l ++ ni r
1530 ni (PRewrite _ l r _) = ni l ++ ni r
1531 ni (PTyped l r) = ni l ++ ni r
1532 ni (PPair _ _ l r) = ni l ++ ni r
1533 ni (PDPair _ _ (PRef _ n) t r) = ni t ++ ni r
1534 ni (PDPair _ _ l t r) = ni l ++ ni t ++ ni r
1535 ni (PAlternative a as) = concatMap (ni) as
1536 ni (PHidden tm) = ni tm
1537 ni (PUnifyLog tm) = ni tm
1538 ni (PDisamb _ tm) = ni tm
1539 ni (PNoImplicits tm) = ni tm
1542 niTacImp (TacImp _ _ scr) = ni scr
1545-- Return names which are free in the given term.
1546namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1547namesIn uvars ist tm = nub $ ni [] tm
1550 | not (n `elem` env)
1551 = case lookupTy n (tt_ctxt ist) of
1553 _ -> if n `elem` (map fst uvars) then [n] else []
1554 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1555 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1556 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1557 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1558 ni env (PPi p n ty sc) = niTacImp env p ++ ni env ty ++ ni (n:env) sc
1559 ni env (PEq _ l r) = ni env l ++ ni env r
1560 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1561 ni env (PTyped l r) = ni env l ++ ni env r
1562 ni env (PPair _ _ l r) = ni env l ++ ni env r
1563 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1564 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1565 ni env (PAlternative a as) = concatMap (ni env) as
1566 ni env (PHidden tm) = ni env tm
1567 ni env (PUnifyLog tm) = ni env tm
1568 ni env (PDisamb _ tm) = ni env tm
1569 ni env (PNoImplicits tm) = ni env tm
1572 niTacImp env (TacImp _ _ scr) = ni env scr
1575-- Return which of the given names are used in the given term.
1577usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1578usedNamesIn vars ist tm = nub $ ni [] tm
1579 where -- TODO THINK added niTacImp, but is it right?
1581 | n `elem` vars && not (n `elem` env)
1582 = case lookupTy n (tt_ctxt ist) of
1585 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1586 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1587 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1588 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1589 ni env (PPi p n ty sc) = niTacImp env p ++ ni env ty ++ ni (n:env) sc
1590 ni env (PEq _ l r) = ni env l ++ ni env r
1591 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1592 ni env (PTyped l r) = ni env l ++ ni env r
1593 ni env (PPair _ _ l r) = ni env l ++ ni env r
1594 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1595 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1596 ni env (PAlternative a as) = concatMap (ni env) as
1597 ni env (PHidden tm) = ni env tm
1598 ni env (PUnifyLog tm) = ni env tm
1599 ni env (PDisamb _ tm) = ni env tm
1600 ni env (PNoImplicits tm) = ni env tm
1603 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