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--- Pretty printing declarations and terms
897instance Show PTerm where
898 show tm = showImp Nothing False False tm
901instance Show PDecl where
902 show d = showDeclImp False d
904instance Show PClause where
905 show c = showCImp True c
907instance Show PData where
908 show d = showDImp False d
910showDecls :: Bool -> [PDecl] -> String
912showDecls i (d:ds) = showDeclImp i d ++ "\n" ++ showDecls i ds
914showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
915showDeclImp i (PTy _ _ _ _ n t) = "tydecl " ++ showCG n ++ " : " ++ showImp Nothing i False t
916showDeclImp i (PClauses _ _ n cs) = "pat " ++ showCG n ++ "\t" ++ showSep "\n\t" (map (showCImp i) cs)
917showDeclImp _ (PData _ _ _ _ d) = showDImp True d
918showDeclImp i (PParams _ ns ps) = "params {" ++ show ns ++ "\n" ++ showDecls i ps ++ "}\n"
919showDeclImp i (PNamespace n ps) = "namespace {" ++ n ++ "\n" ++ showDecls i ps ++ "}\n"
920showDeclImp _ (PSyntax _ syn) = "syntax " ++ show syn
921showDeclImp i (PClass _ _ _ cs n ps ds)
922 = "class " ++ show cs ++ " " ++ show n ++ " " ++ show ps ++ "\n" ++ showDecls i ds
923showDeclImp i (PInstance _ _ cs n _ t _ ds)
924 = "instance " ++ show cs ++ " " ++ show n ++ " " ++ show t ++ "\n" ++ showDecls i ds
925showDeclImp _ _ = "..."
926-- showDeclImp (PImport i) = "import " ++ i
929showCImp :: Bool -> PClause -> String
930showCImp impl (PClause _ n l ws r w)
931 = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
932 ++ " where " ++ show w
935 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
936showCImp impl (PWith _ n l ws r w)
937 = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
938 ++ " { " ++ show w ++ " } "
941 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
944showDImp :: Bool -> PData -> String
945showDImp impl (PDatadecl n ty cons)
946 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
948 (map (\ (_, n, t, _, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
950getImps :: [PArg] -> [(Name, PTerm)]
952getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
953getImps (_ : xs) = getImps xs
955getExps :: [PArg] -> [PTerm]
957getExps (PExp _ _ tm _ : xs) = tm : getExps xs
958getExps (_ : xs) = getExps xs
960getConsts :: [PArg] -> [PTerm]
962getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
963getConsts (_ : xs) = getConsts xs
965getAll :: [PArg] -> [PTerm]
970showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
971 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
972 -> Bool -- ^^ whether to show implicits
973 -> Bool -- ^^ whether to colourise
974 -> Name -- ^^ the term to show
976showName ist bnd impl colour n = case ist of
977 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
978 Nothing -> showbasic n
979 where name = if impl then show n else showbasic n
980 showbasic n@(UN _) = showCG n
981 showbasic (MN _ s) = str s
982 showbasic (NS n s) = showSep "." (map str (reverse s)) ++ "." ++ showbasic n
983 showbasic (SN s) = show s
985 colourise n t = let ctxt' = fmap tt_ctxt ist in
988 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
989 else colouriseBound t name
990 | isDConName n ctxt -> colouriseData t name
991 | isFnName n ctxt -> colouriseFun t name
992 | isTConName n ctxt -> colouriseType t name
993 -- The assumption is that if a name is not bound and does not exist in the
994 -- global context, then we're somewhere in which implicit info has been lost
995 -- (like error messages). Thus, unknown vars are colourised as implicits.
996 | otherwise -> colouriseImplicit t name
999showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1000 -> Bool -- ^^ whether to show implicits
1001 -> Bool -- ^^ whether to colourise
1002 -> PTerm -- ^^ the term to show
1004showImp ist impl colour tm = se 10 [] tm where
1005 perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1006 perhapsColourise col str = case ist of
1007 Just i -> if colour then col (idris_colourTheme i) str else str
1010 se :: Int -> [(Name, Bool)] -> PTerm -> String
1011 se p bnd (PQuote r) = "![" ++ show r ++ "]"
1012 se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1013 se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1015 | Just str <- slist p bnd e = str
1016 | Just num <- snat p e = perhapsColourise colouriseData (show num)
1017 se p bnd (PRef fc n) = showName ist bnd impl colour n
1018 se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1019 (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1020 ++ se 10 ((n, False):bnd) sc
1021 se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1022 " = " ++ se 10 bnd v ++
1023 " in " ++ se 10 ((n, False):bnd) sc
1024 se p bnd (PPi (Exp l s _ param) n ty sc)
1025 | n `elem` allNamesIn sc || impl
1027 (if l then "|(" else "(") ++
1028 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1030 (if (impl && param) then "P" else "") ++
1032 "-> " ++ se 10 ((n, False):bnd) sc
1033 | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1034 where st = case s of
1035 Static -> "[static] "
1037 se p bnd (PPi (Imp l s _ _) n ty sc)
1038 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1039 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1040 "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1041 | otherwise = se 10 ((n, True):bnd) sc
1042 where st = case s of
1043 Static -> "[static] "
1045 se p bnd (PPi (Constraint _ _ _) n ty sc)
1046 = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1047 se p bnd (PPi (TacImp _ _ s _) n ty sc)
1049 "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1050 se 10 ((n, False):bnd) sc
1051 se p bnd (PMatchApp _ f) = "match " ++ show f
1052 se p bnd (PApp _ hd@(PRef _ f) [])
1053 | not impl = se p bnd hd
1054 se p bnd (PAppBind _ hd@(PRef _ f) [])
1055 | not impl = "!" ++ se p bnd hd
1056 se p bnd (PApp _ op@(PRef _ (UN nm)) args)
1058 length (getExps args) == 2 && not impl && not (isAlpha (thead nm))
1059 = let [l, r] = getExps args in
1060 bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1061 se p bnd (PApp _ f as)
1062 = -- let args = getExps as in
1063 bracket p 1 $ se 1 bnd f ++
1064 if impl then concatMap (sArg bnd) as
1065 else concatMap (suiArg impl bnd) as
1066 se p bnd (PAppBind _ f as)
1067 = let args = getExps as in
1068 "!" ++ (bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1069 else concatMap (seArg bnd) args)
1070 se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1071 where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1072 se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1073 se p bnd (PRefl _ t)
1074 | not impl = perhapsColourise colouriseData "refl"
1075 | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1076 se p bnd (PResolveTC _) = "resolvetc"
1077 se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1078 se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1079 se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1080 se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1081 se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1082 se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1083 se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1084 se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1085 se p bnd PType = perhapsColourise colouriseType "Type"
1086 se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1087 where cfun (AType _) = colouriseType
1088 cfun StrType = colouriseType
1089 cfun PtrType = colouriseType
1090 cfun VoidType = colouriseType
1091 cfun _ = colouriseData
1092 se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1093 se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1094 se p bnd (PMetavar n) = "?" ++ show n
1095 se p bnd (PReturn f) = "return"
1096 se p bnd PImpossible = "impossible"
1097 se p bnd Placeholder = "_"
1098 se p bnd (PDoBlock _) = "do block show not implemented"
1099 se p bnd (PElabError s) = show s
1100 se p bnd (PCoerced t) = se p bnd t
1101 se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1102 se p bnd (PDisamb ns t) = "%disamb " ++ show ns ++ se p bnd t
1103 se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1104-- se p bnd x = "Not implemented"
1106 slist' p bnd (PApp _ (PRef _ nil) _)
1107 | not impl && nsroot nil == sUN "Nil" = Just []
1108 slist' p bnd (PApp _ (PRef _ cons) args)
1109 | nsroot cons == sUN "::",
1110 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1112 Just tl' <- slist' p bnd tl
1115 isImp (PImp {}) = True
1117 slist' _ _ _ = Nothing
1119 slist p bnd e | Just es <- slist' p bnd e = Just $
1120 case es of [] -> "[]"
1121 [x] -> "[" ++ se p bnd x ++ "]"
1122 xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1123 slist _ _ _ = Nothing
1125 -- since Prelude is always imported, S & Z are unqualified iff they're the
1128 | show o == (natns++"Z") || show o == "Z" = Just 0
1129 snat p (PApp _ s [PExp {getTm=n}])
1130 | show s == (natns++"S") || show s == "S",
1135 natns = "Prelude.Nat."
1137 sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1138 sArg bnd (PExp _ _ tm _) = seArg bnd tm
1139 sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1140 sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1142 -- show argument, implicits given by the user also shown
1143 suiArg impl bnd (PImp _ mi _ n tm _)
1144 | impl || not mi = siArg bnd (n, tm)
1145 suiArg impl bnd (PExp _ _ tm _) = seArg bnd tm
1146 suiArg impl bnd _ = ""
1148 seArg bnd arg = " " ++ se 0 bnd arg
1149 siArg bnd (n, val) =
1151 val' = se 10 bnd val in
1153 then " {" ++ n' ++ "}"
1154 else " {" ++ n' ++ " = " ++ val' ++ "}"
1155 scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1156 stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1158 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1162instance Sized PTerm where
1163 size (PQuote rawTerm) = size rawTerm
1164 size (PRef fc name) = size name
1165 size (PLam name ty bdy) = 1 + size ty + size bdy
1166 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1167 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1168 size (PTyped trm ty) = 1 + size trm + size ty
1169 size (PApp fc name args) = 1 + size args
1170 size (PAppBind fc name args) = 1 + size args
1171 size (PCase fc trm bdy) = 1 + size trm + size bdy
1173 size (PFalse fc) = 1
1174 size (PRefl fc _) = 1
1175 size (PResolveTC fc) = 1
1176 size (PEq fc left right) = 1 + size left + size right
1177 size (PRewrite fc left right _) = 1 + size left + size right
1178 size (PPair fc left right) = 1 + size left + size right
1179 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1180 size (PAlternative a alts) = 1 + size alts
1181 size (PHidden hidden) = size hidden
1182 size (PUnifyLog tm) = size tm
1183 size (PDisamb _ tm) = size tm
1184 size (PNoImplicits tm) = size tm
1186 size (PConstant const) = 1 + size const
1187 size Placeholder = 1
1188 size (PDoBlock dos) = 1 + size dos
1189 size (PIdiom fc term) = 1 + size term
1190 size (PReturn fc) = 1
1191 size (PMetavar name) = 1
1192 size (PProof tactics) = size tactics
1193 size (PElabError err) = size err
1194 size PImpossible = 1
1196getPArity :: PTerm -> Int
1197getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1200-- Return all names, free or globally bound, in the given term.
1202allNamesIn :: PTerm -> [Name]
1203allNamesIn tm = nub $ ni [] tm
1206 | not (n `elem` env) = [n]
1207 ni env (PPatvar _ n) = [n]
1208 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1209 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1210 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1211 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1212 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1213 ni env (PHidden tm) = ni env tm
1214 ni env (PEq _ l r) = ni env l ++ ni env r
1215 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1216 ni env (PTyped l r) = ni env l ++ ni env r
1217 ni env (PPair _ l r) = ni env l ++ ni env r
1218 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1219 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1220 ni env (PAlternative a ls) = concatMap (ni env) ls
1221 ni env (PUnifyLog tm) = ni env tm
1222 ni env (PDisamb _ tm) = ni env tm
1223 ni env (PNoImplicits tm) = ni env tm
1226-- Return all names defined in binders in the given term
1227boundNamesIn :: PTerm -> [Name]
1228boundNamesIn tm = nub $ ni tm
1230 ni (PApp _ f as) = ni f ++ concatMap (ni) (map getTm as)
1231 ni (PAppBind _ f as) = ni f ++ concatMap (ni) (map getTm as)
1232 ni (PCase _ c os) = ni c ++ concatMap (ni) (map snd os)
1233 ni (PLam n ty sc) = n : (ni ty ++ ni sc)
1234 ni (PLet n ty val sc) = n : (ni ty ++ ni val ++ ni sc)
1235 ni (PPi _ n ty sc) = n : (ni ty ++ ni sc)
1236 ni (PEq _ l r) = ni l ++ ni r
1237 ni (PRewrite _ l r _) = ni l ++ ni r
1238 ni (PTyped l r) = ni l ++ ni r
1239 ni (PPair _ l r) = ni l ++ ni r
1240 ni (PDPair _ (PRef _ n) t r) = ni t ++ ni r
1241 ni (PDPair _ l t r) = ni l ++ ni t ++ ni r
1242 ni (PAlternative a as) = concatMap (ni) as
1243 ni (PHidden tm) = ni tm
1244 ni (PUnifyLog tm) = ni tm
1245 ni (PDisamb _ tm) = ni tm
1246 ni (PNoImplicits tm) = ni tm
1249-- Return names which are free in the given term.
1250namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1251namesIn uvars ist tm = nub $ ni [] tm
1254 | not (n `elem` env)
1255 = case lookupTy n (tt_ctxt ist) of
1257 _ -> if n `elem` (map fst uvars) then [n] else []
1258 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1259 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1260 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1261 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1262 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1263 ni env (PEq _ l r) = ni env l ++ ni env r
1264 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1265 ni env (PTyped l r) = ni env l ++ ni env r
1266 ni env (PPair _ l r) = ni env l ++ ni env r
1267 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1268 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1269 ni env (PAlternative a as) = concatMap (ni env) as
1270 ni env (PHidden tm) = ni env tm
1271 ni env (PUnifyLog tm) = ni env tm
1272 ni env (PDisamb _ tm) = ni env tm
1273 ni env (PNoImplicits tm) = ni env tm
1276-- Return which of the given names are used in the given term.
1278usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1279usedNamesIn vars ist tm = nub $ ni [] tm
1282 | n `elem` vars && not (n `elem` env)
1283 = case lookupTy n (tt_ctxt ist) of
1286 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1287 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1288 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1289 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1290 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1291 ni env (PEq _ l r) = ni env l ++ ni env r
1292 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1293 ni env (PTyped l r) = ni env l ++ ni env r
1294 ni env (PPair _ l r) = ni env l ++ ni env r
1295 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1296 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1297 ni env (PAlternative a as) = concatMap (ni env) as
1298 ni env (PHidden tm) = ni env tm
1299 ni env (PUnifyLog tm) = ni env tm
1300 ni env (PDisamb _ tm) = ni env tm
1301 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