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
27import qualified Data.Text as T
29import Data.Word (Word)
33import Text.PrettyPrint.Annotated.Leijen
35data IOption = IOption { opt_logLevel :: Int,
37 opt_typeintype :: Bool,
39 opt_showimp :: Bool, -- ^^ show implicits
40 opt_errContext :: Bool,
45 opt_codegen :: Codegen,
46 opt_outputTy :: OutputType,
47 opt_ibcsubdir :: FilePath,
48 opt_importdirs :: [FilePath],
52 opt_cmdline :: [Opt] -- remember whole command line
56defaultOpts = IOption { opt_logLevel = 0
57 , opt_typecase = False
58 , opt_typeintype = False
61 , opt_errContext = False
64 , opt_nobanner = False
67 , opt_outputTy = Executable
76data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
78-- | The output mode in use
79data OutputMode = RawOutput | IdeSlave Integer deriving Show
81-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
82-- without typechecking).
83-- This will include all the functions and data declarations, plus fixity declarations
86-- | The global state used in the Idris monad
88 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
89 idris_constraints :: [(UConstraint, FC)],
90 -- ^ A list of universe constraints and their corresponding source locations
91 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
92 idris_implicits :: Ctxt [PArg],
93 idris_statics :: Ctxt [Bool],
94 idris_classes :: Ctxt ClassInfo,
95 idris_dsls :: Ctxt DSL,
96 idris_optimisation :: Ctxt OptInfo,
97 idris_datatypes :: Ctxt TypeInfo,
98 idris_namehints :: Ctxt [Name],
99 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
100 -- ^ list of lhs/rhs, and a list of missing clauses
101 idris_flags :: Ctxt [FnOpt],
102 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
103 idris_calledgraph :: Ctxt [Name],
104 idris_docstrings :: Ctxt String,
105 idris_totcheck :: [(FC, Name)], -- names to check totality on
106 idris_defertotcheck :: [(FC, Name)], -- names to check at the end
107 idris_options :: IOption,
109 idris_lineapps :: [((FilePath, Int), PTerm)],
110 -- ^ Full application LHS on source line
111 idris_metavars :: [(Name, (Maybe Name, Int, Bool))], -- ^ The currently defined but not proven metavariables
112 idris_coercions :: [Name],
113 idris_transforms :: [(Term, Term)],
114 syntax_rules :: [Syntax],
115 syntax_keywords :: [String],
116 imported :: [FilePath], -- ^ The imported modules
117 idris_scprims :: [(Name, (Int, PrimFn))],
118 idris_objs :: [(Codegen, FilePath)],
119 idris_libs :: [(Codegen, String)],
120 idris_cgflags :: [(Codegen, String)],
121 idris_hdrs :: [(Codegen, String)],
122 proof_list :: [(Name, [String])],
123 errLine :: Maybe Int,
124 lastParse :: Maybe Name,
125 indent_stack :: [Int],
126 brace_stack :: [Maybe Int],
127 hide_list :: [(Name, Maybe Accessibility)],
128 default_access :: Accessibility,
129 default_total :: Bool,
130 ibc_write :: [IBCWrite],
131 compiled_so :: Maybe String,
132 idris_dynamic_libs :: [DynamicLib],
133 idris_language_extensions :: [LanguageExt],
134 idris_outputmode :: OutputMode,
135 idris_colourRepl :: Bool,
136 idris_colourTheme :: ColourTheme,
137 idris_outh :: Handle,
138 idris_errorhandlers :: [Name],
139 idris_nameIdx :: (Int, Ctxt (Int, Name))
142data SizeChange = Smaller | Same | Bigger | Unknown
145deriving instance Binary SizeChange
146deriving instance NFData SizeChange
149type SCGEntry = (Name, [Maybe (Int, SizeChange)])
151data CGInfo = CGInfo { argsdef :: [Name],
152 calls :: [(Name, [[Name]])],
158deriving instance Binary CGInfo
159deriving instance NFData CGInfo
162primDefs = [sUN "unsafePerformPrimIO",
163 sUN "mkLazyForeignPrim",
167-- information that needs writing for the current module's .ibc file
168data IBCWrite = IBCFix FixDecl
172 | IBCInstance Bool Name Name
179 | IBCObj Codegen FilePath
180 | IBCLib Codegen String
181 | IBCCGFlag Codegen String
183 | IBCHeader Codegen String
184 | IBCAccess Name Accessibility
185 | IBCMetaInformation Name MetaInformation
186 | IBCTotal Name Totality
187 | IBCFlags Name [FnOpt]
188 | IBCTrans (Term, Term)
192 | IBCDef Name -- i.e. main context
193 | IBCNameHint (Name, Name)
194 | IBCLineApp FilePath Int PTerm
197-- | The initial state for the compiler
199idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
200 emptyContext emptyContext emptyContext emptyContext
201 emptyContext emptyContext emptyContext emptyContext
203 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] []
204 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
205 True defaultTheme stdout [] (0, emptyContext)
207-- | The monad for the main REPL - reading and processing files and updating
208-- global state (hence the IO inner monad).
209--type Idris = WriterT [Either String (IO ())] (State IState a))
210type Idris = StateT IState (ErrorT Err IO)
212-- Commands in the REPL
231 | ChangeDirectory FilePath
234 | Compile Codegen String
239 | AddProof (Maybe Name)
251 | DynamicLink FilePath
256 | CaseSplitAt Bool Int Name
257 | AddClauseFrom Bool Int Name
258 | AddProofClauseFrom Bool Int Name
259 | AddMissing Bool Int Name
260 | MakeWith Bool Int Name
261 | DoProofSearch Bool Int Name [Name]
265 | SetColour ColourType IdrisColour
270data Opt = Filename String
282 | NoBuiltins -- only for the really primitive stuff!
307 | OutputTy OutputType
308 | Extension LanguageExt
309 | InterpretScript String
310 | TargetTriple String
316-- Parsed declarations
318data Fixity = Infixl { prec :: Int }
319 | Infixr { prec :: Int }
320 | InfixN { prec :: Int }
321 | PrefixN { prec :: Int }
324deriving instance Binary Fixity
325deriving instance NFData Fixity
328instance Show Fixity where
329 show (Infixl i) = "infixl " ++ show i
330 show (Infixr i) = "infixr " ++ show i
331 show (InfixN i) = "infix " ++ show i
332 show (PrefixN i) = "prefix " ++ show i
334data FixDecl = Fix Fixity String
337instance Show FixDecl where
338 show (Fix f s) = show f ++ " " ++ s
341deriving instance Binary FixDecl
342deriving instance NFData FixDecl
345instance Ord FixDecl where
346 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
349data Static = Static | Dynamic
352deriving instance Binary Static
353deriving instance NFData Static
356-- Mark bindings with their explicitness, and laziness
357data Plicity = Imp { plazy :: Bool,
361 | Exp { plazy :: Bool,
365 | Constraint { plazy :: Bool,
368 | TacImp { plazy :: Bool,
375deriving instance Binary Plicity
376deriving instance NFData Plicity
379impl = Imp False Dynamic "" False
380expl = Exp False Dynamic "" False
381expl_param = Exp False Dynamic "" True
382constraint = Constraint False Dynamic ""
383tacimpl t = TacImp False Dynamic t ""
385data FnOpt = Inlinable -- always evaluate when simplifying
386 | TotalFn | PartialFn
387 | Coinductive | AssertTotal
388 | Dictionary -- type class dictionary, eval only when
389 -- a function argument, and further evaluation resutls
390 | Implicit -- implicit coercion
391 | CExport String -- export, with a C name
392 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
393 | Reflection -- a reflecting function, compile-time only
394 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
397deriving instance Binary FnOpt
398deriving instance NFData FnOpt
403inlinable :: FnOpts -> Bool
404inlinable = elem Inlinable
406dictionary :: FnOpts -> Bool
407dictionary = elem Dictionary
410-- | Data declaration options
411data DataOpt = Codata -- Set if the the data-type is coinductive
412 | DefaultEliminator -- Set if an eliminator should be generated for data type
415type DataOpts = [DataOpt]
417-- | Top-level declarations such as compiler directives, definitions,
418-- datatypes and typeclasses.
420 = PFix FC Fixity [String] -- ^ Fixity declaration
421 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
422 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
423 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
424 | PCAF FC Name t -- ^ Top level constant
425 | PData String SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
426 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
427 | PNamespace String [PDecl' t] -- ^ New namespace
428 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
429 | PClass String SyntaxInfo FC
432 [(Name, t)] -- parameters
433 [PDecl' t] -- declarations
434 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
435 -- class name, parameters, method declarations
436 | PInstance SyntaxInfo FC [t] -- constraints
439 t -- full instance type
440 (Maybe Name) -- explicit name
442 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
443 -- class name, parameters, full instance type, optional explicit name, and definitions
444 | PDSL Name (DSL' t) -- ^ DSL declaration
445 | PSyntax FC Syntax -- ^ Syntax definition
446 | PMutual FC [PDecl' t] -- ^ Mutual block
447 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
448 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
449 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
450 -- bool is True, lhs and rhs must be convertible
453deriving instance Binary PDecl'
454deriving instance NFData PDecl'
457-- For elaborator state
458type ElabD a = Elab' [PDecl] a
460-- | One clause of a top-level definition. Term arguments to constructors are:
462-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
464-- 2. The list of extra 'with' patterns
466-- 3. The right-hand side
468-- 4. The where block (PDecl' t)
470data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
471 | PWith FC Name t [t] t [PDecl' t]
472 | PClauseR FC [t] t [PDecl' t]
473 | PWithR FC [t] t [PDecl' t]
476deriving instance Binary PClause'
477deriving instance NFData PClause'
481data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
482 d_tcon :: t, -- ^ Type constructor
483 d_cons :: [(String, Name, t, FC, [Name])] -- ^ Constructors
485 -- ^ Data declaration
486 | PLaterdecl { d_name :: Name, d_tcon :: t }
487 -- ^ "Placeholder" for data whose constructors are defined later
490deriving instance Binary PData'
491deriving instance NFData PData'
494-- Handy to get a free function for applying PTerm -> PTerm functions
495-- across a program, by deriving Functor
497type PDecl = PDecl' PTerm
498type PData = PData' PTerm
499type PClause = PClause' PTerm
501-- get all the names declared in a decl
503declared :: PDecl -> [Name]
504declared (PFix _ _ _) = []
505declared (PTy _ _ _ _ n t) = [n]
506declared (PPostulate _ _ _ _ n t) = [n]
507declared (PClauses _ _ n _) = [] -- not a declaration
508declared (PCAF _ n _) = [n]
509declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
510 where fstt (_, a, _, _, _) = a
511declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
512declared (PParams _ _ ds) = concatMap declared ds
513declared (PNamespace _ ds) = concatMap declared ds
514declared (PRecord _ _ _ n _ _ c _) = [n, c]
515declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
516declared (PInstance _ _ _ _ _ _ _ _) = []
517declared (PDSL n _) = [n]
518declared (PSyntax _ _) = []
519declared (PMutual _ ds) = concatMap declared ds
520declared (PDirective _) = []
522-- get the names declared, not counting nested parameter blocks
523tldeclared :: PDecl -> [Name]
524tldeclared (PFix _ _ _) = []
525tldeclared (PTy _ _ _ _ n t) = [n]
526tldeclared (PPostulate _ _ _ _ n t) = [n]
527tldeclared (PClauses _ _ n _) = [] -- not a declaration
528tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
529tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
530 where fstt (_, a, _, _, _) = a
531tldeclared (PParams _ _ ds) = []
532tldeclared (PMutual _ ds) = concatMap tldeclared ds
533tldeclared (PNamespace _ ds) = concatMap tldeclared ds
534tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
535tldeclared (PInstance _ _ _ _ _ _ _ _) = []
538defined :: PDecl -> [Name]
539defined (PFix _ _ _) = []
540defined (PTy _ _ _ _ n t) = []
541defined (PPostulate _ _ _ _ n t) = []
542defined (PClauses _ _ n _) = [n] -- not a declaration
543defined (PCAF _ n _) = [n]
544defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
545 where fstt (_, a, _, _, _) = a
546defined (PData _ _ _ _ (PLaterdecl n _)) = []
547defined (PParams _ _ ds) = concatMap defined ds
548defined (PNamespace _ ds) = concatMap defined ds
549defined (PRecord _ _ _ n _ _ c _) = [n, c]
550defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
551defined (PInstance _ _ _ _ _ _ _ _) = []
552defined (PDSL n _) = [n]
553defined (PSyntax _ _) = []
554defined (PMutual _ ds) = concatMap defined ds
555defined (PDirective _) = []
558updateN :: [(Name, Name)] -> Name -> Name
559updateN ns n | Just n' <- lookup n ns = n'
562updateNs :: [(Name, Name)] -> PTerm -> PTerm
564updateNs ns t = mapPT updateRef t
565 where updateRef (PRef fc f) = PRef fc (updateN ns f)
568-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
570-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
571-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
572-- where updateCNs ns (PClause n l ts r ds)
573-- = PClause (updateN ns n) (fmap (updateNs ns) l)
574-- (map (fmap (updateNs ns)) ts)
575-- (fmap (updateNs ns) r)
576-- (map (updateDNs ns) ds)
579-- | High level language terms
580data PTerm = PQuote Raw
582 | PInferRef FC Name -- ^ A name to be defined later
584 | PLam Name PTerm PTerm
585 | PPi Plicity Name PTerm PTerm
586 | PLet Name PTerm PTerm PTerm
587 | PTyped PTerm PTerm -- ^ Term with explicit type
588 | PApp FC PTerm [PArg]
589 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
590 | PMatchApp FC Name -- ^ Make an application by type matching
591 | PCase FC PTerm [(PTerm, PTerm)]
597 | PRewrite FC PTerm PTerm (Maybe PTerm)
598 | PPair FC PTerm PTerm
599 | PDPair FC PTerm PTerm PTerm
600 | PAlternative Bool [PTerm] -- True if only one may work
601 | PHidden PTerm -- ^ Irrelevant or hidden pattern
603 | PGoal FC PTerm Name PTerm
610 | PProof [PTactic] -- ^ Proof script
611 | PTactics [PTactic] -- ^ As PProof, but no auto solving
612 | PElabError Err -- ^ Error to report on elaboration
613 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
614 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
615 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
616 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
617 | PNoImplicits PTerm -- ^ never run implicit converions on the term
622deriving instance Binary PTerm
623deriving instance NFData PTerm
626mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
627mapPT f t = f (mpt t) where
628 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
629 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
630 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
631 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
633 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
634 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
635 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
636 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
637 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
638 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
639 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
640 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
641 mpt (PHidden t) = PHidden (mapPT f t)
642 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
643 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
644 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
645 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
646 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
647 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
648 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
652data PTactic' t = Intro [Name] | Intros | Focus Name
653 | Refine Name [Bool] | Rewrite t
657 | LetTac Name t | LetTacTy Name t t
658 | Exact t | Compute | Trivial
659 | ProofSearch (Maybe Name) Name [Name]
662 | ProofState | ProofTerm | Undo
663 | Try (PTactic' t) (PTactic' t)
664 | TSeq (PTactic' t) (PTactic' t)
665 | ApplyTactic t -- see Language.Reflection module
669 | GoalType String (PTactic' t)
671 deriving (Show, Eq, Functor)
673deriving instance Binary PTactic'
674deriving instance NFData PTactic'
677instance Sized a => Sized (PTactic' a) where
678 size (Intro nms) = 1 + size nms
680 size (Focus nm) = 1 + size nm
681 size (Refine nm bs) = 1 + size nm + length bs
682 size (Rewrite t) = 1 + size t
683 size (Induction t) = 1 + size t
684 size (LetTac nm t) = 1 + size nm + size t
685 size (Exact t) = 1 + size t
693 size (Try l r) = 1 + size l + size r
694 size (TSeq l r) = 1 + size l + size r
695 size (ApplyTactic t) = 1 + size t
696 size (Reflect t) = 1 + size t
697 size (Fill t) = 1 + size t
701type PTactic = PTactic' PTerm
703data PDo' t = DoExp FC t
708 deriving (Eq, Functor)
710deriving instance Binary PDo'
711deriving instance NFData PDo'
714instance Sized a => Sized (PDo' a) where
715 size (DoExp fc t) = 1 + size fc + size t
716 size (DoBind fc nm t) = 1 + size fc + size nm + size t
717 size (DoBindP fc l r) = 1 + size fc + size l + size r
718 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
719 size (DoLetP fc l r) = 1 + size fc + size l + size r
723-- The priority gives a hint as to elaboration order. Best to elaborate
724-- things early which will help give a more concrete type to other
725-- variables, e.g. a before (interpTy a).
727data PArg' t = PImp { priority :: Int,
728 machine_inf :: Bool, -- true if the machine inferred it
729 lazyarg :: Bool, pname :: Name, getTm :: t,
731 | PExp { priority :: Int,
732 lazyarg :: Bool, getTm :: t,
734 | PConstraint { priority :: Int,
735 lazyarg :: Bool, getTm :: t,
737 | PTacImplicit { priority :: Int,
738 lazyarg :: Bool, pname :: Name,
742 deriving (Show, Eq, Functor)
744instance Sized a => Sized (PArg' a) where
745 size (PImp p _ l nm trm _) = 1 + size nm + size trm
746 size (PExp p l trm _) = 1 + size trm
747 size (PConstraint p l trm _) = 1 + size trm
748 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
751deriving instance Binary PArg'
752deriving instance NFData PArg'
755pimp n t mach = PImp 1 mach True n t ""
756pexp t = PExp 1 False t ""
757pconst t = PConstraint 1 False t ""
758ptacimp n s t = PTacImplicit 2 True n s t ""
760type PArg = PArg' PTerm
764data ClassInfo = CI { instanceName :: Name,
765 class_methods :: [(Name, (FnOpts, PTerm))],
766 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
767 class_default_superclasses :: [PDecl],
768 class_params :: [Name],
769 class_instances :: [Name] }
772deriving instance Binary ClassInfo
773deriving instance NFData ClassInfo
776-- An argument is conditionally forceable iff its forceability
777-- depends on the collapsibility of the whole type.
778data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
781deriving instance Binary Forceability
782deriving instance NFData Forceability
785data OptInfo = Optimise { collapsible :: Bool,
787 -- The following should actually be (IntMap Forceability)
788 -- but the corresponding Binary instance seems to be broken.
789 -- Let's store a list and convert it to IntMap whenever needed.
790 forceable :: [(Int, Forceability)],
794deriving instance Binary OptInfo
795deriving instance NFData OptInfo
799data TypeInfo = TI { con_names :: [Name],
804deriving instance Binary TypeInfo
805deriving instance NFData TypeInfo
808-- Syntactic sugar info
810data DSL' t = DSL { dsl_bind :: t,
815 index_first :: Maybe t,
816 index_next :: Maybe t,
817 dsl_lambda :: Maybe t,
820 deriving (Show, Functor)
822deriving instance Binary DSL'
823deriving instance NFData DSL'
828data SynContext = PatternSyntax | TermSyntax | AnySyntax
831deriving instance Binary SynContext
832deriving instance NFData SynContext
835data Syntax = Rule [SSymbol] PTerm SynContext
838deriving instance Binary Syntax
839deriving instance NFData Syntax
842data SSymbol = Keyword Name
849deriving instance Binary SSymbol
850deriving instance NFData SSymbol
853initDSL = DSL (PRef f (sUN ">>="))
854 (PRef f (sUN "return"))
856 (PRef f (sUN "pure"))
862 where f = fileFC "(builtin)"
864data Using = UImplicit Name PTerm
865 | UConstraint Name [Name]
868deriving instance Binary Using
869deriving instance NFData Using
872data SyntaxInfo = Syn { using :: [Using],
873 syn_params :: [(Name, PTerm)],
874 syn_namespace :: [String],
876 decoration :: Name -> Name,
878 implicitAllowed :: Bool,
882deriving instance NFData SyntaxInfo
883deriving instance Binary SyntaxInfo
886defaultSyntax = Syn [] [] [] [] id False False initDSL
888expandNS :: SyntaxInfo -> Name -> Name
889expandNS syn n@(NS _ _) = n
890expandNS syn n = case syn_namespace syn of
895-- For inferring types of things
899inferTy = sMN 0 "__Infer"
900inferCon = sMN 0 "__infer"
901inferDecl = PDatadecl inferTy
903 [("", inferCon, PPi impl (sMN 0 "iType") PType (
904 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
905 (PRef bi inferTy)), bi, [])]
908infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
909infP = P (TCon 6 0) inferTy (TType (UVal 0))
911getInferTerm, getInferType :: Term -> Term
912getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
913getInferTerm (App (App _ _) tm) = tm
914getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
916getInferType (Bind n b sc) = Bind n b $ getInferType sc
917getInferType (App (App _ ty) _) = ty
921-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign, Elim type class
923primNames = [unitTy, unitCon,
924 falseTy, pairTy, pairCon,
925 eqTy, eqCon, inferTy, inferCon]
927unitTy = sMN 0 "__Unit"
928unitCon = sMN 0 "__II"
929unitDecl = PDatadecl unitTy PType
930 [("", unitCon, PRef bi unitTy, bi, [])]
931unitOpts = [DefaultEliminator]
933falseTy = sMN 0 "__False"
934falseDecl = PDatadecl falseTy PType []
937pairTy = sMN 0 "__Pair"
938pairCon = sMN 0 "__MkPair"
939pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
940 [("", pairCon, PPi impl (n "A") PType (
941 PPi impl (n "B") PType (
942 PPi expl (n "a") (PRef bi (n "A")) (
943 PPi expl (n "b") (PRef bi (n "B"))
944 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
945 pexp (PRef bi (n "B"))])))), bi, [])]
951eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
952 (n "x", PRef bi (n "A")), (n "y", PRef bi (n "B"))]
954 [("", eqCon, PPi impl (n "A") PType (
955 PPi impl (n "x") (PRef bi (n "A"))
956 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
957 pimp (n "B") Placeholder False,
958 pexp (PRef bi (n "x")),
959 pexp (PRef bi (n "x"))])), bi, [])]
963elimName = sUN "__Elim"
964elimMethElimTy = sUN "__elimTy"
965elimMethElim = sUN "elim"
966elimDecl = PClass "Type class for eliminators" defaultSyntax bi [] elimName [(sUN "scrutineeType", PType)]
967 [PTy "" defaultSyntax bi [TotalFn] elimMethElimTy PType,
968 PTy "" defaultSyntax bi [TotalFn] elimMethElim (PRef bi elimMethElimTy)]
970-- Defined in builtins.idr
971sigmaTy = sUN "Exists"
972existsCon = sUN "Ex_intro"
974piBind :: [(Name, PTerm)] -> PTerm -> PTerm
977piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
979piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
982-- Pretty-printing declarations and terms
984instance Show PTerm where
985 showsPrec _ tm = (displayS . renderCompact . prettyImp False) tm
987instance Show PDecl where
988 showsPrec _ d = (displayS . renderCompact . showDeclImp False) d
990instance Show PClause where
991 showsPrec _ c = (displayS . renderCompact . showCImp True) c
993instance Show PData where
994 showsPrec _ d = (displayS . renderCompact . showDImp False) d
996instance Pretty PTerm OutputAnnotation where
997 pretty = prettyImp False
999-- | Colourise annotations according to an Idris state. It ignores the names
1000-- in the annotation, as there's no good way to show extended information on a
1002consoleDecorate :: IState -> OutputAnnotation -> String -> String
1003consoleDecorate ist _ | not (idris_colourRepl ist) = id
1004consoleDecorate ist AnnConstData = let theme = idris_colourTheme ist
1005 in colouriseData theme
1006consoleDecorate ist AnnConstType = let theme = idris_colourTheme ist
1007 in colouriseType theme
1008consoleDecorate ist (AnnBoundName _ True) = colouriseImplicit (idris_colourTheme ist)
1009consoleDecorate ist (AnnBoundName _ False) = colouriseBound (idris_colourTheme ist)
1010consoleDecorate ist (AnnName n _ _) = let ctxt = tt_ctxt ist
1011 theme = idris_colourTheme ist
1013 _ | isDConName n ctxt -> colouriseData theme
1014 _ | isFnName n ctxt -> colouriseFun theme
1015 _ | isTConName n ctxt -> colouriseType theme
1016 _ | otherwise -> id -- don't colourise unknown names
1018-- | Pretty-print a high-level closed Idris term
1019prettyImp :: Bool -- ^^ whether to show implicits
1020 -> PTerm -- ^^ the term to pretty-print
1021 -> Doc OutputAnnotation
1022prettyImp impl = pprintPTerm impl []
1024-- | Pretty-print a high-level Idris term in some bindings context
1025pprintPTerm :: Bool -- ^^ whether to show implicits
1026 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
1027 -> PTerm -- ^^ the term to pretty-print
1028 -> Doc OutputAnnotation
1029pprintPTerm impl bnd = prettySe 10 bnd
1031 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1032 prettySe p bnd (PQuote r) =
1033 if size r > breakingSize then
1034 text "![" <> line <> pretty r <> text "]"
1036 text "![" <> pretty r <> text "]"
1037 prettySe p bnd (PPatvar fc n) = pretty n
1039 | Just str <- slist p bnd e = str
1040 | Just n <- snat p e = annotate AnnConstData (text (show n))
1041 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1042 prettySe p bnd (PLam n ty sc) =
1044 if size sc > breakingSize then
1045 text "\\" <> bindingOf n False <+> text "=>" <> line <> prettySe 10 ((n, False):bnd) sc
1047 text "\\" <> bindingOf n False <+> text "=>" <+> prettySe 10 ((n, False):bnd) sc
1048 prettySe p bnd (PLet n ty v sc) =
1050 if size sc > breakingSize then
1051 text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <>
1052 nest nestingSize (prettySe 10 ((n, False):bnd) sc)
1054 text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <+>
1055 prettySe 10 ((n, False):bnd) sc
1056 prettySe p bnd (PPi (Exp l s _ _) n ty sc)
1057 | n `elem` allNamesIn sc || impl =
1058 let open = (if l then text "|" else empty) <> lparen in
1060 if size sc > breakingSize then
1061 enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1062 st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
1064 enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1065 st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
1068 if size sc > breakingSize then
1069 prettySe 0 bnd ty <+> st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
1071 prettySe 0 bnd ty <+> st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
1075 Static -> text "[static]" <> space
1077 prettySe p bnd (PPi (Imp l s _ _) n ty sc)
1079 let open = if l then text "|" <> lbrace else lbrace in
1081 if size sc > breakingSize then
1082 open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1083 st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
1085 open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1086 st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
1087 | otherwise = prettySe 10 ((n, True):bnd) sc
1091 Static -> text "[static]" <> space
1093 prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
1095 if size sc > breakingSize then
1096 prettySe 10 bnd ty <+> text "=>" <+> prettySe 10 ((n, True):bnd) sc
1098 prettySe 10 bnd ty <+> text "=>" <> line <> prettySe 10 ((n, True):bnd) sc
1099 prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
1101 if size sc > breakingSize then
1102 lbrace <> text "tacimp" <+> bindingOf n True <+> colon <+> prettySe 10 bnd ty <>
1103 rbrace <+> text "->" <> line <> prettySe 10 ((n, True):bnd) sc
1105 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1106 rbrace <+> text "->" <+> prettySe 10 ((n, True):bnd) sc
1107 prettySe p bnd (PApp _ (PRef _ f) [])
1108 | not impl = prettyName impl bnd f
1109 prettySe p bnd (PAppBind _ (PRef _ f) [])
1110 | not impl = text "!" <> prettyName impl bnd f
1111 prettySe p bnd (PApp _ (PRef _ op@(UN nm)) args)
1113 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
1114 let [l, r] = getExps args in
1116 if size r > breakingSize then
1117 prettySe 1 bnd l <+> prettyName impl bnd op <> line <> prettySe 0 bnd r
1119 prettySe 1 bnd l <+> prettyName impl bnd op <+> prettySe 0 bnd r
1120 prettySe p bnd (PApp _ f as) =
1121 let args = getExps as
1122 fp = prettySe 1 bnd f
1126 then fp : map (prettyArgS bnd) as
1127 else fp : map (prettyArgSe bnd) args)
1128 prettySe p bnd (PCase _ scr opts) =
1129 text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
1131 prettyBody = foldr (<>) empty $ intersperse (text "|") $ map sc opts
1133 sc (l, r) = nest nestingSize $ prettySe 10 bnd l <+> text "=>" <+> prettySe 10 bnd r
1134 prettySe p bnd (PHidden tm) = text "." <> prettySe 0 bnd tm
1135 prettySe p bnd (PRefl _ _) = annotate (AnnName eqCon Nothing Nothing) $ text "refl"
1136 prettySe p bnd (PResolveTC _) = text "resolvetc"
1137 prettySe p bnd (PTrue _) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1138 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1139 prettySe p bnd (PEq _ l r) =
1141 if size r > breakingSize then
1142 prettySe 10 bnd l <+> eq <$> nest nestingSize (prettySe 10 bnd r)
1144 prettySe 10 bnd l <+> eq <+> prettySe 10 bnd r
1145 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1146 prettySe p bnd (PRewrite _ l r _) =
1148 if size r > breakingSize then
1149 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <> nest nestingSize (prettySe 10 bnd r)
1151 text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1152 prettySe p bnd (PTyped l r) =
1153 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1154 prettySe p bnd (PPair _ l r) =
1155 if size r > breakingSize then
1156 lparen <> prettySe 10 bnd l <> text "," <>
1157 line <> prettySe 10 bnd r <> rparen
1159 lparen <> prettySe 10 bnd l <> text "," <+> prettySe 10 bnd r <> rparen
1160 prettySe p bnd (PDPair _ l t r) =
1161 if size r > breakingSize then
1162 lparen <> prettySe 10 bnd l <+> text "**" <>
1163 line <> prettySe 10 bnd r <> rparen
1165 lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1166 prettySe p bnd (PAlternative a as) =
1167 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1170 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10 bnd) as
1171 prettySe p bnd PType = text "Type"
1172 prettySe p bnd (PConstant c) = annotate (annot c) (text (show c))
1173 where annot (AType _) = AnnConstType
1174 annot StrType = AnnConstType
1175 annot PtrType = AnnConstType
1176 annot VoidType = AnnConstType
1177 annot _ = AnnConstData
1178 -- XXX: add pretty for tactics
1179 prettySe p bnd (PProof ts) =
1180 text "proof" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1181 prettySe p bnd (PTactics ts) =
1182 text "tactics" <+> lbrace <> nest nestingSize (text . show $ ts) <> rbrace
1183 prettySe p bnd (PMetavar n) = text "?" <> pretty n
1184 prettySe p bnd (PReturn f) = text "return"
1185 prettySe p bnd PImpossible = text "impossible"
1186 prettySe p bnd Placeholder = text "_"
1187 prettySe p bnd (PDoBlock _) = text "do block pretty not implemented"
1188 prettySe p bnd (PElabError s) = pretty s
1190 prettySe p bnd _ = text "test"
1192 prettyArgS bnd (PImp _ _ _ n tm _) = prettyArgSi bnd (n, tm)
1193 prettyArgS bnd (PExp _ _ tm _) = prettyArgSe bnd tm
1194 prettyArgS bnd (PConstraint _ _ tm _) = prettyArgSc bnd tm
1195 prettyArgS bnd (PTacImplicit _ _ n _ tm _) = prettyArgSti bnd (n, tm)
1197 prettyArgSe bnd arg = prettySe 0 bnd arg
1198 prettyArgSi bnd (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1199 prettyArgSc bnd val = lbrace <> lbrace <> prettySe 10 bnd val <> rbrace <> rbrace
1200 prettyArgSti bnd (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 bnd val <> rbrace
1203 slist' p bnd (PApp _ (PRef _ nil) _)
1204 | not impl && nsroot nil == sUN "Nil" = Just []
1205 slist' p bnd (PApp _ (PRef _ cons) args)
1206 | nsroot cons == sUN "::",
1207 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1209 Just tl' <- slist' p bnd tl
1212 isImp (PImp {}) = True
1214 slist' _ _ _ = Nothing
1216 slist p bnd e | Just es <- slist' p bnd e = Just $
1217 case es of [] -> annotate AnnConstData $ text "[]"
1221 xs -> (enclose left right . hsep . punctuate comma . map (prettySe p bnd)) xs
1222 where left = (annotate AnnConstData (text "["))
1223 right = (annotate AnnConstData (text "]"))
1224 comma = (annotate AnnConstData (text ","))
1225 slist _ _ _ = Nothing
1227 natns = "Prelude.Nat."
1230 | show z == (natns++"Z") || show z == "Z" = Just 0
1231 snat p (PApp _ s [PExp {getTm=n}])
1232 | show s == (natns++"S") || show s == "S",
1237 bracket outer inner doc
1238 | inner > outer = lparen <> doc <> rparen
1241-- | Pretty-printer helper for the binding site of a name
1242bindingOf :: Name -- ^^ the bound name
1243 -> Bool -- ^^ whether the name is implicit
1244 -> Doc OutputAnnotation
1245bindingOf n imp = annotate (AnnBoundName n imp) (text (show n))
1247-- | Pretty-printer helper for names that attaches the correct annotations
1248prettyName :: Bool -- ^^ whether to show namespaces
1249 -> [(Name, Bool)] -- ^^ the current bound variables and whether they are implicit
1250 -> Name -- ^^ the name to pprint
1251 -> Doc OutputAnnotation
1252prettyName showNS bnd n | Just imp <- lookup n bnd = annotate (AnnBoundName n imp) (text (strName n))
1253 | otherwise = annotate (AnnName n Nothing Nothing) (text (strName n))
1254 where strName (UN n) = T.unpack n
1255 strName (NS n ns) | showNS = (concatMap (++ ".") . map T.unpack . reverse) ns ++ strName n
1256 | otherwise = strName n
1257 strName (MN i s) = T.unpack s
1258 strName other = show other
1261showCImp :: Bool -> PClause -> Doc OutputAnnotation
1262showCImp impl (PClause _ n l ws r w)
1263 = prettyImp impl l <+> showWs ws <+> text "=" <+> prettyImp impl r
1264 <+> text "where" <+> text (show w)
1267 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1268showCImp impl (PWith _ n l ws r w)
1269 = prettyImp impl l <+> showWs ws <+> text "with" <+> prettyImp impl r
1270 <+> braces (text (show w))
1273 showWs (x : xs) = text "|" <+> prettyImp impl x <+> showWs xs
1276showDImp :: Bool -> PData -> Doc OutputAnnotation
1277showDImp impl (PDatadecl n ty cons)
1278 = text "data" <+> text (show n) <+> colon <+> prettyImp impl ty <+> text "where" <$>
1279 (indent 2 $ vsep (map (\ (_, n, t, _, _) -> pipe <+> prettyName False [] n <+> colon <+> prettyImp impl t) cons))
1281showDecls :: Bool -> [PDecl] -> Doc OutputAnnotation
1282showDecls i ds = vsep (map (showDeclImp i) ds)
1284showDeclImp _ (PFix _ f ops) = text (show f) <+> cat (punctuate (text ",") (map text ops))
1285showDeclImp i (PTy _ _ _ _ n t) = text "tydecl" <+> text (showCG n) <+> colon <+> prettyImp i t
1286showDeclImp i (PClauses _ _ n cs) = text "pat" <+> text (showCG n) <+> text "\t" <+>
1287 indent 2 (vsep (map (showCImp i) cs))
1288showDeclImp _ (PData _ _ _ _ d) = showDImp True d
1289showDeclImp i (PParams _ ns ps) = text "params" <+> braces (text (show ns) <> line <> showDecls i ps <> line)
1290showDeclImp i (PNamespace n ps) = text "namespace" <+> text n <> braces (line <> showDecls i ps <> line)
1291showDeclImp _ (PSyntax _ syn) = text "syntax" <+> text (show syn)
1292showDeclImp i (PClass _ _ _ cs n ps ds)
1293 = text "class" <+> text (show cs) <+> text (show n) <+> text (show ps) <> line <> showDecls i ds
1294showDeclImp i (PInstance _ _ cs n _ t _ ds)
1295 = text "instance" <+> text (show cs) <+> text (show n) <+> prettyImp i t <> line <> showDecls i ds
1296showDeclImp _ _ = text "..."
1297-- showDeclImp (PImport i) = "import " ++ i
1299instance Show (Doc OutputAnnotation) where
1300 show = flip (displayS . renderCompact) ""
1302getImps :: [PArg] -> [(Name, PTerm)]
1304getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
1305getImps (_ : xs) = getImps xs
1307getExps :: [PArg] -> [PTerm]
1309getExps (PExp _ _ tm _ : xs) = tm : getExps xs
1310getExps (_ : xs) = getExps xs
1312getConsts :: [PArg] -> [PTerm]
1314getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
1315getConsts (_ : xs) = getConsts xs
1317getAll :: [PArg] -> [PTerm]
1322showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1323 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1324 -> Bool -- ^^ whether to show implicits
1325 -> Bool -- ^^ whether to colourise
1326 -> Name -- ^^ the term to show
1328showName ist bnd impl colour n = case ist of
1329 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1330 Nothing -> showbasic n
1331 where name = if impl then show n else showbasic n
1332 showbasic n@(UN _) = showCG n
1333 showbasic (MN _ s) = str s
1334 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1335 showbasic (SN s) = show s
1337 colourise n t = let ctxt' = fmap tt_ctxt ist in
1340 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1341 else colouriseBound t name
1342 | isDConName n ctxt -> colouriseData t name
1343 | isFnName n ctxt -> colouriseFun t name
1344 | isTConName n ctxt -> colouriseType t name
1345 -- The assumption is that if a name is not bound and does not exist in the
1346 -- global context, then we're somewhere in which implicit info has been lost
1347 -- (like error messages). Thus, unknown vars are colourised as implicits.
1348 | otherwise -> colouriseImplicit t name
1350showTm :: IState -- ^^ the Idris state, for information about identifiers and colours
1351 -> PTerm -- ^^ the term to show
1353showTm ist = displayDecorated (consoleDecorate ist) . renderCompact . prettyImp (opt_showimp (idris_options ist))
1355-- | Show a term with implicits, no colours
1356showTmImpls :: PTerm -> String
1357showTmImpls = flip (displayS . renderCompact . prettyImp True) ""
1360instance Sized PTerm where
1361 size (PQuote rawTerm) = size rawTerm
1362 size (PRef fc name) = size name
1363 size (PLam name ty bdy) = 1 + size ty + size bdy
1364 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1365 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1366 size (PTyped trm ty) = 1 + size trm + size ty
1367 size (PApp fc name args) = 1 + size args
1368 size (PAppBind fc name args) = 1 + size args
1369 size (PCase fc trm bdy) = 1 + size trm + size bdy
1371 size (PFalse fc) = 1
1372 size (PRefl fc _) = 1
1373 size (PResolveTC fc) = 1
1374 size (PEq fc left right) = 1 + size left + size right
1375 size (PRewrite fc left right _) = 1 + size left + size right
1376 size (PPair fc left right) = 1 + size left + size right
1377 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1378 size (PAlternative a alts) = 1 + size alts
1379 size (PHidden hidden) = size hidden
1380 size (PUnifyLog tm) = size tm
1381 size (PDisamb _ tm) = size tm
1382 size (PNoImplicits tm) = size tm
1384 size (PConstant const) = 1 + size const
1385 size Placeholder = 1
1386 size (PDoBlock dos) = 1 + size dos
1387 size (PIdiom fc term) = 1 + size term
1388 size (PReturn fc) = 1
1389 size (PMetavar name) = 1
1390 size (PProof tactics) = size tactics
1391 size (PElabError err) = size err
1392 size PImpossible = 1
1394getPArity :: PTerm -> Int
1395getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1398-- Return all names, free or globally bound, in the given term.
1400allNamesIn :: PTerm -> [Name]
1401allNamesIn tm = nub $ ni [] tm
1404 | not (n `elem` env) = [n]
1405 ni env (PPatvar _ n) = [n]
1406 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1407 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1408 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1409 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1410 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1411 ni env (PHidden tm) = ni env tm
1412 ni env (PEq _ l r) = ni env l ++ ni env r
1413 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1414 ni env (PTyped l r) = ni env l ++ ni env r
1415 ni env (PPair _ l r) = ni env l ++ ni env r
1416 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1417 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1418 ni env (PAlternative a ls) = concatMap (ni env) ls
1419 ni env (PUnifyLog tm) = ni env tm
1420 ni env (PDisamb _ tm) = ni env tm
1421 ni env (PNoImplicits tm) = ni env tm
1424-- Return all names defined in binders in the given term
1425boundNamesIn :: PTerm -> [Name]
1426boundNamesIn tm = nub $ ni tm
1428 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1429 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1430 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1431 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1432 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1433 ni (PPi _ n ty sc) = n : (ni ty ++ ni sc)
1434 ni (PEq _ l r) = ni l ++ ni r
1435 ni (PRewrite _ l r _) = ni l ++ ni r
1436 ni (PTyped l r) = ni l ++ ni r
1437 ni (PPair _ l r) = ni l ++ ni r
1438 ni (PDPair _ (PRef _ n) t r) = ni t ++ ni r
1439 ni (PDPair _ l t r) = ni l ++ ni t ++ ni r
1440 ni (PAlternative a as) = concatMap (ni) as
1441 ni (PHidden tm) = ni tm
1442 ni (PUnifyLog tm) = ni tm
1443 ni (PDisamb _ tm) = ni tm
1444 ni (PNoImplicits tm) = ni tm
1447-- Return names which are free in the given term.
1448namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1449namesIn uvars ist tm = nub $ ni [] tm
1452 | not (n `elem` env)
1453 = case lookupTy n (tt_ctxt ist) of
1455 _ -> if n `elem` (map fst uvars) then [n] else []
1456 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1457 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1458 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1459 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1460 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1461 ni env (PEq _ l r) = ni env l ++ ni env r
1462 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1463 ni env (PTyped l r) = ni env l ++ ni env r
1464 ni env (PPair _ l r) = ni env l ++ ni env r
1465 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1466 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1467 ni env (PAlternative a as) = concatMap (ni env) as
1468 ni env (PHidden tm) = ni env tm
1469 ni env (PUnifyLog tm) = ni env tm
1470 ni env (PDisamb _ tm) = ni env tm
1471 ni env (PNoImplicits tm) = ni env tm
1474-- Return which of the given names are used in the given term.
1476usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1477usedNamesIn vars ist tm = nub $ ni [] tm
1480 | n `elem` vars && not (n `elem` env)
1481 = case lookupTy n (tt_ctxt ist) of
1484 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1485 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1486 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1487 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1488 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1489 ni env (PEq _ l r) = ni env l ++ ni env r
1490 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1491 ni env (PTyped l r) = ni env l ++ ni env r
1492 ni env (PPair _ l r) = ni env l ++ ni env r
1493 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1494 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1495 ni env (PAlternative a as) = concatMap (ni env) as
1496 ni env (PHidden tm) = ni env tm
1497 ni env (PUnifyLog tm) = ni env tm
1498 ni env (PDisamb _ tm) = ni env tm
1499 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