1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
7import Idris.Core.Evaluate
8import Idris.Core.Elaborate hiding (Tactic(..))
9import Idris.Core.Typecheck
11import IRTS.CodegenCommon
13import Util.DynamicLinker
19import System.Console.Haskeline
22import Control.Monad.Trans.State.Strict
23import Control.Monad.Trans.Error
25import Data.List hiding (group)
27import qualified Data.Map as M
28import qualified Data.Text as T
29import qualified Data.Map as M
31import qualified Data.Set as S
32import Data.Word (Word)
36import Text.PrettyPrint.Annotated.Leijen
38data IOption = IOption { opt_logLevel :: Int,
40 opt_typeintype :: Bool,
42 opt_showimp :: Bool, -- ^^ show implicits
43 opt_errContext :: Bool,
48 opt_codegen :: Codegen,
49 opt_outputTy :: OutputType,
50 opt_ibcsubdir :: FilePath,
51 opt_importdirs :: [FilePath],
55 opt_cmdline :: [Opt], -- remember whole command line
60defaultOpts = IOption { opt_logLevel = 0
61 , opt_typecase = False
62 , opt_typeintype = False
65 , opt_errContext = False
68 , opt_nobanner = False
71 , opt_outputTy = Executable
81data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
83-- | The output mode in use
84data OutputMode = RawOutput | IdeSlave Integer deriving Show
86-- | How wide is the console?
87data ConsoleWidth = InfinitelyWide -- ^ Have pretty-printer assume that lines should not be broken
88 | ColsWide Int -- ^ Manually specified - must be positive
89 | AutomaticWidth -- ^ Attempt to determine width, or 80 otherwise
91-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
92-- without typechecking).
93-- This will include all the functions and data declarations, plus fixity declarations
96-- | The global state used in the Idris monad
98 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
99 idris_constraints :: [(UConstraint, FC)],
100 -- ^ A list of universe constraints and their corresponding source locations
101 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
102 idris_implicits :: Ctxt [PArg],
103 idris_statics :: Ctxt [Bool],
104 idris_classes :: Ctxt ClassInfo,
105 idris_dsls :: Ctxt DSL,
106 idris_optimisation :: Ctxt OptInfo,
107 idris_datatypes :: Ctxt TypeInfo,
108 idris_namehints :: Ctxt [Name],
109 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
110 -- ^ list of lhs/rhs, and a list of missing clauses
111 idris_flags :: Ctxt [FnOpt],
112 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
113 idris_calledgraph :: Ctxt [Name],
114 idris_docstrings :: Ctxt String,
115 idris_tyinfodata :: Ctxt TIData,
116 idris_totcheck :: [(FC, Name)], -- names to check totality on
117 idris_defertotcheck :: [(FC, Name)], -- names to check at the end
118 idris_options :: IOption,
120 idris_lineapps :: [((FilePath, Int), PTerm)],
121 -- ^ Full application LHS on source line
122 idris_metavars :: [(Name, (Maybe Name, Int, Bool))], -- ^ The currently defined but not proven metavariables
123 idris_coercions :: [Name],
124 idris_transforms :: [(Term, Term)],
125 idris_errRev :: [(Term, Term)],
126 syntax_rules :: [Syntax],
127 syntax_keywords :: [String],
128 imported :: [FilePath], -- ^ The imported modules
129 idris_scprims :: [(Name, (Int, PrimFn))],
130 idris_objs :: [(Codegen, FilePath)],
131 idris_libs :: [(Codegen, String)],
132 idris_cgflags :: [(Codegen, String)],
133 idris_hdrs :: [(Codegen, String)],
134 proof_list :: [(Name, [String])],
135 errLine :: Maybe Int,
136 parserWarnings :: [(FC, Err)],
137 lastParse :: Maybe Name,
138 indent_stack :: [Int],
139 brace_stack :: [Maybe Int],
140 hide_list :: [(Name, Maybe Accessibility)],
141 default_access :: Accessibility,
142 default_total :: Bool,
143 ibc_write :: [IBCWrite],
144 compiled_so :: Maybe String,
145 idris_dynamic_libs :: [DynamicLib],
146 idris_language_extensions :: [LanguageExt],
147 idris_outputmode :: OutputMode,
148 idris_colourRepl :: Bool,
149 idris_colourTheme :: ColourTheme,
150 idris_outh :: Handle,
151 idris_errorhandlers :: [Name], -- ^ Global error handlers
152 idris_nameIdx :: (Int, Ctxt (Int, Name)),
153 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
154 module_aliases :: M.Map [T.Text] [T.Text],
155 idris_consolewidth :: ConsoleWidth -- ^ How many chars wide is the console?
158data SizeChange = Smaller | Same | Bigger | Unknown
161deriving instance Binary SizeChange
162deriving instance NFData SizeChange
165type SCGEntry = (Name, [Maybe (Int, SizeChange)])
167data CGInfo = CGInfo { argsdef :: [Name],
168 calls :: [(Name, [[Name]])],
174deriving instance Binary CGInfo
175deriving instance NFData CGInfo
178primDefs = [sUN "unsafePerformPrimIO",
179 sUN "mkLazyForeignPrim",
183-- information that needs writing for the current module's .ibc file
184data IBCWrite = IBCFix FixDecl
188 | IBCInstance Bool Name Name
196 | IBCObj Codegen FilePath
197 | IBCLib Codegen String
198 | IBCCGFlag Codegen String
200 | IBCHeader Codegen String
201 | IBCAccess Name Accessibility
202 | IBCMetaInformation Name MetaInformation
203 | IBCTotal Name Totality
204 | IBCFlags Name [FnOpt]
205 | IBCTrans (Term, Term)
206 | IBCErrRev (Term, Term)
210 | IBCDef Name -- i.e. main context
211 | IBCNameHint (Name, Name)
212 | IBCLineApp FilePath Int PTerm
213 | IBCErrorHandler Name
214 | IBCFunctionErrorHandler Name Name Name
217-- | The initial state for the compiler
219idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
220 emptyContext emptyContext emptyContext emptyContext
221 emptyContext emptyContext emptyContext emptyContext
222 emptyContext emptyContext
223 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
224 [] Nothing [] Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
225 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
228-- | The monad for the main REPL - reading and processing files and updating
229-- global state (hence the IO inner monad).
230--type Idris = WriterT [Either String (IO ())] (State IState a))
231type Idris = StateT IState (ErrorT Err IO)
233-- Commands in the REPL
252 | ChangeDirectory FilePath
255 | Compile Codegen String
260 | AddProof (Maybe Name)
272 | DynamicLink FilePath
277 | CaseSplitAt Bool Int Name
278 | AddClauseFrom Bool Int Name
279 | AddProofClauseFrom Bool Int Name
280 | AddMissing Bool Int Name
281 | MakeWith Bool Int Name
282 | DoProofSearch Bool Int Name [Name]
286 | SetColour ColourType IdrisColour
290 | SetConsoleWidth ConsoleWidth
292data Opt = Filename String
304 | NoBuiltins -- only for the really primitive stuff!
330 | OutputTy OutputType
331 | Extension LanguageExt
332 | InterpretScript String
333 | TargetTriple String
338 | AutoWidth -- ^ Automatically adjust terminal width
341-- Parsed declarations
343data Fixity = Infixl { prec :: Int }
344 | Infixr { prec :: Int }
345 | InfixN { prec :: Int }
346 | PrefixN { prec :: Int }
349deriving instance Binary Fixity
350deriving instance NFData Fixity
353instance Show Fixity where
354 show (Infixl i) = "infixl " ++ show i
355 show (Infixr i) = "infixr " ++ show i
356 show (InfixN i) = "infix " ++ show i
357 show (PrefixN i) = "prefix " ++ show i
359data FixDecl = Fix Fixity String
362instance Show FixDecl where
363 show (Fix f s) = show f ++ " " ++ s
366deriving instance Binary FixDecl
367deriving instance NFData FixDecl
370instance Ord FixDecl where
371 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
374data Static = Static | Dynamic
377deriving instance Binary Static
378deriving instance NFData Static
381-- Mark bindings with their explicitness, and laziness
382data Plicity = Imp { pargopts :: [ArgOpt],
386 | Exp { pargopts :: [ArgOpt],
390 | Constraint { pargopts :: [ArgOpt],
393 | TacImp { pargopts :: [ArgOpt],
399plazy :: Plicity -> Bool
400plazy tm = Lazy `elem` pargopts tm
403deriving instance Binary Plicity
404deriving instance NFData Plicity
407impl = Imp [Lazy] Dynamic "" False
408expl = Exp [] Dynamic "" False
409expl_param = Exp [] Dynamic "" True
410constraint = Constraint [] Dynamic ""
411tacimpl t = TacImp [] Dynamic t ""
413data FnOpt = Inlinable -- always evaluate when simplifying
414 | TotalFn | PartialFn
415 | Coinductive | AssertTotal
416 | Dictionary -- type class dictionary, eval only when
417 -- a function argument, and further evaluation resutls
418 | Implicit -- implicit coercion
419 | CExport String -- export, with a C name
420 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
421 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
422 | Reflection -- a reflecting function, compile-time only
423 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
426deriving instance Binary FnOpt
427deriving instance NFData FnOpt
432inlinable :: FnOpts -> Bool
433inlinable = elem Inlinable
435dictionary :: FnOpts -> Bool
436dictionary = elem Dictionary
439-- | Data declaration options
440data DataOpt = Codata -- Set if the the data-type is coinductive
441 | DefaultEliminator -- Set if an eliminator should be generated for data type
445type DataOpts = [DataOpt]
447-- | Top-level declarations such as compiler directives, definitions,
448-- datatypes and typeclasses.
450 = PFix FC Fixity [String] -- ^ Fixity declaration
451 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
452 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
453 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
454 | PCAF FC Name t -- ^ Top level constant
455 | PData String SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
456 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
457 | PNamespace String [PDecl' t] -- ^ New namespace
458 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
459 | PClass String SyntaxInfo FC
462 [(Name, t)] -- parameters
463 [PDecl' t] -- declarations
464 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
465 -- class name, parameters, method declarations
466 | PInstance SyntaxInfo FC [t] -- constraints
469 t -- full instance type
470 (Maybe Name) -- explicit name
472 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
473 -- class name, parameters, full instance type, optional explicit name, and definitions
474 | PDSL Name (DSL' t) -- ^ DSL declaration
475 | PSyntax FC Syntax -- ^ Syntax definition
476 | PMutual FC [PDecl' t] -- ^ Mutual block
477 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
478 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
479 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
480 -- bool is True, lhs and rhs must be convertible
483deriving instance Binary PDecl'
484deriving instance NFData PDecl'
487-- For elaborator state
488type ElabD a = Elab' [PDecl] a
490-- | One clause of a top-level definition. Term arguments to constructors are:
492-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
494-- 2. The list of extra 'with' patterns
496-- 3. The right-hand side
498-- 4. The where block (PDecl' t)
500data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
501 | PWith FC Name t [t] t [PDecl' t]
502 | PClauseR FC [t] t [PDecl' t]
503 | PWithR FC [t] t [PDecl' t]
506deriving instance Binary PClause'
507deriving instance NFData PClause'
511data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
512 d_tcon :: t, -- ^ Type constructor
513 d_cons :: [(String, Name, t, FC, [Name])] -- ^ Constructors
515 -- ^ Data declaration
516 | PLaterdecl { d_name :: Name, d_tcon :: t }
517 -- ^ "Placeholder" for data whose constructors are defined later
520deriving instance Binary PData'
521deriving instance NFData PData'
524-- Handy to get a free function for applying PTerm -> PTerm functions
525-- across a program, by deriving Functor
527type PDecl = PDecl' PTerm
528type PData = PData' PTerm
529type PClause = PClause' PTerm
531-- get all the names declared in a decl
533declared :: PDecl -> [Name]
534declared (PFix _ _ _) = []
535declared (PTy _ _ _ _ n t) = [n]
536declared (PPostulate _ _ _ _ n t) = [n]
537declared (PClauses _ _ n _) = [] -- not a declaration
538declared (PCAF _ n _) = [n]
539declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
540 where fstt (_, a, _, _, _) = a
541declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
542declared (PParams _ _ ds) = concatMap declared ds
543declared (PNamespace _ ds) = concatMap declared ds
544declared (PRecord _ _ _ n _ _ c _) = [n, c]
545declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
546declared (PInstance _ _ _ _ _ _ _ _) = []
547declared (PDSL n _) = [n]
548declared (PSyntax _ _) = []
549declared (PMutual _ ds) = concatMap declared ds
550declared (PDirective _) = []
552-- get the names declared, not counting nested parameter blocks
553tldeclared :: PDecl -> [Name]
554tldeclared (PFix _ _ _) = []
555tldeclared (PTy _ _ _ _ n t) = [n]
556tldeclared (PPostulate _ _ _ _ n t) = [n]
557tldeclared (PClauses _ _ n _) = [] -- not a declaration
558tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
559tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
560 where fstt (_, a, _, _, _) = a
561tldeclared (PParams _ _ ds) = []
562tldeclared (PMutual _ ds) = concatMap tldeclared ds
563tldeclared (PNamespace _ ds) = concatMap tldeclared ds
564tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
565tldeclared (PInstance _ _ _ _ _ _ _ _) = []
568defined :: PDecl -> [Name]
569defined (PFix _ _ _) = []
570defined (PTy _ _ _ _ n t) = []
571defined (PPostulate _ _ _ _ n t) = []
572defined (PClauses _ _ n _) = [n] -- not a declaration
573defined (PCAF _ n _) = [n]
574defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
575 where fstt (_, a, _, _, _) = a
576defined (PData _ _ _ _ (PLaterdecl n _)) = []
577defined (PParams _ _ ds) = concatMap defined ds
578defined (PNamespace _ ds) = concatMap defined ds
579defined (PRecord _ _ _ n _ _ c _) = [n, c]
580defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
581defined (PInstance _ _ _ _ _ _ _ _) = []
582defined (PDSL n _) = [n]
583defined (PSyntax _ _) = []
584defined (PMutual _ ds) = concatMap defined ds
585defined (PDirective _) = []
588updateN :: [(Name, Name)] -> Name -> Name
589updateN ns n | Just n' <- lookup n ns = n'
592updateNs :: [(Name, Name)] -> PTerm -> PTerm
594updateNs ns t = mapPT updateRef t
595 where updateRef (PRef fc f) = PRef fc (updateN ns f)
598-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
600-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
601-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
602-- where updateCNs ns (PClause n l ts r ds)
603-- = PClause (updateN ns n) (fmap (updateNs ns) l)
604-- (map (fmap (updateNs ns)) ts)
605-- (fmap (updateNs ns) r)
606-- (map (updateDNs ns) ds)
609data PunInfo = IsType | IsTerm | TypeOrTerm deriving (Eq, Show)
611-- | High level language terms
612data PTerm = PQuote Raw
614 | PInferRef FC Name -- ^ A name to be defined later
616 | PLam Name PTerm PTerm
617 | PPi Plicity Name PTerm PTerm
618 | PLet Name PTerm PTerm PTerm
619 | PTyped PTerm PTerm -- ^ Term with explicit type
620 | PApp FC PTerm [PArg]
621 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
622 | PMatchApp FC Name -- ^ Make an application by type matching
623 | PCase FC PTerm [(PTerm, PTerm)]
629 | PRewrite FC PTerm PTerm (Maybe PTerm)
630 | PPair FC PunInfo PTerm PTerm
631 | PDPair FC PunInfo PTerm PTerm PTerm
632 | PAlternative Bool [PTerm] -- True if only one may work
633 | PHidden PTerm -- ^ Irrelevant or hidden pattern
635 | PGoal FC PTerm Name PTerm
642 | PProof [PTactic] -- ^ Proof script
643 | PTactics [PTactic] -- ^ As PProof, but no auto solving
644 | PElabError Err -- ^ Error to report on elaboration
645 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
646 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
647 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
648 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
649 | PNoImplicits PTerm -- ^ never run implicit converions on the term
654deriving instance Binary PTerm
655deriving instance NFData PTerm
658mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
659mapPT f t = f (mpt t) where
660 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
661 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
662 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
663 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
665 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
666 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
667 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
668 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
669 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
670 mpt (PPair fc p l r) = PPair fc p (mapPT f l) (mapPT f r)
671 mpt (PDPair fc p l t r) = PDPair fc p (mapPT f l) (mapPT f t) (mapPT f r)
672 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
673 mpt (PHidden t) = PHidden (mapPT f t)
674 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
675 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
676 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
677 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
678 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
679 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
680 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
684data PTactic' t = Intro [Name] | Intros | Focus Name
685 | Refine Name [Bool] | Rewrite t
689 | LetTac Name t | LetTacTy Name t t
690 | Exact t | Compute | Trivial | TCInstance
691 | ProofSearch (Maybe Name) Name [Name]
694 | ProofState | ProofTerm | Undo
695 | Try (PTactic' t) (PTactic' t)
696 | TSeq (PTactic' t) (PTactic' t)
697 | ApplyTactic t -- see Language.Reflection module
701 | GoalType String (PTactic' t)
705 deriving (Show, Eq, Functor)
707deriving instance Binary PTactic'
708deriving instance NFData PTactic'
710instance Sized a => Sized (PTactic' a) where
711 size (Intro nms) = 1 + size nms
713 size (Focus nm) = 1 + size nm
714 size (Refine nm bs) = 1 + size nm + length bs
715 size (Rewrite t) = 1 + size t
716 size (Induction t) = 1 + size t
717 size (LetTac nm t) = 1 + size nm + size t
718 size (Exact t) = 1 + size t
726 size (Try l r) = 1 + size l + size r
727 size (TSeq l r) = 1 + size l + size r
728 size (ApplyTactic t) = 1 + size t
729 size (Reflect t) = 1 + size t
730 size (Fill t) = 1 + size t
734type PTactic = PTactic' PTerm
736data PDo' t = DoExp FC t
741 deriving (Eq, Functor)
743deriving instance Binary PDo'
744deriving instance NFData PDo'
747instance Sized a => Sized (PDo' a) where
748 size (DoExp fc t) = 1 + size fc + size t
749 size (DoBind fc nm t) = 1 + size fc + size nm + size t
750 size (DoBindP fc l r) = 1 + size fc + size l + size r
751 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
752 size (DoLetP fc l r) = 1 + size fc + size l + size r
756-- The priority gives a hint as to elaboration order. Best to elaborate
757-- things early which will help give a more concrete type to other
758-- variables, e.g. a before (interpTy a).
760data PArg' t = PImp { priority :: Int,
761 machine_inf :: Bool, -- true if the machine inferred it
763 pname :: Name, getTm :: t,
765 | PExp { priority :: Int,
769 | PConstraint { priority :: Int,
773 | PTacImplicit { priority :: Int,
779 deriving (Show, Eq, Functor)
781data ArgOpt = Lazy | HideDisplay
784lazyarg :: PArg' t -> Bool
785lazyarg tm = Lazy `elem` argopts tm
787instance Sized a => Sized (PArg' a) where
788 size (PImp p _ l nm trm _) = 1 + size nm + size trm
789 size (PExp p l trm _) = 1 + size trm
790 size (PConstraint p l trm _) = 1 + size trm
791 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
794deriving instance Binary PArg'
795deriving instance NFData PArg'
798pimp n t mach = PImp 1 mach [Lazy] n t ""
799pexp t = PExp 1 [] t ""
800pconst t = PConstraint 1 [] t ""
801ptacimp n s t = PTacImplicit 2 [Lazy] n s t ""
803type PArg = PArg' PTerm
807data ClassInfo = CI { instanceName :: Name,
808 class_methods :: [(Name, (FnOpts, PTerm))],
809 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
810 class_default_superclasses :: [PDecl],
811 class_params :: [Name],
812 class_instances :: [Name] }
815deriving instance Binary ClassInfo
816deriving instance NFData ClassInfo
819-- Type inference data
821data TIData = TIPartial -- ^ a function with a partially defined type
822 | TISolution [Term] -- ^ possible solutions to a metavariable in a type
825-- An argument is conditionally forceable iff its forceability
826-- depends on the collapsibility of the whole type.
827data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
830deriving instance Binary Forceability
831deriving instance NFData Forceability
834data OptInfo = Optimise { collapsible :: Bool,
836 -- The following should actually be (IntMap Forceability)
837 -- but the corresponding Binary instance seems to be broken.
838 -- Let's store a list and convert it to IntMap whenever needed.
839 forceable :: [(Int, Forceability)],
843deriving instance Binary OptInfo
844deriving instance NFData OptInfo
848data TypeInfo = TI { con_names :: [Name],
850 data_opts :: DataOpts,
854deriving instance Binary TypeInfo
855deriving instance NFData TypeInfo
858-- Syntactic sugar info
860data DSL' t = DSL { dsl_bind :: t,
865 index_first :: Maybe t,
866 index_next :: Maybe t,
867 dsl_lambda :: Maybe t,
870 deriving (Show, Functor)
872deriving instance Binary DSL'
873deriving instance NFData DSL'
878data SynContext = PatternSyntax | TermSyntax | AnySyntax
881deriving instance Binary SynContext
882deriving instance NFData SynContext
885data Syntax = Rule [SSymbol] PTerm SynContext
888deriving instance Binary Syntax
889deriving instance NFData Syntax
892data SSymbol = Keyword Name
899deriving instance Binary SSymbol
900deriving instance NFData SSymbol
903initDSL = DSL (PRef f (sUN ">>="))
904 (PRef f (sUN "return"))
906 (PRef f (sUN "pure"))
912 where f = fileFC "(builtin)"
914data Using = UImplicit Name PTerm
915 | UConstraint Name [Name]
918deriving instance Binary Using
919deriving instance NFData Using
922data SyntaxInfo = Syn { using :: [Using],
923 syn_params :: [(Name, PTerm)],
924 syn_namespace :: [String],
926 decoration :: Name -> Name,
928 implicitAllowed :: Bool,
932deriving instance NFData SyntaxInfo
933deriving instance Binary SyntaxInfo
936defaultSyntax = Syn [] [] [] [] id False False initDSL
938expandNS :: SyntaxInfo -> Name -> Name
939expandNS syn n@(NS _ _) = n
940expandNS syn n = case syn_namespace syn of
945-- For inferring types of things
949inferTy = sMN 0 "__Infer"
950inferCon = sMN 0 "__infer"
951inferDecl = PDatadecl inferTy
953 [("", inferCon, PPi impl (sMN 0 "iType") PType (
954 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
955 (PRef bi inferTy)), bi, [])]
958infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
959infP = P (TCon 6 0) inferTy (TType (UVal 0))
961getInferTerm, getInferType :: Term -> Term
962getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
963getInferTerm (App (App _ _) tm) = tm
964getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
966getInferType (Bind n b sc) = Bind n (toTy b) $ getInferType sc
967 where toTy (Lam t) = Pi t
968 toTy (PVar t) = PVTy t
970getInferType (App (App _ ty) _) = ty
974-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
976primNames = [unitTy, unitCon,
977 falseTy, pairTy, pairCon,
978 eqTy, eqCon, inferTy, inferCon]
980unitTy = sMN 0 "__Unit"
981unitCon = sMN 0 "__II"
982unitDecl = PDatadecl unitTy PType
983 [("", unitCon, PRef bi unitTy, bi, [])]
984unitOpts = [DefaultEliminator]
986falseTy = sMN 0 "__False"
987falseDecl = PDatadecl falseTy PType []
990pairTy = sMN 0 "__Pair"
991pairCon = sMN 0 "__MkPair"
992pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
993 [("", pairCon, PPi impl (n "A") PType (
994 PPi impl (n "B") PType (
995 PPi expl (n "a") (PRef bi (n "A")) (
996 PPi expl (n "b") (PRef bi (n "B"))
997 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
998 pexp (PRef bi (n "B"))])))), bi, [])]
1004eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
1005 (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
1007 [("", eqCon, PPi impl (n "A") PType (
1008 PPi impl (n "x") (PRef bi (n "A"))
1009 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
1010 pimp (n "B") Placeholder False,
1011 pexp (PRef bi (n "x")),
1012 pexp (PRef bi (n "x"))])), bi, [])]
1016elimName = sUN "__Elim"
1017elimMethElimTy = sUN "__elimTy"
1018elimMethElim = sUN "elim"
1019elimDecl = PClass "Type class for eliminators" defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
1020 [PTy "" defaultSyntax bi [TotalFn] elimMethElimTy PType,
1021 PTy "" defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
1023-- Defined in builtins.idr
1024sigmaTy = sUN "Exists"
1025existsCon = sUN "Ex_intro"
1027piBind :: [(Name, PTerm)] -> PTerm -> PTerm
1028piBind = piBindp expl
1030piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
1032piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
1035-- Pretty-printing declarations and terms
1037-- These "show" instances render to an absurdly wide screen because inserted line breaks
1038-- could interfere with interactive editing, which calls "show".
1040instance Show PTerm where
1041 showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
1043instance Show PDecl where
1044 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
1046instance Show PClause where
1047 showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
1049instance Show PData where
1050 showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
1052instance Pretty PTerm OutputAnnotation where
1053 pretty = prettyImp False
1055-- | Colourise annotations according to an Idris state. It ignores the names
1056-- in the annotation, as there's no good way to show extended information on a
1058consoleDecorate :: IState -> OutputAnnotation -> String -> String
1059consoleDecorate ist _ | not (idris_colourRepl ist) = id
1060consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1061 in colouriseData theme
1062consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1063 in colouriseType theme
1064consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1065consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1066consoleDecorate ist (AnnName n _ _) = let ctxt = tt_ctxt ist
1067 theme = idris_colourTheme ist
1069 _ | isDConName n ctxt -> colouriseData theme
1070 _ | isFnName n ctxt -> colouriseFun theme
1071 _ | isTConName n ctxt -> colouriseType theme
1072 _ | otherwise -> id -- don't colourise unknown names
1073consoleDecorate ist (AnnFC _) = id
1075-- | Pretty-print a high-level closed Idris term
1076prettyImp :: Bool -- ^^ whether to show implicits
1077 -> PTerm -- ^^ the term to pretty-print
1078 -> Doc OutputAnnotation
1079prettyImp impl = pprintPTerm impl []
1081-- | Pretty-print a high-level Idris term in some bindings context
1082pprintPTerm :: Bool -- ^^ whether to show implicits
1083 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1084 -> PTerm -- ^^ the term to pretty-print
1085 -> Doc OutputAnnotation
1086pprintPTerm impl bnd = prettySe 10 bnd
1088 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1089 prettySe p bnd (PQuote r) =
1090 text "![" <> pretty r <> text "]"
1091 prettySe p bnd (PPatvar fc n) = pretty n
1093 | Just str <- slist p bnd e = str
1094 | Just n <- snat p e = annotate AnnConstData (text (show n))
1095 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1096 prettySe p bnd (PLam n ty sc) =
1097 bracket p 2 . group . align . hang 2 $
1098 text "\\" <> bindingOf n False <+> text "=>" <$>
1099 prettySe 10 ((n, False):bnd) sc
1100 prettySe p bnd (PLet n ty v sc) =
1102 text "let" <+> bindingOf n False <+> text "=" </>
1103 prettySe 10 bnd v <+> text "in" </>
1104 prettySe 10 ((n, False):bnd) sc
1105 prettySe p bnd (PPi (Exp l s _ _) n ty sc)
1106 | n `elem` allNamesIn sc || impl =
1107 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1108 bracket p 2 . group $
1109 enclose open rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1110 st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
1112 bracket p 2 . group $
1113 group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
1117 Static -> text "[static]" <> space
1119 prettySe p bnd (PPi (Imp l s _ _) n ty sc)
1121 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1123 open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1124 st <> text "->" </> prettySe 10 ((n, True):bnd) sc
1125 | otherwise = prettySe 10 ((n, True):bnd) sc
1129 Static -> text "[static]" <> space
1131 prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
1133 prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
1134 prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
1136 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1137 rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
1138 prettySe p bnd (PApp _ (PRef _ f) args) -- normal names, no explicit args
1139 | UN nm <- basename f
1140 , not impl && null (getExps args) = if isAlpha (thead nm)
1141 then prettyName impl bnd f
1142 else enclose lparen rparen $
1143 prettyName impl bnd f
1144 prettySe p bnd (PAppBind _ (PRef _ f) [])
1145 | not impl = text "!" <> prettyName impl bnd f
1146 prettySe p bnd (PApp _ (PRef _ op) args)
1147 | UN nm <- basename op
1149 (not impl) && (not $ isAlpha (thead nm)) =
1150 case getExps args of
1151 [] -> enclose lparen rparen opName
1152 [x] -> group (enclose lparen rparen opName <$> group (prettySe 0 bnd x))
1153 [l,r] -> bracket p 1 $ inFix l r
1154 (l:r:rest) -> bracket p 1 $
1155 enclose lparen rparen (inFix l r) <+>
1156 align (group (vsep (map (prettyArgSe bnd) rest)))
1157 where opName = prettyName impl bnd op
1158 inFix l r = align . group $
1159 (prettySe 1 bnd l <+> opName) <$> group (prettySe 0 bnd r)
1160 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1161 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1162 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1163 prettySe p bnd (PApp _ f as) =
1164 let args = getExps as
1165 fp = prettySe 1 bnd f
1167 bracket p 1 . group $
1171 else fp <+> align (vsep (map (prettyArgS bnd) as))
1174 else fp <+> align (vsep (map (prettyArgSe bnd) args))
1175 prettySe p bnd (PCase _ scr opts) =
1176 text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
1178 prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1180 sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1181 prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1182 prettySe p bnd (PRefl _ _) = annotate (AnnName eqCon Nothing Nothing) $ text "refl"
1183 prettySe p bnd (PResolveTC _) = text "resolvetc"
1184 prettySe p bnd (PTrue _ IsType) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1185 prettySe p bnd (PTrue _ IsTerm) = annotate (AnnName unitCon Nothing Nothing) $ text "()"
1186 prettySe p bnd (PTrue _ TypeOrTerm) = text "()"
1187 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1188 prettySe p bnd (PEq _ l r) =
1189 bracket p 2 . align . group $
1190 prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
1191 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1192 prettySe p bnd (PRewrite _ l r _) =
1194 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1195 prettySe p bnd (PTyped l r) =
1196 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1197 prettySe p bnd pair@(PPair _ pun _ _) -- flatten tuples to the right, like parser
1198 | Just elts <- pairElts pair = enclose (ann lparen) (ann rparen) .
1199 align . group . vsep . punctuate (ann comma) $
1200 map (prettySe 10 bnd) elts
1201 where ann = case pun of
1203 IsType -> annotate (AnnName pairTy Nothing Nothing)
1204 IsTerm -> annotate (AnnName pairCon Nothing Nothing)
1205 prettySe p bnd (PDPair _ TypeOrTerm l t r) =
1206 lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1207 prettySe p bnd (PDPair _ IsType (PRef _ n) t r) =
1208 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1209 bindingOf n False <+>
1210 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1211 prettySe 10 ((n, False):bnd) r <>
1212 annotate (AnnName sigmaTy Nothing Nothing) rparen
1213 prettySe p bnd (PDPair _ IsType l t r) =
1214 annotate (AnnName sigmaTy Nothing Nothing) lparen <>
1215 prettySe 10 bnd l <+>
1216 annotate (AnnName sigmaTy Nothing Nothing) (text "**") <+>
1217 prettySe 10 bnd r <>
1218 annotate (AnnName sigmaTy Nothing Nothing) rparen
1219 prettySe p bnd (PDPair _ IsTerm l t r) =
1220 annotate (AnnName existsCon Nothing Nothing) lparen <>
1221 prettySe 10 bnd l <+>
1222 annotate (AnnName existsCon Nothing Nothing) (text "**") <+>
1223 prettySe 10 bnd r <>
1224 annotate (AnnName existsCon Nothing Nothing) rparen
1225 prettySe p bnd (PAlternative a as) =
1226 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1229 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1230 prettySe p bnd PType = annotate AnnConstType $ text "Type"
1231 prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1232 where annot (AType _) = AnnConstType
1233 annot StrType = AnnConstType
1234 annot PtrType = AnnConstType
1235 annot VoidType = AnnConstType
1236 annot _ = AnnConstData
1237 -- XXX: add pretty for tactics
1238 prettySe p bnd (PProof ts) =
1239 text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1240 prettySe p bnd (PTactics ts) =
1241 text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1242 prettySe p bnd (PMetavar n) = text "?" <> pretty n
1243 prettySe p bnd (PReturn f) = text "return"
1244 prettySe p bnd PImpossible = text "impossible"
1245 prettySe p bnd Placeholder = text "_"
1246 prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1247 prettySe p bnd (PElabError s) = pretty s
1249 prettySe p bnd _ = text "test"
1251 prettyArgS bnd (PImp _ _ _ n tm _) = prettyArgSi bnd (n, tm)
1252 prettyArgS bnd (PExp _ _ tm _) = prettyArgSe bnd tm
1253 prettyArgS bnd (PConstraint _ _ tm _) = prettyArgSc bnd tm
1254 prettyArgS bnd (PTacImplicit _ _ n _ tm _) = prettyArgSti bnd (n, tm)
1256 prettyArgSe bnd arg = prettySe 0 bnd arg
1257 prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1258 prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1259 prettyArgSti bnd (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1261 basename :: Name -> Name
1262 basename (NS n _) = basename n
1265 slist' p bnd (PApp _ (PRef _ nil) _)
1266 | not impl && nsroot nil == sUN "Nil" = Just []
1267 slist' p bnd (PRef _ nil)
1268 | not impl && nsroot nil == sUN "Nil" = Just []
1269 slist' p bnd (PApp _ (PRef _ cons) args)
1270 | nsroot cons == sUN "::",
1271 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1273 Just tl' <- slist' p bnd tl
1276 isImp (PImp {}) = True
1278 slist' _ _ tm = Nothing
1280 slist p bnd e | Just es <- slist' p bnd e = Just $
1281 case es of [] -> annotate AnnConstData $ text "[]"
1282 [x] -> enclose left right . group $
1284 xs -> (enclose left right .
1285 align . group . vsep .
1287 map (prettySe p bnd)) xs
1288 where left = (annotate AnnConstData (text "["))
1289 right = (annotate AnnConstData (text "]"))
1290 comma = (annotate AnnConstData (text ","))
1291 slist _ _ _ = Nothing
1293 pairElts :: PTerm -> Maybe [PTerm]
1294 pairElts (PPair _ _ x y) | Just elts <- pairElts y = Just (x:elts)
1295 | otherwise = Just [x, y]
1296 pairElts _ = Nothing
1298 natns = "Prelude.Nat."
1301 | show z == (natns++"Z") || show z == "Z" = Just 0
1302 snat p (PApp _ s [PExp {getTm=n}])
1303 | show s == (natns++"S") || show s == "S",
1308 bracket outer inner doc
1309 | inner > outer = lparen <> doc <> rparen
1312-- | Pretty-printer helper for the binding site of a name
1313bindingOf :: Name -- ^^ the bound name
1314 -> Bool -- ^^ whether the name is implicit
1315 -> Doc OutputAnnotation
1316bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1318-- | Pretty-printer helper for names that attaches the correct annotations
1319prettyName :: Bool -- ^^ whether to show namespaces
1320 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1321 -> Name -- ^^ the name to pprint
1322 -> Doc OutputAnnotation
1323prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1324 | otherwise = annotate (AnnName n Nothing Nothing) (text (strName n))
1325 where strName (UN n) = T.unpack n
1326 strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1327 | otherwise = strName n
1328 strName (MN i s) = T.unpack s
1329 strName other = show other
1332showCImp :: Bool -> PClause -> Doc OutputAnnotation
1333showCImp impl (PClause _ n l ws r w)
1334 = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1335 <+> text "where" <+> text (show w)
1338 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1339showCImp impl (PWith _ n l ws r w)
1340 = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1341 <+> braces (text (show w))
1344 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1347showDImp :: Bool -> PData -> Doc OutputAnnotation
1348showDImp impl (PDatadecl n ty cons)
1349 = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1350 (indent 2 $ vsep (map (\ (_, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1352showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1353showDecls i ds = vsep (map (showDeclImp i) ds)
1355showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1356showDeclImp i (PTy _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1357showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1358 indent 2 (vsep (map (showCImp i) cs))
1359showDeclImp _ (PData _ _ _ _ d) = showDImp True d
1360showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1361showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1362showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1363showDeclImp i (PClass _ _ _ cs n ps ds)
1364 = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1365showDeclImp i (PInstance _ _ cs n _ t _ ds)
1366 = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1367showDeclImp _ _ = text "..."
1368-- showDeclImp (PImport i) = "import " ++ i
1370instance Show (Doc OutputAnnotation) where
1371 show = flip (displayS . renderCompact) ""
1373getImps :: [PArg] -> [(Name, PTerm)]
1375getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
1376getImps (_ : xs) = getImps xs
1378getExps :: [PArg] -> [PTerm]
1380getExps (PExp _ _ tm _ : xs) = tm : getExps xs
1381getExps (_ : xs) = getExps xs
1383getConsts :: [PArg] -> [PTerm]
1385getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
1386getConsts (_ : xs) = getConsts xs
1388getAll :: [PArg] -> [PTerm]
1393showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1394 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1395 -> Bool -- ^^ whether to show implicits
1396 -> Bool -- ^^ whether to colourise
1397 -> Name -- ^^ the term to show
1399showName ist bnd impl colour n = case ist of
1400 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1401 Nothing -> showbasic n
1402 where name = if impl then show n else showbasic n
1403 showbasic n@(UN _) = showCG n
1404 showbasic (MN _ s) = str s
1405 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1406 showbasic (SN s) = show s
1408 colourise n t = let ctxt' = fmap tt_ctxt ist in
1411 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1412 else colouriseBound t name
1413 | isDConName n ctxt -> colouriseData t name
1414 | isFnName n ctxt -> colouriseFun t name
1415 | isTConName n ctxt -> colouriseType t name
1416 -- The assumption is that if a name is not bound and does not exist in the
1417 -- global context, then we're somewhere in which implicit info has been lost
1418 -- (like error messages). Thus, unknown vars are colourised as implicits.
1419 | otherwise -> colouriseImplicit t name
1421showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1422 -> PTerm -- ^^ the term to show
1424showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
1426-- | Show a term with implicits, no colours
1427showTmImpls :: PTerm -> String
1428showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1431instance Sized PTerm where
1432 size (PQuote rawTerm) = size rawTerm
1433 size (PRef fc name) = size name
1434 size (PLam name ty bdy) = 1 + size ty + size bdy
1435 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1436 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1437 size (PTyped trm ty) = 1 + size trm + size ty
1438 size (PApp fc name args) = 1 + size args
1439 size (PAppBind fc name args) = 1 + size args
1440 size (PCase fc trm bdy) = 1 + size trm + size bdy
1441 size (PTrue fc _) = 1
1442 size (PFalse fc) = 1
1443 size (PRefl fc _) = 1
1444 size (PResolveTC fc) = 1
1445 size (PEq fc left right) = 1 + size left + size right
1446 size (PRewrite fc left right _) = 1 + size left + size right
1447 size (PPair fc _ left right) = 1 + size left + size right
1448 size (PDPair fs _ left ty right) = 1 + size left + size ty + size right
1449 size (PAlternative a alts) = 1 + size alts
1450 size (PHidden hidden) = size hidden
1451 size (PUnifyLog tm) = size tm
1452 size (PDisamb _ tm) = size tm
1453 size (PNoImplicits tm) = size tm
1455 size (PConstant const) = 1 + size const
1456 size Placeholder = 1
1457 size (PDoBlock dos) = 1 + size dos
1458 size (PIdiom fc term) = 1 + size term
1459 size (PReturn fc) = 1
1460 size (PMetavar name) = 1
1461 size (PProof tactics) = size tactics
1462 size (PElabError err) = size err
1463 size PImpossible = 1
1465getPArity :: PTerm -> Int
1466getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1469-- Return all names, free or globally bound, in the given term.
1471allNamesIn :: PTerm -> [Name]
1472allNamesIn tm = nub $ ni [] tm
1475 | not (n `elem` env) = [n]
1476 ni env (PPatvar _ n) = [n]
1477 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1478 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1479 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1480 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1481 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1482 ni env (PHidden tm) = ni env tm
1483 ni env (PEq _ l r) = ni env l ++ ni env r
1484 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1485 ni env (PTyped l r) = ni env l ++ ni env r
1486 ni env (PPair _ _ l r) = ni env l ++ ni env r
1487 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1488 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1489 ni env (PAlternative a ls) = concatMap (ni env) ls
1490 ni env (PUnifyLog tm) = ni env tm
1491 ni env (PDisamb _ tm) = ni env tm
1492 ni env (PNoImplicits tm) = ni env tm
1495-- Return all names defined in binders in the given term
1496boundNamesIn :: PTerm -> [Name]
1497boundNamesIn tm = nub $ ni tm
1499 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1500 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1501 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1502 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1503 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1504 ni (PPi _ n ty sc) = n : (ni ty ++ ni sc)
1505 ni (PEq _ l r) = ni l ++ ni r
1506 ni (PRewrite _ l r _) = ni l ++ ni r
1507 ni (PTyped l r) = ni l ++ ni r
1508 ni (PPair _ _ l r) = ni l ++ ni r
1509 ni (PDPair _ _ (PRef _ n) t r) = ni t ++ ni r
1510 ni (PDPair _ _ l t r) = ni l ++ ni t ++ ni r
1511 ni (PAlternative a as) = concatMap (ni) as
1512 ni (PHidden tm) = ni tm
1513 ni (PUnifyLog tm) = ni tm
1514 ni (PDisamb _ tm) = ni tm
1515 ni (PNoImplicits tm) = ni tm
1518-- Return names which are free in the given term.
1519namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1520namesIn uvars ist tm = nub $ ni [] tm
1523 | not (n `elem` env)
1524 = case lookupTy n (tt_ctxt ist) of
1526 _ -> if n `elem` (map fst uvars) then [n] else []
1527 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1528 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1529 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1530 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1531 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1532 ni env (PEq _ l r) = ni env l ++ ni env r
1533 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1534 ni env (PTyped l r) = ni env l ++ ni env r
1535 ni env (PPair _ _ l r) = ni env l ++ ni env r
1536 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1537 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1538 ni env (PAlternative a as) = concatMap (ni env) as
1539 ni env (PHidden tm) = ni env tm
1540 ni env (PUnifyLog tm) = ni env tm
1541 ni env (PDisamb _ tm) = ni env tm
1542 ni env (PNoImplicits tm) = ni env tm
1545-- Return which of the given names are used in the given term.
1547usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1548usedNamesIn vars ist tm = nub $ ni [] tm
1551 | n `elem` vars && not (n `elem` env)
1552 = case lookupTy n (tt_ctxt ist) of
1555 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1556 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1557 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1558 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1559 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1560 ni env (PEq _ l r) = ni env l ++ ni env r
1561 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1562 ni env (PTyped l r) = ni env l ++ ni env r
1563 ni env (PPair _ _ l r) = ni env l ++ ni env r
1564 ni env (PDPair _ _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1565 ni env (PDPair _ _ l t r) = ni env l ++ ni env t ++ ni env r
1566 ni env (PAlternative a as) = concatMap (ni env) as
1567 ni env (PHidden tm) = ni env tm
1568 ni env (PUnifyLog tm) = ni env tm
1569 ni env (PDisamb _ tm) = ni env tm
1570 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