1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
8import Core.Elaborate hiding (Tactic(..))
11import IRTS.CodegenCommon
13import Util.DynamicLinker
19import System.Console.Haskeline
22import Control.Monad.Trans.State.Strict
23import Control.Monad.Trans.Error
28import Data.Word (Word)
32data IOption = IOption { opt_logLevel :: Int,
34 opt_typeintype :: Bool,
36 opt_showimp :: Bool, -- ^^ show implicits
37 opt_errContext :: Bool,
41 opt_codegen :: Codegen,
42 opt_outputTy :: OutputType,
43 opt_ibcsubdir :: FilePath,
44 opt_importdirs :: [FilePath],
48 opt_cmdline :: [Opt] -- remember whole command line
52defaultOpts = IOption { opt_logLevel = 0
53 , opt_typecase = False
54 , opt_typeintype = False
57 , opt_errContext = False
62 , opt_outputTy = Executable
71data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
73-- | The output mode in use
74data OutputMode = RawOutput | IdeSlave Integer deriving Show
76-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
77-- without typechecking).
78-- This will include all the functions and data declarations, plus fixity declarations
81-- | The global state used in the Idris monad
83 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
84 idris_constraints :: [(UConstraint, FC)],
85 -- ^ A list of universe constraints and their corresponding source locations
86 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
87 idris_implicits :: Ctxt [PArg],
88 idris_statics :: Ctxt [Bool],
89 idris_classes :: Ctxt ClassInfo,
90 idris_dsls :: Ctxt DSL,
91 idris_optimisation :: Ctxt OptInfo,
92 idris_datatypes :: Ctxt TypeInfo,
93 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
94 -- ^ list of lhs/rhs, and a list of missing clauses
95 idris_flags :: Ctxt [FnOpt],
96 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
97 idris_calledgraph :: Ctxt [Name],
98 idris_docstrings :: Ctxt String,
99 idris_totcheck :: [(FC, Name)],
101 idris_options :: IOption,
103 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
104 idris_coercions :: [Name],
105 idris_transforms :: [(Term, Term)],
106 syntax_rules :: [Syntax],
107 syntax_keywords :: [String],
108 imported :: [FilePath], -- ^ The imported modules
109 idris_scprims :: [(Name, (Int, PrimFn))],
110 idris_objs :: [(Codegen, FilePath)],
111 idris_libs :: [(Codegen, String)],
112 idris_cgflags :: [(Codegen, String)],
113 idris_hdrs :: [(Codegen, String)],
114 proof_list :: [(Name, [String])],
115 errLine :: Maybe Int,
116 lastParse :: Maybe Name,
117 indent_stack :: [Int],
118 brace_stack :: [Maybe Int],
119 hide_list :: [(Name, Maybe Accessibility)],
120 default_access :: Accessibility,
121 default_total :: Bool,
122 ibc_write :: [IBCWrite],
123 compiled_so :: Maybe String,
124 idris_dynamic_libs :: [DynamicLib],
125 idris_language_extensions :: [LanguageExt],
126 idris_outputmode :: OutputMode,
127 idris_colourRepl :: Bool,
128 idris_colourTheme :: ColourTheme
131data SizeChange = Smaller | Same | Bigger | Unknown
134deriving instance Binary SizeChange
137type SCGEntry = (Name, [Maybe (Int, SizeChange)])
139data CGInfo = CGInfo { argsdef :: [Name],
140 calls :: [(Name, [[Name]])],
146deriving instance Binary CGInfo
149primDefs = [UN "unsafePerformPrimIO",
150 UN "mkLazyForeignPrim",
154-- information that needs writing for the current module's .ibc file
155data IBCWrite = IBCFix FixDecl
159 | IBCInstance Bool Name Name
166 | IBCObj Codegen FilePath
167 | IBCLib Codegen String
168 | IBCCGFlag Codegen String
170 | IBCHeader Codegen String
171 | IBCAccess Name Accessibility
172 | IBCTotal Name Totality
173 | IBCFlags Name [FnOpt]
174 | IBCTrans (Term, Term)
178 | IBCDef Name -- i.e. main context
181idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
182 emptyContext emptyContext emptyContext emptyContext
183 emptyContext emptyContext emptyContext emptyContext
184 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] [] [] []
185 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
188-- | The monad for the main REPL - reading and processing files and updating
189-- global state (hence the IO inner monad).
190--type Idris = WriterT [Either String (IO ())] (State IState a))
191type Idris = StateT IState (ErrorT Err IO)
193-- Commands in the REPL
212 | ChangeDirectory FilePath
215 | Compile Codegen String
220 | AddProof (Maybe Name)
233 | DynamicLink FilePath
241 | SetColour ColourType IdrisColour
245data Opt = Filename String
279 | OutputTy OutputType
280 | Extension LanguageExt
281 | InterpretScript String
282 | TargetTriple String
287-- Parsed declarations
289data Fixity = Infixl { prec :: Int }
290 | Infixr { prec :: Int }
291 | InfixN { prec :: Int }
292 | PrefixN { prec :: Int }
295deriving instance Binary Fixity
298instance Show Fixity where
299 show (Infixl i) = "infixl " ++ show i
300 show (Infixr i) = "infixr " ++ show i
301 show (InfixN i) = "infix " ++ show i
302 show (PrefixN i) = "prefix " ++ show i
304data FixDecl = Fix Fixity String
307instance Show FixDecl where
308 show (Fix f s) = show f ++ " " ++ s
311deriving instance Binary FixDecl
314instance Ord FixDecl where
315 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
318data Static = Static | Dynamic
321deriving instance Binary Static
324-- Mark bindings with their explicitness, and laziness
325data Plicity = Imp { plazy :: Bool,
328 | Exp { plazy :: Bool,
331 | Constraint { plazy :: Bool,
334 | TacImp { plazy :: Bool,
341deriving instance Binary Plicity
344impl = Imp False Dynamic ""
345expl = Exp False Dynamic ""
346constraint = Constraint False Dynamic ""
347tacimpl t = TacImp False Dynamic t ""
349data FnOpt = Inlinable -- always evaluate when simplifying
350 | TotalFn | PartialFn
351 | Coinductive | AssertTotal
352 | Dictionary -- type class dictionary, eval only when
353 -- a function argument, and further evaluation resutls
354 | Implicit -- implicit coercion
355 | CExport String -- export, with a C name
356 | Reflection -- a reflecting function, compile-time only
357 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
360deriving instance Binary FnOpt
365inlinable :: FnOpts -> Bool
366inlinable = elem Inlinable
368dictionary :: FnOpts -> Bool
369dictionary = elem Dictionary
371-- | Top-level declarations such as compiler directives, definitions,
372-- datatypes and typeclasses.
374 = PFix FC Fixity [String] -- ^ Fixity declaration
375 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
376 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
377 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
378 | PCAF FC Name t -- ^ Top level constant
379 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
380 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
381 | PNamespace String [PDecl' t] -- ^ New namespace
382 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
383 | PClass String SyntaxInfo FC
386 [(Name, t)] -- parameters
387 [PDecl' t] -- declarations
388 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
389 -- class name, parameters, method declarations
390 | PInstance SyntaxInfo FC [t] -- constraints
393 t -- full instance type
394 (Maybe Name) -- explicit name
396 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
397 -- class name, parameters, full instance type, optional explicit name, and definitions
398 | PDSL Name (DSL' t) -- ^ DSL declaration
399 | PSyntax FC Syntax -- ^ Syntax definition
400 | PMutual FC [PDecl' t] -- ^ Mutual block
401 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
402 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
403 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
404 -- bool is True, lhs and rhs must be convertible
407deriving instance Binary PDecl'
410-- | One clause of a top-level definition. Term arguments to constructors are:
412-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
414-- 2. The list of patterns
416-- 3. The right-hand side
418-- 4. The where block (PDecl' t)
419data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
420 | PWith FC Name t [t] t [PDecl' t]
421 | PClauseR FC [t] t [PDecl' t]
422 | PWithR FC [t] t [PDecl' t]
425deriving instance Binary PClause'
429data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
430 d_tcon :: t, -- ^ Type constructor
431 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
433 -- ^ Data declaration
434 | PLaterdecl { d_name :: Name, d_tcon :: t }
435 -- ^ "Placeholder" for data whose constructors are defined later
438deriving instance Binary PData'
441-- Handy to get a free function for applying PTerm -> PTerm functions
442-- across a program, by deriving Functor
444type PDecl = PDecl' PTerm
445type PData = PData' PTerm
446type PClause = PClause' PTerm
448-- get all the names declared in a decl
450declared :: PDecl -> [Name]
451declared (PFix _ _ _) = []
452declared (PTy _ _ _ _ n t) = [n]
453declared (PPostulate _ _ _ _ n t) = [n]
454declared (PClauses _ _ n _) = [] -- not a declaration
455declared (PCAF _ n _) = [n]
456declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
457 where fstt (_, a, _, _) = a
458declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
459declared (PParams _ _ ds) = concatMap declared ds
460declared (PNamespace _ ds) = concatMap declared ds
461declared (PRecord _ _ _ n _ _ c _) = [n, c]
462declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
463declared (PInstance _ _ _ _ _ _ _ _) = []
464declared (PDSL n _) = [n]
465declared (PSyntax _ _) = []
466declared (PMutual _ ds) = concatMap declared ds
467declared (PDirective _) = []
469-- get the names declared, not counting nested parameter blocks
470tldeclared :: PDecl -> [Name]
471tldeclared (PFix _ _ _) = []
472tldeclared (PTy _ _ _ _ n t) = [n]
473tldeclared (PPostulate _ _ _ _ n t) = [n]
474tldeclared (PClauses _ _ n _) = [] -- not a declaration
475tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
476tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
477 where fstt (_, a, _, _) = a
478tldeclared (PParams _ _ ds) = []
479tldeclared (PMutual _ ds) = concatMap tldeclared ds
480tldeclared (PNamespace _ ds) = concatMap tldeclared ds
481tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
482tldeclared (PInstance _ _ _ _ _ _ _ _) = []
485defined :: PDecl -> [Name]
486defined (PFix _ _ _) = []
487defined (PTy _ _ _ _ n t) = []
488defined (PPostulate _ _ _ _ n t) = []
489defined (PClauses _ _ n _) = [n] -- not a declaration
490defined (PCAF _ n _) = [n]
491defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
492 where fstt (_, a, _, _) = a
493defined (PData _ _ _ _ (PLaterdecl n _)) = []
494defined (PParams _ _ ds) = concatMap defined ds
495defined (PNamespace _ ds) = concatMap defined ds
496defined (PRecord _ _ _ n _ _ c _) = [n, c]
497defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
498defined (PInstance _ _ _ _ _ _ _ _) = []
499defined (PDSL n _) = [n]
500defined (PSyntax _ _) = []
501defined (PMutual _ ds) = concatMap defined ds
502defined (PDirective _) = []
505updateN :: [(Name, Name)] -> Name -> Name
506updateN ns n | Just n' <- lookup n ns = n'
509updateNs :: [(Name, Name)] -> PTerm -> PTerm
511updateNs ns t = mapPT updateRef t
512 where updateRef (PRef fc f) = PRef fc (updateN ns f)
515-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
517-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
518-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
519-- where updateCNs ns (PClause n l ts r ds)
520-- = PClause (updateN ns n) (fmap (updateNs ns) l)
521-- (map (fmap (updateNs ns)) ts)
522-- (fmap (updateNs ns) r)
523-- (map (updateDNs ns) ds)
526-- | High level language terms
527data PTerm = PQuote Raw
529 | PInferRef FC Name -- ^ A name to be defined later
531 | PLam Name PTerm PTerm
532 | PPi Plicity Name PTerm PTerm
533 | PLet Name PTerm PTerm PTerm
534 | PTyped PTerm PTerm -- ^ Term with explicit type
535 | PApp FC PTerm [PArg]
536 | PMatchApp FC Name -- ^ Make an application by type matching
537 | PCase FC PTerm [(PTerm, PTerm)]
543 | PRewrite FC PTerm PTerm (Maybe PTerm)
544 | PPair FC PTerm PTerm
545 | PDPair FC PTerm PTerm PTerm
546 | PAlternative Bool [PTerm] -- True if only one may work
547 | PHidden PTerm -- ^ Irrelevant or hidden pattern
549 | PGoal FC PTerm Name PTerm
556 | PProof [PTactic] -- ^ Proof script
557 | PTactics [PTactic] -- ^ As PProof, but no auto solving
558 | PElabError Err -- ^ Error to report on elaboration
559 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
560 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
561 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
562 | PNoImplicits PTerm -- ^ never run implicit converions on the term
565deriving instance Binary PTerm
568mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
569mapPT f t = f (mpt t) where
570 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
571 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
572 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
573 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
575 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
576 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
577 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
578 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
579 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
580 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
581 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
582 mpt (PHidden t) = PHidden (mapPT f t)
583 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
584 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
585 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
586 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
587 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
588 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
592data PTactic' t = Intro [Name] | Intros | Focus Name
593 | Refine Name [Bool] | Rewrite t
596 | LetTac Name t | LetTacTy Name t t
597 | Exact t | Compute | Trivial
600 | ProofState | ProofTerm | Undo
601 | Try (PTactic' t) (PTactic' t)
602 | TSeq (PTactic' t) (PTactic' t)
603 | ApplyTactic t -- see Language.Reflection module
606 | GoalType String (PTactic' t)
608 deriving (Show, Eq, Functor)
610deriving instance Binary PTactic'
613instance Sized a => Sized (PTactic' a) where
614 size (Intro nms) = 1 + size nms
616 size (Focus nm) = 1 + size nm
617 size (Refine nm bs) = 1 + size nm + length bs
618 size (Rewrite t) = 1 + size t
619 size (LetTac nm t) = 1 + size nm + size t
620 size (Exact t) = 1 + size t
628 size (Try l r) = 1 + size l + size r
629 size (TSeq l r) = 1 + size l + size r
630 size (ApplyTactic t) = 1 + size t
631 size (Reflect t) = 1 + size t
632 size (Fill t) = 1 + size t
636type PTactic = PTactic' PTerm
638data PDo' t = DoExp FC t
643 deriving (Eq, Functor)
645deriving instance Binary PDo'
648instance Sized a => Sized (PDo' a) where
649 size (DoExp fc t) = 1 + size fc + size t
650 size (DoBind fc nm t) = 1 + size fc + size nm + size t
651 size (DoBindP fc l r) = 1 + size fc + size l + size r
652 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
653 size (DoLetP fc l r) = 1 + size fc + size l + size r
657-- The priority gives a hint as to elaboration order. Best to elaborate
658-- things early which will help give a more concrete type to other
659-- variables, e.g. a before (interpTy a).
660-- TODO: priority no longer serves any purpose, drop it!
662data PArg' t = PImp { priority :: Int,
663 machine_inf :: Bool, -- true if the machine inferred it
664 lazyarg :: Bool, pname :: Name, getTm :: t,
666 | PExp { priority :: Int,
667 lazyarg :: Bool, getTm :: t,
669 | PConstraint { priority :: Int,
670 lazyarg :: Bool, getTm :: t,
672 | PTacImplicit { priority :: Int,
673 lazyarg :: Bool, pname :: Name,
677 deriving (Show, Eq, Functor)
679instance Sized a => Sized (PArg' a) where
680 size (PImp p _ l nm trm _) = 1 + size nm + size trm
681 size (PExp p l trm _) = 1 + size trm
682 size (PConstraint p l trm _) = 1 + size trm
683 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
686deriving instance Binary PArg'
689pimp n t mach = PImp 1 mach True n t ""
690pexp t = PExp 1 False t ""
691pconst t = PConstraint 1 False t ""
692ptacimp n s t = PTacImplicit 0 True n s t ""
694type PArg = PArg' PTerm
698data ClassInfo = CI { instanceName :: Name,
699 class_methods :: [(Name, (FnOpts, PTerm))],
700 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
701 class_params :: [Name],
702 class_instances :: [Name] }
705deriving instance Binary ClassInfo
708data OptInfo = Optimise { collapsible :: Bool,
710 forceable :: [Int], -- argument positions
714deriving instance Binary OptInfo
718data TypeInfo = TI { con_names :: [Name],
723deriving instance Binary TypeInfo
726-- Syntactic sugar info
728data DSL' t = DSL { dsl_bind :: t,
733 index_first :: Maybe t,
734 index_next :: Maybe t,
735 dsl_lambda :: Maybe t,
738 deriving (Show, Functor)
740deriving instance Binary DSL'
745data SynContext = PatternSyntax | TermSyntax | AnySyntax
748deriving instance Binary SynContext
751data Syntax = Rule [SSymbol] PTerm SynContext
754deriving instance Binary Syntax
757data SSymbol = Keyword Name
764deriving instance Binary SSymbol
767initDSL = DSL (PRef f (UN ">>="))
768 (PRef f (UN "return"))
776 where f = FC "(builtin)" 0
778data Using = UImplicit Name PTerm
779 | UConstraint Name [Name]
782deriving instance Binary Using
785data SyntaxInfo = Syn { using :: [Using],
786 syn_params :: [(Name, PTerm)],
787 syn_namespace :: [String],
789 decoration :: Name -> Name,
791 implicitAllowed :: Bool,
795deriving instance Binary SyntaxInfo
798defaultSyntax = Syn [] [] [] [] id False False initDSL
800expandNS :: SyntaxInfo -> Name -> Name
801expandNS syn n@(NS _ _) = n
802expandNS syn n = case syn_namespace syn of
807--- Pretty printing declarations and terms
809instance Show PTerm where
810 show tm = showImp Nothing False False tm
812instance Pretty PTerm where
813 pretty = prettyImp False
815instance Show PDecl where
816 show d = showDeclImp False d
818instance Show PClause where
819 show c = showCImp True c
821instance Show PData where
822 show d = showDImp False d
824showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
825showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
826showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
827showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
828showDeclImp _ (PData _ _ _ _ d) = show d
829showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
830 showSep "\n" (map show ps)
833showCImp :: Bool -> PClause -> String
834showCImp impl (PClause _ n l ws r w)
835 = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
836 ++ " where " ++ show w
839 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
840showCImp impl (PWith _ n l ws r w)
841 = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
842 ++ " { " ++ show w ++ " } "
845 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
848showDImp :: Bool -> PData -> String
849showDImp impl (PDatadecl n ty cons)
850 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
852 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
854getImps :: [PArg] -> [(Name, PTerm)]
856getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
857getImps (_ : xs) = getImps xs
859getExps :: [PArg] -> [PTerm]
861getExps (PExp _ _ tm _ : xs) = tm : getExps xs
862getExps (_ : xs) = getExps xs
864getConsts :: [PArg] -> [PTerm]
866getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
867getConsts (_ : xs) = getConsts xs
869getAll :: [PArg] -> [PTerm]
872-- | Pretty-print a high-level Idris term
873prettyImp :: Bool -- ^^ whether to show implicits
874 -> PTerm -- ^^ the term to pretty-print
876prettyImp impl = prettySe 10
878 prettySe p (PQuote r) =
879 if size r > breakingSize then
880 text "![" $$ pretty r <> text "]"
882 text "![" <> pretty r <> text "]"
883 prettySe p (PPatvar fc n) = pretty n
884 prettySe p (PRef fc n) =
890 prettyBasic n@(UN _) = pretty n
891 prettyBasic (MN _ s) = text s
892 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
893 prettySe p (PLam n ty sc) =
895 if size sc > breakingSize then
896 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
898 text "λ" <> pretty n <+> text "=>" <+> pretty sc
899 prettySe p (PLet n ty v sc) =
901 if size sc > breakingSize then
902 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
903 nest nestingSize (prettySe 10 sc)
905 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
907 prettySe p (PPi (Exp l s _) n ty sc)
908 | n `elem` allNamesIn sc || impl =
909 let open = if l then text "|" <> lparen else lparen in
911 if size sc > breakingSize then
912 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
913 st <+> text "->" $+$ prettySe 10 sc
915 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
916 st <+> text "->" <+> prettySe 10 sc
919 if size sc > breakingSize then
920 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
922 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
926 Static -> text "[static]"
928 prettySe p (PPi (Imp l s _) n ty sc)
930 let open = if l then text "|" <> lbrace else lbrace in
932 if size sc > breakingSize then
933 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
934 st <+> text "->" <+> prettySe 10 sc
936 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
937 st <+> text "->" <+> prettySe 10 sc
938 | otherwise = prettySe 10 sc
942 Static -> text $ "[static]"
944 prettySe p (PPi (Constraint _ _ _) n ty sc) =
946 if size sc > breakingSize then
947 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
949 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
950 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
952 if size sc > breakingSize then
953 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
954 rbrace <+> text "->" $+$ prettySe 10 sc
956 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
957 rbrace <+> text "->" <+> prettySe 10 sc
958 prettySe p (PApp _ (PRef _ f) [])
959 | not impl = pretty f
960 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
961 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
962 let [l, r] = getExps args in
964 if size r > breakingSize then
965 prettySe 1 l <+> pretty op $+$ prettySe 0 r
967 prettySe 1 l <+> pretty op <+> prettySe 0 r
968 prettySe p (PApp _ f as) =
969 let args = getExps as in
974 -- foldr (<+>) empty $ map prettyArgS as
976 foldl' fSe empty args
977 -- foldr (<+>) empty $ map prettyArgSe args
980 if size r > breakingSize then
981 l $+$ nest nestingSize (prettyArgS r)
986 if size r > breakingSize then
987 l $+$ nest nestingSize (prettyArgSe r)
990 prettySe p (PCase _ scr opts) =
991 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
993 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
995 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
996 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
997 prettySe p (PRefl _ _) = text "refl"
998 prettySe p (PResolveTC _) = text "resolvetc"
999 prettySe p (PTrue _) = text "()"
1000 prettySe p (PFalse _) = text "_|_"
1001 prettySe p (PEq _ l r) =
1003 if size r > breakingSize then
1004 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
1006 prettySe 10 l <+> text "=" <+> prettySe 10 r
1007 prettySe p (PRewrite _ l r _) =
1009 if size r > breakingSize then
1010 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
1012 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1013 prettySe p (PTyped l r) =
1014 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1015 prettySe p (PPair _ l r) =
1016 if size r > breakingSize then
1017 lparen <> prettySe 10 l <> text "," $+$
1018 prettySe 10 r <> rparen
1020 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
1021 prettySe p (PDPair _ l t r) =
1022 if size r > breakingSize then
1023 lparen <> prettySe 10 l <+> text "**" $+$
1024 prettySe 10 r <> rparen
1026 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1027 prettySe p (PAlternative a as) =
1028 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1031 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1032 prettySe p PType = text "Type"
1033 prettySe p (PConstant c) = pretty c
1034 -- XXX: add pretty for tactics
1035 prettySe p (PProof ts) =
1036 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1037 prettySe p (PTactics ts) =
1038 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1039 prettySe p (PMetavar n) = text "?" <> pretty n
1040 prettySe p (PReturn f) = text "return"
1041 prettySe p PImpossible = text "impossible"
1042 prettySe p Placeholder = text "_"
1043 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1044 prettySe p (PElabError s) = pretty s
1046 prettySe p _ = text "test"
1048 prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1049 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1050 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1051 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1053 prettyArgSe arg = prettySe 0 arg
1054 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1055 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1056 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1058 bracket outer inner doc
1059 | inner > outer = lparen <> doc <> rparen
1063showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1064 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1065 -> Bool -- ^^ whether to show implicits
1066 -> Bool -- ^^ whether to colourise
1067 -> Name -- ^^ the term to show
1069showName ist bnd impl colour n = case ist of
1070 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1071 Nothing -> showbasic n
1072 where name = if impl then show n else showbasic n
1073 showbasic n@(UN _) = show n
1074 showbasic (MN _ s) = s
1075 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1076 showbasic (SN s) = show s
1078 colourise n t = let ctxt' = fmap tt_ctxt ist in
1081 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1082 else colouriseBound t name
1083 | isDConName n ctxt -> colouriseData t name
1084 | isFnName n ctxt -> colouriseFun t name
1085 | isTConName n ctxt -> colouriseType t name
1086 -- The assumption is that if a name is not bound and does not exist in the
1087 -- global context, then we're somewhere in which implicit info has been lost
1088 -- (like error messages). Thus, unknown vars are colourised as implicits.
1089 | otherwise -> colouriseImplicit t name
1092showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1093 -> Bool -- ^^ whether to show implicits
1094 -> Bool -- ^^ whether to colourise
1095 -> PTerm -- ^^ the term to show
1097showImp ist impl colour tm = se 10 [] tm where
1098 perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1099 perhapsColourise col str = case ist of
1100 Just i -> if colour then col (idris_colourTheme i) str else str
1103 se :: Int -> [(Name, Bool)] -> PTerm -> String
1104 se p bnd (PQuote r) = "![" ++ show r ++ "]"
1105 se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1106 se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1108 | Just str <- slist p bnd e = str
1109 | Just num <- snat p e = perhapsColourise colouriseData (show num)
1110 se p bnd (PRef fc n) = showName ist bnd impl colour n
1111 se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1112 (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1113 ++ se 10 ((n, False):bnd) sc
1114 se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1115 " = " ++ se 10 bnd v ++
1116 " in " ++ se 10 ((n, False):bnd) sc
1117 se p bnd (PPi (Exp l s _) n ty sc)
1118 | n `elem` allNamesIn sc || impl
1120 (if l then "|(" else "(") ++
1121 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1123 "-> " ++ se 10 ((n, False):bnd) sc
1124 | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1125 where st = case s of
1126 Static -> "[static] "
1128 se p bnd (PPi (Imp l s _) n ty sc)
1129 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1130 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1131 "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1132 | otherwise = se 10 ((n, True):bnd) sc
1133 where st = case s of
1134 Static -> "[static] "
1136 se p bnd (PPi (Constraint _ _ _) n ty sc)
1137 = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1138 se p bnd (PPi (TacImp _ _ s _) n ty sc)
1140 "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1141 se 10 ((n, False):bnd) sc
1142 se p bnd (PMatchApp _ f) = "match " ++ show f
1143 se p bnd (PApp _ hd@(PRef _ f) [])
1144 | not impl = se p bnd hd
1145 se p bnd (PApp _ op@(PRef _ (UN (f:_))) args)
1146 | length (getExps args) == 2 && not impl && not (isAlpha f)
1147 = let [l, r] = getExps args in
1148 bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1149 se p bnd (PApp _ f as)
1150 = let args = getExps as in
1151 bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1152 else concatMap (seArg bnd) args
1153 se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1154 where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1155 se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1156 se p bnd (PRefl _ t)
1157 | not impl = perhapsColourise colouriseData "refl"
1158 | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1159 se p bnd (PResolveTC _) = "resolvetc"
1160 se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1161 se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1162 se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1163 se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1164 se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1165 se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1166 se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1167 se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1168 se p bnd PType = perhapsColourise colouriseType "Type"
1169 se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1170 where cfun (AType _) = colouriseType
1171 cfun StrType = colouriseType
1172 cfun PtrType = colouriseType
1173 cfun VoidType = colouriseType
1174 cfun _ = colouriseData
1175 se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1176 se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1177 se p bnd (PMetavar n) = "?" ++ show n
1178 se p bnd (PReturn f) = "return"
1179 se p bnd PImpossible = "impossible"
1180 se p bnd Placeholder = "_"
1181 se p bnd (PDoBlock _) = "do block show not implemented"
1182 se p bnd (PElabError s) = show s
1183 se p bnd (PCoerced t) = se p bnd t
1184 se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1185 se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1186-- se p bnd x = "Not implemented"
1188 slist' p bnd (PApp _ (PRef _ nil) _)
1189 | nsroot nil == UN "Nil" = Just []
1190 slist' p bnd (PApp _ (PRef _ cons) args)
1191 | nsroot cons == UN "::",
1192 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1194 Just tl' <- slist' p bnd tl
1197 isImp (PImp {}) = True
1199 slist' _ _ _ = Nothing
1201 slist p bnd e | Just es <- slist' p bnd e = Just $
1202 case es of [] -> "[]"
1203 [x] -> "[" ++ se p bnd x ++ "]"
1204 xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1205 slist _ _ _ = Nothing
1207 -- since Prelude is always imported, S & Z are unqualified iff they're the
1210 | show o == (natns++"Z") || show o == "Z" = Just 0
1211 snat p (PApp _ s [PExp {getTm=n}])
1212 | show s == (natns++"S") || show s == "S",
1217 natns = "Prelude.Nat."
1219 sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1220 sArg bnd (PExp _ _ tm _) = seArg bnd tm
1221 sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1222 sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1224 seArg bnd arg = " " ++ se 0 bnd arg
1225 siArg bnd (n, val) = " {" ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1226 scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1227 stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1229 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1233instance Sized PTerm where
1234 size (PQuote rawTerm) = size rawTerm
1235 size (PRef fc name) = size name
1236 size (PLam name ty bdy) = 1 + size ty + size bdy
1237 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1238 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1239 size (PTyped trm ty) = 1 + size trm + size ty
1240 size (PApp fc name args) = 1 + size args
1241 size (PCase fc trm bdy) = 1 + size trm + size bdy
1243 size (PFalse fc) = 1
1244 size (PRefl fc _) = 1
1245 size (PResolveTC fc) = 1
1246 size (PEq fc left right) = 1 + size left + size right
1247 size (PRewrite fc left right _) = 1 + size left + size right
1248 size (PPair fc left right) = 1 + size left + size right
1249 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1250 size (PAlternative a alts) = 1 + size alts
1251 size (PHidden hidden) = size hidden
1252 size (PUnifyLog tm) = size tm
1253 size (PNoImplicits tm) = size tm
1255 size (PConstant const) = 1 + size const
1256 size Placeholder = 1
1257 size (PDoBlock dos) = 1 + size dos
1258 size (PIdiom fc term) = 1 + size term
1259 size (PReturn fc) = 1
1260 size (PMetavar name) = 1
1261 size (PProof tactics) = size tactics
1262 size (PElabError err) = size err
1263 size PImpossible = 1
1265getPArity :: PTerm -> Int
1266getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1269-- Return all names, free or globally bound, in the given term.
1271allNamesIn :: PTerm -> [Name]
1272allNamesIn tm = nub $ ni [] tm
1275 | not (n `elem` env) = [n]
1276 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1277 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1278 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1279 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1280 ni env (PHidden tm) = ni env tm
1281 ni env (PEq _ l r) = ni env l ++ ni env r
1282 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1283 ni env (PTyped l r) = ni env l ++ ni env r
1284 ni env (PPair _ l r) = ni env l ++ ni env r
1285 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1286 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1287 ni env (PAlternative a ls) = concatMap (ni env) ls
1288 ni env (PUnifyLog tm) = ni env tm
1289 ni env (PNoImplicits tm) = ni env tm
1292-- Return names which are free in the given term.
1294namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1295namesIn uvars ist tm = nub $ ni [] tm
1298 | not (n `elem` env)
1299 = case lookupTy n (tt_ctxt ist) of
1301 _ -> if n `elem` (map fst uvars) then [n] else []
1302 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1303 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1304 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1305 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1306 ni env (PEq _ l r) = ni env l ++ ni env r
1307 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1308 ni env (PTyped l r) = ni env l ++ ni env r
1309 ni env (PPair _ l r) = ni env l ++ ni env r
1310 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1311 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1312 ni env (PAlternative a as) = concatMap (ni env) as
1313 ni env (PHidden tm) = ni env tm
1314 ni env (PUnifyLog tm) = ni env tm
1315 ni env (PNoImplicits tm) = ni env tm
1318-- Return which of the given names are used in the given term.
1320usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1321usedNamesIn vars ist tm = nub $ ni [] tm
1324 | n `elem` vars && not (n `elem` env)
1325 = case lookupTy n (tt_ctxt ist) of
1328 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1329 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1330 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1331 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1332 ni env (PEq _ l r) = ni env l ++ ni env r
1333 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1334 ni env (PTyped l r) = ni env l ++ ni env r
1335 ni env (PPair _ l r) = ni env l ++ ni env r
1336 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1337 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1338 ni env (PAlternative a as) = concatMap (ni env) as
1339 ni env (PHidden tm) = ni env tm
1340 ni env (PUnifyLog tm) = ni env tm
1341 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