1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
8import Core.Elaborate hiding (Tactic(..))
11import IRTS.CodegenCommon
13import Util.DynamicLinker
17import System.Console.Haskeline
19import Control.Monad.Trans.State.Strict
27data IOption = IOption { opt_logLevel :: Int,
29 opt_typeintype :: Bool,
32 opt_errContext :: Bool,
36 opt_codegen :: Codegen,
37 opt_outputTy :: OutputType,
38 opt_ibcsubdir :: FilePath,
39 opt_importdirs :: [FilePath],
43 opt_cmdline :: [Opt] -- remember whole command line
47defaultOpts = IOption { opt_logLevel = 0
48 , opt_typecase = False
49 , opt_typeintype = False
52 , opt_errContext = False
57 , opt_outputTy = Executable
66data LanguageExt = TypeProviders deriving (Show, Eq, Read, Ord)
68-- | The output mode in use
69data OutputMode = RawOutput | IdeSlave Integer deriving Show
71-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
72-- without typechecking).
73-- This will include all the functions and data declarations, plus fixity declarations
76-- | The global state used in the Idris monad
78 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
79 idris_constraints :: [(UConstraint, FC)],
80 -- ^ A list of universe constraints and their corresponding source locations
81 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
82 idris_implicits :: Ctxt [PArg],
83 idris_statics :: Ctxt [Bool],
84 idris_classes :: Ctxt ClassInfo,
85 idris_dsls :: Ctxt DSL,
86 idris_optimisation :: Ctxt OptInfo,
87 idris_datatypes :: Ctxt TypeInfo,
88 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
89 -- ^ list of lhs/rhs, and a list of missing clauses
90 idris_flags :: Ctxt [FnOpt],
91 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
92 idris_calledgraph :: Ctxt [Name],
93 idris_docstrings :: Ctxt String,
94 idris_totcheck :: [(FC, Name)],
96 idris_options :: IOption,
98 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
99 idris_coercions :: [Name],
100 idris_transforms :: [(Term, Term)],
101 syntax_rules :: [Syntax],
102 syntax_keywords :: [String],
103 imported :: [FilePath], -- ^ The imported modules
104 idris_scprims :: [(Name, (Int, PrimFn))],
105 idris_objs :: [(Codegen, FilePath)],
106 idris_libs :: [(Codegen, String)],
107 idris_hdrs :: [(Codegen, String)],
108 proof_list :: [(Name, [String])],
109 errLine :: Maybe Int,
110 lastParse :: Maybe Name,
111 indent_stack :: [Int],
112 brace_stack :: [Maybe Int],
113 hide_list :: [(Name, Maybe Accessibility)],
114 default_access :: Accessibility,
115 default_total :: Bool,
116 ibc_write :: [IBCWrite],
117 compiled_so :: Maybe String,
118 idris_dynamic_libs :: [DynamicLib],
119 idris_language_extensions :: [LanguageExt],
120 idris_outputmode :: OutputMode
123data SizeChange = Smaller | Same | Bigger | Unknown
126deriving instance Binary SizeChange
129type SCGEntry = (Name, [Maybe (Int, SizeChange)])
131data CGInfo = CGInfo { argsdef :: [Name],
132 calls :: [(Name, [[Name]])],
138deriving instance Binary CGInfo
141primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
143-- information that needs writing for the current module's .ibc file
144data IBCWrite = IBCFix FixDecl
148 | IBCInstance Bool Name Name
155 | IBCObj Codegen FilePath
156 | IBCLib Codegen String
158 | IBCHeader Codegen String
159 | IBCAccess Name Accessibility
160 | IBCTotal Name Totality
161 | IBCFlags Name [FnOpt]
162 | IBCTrans (Term, Term)
166 | IBCDef Name -- i.e. main context
169idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
170 emptyContext emptyContext emptyContext emptyContext
171 emptyContext emptyContext emptyContext emptyContext
172 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] [] []
173 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
175-- | The monad for the main REPL - reading and processing files and updating
176-- global state (hence the IO inner monad).
177--type Idris = WriterT [Either String (IO ())] (State IState a))
178type Idris = StateT IState IO
180-- Commands in the REPL
199 | ChangeDirectory FilePath
202 | Compile Codegen String
207 | AddProof (Maybe Name)
219 | DynamicLink FilePath
228data Opt = Filename String
261 | OutputTy OutputType
262 | Extension LanguageExt
263 | InterpretScript String
264 | TargetTriple String
269-- Parsed declarations
271data Fixity = Infixl { prec :: Int }
272 | Infixr { prec :: Int }
273 | InfixN { prec :: Int }
274 | PrefixN { prec :: Int }
277deriving instance Binary Fixity
280instance Show Fixity where
281 show (Infixl i) = "infixl " ++ show i
282 show (Infixr i) = "infixr " ++ show i
283 show (InfixN i) = "infix " ++ show i
284 show (PrefixN i) = "prefix " ++ show i
286data FixDecl = Fix Fixity String
289instance Show FixDecl where
290 show (Fix f s) = show f ++ " " ++ s
293deriving instance Binary FixDecl
296instance Ord FixDecl where
297 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
300data Static = Static | Dynamic
303deriving instance Binary Static
306-- Mark bindings with their explicitness, and laziness
307data Plicity = Imp { plazy :: Bool,
310 | Exp { plazy :: Bool,
313 | Constraint { plazy :: Bool,
316 | TacImp { plazy :: Bool,
323deriving instance Binary Plicity
326impl = Imp False Dynamic ""
327expl = Exp False Dynamic ""
328constraint = Constraint False Dynamic ""
329tacimpl t = TacImp False Dynamic t ""
331data FnOpt = Inlinable -- always evaluate when simplifying
332 | TotalFn | PartialFn
333 | Coinductive | AssertTotal
334 | Dictionary -- type class dictionary, eval only when
335 -- a function argument, and further evaluation resutls
336 | Implicit -- implicit coercion
337 | CExport String -- export, with a C name
338 | Reflection -- a reflecting function, compile-time only
339 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
342deriving instance Binary FnOpt
347inlinable :: FnOpts -> Bool
348inlinable = elem Inlinable
350dictionary :: FnOpts -> Bool
351dictionary = elem Dictionary
353-- | Top-level declarations such as compiler directives, definitions,
354-- datatypes and typeclasses.
356 = PFix FC Fixity [String] -- ^ Fixity declaration
357 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
358 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
359 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
360 | PCAF FC Name t -- ^ Top level constant
361 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
362 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
363 | PNamespace String [PDecl' t] -- ^ New namespace
364 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
365 | PClass String SyntaxInfo FC
368 [(Name, t)] -- parameters
369 [PDecl' t] -- declarations
370 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
371 -- class name, parameters, method declarations
372 | PInstance SyntaxInfo FC [t] -- constraints
375 t -- full instance type
376 (Maybe Name) -- explicit name
378 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
379 -- class name, parameters, full instance type, optional explicit name, and definitions
380 | PDSL Name (DSL' t) -- ^ DSL declaration
381 | PSyntax FC Syntax -- ^ Syntax definition
382 | PMutual FC [PDecl' t] -- ^ Mutual block
383 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
384 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
385 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
386 -- bool is True, lhs and rhs must be convertible
389deriving instance Binary PDecl'
392-- | One clause of a top-level definition. Term arguments to constructors are:
394-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
396-- 2. The list of patterns
398-- 3. The right-hand side
400-- 4. The where block (PDecl' t)
401data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
402 | PWith FC Name t [t] t [PDecl' t]
403 | PClauseR FC [t] t [PDecl' t]
404 | PWithR FC [t] t [PDecl' t]
407deriving instance Binary PClause'
411data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
412 d_tcon :: t, -- ^ Type constructor
413 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
415 -- ^ Data declaration
416 | PLaterdecl { d_name :: Name, d_tcon :: t }
417 -- ^ "Placeholder" for data whose constructors are defined later
420deriving instance Binary PData'
423-- Handy to get a free function for applying PTerm -> PTerm functions
424-- across a program, by deriving Functor
426type PDecl = PDecl' PTerm
427type PData = PData' PTerm
428type PClause = PClause' PTerm
430-- get all the names declared in a decl
432declared :: PDecl -> [Name]
433declared (PFix _ _ _) = []
434declared (PTy _ _ _ _ n t) = [n]
435declared (PPostulate _ _ _ _ n t) = [n]
436declared (PClauses _ _ n _) = [] -- not a declaration
437declared (PCAF _ n _) = [n]
438declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
439 where fstt (_, a, _, _) = a
440declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
441declared (PParams _ _ ds) = concatMap declared ds
442declared (PNamespace _ ds) = concatMap declared ds
443declared (PRecord _ _ _ n _ _ c _) = [n, c]
444declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
445declared (PInstance _ _ _ _ _ _ _ _) = []
446declared (PDSL n _) = [n]
447declared (PSyntax _ _) = []
448declared (PMutual _ ds) = concatMap declared ds
449declared (PDirective _) = []
451-- get the names declared, not counting nested parameter blocks
452tldeclared :: PDecl -> [Name]
453tldeclared (PFix _ _ _) = []
454tldeclared (PTy _ _ _ _ n t) = [n]
455tldeclared (PPostulate _ _ _ _ n t) = [n]
456tldeclared (PClauses _ _ n _) = [] -- not a declaration
457tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
458tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
459 where fstt (_, a, _, _) = a
460tldeclared (PParams _ _ ds) = []
461tldeclared (PMutual _ ds) = concatMap tldeclared ds
462tldeclared (PNamespace _ ds) = concatMap tldeclared ds
463tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
464tldeclared (PInstance _ _ _ _ _ _ _ _) = []
467defined :: PDecl -> [Name]
468defined (PFix _ _ _) = []
469defined (PTy _ _ _ _ n t) = []
470defined (PPostulate _ _ _ _ n t) = []
471defined (PClauses _ _ n _) = [n] -- not a declaration
472defined (PCAF _ n _) = [n]
473defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
474 where fstt (_, a, _, _) = a
475defined (PData _ _ _ _ (PLaterdecl n _)) = []
476defined (PParams _ _ ds) = concatMap defined ds
477defined (PNamespace _ ds) = concatMap defined ds
478defined (PRecord _ _ _ n _ _ c _) = [n, c]
479defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
480defined (PInstance _ _ _ _ _ _ _ _) = []
481defined (PDSL n _) = [n]
482defined (PSyntax _ _) = []
483defined (PMutual _ ds) = concatMap defined ds
484defined (PDirective _) = []
487updateN :: [(Name, Name)] -> Name -> Name
488updateN ns n | Just n' <- lookup n ns = n'
491updateNs :: [(Name, Name)] -> PTerm -> PTerm
493updateNs ns t = mapPT updateRef t
494 where updateRef (PRef fc f) = PRef fc (updateN ns f)
497-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
499-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
500-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
501-- where updateCNs ns (PClause n l ts r ds)
502-- = PClause (updateN ns n) (fmap (updateNs ns) l)
503-- (map (fmap (updateNs ns)) ts)
504-- (fmap (updateNs ns) r)
505-- (map (updateDNs ns) ds)
508-- | High level language terms
509data PTerm = PQuote Raw
511 | PInferRef FC Name -- ^ A name to be defined later
513 | PLam Name PTerm PTerm
514 | PPi Plicity Name PTerm PTerm
515 | PLet Name PTerm PTerm PTerm
516 | PTyped PTerm PTerm -- ^ Term with explicit type
517 | PApp FC PTerm [PArg]
518 | PMatchApp FC Name -- ^ Make an application by type matching
519 | PCase FC PTerm [(PTerm, PTerm)]
525 | PRewrite FC PTerm PTerm (Maybe PTerm)
526 | PPair FC PTerm PTerm
527 | PDPair FC PTerm PTerm PTerm
528 | PAlternative Bool [PTerm] -- True if only one may work
529 | PHidden PTerm -- ^ Irrelevant or hidden pattern
531 | PGoal FC PTerm Name PTerm
538 | PProof [PTactic] -- ^ Proof script
539 | PTactics [PTactic] -- ^ As PProof, but no auto solving
540 | PElabError Err -- ^ Error to report on elaboration
541 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
542 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
543 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
546deriving instance Binary PTerm
549mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
550mapPT f t = f (mpt t) where
551 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
552 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
553 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
554 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
556 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
557 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
558 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
559 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
560 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
561 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
562 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
563 mpt (PHidden t) = PHidden (mapPT f t)
564 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
565 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
566 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
567 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
568 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
572data PTactic' t = Intro [Name] | Intros | Focus Name
573 | Refine Name [Bool] | Rewrite t
576 | LetTac Name t | LetTacTy Name t t
577 | Exact t | Compute | Trivial
580 | ProofState | ProofTerm | Undo
581 | Try (PTactic' t) (PTactic' t)
582 | TSeq (PTactic' t) (PTactic' t)
583 | ApplyTactic t -- see Language.Reflection module
586 | GoalType String (PTactic' t)
588 deriving (Show, Eq, Functor)
590deriving instance Binary PTactic'
593instance Sized a => Sized (PTactic' a) where
594 size (Intro nms) = 1 + size nms
596 size (Focus nm) = 1 + size nm
597 size (Refine nm bs) = 1 + size nm + length bs
598 size (Rewrite t) = 1 + size t
599 size (LetTac nm t) = 1 + size nm + size t
600 size (Exact t) = 1 + size t
608 size (Try l r) = 1 + size l + size r
609 size (TSeq l r) = 1 + size l + size r
610 size (ApplyTactic t) = 1 + size t
611 size (Reflect t) = 1 + size t
612 size (Fill t) = 1 + size t
616type PTactic = PTactic' PTerm
618data PDo' t = DoExp FC t
623 deriving (Eq, Functor)
625deriving instance Binary PDo'
628instance Sized a => Sized (PDo' a) where
629 size (DoExp fc t) = 1 + size fc + size t
630 size (DoBind fc nm t) = 1 + size fc + size nm + size t
631 size (DoBindP fc l r) = 1 + size fc + size l + size r
632 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
633 size (DoLetP fc l r) = 1 + size fc + size l + size r
637-- The priority gives a hint as to elaboration order. Best to elaborate
638-- things early which will help give a more concrete type to other
639-- variables, e.g. a before (interpTy a).
640-- TODO: priority no longer serves any purpose, drop it!
642data PArg' t = PImp { priority :: Int,
643 lazyarg :: Bool, pname :: Name, getTm :: t,
645 | PExp { priority :: Int,
646 lazyarg :: Bool, getTm :: t,
648 | PConstraint { priority :: Int,
649 lazyarg :: Bool, getTm :: t,
651 | PTacImplicit { priority :: Int,
652 lazyarg :: Bool, pname :: Name,
656 deriving (Show, Eq, Functor)
658instance Sized a => Sized (PArg' a) where
659 size (PImp p l nm trm _) = 1 + size nm + size trm
660 size (PExp p l trm _) = 1 + size trm
661 size (PConstraint p l trm _) = 1 + size trm
662 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
665deriving instance Binary PArg'
668pimp n t = PImp 0 True n t ""
669pexp t = PExp 0 False t ""
670pconst t = PConstraint 0 False t ""
671ptacimp n s t = PTacImplicit 0 True n s t ""
673type PArg = PArg' PTerm
677data ClassInfo = CI { instanceName :: Name,
678 class_methods :: [(Name, (FnOpts, PTerm))],
679 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
680 class_params :: [Name],
681 class_instances :: [Name] }
684deriving instance Binary ClassInfo
687data OptInfo = Optimise { collapsible :: Bool,
688 forceable :: [Int], -- argument positions
692deriving instance Binary OptInfo
696data TypeInfo = TI { con_names :: [Name],
701deriving instance Binary TypeInfo
704-- Syntactic sugar info
706data DSL' t = DSL { dsl_bind :: t,
711 index_first :: Maybe t,
712 index_next :: Maybe t,
713 dsl_lambda :: Maybe t,
716 deriving (Show, Functor)
718deriving instance Binary DSL'
723data SynContext = PatternSyntax | TermSyntax | AnySyntax
726deriving instance Binary SynContext
729data Syntax = Rule [SSymbol] PTerm SynContext
732deriving instance Binary Syntax
735data SSymbol = Keyword Name
742deriving instance Binary SSymbol
745initDSL = DSL (PRef f (UN ">>="))
746 (PRef f (UN "return"))
754 where f = FC "(builtin)" 0
756data Using = UImplicit Name PTerm
757 | UConstraint Name [Name]
760deriving instance Binary Using
763data SyntaxInfo = Syn { using :: [Using],
764 syn_params :: [(Name, PTerm)],
765 syn_namespace :: [String],
767 decoration :: Name -> Name,
769 implicitAllowed :: Bool,
773deriving instance Binary SyntaxInfo
776defaultSyntax = Syn [] [] [] [] id False False initDSL
778expandNS :: SyntaxInfo -> Name -> Name
779expandNS syn n@(NS _ _) = n
780expandNS syn n = case syn_namespace syn of
785--- Pretty printing declarations and terms
787instance Show PTerm where
788 show tm = showImp False tm
790instance Pretty PTerm where
791 pretty = prettyImp False
793instance Show PDecl where
794 show d = showDeclImp False d
796instance Show PClause where
797 show c = showCImp True c
799instance Show PData where
800 show d = showDImp False d
802showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
803showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
804showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
805showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
806showDeclImp _ (PData _ _ _ _ d) = show d
807showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
808 showSep "\n" (map show ps)
811showCImp :: Bool -> PClause -> String
812showCImp impl (PClause _ n l ws r w)
813 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
814 ++ " where " ++ show w
817 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
818showCImp impl (PWith _ n l ws r w)
819 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
820 ++ " { " ++ show w ++ " } "
823 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
826showDImp :: Bool -> PData -> String
827showDImp impl (PDatadecl n ty cons)
828 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
830 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
832getImps :: [PArg] -> [(Name, PTerm)]
834getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
835getImps (_ : xs) = getImps xs
837getExps :: [PArg] -> [PTerm]
839getExps (PExp _ _ tm _ : xs) = tm : getExps xs
840getExps (_ : xs) = getExps xs
842getConsts :: [PArg] -> [PTerm]
844getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
845getConsts (_ : xs) = getConsts xs
847getAll :: [PArg] -> [PTerm]
850prettyImp :: Bool -> PTerm -> Doc
851prettyImp impl = prettySe 10
853 prettySe p (PQuote r) =
854 if size r > breakingSize then
855 text "![" $$ pretty r <> text "]"
857 text "![" <> pretty r <> text "]"
858 prettySe p (PPatvar fc n) = pretty n
859 prettySe p (PRef fc n) =
865 prettyBasic n@(UN _) = pretty n
866 prettyBasic (MN _ s) = text s
867 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
868 prettySe p (PLam n ty sc) =
870 if size sc > breakingSize then
871 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
873 text "λ" <> pretty n <+> text "=>" <+> pretty sc
874 prettySe p (PLet n ty v sc) =
876 if size sc > breakingSize then
877 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
878 nest nestingSize (prettySe 10 sc)
880 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
882 prettySe p (PPi (Exp l s _) n ty sc)
883 | n `elem` allNamesIn sc || impl =
884 let open = if l then text "|" <> lparen else lparen in
886 if size sc > breakingSize then
887 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
888 st <+> text "->" $+$ prettySe 10 sc
890 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
891 st <+> text "->" <+> prettySe 10 sc
894 if size sc > breakingSize then
895 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
897 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
901 Static -> text "[static]"
903 prettySe p (PPi (Imp l s _) n ty sc)
905 let open = if l then text "|" <> lbrace else lbrace in
907 if size sc > breakingSize then
908 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
909 st <+> text "->" <+> prettySe 10 sc
911 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
912 st <+> text "->" <+> prettySe 10 sc
913 | otherwise = prettySe 10 sc
917 Static -> text $ "[static]"
919 prettySe p (PPi (Constraint _ _ _) n ty sc) =
921 if size sc > breakingSize then
922 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
924 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
925 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
927 if size sc > breakingSize then
928 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
929 rbrace <+> text "->" $+$ prettySe 10 sc
931 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
932 rbrace <+> text "->" <+> prettySe 10 sc
933 prettySe p (PApp _ (PRef _ f) [])
934 | not impl = pretty f
935 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
936 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
937 let [l, r] = getExps args in
939 if size r > breakingSize then
940 prettySe 1 l <+> pretty op $+$ prettySe 0 r
942 prettySe 1 l <+> pretty op <+> prettySe 0 r
943 prettySe p (PApp _ f as) =
944 let args = getExps as in
949 -- foldr (<+>) empty $ map prettyArgS as
951 foldl' fSe empty args
952 -- foldr (<+>) empty $ map prettyArgSe args
955 if size r > breakingSize then
956 l $+$ nest nestingSize (prettyArgS r)
961 if size r > breakingSize then
962 l $+$ nest nestingSize (prettyArgSe r)
965 prettySe p (PCase _ scr opts) =
966 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
968 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
970 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
971 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
972 prettySe p (PRefl _ _) = text "refl"
973 prettySe p (PResolveTC _) = text "resolvetc"
974 prettySe p (PTrue _) = text "()"
975 prettySe p (PFalse _) = text "_|_"
976 prettySe p (PEq _ l r) =
978 if size r > breakingSize then
979 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
981 prettySe 10 l <+> text "=" <+> prettySe 10 r
982 prettySe p (PRewrite _ l r _) =
984 if size r > breakingSize then
985 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
987 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
988 prettySe p (PTyped l r) =
989 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
990 prettySe p (PPair _ l r) =
991 if size r > breakingSize then
992 lparen <> prettySe 10 l <> text "," $+$
993 prettySe 10 r <> rparen
995 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
996 prettySe p (PDPair _ l t r) =
997 if size r > breakingSize then
998 lparen <> prettySe 10 l <+> text "**" $+$
999 prettySe 10 r <> rparen
1001 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1002 prettySe p (PAlternative a as) =
1003 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1006 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1007 prettySe p PType = text "Type"
1008 prettySe p (PConstant c) = pretty c
1009 -- XXX: add pretty for tactics
1010 prettySe p (PProof ts) =
1011 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1012 prettySe p (PTactics ts) =
1013 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1014 prettySe p (PMetavar n) = text "?" <> pretty n
1015 prettySe p (PReturn f) = text "return"
1016 prettySe p PImpossible = text "impossible"
1017 prettySe p Placeholder = text "_"
1018 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1019 prettySe p (PElabError s) = pretty s
1021 prettySe p _ = text "test"
1023 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
1024 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1025 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1026 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1028 prettyArgSe arg = prettySe 0 arg
1029 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1030 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1031 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1033 bracket outer inner doc
1034 | inner > outer = lparen <> doc <> rparen
1037showImp :: Bool -> PTerm -> String
1038showImp impl tm = se 10 tm where
1039 se p (PQuote r) = "![" ++ show r ++ "]"
1040 se p (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1041 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1042 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
1044 where showbasic n@(UN _) = show n
1045 showbasic (MN _ s) = s
1046 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1047 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
1048 (if impl then " : " ++ se 10 ty else "") ++ " => "
1050 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
1052 se p (PPi (Exp l s _) n ty sc)
1053 | n `elem` allNamesIn sc || impl
1055 (if l then "|(" else "(") ++
1056 show n ++ " : " ++ se 10 ty ++
1059 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
1060 where st = case s of
1061 Static -> "[static] "
1063 se p (PPi (Imp l s _) n ty sc)
1064 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1065 show n ++ " : " ++ se 10 ty ++
1066 "} " ++ st ++ "-> " ++ se 10 sc
1067 | otherwise = se 10 sc
1068 where st = case s of
1069 Static -> "[static] "
1071 se p (PPi (Constraint _ _ _) n ty sc)
1072 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
1073 se p (PPi (TacImp _ _ s _) n ty sc)
1074 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
1076 | Just str <- slist p e = str
1077 | Just num <- snat p e = show num
1078 se p (PMatchApp _ f) = "match " ++ show f
1079 se p (PApp _ (PRef _ f) [])
1081 se p (PApp _ (PRef _ op@(UN (f:_))) args)
1082 | length (getExps args) == 2 && not impl && not (isAlpha f)
1083 = let [l, r] = getExps args in
1084 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
1086 = let args = getExps as in
1087 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
1088 else concatMap seArg args
1089 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
1090 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
1091 se p (PHidden tm) = "." ++ se 0 tm
1094 | otherwise = "refl {" ++ se 10 t ++ "}"
1095 se p (PResolveTC _) = "resolvetc"
1096 se p (PTrue _) = "()"
1097 se p (PFalse _) = "_|_"
1098 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
1099 se p (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 l ++ " in " ++ se 10 r
1100 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
1101 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
1102 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
1103 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
1105 se p (PConstant c) = show c
1106 se p (PProof ts) = "proof { " ++ show ts ++ "}"
1107 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
1108 se p (PMetavar n) = "?" ++ show n
1109 se p (PReturn f) = "return"
1110 se p PImpossible = "impossible"
1111 se p Placeholder = "_"
1112 se p (PDoBlock _) = "do block show not implemented"
1113 se p (PElabError s) = show s
1114 se p (PCoerced t) = se p t
1115 se p (PUnifyLog t) = "%unifyLog " ++ se p t
1116 se p (PGoal f t n sc) = "quoteGoal " ++ show n ++ " by " ++ se 10 t ++
1118-- se p x = "Not implemented"
1120 slist' p (PApp _ (PRef _ nil) _)
1121 | nsroot nil == UN "Nil" = Just []
1122 slist' p (PApp _ (PRef _ cons) args)
1123 | nsroot cons == UN "::",
1124 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1126 Just tl' <- slist' p tl
1129 isImp (PImp {}) = True
1131 slist' _ _ = Nothing
1133 slist p e | Just es <- slist' p e = Just $
1134 case es of [] -> "[]"
1135 [x] -> "[" ++ se p x ++ "]"
1136 xs -> "[" ++ intercalate "," (map (se p) xs) ++ "]"
1139 -- since Prelude is always imported, S & Z are unqualified iff they're the
1142 | show o == (natns++"Z") || show o == "Z" = Just 0
1143 snat p (PApp _ s [PExp {getTm=n}])
1144 | show s == (natns++"S") || show s == "S",
1149 natns = "Prelude.Nat."
1151 sArg (PImp _ _ n tm _) = siArg (n, tm)
1152 sArg (PExp _ _ tm _) = seArg tm
1153 sArg (PConstraint _ _ tm _) = scArg tm
1154 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
1156 seArg arg = " " ++ se 0 arg
1157 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1158 scArg val = " {{" ++ se 10 val ++ "}}"
1159 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1161 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1165instance Sized PTerm where
1166 size (PQuote rawTerm) = size rawTerm
1167 size (PRef fc name) = size name
1168 size (PLam name ty bdy) = 1 + size ty + size bdy
1169 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1170 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1171 size (PTyped trm ty) = 1 + size trm + size ty
1172 size (PApp fc name args) = 1 + size args
1173 size (PCase fc trm bdy) = 1 + size trm + size bdy
1175 size (PFalse fc) = 1
1176 size (PRefl fc _) = 1
1177 size (PResolveTC fc) = 1
1178 size (PEq fc left right) = 1 + size left + size right
1179 size (PRewrite fc left right _) = 1 + size left + size right
1180 size (PPair fc left right) = 1 + size left + size right
1181 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1182 size (PAlternative a alts) = 1 + size alts
1183 size (PHidden hidden) = size hidden
1184 size (PUnifyLog 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 (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1208 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1209 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1210 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1211 ni env (PHidden tm) = ni env tm
1212 ni env (PEq _ l r) = ni env l ++ ni env r
1213 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1214 ni env (PTyped l r) = ni env l ++ ni env r
1215 ni env (PPair _ l r) = ni env l ++ ni env r
1216 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1217 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1218 ni env (PAlternative a ls) = concatMap (ni env) ls
1219 ni env (PUnifyLog tm) = ni env tm
1222-- Return names which are free in the given term.
1224namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1225namesIn uvars ist tm = nub $ ni [] tm
1228 | not (n `elem` env)
1229 = case lookupTy n (tt_ctxt ist) of
1231 _ -> if n `elem` (map fst uvars) then [n] else []
1232 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1233 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1234 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1235 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1236 ni env (PEq _ l r) = ni env l ++ ni env r
1237 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1238 ni env (PTyped l r) = ni env l ++ ni env r
1239 ni env (PPair _ l r) = ni env l ++ ni env r
1240 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1241 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1242 ni env (PAlternative a as) = concatMap (ni env) as
1243 ni env (PHidden tm) = ni env tm
1244 ni env (PUnifyLog tm) = ni env tm
1247-- Return which of the given names are used in the given term.
1249usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1250usedNamesIn vars ist tm = nub $ ni [] tm
1253 | n `elem` vars && not (n `elem` env)
1254 = case lookupTy n (tt_ctxt ist) of
1257 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1258 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1259 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1260 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1261 ni env (PEq _ l r) = ni env l ++ ni env r
1262 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1263 ni env (PTyped l r) = ni env l ++ ni env r
1264 ni env (PPair _ l r) = ni env l ++ ni env r
1265 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1266 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1267 ni env (PAlternative a as) = concatMap (ni env) as
1268 ni env (PHidden tm) = ni env tm
1269 ni env (PUnifyLog 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