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
208 | AddProof (Maybe Name)
220 | DynamicLink FilePath
229data Opt = Filename String
263 | OutputTy OutputType
264 | Extension LanguageExt
265 | InterpretScript String
266 | TargetTriple String
271-- Parsed declarations
273data Fixity = Infixl { prec :: Int }
274 | Infixr { prec :: Int }
275 | InfixN { prec :: Int }
276 | PrefixN { prec :: Int }
279deriving instance Binary Fixity
282instance Show Fixity where
283 show (Infixl i) = "infixl " ++ show i
284 show (Infixr i) = "infixr " ++ show i
285 show (InfixN i) = "infix " ++ show i
286 show (PrefixN i) = "prefix " ++ show i
288data FixDecl = Fix Fixity String
291instance Show FixDecl where
292 show (Fix f s) = show f ++ " " ++ s
295deriving instance Binary FixDecl
298instance Ord FixDecl where
299 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
302data Static = Static | Dynamic
305deriving instance Binary Static
308-- Mark bindings with their explicitness, and laziness
309data Plicity = Imp { plazy :: Bool,
312 | Exp { plazy :: Bool,
315 | Constraint { plazy :: Bool,
318 | TacImp { plazy :: Bool,
325deriving instance Binary Plicity
328impl = Imp False Dynamic ""
329expl = Exp False Dynamic ""
330constraint = Constraint False Dynamic ""
331tacimpl t = TacImp False Dynamic t ""
333data FnOpt = Inlinable | TotalFn | PartialFn
334 | Coinductive | AssertTotal | TCGen
335 | Implicit -- implicit coercion
336 | CExport String -- export, with a C name
337 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
340deriving instance Binary FnOpt
345inlinable :: FnOpts -> Bool
346inlinable = elem Inlinable
348-- | Top-level declarations such as compiler directives, definitions,
349-- datatypes and typeclasses.
351 = PFix FC Fixity [String] -- ^ Fixity declaration
352 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
353 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
354 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
355 | PCAF FC Name t -- ^ Top level constant
356 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
357 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
358 | PNamespace String [PDecl' t] -- ^ New namespace
359 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
360 | PClass String SyntaxInfo FC
363 [(Name, t)] -- parameters
364 [PDecl' t] -- declarations
365 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
366 -- class name, parameters, method declarations
367 | PInstance SyntaxInfo FC [t] -- constraints
370 t -- full instance type
371 (Maybe Name) -- explicit name
373 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
374 -- class name, parameters, full instance type, optional explicit name, and definitions
375 | PDSL Name (DSL' t) -- ^ DSL declaration
376 | PSyntax FC Syntax -- ^ Syntax definition
377 | PMutual FC [PDecl' t] -- ^ Mutual block
378 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
379 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
380 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
381 -- bool is True, lhs and rhs must be convertible
382 | PReflection FC Name -- type to reflect to
383 t -- type of variables
384 [PClause' t] -- ^ Pattern clauses
387deriving instance Binary PDecl'
390-- | One clause of a top-level definition. Term arguments to constructors are:
392-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
394-- 2. The list of patterns
396-- 3. The right-hand side
398-- 4. The where block (PDecl' t)
399data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
400 | PWith FC Name t [t] t [PDecl' t]
401 | PClauseR FC [t] t [PDecl' t]
402 | PWithR FC [t] t [PDecl' t]
405deriving instance Binary PClause'
409data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
410 d_tcon :: t, -- ^ Type constructor
411 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
413 -- ^ Data declaration
414 | PLaterdecl { d_name :: Name, d_tcon :: t }
415 -- ^ "Placeholder" for data whose constructors are defined later
418deriving instance Binary PData'
421-- Handy to get a free function for applying PTerm -> PTerm functions
422-- across a program, by deriving Functor
424type PDecl = PDecl' PTerm
425type PData = PData' PTerm
426type PClause = PClause' PTerm
428-- get all the names declared in a decl
430declared :: PDecl -> [Name]
431declared (PFix _ _ _) = []
432declared (PTy _ _ _ _ n t) = [n]
433declared (PPostulate _ _ _ _ n t) = [n]
434declared (PClauses _ _ n _) = [] -- not a declaration
435declared (PCAF _ n _) = [n]
436declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
437 where fstt (_, a, _, _) = a
438declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
439declared (PParams _ _ ds) = concatMap declared ds
440declared (PNamespace _ ds) = concatMap declared ds
441declared (PRecord _ _ _ n _ _ c _) = [n, c]
442declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
443declared (PInstance _ _ _ _ _ _ _ _) = []
444declared (PDSL n _) = [n]
445declared (PSyntax _ _) = []
446declared (PMutual _ ds) = concatMap declared ds
447declared (PDirective _) = []
449-- get the names declared, not counting nested parameter blocks
450tldeclared :: PDecl -> [Name]
451tldeclared (PFix _ _ _) = []
452tldeclared (PTy _ _ _ _ n t) = [n]
453tldeclared (PPostulate _ _ _ _ n t) = [n]
454tldeclared (PClauses _ _ n _) = [] -- not a declaration
455tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
456tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
457 where fstt (_, a, _, _) = a
458tldeclared (PParams _ _ ds) = []
459tldeclared (PMutual _ ds) = concatMap tldeclared ds
460tldeclared (PNamespace _ ds) = concatMap tldeclared ds
463defined :: PDecl -> [Name]
464defined (PFix _ _ _) = []
465defined (PTy _ _ _ _ n t) = []
466defined (PPostulate _ _ _ _ n t) = []
467defined (PClauses _ _ n _) = [n] -- not a declaration
468defined (PCAF _ n _) = [n]
469defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
470 where fstt (_, a, _, _) = a
471defined (PData _ _ _ _ (PLaterdecl n _)) = []
472defined (PParams _ _ ds) = concatMap defined ds
473defined (PNamespace _ ds) = concatMap defined ds
474defined (PRecord _ _ _ n _ _ c _) = [n, c]
475defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
476defined (PInstance _ _ _ _ _ _ _ _) = []
477defined (PDSL n _) = [n]
478defined (PSyntax _ _) = []
479defined (PMutual _ ds) = concatMap defined ds
480defined (PDirective _) = []
483updateN :: [(Name, Name)] -> Name -> Name
484updateN ns n | Just n' <- lookup n ns = n'
487updateNs :: [(Name, Name)] -> PTerm -> PTerm
489updateNs ns t = mapPT updateRef t
490 where updateRef (PRef fc f) = PRef fc (updateN ns f)
493-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
495-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
496-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
497-- where updateCNs ns (PClause n l ts r ds)
498-- = PClause (updateN ns n) (fmap (updateNs ns) l)
499-- (map (fmap (updateNs ns)) ts)
500-- (fmap (updateNs ns) r)
501-- (map (updateDNs ns) ds)
504-- | High level language terms
505data PTerm = PQuote Raw
507 | PInferRef FC Name -- ^ A name to be defined later
509 | PLam Name PTerm PTerm
510 | PPi Plicity Name PTerm PTerm
511 | PLet Name PTerm PTerm PTerm
512 | PTyped PTerm PTerm -- ^ Term with explicit type
513 | PApp FC PTerm [PArg]
514 | PMatchApp FC Name -- ^ Make an application by type matching
515 | PCase FC PTerm [(PTerm, PTerm)]
521 | PRewrite FC PTerm PTerm (Maybe PTerm)
522 | PPair FC PTerm PTerm
523 | PDPair FC PTerm PTerm PTerm
524 | PAlternative Bool [PTerm] -- True if only one may work
525 | PHidden PTerm -- ^ Irrelevant or hidden pattern
533 | PProof [PTactic] -- ^ Proof script
534 | PTactics [PTactic] -- ^ As PProof, but no auto solving
535 | PElabError Err -- ^ Error to report on elaboration
536 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
537 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
538 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
541deriving instance Binary PTerm
544mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
545mapPT f t = f (mpt t) where
546 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
547 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
548 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
549 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
551 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
552 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
553 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
554 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
555 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
556 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
557 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
558 mpt (PHidden t) = PHidden (mapPT f t)
559 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
560 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
561 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
562 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
566data PTactic' t = Intro [Name] | Intros | Focus Name
567 | Refine Name [Bool] | Rewrite t
570 | LetTac Name t | LetTacTy Name t t
571 | Exact t | Compute | Trivial
574 | ProofState | ProofTerm | Undo
575 | Try (PTactic' t) (PTactic' t)
576 | TSeq (PTactic' t) (PTactic' t)
577 | ApplyTactic t -- see Language.Reflection module
580 | GoalType String (PTactic' t)
582 deriving (Show, Eq, Functor)
584deriving instance Binary PTactic'
587instance Sized a => Sized (PTactic' a) where
588 size (Intro nms) = 1 + size nms
590 size (Focus nm) = 1 + size nm
591 size (Refine nm bs) = 1 + size nm + length bs
592 size (Rewrite t) = 1 + size t
593 size (LetTac nm t) = 1 + size nm + size t
594 size (Exact t) = 1 + size t
602 size (Try l r) = 1 + size l + size r
603 size (TSeq l r) = 1 + size l + size r
604 size (ApplyTactic t) = 1 + size t
605 size (Reflect t) = 1 + size t
606 size (Fill t) = 1 + size t
610type PTactic = PTactic' PTerm
612data PDo' t = DoExp FC t
617 deriving (Eq, Functor)
619deriving instance Binary PDo'
622instance Sized a => Sized (PDo' a) where
623 size (DoExp fc t) = 1 + size fc + size t
624 size (DoBind fc nm t) = 1 + size fc + size nm + size t
625 size (DoBindP fc l r) = 1 + size fc + size l + size r
626 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
627 size (DoLetP fc l r) = 1 + size fc + size l + size r
631-- The priority gives a hint as to elaboration order. Best to elaborate
632-- things early which will help give a more concrete type to other
633-- variables, e.g. a before (interpTy a).
634-- TODO: priority no longer serves any purpose, drop it!
636data PArg' t = PImp { priority :: Int,
637 lazyarg :: Bool, pname :: Name, getTm :: t,
639 | PExp { priority :: Int,
640 lazyarg :: Bool, getTm :: t,
642 | PConstraint { priority :: Int,
643 lazyarg :: Bool, getTm :: t,
645 | PTacImplicit { priority :: Int,
646 lazyarg :: Bool, pname :: Name,
650 deriving (Show, Eq, Functor)
652instance Sized a => Sized (PArg' a) where
653 size (PImp p l nm trm _) = 1 + size nm + size trm
654 size (PExp p l trm _) = 1 + size trm
655 size (PConstraint p l trm _) = 1 + size trm
656 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
659deriving instance Binary PArg'
662pimp n t = PImp 0 True n t ""
663pexp t = PExp 0 False t ""
664pconst t = PConstraint 0 False t ""
665ptacimp n s t = PTacImplicit 0 True n s t ""
667type PArg = PArg' PTerm
671data ClassInfo = CI { instanceName :: Name,
672 class_methods :: [(Name, (FnOpts, PTerm))],
673 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
674 class_params :: [Name],
675 class_instances :: [Name] }
678deriving instance Binary ClassInfo
681data OptInfo = Optimise { collapsible :: Bool,
682 forceable :: [Int], -- argument positions
686deriving instance Binary OptInfo
690data TypeInfo = TI { con_names :: [Name],
695deriving instance Binary TypeInfo
698-- Syntactic sugar info
700data DSL' t = DSL { dsl_bind :: t,
705 index_first :: Maybe t,
706 index_next :: Maybe t,
707 dsl_lambda :: Maybe t,
710 deriving (Show, Functor)
712deriving instance Binary DSL'
717data SynContext = PatternSyntax | TermSyntax | AnySyntax
720deriving instance Binary SynContext
723data Syntax = Rule [SSymbol] PTerm SynContext
726deriving instance Binary Syntax
729data SSymbol = Keyword Name
736deriving instance Binary SSymbol
739initDSL = DSL (PRef f (UN ">>="))
740 (PRef f (UN "return"))
748 where f = FC "(builtin)" 0
750data Using = UImplicit Name PTerm
751 | UConstraint Name [Name]
754deriving instance Binary Using
757data SyntaxInfo = Syn { using :: [Using],
758 syn_params :: [(Name, PTerm)],
759 syn_namespace :: [String],
761 decoration :: Name -> Name,
763 implicitAllowed :: Bool,
767deriving instance Binary SyntaxInfo
770defaultSyntax = Syn [] [] [] [] id False False initDSL
772expandNS :: SyntaxInfo -> Name -> Name
773expandNS syn n@(NS _ _) = n
774expandNS syn n = case syn_namespace syn of
779--- Pretty printing declarations and terms
781instance Show PTerm where
782 show tm = showImp False tm
784instance Pretty PTerm where
785 pretty = prettyImp False
787instance Show PDecl where
788 show d = showDeclImp False d
790instance Show PClause where
791 show c = showCImp True c
793instance Show PData where
794 show d = showDImp False d
796showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
797showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
798showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
799showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
800showDeclImp _ (PData _ _ _ _ d) = show d
801showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
802 showSep "\n" (map show ps)
805showCImp :: Bool -> PClause -> String
806showCImp impl (PClause _ n l ws r w)
807 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
808 ++ " where " ++ show w
811 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
812showCImp impl (PWith _ n l ws r w)
813 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
814 ++ " { " ++ show w ++ " } "
817 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
820showDImp :: Bool -> PData -> String
821showDImp impl (PDatadecl n ty cons)
822 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
824 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
826getImps :: [PArg] -> [(Name, PTerm)]
828getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
829getImps (_ : xs) = getImps xs
831getExps :: [PArg] -> [PTerm]
833getExps (PExp _ _ tm _ : xs) = tm : getExps xs
834getExps (_ : xs) = getExps xs
836getConsts :: [PArg] -> [PTerm]
838getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
839getConsts (_ : xs) = getConsts xs
841getAll :: [PArg] -> [PTerm]
844prettyImp :: Bool -> PTerm -> Doc
845prettyImp impl = prettySe 10
847 prettySe p (PQuote r) =
848 if size r > breakingSize then
849 text "![" $$ pretty r <> text "]"
851 text "![" <> pretty r <> text "]"
852 prettySe p (PPatvar fc n) = pretty n
853 prettySe p (PRef fc n) =
859 prettyBasic n@(UN _) = pretty n
860 prettyBasic (MN _ s) = text s
861 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
862 prettySe p (PLam n ty sc) =
864 if size sc > breakingSize then
865 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
867 text "λ" <> pretty n <+> text "=>" <+> pretty sc
868 prettySe p (PLet n ty v sc) =
870 if size sc > breakingSize then
871 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
872 nest nestingSize (prettySe 10 sc)
874 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
876 prettySe p (PPi (Exp l s _) n ty sc)
877 | n `elem` allNamesIn sc || impl =
878 let open = if l then text "|" <> lparen else lparen in
880 if size sc > breakingSize then
881 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
882 st <+> text "->" $+$ prettySe 10 sc
884 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
885 st <+> text "->" <+> prettySe 10 sc
888 if size sc > breakingSize then
889 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
891 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
895 Static -> text "[static]"
897 prettySe p (PPi (Imp l s _) n ty sc)
899 let open = if l then text "|" <> lbrace else lbrace in
901 if size sc > breakingSize then
902 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
903 st <+> text "->" <+> prettySe 10 sc
905 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
906 st <+> text "->" <+> prettySe 10 sc
907 | otherwise = prettySe 10 sc
911 Static -> text $ "[static]"
913 prettySe p (PPi (Constraint _ _ _) n ty sc) =
915 if size sc > breakingSize then
916 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
918 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
919 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
921 if size sc > breakingSize then
922 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
923 rbrace <+> text "->" $+$ prettySe 10 sc
925 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
926 rbrace <+> text "->" <+> prettySe 10 sc
927 prettySe p (PApp _ (PRef _ f) [])
928 | not impl = pretty f
929 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
930 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
931 let [l, r] = getExps args in
933 if size r > breakingSize then
934 prettySe 1 l <+> pretty op $+$ prettySe 0 r
936 prettySe 1 l <+> pretty op <+> prettySe 0 r
937 prettySe p (PApp _ f as) =
938 let args = getExps as in
943 -- foldr (<+>) empty $ map prettyArgS as
945 foldl' fSe empty args
946 -- foldr (<+>) empty $ map prettyArgSe args
949 if size r > breakingSize then
950 l $+$ nest nestingSize (prettyArgS r)
955 if size r > breakingSize then
956 l $+$ nest nestingSize (prettyArgSe r)
959 prettySe p (PCase _ scr opts) =
960 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
962 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
964 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
965 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
966 prettySe p (PRefl _ _) = text "refl"
967 prettySe p (PResolveTC _) = text "resolvetc"
968 prettySe p (PTrue _) = text "()"
969 prettySe p (PFalse _) = text "_|_"
970 prettySe p (PEq _ l r) =
972 if size r > breakingSize then
973 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
975 prettySe 10 l <+> text "=" <+> prettySe 10 r
976 prettySe p (PRewrite _ l r _) =
978 if size r > breakingSize then
979 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
981 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
982 prettySe p (PTyped l r) =
983 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
984 prettySe p (PPair _ l r) =
985 if size r > breakingSize then
986 lparen <> prettySe 10 l <> text "," $+$
987 prettySe 10 r <> rparen
989 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
990 prettySe p (PDPair _ l t 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 (PAlternative a as) =
997 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1000 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1001 prettySe p PType = text "Type"
1002 prettySe p (PConstant c) = pretty c
1003 -- XXX: add pretty for tactics
1004 prettySe p (PProof ts) =
1005 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1006 prettySe p (PTactics ts) =
1007 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1008 prettySe p (PMetavar n) = text "?" <> pretty n
1009 prettySe p (PReturn f) = text "return"
1010 prettySe p PImpossible = text "impossible"
1011 prettySe p Placeholder = text "_"
1012 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1013 prettySe p (PElabError s) = pretty s
1015 prettySe p _ = text "test"
1017 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
1018 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1019 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1020 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1022 prettyArgSe arg = prettySe 0 arg
1023 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1024 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1025 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1027 bracket outer inner doc
1028 | inner > outer = lparen <> doc <> rparen
1031showImp :: Bool -> PTerm -> String
1032showImp impl tm = se 10 tm where
1033 se p (PQuote r) = "![" ++ show r ++ "]"
1034 se p (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1035 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1036 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
1038 where showbasic n@(UN _) = show n
1039 showbasic (MN _ s) = s
1040 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1041 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
1042 (if impl then " : " ++ se 10 ty else "") ++ " => "
1044 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
1046 se p (PPi (Exp l s _) n ty sc)
1047 | n `elem` allNamesIn sc || impl
1049 (if l then "|(" else "(") ++
1050 show n ++ " : " ++ se 10 ty ++
1053 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
1054 where st = case s of
1055 Static -> "[static] "
1057 se p (PPi (Imp l s _) n ty sc)
1058 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1059 show n ++ " : " ++ se 10 ty ++
1060 "} " ++ st ++ "-> " ++ se 10 sc
1061 | otherwise = se 10 sc
1062 where st = case s of
1063 Static -> "[static] "
1065 se p (PPi (Constraint _ _ _) n ty sc)
1066 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
1067 se p (PPi (TacImp _ _ s _) n ty sc)
1068 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
1070 | Just str <- slist p e = str
1071 | Just num <- snat p e = show num
1072 se p (PMatchApp _ f) = "match " ++ show f
1073 se p (PApp _ (PRef _ f) [])
1075 se p (PApp _ (PRef _ op@(UN (f:_))) args)
1076 | length (getExps args) == 2 && not impl && not (isAlpha f)
1077 = let [l, r] = getExps args in
1078 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
1080 = let args = getExps as in
1081 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
1082 else concatMap seArg args
1083 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
1084 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
1085 se p (PHidden tm) = "." ++ se 0 tm
1088 | otherwise = "refl {" ++ se 10 t ++ "}"
1089 se p (PResolveTC _) = "resolvetc"
1090 se p (PTrue _) = "()"
1091 se p (PFalse _) = "_|_"
1092 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
1093 se p (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 l ++ " in " ++ se 10 r
1094 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
1095 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
1096 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
1097 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
1099 se p (PConstant c) = show c
1100 se p (PProof ts) = "proof { " ++ show ts ++ "}"
1101 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
1102 se p (PMetavar n) = "?" ++ show n
1103 se p (PReturn f) = "return"
1104 se p PImpossible = "impossible"
1105 se p Placeholder = "_"
1106 se p (PDoBlock _) = "do block show not implemented"
1107 se p (PElabError s) = show s
1108 se p (PCoerced t) = se p t
1109 se p (PUnifyLog t) = "%unifyLog " ++ se p t
1110-- se p x = "Not implemented"
1112 slist' p (PApp _ (PRef _ nil) _)
1113 | nsroot nil == UN "Nil" = Just []
1114 slist' p (PApp _ (PRef _ cons) args)
1115 | nsroot cons == UN "::",
1116 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1118 Just tl' <- slist' p tl
1121 isImp (PImp {}) = True
1123 slist' _ _ = Nothing
1125 slist p e | Just es <- slist' p e = Just $
1126 case es of [] -> "[]"
1127 [x] -> "[" ++ se p x ++ "]"
1128 xs -> "[" ++ intercalate "," (map (se p) xs) ++ "]"
1131 -- since Prelude is always imported, S & Z are unqualified iff they're the
1134 | show o == (natns++"Z") || show o == "Z" = Just 0
1135 snat p (PApp _ s [PExp {getTm=n}])
1136 | show s == (natns++"S") || show s == "S",
1141 natns = "Prelude.Nat."
1143 sArg (PImp _ _ n tm _) = siArg (n, tm)
1144 sArg (PExp _ _ tm _) = seArg tm
1145 sArg (PConstraint _ _ tm _) = scArg tm
1146 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
1148 seArg arg = " " ++ se 0 arg
1149 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1150 scArg val = " {{" ++ se 10 val ++ "}}"
1151 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1153 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1157instance Sized PTerm where
1158 size (PQuote rawTerm) = size rawTerm
1159 size (PRef fc name) = size name
1160 size (PLam name ty bdy) = 1 + size ty + size bdy
1161 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1162 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1163 size (PTyped trm ty) = 1 + size trm + size ty
1164 size (PApp fc name args) = 1 + size args
1165 size (PCase fc trm bdy) = 1 + size trm + size bdy
1167 size (PFalse fc) = 1
1168 size (PRefl fc _) = 1
1169 size (PResolveTC fc) = 1
1170 size (PEq fc left right) = 1 + size left + size right
1171 size (PRewrite fc left right _) = 1 + size left + size right
1172 size (PPair fc left right) = 1 + size left + size right
1173 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1174 size (PAlternative a alts) = 1 + size alts
1175 size (PHidden hidden) = size hidden
1176 size (PUnifyLog tm) = size tm
1178 size (PConstant const) = 1 + size const
1179 size Placeholder = 1
1180 size (PDoBlock dos) = 1 + size dos
1181 size (PIdiom fc term) = 1 + size term
1182 size (PReturn fc) = 1
1183 size (PMetavar name) = 1
1184 size (PProof tactics) = size tactics
1185 size (PElabError err) = size err
1186 size PImpossible = 1
1188getPArity :: PTerm -> Int
1189getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1192-- Return all names, free or globally bound, in the given term.
1194allNamesIn :: PTerm -> [Name]
1195allNamesIn tm = nub $ ni [] tm
1198 | not (n `elem` env) = [n]
1199 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1200 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1201 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1202 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1203 ni env (PHidden tm) = ni env tm
1204 ni env (PEq _ l r) = ni env l ++ ni env r
1205 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1206 ni env (PTyped l r) = ni env l ++ ni env r
1207 ni env (PPair _ l r) = ni env l ++ ni env r
1208 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1209 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1210 ni env (PAlternative a ls) = concatMap (ni env) ls
1211 ni env (PUnifyLog tm) = ni env tm
1214-- Return names which are free in the given term.
1216namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1217namesIn uvars ist tm = nub $ ni [] tm
1220 | not (n `elem` env)
1221 = case lookupTy n (tt_ctxt ist) of
1223 _ -> if n `elem` (map fst uvars) then [n] else []
1224 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1225 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1226 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1227 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1228 ni env (PEq _ l r) = ni env l ++ ni env r
1229 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1230 ni env (PTyped l r) = ni env l ++ ni env r
1231 ni env (PPair _ l r) = ni env l ++ ni env r
1232 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1233 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1234 ni env (PAlternative a as) = concatMap (ni env) as
1235 ni env (PHidden tm) = ni env tm
1236 ni env (PUnifyLog tm) = ni env tm
1239-- Return which of the given names are used in the given term.
1241usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1242usedNamesIn vars ist tm = nub $ ni [] tm
1245 | n `elem` vars && not (n `elem` env)
1246 = case lookupTy n (tt_ctxt ist) of
1249 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1250 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1251 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1252 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1253 ni env (PEq _ l r) = ni env l ++ ni env r
1254 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1255 ni env (PTyped l r) = ni env l ++ ni env r
1256 ni env (PPair _ l r) = ni env l ++ ni env r
1257 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1258 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1259 ni env (PAlternative a as) = concatMap (ni env) as
1260 ni env (PHidden tm) = ni env tm
1261 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