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
28import qualified Data.Map as M
30import Data.Word (Word)
34data IOption = IOption { opt_logLevel :: Int,
36 opt_typeintype :: Bool,
38 opt_showimp :: Bool, -- ^^ show implicits
39 opt_errContext :: Bool,
44 opt_codegen :: Codegen,
45 opt_outputTy :: OutputType,
46 opt_ibcsubdir :: FilePath,
47 opt_importdirs :: [FilePath],
51 opt_cmdline :: [Opt] -- remember whole command line
55defaultOpts = IOption { opt_logLevel = 0
56 , opt_typecase = False
57 , opt_typeintype = False
60 , opt_errContext = False
63 , opt_nobanner = False
66 , opt_outputTy = Executable
75data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
77-- | The output mode in use
78data OutputMode = RawOutput | IdeSlave Integer deriving Show
80-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
81-- without typechecking).
82-- This will include all the functions and data declarations, plus fixity declarations
85-- | The global state used in the Idris monad
87 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
88 idris_constraints :: [(UConstraint, FC)],
89 -- ^ A list of universe constraints and their corresponding source locations
90 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
91 idris_implicits :: Ctxt [PArg],
92 idris_statics :: Ctxt [Bool],
93 idris_classes :: Ctxt ClassInfo,
94 idris_dsls :: Ctxt DSL,
95 idris_optimisation :: Ctxt OptInfo,
96 idris_datatypes :: Ctxt TypeInfo,
97 idris_namehints :: Ctxt [Name],
98 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
99 -- ^ list of lhs/rhs, and a list of missing clauses
100 idris_flags :: Ctxt [FnOpt],
101 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
102 idris_calledgraph :: Ctxt [Name],
103 idris_docstrings :: Ctxt String,
104 idris_totcheck :: [(FC, Name)], -- names to check totality on
105 idris_defertotcheck :: [(FC, Name)], -- names to check at the end
106 idris_options :: IOption,
108 idris_lineapps :: [((FilePath, Int), PTerm)],
109 -- ^ Full application LHS on source line
110 idris_metavars :: [(Name, (Maybe Name, Int, Bool))], -- ^ The currently defined but not proven metavariables
111 idris_coercions :: [Name],
112 idris_transforms :: [(Term, Term)],
113 idris_errRev :: [(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)),
140 module_aliases :: M.Map [T.Text] [T.Text]
143data SizeChange = Smaller | Same | Bigger | Unknown
146deriving instance Binary SizeChange
147deriving instance NFData SizeChange
150type SCGEntry = (Name, [Maybe (Int, SizeChange)])
152data CGInfo = CGInfo { argsdef :: [Name],
153 calls :: [(Name, [[Name]])],
159deriving instance Binary CGInfo
160deriving instance NFData CGInfo
163primDefs = [sUN "unsafePerformPrimIO",
164 sUN "mkLazyForeignPrim",
168-- information that needs writing for the current module's .ibc file
169data IBCWrite = IBCFix FixDecl
173 | IBCInstance Bool Name Name
180 | IBCObj Codegen FilePath
181 | IBCLib Codegen String
182 | IBCCGFlag Codegen String
184 | IBCHeader Codegen String
185 | IBCAccess Name Accessibility
186 | IBCMetaInformation Name MetaInformation
187 | IBCTotal Name Totality
188 | IBCFlags Name [FnOpt]
189 | IBCTrans (Term, Term)
190 | IBCErrRev (Term, Term)
194 | IBCDef Name -- i.e. main context
195 | IBCNameHint (Name, Name)
196 | IBCLineApp FilePath Int PTerm
199-- | The initial state for the compiler
201idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
202 emptyContext emptyContext emptyContext emptyContext
203 emptyContext emptyContext emptyContext emptyContext
205 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
206 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
207 True defaultTheme stdout [] (0, emptyContext) M.empty
209-- | The monad for the main REPL - reading and processing files and updating
210-- global state (hence the IO inner monad).
211--type Idris = WriterT [Either String (IO ())] (State IState a))
212type Idris = StateT IState (ErrorT Err IO)
214-- Commands in the REPL
233 | ChangeDirectory FilePath
236 | Compile Codegen String
241 | AddProof (Maybe Name)
253 | DynamicLink FilePath
258 | CaseSplitAt Bool Int Name
259 | AddClauseFrom Bool Int Name
260 | AddProofClauseFrom Bool Int Name
261 | AddMissing Bool Int Name
262 | MakeWith Bool Int Name
263 | DoProofSearch Bool Int Name [Name]
267 | SetColour ColourType IdrisColour
272data Opt = Filename String
284 | NoBuiltins -- only for the really primitive stuff!
309 | OutputTy OutputType
310 | Extension LanguageExt
311 | InterpretScript String
312 | TargetTriple String
318-- Parsed declarations
320data Fixity = Infixl { prec :: Int }
321 | Infixr { prec :: Int }
322 | InfixN { prec :: Int }
323 | PrefixN { prec :: Int }
326deriving instance Binary Fixity
327deriving instance NFData Fixity
330instance Show Fixity where
331 show (Infixl i) = "infixl " ++ show i
332 show (Infixr i) = "infixr " ++ show i
333 show (InfixN i) = "infix " ++ show i
334 show (PrefixN i) = "prefix " ++ show i
336data FixDecl = Fix Fixity String
339instance Show FixDecl where
340 show (Fix f s) = show f ++ " " ++ s
343deriving instance Binary FixDecl
344deriving instance NFData FixDecl
347instance Ord FixDecl where
348 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
351data Static = Static | Dynamic
354deriving instance Binary Static
355deriving instance NFData Static
358-- Mark bindings with their explicitness, and laziness
359data Plicity = Imp { pargopts :: [ArgOpt],
363 | Exp { pargopts :: [ArgOpt],
367 | Constraint { pargopts :: [ArgOpt],
370 | TacImp { pargopts :: [ArgOpt],
376plazy :: Plicity -> Bool
377plazy tm = Lazy `elem` pargopts tm
380deriving instance Binary Plicity
381deriving instance NFData Plicity
384impl = Imp [Lazy] Dynamic "" False
385expl = Exp [] Dynamic "" False
386expl_param = Exp [] Dynamic "" True
387constraint = Constraint [] Dynamic ""
388tacimpl t = TacImp [] Dynamic t ""
390data FnOpt = Inlinable -- always evaluate when simplifying
391 | TotalFn | PartialFn
392 | Coinductive | AssertTotal
393 | Dictionary -- type class dictionary, eval only when
394 -- a function argument, and further evaluation resutls
395 | Implicit -- implicit coercion
396 | CExport String -- export, with a C name
397 | ErrorHandler -- ^^ an error handler for use with the ErrorReflection extension
398 | ErrorReverse -- ^^ attempt to reverse normalise before showing in error
399 | Reflection -- a reflecting function, compile-time only
400 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
403deriving instance Binary FnOpt
404deriving instance NFData FnOpt
409inlinable :: FnOpts -> Bool
410inlinable = elem Inlinable
412dictionary :: FnOpts -> Bool
413dictionary = elem Dictionary
416-- | Data declaration options
417data DataOpt = Codata -- Set if the the data-type is coinductive
418 | DefaultEliminator -- Set if an eliminator should be generated for data type
422type DataOpts = [DataOpt]
424-- | Top-level declarations such as compiler directives, definitions,
425-- datatypes and typeclasses.
427 = PFix FC Fixity [String] -- ^ Fixity declaration
428 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
429 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
430 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
431 | PCAF FC Name t -- ^ Top level constant
432 | PData String SyntaxInfo FC DataOpts (PData' t) -- ^ Data declaration.
433 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
434 | PNamespace String [PDecl' t] -- ^ New namespace
435 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
436 | PClass String SyntaxInfo FC
439 [(Name, t)] -- parameters
440 [PDecl' t] -- declarations
441 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
442 -- class name, parameters, method declarations
443 | PInstance SyntaxInfo FC [t] -- constraints
446 t -- full instance type
447 (Maybe Name) -- explicit name
449 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
450 -- class name, parameters, full instance type, optional explicit name, and definitions
451 | PDSL Name (DSL' t) -- ^ DSL declaration
452 | PSyntax FC Syntax -- ^ Syntax definition
453 | PMutual FC [PDecl' t] -- ^ Mutual block
454 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
455 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
456 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
457 -- bool is True, lhs and rhs must be convertible
460deriving instance Binary PDecl'
461deriving instance NFData PDecl'
464-- For elaborator state
465type ElabD a = Elab' [PDecl] a
467-- | One clause of a top-level definition. Term arguments to constructors are:
469-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
471-- 2. The list of extra 'with' patterns
473-- 3. The right-hand side
475-- 4. The where block (PDecl' t)
477data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
478 | PWith FC Name t [t] t [PDecl' t]
479 | PClauseR FC [t] t [PDecl' t]
480 | PWithR FC [t] t [PDecl' t]
483deriving instance Binary PClause'
484deriving instance NFData PClause'
488data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
489 d_tcon :: t, -- ^ Type constructor
490 d_cons :: [(String, Name, t, FC, [Name])] -- ^ Constructors
492 -- ^ Data declaration
493 | PLaterdecl { d_name :: Name, d_tcon :: t }
494 -- ^ "Placeholder" for data whose constructors are defined later
497deriving instance Binary PData'
498deriving instance NFData PData'
501-- Handy to get a free function for applying PTerm -> PTerm functions
502-- across a program, by deriving Functor
504type PDecl = PDecl' PTerm
505type PData = PData' PTerm
506type PClause = PClause' PTerm
508-- get all the names declared in a decl
510declared :: PDecl -> [Name]
511declared (PFix _ _ _) = []
512declared (PTy _ _ _ _ n t) = [n]
513declared (PPostulate _ _ _ _ n t) = [n]
514declared (PClauses _ _ n _) = [] -- not a declaration
515declared (PCAF _ n _) = [n]
516declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
517 where fstt (_, a, _, _, _) = a
518declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
519declared (PParams _ _ ds) = concatMap declared ds
520declared (PNamespace _ ds) = concatMap declared ds
521declared (PRecord _ _ _ n _ _ c _) = [n, c]
522declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
523declared (PInstance _ _ _ _ _ _ _ _) = []
524declared (PDSL n _) = [n]
525declared (PSyntax _ _) = []
526declared (PMutual _ ds) = concatMap declared ds
527declared (PDirective _) = []
529-- get the names declared, not counting nested parameter blocks
530tldeclared :: PDecl -> [Name]
531tldeclared (PFix _ _ _) = []
532tldeclared (PTy _ _ _ _ n t) = [n]
533tldeclared (PPostulate _ _ _ _ n t) = [n]
534tldeclared (PClauses _ _ n _) = [] -- not a declaration
535tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
536tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
537 where fstt (_, a, _, _, _) = a
538tldeclared (PParams _ _ ds) = []
539tldeclared (PMutual _ ds) = concatMap tldeclared ds
540tldeclared (PNamespace _ ds) = concatMap tldeclared ds
541tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
542tldeclared (PInstance _ _ _ _ _ _ _ _) = []
545defined :: PDecl -> [Name]
546defined (PFix _ _ _) = []
547defined (PTy _ _ _ _ n t) = []
548defined (PPostulate _ _ _ _ n t) = []
549defined (PClauses _ _ n _) = [n] -- not a declaration
550defined (PCAF _ n _) = [n]
551defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
552 where fstt (_, a, _, _, _) = a
553defined (PData _ _ _ _ (PLaterdecl n _)) = []
554defined (PParams _ _ ds) = concatMap defined ds
555defined (PNamespace _ ds) = concatMap defined ds
556defined (PRecord _ _ _ n _ _ c _) = [n, c]
557defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
558defined (PInstance _ _ _ _ _ _ _ _) = []
559defined (PDSL n _) = [n]
560defined (PSyntax _ _) = []
561defined (PMutual _ ds) = concatMap defined ds
562defined (PDirective _) = []
565updateN :: [(Name, Name)] -> Name -> Name
566updateN ns n | Just n' <- lookup n ns = n'
569updateNs :: [(Name, Name)] -> PTerm -> PTerm
571updateNs ns t = mapPT updateRef t
572 where updateRef (PRef fc f) = PRef fc (updateN ns f)
575-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
577-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
578-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
579-- where updateCNs ns (PClause n l ts r ds)
580-- = PClause (updateN ns n) (fmap (updateNs ns) l)
581-- (map (fmap (updateNs ns)) ts)
582-- (fmap (updateNs ns) r)
583-- (map (updateDNs ns) ds)
586-- | High level language terms
587data PTerm = PQuote Raw
589 | PInferRef FC Name -- ^ A name to be defined later
591 | PLam Name PTerm PTerm
592 | PPi Plicity Name PTerm PTerm
593 | PLet Name PTerm PTerm PTerm
594 | PTyped PTerm PTerm -- ^ Term with explicit type
595 | PApp FC PTerm [PArg]
596 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
597 | PMatchApp FC Name -- ^ Make an application by type matching
598 | PCase FC PTerm [(PTerm, PTerm)]
604 | PRewrite FC PTerm PTerm (Maybe PTerm)
605 | PPair FC PTerm PTerm
606 | PDPair FC PTerm PTerm PTerm
607 | PAlternative Bool [PTerm] -- True if only one may work
608 | PHidden PTerm -- ^ Irrelevant or hidden pattern
610 | PGoal FC PTerm Name PTerm
617 | PProof [PTactic] -- ^ Proof script
618 | PTactics [PTactic] -- ^ As PProof, but no auto solving
619 | PElabError Err -- ^ Error to report on elaboration
620 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
621 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
622 | PDisamb [[T.Text]] PTerm -- ^ Preferences for explicit namespaces
623 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
624 | PNoImplicits PTerm -- ^ never run implicit converions on the term
629deriving instance Binary PTerm
630deriving instance NFData PTerm
633mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
634mapPT f t = f (mpt t) where
635 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
636 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
637 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
638 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
640 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
641 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
642 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
643 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
644 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
645 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
646 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
647 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
648 mpt (PHidden t) = PHidden (mapPT f t)
649 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
650 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
651 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
652 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
653 mpt (PDisamb ns tm) = PDisamb ns (mapPT f tm)
654 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
655 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
659data PTactic' t = Intro [Name] | Intros | Focus Name
660 | Refine Name [Bool] | Rewrite t
664 | LetTac Name t | LetTacTy Name t t
665 | Exact t | Compute | Trivial
666 | ProofSearch (Maybe Name) Name [Name]
669 | ProofState | ProofTerm | Undo
670 | Try (PTactic' t) (PTactic' t)
671 | TSeq (PTactic' t) (PTactic' t)
672 | ApplyTactic t -- see Language.Reflection module
676 | GoalType String (PTactic' t)
678 deriving (Show, Eq, Functor)
680deriving instance Binary PTactic'
681deriving instance NFData PTactic'
684instance Sized a => Sized (PTactic' a) where
685 size (Intro nms) = 1 + size nms
687 size (Focus nm) = 1 + size nm
688 size (Refine nm bs) = 1 + size nm + length bs
689 size (Rewrite t) = 1 + size t
690 size (Induction t) = 1 + size t
691 size (LetTac nm t) = 1 + size nm + size t
692 size (Exact t) = 1 + size t
700 size (Try l r) = 1 + size l + size r
701 size (TSeq l r) = 1 + size l + size r
702 size (ApplyTactic t) = 1 + size t
703 size (Reflect t) = 1 + size t
704 size (Fill t) = 1 + size t
708type PTactic = PTactic' PTerm
710data PDo' t = DoExp FC t
715 deriving (Eq, Functor)
717deriving instance Binary PDo'
718deriving instance NFData PDo'
721instance Sized a => Sized (PDo' a) where
722 size (DoExp fc t) = 1 + size fc + size t
723 size (DoBind fc nm t) = 1 + size fc + size nm + size t
724 size (DoBindP fc l r) = 1 + size fc + size l + size r
725 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
726 size (DoLetP fc l r) = 1 + size fc + size l + size r
730-- The priority gives a hint as to elaboration order. Best to elaborate
731-- things early which will help give a more concrete type to other
732-- variables, e.g. a before (interpTy a).
734data PArg' t = PImp { priority :: Int,
735 machine_inf :: Bool, -- true if the machine inferred it
737 pname :: Name, getTm :: t,
739 | PExp { priority :: Int,
743 | PConstraint { priority :: Int,
747 | PTacImplicit { priority :: Int,
753 deriving (Show, Eq, Functor)
755data ArgOpt = Lazy | HideDisplay
758lazyarg :: PArg' t -> Bool
759lazyarg tm = Lazy `elem` argopts tm
761instance Sized a => Sized (PArg' a) where
762 size (PImp p _ l nm trm _) = 1 + size nm + size trm
763 size (PExp p l trm _) = 1 + size trm
764 size (PConstraint p l trm _) = 1 + size trm
765 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
768deriving instance Binary PArg'
769deriving instance NFData PArg'
772pimp n t mach = PImp 1 mach [Lazy] n t ""
773pexp t = PExp 1 [] t ""
774pconst t = PConstraint 1 [] t ""
775ptacimp n s t = PTacImplicit 2 [Lazy] n s t ""
777type PArg = PArg' PTerm
781data ClassInfo = CI { instanceName :: Name,
782 class_methods :: [(Name, (FnOpts, PTerm))],
783 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
784 class_default_superclasses :: [PDecl],
785 class_params :: [Name],
786 class_instances :: [Name] }
789deriving instance Binary ClassInfo
790deriving instance NFData ClassInfo
793-- An argument is conditionally forceable iff its forceability
794-- depends on the collapsibility of the whole type.
795data Forceability = Conditional | Unconditional deriving (Show, Enum, Bounded, Eq, Ord)
798deriving instance Binary Forceability
799deriving instance NFData Forceability
802data OptInfo = Optimise { collapsible :: Bool,
804 -- The following should actually be (IntMap Forceability)
805 -- but the corresponding Binary instance seems to be broken.
806 -- Let's store a list and convert it to IntMap whenever needed.
807 forceable :: [(Int, Forceability)],
811deriving instance Binary OptInfo
812deriving instance NFData OptInfo
816data TypeInfo = TI { con_names :: [Name],
818 data_opts :: DataOpts,
822deriving instance Binary TypeInfo
823deriving instance NFData TypeInfo
826-- Syntactic sugar info
828data DSL' t = DSL { dsl_bind :: t,
833 index_first :: Maybe t,
834 index_next :: Maybe t,
835 dsl_lambda :: Maybe t,
838 deriving (Show, Functor)
840deriving instance Binary DSL'
841deriving instance NFData DSL'
846data SynContext = PatternSyntax | TermSyntax | AnySyntax
849deriving instance Binary SynContext
850deriving instance NFData SynContext
853data Syntax = Rule [SSymbol] PTerm SynContext
856deriving instance Binary Syntax
857deriving instance NFData Syntax
860data SSymbol = Keyword Name
867deriving instance Binary SSymbol
868deriving instance NFData SSymbol
871initDSL = DSL (PRef f (sUN ">>="))
872 (PRef f (sUN "return"))
874 (PRef f (sUN "pure"))
880 where f = fileFC "(builtin)"
882data Using = UImplicit Name PTerm
883 | UConstraint Name [Name]
886deriving instance Binary Using
887deriving instance NFData Using
890data SyntaxInfo = Syn { using :: [Using],
891 syn_params :: [(Name, PTerm)],
892 syn_namespace :: [String],
894 decoration :: Name -> Name,
896 implicitAllowed :: Bool,
900deriving instance NFData SyntaxInfo
901deriving instance Binary SyntaxInfo
904defaultSyntax = Syn [] [] [] [] id False False initDSL
906expandNS :: SyntaxInfo -> Name -> Name
907expandNS syn n@(NS _ _) = n
908expandNS syn n = case syn_namespace syn of
913--- Pretty printing declarations and terms
915instance Show PTerm where
916 show tm = showImp Nothing False False tm
918instance Pretty PTerm where
919 pretty = prettyImp False
921instance Show PDecl where
922 show d = showDeclImp False d
924instance Show PClause where
925 show c = showCImp True c
927instance Show PData where
928 show d = showDImp False d
930showDecls :: Bool -> [PDecl] -> String
932showDecls i (d:ds) = showDeclImp i d ++ "\n" ++ showDecls i ds
934showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
935showDeclImp i (PTy _ _ _ _ n t) = "tydecl " ++ showCG n ++ " : " ++ showImp Nothing i False t
936showDeclImp i (PClauses _ _ n cs) = "pat " ++ showCG n ++ "\t" ++ showSep "\n\t" (map (showCImp i) cs)
937showDeclImp _ (PData _ _ _ _ d) = showDImp True d
938showDeclImp i (PParams _ ns ps) = "params {" ++ show ns ++ "\n" ++ showDecls i ps ++ "}\n"
939showDeclImp i (PNamespace n ps) = "namespace {" ++ n ++ "\n" ++ showDecls i ps ++ "}\n"
940showDeclImp _ (PSyntax _ syn) = "syntax " ++ show syn
941showDeclImp i (PClass _ _ _ cs n ps ds)
942 = "class " ++ show cs ++ " " ++ show n ++ " " ++ show ps ++ "\n" ++ showDecls i ds
943showDeclImp i (PInstance _ _ cs n _ t _ ds)
944 = "instance " ++ show cs ++ " " ++ show n ++ " " ++ show t ++ "\n" ++ showDecls i ds
945showDeclImp _ _ = "..."
946-- showDeclImp (PImport i) = "import " ++ i
949showCImp :: Bool -> PClause -> String
950showCImp impl (PClause _ n l ws r w)
951 = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
952 ++ " where " ++ show w
955 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
956showCImp impl (PWith _ n l ws r w)
957 = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
958 ++ " { " ++ show w ++ " } "
961 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
964showDImp :: Bool -> PData -> String
965showDImp impl (PDatadecl n ty cons)
966 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
968 (map (\ (_, n, t, _, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
970getImps :: [PArg] -> [(Name, PTerm)]
972getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
973getImps (_ : xs) = getImps xs
975getExps :: [PArg] -> [PTerm]
977getExps (PExp _ _ tm _ : xs) = tm : getExps xs
978getExps (_ : xs) = getExps xs
980getConsts :: [PArg] -> [PTerm]
982getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
983getConsts (_ : xs) = getConsts xs
985getAll :: [PArg] -> [PTerm]
988-- | Pretty-print a high-level Idris term
989prettyImp :: Bool -- ^^ whether to show implicits
990 -> PTerm -- ^^ the term to pretty-print
992prettyImp impl = prettySe 10
994 prettySe p (PQuote r) =
995 if size r > breakingSize then
996 text "![" $$ pretty r <> text "]"
998 text "![" <> pretty r <> text "]"
999 prettySe p (PPatvar fc n) = pretty n
1000 prettySe p (PRef fc n) =
1006 prettyBasic n@(UN _) = pretty n
1007 prettyBasic (MN _ s) = text (str s)
1008 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map (text.str) $ reverse s))) <> prettyBasic n
1009 prettyBasic (SN sn) = text (show sn)
1010 prettySe p (PLam n ty sc) =
1012 if size sc > breakingSize then
1013 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
1015 text "λ" <> pretty n <+> text "=>" <+> pretty sc
1016 prettySe p (PLet n ty v sc) =
1018 if size sc > breakingSize then
1019 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
1020 nest nestingSize (prettySe 10 sc)
1022 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
1024 prettySe p (PPi (Exp l s _ _) n ty sc)
1025 | n `elem` allNamesIn sc || impl =
1026 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1028 if size sc > breakingSize then
1029 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
1030 st <+> text "->" $+$ prettySe 10 sc
1032 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
1033 st <+> text "->" <+> prettySe 10 sc
1036 if size sc > breakingSize then
1037 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
1039 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
1043 Static -> text "[static]"
1045 prettySe p (PPi (Imp l s _ _) n ty sc)
1047 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1049 if size sc > breakingSize then
1050 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
1051 st <+> text "->" <+> prettySe 10 sc
1053 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
1054 st <+> text "->" <+> prettySe 10 sc
1055 | otherwise = prettySe 10 sc
1059 Static -> text $ "[static]"
1061 prettySe p (PPi (Constraint _ _ _) n ty sc) =
1063 if size sc > breakingSize then
1064 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
1066 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
1067 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
1069 if size sc > breakingSize then
1070 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
1071 rbrace <+> text "->" $+$ prettySe 10 sc
1073 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
1074 rbrace <+> text "->" <+> prettySe 10 sc
1075 prettySe p (PApp _ (PRef _ f) [])
1076 | not impl = pretty f
1077 prettySe p (PAppBind _ (PRef _ f) [])
1078 | not impl = text "!" <+> pretty f
1079 prettySe p (PApp _ (PRef _ op@(UN nm)) args)
1081 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
1082 let [l, r] = getExps args in
1084 if size r > breakingSize then
1085 prettySe 1 l <+> pretty op $+$ prettySe 0 r
1087 prettySe 1 l <+> pretty op <+> prettySe 0 r
1088 prettySe p (PApp _ f as) =
1089 let args = getExps as in
1094 -- foldr (<+>) empty $ map prettyArgS as
1096 foldl' fSe empty args
1097 -- foldr (<+>) empty $ map prettyArgSe args
1100 if size r > breakingSize then
1101 l $+$ nest nestingSize (prettyArgS r)
1106 if size r > breakingSize then
1107 l $+$ nest nestingSize (prettyArgSe r)
1110 prettySe p (PCase _ scr opts) =
1111 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
1113 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
1115 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
1116 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
1117 prettySe p (PRefl _ _) = text "refl"
1118 prettySe p (PResolveTC _) = text "resolvetc"
1119 prettySe p (PTrue _) = text "()"
1120 prettySe p (PFalse _) = text "_|_"
1121 prettySe p (PEq _ l r) =
1123 if size r > breakingSize then
1124 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
1126 prettySe 10 l <+> text "=" <+> prettySe 10 r
1127 prettySe p (PRewrite _ l r _) =
1129 if size r > breakingSize then
1130 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
1132 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1133 prettySe p (PTyped l r) =
1134 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1135 prettySe p (PPair _ l r) =
1136 if size r > breakingSize then
1137 lparen <> prettySe 10 l <> text "," $+$
1138 prettySe 10 r <> rparen
1140 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
1141 prettySe p (PDPair _ l t r) =
1142 if size r > breakingSize then
1143 lparen <> prettySe 10 l <+> text "**" $+$
1144 prettySe 10 r <> rparen
1146 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1147 prettySe p (PAlternative a as) =
1148 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1151 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1152 prettySe p PType = text "Type"
1153 prettySe p (PConstant c) = pretty c
1154 -- XXX: add pretty for tactics
1155 prettySe p (PProof ts) =
1156 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1157 prettySe p (PTactics ts) =
1158 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1159 prettySe p (PMetavar n) = text "?" <> pretty n
1160 prettySe p (PReturn f) = text "return"
1161 prettySe p PImpossible = text "impossible"
1162 prettySe p Placeholder = text "_"
1163 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1164 prettySe p (PElabError s) = pretty s
1166 prettySe p _ = text "test"
1168 prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1169 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1170 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1171 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1173 prettyArgSe arg = prettySe 0 arg
1174 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1175 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1176 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1178 bracket outer inner doc
1179 | inner > outer = lparen <> doc <> rparen
1183showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1184 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1185 -> Bool -- ^^ whether to show implicits
1186 -> Bool -- ^^ whether to colourise
1187 -> Name -- ^^ the term to show
1189showName ist bnd impl colour n = case ist of
1190 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1191 Nothing -> showbasic n
1192 where name = if impl then show n else showbasic n
1193 showbasic n@(UN _) = showCG n
1194 showbasic (MN _ s) = str s
1195 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
1196 showbasic (SN s) = show s
1198 colourise n t = let ctxt' = fmap tt_ctxt ist in
1201 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1202 else colouriseBound t name
1203 | isDConName n ctxt -> colouriseData t name
1204 | isFnName n ctxt -> colouriseFun t name
1205 | isTConName n ctxt -> colouriseType t name
1206 -- The assumption is that if a name is not bound and does not exist in the
1207 -- global context, then we're somewhere in which implicit info has been lost
1208 -- (like error messages). Thus, unknown vars are colourised as implicits.
1209 | otherwise -> colouriseImplicit t name
1212showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1213 -> Bool -- ^^ whether to show implicits
1214 -> Bool -- ^^ whether to colourise
1215 -> PTerm -- ^^ the term to show
1217showImp ist impl colour tm = se 10 [] tm where
1218 perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1219 perhapsColourise col str = case ist of
1220 Just i -> if colour then col (idris_colourTheme i) str else str
1223 se :: Int -> [(Name, Bool)] -> PTerm -> String
1224 se p bnd (PQuote r) = "![" ++ show r ++ "]"
1225 se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1226 se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1228 | Just str <- slist p bnd e = str
1229 | Just num <- snat p e = perhapsColourise colouriseData (show num)
1230 se p bnd (PRef fc n) = showName ist bnd impl colour n
1231 se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1232 (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1233 ++ se 10 ((n, False):bnd) sc
1234 se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1235 " = " ++ se 10 bnd v ++
1236 " in " ++ se 10 ((n, False):bnd) sc
1237 se p bnd (PPi (Exp l s _ param) n ty sc)
1238 | n `elem` allNamesIn sc || impl
1240 (if Lazy `elem` l then "|(" else "(") ++
1241 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1243 (if (impl && param) then "P" else "") ++
1245 "-> " ++ se 10 ((n, False):bnd) sc
1246 | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1247 where st = case s of
1248 Static -> "[static] "
1250 se p bnd (PPi (Imp l s _ _) n ty sc)
1251 | impl = bracket p 2 $ (if Lazy `elem` l then "|{" else "{") ++
1252 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1253 "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1254 | otherwise = se 10 ((n, True):bnd) sc
1255 where st = case s of
1256 Static -> "[static] "
1258 se p bnd (PPi (Constraint _ _ _) n ty sc)
1259 = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1260 se p bnd (PPi (TacImp _ _ s _) n ty sc)
1262 "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1263 se 10 ((n, False):bnd) sc
1264 se p bnd (PMatchApp _ f) = "match " ++ show f
1265 se p bnd (PApp _ hd@(PRef fc f) [tm])
1266 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1267 f == sUN "Symbol_" = "'" ++ se 10 bnd (PRef fc (sUN str))
1268 se p bnd (PApp _ hd@(PRef _ f) [])
1269 | not impl = se p bnd hd
1270 se p bnd (PAppBind _ hd@(PRef _ f) [])
1271 | not impl = "!" ++ se p bnd hd
1272 se p bnd (PApp _ op@(PRef _ (UN nm)) args)
1274 length (getExps args) == 2 && not impl && not (isAlpha (thead nm))
1275 = let [l, r] = getExps args in
1276 bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1277 se p bnd (PApp _ f as)
1278 = -- let args = getExps as in
1279 bracket p 1 $ se 1 bnd f ++
1280 if impl then concatMap (sArg bnd) as
1281 else concatMap (suiArg impl bnd) as
1282 se p bnd (PAppBind _ f as)
1283 = let args = getExps as in
1284 "!" ++ (bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1285 else concatMap (seArg bnd) args)
1286 se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1287 where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1288 se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1289 se p bnd (PRefl _ t)
1290 | not impl = perhapsColourise colouriseData "refl"
1291 | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1292 se p bnd (PResolveTC _) = "resolvetc"
1293 se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1294 se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1295 se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1296 se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1297 se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1298 se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1299 se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1300 se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1301 se p bnd PType = perhapsColourise colouriseType "Type"
1302 se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1303 where cfun (AType _) = colouriseType
1304 cfun StrType = colouriseType
1305 cfun PtrType = colouriseType
1306 cfun VoidType = colouriseType
1307 cfun _ = colouriseData
1308 se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1309 se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1310 se p bnd (PMetavar n) = "?" ++ show n
1311 se p bnd (PReturn f) = "return"
1312 se p bnd PImpossible = "impossible"
1313 se p bnd Placeholder = "_"
1314 se p bnd (PDoBlock _) = "do block show not implemented"
1315 se p bnd (PElabError s) = show s
1316 se p bnd (PCoerced t) = se p bnd t
1317 se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1318 se p bnd (PDisamb ns t) = "%disamb " ++ show ns ++ se p bnd t
1319 se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1320-- se p bnd x = "Not implemented"
1322 slist' p bnd (PApp _ (PRef _ nil) _)
1323 | not impl && nsroot nil == sUN "Nil" = Just []
1324 slist' p bnd (PApp _ (PRef _ cons) args)
1325 | nsroot cons == sUN "::",
1326 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1328 Just tl' <- slist' p bnd tl
1331 isImp (PImp {}) = True
1333 slist' _ _ _ = Nothing
1335 slist p bnd e | Just es <- slist' p bnd e = Just $
1336 case es of [] -> "[]"
1337 [x] -> "[" ++ se p bnd x ++ "]"
1338 xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1339 slist _ _ _ = Nothing
1341 -- since Prelude is always imported, S & Z are unqualified iff they're the
1344 | show o == (natns++"Z") || show o == "Z" = Just 0
1345 snat p (PApp _ s [PExp {getTm=n}])
1346 | show s == (natns++"S") || show s == "S",
1351 natns = "Prelude.Nat."
1353 sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1354 sArg bnd (PExp _ _ tm _) = seArg bnd tm
1355 sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1356 sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1358 -- show argument, implicits given by the user also shown
1359 suiArg impl bnd (PImp _ mi _ n tm _)
1360 | impl || not mi = siArg bnd (n, tm)
1361 suiArg impl bnd (PExp _ _ tm _) = seArg bnd tm
1362 suiArg impl bnd _ = ""
1364 seArg bnd arg = " " ++ se 0 bnd arg
1365 siArg bnd (n, val) =
1367 val' = se 10 bnd val in
1369 then " {" ++ n' ++ "}"
1370 else " {" ++ n' ++ " = " ++ val' ++ "}"
1371 scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1372 stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1374 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1378instance Sized PTerm where
1379 size (PQuote rawTerm) = size rawTerm
1380 size (PRef fc name) = size name
1381 size (PLam name ty bdy) = 1 + size ty + size bdy
1382 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1383 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1384 size (PTyped trm ty) = 1 + size trm + size ty
1385 size (PApp fc name args) = 1 + size args
1386 size (PAppBind fc name args) = 1 + size args
1387 size (PCase fc trm bdy) = 1 + size trm + size bdy
1389 size (PFalse fc) = 1
1390 size (PRefl fc _) = 1
1391 size (PResolveTC fc) = 1
1392 size (PEq fc left right) = 1 + size left + size right
1393 size (PRewrite fc left right _) = 1 + size left + size right
1394 size (PPair fc left right) = 1 + size left + size right
1395 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1396 size (PAlternative a alts) = 1 + size alts
1397 size (PHidden hidden) = size hidden
1398 size (PUnifyLog tm) = size tm
1399 size (PDisamb _ tm) = size tm
1400 size (PNoImplicits tm) = size tm
1402 size (PConstant const) = 1 + size const
1403 size Placeholder = 1
1404 size (PDoBlock dos) = 1 + size dos
1405 size (PIdiom fc term) = 1 + size term
1406 size (PReturn fc) = 1
1407 size (PMetavar name) = 1
1408 size (PProof tactics) = size tactics
1409 size (PElabError err) = size err
1410 size PImpossible = 1
1412getPArity :: PTerm -> Int
1413getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1416-- Return all names, free or globally bound, in the given term.
1418allNamesIn :: PTerm -> [Name]
1419allNamesIn tm = nub $ ni [] tm
1422 | not (n `elem` env) = [n]
1423 ni env (PPatvar _ n) = [n]
1424 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1425 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1426 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1427 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1428 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1429 ni env (PHidden tm) = ni env tm
1430 ni env (PEq _ l r) = ni env l ++ ni env r
1431 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1432 ni env (PTyped l r) = ni env l ++ ni env r
1433 ni env (PPair _ l r) = ni env l ++ ni env r
1434 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1435 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1436 ni env (PAlternative a ls) = concatMap (ni env) ls
1437 ni env (PUnifyLog tm) = ni env tm
1438 ni env (PDisamb _ tm) = ni env tm
1439 ni env (PNoImplicits tm) = ni env tm
1442-- Return all names defined in binders in the given term
1443boundNamesIn :: PTerm -> [Name]
1444boundNamesIn tm = nub $ ni tm
1446 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1447 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1448 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1449 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1450 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1451 ni (PPi _ n ty sc) = n : (ni ty ++ ni sc)
1452 ni (PEq _ l r) = ni l ++ ni r
1453 ni (PRewrite _ l r _) = ni l ++ ni r
1454 ni (PTyped l r) = ni l ++ ni r
1455 ni (PPair _ l r) = ni l ++ ni r
1456 ni (PDPair _ (PRef _ n) t r) = ni t ++ ni r
1457 ni (PDPair _ l t r) = ni l ++ ni t ++ ni r
1458 ni (PAlternative a as) = concatMap (ni) as
1459 ni (PHidden tm) = ni tm
1460 ni (PUnifyLog tm) = ni tm
1461 ni (PDisamb _ tm) = ni tm
1462 ni (PNoImplicits tm) = ni tm
1465-- Return names which are free in the given term.
1466namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1467namesIn uvars ist tm = nub $ ni [] tm
1470 | not (n `elem` env)
1471 = case lookupTy n (tt_ctxt ist) of
1473 _ -> if n `elem` (map fst uvars) then [n] else []
1474 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1475 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1476 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1477 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1478 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1479 ni env (PEq _ l r) = ni env l ++ ni env r
1480 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1481 ni env (PTyped l r) = ni env l ++ ni env r
1482 ni env (PPair _ l r) = ni env l ++ ni env r
1483 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1484 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1485 ni env (PAlternative a as) = concatMap (ni env) as
1486 ni env (PHidden tm) = ni env tm
1487 ni env (PUnifyLog tm) = ni env tm
1488 ni env (PDisamb _ tm) = ni env tm
1489 ni env (PNoImplicits tm) = ni env tm
1492-- Return which of the given names are used in the given term.
1494usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1495usedNamesIn vars ist tm = nub $ ni [] tm
1498 | n `elem` vars && not (n `elem` env)
1499 = case lookupTy n (tt_ctxt ist) of
1502 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1503 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1504 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1505 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1506 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1507 ni env (PEq _ l r) = ni env l ++ ni env r
1508 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1509 ni env (PTyped l r) = ni env l ++ ni env r
1510 ni env (PPair _ l r) = ni env l ++ ni env r
1511 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1512 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1513 ni env (PAlternative a as) = concatMap (ni env) as
1514 ni env (PHidden tm) = ni env tm
1515 ni env (PUnifyLog tm) = ni env tm
1516 ni env (PDisamb _ tm) = ni env tm
1517 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