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 lastParse :: Maybe Name,
137 indent_stack :: [Int],
138 brace_stack :: [Maybe Int],
139 hide_list :: [(Name, Maybe Accessibility)],
140 default_access :: Accessibility,
141 default_total :: Bool,
142 ibc_write :: [IBCWrite],
143 compiled_so :: Maybe String,
144 idris_dynamic_libs :: [DynamicLib],
145 idris_language_extensions :: [LanguageExt],
146 idris_outputmode :: OutputMode,
147 idris_colourRepl :: Bool,
148 idris_colourTheme :: ColourTheme,
149 idris_outh :: Handle,
150 idris_errorhandlers :: [Name], -- ^ Global error handlers
151 idris_nameIdx :: (Int, Ctxt (Int, Name)),
152 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
153 module_aliases :: M.Map [T.Text] [T.Text],
154 idris_consolewidth :: ConsoleWidth -- ^ How many chars wide is the console?
157data SizeChange = Smaller | Same | Bigger | Unknown
160deriving instance Binary SizeChange
161deriving instance NFData SizeChange
164type SCGEntry = (Name, [Maybe (Int, SizeChange)])
166data CGInfo = CGInfo { argsdef :: [Name],
167 calls :: [(Name, [[Name]])],
173deriving instance Binary CGInfo
174deriving instance NFData CGInfo
177primDefs = [sUN "unsafePerformPrimIO",
178 sUN "mkLazyForeignPrim",
182-- information that needs writing for the current module's .ibc file
183data IBCWrite = IBCFix FixDecl
187 | IBCInstance Bool Name Name
194 | IBCObj Codegen FilePath
195 | IBCLib Codegen String
196 | IBCCGFlag Codegen String
198 | IBCHeader Codegen String
199 | IBCAccess Name Accessibility
200 | IBCMetaInformation Name MetaInformation
201 | IBCTotal Name Totality
202 | IBCFlags Name [FnOpt]
203 | IBCTrans (Term, Term)
204 | IBCErrRev (Term, Term)
208 | IBCDef Name -- i.e. main context
209 | IBCNameHint (Name, Name)
210 | IBCLineApp FilePath Int PTerm
211 | IBCErrorHandler Name
212 | IBCFunctionErrorHandler Name Name Name
215-- | The initial state for the compiler
217idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
218 emptyContext emptyContext emptyContext emptyContext
219 emptyContext emptyContext emptyContext emptyContext
220 emptyContext emptyContext
221 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
222 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
223 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
226-- | The monad for the main REPL - reading and processing files and updating
227-- global state (hence the IO inner monad).
228--type Idris = WriterT [Either String (IO ())] (State IState a))
229type Idris = StateT IState (ErrorT Err IO)
231-- Commands in the REPL
250 | ChangeDirectory FilePath
253 | Compile Codegen String
258 | AddProof (Maybe Name)
270 | DynamicLink FilePath
275 | CaseSplitAt Bool Int Name
276 | AddClauseFrom Bool Int Name
277 | AddProofClauseFrom Bool Int Name
278 | AddMissing Bool Int Name
279 | MakeWith Bool Int Name
280 | DoProofSearch Bool Int Name [Name]
284 | SetColour ColourType IdrisColour
288 | SetConsoleWidth ConsoleWidth
290data Opt = Filename String
302 | NoBuiltins -- only for the really primitive stuff!
328 | OutputTy OutputType
329 | Extension LanguageExt
330 | InterpretScript String
331 | TargetTriple String
336 | AutoWidth -- ^ Automatically adjust terminal width
339-- Parsed declarations
341data Fixity = Infixl { prec :: Int }
342 | Infixr { prec :: Int }
343 | InfixN { prec :: Int }
344 | PrefixN { prec :: Int }
347deriving instance Binary Fixity
348deriving instance NFData Fixity
351instance Show Fixity where
352 show (Infixl i) = "infixl " ++ show i
353 show (Infixr i) = "infixr " ++ show i
354 show (InfixN i) = "infix " ++ show i
355 show (PrefixN i) = "prefix " ++ show i
357data FixDecl = Fix Fixity String
360instance Show FixDecl where
361 show (Fix f s) = show f ++ " " ++ s
364deriving instance Binary FixDecl
365deriving instance NFData FixDecl
368instance Ord FixDecl where
369 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
372data Static = Static | Dynamic
375deriving instance Binary Static
376deriving instance NFData Static
379-- Mark bindings with their explicitness, and laziness
380data Plicity = Imp { pargopts :: [ArgOpt],
384 | Exp { pargopts :: [ArgOpt],
388 | Constraint { pargopts :: [ArgOpt],
391 | TacImp { pargopts :: [ArgOpt],
397plazy :: Plicity -> Bool
398plazy tm = Lazy `elem` pargopts tm
401deriving instance Binary Plicity
402deriving instance NFData Plicity
405impl = Imp [Lazy] Dynamic "" False
406expl = Exp [] Dynamic "" False
407expl_param = Exp [] Dynamic "" True
408constraint = Constraint [] Dynamic ""
409tacimpl t = TacImp [] Dynamic t ""
411data FnOpt = Inlinable -- always evaluate when simplifying
412 | TotalFn | PartialFn
413 | Coinductive | AssertTotal
414 | Dictionary -- type class dictionary, eval only when
415 -- a function argument, and further evaluation resutls
416 | Implicit -- implicit coercion
417 | CExport String -- export, with a C name
418 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
419 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
420 | Reflection -- a reflecting function, compile-time only
421 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
424deriving instance Binary FnOpt
425deriving instance NFData FnOpt
430inlinable :: FnOpts -> Bool
431inlinable = elem Inlinable
433dictionary :: FnOpts -> Bool
434dictionary = elem Dictionary
437-- | Data declaration options
438data DataOpt = Codata -- Set if the the data-type is coinductive
439 | DefaultEliminator -- Set if an eliminator should be generated for data type
443type DataOpts = [DataOpt]
445-- | Top-level declarations such as compiler directives, definitions,
446-- datatypes and typeclasses.
448 = PFix FC Fixity [String] -- ^ Fixity declaration
449 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
450 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
451 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
452 | PCAF FC Name t -- ^ Top level constant
453 | PData String SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
454 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
455 | PNamespace String [PDecl' t] -- ^ New namespace
456 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
457 | PClass String SyntaxInfo FC
460 [(Name, t)] -- parameters
461 [PDecl' t] -- declarations
462 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
463 -- class name, parameters, method declarations
464 | PInstance SyntaxInfo FC [t] -- constraints
467 t -- full instance type
468 (Maybe Name) -- explicit name
470 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
471 -- class name, parameters, full instance type, optional explicit name, and definitions
472 | PDSL Name (DSL' t) -- ^ DSL declaration
473 | PSyntax FC Syntax -- ^ Syntax definition
474 | PMutual FC [PDecl' t] -- ^ Mutual block
475 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
476 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
477 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
478 -- bool is True, lhs and rhs must be convertible
481deriving instance Binary PDecl'
482deriving instance NFData PDecl'
485-- For elaborator state
486type ElabD a = Elab' [PDecl] a
488-- | One clause of a top-level definition. Term arguments to constructors are:
490-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
492-- 2. The list of extra 'with' patterns
494-- 3. The right-hand side
496-- 4. The where block (PDecl' t)
498data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
499 | PWith FC Name t [t] t [PDecl' t]
500 | PClauseR FC [t] t [PDecl' t]
501 | PWithR FC [t] t [PDecl' t]
504deriving instance Binary PClause'
505deriving instance NFData PClause'
509data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
510 d_tcon :: t, -- ^ Type constructor
511 d_cons :: [(String, Name, t, FC, [Name])] -- ^ Constructors
513 -- ^ Data declaration
514 | PLaterdecl { d_name :: Name, d_tcon :: t }
515 -- ^ "Placeholder" for data whose constructors are defined later
518deriving instance Binary PData'
519deriving instance NFData PData'
522-- Handy to get a free function for applying PTerm -> PTerm functions
523-- across a program, by deriving Functor
525type PDecl = PDecl' PTerm
526type PData = PData' PTerm
527type PClause = PClause' PTerm
529-- get all the names declared in a decl
531declared :: PDecl -> [Name]
532declared (PFix _ _ _) = []
533declared (PTy _ _ _ _ n t) = [n]
534declared (PPostulate _ _ _ _ n t) = [n]
535declared (PClauses _ _ n _) = [] -- not a declaration
536declared (PCAF _ n _) = [n]
537declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
538 where fstt (_, a, _, _, _) = a
539declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
540declared (PParams _ _ ds) = concatMap declared ds
541declared (PNamespace _ ds) = concatMap declared ds
542declared (PRecord _ _ _ n _ _ c _) = [n, c]
543declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
544declared (PInstance _ _ _ _ _ _ _ _) = []
545declared (PDSL n _) = [n]
546declared (PSyntax _ _) = []
547declared (PMutual _ ds) = concatMap declared ds
548declared (PDirective _) = []
550-- get the names declared, not counting nested parameter blocks
551tldeclared :: PDecl -> [Name]
552tldeclared (PFix _ _ _) = []
553tldeclared (PTy _ _ _ _ n t) = [n]
554tldeclared (PPostulate _ _ _ _ n t) = [n]
555tldeclared (PClauses _ _ n _) = [] -- not a declaration
556tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
557tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
558 where fstt (_, a, _, _, _) = a
559tldeclared (PParams _ _ ds) = []
560tldeclared (PMutual _ ds) = concatMap tldeclared ds
561tldeclared (PNamespace _ ds) = concatMap tldeclared ds
562tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
563tldeclared (PInstance _ _ _ _ _ _ _ _) = []
566defined :: PDecl -> [Name]
567defined (PFix _ _ _) = []
568defined (PTy _ _ _ _ n t) = []
569defined (PPostulate _ _ _ _ n t) = []
570defined (PClauses _ _ n _) = [n] -- not a declaration
571defined (PCAF _ n _) = [n]
572defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
573 where fstt (_, a, _, _, _) = a
574defined (PData _ _ _ _ (PLaterdecl n _)) = []
575defined (PParams _ _ ds) = concatMap defined ds
576defined (PNamespace _ ds) = concatMap defined ds
577defined (PRecord _ _ _ n _ _ c _) = [n, c]
578defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
579defined (PInstance _ _ _ _ _ _ _ _) = []
580defined (PDSL n _) = [n]
581defined (PSyntax _ _) = []
582defined (PMutual _ ds) = concatMap defined ds
583defined (PDirective _) = []
586updateN :: [(Name, Name)] -> Name -> Name
587updateN ns n | Just n' <- lookup n ns = n'
590updateNs :: [(Name, Name)] -> PTerm -> PTerm
592updateNs ns t = mapPT updateRef t
593 where updateRef (PRef fc f) = PRef fc (updateN ns f)
596-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
598-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
599-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
600-- where updateCNs ns (PClause n l ts r ds)
601-- = PClause (updateN ns n) (fmap (updateNs ns) l)
602-- (map (fmap (updateNs ns)) ts)
603-- (fmap (updateNs ns) r)
604-- (map (updateDNs ns) ds)
607data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show)
609-- | High level language terms
610data PTerm = PQuote Raw
612 | PInferRef FC Name -- ^ A name to be defined later
614 | PLam Name PTerm PTerm
615 | PPi Plicity Name PTerm PTerm
616 | PLet Name PTerm PTerm PTerm
617 | PTyped PTerm PTerm -- ^ Term with explicit type
618 | PApp FC PTerm [PArg]
619 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
620 | PMatchApp FC Name -- ^ Make an application by type matching
621 | PCase FC PTerm [(PTerm, PTerm)]
627 | PRewrite FC PTerm PTerm (Maybe PTerm)
628 | PPair FC PunInfo PTerm PTerm
629 | PDPair FC PunInfo PTerm PTerm PTerm
630 | PAlternative Bool [PTerm] -- True if only one may work
631 | PHidden PTerm -- ^ Irrelevant or hidden pattern
633 | PGoal FC PTerm Name PTerm
640 | PProof [PTactic] -- ^ Proof script
641 | PTactics [PTactic] -- ^ As PProof, but no auto solving
642 | PElabError Err -- ^ Error to report on elaboration
643 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
644 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
645 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
646 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
647 | PNoImplicits PTerm -- ^ never run implicit converions on the term
652deriving instance Binary PTerm
653deriving instance NFData PTerm
656mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
657mapPT f t = f (mpt t) where
658 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
659 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
660 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
661 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
663 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
664 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
665 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
666 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
667 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
668 mpt (PPair fc p l r) = PPair fc p (mapPT f l) (mapPT f r)
669 mpt (PDPair fc p l t r) = PDPair fc p (mapPT f l) (mapPT f t) (mapPT f r)
670 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
671 mpt (PHidden t) = PHidden (mapPT f t)
672 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
673 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
674 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
675 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
676 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
677 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
678 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
682data PTactic' t = Intro [Name] | Intros | Focus Name
683 | Refine Name [Bool] | Rewrite t
687 | LetTac Name t | LetTacTy Name t t
688 | Exact t | Compute | Trivial | TCInstance
689 | ProofSearch (Maybe Name) Name [Name]
692 | ProofState | ProofTerm | Undo
693 | Try (PTactic' t) (PTactic' t)
694 | TSeq (PTactic' t) (PTactic' t)
695 | ApplyTactic t -- see Language.Reflection module
699 | GoalType String (PTactic' t)
701 deriving (Show, Eq, Functor)
703deriving instance Binary PTactic'
704deriving instance NFData PTactic'
706instance Sized a => Sized (PTactic' a) where
707 size (Intro nms) = 1 + size nms
709 size (Focus nm) = 1 + size nm
710 size (Refine nm bs) = 1 + size nm + length bs
711 size (Rewrite t) = 1 + size t
712 size (Induction t) = 1 + size t
713 size (LetTac nm t) = 1 + size nm + size t
714 size (Exact t) = 1 + size t
722 size (Try l r) = 1 + size l + size r
723 size (TSeq l r) = 1 + size l + size r
724 size (ApplyTactic t) = 1 + size t
725 size (Reflect t) = 1 + size t
726 size (Fill t) = 1 + size t
730type PTactic = PTactic' PTerm
732data PDo' t = DoExp FC t
737 deriving (Eq, Functor)
739deriving instance Binary PDo'
740deriving instance NFData PDo'
743instance Sized a => Sized (PDo' a) where
744 size (DoExp fc t) = 1 + size fc + size t
745 size (DoBind fc nm t) = 1 + size fc + size nm + size t
746 size (DoBindP fc l r) = 1 + size fc + size l + size r
747 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
748 size (DoLetP fc l r) = 1 + size fc + size l + size r
752-- The priority gives a hint as to elaboration order. Best to elaborate
753-- things early which will help give a more concrete type to other
754-- variables, e.g. a before (interpTy a).
756data PArg' t = PImp { priority :: Int,
757 machine_inf :: Bool, -- true if the machine inferred it
759 pname :: Name, getTm :: t,
761 | PExp { priority :: Int,
765 | PConstraint { priority :: Int,
769 | PTacImplicit { priority :: Int,
775 deriving (Show, Eq, Functor)
777data ArgOpt = Lazy | HideDisplay
780lazyarg :: PArg' t -> Bool
781lazyarg tm = Lazy `elem` argopts tm
783instance Sized a => Sized (PArg' a) where
784 size (PImp p _ l nm trm _) = 1 + size nm + size trm
785 size (PExp p l trm _) = 1 + size trm
786 size (PConstraint p l trm _) = 1 + size trm
787 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
790deriving instance Binary PArg'
791deriving instance NFData PArg'
794pimp n t mach = PImp 1 mach [Lazy] n t ""
795pexp t = PExp 1 [] t ""
796pconst t = PConstraint 1 [] t ""
797ptacimp n s t = PTacImplicit 2 [Lazy] n s t ""
799type PArg = PArg' PTerm
803data ClassInfo = CI { instanceName :: Name,
804 class_methods :: [(Name, (FnOpts, PTerm))],
805 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
806 class_default_superclasses :: [PDecl],
807 class_params :: [Name],
808 class_instances :: [Name] }
811deriving instance Binary ClassInfo
812deriving instance NFData ClassInfo
815-- Type inference data
817data TIData = TIPartial -- ^ a function with a partially defined type
818 | TISolution [Term] -- ^ possible solutions to a metavariable in a type
821-- An argument is conditionally forceable iff its forceability
822-- depends on the collapsibility of the whole type.
823data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
826deriving instance Binary Forceability
827deriving instance NFData Forceability
830data OptInfo = Optimise { collapsible :: Bool,
832 -- The following should actually be (IntMap Forceability)
833 -- but the corresponding Binary instance seems to be broken.
834 -- Let's store a list and convert it to IntMap whenever needed.
835 forceable :: [(Int, Forceability)],
839deriving instance Binary OptInfo
840deriving instance NFData OptInfo
844data TypeInfo = TI { con_names :: [Name],
846 data_opts :: DataOpts,
850deriving instance Binary TypeInfo
851deriving instance NFData TypeInfo
854-- Syntactic sugar info
856data DSL' t = DSL { dsl_bind :: t,
861 index_first :: Maybe t,
862 index_next :: Maybe t,
863 dsl_lambda :: Maybe t,
866 deriving (Show, Functor)
868deriving instance Binary DSL'
869deriving instance NFData DSL'
874data SynContext = PatternSyntax | TermSyntax | AnySyntax
877deriving instance Binary SynContext
878deriving instance NFData SynContext
881data Syntax = Rule [SSymbol] PTerm SynContext
884deriving instance Binary Syntax
885deriving instance NFData Syntax
888data SSymbol = Keyword Name
895deriving instance Binary SSymbol
896deriving instance NFData SSymbol
899initDSL = DSL (PRef f (sUN ">>="))
900 (PRef f (sUN "return"))
902 (PRef f (sUN "pure"))
908 where f = fileFC "(builtin)"
910data Using = UImplicit Name PTerm
911 | UConstraint Name [Name]
914deriving instance Binary Using
915deriving instance NFData Using
918data SyntaxInfo = Syn { using :: [Using],
919 syn_params :: [(Name, PTerm)],
920 syn_namespace :: [String],
922 decoration :: Name -> Name,
924 implicitAllowed :: Bool,
928deriving instance NFData SyntaxInfo
929deriving instance Binary SyntaxInfo
932defaultSyntax = Syn [] [] [] [] id False False initDSL
934expandNS :: SyntaxInfo -> Name -> Name
935expandNS syn n@(NS _ _) = n
936expandNS syn n = case syn_namespace syn of
941-- For inferring types of things
945inferTy = sMN 0 "__Infer"
946inferCon = sMN 0 "__infer"
947inferDecl = PDatadecl inferTy
949 [("", inferCon, PPi impl (sMN 0 "iType") PType (
950 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
951 (PRef bi inferTy)), bi, [])]
954infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
955infP = P (TCon 6 0) inferTy (TType (UVal 0))
957getInferTerm, getInferType :: Term -> Term
958getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
959getInferTerm (App (App _ _) tm) = tm
960getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
962getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
963 where toTy (Lam t) = Pi t
964 toTy (PVar t) = PVTy t
966getInferType (App (App _ ty) _) = ty
970-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
972primNames = [unitTy, unitCon,
973 falseTy, pairTy, pairCon,
974 eqTy, eqCon, inferTy, inferCon]
976unitTy = sMN 0 "__Unit"
977unitCon = sMN 0 "__II"
978unitDecl = PDatadecl unitTy PType
979 [("", unitCon, PRef bi unitTy, bi, [])]
980unitOpts = [DefaultEliminator]
982falseTy = sMN 0 "__False"
983falseDecl = PDatadecl falseTy PType []
986pairTy = sMN 0 "__Pair"
987pairCon = sMN 0 "__MkPair"
988pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
989 [("", pairCon, PPi impl (n "A") PType (
990 PPi impl (n "B") PType (
991 PPi expl (n "a") (PRef bi (n "A")) (
992 PPi expl (n "b") (PRef bi (n "B"))
993 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
994 pexp (PRef bi (n "B"))])))), bi, [])]
1000eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
1001 (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
1003 [("", eqCon, PPi impl (n "A") PType (
1004 PPi impl (n "x") (PRef bi (n "A"))
1005 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
1006 pimp (n "B") Placeholder False,
1007 pexp (PRef bi (n "x")),
1008 pexp (PRef bi (n "x"))])), bi, [])]
1012elimName = sUN "__Elim"
1013elimMethElimTy = sUN "__elimTy"
1014elimMethElim = sUN "elim"
1015elimDecl = PClass "Type class for eliminators" defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
1016 [PTy "" defaultSyntax bi [TotalFn] elimMethElimTy PType,
1017 PTy "" defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
1019-- Defined in builtins.idr
1020sigmaTy = sUN "Exists"
1021existsCon = sUN "Ex_intro"
1023piBind :: [(Name, PTerm)] -> PTerm -> PTerm
1024piBind = piBindp expl
1026piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
1028piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
1031-- Pretty-printing declarations and terms
1033-- These "show" instances render to an absurdly wide screen because inserted line breaks
1034-- could interfere with interactive editing, which calls "show".
1036instance Show PTerm where
1037 showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
1039instance Show PDecl where
1040 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
1042instance Show PClause where
1043 showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
1045instance Show PData where
1046 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
1048instance Pretty PTerm OutputAnnotation where
1049 pretty = prettyImp False
1051-- | Colourise annotations according to an Idris state. It ignores the names
1052-- in the annotation, as there's no good way to show extended information on a
1054consoleDecorate :: IState -> OutputAnnotation -> String -> String
1055consoleDecorate ist _ | not (idris_colourRepl ist) = id
1056consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1057 in colouriseData theme
1058consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1059 in colouriseType theme
1060consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1061consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1062consoleDecorate ist (AnnName n _ _) = let ctxt = tt_ctxt ist
1063 theme = idris_colourTheme ist
1065 _ | isDConName n ctxt -> colouriseData theme
1066 _ | isFnName n ctxt -> colouriseFun theme
1067 _ | isTConName n ctxt -> colouriseType theme
1068 _ | otherwise -> id -- don't colourise unknown names
1069consoleDecorate ist (AnnFC _) = id
1071-- | Pretty-print a high-level closed Idris term
1072prettyImp :: Bool -- ^^ whether to show implicits
1073 -> PTerm -- ^^ the term to pretty-print
1074 -> Doc OutputAnnotation
1075prettyImp impl = pprintPTerm impl []
1077-- | Pretty-print a high-level Idris term in some bindings context
1078pprintPTerm :: Bool -- ^^ whether to show implicits
1079 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1080 -> PTerm -- ^^ the term to pretty-print
1081 -> Doc OutputAnnotation
1082pprintPTerm impl bnd = prettySe 10 bnd
1084 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1085 prettySe p bnd (PQuote r) =
1086 text "![" <> pretty r <> text "]"
1087 prettySe p bnd (PPatvar fc n) = pretty n
1089 | Just str <- slist p bnd e = str
1090 | Just n <- snat p e = annotate AnnConstData (text (show n))
1091 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1092 prettySe p bnd (PLam n ty sc) =
1093 bracket p 2 . group . align . hang 2 $
1094 text "\\" <> bindingOf n False <+> text "=>" <$>
1095 prettySe 10 ((n, False):bnd) sc
1096 prettySe p bnd (PLet n ty v sc) =
1098 text "let" <+> bindingOf n False <+> text "=" </>
1099 prettySe 10 bnd v <+> text "in" </>
1100 prettySe 10 ((n, False):bnd) sc
1101 prettySe p bnd (PPi (Exp l s _ _) n ty sc)
1102 | n `elem` allNamesIn sc || impl =
1103 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1104 bracket p 2 . group $
1105 enclose open rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1106 st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
1108 bracket p 2 . group $
1109 group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
1113 Static -> text "[static]" <> space
1115 prettySe p bnd (PPi (Imp l s _ _) n ty sc)
1117 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1119 open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1120 st <> text "->" </> prettySe 10 ((n, True):bnd) sc
1121 | otherwise = prettySe 10 ((n, True):bnd) sc
1125 Static -> text "[static]" <> space
1127 prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
1129 prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
1130 prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
1132 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1133 rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
1134 prettySe p bnd (PApp _ (PRef _ f) [])
1135 | not impl = prettyName impl bnd f
1136 prettySe p bnd (PAppBind _ (PRef _ f) [])
1137 | not impl = text "!" <> prettyName impl bnd f
1138 prettySe p bnd (PApp _ (PRef _ op) args)
1139 | UN nm <- basename op
1141 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
1142 let [l, r] = getExps args in
1143 bracket p 1 . align . group $
1144 (prettySe 1 bnd l <+> prettyName impl bnd op) <$> group (prettySe 0 bnd r)
1145 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1146 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1147 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1148 prettySe p bnd (PApp _ f as) =
1149 let args = getExps as
1150 fp = prettySe 1 bnd f
1152 bracket p 1 . group $
1156 else fp <+> align (vsep (map (prettyArgS bnd) as))
1159 else fp <+> align (vsep (map (prettyArgSe bnd) args))
1160 prettySe p bnd (PCase _ scr opts) =
1161 text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
1163 prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1165 sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1166 prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1167 prettySe p bnd (PRefl _ _) = annotate (AnnName eqCon Nothing Nothing) $ text "refl"
1168 prettySe p bnd (PResolveTC _) = text "resolvetc"
1169 prettySe p bnd (PTrue _ IsType) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1170 prettySe p bnd (PTrue _ IsTerm) = annotate (AnnName unitCon Nothing Nothing) $ text "()"
1171 prettySe p bnd (PTrue _ TypeOrTerm) = text "()"
1172 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1173 prettySe p bnd (PEq _ l r) =
1174 bracket p 2 . align . group $
1175 prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
1176 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1177 prettySe p bnd (PRewrite _ l r _) =
1179 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1180 prettySe p bnd (PTyped l r) =
1181 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1182 prettySe p bnd (PPair _ TypeOrTerm l r) =
1183 lparen <> prettySe 10 bnd l <> text "," <+> prettySe 10 bnd r <> rparen
1184 prettySe p bnd (PPair _ IsType l r) =
1185 annotate (AnnName pairTy Nothing Nothing) lparen <>
1186 prettySe 10 bnd l <>
1187 annotate (AnnName pairTy Nothing Nothing) comma <+>
1188 prettySe 10 bnd r <>
1189 annotate (AnnName pairTy Nothing Nothing) rparen
1190 prettySe p bnd (PPair _ IsTerm l r) =
1191 annotate (AnnName pairCon Nothing Nothing) lparen <>
1192 prettySe 10 bnd l <>
1193 annotate (AnnName pairCon Nothing Nothing) comma <+>
1194 prettySe 10 bnd r <>
1195 annotate (AnnName pairCon Nothing Nothing) rparen
1196 prettySe p bnd (PDPair _ TypeOrTerm l t r) =
1197 lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1198 prettySe p bnd (PDPair _ IsType (PRef _ n) t r) =
1199 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1200 bindingOf n False <+>
1201 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1202 prettySe 10 ((n, False):bnd) r <>
1203 annotate (AnnName sigmaTy Nothing Nothing) rparen
1204 prettySe p bnd (PDPair _ IsType l t r) =
1205 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1206 prettySe 10 bnd l <+>
1207 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1208 prettySe 10 bnd r <>
1209 annotate (AnnName sigmaTy Nothing Nothing) rparen
1210 prettySe p bnd (PDPair _ IsTerm l t r) =
1211 annotate (AnnName existsCon Nothing Nothing) lparen <>
1212 prettySe 10 bnd l <+>
1213 annotate (AnnName existsCon Nothing Nothing) (text "**") <+>
1214 prettySe 10 bnd r <>
1215 annotate (AnnName existsCon Nothing Nothing) rparen
1216 prettySe p bnd (PAlternative a as) =
1217 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1220 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1221 prettySe p bnd PType = annotate AnnConstType $ text "Type"
1222 prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1223 where annot (AType _) = AnnConstType
1224 annot StrType = AnnConstType
1225 annot PtrType = AnnConstType
1226 annot VoidType = AnnConstType
1227 annot _ = AnnConstData
1228 -- XXX: add pretty for tactics
1229 prettySe p bnd (PProof ts) =
1230 text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1231 prettySe p bnd (PTactics ts) =
1232 text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1233 prettySe p bnd (PMetavar n) = text "?" <> pretty n
1234 prettySe p bnd (PReturn f) = text "return"
1235 prettySe p bnd PImpossible = text "impossible"
1236 prettySe p bnd Placeholder = text "_"
1237 prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1238 prettySe p bnd (PElabError s) = pretty s
1240 prettySe p bnd _ = text "test"
1242 prettyArgS bnd (PImp _ _ _ n tm _) = prettyArgSi bnd (n, tm)
1243 prettyArgS bnd (PExp _ _ tm _) = prettyArgSe bnd tm
1244 prettyArgS bnd (PConstraint _ _ tm _) = prettyArgSc bnd tm
1245 prettyArgS bnd (PTacImplicit _ _ n _ tm _) = prettyArgSti bnd (n, tm)
1247 prettyArgSe bnd arg = prettySe 0 bnd arg
1248 prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1249 prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1250 prettyArgSti bnd (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1252 basename :: Name -> Name
1253 basename (NS n _) = basename n
1256 slist' p bnd (PApp _ (PRef _ nil) _)
1257 | not impl && nsroot nil == sUN "Nil" = Just []
1258 slist' p bnd (PRef _ nil)
1259 | not impl && nsroot nil == sUN "Nil" = Just []
1260 slist' p bnd (PApp _ (PRef _ cons) args)
1261 | nsroot cons == sUN "::",
1262 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1264 Just tl' <- slist' p bnd tl
1267 isImp (PImp {}) = True
1269 slist' _ _ tm = Nothing
1271 slist p bnd e | Just es <- slist' p bnd e = Just $
1272 case es of [] -> annotate AnnConstData $ text "[]"
1273 [x] -> enclose left right . group $
1275 xs -> (enclose left right .
1276 align . group . vsep .
1278 map (prettySe p bnd)) xs
1279 where left = (annotate AnnConstData (text "["))
1280 right = (annotate AnnConstData (text "]"))
1281 comma = (annotate AnnConstData (text ","))
1282 slist _ _ _ = Nothing
1284 natns = "Prelude.Nat."
1287 | show z == (natns++"Z") || show z == "Z" = Just 0
1288 snat p (PApp _ s [PExp {getTm=n}])
1289 | show s == (natns++"S") || show s == "S",
1294 bracket outer inner doc
1295 | inner > outer = lparen <> doc <> rparen
1298-- | Pretty-printer helper for the binding site of a name
1299bindingOf :: Name -- ^^ the bound name
1300 -> Bool -- ^^ whether the name is implicit
1301 -> Doc OutputAnnotation
1302bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1304-- | Pretty-printer helper for names that attaches the correct annotations
1305prettyName :: Bool -- ^^ whether to show namespaces
1306 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1307 -> Name -- ^^ the name to pprint
1308 -> Doc OutputAnnotation
1309prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1310 | otherwise = annotate (AnnName n Nothing Nothing) (text (strName n))
1311 where strName (UN n) = T.unpack n
1312 strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1313 | otherwise = strName n
1314 strName (MN i s) = T.unpack s
1315 strName other = show other
1318showCImp :: Bool -> PClause -> Doc OutputAnnotation
1319showCImp impl (PClause _ n l ws r w)
1320 = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1321 <+> text "where" <+> text (show w)
1324 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1325showCImp impl (PWith _ n l ws r w)
1326 = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1327 <+> braces (text (show w))
1330 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1333showDImp :: Bool -> PData -> Doc OutputAnnotation
1334showDImp impl (PDatadecl n ty cons)
1335 = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1336 (indent 2 $ vsep (map (\ (_, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1338showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1339showDecls i ds = vsep (map (showDeclImp i) ds)
1341showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1342showDeclImp i (PTy _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1343showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1344 indent 2 (vsep (map (showCImp i) cs))
1345showDeclImp _ (PData _ _ _ _ d) = showDImp True d
1346showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1347showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1348showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1349showDeclImp i (PClass _ _ _ cs n ps ds)
1350 = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1351showDeclImp i (PInstance _ _ cs n _ t _ ds)
1352 = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1353showDeclImp _ _ = text "..."
1354-- showDeclImp (PImport i) = "import " ++ i
1356instance Show (Doc OutputAnnotation) where
1357 show = flip (displayS . renderCompact) ""
1359getImps :: [PArg] -> [(Name, PTerm)]
1361getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
1362getImps (_ : xs) = getImps xs
1364getExps :: [PArg] -> [PTerm]
1366getExps (PExp _ _ tm _ : xs) = tm : getExps xs
1367getExps (_ : xs) = getExps xs
1369getConsts :: [PArg] -> [PTerm]
1371getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
1372getConsts (_ : xs) = getConsts xs
1374getAll :: [PArg] -> [PTerm]
1379showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1380 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1381 -> Bool -- ^^ whether to show implicits
1382 -> Bool -- ^^ whether to colourise
1383 -> Name -- ^^ the term to show
1385showName ist bnd impl colour n = case ist of
1386 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1387 Nothing -> showbasic n
1388 where name = if impl then show n else showbasic n
1389 showbasic n@(UN _) = showCG n
1390 showbasic (MN _ s) = str s
1391 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1392 showbasic (SN s) = show s
1394 colourise n t = let ctxt' = fmap tt_ctxt ist in
1397 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1398 else colouriseBound t name
1399 | isDConName n ctxt -> colouriseData t name
1400 | isFnName n ctxt -> colouriseFun t name
1401 | isTConName n ctxt -> colouriseType t name
1402 -- The assumption is that if a name is not bound and does not exist in the
1403 -- global context, then we're somewhere in which implicit info has been lost
1404 -- (like error messages). Thus, unknown vars are colourised as implicits.
1405 | otherwise -> colouriseImplicit t name
1407showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1408 -> PTerm -- ^^ the term to show
1410showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
1412-- | Show a term with implicits, no colours
1413showTmImpls :: PTerm -> String
1414showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1417instance Sized PTerm where
1418 size (PQuote rawTerm) = size rawTerm
1419 size (PRef fc name) = size name
1420 size (PLam name ty bdy) = 1 + size ty + size bdy
1421 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1422 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1423 size (PTyped trm ty) = 1 + size trm + size ty
1424 size (PApp fc name args) = 1 + size args
1425 size (PAppBind fc name args) = 1 + size args
1426 size (PCase fc trm bdy) = 1 + size trm + size bdy
1427 size (PTrue fc _) = 1
1428 size (PFalse fc) = 1
1429 size (PRefl fc _) = 1
1430 size (PResolveTC fc) = 1
1431 size (PEq fc left right) = 1 + size left + size right
1432 size (PRewrite fc left right _) = 1 + size left + size right
1433 size (PPair fc _ left right) = 1 + size left + size right
1434 size (PDPair fs _ left ty right) = 1 + size left + size ty + size right
1435 size (PAlternative a alts) = 1 + size alts
1436 size (PHidden hidden) = size hidden
1437 size (PUnifyLog tm) = size tm
1438 size (PDisamb _ tm) = size tm
1439 size (PNoImplicits tm) = size tm
1441 size (PConstant const) = 1 + size const
1442 size Placeholder = 1
1443 size (PDoBlock dos) = 1 + size dos
1444 size (PIdiom fc term) = 1 + size term
1445 size (PReturn fc) = 1
1446 size (PMetavar name) = 1
1447 size (PProof tactics) = size tactics
1448 size (PElabError err) = size err
1449 size PImpossible = 1
1451getPArity :: PTerm -> Int
1452getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1455-- Return all names, free or globally bound, in the given term.
1457allNamesIn :: PTerm -> [Name]
1458allNamesIn tm = nub $ ni [] tm
1461 | not (n `elem` env) = [n]
1462 ni env (PPatvar _ n) = [n]
1463 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1464 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1465 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1466 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1467 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1468 ni env (PHidden tm) = ni env tm
1469 ni env (PEq _ l r) = ni env l ++ ni env r
1470 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1471 ni env (PTyped l r) = ni env l ++ ni env r
1472 ni env (PPair _ _ l r) = ni env l ++ ni env r
1473 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1474 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1475 ni env (PAlternative a ls) = concatMap (ni env) ls
1476 ni env (PUnifyLog tm) = ni env tm
1477 ni env (PDisamb _ tm) = ni env tm
1478 ni env (PNoImplicits tm) = ni env tm
1481-- Return all names defined in binders in the given term
1482boundNamesIn :: PTerm -> [Name]
1483boundNamesIn tm = nub $ ni tm
1485 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1486 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1487 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1488 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1489 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1490 ni (PPi _ n ty sc) = n : (ni ty ++ ni sc)
1491 ni (PEq _ l r) = ni l ++ ni r
1492 ni (PRewrite _ l r _) = ni l ++ ni r
1493 ni (PTyped l r) = ni l ++ ni r
1494 ni (PPair _ _ l r) = ni l ++ ni r
1495 ni (PDPair _ _ (PRef _ n) t r) = ni t ++ ni r
1496 ni (PDPair _ _ l t r) = ni l ++ ni t ++ ni r
1497 ni (PAlternative a as) = concatMap (ni) as
1498 ni (PHidden tm) = ni tm
1499 ni (PUnifyLog tm) = ni tm
1500 ni (PDisamb _ tm) = ni tm
1501 ni (PNoImplicits tm) = ni tm
1504-- Return names which are free in the given term.
1505namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1506namesIn uvars ist tm = nub $ ni [] tm
1509 | not (n `elem` env)
1510 = case lookupTy n (tt_ctxt ist) of
1512 _ -> if n `elem` (map fst uvars) then [n] else []
1513 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1514 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1515 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1516 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1517 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1518 ni env (PEq _ l r) = ni env l ++ ni env r
1519 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1520 ni env (PTyped l r) = ni env l ++ ni env r
1521 ni env (PPair _ _ l r) = ni env l ++ ni env r
1522 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1523 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1524 ni env (PAlternative a as) = concatMap (ni env) as
1525 ni env (PHidden tm) = ni env tm
1526 ni env (PUnifyLog tm) = ni env tm
1527 ni env (PDisamb _ tm) = ni env tm
1528 ni env (PNoImplicits tm) = ni env tm
1531-- Return which of the given names are used in the given term.
1533usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1534usedNamesIn vars ist tm = nub $ ni [] tm
1537 | n `elem` vars && not (n `elem` env)
1538 = case lookupTy n (tt_ctxt ist) of
1541 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1542 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1543 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1544 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1545 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1546 ni env (PEq _ l r) = ni env l ++ ni env r
1547 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1548 ni env (PTyped l r) = ni env l ++ ni env r
1549 ni env (PPair _ _ l r) = ni env l ++ ni env r
1550 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1551 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1552 ni env (PAlternative a as) = concatMap (ni env) as
1553 ni env (PHidden tm) = ni env tm
1554 ni env (PUnifyLog tm) = ni env tm
1555 ni env (PDisamb _ tm) = ni env tm
1556 ni env (PNoImplicits tm) = ni env tm
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