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
195 | IBCObj Codegen FilePath
196 | IBCLib Codegen String
197 | IBCCGFlag Codegen String
199 | IBCHeader Codegen String
200 | IBCAccess Name Accessibility
201 | IBCMetaInformation Name MetaInformation
202 | IBCTotal Name Totality
203 | IBCFlags Name [FnOpt]
204 | IBCTrans (Term, Term)
205 | IBCErrRev (Term, Term)
209 | IBCDef Name -- i.e. main context
210 | IBCNameHint (Name, Name)
211 | IBCLineApp FilePath Int PTerm
212 | IBCErrorHandler Name
213 | IBCFunctionErrorHandler Name Name Name
216-- | The initial state for the compiler
218idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
219 emptyContext emptyContext emptyContext emptyContext
220 emptyContext emptyContext emptyContext emptyContext
221 emptyContext emptyContext
222 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
223 [] Nothing [] Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
224 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
227-- | The monad for the main REPL - reading and processing files and updating
228-- global state (hence the IO inner monad).
229--type Idris = WriterT [Either String (IO ())] (State IState a))
230type Idris = StateT IState (ErrorT Err IO)
232-- Commands in the REPL
251 | ChangeDirectory FilePath
254 | Compile Codegen String
259 | AddProof (Maybe Name)
271 | DynamicLink FilePath
276 | CaseSplitAt Bool Int Name
277 | AddClauseFrom Bool Int Name
278 | AddProofClauseFrom Bool Int Name
279 | AddMissing Bool Int Name
280 | MakeWith Bool Int Name
281 | DoProofSearch Bool Int Name [Name]
285 | SetColour ColourType IdrisColour
289 | SetConsoleWidth ConsoleWidth
291data Opt = Filename String
303 | NoBuiltins -- only for the really primitive stuff!
329 | OutputTy OutputType
330 | Extension LanguageExt
331 | InterpretScript String
332 | TargetTriple String
337 | AutoWidth -- ^ Automatically adjust terminal width
340-- Parsed declarations
342data Fixity = Infixl { prec :: Int }
343 | Infixr { prec :: Int }
344 | InfixN { prec :: Int }
345 | PrefixN { prec :: Int }
348deriving instance Binary Fixity
349deriving instance NFData Fixity
352instance Show Fixity where
353 show (Infixl i) = "infixl " ++ show i
354 show (Infixr i) = "infixr " ++ show i
355 show (InfixN i) = "infix " ++ show i
356 show (PrefixN i) = "prefix " ++ show i
358data FixDecl = Fix Fixity String
361instance Show FixDecl where
362 show (Fix f s) = show f ++ " " ++ s
365deriving instance Binary FixDecl
366deriving instance NFData FixDecl
369instance Ord FixDecl where
370 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
373data Static = Static | Dynamic
376deriving instance Binary Static
377deriving instance NFData Static
380-- Mark bindings with their explicitness, and laziness
381data Plicity = Imp { pargopts :: [ArgOpt],
385 | Exp { pargopts :: [ArgOpt],
389 | Constraint { pargopts :: [ArgOpt],
392 | TacImp { pargopts :: [ArgOpt],
398plazy :: Plicity -> Bool
399plazy tm = Lazy `elem` pargopts tm
402deriving instance Binary Plicity
403deriving instance NFData Plicity
406impl = Imp [Lazy] Dynamic "" False
407expl = Exp [] Dynamic "" False
408expl_param = Exp [] Dynamic "" True
409constraint = Constraint [] Dynamic ""
410tacimpl t = TacImp [] Dynamic t ""
412data FnOpt = Inlinable -- always evaluate when simplifying
413 | TotalFn | PartialFn
414 | Coinductive | AssertTotal
415 | Dictionary -- type class dictionary, eval only when
416 -- a function argument, and further evaluation resutls
417 | Implicit -- implicit coercion
418 | CExport String -- export, with a C name
419 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
420 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
421 | Reflection -- a reflecting function, compile-time only
422 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
425deriving instance Binary FnOpt
426deriving instance NFData FnOpt
431inlinable :: FnOpts -> Bool
432inlinable = elem Inlinable
434dictionary :: FnOpts -> Bool
435dictionary = elem Dictionary
438-- | Data declaration options
439data DataOpt = Codata -- Set if the the data-type is coinductive
440 | DefaultEliminator -- Set if an eliminator should be generated for data type
444type DataOpts = [DataOpt]
446-- | Type provider - what to provide
447data ProvideWhat = ProvTerm -- ^ only allow providing terms
448 | ProvPostulate -- ^ only allow postulates
449 | ProvAny -- ^ either is ok
452-- | Top-level declarations such as compiler directives, definitions,
453-- datatypes and typeclasses.
455 = PFix FC Fixity [String] -- ^ Fixity declaration
456 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
457 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
458 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
459 | PCAF FC Name t -- ^ Top level constant
460 | PData String SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
461 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
462 | PNamespace String [PDecl' t] -- ^ New namespace
463 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
464 | PClass String SyntaxInfo FC
467 [(Name, t)] -- parameters
468 [PDecl' t] -- declarations
469 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
470 -- class name, parameters, method declarations
471 | PInstance SyntaxInfo FC [t] -- constraints
474 t -- full instance type
475 (Maybe Name) -- explicit name
477 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
478 -- class name, parameters, full instance type, optional explicit name, and definitions
479 | PDSL Name (DSL' t) -- ^ DSL declaration
480 | PSyntax FC Syntax -- ^ Syntax definition
481 | PMutual FC [PDecl' t] -- ^ Mutual block
482 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
483 | PProvider SyntaxInfo FC ProvideWhat Name t t -- ^ Type provider. The first t is the type, the second is the term
484 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
485 -- bool is True, lhs and rhs must be convertible
488deriving instance Binary PDecl'
489deriving instance NFData PDecl'
492-- For elaborator state
493type ElabD a = Elab' [PDecl] a
495-- | One clause of a top-level definition. Term arguments to constructors are:
497-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
499-- 2. The list of extra 'with' patterns
501-- 3. The right-hand side
503-- 4. The where block (PDecl' t)
505data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
506 | PWith FC Name t [t] t [PDecl' t]
507 | PClauseR FC [t] t [PDecl' t]
508 | PWithR FC [t] t [PDecl' t]
511deriving instance Binary PClause'
512deriving instance NFData PClause'
516data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
517 d_tcon :: t, -- ^ Type constructor
518 d_cons :: [(String, Name, t, FC, [Name])] -- ^ Constructors
520 -- ^ Data declaration
521 | PLaterdecl { d_name :: Name, d_tcon :: t }
522 -- ^ "Placeholder" for data whose constructors are defined later
525deriving instance Binary PData'
526deriving instance NFData PData'
529-- Handy to get a free function for applying PTerm -> PTerm functions
530-- across a program, by deriving Functor
532type PDecl = PDecl' PTerm
533type PData = PData' PTerm
534type PClause = PClause' PTerm
536-- get all the names declared in a decl
538declared :: PDecl -> [Name]
539declared (PFix _ _ _) = []
540declared (PTy _ _ _ _ n t) = [n]
541declared (PPostulate _ _ _ _ n t) = [n]
542declared (PClauses _ _ n _) = [] -- not a declaration
543declared (PCAF _ n _) = [n]
544declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
545 where fstt (_, a, _, _, _) = a
546declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
547declared (PParams _ _ ds) = concatMap declared ds
548declared (PNamespace _ ds) = concatMap declared ds
549declared (PRecord _ _ _ n _ _ c _) = [n, c]
550declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
551declared (PInstance _ _ _ _ _ _ _ _) = []
552declared (PDSL n _) = [n]
553declared (PSyntax _ _) = []
554declared (PMutual _ ds) = concatMap declared ds
555declared (PDirective _) = []
557-- get the names declared, not counting nested parameter blocks
558tldeclared :: PDecl -> [Name]
559tldeclared (PFix _ _ _) = []
560tldeclared (PTy _ _ _ _ n t) = [n]
561tldeclared (PPostulate _ _ _ _ n t) = [n]
562tldeclared (PClauses _ _ n _) = [] -- not a declaration
563tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
564tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
565 where fstt (_, a, _, _, _) = a
566tldeclared (PParams _ _ ds) = []
567tldeclared (PMutual _ ds) = concatMap tldeclared ds
568tldeclared (PNamespace _ ds) = concatMap tldeclared ds
569tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
570tldeclared (PInstance _ _ _ _ _ _ _ _) = []
573defined :: PDecl -> [Name]
574defined (PFix _ _ _) = []
575defined (PTy _ _ _ _ n t) = []
576defined (PPostulate _ _ _ _ n t) = []
577defined (PClauses _ _ n _) = [n] -- not a declaration
578defined (PCAF _ n _) = [n]
579defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
580 where fstt (_, a, _, _, _) = a
581defined (PData _ _ _ _ (PLaterdecl n _)) = []
582defined (PParams _ _ ds) = concatMap defined ds
583defined (PNamespace _ ds) = concatMap defined ds
584defined (PRecord _ _ _ n _ _ c _) = [n, c]
585defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
586defined (PInstance _ _ _ _ _ _ _ _) = []
587defined (PDSL n _) = [n]
588defined (PSyntax _ _) = []
589defined (PMutual _ ds) = concatMap defined ds
590defined (PDirective _) = []
593updateN :: [(Name, Name)] -> Name -> Name
594updateN ns n | Just n' <- lookup n ns = n'
597updateNs :: [(Name, Name)] -> PTerm -> PTerm
599updateNs ns t = mapPT updateRef t
600 where updateRef (PRef fc f) = PRef fc (updateN ns f)
603-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
605-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
606-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
607-- where updateCNs ns (PClause n l ts r ds)
608-- = PClause (updateN ns n) (fmap (updateNs ns) l)
609-- (map (fmap (updateNs ns)) ts)
610-- (fmap (updateNs ns) r)
611-- (map (updateDNs ns) ds)
614data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show)
616-- | High level language terms
617data PTerm = PQuote Raw
619 | PInferRef FC Name -- ^ A name to be defined later
621 | PLam Name PTerm PTerm
622 | PPi Plicity Name PTerm PTerm
623 | PLet Name PTerm PTerm PTerm
624 | PTyped PTerm PTerm -- ^ Term with explicit type
625 | PApp FC PTerm [PArg]
626 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
627 | PMatchApp FC Name -- ^ Make an application by type matching
628 | PCase FC PTerm [(PTerm, PTerm)]
634 | PRewrite FC PTerm PTerm (Maybe PTerm)
635 | PPair FC PunInfo PTerm PTerm
636 | PDPair FC PunInfo PTerm PTerm PTerm
637 | PAlternative Bool [PTerm] -- True if only one may work
638 | PHidden PTerm -- ^ Irrelevant or hidden pattern
640 | PGoal FC PTerm Name PTerm
647 | PProof [PTactic] -- ^ Proof script
648 | PTactics [PTactic] -- ^ As PProof, but no auto solving
649 | PElabError Err -- ^ Error to report on elaboration
650 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
651 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
652 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
653 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
654 | PNoImplicits PTerm -- ^ never run implicit converions on the term
659deriving instance Binary PTerm
660deriving instance NFData PTerm
663mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
664mapPT f t = f (mpt t) where
665 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
666 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
667 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
668 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
670 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
671 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
672 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
673 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
674 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
675 mpt (PPair fc p l r) = PPair fc p (mapPT f l) (mapPT f r)
676 mpt (PDPair fc p l t r) = PDPair fc p (mapPT f l) (mapPT f t) (mapPT f r)
677 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
678 mpt (PHidden t) = PHidden (mapPT f t)
679 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
680 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
681 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
682 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
683 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
684 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
685 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
689data PTactic' t = Intro [Name] | Intros | Focus Name
690 | Refine Name [Bool] | Rewrite t
694 | LetTac Name t | LetTacTy Name t t
695 | Exact t | Compute | Trivial | TCInstance
696 | ProofSearch (Maybe Name) Name [Name]
699 | ProofState | ProofTerm | Undo
700 | Try (PTactic' t) (PTactic' t)
701 | TSeq (PTactic' t) (PTactic' t)
702 | ApplyTactic t -- see Language.Reflection module
706 | GoalType String (PTactic' t)
710 deriving (Show, Eq, Functor)
712deriving instance Binary PTactic'
713deriving instance NFData PTactic'
715instance Sized a => Sized (PTactic' a) where
716 size (Intro nms) = 1 + size nms
718 size (Focus nm) = 1 + size nm
719 size (Refine nm bs) = 1 + size nm + length bs
720 size (Rewrite t) = 1 + size t
721 size (Induction t) = 1 + size t
722 size (LetTac nm t) = 1 + size nm + size t
723 size (Exact t) = 1 + size t
731 size (Try l r) = 1 + size l + size r
732 size (TSeq l r) = 1 + size l + size r
733 size (ApplyTactic t) = 1 + size t
734 size (Reflect t) = 1 + size t
735 size (Fill t) = 1 + size t
739type PTactic = PTactic' PTerm
741data PDo' t = DoExp FC t
746 deriving (Eq, Functor)
748deriving instance Binary PDo'
749deriving instance NFData PDo'
752instance Sized a => Sized (PDo' a) where
753 size (DoExp fc t) = 1 + size fc + size t
754 size (DoBind fc nm t) = 1 + size fc + size nm + size t
755 size (DoBindP fc l r) = 1 + size fc + size l + size r
756 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
757 size (DoLetP fc l r) = 1 + size fc + size l + size r
761-- The priority gives a hint as to elaboration order. Best to elaborate
762-- things early which will help give a more concrete type to other
763-- variables, e.g. a before (interpTy a).
765data PArg' t = PImp { priority :: Int,
766 machine_inf :: Bool, -- true if the machine inferred it
768 pname :: Name, getTm :: t,
770 | PExp { priority :: Int,
774 | PConstraint { priority :: Int,
778 | PTacImplicit { priority :: Int,
784 deriving (Show, Eq, Functor)
786data ArgOpt = Lazy | HideDisplay
789lazyarg :: PArg' t -> Bool
790lazyarg tm = Lazy `elem` argopts tm
792instance Sized a => Sized (PArg' a) where
793 size (PImp p _ l nm trm _) = 1 + size nm + size trm
794 size (PExp p l trm _) = 1 + size trm
795 size (PConstraint p l trm _) = 1 + size trm
796 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
799deriving instance Binary PArg'
800deriving instance NFData PArg'
803pimp n t mach = PImp 1 mach [Lazy] n t ""
804pexp t = PExp 1 [] t ""
805pconst t = PConstraint 1 [] t ""
806ptacimp n s t = PTacImplicit 2 [Lazy] n s t ""
808type PArg = PArg' PTerm
812data ClassInfo = CI { instanceName :: Name,
813 class_methods :: [(Name, (FnOpts, PTerm))],
814 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
815 class_default_superclasses :: [PDecl],
816 class_params :: [Name],
817 class_instances :: [Name] }
820deriving instance Binary ClassInfo
821deriving instance NFData ClassInfo
824-- Type inference data
826data TIData = TIPartial -- ^ a function with a partially defined type
827 | TISolution [Term] -- ^ possible solutions to a metavariable in a type
830-- An argument is conditionally forceable iff its forceability
831-- depends on the collapsibility of the whole type.
832data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
835deriving instance Binary Forceability
836deriving instance NFData Forceability
839data OptInfo = Optimise { collapsible :: Bool,
841 -- The following should actually be (IntMap Forceability)
842 -- but the corresponding Binary instance seems to be broken.
843 -- Let's store a list and convert it to IntMap whenever needed.
844 forceable :: [(Int, Forceability)],
848deriving instance Binary OptInfo
849deriving instance NFData OptInfo
853data TypeInfo = TI { con_names :: [Name],
855 data_opts :: DataOpts,
859deriving instance Binary TypeInfo
860deriving instance NFData TypeInfo
863-- Syntactic sugar info
865data DSL' t = DSL { dsl_bind :: t,
870 index_first :: Maybe t,
871 index_next :: Maybe t,
872 dsl_lambda :: Maybe t,
875 deriving (Show, Functor)
877deriving instance Binary DSL'
878deriving instance NFData DSL'
883data SynContext = PatternSyntax | TermSyntax | AnySyntax
886deriving instance Binary SynContext
887deriving instance NFData SynContext
890data Syntax = Rule [SSymbol] PTerm SynContext
893deriving instance Binary Syntax
894deriving instance NFData Syntax
897data SSymbol = Keyword Name
904deriving instance Binary SSymbol
905deriving instance NFData SSymbol
908initDSL = DSL (PRef f (sUN ">>="))
909 (PRef f (sUN "return"))
911 (PRef f (sUN "pure"))
917 where f = fileFC "(builtin)"
919data Using = UImplicit Name PTerm
920 | UConstraint Name [Name]
923deriving instance Binary Using
924deriving instance NFData Using
927data SyntaxInfo = Syn { using :: [Using],
928 syn_params :: [(Name, PTerm)],
929 syn_namespace :: [String],
931 decoration :: Name -> Name,
933 implicitAllowed :: Bool,
937deriving instance NFData SyntaxInfo
938deriving instance Binary SyntaxInfo
941defaultSyntax = Syn [] [] [] [] id False False initDSL
943expandNS :: SyntaxInfo -> Name -> Name
944expandNS syn n@(NS _ _) = n
945expandNS syn n = case syn_namespace syn of
950-- For inferring types of things
954inferTy = sMN 0 "__Infer"
955inferCon = sMN 0 "__infer"
956inferDecl = PDatadecl inferTy
958 [("", inferCon, PPi impl (sMN 0 "iType") PType (
959 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
960 (PRef bi inferTy)), bi, [])]
963infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
964infP = P (TCon 6 0) inferTy (TType (UVal 0))
966getInferTerm, getInferType :: Term -> Term
967getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
968getInferTerm (App (App _ _) tm) = tm
969getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
971getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
972 where toTy (Lam t) = Pi t
973 toTy (PVar t) = PVTy t
975getInferType (App (App _ ty) _) = ty
979-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
981primNames = [unitTy, unitCon,
982 falseTy, pairTy, pairCon,
983 eqTy, eqCon, inferTy, inferCon]
985unitTy = sMN 0 "__Unit"
986unitCon = sMN 0 "__II"
987unitDecl = PDatadecl unitTy PType
988 [("", unitCon, PRef bi unitTy, bi, [])]
989unitOpts = [DefaultEliminator]
991falseTy = sMN 0 "__False"
992falseDecl = PDatadecl falseTy PType []
995pairTy = sMN 0 "__Pair"
996pairCon = sMN 0 "__MkPair"
997pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
998 [("", pairCon, PPi impl (n "A") PType (
999 PPi impl (n "B") PType (
1000 PPi expl (n "a") (PRef bi (n "A")) (
1001 PPi expl (n "b") (PRef bi (n "B"))
1002 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
1003 pexp (PRef bi (n "B"))])))), bi, [])]
1009eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
1010 (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
1012 [("", eqCon, PPi impl (n "A") PType (
1013 PPi impl (n "x") (PRef bi (n "A"))
1014 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
1015 pimp (n "B") Placeholder False,
1016 pexp (PRef bi (n "x")),
1017 pexp (PRef bi (n "x"))])), bi, [])]
1021elimName = sUN "__Elim"
1022elimMethElimTy = sUN "__elimTy"
1023elimMethElim = sUN "elim"
1024elimDecl = PClass "Type class for eliminators" defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
1025 [PTy "" defaultSyntax bi [TotalFn] elimMethElimTy PType,
1026 PTy "" defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
1028-- Defined in builtins.idr
1029sigmaTy = sUN "Exists"
1030existsCon = sUN "Ex_intro"
1032piBind :: [(Name, PTerm)] -> PTerm -> PTerm
1033piBind = piBindp expl
1035piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
1037piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
1040-- Pretty-printing declarations and terms
1042-- These "show" instances render to an absurdly wide screen because inserted line breaks
1043-- could interfere with interactive editing, which calls "show".
1045instance Show PTerm where
1046 showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
1048instance Show PDecl where
1049 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
1051instance Show PClause where
1052 showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
1054instance Show PData where
1055 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
1057instance Pretty PTerm OutputAnnotation where
1058 pretty = prettyImp False
1060-- | Colourise annotations according to an Idris state. It ignores the names
1061-- in the annotation, as there's no good way to show extended information on a
1063consoleDecorate :: IState -> OutputAnnotation -> String -> String
1064consoleDecorate ist _ | not (idris_colourRepl ist) = id
1065consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1066 in colouriseData theme
1067consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1068 in colouriseType theme
1069consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1070consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1071consoleDecorate ist (AnnName n _ _) = let ctxt = tt_ctxt ist
1072 theme = idris_colourTheme ist
1074 _ | isDConName n ctxt -> colouriseData theme
1075 _ | isFnName n ctxt -> colouriseFun theme
1076 _ | isTConName n ctxt -> colouriseType theme
1077 _ | otherwise -> id -- don't colourise unknown names
1078consoleDecorate ist (AnnFC _) = id
1080-- | Pretty-print a high-level closed Idris term
1081prettyImp :: Bool -- ^^ whether to show implicits
1082 -> PTerm -- ^^ the term to pretty-print
1083 -> Doc OutputAnnotation
1084prettyImp impl = pprintPTerm impl []
1086-- | Pretty-print a high-level Idris term in some bindings context
1087pprintPTerm :: Bool -- ^^ whether to show implicits
1088 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1089 -> PTerm -- ^^ the term to pretty-print
1090 -> Doc OutputAnnotation
1091pprintPTerm impl bnd = prettySe 10 bnd
1093 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1094 prettySe p bnd (PQuote r) =
1095 text "![" <> pretty r <> text "]"
1096 prettySe p bnd (PPatvar fc n) = pretty n
1098 | Just str <- slist p bnd e = str
1099 | Just n <- snat p e = annotate AnnConstData (text (show n))
1100 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1101 prettySe p bnd (PLam n ty sc) =
1102 bracket p 2 . group . align . hang 2 $
1103 text "\\" <> bindingOf n False <+> text "=>" <$>
1104 prettySe 10 ((n, False):bnd) sc
1105 prettySe p bnd (PLet n ty v sc) =
1107 text "let" <+> bindingOf n False <+> text "=" </>
1108 prettySe 10 bnd v <+> text "in" </>
1109 prettySe 10 ((n, False):bnd) sc
1110 prettySe p bnd (PPi (Exp l s _ _) n ty sc)
1111 | n `elem` allNamesIn sc || impl =
1112 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1113 bracket p 2 . group $
1114 enclose open rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1115 st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
1117 bracket p 2 . group $
1118 group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
1122 Static -> text "[static]" <> space
1124 prettySe p bnd (PPi (Imp l s _ _) n ty sc)
1126 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1128 open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1129 st <> text "->" </> prettySe 10 ((n, True):bnd) sc
1130 | otherwise = prettySe 10 ((n, True):bnd) sc
1134 Static -> text "[static]" <> space
1136 prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
1138 prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
1139 prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
1141 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1142 rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
1143 prettySe p bnd (PApp _ (PRef _ f) args) -- normal names, no explicit args
1144 | UN nm <- basename f
1145 , not impl && null (getExps args) = if isAlpha (thead nm)
1146 then prettyName impl bnd f
1147 else enclose lparen rparen $
1148 prettyName impl bnd f
1149 prettySe p bnd (PAppBind _ (PRef _ f) [])
1150 | not impl = text "!" <> prettyName impl bnd f
1151 prettySe p bnd (PApp _ (PRef _ op) args)
1152 | UN nm <- basename op
1154 (not impl) && (not $ isAlpha (thead nm)) =
1155 case getExps args of
1156 [] -> enclose lparen rparen opName
1157 [x] -> group (enclose lparen rparen opName <$> group (prettySe 0 bnd x))
1158 [l,r] -> bracket p 1 $ inFix l r
1159 (l:r:rest) -> bracket p 1 $
1160 enclose lparen rparen (inFix l r) <+>
1161 align (group (vsep (map (prettyArgSe bnd) rest)))
1162 where opName = prettyName impl bnd op
1163 inFix l r = align . group $
1164 (prettySe 1 bnd l <+> opName) <$> group (prettySe 0 bnd r)
1165 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1166 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1167 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1168 prettySe p bnd (PApp _ f as) =
1169 let args = getExps as
1170 fp = prettySe 1 bnd f
1172 bracket p 1 . group $
1176 else fp <+> align (vsep (map (prettyArgS bnd) as))
1179 else fp <+> align (vsep (map (prettyArgSe bnd) args))
1180 prettySe p bnd (PCase _ scr opts) =
1181 text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
1183 prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1185 sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1186 prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1187 prettySe p bnd (PRefl _ _) = annotate (AnnName eqCon Nothing Nothing) $ text "refl"
1188 prettySe p bnd (PResolveTC _) = text "resolvetc"
1189 prettySe p bnd (PTrue _ IsType) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1190 prettySe p bnd (PTrue _ IsTerm) = annotate (AnnName unitCon Nothing Nothing) $ text "()"
1191 prettySe p bnd (PTrue _ TypeOrTerm) = text "()"
1192 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1193 prettySe p bnd (PEq _ l r) =
1194 bracket p 2 . align . group $
1195 prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
1196 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1197 prettySe p bnd (PRewrite _ l r _) =
1199 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1200 prettySe p bnd (PTyped l r) =
1201 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1202 prettySe p bnd pair@(PPair _ pun _ _) -- flatten tuples to the right, like parser
1203 | Just elts <- pairElts pair = enclose (ann lparen) (ann rparen) .
1204 align . group . vsep . punctuate (ann comma) $
1205 map (prettySe 10 bnd) elts
1206 where ann = case pun of
1208 IsType -> annotate (AnnName pairTy Nothing Nothing)
1209 IsTerm -> annotate (AnnName pairCon Nothing Nothing)
1210 prettySe p bnd (PDPair _ TypeOrTerm l t r) =
1211 lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1212 prettySe p bnd (PDPair _ IsType (PRef _ n) t r) =
1213 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1214 bindingOf n False <+>
1215 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1216 prettySe 10 ((n, False):bnd) r <>
1217 annotate (AnnName sigmaTy Nothing Nothing) rparen
1218 prettySe p bnd (PDPair _ IsType l t r) =
1219 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1220 prettySe 10 bnd l <+>
1221 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1222 prettySe 10 bnd r <>
1223 annotate (AnnName sigmaTy Nothing Nothing) rparen
1224 prettySe p bnd (PDPair _ IsTerm l t r) =
1225 annotate (AnnName existsCon Nothing Nothing) lparen <>
1226 prettySe 10 bnd l <+>
1227 annotate (AnnName existsCon Nothing Nothing) (text "**") <+>
1228 prettySe 10 bnd r <>
1229 annotate (AnnName existsCon Nothing Nothing) rparen
1230 prettySe p bnd (PAlternative a as) =
1231 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1234 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1235 prettySe p bnd PType = annotate AnnConstType $ text "Type"
1236 prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1237 where annot (AType _) = AnnConstType
1238 annot StrType = AnnConstType
1239 annot PtrType = AnnConstType
1240 annot VoidType = AnnConstType
1241 annot _ = AnnConstData
1242 -- XXX: add pretty for tactics
1243 prettySe p bnd (PProof ts) =
1244 text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1245 prettySe p bnd (PTactics ts) =
1246 text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1247 prettySe p bnd (PMetavar n) = text "?" <> pretty n
1248 prettySe p bnd (PReturn f) = text "return"
1249 prettySe p bnd PImpossible = text "impossible"
1250 prettySe p bnd Placeholder = text "_"
1251 prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1252 prettySe p bnd (PElabError s) = pretty s
1254 prettySe p bnd _ = text "test"
1256 prettyArgS bnd (PImp _ _ _ n tm _) = prettyArgSi bnd (n, tm)
1257 prettyArgS bnd (PExp _ _ tm _) = prettyArgSe bnd tm
1258 prettyArgS bnd (PConstraint _ _ tm _) = prettyArgSc bnd tm
1259 prettyArgS bnd (PTacImplicit _ _ n _ tm _) = prettyArgSti bnd (n, tm)
1261 prettyArgSe bnd arg = prettySe 0 bnd arg
1262 prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1263 prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1264 prettyArgSti bnd (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1266 basename :: Name -> Name
1267 basename (NS n _) = basename n
1270 slist' p bnd (PApp _ (PRef _ nil) _)
1271 | not impl && nsroot nil == sUN "Nil" = Just []
1272 slist' p bnd (PRef _ nil)
1273 | not impl && nsroot nil == sUN "Nil" = Just []
1274 slist' p bnd (PApp _ (PRef _ cons) args)
1275 | nsroot cons == sUN "::",
1276 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1278 Just tl' <- slist' p bnd tl
1281 isImp (PImp {}) = True
1283 slist' _ _ tm = Nothing
1285 slist p bnd e | Just es <- slist' p bnd e = Just $
1286 case es of [] -> annotate AnnConstData $ text "[]"
1287 [x] -> enclose left right . group $
1289 xs -> (enclose left right .
1290 align . group . vsep .
1292 map (prettySe p bnd)) xs
1293 where left = (annotate AnnConstData (text "["))
1294 right = (annotate AnnConstData (text "]"))
1295 comma = (annotate AnnConstData (text ","))
1296 slist _ _ _ = Nothing
1298 pairElts :: PTerm -> Maybe [PTerm]
1299 pairElts (PPair _ _ x y) | Just elts <- pairElts y = Just (x:elts)
1300 | otherwise = Just [x, y]
1301 pairElts _ = Nothing
1303 natns = "Prelude.Nat."
1306 | show z == (natns++"Z") || show z == "Z" = Just 0
1307 snat p (PApp _ s [PExp {getTm=n}])
1308 | show s == (natns++"S") || show s == "S",
1313 bracket outer inner doc
1314 | inner > outer = lparen <> doc <> rparen
1317-- | Pretty-printer helper for the binding site of a name
1318bindingOf :: Name -- ^^ the bound name
1319 -> Bool -- ^^ whether the name is implicit
1320 -> Doc OutputAnnotation
1321bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1323-- | Pretty-printer helper for names that attaches the correct annotations
1324prettyName :: Bool -- ^^ whether to show namespaces
1325 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1326 -> Name -- ^^ the name to pprint
1327 -> Doc OutputAnnotation
1328prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1329 | otherwise = annotate (AnnName n Nothing Nothing) (text (strName n))
1330 where strName (UN n) = T.unpack n
1331 strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1332 | otherwise = strName n
1333 strName (MN i s) = T.unpack s
1334 strName other = show other
1337showCImp :: Bool -> PClause -> Doc OutputAnnotation
1338showCImp impl (PClause _ n l ws r w)
1339 = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1340 <+> text "where" <+> text (show w)
1343 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1344showCImp impl (PWith _ n l ws r w)
1345 = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1346 <+> braces (text (show w))
1349 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1352showDImp :: Bool -> PData -> Doc OutputAnnotation
1353showDImp impl (PDatadecl n ty cons)
1354 = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1355 (indent 2 $ vsep (map (\ (_, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1357showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1358showDecls i ds = vsep (map (showDeclImp i) ds)
1360showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1361showDeclImp i (PTy _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1362showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1363 indent 2 (vsep (map (showCImp i) cs))
1364showDeclImp _ (PData _ _ _ _ d) = showDImp True d
1365showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1366showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1367showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1368showDeclImp i (PClass _ _ _ cs n ps ds)
1369 = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1370showDeclImp i (PInstance _ _ cs n _ t _ ds)
1371 = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1372showDeclImp _ _ = text "..."
1373-- showDeclImp (PImport i) = "import " ++ i
1375instance Show (Doc OutputAnnotation) where
1376 show = flip (displayS . renderCompact) ""
1378getImps :: [PArg] -> [(Name, PTerm)]
1380getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
1381getImps (_ : xs) = getImps xs
1383getExps :: [PArg] -> [PTerm]
1385getExps (PExp _ _ tm _ : xs) = tm : getExps xs
1386getExps (_ : xs) = getExps xs
1388getConsts :: [PArg] -> [PTerm]
1390getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
1391getConsts (_ : xs) = getConsts xs
1393getAll :: [PArg] -> [PTerm]
1398showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1399 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1400 -> Bool -- ^^ whether to show implicits
1401 -> Bool -- ^^ whether to colourise
1402 -> Name -- ^^ the term to show
1404showName ist bnd impl colour n = case ist of
1405 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1406 Nothing -> showbasic n
1407 where name = if impl then show n else showbasic n
1408 showbasic n@(UN _) = showCG n
1409 showbasic (MN _ s) = str s
1410 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1411 showbasic (SN s) = show s
1413 colourise n t = let ctxt' = fmap tt_ctxt ist in
1416 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1417 else colouriseBound t name
1418 | isDConName n ctxt -> colouriseData t name
1419 | isFnName n ctxt -> colouriseFun t name
1420 | isTConName n ctxt -> colouriseType t name
1421 -- The assumption is that if a name is not bound and does not exist in the
1422 -- global context, then we're somewhere in which implicit info has been lost
1423 -- (like error messages). Thus, unknown vars are colourised as implicits.
1424 | otherwise -> colouriseImplicit t name
1426showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1427 -> PTerm -- ^^ the term to show
1429showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
1431-- | Show a term with implicits, no colours
1432showTmImpls :: PTerm -> String
1433showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1436instance Sized PTerm where
1437 size (PQuote rawTerm) = size rawTerm
1438 size (PRef fc name) = size name
1439 size (PLam name ty bdy) = 1 + size ty + size bdy
1440 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1441 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1442 size (PTyped trm ty) = 1 + size trm + size ty
1443 size (PApp fc name args) = 1 + size args
1444 size (PAppBind fc name args) = 1 + size args
1445 size (PCase fc trm bdy) = 1 + size trm + size bdy
1446 size (PTrue fc _) = 1
1447 size (PFalse fc) = 1
1448 size (PRefl fc _) = 1
1449 size (PResolveTC fc) = 1
1450 size (PEq fc left right) = 1 + size left + size right
1451 size (PRewrite fc left right _) = 1 + size left + size right
1452 size (PPair fc _ left right) = 1 + size left + size right
1453 size (PDPair fs _ left ty right) = 1 + size left + size ty + size right
1454 size (PAlternative a alts) = 1 + size alts
1455 size (PHidden hidden) = size hidden
1456 size (PUnifyLog tm) = size tm
1457 size (PDisamb _ tm) = size tm
1458 size (PNoImplicits tm) = size tm
1460 size (PConstant const) = 1 + size const
1461 size Placeholder = 1
1462 size (PDoBlock dos) = 1 + size dos
1463 size (PIdiom fc term) = 1 + size term
1464 size (PReturn fc) = 1
1465 size (PMetavar name) = 1
1466 size (PProof tactics) = size tactics
1467 size (PElabError err) = size err
1468 size PImpossible = 1
1470getPArity :: PTerm -> Int
1471getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1474-- Return all names, free or globally bound, in the given term.
1476allNamesIn :: PTerm -> [Name]
1477allNamesIn tm = nub $ ni [] tm
1480 | not (n `elem` env) = [n]
1481 ni env (PPatvar _ n) = [n]
1482 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1483 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1484 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1485 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1486 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1487 ni env (PHidden tm) = ni env tm
1488 ni env (PEq _ l r) = ni env l ++ ni env r
1489 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1490 ni env (PTyped l r) = ni env l ++ ni env r
1491 ni env (PPair _ _ l r) = ni env l ++ ni env r
1492 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1493 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1494 ni env (PAlternative a ls) = concatMap (ni env) ls
1495 ni env (PUnifyLog tm) = ni env tm
1496 ni env (PDisamb _ tm) = ni env tm
1497 ni env (PNoImplicits tm) = ni env tm
1500-- Return all names defined in binders in the given term
1501boundNamesIn :: PTerm -> [Name]
1502boundNamesIn tm = nub $ ni tm
1504 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1505 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1506 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1507 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1508 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1509 ni (PPi _ n ty sc) = n : (ni ty ++ ni sc)
1510 ni (PEq _ l r) = ni l ++ ni r
1511 ni (PRewrite _ l r _) = ni l ++ ni r
1512 ni (PTyped l r) = ni l ++ ni r
1513 ni (PPair _ _ l r) = ni l ++ ni r
1514 ni (PDPair _ _ (PRef _ n) t r) = ni t ++ ni r
1515 ni (PDPair _ _ l t r) = ni l ++ ni t ++ ni r
1516 ni (PAlternative a as) = concatMap (ni) as
1517 ni (PHidden tm) = ni tm
1518 ni (PUnifyLog tm) = ni tm
1519 ni (PDisamb _ tm) = ni tm
1520 ni (PNoImplicits tm) = ni tm
1523-- Return names which are free in the given term.
1524namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1525namesIn uvars ist tm = nub $ ni [] tm
1528 | not (n `elem` env)
1529 = case lookupTy n (tt_ctxt ist) of
1531 _ -> if n `elem` (map fst uvars) then [n] else []
1532 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1533 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1534 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1535 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1536 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1537 ni env (PEq _ l r) = ni env l ++ ni env r
1538 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1539 ni env (PTyped l r) = ni env l ++ ni env r
1540 ni env (PPair _ _ l r) = ni env l ++ ni env r
1541 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1542 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1543 ni env (PAlternative a as) = concatMap (ni env) as
1544 ni env (PHidden tm) = ni env tm
1545 ni env (PUnifyLog tm) = ni env tm
1546 ni env (PDisamb _ tm) = ni env tm
1547 ni env (PNoImplicits tm) = ni env tm
1550-- Return which of the given names are used in the given term.
1552usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1553usedNamesIn vars ist tm = nub $ ni [] tm
1556 | n `elem` vars && not (n `elem` env)
1557 = case lookupTy n (tt_ctxt ist) of
1560 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1561 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1562 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1563 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1564 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1565 ni env (PEq _ l r) = ni env l ++ ni env r
1566 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1567 ni env (PTyped l r) = ni env l ++ ni env r
1568 ni env (PPair _ _ l r) = ni env l ++ ni env r
1569 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1570 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1571 ni env (PAlternative a as) = concatMap (ni env) as
1572 ni env (PHidden tm) = ni env tm
1573 ni env (PUnifyLog tm) = ni env tm
1574 ni env (PDisamb _ tm) = ni env tm
1575 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