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
11import IRTS.CodegenCommon
13import Util.DynamicLinker
19import System.Console.Haskeline
22import Control.Monad.Trans.State.Strict
23import Control.Monad.Trans.Error
25import Data.List hiding (group)
27import qualified Data.Map as M
28import qualified Data.Text as T
29import qualified Data.Map as M
31import qualified Data.Set as S
32import Data.Word (Word)
36import Text.PrettyPrint.Annotated.Leijen
38data IOption = IOption { opt_logLevel :: Int,
40 opt_typeintype :: Bool,
42 opt_showimp :: Bool, -- ^^ show implicits
43 opt_errContext :: Bool,
48 opt_codegen :: Codegen,
49 opt_outputTy :: OutputType,
50 opt_ibcsubdir :: FilePath,
51 opt_importdirs :: [FilePath],
55 opt_cmdline :: [Opt], -- remember whole command line
60defaultOpts = IOption { opt_logLevel = 0
61 , opt_typecase = False
62 , opt_typeintype = False
65 , opt_errContext = False
68 , opt_nobanner = False
71 , opt_outputTy = Executable
81data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
83-- | The output mode in use
84data OutputMode = RawOutput | IdeSlave Integer deriving Show
86-- | How wide is the console?
87data ConsoleWidth = InfinitelyWide -- ^ Have pretty-printer assume that lines should not be broken
88 | ColsWide Int -- ^ Manually specified - must be positive
89 | AutomaticWidth -- ^ Attempt to determine width, or 80 otherwise
91-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
92-- without typechecking).
93-- This will include all the functions and data declarations, plus fixity declarations
96-- | The global state used in the Idris monad
98 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
99 idris_constraints :: [(UConstraint, FC)],
100 -- ^ A list of universe constraints and their corresponding source locations
101 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
102 idris_implicits :: Ctxt [PArg],
103 idris_statics :: Ctxt [Bool],
104 idris_classes :: Ctxt ClassInfo,
105 idris_dsls :: Ctxt DSL,
106 idris_optimisation :: Ctxt OptInfo,
107 idris_datatypes :: Ctxt TypeInfo,
108 idris_namehints :: Ctxt [Name],
109 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
110 -- ^ list of lhs/rhs, and a list of missing clauses
111 idris_flags :: Ctxt [FnOpt],
112 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
113 idris_calledgraph :: Ctxt [Name],
114 idris_docstrings :: Ctxt String,
115 idris_tyinfodata :: Ctxt TIData,
116 idris_totcheck :: [(FC, Name)], -- names to check totality on
117 idris_defertotcheck :: [(FC, Name)], -- names to check at the end
118 idris_options :: IOption,
120 idris_lineapps :: [((FilePath, Int), PTerm)],
121 -- ^ Full application LHS on source line
122 idris_metavars :: [(Name, (Maybe Name, Int, Bool))], -- ^ The currently defined but not proven metavariables
123 idris_coercions :: [Name],
124 idris_transforms :: [(Term, Term)],
125 idris_errRev :: [(Term, Term)],
126 syntax_rules :: [Syntax],
127 syntax_keywords :: [String],
128 imported :: [FilePath], -- ^ The imported modules
129 idris_scprims :: [(Name, (Int, PrimFn))],
130 idris_objs :: [(Codegen, FilePath)],
131 idris_libs :: [(Codegen, String)],
132 idris_cgflags :: [(Codegen, String)],
133 idris_hdrs :: [(Codegen, String)],
134 proof_list :: [(Name, [String])],
135 errLine :: Maybe Int,
136 parserWarnings :: [(FC, Err)],
137 lastParse :: Maybe Name,
138 indent_stack :: [Int],
139 brace_stack :: [Maybe Int],
140 hide_list :: [(Name, Maybe Accessibility)],
141 default_access :: Accessibility,
142 default_total :: Bool,
143 ibc_write :: [IBCWrite],
144 compiled_so :: Maybe String,
145 idris_dynamic_libs :: [DynamicLib],
146 idris_language_extensions :: [LanguageExt],
147 idris_outputmode :: OutputMode,
148 idris_colourRepl :: Bool,
149 idris_colourTheme :: ColourTheme,
150 idris_outh :: Handle,
151 idris_errorhandlers :: [Name], -- ^ Global error handlers
152 idris_nameIdx :: (Int, Ctxt (Int, Name)),
153 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
154 module_aliases :: M.Map [T.Text] [T.Text],
155 idris_consolewidth :: ConsoleWidth -- ^ How many chars wide is the console?
158data SizeChange = Smaller | Same | Bigger | Unknown
161deriving instance Binary SizeChange
162deriving instance NFData SizeChange
165type SCGEntry = (Name, [Maybe (Int, SizeChange)])
167data CGInfo = CGInfo { argsdef :: [Name],
168 calls :: [(Name, [[Name]])],
174deriving instance Binary CGInfo
175deriving instance NFData CGInfo
178primDefs = [sUN "unsafePerformPrimIO",
179 sUN "mkLazyForeignPrim",
183-- information that needs writing for the current module's .ibc file
184data IBCWrite = IBCFix FixDecl
188 | IBCInstance Bool Name Name
196 | IBCObj Codegen FilePath
197 | IBCLib Codegen String
198 | IBCCGFlag Codegen String
200 | IBCHeader Codegen String
201 | IBCAccess Name Accessibility
202 | IBCMetaInformation Name MetaInformation
203 | IBCTotal Name Totality
204 | IBCFlags Name [FnOpt]
205 | IBCTrans (Term, Term)
206 | IBCErrRev (Term, Term)
210 | IBCDef Name -- i.e. main context
211 | IBCNameHint (Name, Name)
212 | IBCLineApp FilePath Int PTerm
213 | IBCErrorHandler Name
214 | IBCFunctionErrorHandler Name Name Name
217-- | The initial state for the compiler
219idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
220 emptyContext emptyContext emptyContext emptyContext
221 emptyContext emptyContext emptyContext emptyContext
222 emptyContext emptyContext
223 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
224 [] Nothing [] Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
225 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
228-- | The monad for the main REPL - reading and processing files and updating
229-- global state (hence the IO inner monad).
230--type Idris = WriterT [Either String (IO ())] (State IState a))
231type Idris = StateT IState (ErrorT Err IO)
233-- Commands in the REPL
252 | ChangeDirectory FilePath
255 | Compile Codegen String
260 | AddProof (Maybe Name)
272 | DynamicLink FilePath
277 | CaseSplitAt Bool Int Name
278 | AddClauseFrom Bool Int Name
279 | AddProofClauseFrom Bool Int Name
280 | AddMissing Bool Int Name
281 | MakeWith Bool Int Name
282 | DoProofSearch Bool Int Name [Name]
286 | SetColour ColourType IdrisColour
290 | SetConsoleWidth ConsoleWidth
292data Opt = Filename String
304 | NoBuiltins -- only for the really primitive stuff!
330 | OutputTy OutputType
331 | Extension LanguageExt
332 | InterpretScript String
333 | TargetTriple String
338 | AutoWidth -- ^ Automatically adjust terminal width
341-- Parsed declarations
343data Fixity = Infixl { prec :: Int }
344 | Infixr { prec :: Int }
345 | InfixN { prec :: Int }
346 | PrefixN { prec :: Int }
349deriving instance Binary Fixity
350deriving instance NFData Fixity
353instance Show Fixity where
354 show (Infixl i) = "infixl " ++ show i
355 show (Infixr i) = "infixr " ++ show i
356 show (InfixN i) = "infix " ++ show i
357 show (PrefixN i) = "prefix " ++ show i
359data FixDecl = Fix Fixity String
362instance Show FixDecl where
363 show (Fix f s) = show f ++ " " ++ s
366deriving instance Binary FixDecl
367deriving instance NFData FixDecl
370instance Ord FixDecl where
371 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
374data Static = Static | Dynamic
377deriving instance Binary Static
378deriving instance NFData Static
381-- Mark bindings with their explicitness, and laziness
382data Plicity = Imp { pargopts :: [ArgOpt],
386 | Exp { pargopts :: [ArgOpt],
390 | Constraint { pargopts :: [ArgOpt],
393 | TacImp { pargopts :: [ArgOpt],
399plazy :: Plicity -> Bool
400plazy tm = Lazy `elem` pargopts tm
403deriving instance Binary Plicity
404deriving instance NFData Plicity
407impl = Imp [Lazy] Dynamic "" False
408expl = Exp [] Dynamic "" False
409expl_param = Exp [] Dynamic "" True
410constraint = Constraint [] Dynamic ""
411tacimpl t = TacImp [] Dynamic t ""
413data FnOpt = Inlinable -- always evaluate when simplifying
414 | TotalFn | PartialFn
415 | Coinductive | AssertTotal
416 | Dictionary -- type class dictionary, eval only when
417 -- a function argument, and further evaluation resutls
418 | Implicit -- implicit coercion
419 | CExport String -- export, with a C name
420 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
421 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
422 | Reflection -- a reflecting function, compile-time only
423 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
426deriving instance Binary FnOpt
427deriving instance NFData FnOpt
432inlinable :: FnOpts -> Bool
433inlinable = elem Inlinable
435dictionary :: FnOpts -> Bool
436dictionary = elem Dictionary
439-- | Data declaration options
440data DataOpt = Codata -- Set if the the data-type is coinductive
441 | DefaultEliminator -- Set if an eliminator should be generated for data type
445type DataOpts = [DataOpt]
447-- | Type provider - what to provide
448data ProvideWhat = ProvTerm -- ^ only allow providing terms
449 | ProvPostulate -- ^ only allow postulates
450 | ProvAny -- ^ either is ok
453-- | Top-level declarations such as compiler directives, definitions,
454-- datatypes and typeclasses.
456 = PFix FC Fixity [String] -- ^ Fixity declaration
457 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
458 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
459 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
460 | PCAF FC Name t -- ^ Top level constant
461 | PData String SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
462 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
463 | PNamespace String [PDecl' t] -- ^ New namespace
464 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
465 | PClass String SyntaxInfo FC
468 [(Name, t)] -- parameters
469 [PDecl' t] -- declarations
470 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
471 -- class name, parameters, method declarations
472 | PInstance SyntaxInfo FC [t] -- constraints
475 t -- full instance type
476 (Maybe Name) -- explicit name
478 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
479 -- class name, parameters, full instance type, optional explicit name, and definitions
480 | PDSL Name (DSL' t) -- ^ DSL declaration
481 | PSyntax FC Syntax -- ^ Syntax definition
482 | PMutual FC [PDecl' t] -- ^ Mutual block
483 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
484 | PProvider SyntaxInfo FC ProvideWhat Name t t -- ^ Type provider. The first t is the type, the second is the term
485 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
486 -- bool is True, lhs and rhs must be convertible
489deriving instance Binary PDecl'
490deriving instance NFData PDecl'
493-- For elaborator state
494type ElabD a = Elab' [PDecl] a
496-- | One clause of a top-level definition. Term arguments to constructors are:
498-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
500-- 2. The list of extra 'with' patterns
502-- 3. The right-hand side
504-- 4. The where block (PDecl' t)
506data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
507 | PWith FC Name t [t] t [PDecl' t]
508 | PClauseR FC [t] t [PDecl' t]
509 | PWithR FC [t] t [PDecl' t]
512deriving instance Binary PClause'
513deriving instance NFData PClause'
517data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
518 d_tcon :: t, -- ^ Type constructor
519 d_cons :: [(String, Name, t, FC, [Name])] -- ^ Constructors
521 -- ^ Data declaration
522 | PLaterdecl { d_name :: Name, d_tcon :: t }
523 -- ^ "Placeholder" for data whose constructors are defined later
526deriving instance Binary PData'
527deriving instance NFData PData'
530-- Handy to get a free function for applying PTerm -> PTerm functions
531-- across a program, by deriving Functor
533type PDecl = PDecl' PTerm
534type PData = PData' PTerm
535type PClause = PClause' PTerm
537-- get all the names declared in a decl
539declared :: PDecl -> [Name]
540declared (PFix _ _ _) = []
541declared (PTy _ _ _ _ n t) = [n]
542declared (PPostulate _ _ _ _ n t) = [n]
543declared (PClauses _ _ n _) = [] -- not a declaration
544declared (PCAF _ n _) = [n]
545declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
546 where fstt (_, a, _, _, _) = a
547declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
548declared (PParams _ _ ds) = concatMap declared ds
549declared (PNamespace _ ds) = concatMap declared ds
550declared (PRecord _ _ _ n _ _ c _) = [n, c]
551declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
552declared (PInstance _ _ _ _ _ _ _ _) = []
553declared (PDSL n _) = [n]
554declared (PSyntax _ _) = []
555declared (PMutual _ ds) = concatMap declared ds
556declared (PDirective _) = []
558-- get the names declared, not counting nested parameter blocks
559tldeclared :: PDecl -> [Name]
560tldeclared (PFix _ _ _) = []
561tldeclared (PTy _ _ _ _ n t) = [n]
562tldeclared (PPostulate _ _ _ _ n t) = [n]
563tldeclared (PClauses _ _ n _) = [] -- not a declaration
564tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
565tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
566 where fstt (_, a, _, _, _) = a
567tldeclared (PParams _ _ ds) = []
568tldeclared (PMutual _ ds) = concatMap tldeclared ds
569tldeclared (PNamespace _ ds) = concatMap tldeclared ds
570tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
571tldeclared (PInstance _ _ _ _ _ _ _ _) = []
574defined :: PDecl -> [Name]
575defined (PFix _ _ _) = []
576defined (PTy _ _ _ _ n t) = []
577defined (PPostulate _ _ _ _ n t) = []
578defined (PClauses _ _ n _) = [n] -- not a declaration
579defined (PCAF _ n _) = [n]
580defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
581 where fstt (_, a, _, _, _) = a
582defined (PData _ _ _ _ (PLaterdecl n _)) = []
583defined (PParams _ _ ds) = concatMap defined ds
584defined (PNamespace _ ds) = concatMap defined ds
585defined (PRecord _ _ _ n _ _ c _) = [n, c]
586defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
587defined (PInstance _ _ _ _ _ _ _ _) = []
588defined (PDSL n _) = [n]
589defined (PSyntax _ _) = []
590defined (PMutual _ ds) = concatMap defined ds
591defined (PDirective _) = []
594updateN :: [(Name, Name)] -> Name -> Name
595updateN ns n | Just n' <- lookup n ns = n'
598updateNs :: [(Name, Name)] -> PTerm -> PTerm
600updateNs ns t = mapPT updateRef t
601 where updateRef (PRef fc f) = PRef fc (updateN ns f)
604-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
606-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
607-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
608-- where updateCNs ns (PClause n l ts r ds)
609-- = PClause (updateN ns n) (fmap (updateNs ns) l)
610-- (map (fmap (updateNs ns)) ts)
611-- (fmap (updateNs ns) r)
612-- (map (updateDNs ns) ds)
615data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show)
617-- | High level language terms
618data PTerm = PQuote Raw
620 | PInferRef FC Name -- ^ A name to be defined later
622 | PLam Name PTerm PTerm
623 | PPi Plicity Name PTerm PTerm
624 | PLet Name PTerm PTerm PTerm
625 | PTyped PTerm PTerm -- ^ Term with explicit type
626 | PApp FC PTerm [PArg]
627 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
628 | PMatchApp FC Name -- ^ Make an application by type matching
629 | PCase FC PTerm [(PTerm, PTerm)]
635 | PRewrite FC PTerm PTerm (Maybe PTerm)
636 | PPair FC PunInfo PTerm PTerm
637 | PDPair FC PunInfo PTerm PTerm PTerm
638 | PAlternative Bool [PTerm] -- True if only one may work
639 | PHidden PTerm -- ^ Irrelevant or hidden pattern
641 | PGoal FC PTerm Name PTerm
648 | PProof [PTactic] -- ^ Proof script
649 | PTactics [PTactic] -- ^ As PProof, but no auto solving
650 | PElabError Err -- ^ Error to report on elaboration
651 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
652 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
653 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
654 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
655 | PNoImplicits PTerm -- ^ never run implicit converions on the term
660deriving instance Binary PTerm
661deriving instance NFData PTerm
664mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
665mapPT f t = f (mpt t) where
666 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
667 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
668 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
669 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
671 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
672 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
673 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
674 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
675 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
676 mpt (PPair fc p l r) = PPair fc p (mapPT f l) (mapPT f r)
677 mpt (PDPair fc p l t r) = PDPair fc p (mapPT f l) (mapPT f t) (mapPT f r)
678 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
679 mpt (PHidden t) = PHidden (mapPT f t)
680 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
681 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
682 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
683 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
684 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
685 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
686 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
690data PTactic' t = Intro [Name] | Intros | Focus Name
691 | Refine Name [Bool] | Rewrite t
695 | LetTac Name t | LetTacTy Name t t
696 | Exact t | Compute | Trivial | TCInstance
697 | ProofSearch (Maybe Name) [Name]
700 | ProofState | ProofTerm | Undo
701 | Try (PTactic' t) (PTactic' t)
702 | TSeq (PTactic' t) (PTactic' t)
703 | ApplyTactic t -- see Language.Reflection module
707 | GoalType String (PTactic' t)
711 deriving (Show, Eq, Functor)
713deriving instance Binary PTactic'
714deriving instance NFData PTactic'
716instance Sized a => Sized (PTactic' a) where
717 size (Intro nms) = 1 + size nms
719 size (Focus nm) = 1 + size nm
720 size (Refine nm bs) = 1 + size nm + length bs
721 size (Rewrite t) = 1 + size t
722 size (Induction t) = 1 + size t
723 size (LetTac nm t) = 1 + size nm + size t
724 size (Exact t) = 1 + size t
732 size (Try l r) = 1 + size l + size r
733 size (TSeq l r) = 1 + size l + size r
734 size (ApplyTactic t) = 1 + size t
735 size (Reflect t) = 1 + size t
736 size (Fill t) = 1 + size t
740type PTactic = PTactic' PTerm
742data PDo' t = DoExp FC t
747 deriving (Eq, Functor)
749deriving instance Binary PDo'
750deriving instance NFData PDo'
753instance Sized a => Sized (PDo' a) where
754 size (DoExp fc t) = 1 + size fc + size t
755 size (DoBind fc nm t) = 1 + size fc + size nm + size t
756 size (DoBindP fc l r) = 1 + size fc + size l + size r
757 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
758 size (DoLetP fc l r) = 1 + size fc + size l + size r
762-- The priority gives a hint as to elaboration order. Best to elaborate
763-- things early which will help give a more concrete type to other
764-- variables, e.g. a before (interpTy a).
766data PArg' t = PImp { priority :: Int,
767 machine_inf :: Bool, -- true if the machine inferred it
769 pname :: Name, getTm :: t,
771 | PExp { priority :: Int,
775 | PConstraint { priority :: Int,
779 | PTacImplicit { priority :: Int,
785 deriving (Show, Eq, Functor)
787data ArgOpt = Lazy | HideDisplay
790lazyarg :: PArg' t -> Bool
791lazyarg tm = Lazy `elem` argopts tm
793instance Sized a => Sized (PArg' a) where
794 size (PImp p _ l nm trm _) = 1 + size nm + size trm
795 size (PExp p l trm _) = 1 + size trm
796 size (PConstraint p l trm _) = 1 + size trm
797 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
800deriving instance Binary PArg'
801deriving instance NFData PArg'
804pimp n t mach = PImp 1 mach [Lazy] n t ""
805pexp t = PExp 1 [] t ""
806pconst t = PConstraint 1 [] t ""
807ptacimp n s t = PTacImplicit 2 [Lazy] n s t ""
809type PArg = PArg' PTerm
813data ClassInfo = CI { instanceName :: Name,
814 class_methods :: [(Name, (FnOpts, PTerm))],
815 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
816 class_default_superclasses :: [PDecl],
817 class_params :: [Name],
818 class_instances :: [Name] }
821deriving instance Binary ClassInfo
822deriving instance NFData ClassInfo
825-- Type inference data
827data TIData = TIPartial -- ^ a function with a partially defined type
828 | TISolution [Term] -- ^ possible solutions to a metavariable in a type
831-- An argument is conditionally forceable iff its forceability
832-- depends on the collapsibility of the whole type.
833data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
836deriving instance Binary Forceability
837deriving instance NFData Forceability
840data OptInfo = Optimise { collapsible :: Bool,
842 -- The following should actually be (IntMap Forceability)
843 -- but the corresponding Binary instance seems to be broken.
844 -- Let's store a list and convert it to IntMap whenever needed.
845 forceable :: [(Int, Forceability)],
849deriving instance Binary OptInfo
850deriving instance NFData OptInfo
854data TypeInfo = TI { con_names :: [Name],
856 data_opts :: DataOpts,
860deriving instance Binary TypeInfo
861deriving instance NFData TypeInfo
864-- Syntactic sugar info
866data DSL' t = DSL { dsl_bind :: t,
871 index_first :: Maybe t,
872 index_next :: Maybe t,
873 dsl_lambda :: Maybe t,
876 deriving (Show, Functor)
878deriving instance Binary DSL'
879deriving instance NFData DSL'
884data SynContext = PatternSyntax | TermSyntax | AnySyntax
887deriving instance Binary SynContext
888deriving instance NFData SynContext
891data Syntax = Rule [SSymbol] PTerm SynContext
894deriving instance Binary Syntax
895deriving instance NFData Syntax
898data SSymbol = Keyword Name
905deriving instance Binary SSymbol
906deriving instance NFData SSymbol
909initDSL = DSL (PRef f (sUN ">>="))
910 (PRef f (sUN "return"))
912 (PRef f (sUN "pure"))
918 where f = fileFC "(builtin)"
920data Using = UImplicit Name PTerm
921 | UConstraint Name [Name]
924deriving instance Binary Using
925deriving instance NFData Using
928data SyntaxInfo = Syn { using :: [Using],
929 syn_params :: [(Name, PTerm)],
930 syn_namespace :: [String],
932 decoration :: Name -> Name,
934 implicitAllowed :: Bool,
938deriving instance NFData SyntaxInfo
939deriving instance Binary SyntaxInfo
942defaultSyntax = Syn [] [] [] [] id False False initDSL
944expandNS :: SyntaxInfo -> Name -> Name
945expandNS syn n@(NS _ _) = n
946expandNS syn n = case syn_namespace syn of
951-- For inferring types of things
955inferTy = sMN 0 "__Infer"
956inferCon = sMN 0 "__infer"
957inferDecl = PDatadecl inferTy
959 [("", inferCon, PPi impl (sMN 0 "iType") PType (
960 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
961 (PRef bi inferTy)), bi, [])]
964infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
965infP = P (TCon 6 0) inferTy (TType (UVal 0))
967getInferTerm, getInferType :: Term -> Term
968getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
969getInferTerm (App (App _ _) tm) = tm
970getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
972getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
973 where toTy (Lam t) = Pi t
974 toTy (PVar t) = PVTy t
976getInferType (App (App _ ty) _) = ty
980-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
982primNames = [unitTy, unitCon,
983 falseTy, pairTy, pairCon,
984 eqTy, eqCon, inferTy, inferCon]
986unitTy = sMN 0 "__Unit"
987unitCon = sMN 0 "__II"
988unitDecl = PDatadecl unitTy PType
989 [("", unitCon, PRef bi unitTy, bi, [])]
990unitOpts = [DefaultEliminator]
992falseTy = sMN 0 "__False"
993falseDecl = PDatadecl falseTy PType []
996pairTy = sMN 0 "__Pair"
997pairCon = sMN 0 "__MkPair"
998pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
999 [("", pairCon, PPi impl (n "A") PType (
1000 PPi impl (n "B") PType (
1001 PPi expl (n "a") (PRef bi (n "A")) (
1002 PPi expl (n "b") (PRef bi (n "B"))
1003 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
1004 pexp (PRef bi (n "B"))])))), bi, [])]
1010eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
1011 (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
1013 [("", eqCon, PPi impl (n "A") PType (
1014 PPi impl (n "x") (PRef bi (n "A"))
1015 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
1016 pimp (n "B") Placeholder False,
1017 pexp (PRef bi (n "x")),
1018 pexp (PRef bi (n "x"))])), bi, [])]
1022elimName = sUN "__Elim"
1023elimMethElimTy = sUN "__elimTy"
1024elimMethElim = sUN "elim"
1025elimDecl = PClass "Type class for eliminators" defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
1026 [PTy "" defaultSyntax bi [TotalFn] elimMethElimTy PType,
1027 PTy "" defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
1029-- Defined in builtins.idr
1030sigmaTy = sUN "Exists"
1031existsCon = sUN "Ex_intro"
1033piBind :: [(Name, PTerm)] -> PTerm -> PTerm
1034piBind = piBindp expl
1036piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
1038piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
1041-- Pretty-printing declarations and terms
1043-- These "show" instances render to an absurdly wide screen because inserted line breaks
1044-- could interfere with interactive editing, which calls "show".
1046instance Show PTerm where
1047 showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
1049instance Show PDecl where
1050 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
1052instance Show PClause where
1053 showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
1055instance Show PData where
1056 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
1058instance Pretty PTerm OutputAnnotation where
1059 pretty = prettyImp False
1061-- | Colourise annotations according to an Idris state. It ignores the names
1062-- in the annotation, as there's no good way to show extended information on a
1064consoleDecorate :: IState -> OutputAnnotation -> String -> String
1065consoleDecorate ist _ | not (idris_colourRepl ist) = id
1066consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1067 in colouriseData theme
1068consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1069 in colouriseType theme
1070consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1071consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1072consoleDecorate ist (AnnName n _ _) = let ctxt = tt_ctxt ist
1073 theme = idris_colourTheme ist
1075 _ | isDConName n ctxt -> colouriseData theme
1076 _ | isFnName n ctxt -> colouriseFun theme
1077 _ | isTConName n ctxt -> colouriseType theme
1078 _ | otherwise -> id -- don't colourise unknown names
1079consoleDecorate ist (AnnFC _) = id
1081-- | Pretty-print a high-level closed Idris term
1082prettyImp :: Bool -- ^^ whether to show implicits
1083 -> PTerm -- ^^ the term to pretty-print
1084 -> Doc OutputAnnotation
1085prettyImp impl = pprintPTerm impl []
1087-- | Pretty-print a high-level Idris term in some bindings context
1088pprintPTerm :: Bool -- ^^ whether to show implicits
1089 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1090 -> PTerm -- ^^ the term to pretty-print
1091 -> Doc OutputAnnotation
1092pprintPTerm impl bnd = prettySe 10 bnd
1094 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1095 prettySe p bnd (PQuote r) =
1096 text "![" <> pretty r <> text "]"
1097 prettySe p bnd (PPatvar fc n) = pretty n
1099 | Just str <- slist p bnd e = str
1100 | Just n <- snat p e = annotate AnnConstData (text (show n))
1101 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1102 prettySe p bnd (PLam n ty sc) =
1103 bracket p 2 . group . align . hang 2 $
1104 text "\\" <> bindingOf n False <+> text "=>" <$>
1105 prettySe 10 ((n, False):bnd) sc
1106 prettySe p bnd (PLet n ty v sc) =
1108 text "let" <+> bindingOf n False <+> text "=" </>
1109 prettySe 10 bnd v <+> text "in" </>
1110 prettySe 10 ((n, False):bnd) sc
1111 prettySe p bnd (PPi (Exp l s _ _) n ty sc)
1112 | n `elem` allNamesIn sc || impl =
1113 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1114 bracket p 2 . group $
1115 enclose open rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1116 st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
1118 bracket p 2 . group $
1119 group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
1123 Static -> text "[static]" <> space
1125 prettySe p bnd (PPi (Imp l s _ _) n ty sc)
1127 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1129 open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1130 st <> text "->" </> prettySe 10 ((n, True):bnd) sc
1131 | otherwise = prettySe 10 ((n, True):bnd) sc
1135 Static -> text "[static]" <> space
1137 prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
1139 prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
1140 prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
1142 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1143 rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
1144 prettySe p bnd (PApp _ (PRef _ f) args) -- normal names, no explicit args
1145 | UN nm <- basename f
1146 , not impl && null (getExps args) = if isAlpha (thead nm)
1147 then prettyName impl bnd f
1148 else enclose lparen rparen $
1149 prettyName impl bnd f
1150 prettySe p bnd (PAppBind _ (PRef _ f) [])
1151 | not impl = text "!" <> prettyName impl bnd f
1152 prettySe p bnd (PApp _ (PRef _ op) args)
1153 | UN nm <- basename op
1155 (not impl) && (not $ isAlpha (thead nm)) =
1156 case getExps args of
1157 [] -> enclose lparen rparen opName
1158 [x] -> group (enclose lparen rparen opName <$> group (prettySe 0 bnd x))
1159 [l,r] -> bracket p 1 $ inFix l r
1160 (l:r:rest) -> bracket p 1 $
1161 enclose lparen rparen (inFix l r) <+>
1162 align (group (vsep (map (prettyArgSe bnd) rest)))
1163 where opName = prettyName impl bnd op
1164 inFix l r = align . group $
1165 (prettySe 1 bnd l <+> opName) <$> group (prettySe 0 bnd r)
1166 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1167 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1168 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1169 prettySe p bnd (PApp _ f as) =
1170 let args = getExps as
1171 fp = prettySe 1 bnd f
1173 bracket p 1 . group $
1177 else fp <+> align (vsep (map (prettyArgS bnd) as))
1180 else fp <+> align (vsep (map (prettyArgSe bnd) args))
1181 prettySe p bnd (PCase _ scr opts) =
1182 text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
1184 prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1186 sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1187 prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1188 prettySe p bnd (PRefl _ _) = annotate (AnnName eqCon Nothing Nothing) $ text "refl"
1189 prettySe p bnd (PResolveTC _) = text "resolvetc"
1190 prettySe p bnd (PTrue _ IsType) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1191 prettySe p bnd (PTrue _ IsTerm) = annotate (AnnName unitCon Nothing Nothing) $ text "()"
1192 prettySe p bnd (PTrue _ TypeOrTerm) = text "()"
1193 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1194 prettySe p bnd (PEq _ l r) =
1195 bracket p 2 . align . group $
1196 prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
1197 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1198 prettySe p bnd (PRewrite _ l r _) =
1200 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1201 prettySe p bnd (PTyped l r) =
1202 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1203 prettySe p bnd pair@(PPair _ pun _ _) -- flatten tuples to the right, like parser
1204 | Just elts <- pairElts pair = enclose (ann lparen) (ann rparen) .
1205 align . group . vsep . punctuate (ann comma) $
1206 map (prettySe 10 bnd) elts
1207 where ann = case pun of
1209 IsType -> annotate (AnnName pairTy Nothing Nothing)
1210 IsTerm -> annotate (AnnName pairCon Nothing Nothing)
1211 prettySe p bnd (PDPair _ TypeOrTerm l t r) =
1212 lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1213 prettySe p bnd (PDPair _ IsType (PRef _ n) t r) =
1214 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1215 bindingOf n False <+>
1216 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1217 prettySe 10 ((n, False):bnd) r <>
1218 annotate (AnnName sigmaTy Nothing Nothing) rparen
1219 prettySe p bnd (PDPair _ IsType l t r) =
1220 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1221 prettySe 10 bnd l <+>
1222 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1223 prettySe 10 bnd r <>
1224 annotate (AnnName sigmaTy Nothing Nothing) rparen
1225 prettySe p bnd (PDPair _ IsTerm l t r) =
1226 annotate (AnnName existsCon Nothing Nothing) lparen <>
1227 prettySe 10 bnd l <+>
1228 annotate (AnnName existsCon Nothing Nothing) (text "**") <+>
1229 prettySe 10 bnd r <>
1230 annotate (AnnName existsCon Nothing Nothing) rparen
1231 prettySe p bnd (PAlternative a as) =
1232 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1235 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1236 prettySe p bnd PType = annotate AnnConstType $ text "Type"
1237 prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1238 where annot (AType _) = AnnConstType
1239 annot StrType = AnnConstType
1240 annot PtrType = AnnConstType
1241 annot VoidType = AnnConstType
1242 annot _ = AnnConstData
1243 -- XXX: add pretty for tactics
1244 prettySe p bnd (PProof ts) =
1245 text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1246 prettySe p bnd (PTactics ts) =
1247 text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1248 prettySe p bnd (PMetavar n) = text "?" <> pretty n
1249 prettySe p bnd (PReturn f) = text "return"
1250 prettySe p bnd PImpossible = text "impossible"
1251 prettySe p bnd Placeholder = text "_"
1252 prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1253 prettySe p bnd (PElabError s) = pretty s
1255 prettySe p bnd _ = text "test"
1257 prettyArgS bnd (PImp _ _ _ n tm _) = prettyArgSi bnd (n, tm)
1258 prettyArgS bnd (PExp _ _ tm _) = prettyArgSe bnd tm
1259 prettyArgS bnd (PConstraint _ _ tm _) = prettyArgSc bnd tm
1260 prettyArgS bnd (PTacImplicit _ _ n _ tm _) = prettyArgSti bnd (n, tm)
1262 prettyArgSe bnd arg = prettySe 0 bnd arg
1263 prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1264 prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1265 prettyArgSti bnd (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1267 basename :: Name -> Name
1268 basename (NS n _) = basename n
1271 slist' p bnd (PApp _ (PRef _ nil) _)
1272 | not impl && nsroot nil == sUN "Nil" = Just []
1273 slist' p bnd (PRef _ nil)
1274 | not impl && nsroot nil == sUN "Nil" = Just []
1275 slist' p bnd (PApp _ (PRef _ cons) args)
1276 | nsroot cons == sUN "::",
1277 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1279 Just tl' <- slist' p bnd tl
1282 isImp (PImp {}) = True
1284 slist' _ _ tm = Nothing
1286 slist p bnd e | Just es <- slist' p bnd e = Just $
1287 case es of [] -> annotate AnnConstData $ text "[]"
1288 [x] -> enclose left right . group $
1290 xs -> (enclose left right .
1291 align . group . vsep .
1293 map (prettySe p bnd)) xs
1294 where left = (annotate AnnConstData (text "["))
1295 right = (annotate AnnConstData (text "]"))
1296 comma = (annotate AnnConstData (text ","))
1297 slist _ _ _ = Nothing
1299 pairElts :: PTerm -> Maybe [PTerm]
1300 pairElts (PPair _ _ x y) | Just elts <- pairElts y = Just (x:elts)
1301 | otherwise = Just [x, y]
1302 pairElts _ = Nothing
1304 natns = "Prelude.Nat."
1307 | show z == (natns++"Z") || show z == "Z" = Just 0
1308 snat p (PApp _ s [PExp {getTm=n}])
1309 | show s == (natns++"S") || show s == "S",
1314 bracket outer inner doc
1315 | inner > outer = lparen <> doc <> rparen
1318-- | Pretty-printer helper for the binding site of a name
1319bindingOf :: Name -- ^^ the bound name
1320 -> Bool -- ^^ whether the name is implicit
1321 -> Doc OutputAnnotation
1322bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1324-- | Pretty-printer helper for names that attaches the correct annotations
1325prettyName :: Bool -- ^^ whether to show namespaces
1326 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1327 -> Name -- ^^ the name to pprint
1328 -> Doc OutputAnnotation
1329prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1330 | otherwise = annotate (AnnName n Nothing Nothing) (text (strName n))
1331 where strName (UN n) = T.unpack n
1332 strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1333 | otherwise = strName n
1334 strName (MN i s) = T.unpack s
1335 strName other = show other
1338showCImp :: Bool -> PClause -> Doc OutputAnnotation
1339showCImp impl (PClause _ n l ws r w)
1340 = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1341 <+> text "where" <+> text (show w)
1344 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1345showCImp impl (PWith _ n l ws r w)
1346 = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1347 <+> braces (text (show w))
1350 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1353showDImp :: Bool -> PData -> Doc OutputAnnotation
1354showDImp impl (PDatadecl n ty cons)
1355 = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1356 (indent 2 $ vsep (map (\ (_, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1358showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1359showDecls i ds = vsep (map (showDeclImp i) ds)
1361showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1362showDeclImp i (PTy _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1363showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1364 indent 2 (vsep (map (showCImp i) cs))
1365showDeclImp _ (PData _ _ _ _ d) = showDImp True d
1366showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1367showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1368showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1369showDeclImp i (PClass _ _ _ cs n ps ds)
1370 = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1371showDeclImp i (PInstance _ _ cs n _ t _ ds)
1372 = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1373showDeclImp _ _ = text "..."
1374-- showDeclImp (PImport i) = "import " ++ i
1376instance Show (Doc OutputAnnotation) where
1377 show = flip (displayS . renderCompact) ""
1379getImps :: [PArg] -> [(Name, PTerm)]
1381getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
1382getImps (_ : xs) = getImps xs
1384getExps :: [PArg] -> [PTerm]
1386getExps (PExp _ _ tm _ : xs) = tm : getExps xs
1387getExps (_ : xs) = getExps xs
1389getConsts :: [PArg] -> [PTerm]
1391getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
1392getConsts (_ : xs) = getConsts xs
1394getAll :: [PArg] -> [PTerm]
1399showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1400 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1401 -> Bool -- ^^ whether to show implicits
1402 -> Bool -- ^^ whether to colourise
1403 -> Name -- ^^ the term to show
1405showName ist bnd impl colour n = case ist of
1406 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1407 Nothing -> showbasic n
1408 where name = if impl then show n else showbasic n
1409 showbasic n@(UN _) = showCG n
1410 showbasic (MN _ s) = str s
1411 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1412 showbasic (SN s) = show s
1414 colourise n t = let ctxt' = fmap tt_ctxt ist in
1417 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1418 else colouriseBound t name
1419 | isDConName n ctxt -> colouriseData t name
1420 | isFnName n ctxt -> colouriseFun t name
1421 | isTConName n ctxt -> colouriseType t name
1422 -- The assumption is that if a name is not bound and does not exist in the
1423 -- global context, then we're somewhere in which implicit info has been lost
1424 -- (like error messages). Thus, unknown vars are colourised as implicits.
1425 | otherwise -> colouriseImplicit t name
1427showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1428 -> PTerm -- ^^ the term to show
1430showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
1432-- | Show a term with implicits, no colours
1433showTmImpls :: PTerm -> String
1434showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1437instance Sized PTerm where
1438 size (PQuote rawTerm) = size rawTerm
1439 size (PRef fc name) = size name
1440 size (PLam name ty bdy) = 1 + size ty + size bdy
1441 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1442 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1443 size (PTyped trm ty) = 1 + size trm + size ty
1444 size (PApp fc name args) = 1 + size args
1445 size (PAppBind fc name args) = 1 + size args
1446 size (PCase fc trm bdy) = 1 + size trm + size bdy
1447 size (PTrue fc _) = 1
1448 size (PFalse fc) = 1
1449 size (PRefl fc _) = 1
1450 size (PResolveTC fc) = 1
1451 size (PEq fc left right) = 1 + size left + size right
1452 size (PRewrite fc left right _) = 1 + size left + size right
1453 size (PPair fc _ left right) = 1 + size left + size right
1454 size (PDPair fs _ left ty right) = 1 + size left + size ty + size right
1455 size (PAlternative a alts) = 1 + size alts
1456 size (PHidden hidden) = size hidden
1457 size (PUnifyLog tm) = size tm
1458 size (PDisamb _ tm) = size tm
1459 size (PNoImplicits tm) = size tm
1461 size (PConstant const) = 1 + size const
1462 size Placeholder = 1
1463 size (PDoBlock dos) = 1 + size dos
1464 size (PIdiom fc term) = 1 + size term
1465 size (PReturn fc) = 1
1466 size (PMetavar name) = 1
1467 size (PProof tactics) = size tactics
1468 size (PElabError err) = size err
1469 size PImpossible = 1
1471getPArity :: PTerm -> Int
1472getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1475-- Return all names, free or globally bound, in the given term.
1477allNamesIn :: PTerm -> [Name]
1478allNamesIn tm = nub $ ni [] tm
1481 | not (n `elem` env) = [n]
1482 ni env (PPatvar _ n) = [n]
1483 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1484 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1485 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1486 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1487 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1488 ni env (PHidden tm) = ni env tm
1489 ni env (PEq _ l r) = ni env l ++ ni env r
1490 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1491 ni env (PTyped l r) = ni env l ++ ni env r
1492 ni env (PPair _ _ l r) = ni env l ++ ni env r
1493 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1494 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1495 ni env (PAlternative a ls) = concatMap (ni env) ls
1496 ni env (PUnifyLog tm) = ni env tm
1497 ni env (PDisamb _ tm) = ni env tm
1498 ni env (PNoImplicits tm) = ni env tm
1501-- Return all names defined in binders in the given term
1502boundNamesIn :: PTerm -> [Name]
1503boundNamesIn tm = nub $ ni tm
1505 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1506 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1507 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1508 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1509 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1510 ni (PPi _ n ty sc) = n : (ni ty ++ ni sc)
1511 ni (PEq _ l r) = ni l ++ ni r
1512 ni (PRewrite _ l r _) = ni l ++ ni r
1513 ni (PTyped l r) = ni l ++ ni r
1514 ni (PPair _ _ l r) = ni l ++ ni r
1515 ni (PDPair _ _ (PRef _ n) t r) = ni t ++ ni r
1516 ni (PDPair _ _ l t r) = ni l ++ ni t ++ ni r
1517 ni (PAlternative a as) = concatMap (ni) as
1518 ni (PHidden tm) = ni tm
1519 ni (PUnifyLog tm) = ni tm
1520 ni (PDisamb _ tm) = ni tm
1521 ni (PNoImplicits tm) = ni tm
1524-- Return names which are free in the given term.
1525namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1526namesIn uvars ist tm = nub $ ni [] tm
1529 | not (n `elem` env)
1530 = case lookupTy n (tt_ctxt ist) of
1532 _ -> if n `elem` (map fst uvars) then [n] else []
1533 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1534 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1535 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1536 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1537 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1538 ni env (PEq _ l r) = ni env l ++ ni env r
1539 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1540 ni env (PTyped l r) = ni env l ++ ni env r
1541 ni env (PPair _ _ l r) = ni env l ++ ni env r
1542 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1543 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1544 ni env (PAlternative a as) = concatMap (ni env) as
1545 ni env (PHidden tm) = ni env tm
1546 ni env (PUnifyLog tm) = ni env tm
1547 ni env (PDisamb _ tm) = ni env tm
1548 ni env (PNoImplicits tm) = ni env tm
1551-- Return which of the given names are used in the given term.
1553usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1554usedNamesIn vars ist tm = nub $ ni [] tm
1557 | n `elem` vars && not (n `elem` env)
1558 = case lookupTy n (tt_ctxt ist) of
1561 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1562 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1563 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1564 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1565 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1566 ni env (PEq _ l r) = ni env l ++ ni env r
1567 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1568 ni env (PTyped l r) = ni env l ++ ni env r
1569 ni env (PPair _ _ l r) = ni env l ++ ni env r
1570 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1571 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1572 ni env (PAlternative a as) = concatMap (ni env) as
1573 ni env (PHidden tm) = ni env tm
1574 ni env (PUnifyLog tm) = ni env tm
1575 ni env (PDisamb _ tm) = ni env tm
1576 ni env (PNoImplicits tm) = ni env tm
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list