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,
37 opt_outputTy :: OutputType,
38 opt_ibcsubdir :: FilePath,
39 opt_importdirs :: [FilePath],
40 opt_cmdline :: [Opt] -- remember whole command line
44defaultOpts = IOption { opt_logLevel = 0
45 , opt_typecase = False
46 , opt_typeintype = False
49 , opt_errContext = False
54 , opt_outputTy = Executable
60data LanguageExt = TypeProviders deriving (Show, Eq, Read, Ord)
62-- | The output mode in use
63data OutputMode = RawOutput | IdeSlave Integer deriving Show
65-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
66-- without typechecking).
67-- This will include all the functions and data declarations, plus fixity declarations
70-- | The global state used in the Idris monad
72 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
73 idris_constraints :: [(UConstraint, FC)],
74 -- ^ A list of universe constraints and their corresponding source locations
75 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
76 idris_implicits :: Ctxt [PArg],
77 idris_statics :: Ctxt [Bool],
78 idris_classes :: Ctxt ClassInfo,
79 idris_dsls :: Ctxt DSL,
80 idris_optimisation :: Ctxt OptInfo,
81 idris_datatypes :: Ctxt TypeInfo,
82 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
83 -- ^ list of lhs/rhs, and a list of missing clauses
84 idris_flags :: Ctxt [FnOpt],
85 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
86 idris_calledgraph :: Ctxt [Name],
87 idris_docstrings :: Ctxt String,
88 idris_totcheck :: [(FC, Name)],
90 idris_options :: IOption,
92 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
93 idris_coercions :: [Name],
94 syntax_rules :: [Syntax],
95 syntax_keywords :: [String],
96 imported :: [FilePath], -- ^ The imported modules
97 idris_scprims :: [(Name, (Int, PrimFn))],
98 idris_objs :: [(Target, FilePath)],
99 idris_libs :: [(Target, String)],
100 idris_hdrs :: [(Target, String)],
101 proof_list :: [(Name, [String])],
102 errLine :: Maybe Int,
103 lastParse :: Maybe Name,
104 indent_stack :: [Int],
105 brace_stack :: [Maybe Int],
106 hide_list :: [(Name, Maybe Accessibility)],
107 default_access :: Accessibility,
108 default_total :: Bool,
109 ibc_write :: [IBCWrite],
110 compiled_so :: Maybe String,
111 idris_dynamic_libs :: [DynamicLib],
112 idris_language_extensions :: [LanguageExt],
113 idris_outputmode :: OutputMode
116data SizeChange = Smaller | Same | Bigger | Unknown
119deriving instance Binary SizeChange
122type SCGEntry = (Name, [Maybe (Int, SizeChange)])
124data CGInfo = CGInfo { argsdef :: [Name],
125 calls :: [(Name, [[Name]])],
131deriving instance Binary CGInfo
134primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
136-- information that needs writing for the current module's .ibc file
137data IBCWrite = IBCFix FixDecl
141 | IBCInstance Bool Name Name
148 | IBCObj Target FilePath
149 | IBCLib Target String
151 | IBCHeader Target String
152 | IBCAccess Name Accessibility
153 | IBCTotal Name Totality
154 | IBCFlags Name [FnOpt]
158 | IBCDef Name -- i.e. main context
161idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
162 emptyContext emptyContext emptyContext emptyContext
163 emptyContext emptyContext emptyContext emptyContext
164 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] []
165 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
167-- | The monad for the main REPL - reading and processing files and updating
168-- global state (hence the IO inner monad).
169--type Idris = WriterT [Either String (IO ())] (State IState a))
170type Idris = StateT IState IO
172-- Commands in the REPL
190 | ChangeDirectory FilePath
193 | Compile Target String
199 | AddProof (Maybe Name)
211 | DynamicLink FilePath
220data Opt = Filename String
254 | OutputTy OutputType
255 | Extension LanguageExt
258-- Parsed declarations
260data Fixity = Infixl { prec :: Int }
261 | Infixr { prec :: Int }
262 | InfixN { prec :: Int }
263 | PrefixN { prec :: Int }
266deriving instance Binary Fixity
269instance Show Fixity where
270 show (Infixl i) = "infixl " ++ show i
271 show (Infixr i) = "infixr " ++ show i
272 show (InfixN i) = "infix " ++ show i
273 show (PrefixN i) = "prefix " ++ show i
275data FixDecl = Fix Fixity String
278instance Show FixDecl where
279 show (Fix f s) = show f ++ " " ++ s
282deriving instance Binary FixDecl
285instance Ord FixDecl where
286 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
289data Static = Static | Dynamic
292deriving instance Binary Static
295-- Mark bindings with their explicitness, and laziness
296data Plicity = Imp { plazy :: Bool,
299 | Exp { plazy :: Bool,
302 | Constraint { plazy :: Bool,
305 | TacImp { plazy :: Bool,
312deriving instance Binary Plicity
315impl = Imp False Dynamic ""
316expl = Exp False Dynamic ""
317constraint = Constraint False Dynamic ""
318tacimpl t = TacImp False Dynamic t ""
320data FnOpt = Inlinable | TotalFn | PartialFn
321 | Coinductive | AssertTotal | TCGen
322 | Implicit -- implicit coercion
323 | CExport String -- export, with a C name
324 | Specialise [Name] -- specialise it, freeze these names
327deriving instance Binary FnOpt
332inlinable :: FnOpts -> Bool
333inlinable = elem Inlinable
335-- | Top-level declarations such as compiler directives, definitions,
336-- datatypes and typeclasses.
338 = PFix FC Fixity [String] -- ^ Fixity declaration
339 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
340 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
341 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
342 | PCAF FC Name t -- ^ Top level constant
343 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
344 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
345 | PNamespace String [PDecl' t] -- ^ New namespace
346 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
347 | PClass String SyntaxInfo FC
350 [(Name, t)] -- parameters
351 [PDecl' t] -- declarations
352 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
353 -- class name, parameters, method declarations
354 | PInstance SyntaxInfo FC [t] -- constraints
357 t -- full instance type
358 (Maybe Name) -- explicit name
360 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
361 -- class name, parameters, full instance type, optional explicit name, and definitions
362 | PDSL Name (DSL' t) -- ^ DSL declaration
363 | PSyntax FC Syntax -- ^ Syntax definition
364 | PMutual FC [PDecl' t] -- ^ Mutual block
365 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
366 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
369deriving instance Binary PDecl'
372-- | One clause of a top-level definition. Term arguments to constructors are:
374-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
376-- 2. The list of patterns
378-- 3. The right-hand side
380-- 4. The where block (PDecl' t)
381data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
382 | PWith FC Name t [t] t [PDecl' t]
383 | PClauseR FC [t] t [PDecl' t]
384 | PWithR FC [t] t [PDecl' t]
387deriving instance Binary PClause'
391data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
392 d_tcon :: t, -- ^ Type constructor
393 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
395 -- ^ Data declaration
396 | PLaterdecl { d_name :: Name, d_tcon :: t }
397 -- ^ "Placeholder" for data whose constructors are defined later
400deriving instance Binary PData'
403-- Handy to get a free function for applying PTerm -> PTerm functions
404-- across a program, by deriving Functor
406type PDecl = PDecl' PTerm
407type PData = PData' PTerm
408type PClause = PClause' PTerm
410-- get all the names declared in a decl
412declared :: PDecl -> [Name]
413declared (PFix _ _ _) = []
414declared (PTy _ _ _ _ n t) = [n]
415declared (PPostulate _ _ _ _ n t) = [n]
416declared (PClauses _ _ n _) = [] -- not a declaration
417declared (PCAF _ n _) = [n]
418declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
419 where fstt (_, a, _, _) = a
420declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
421declared (PParams _ _ ds) = concatMap declared ds
422declared (PNamespace _ ds) = concatMap declared ds
423declared (PRecord _ _ _ n _ _ c _) = [n, c]
424declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
425declared (PInstance _ _ _ _ _ _ _ _) = []
426declared (PDSL n _) = [n]
427declared (PSyntax _ _) = []
428declared (PMutual _ ds) = concatMap declared ds
429declared (PDirective _) = []
431-- get the names declared, not counting nested parameter blocks
432tldeclared :: PDecl -> [Name]
433tldeclared (PFix _ _ _) = []
434tldeclared (PTy _ _ _ _ n t) = [n]
435tldeclared (PPostulate _ _ _ _ n t) = [n]
436tldeclared (PClauses _ _ n _) = [] -- not a declaration
437tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
438tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
439 where fstt (_, a, _, _) = a
440tldeclared (PParams _ _ ds) = []
441tldeclared (PMutual _ ds) = concatMap tldeclared ds
442tldeclared (PNamespace _ ds) = concatMap tldeclared ds
445defined :: PDecl -> [Name]
446defined (PFix _ _ _) = []
447defined (PTy _ _ _ _ n t) = []
448defined (PPostulate _ _ _ _ n t) = []
449defined (PClauses _ _ n _) = [n] -- not a declaration
450defined (PCAF _ n _) = [n]
451defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
452 where fstt (_, a, _, _) = a
453defined (PData _ _ _ _ (PLaterdecl n _)) = []
454defined (PParams _ _ ds) = concatMap defined ds
455defined (PNamespace _ ds) = concatMap defined ds
456defined (PRecord _ _ _ n _ _ c _) = [n, c]
457defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
458defined (PInstance _ _ _ _ _ _ _ _) = []
459defined (PDSL n _) = [n]
460defined (PSyntax _ _) = []
461defined (PMutual _ ds) = concatMap defined ds
462defined (PDirective _) = []
465updateN :: [(Name, Name)] -> Name -> Name
466updateN ns n | Just n' <- lookup n ns = n'
469updateNs :: [(Name, Name)] -> PTerm -> PTerm
471updateNs ns t = mapPT updateRef t
472 where updateRef (PRef fc f) = PRef fc (updateN ns f)
475-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
477-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
478-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
479-- where updateCNs ns (PClause n l ts r ds)
480-- = PClause (updateN ns n) (fmap (updateNs ns) l)
481-- (map (fmap (updateNs ns)) ts)
482-- (fmap (updateNs ns) r)
483-- (map (updateDNs ns) ds)
486-- | High level language terms
487data PTerm = PQuote Raw
489 | PInferRef FC Name -- ^ A name to be defined later
491 | PLam Name PTerm PTerm
492 | PPi Plicity Name PTerm PTerm
493 | PLet Name PTerm PTerm PTerm
494 | PTyped PTerm PTerm -- ^ Term with explicit type
495 | PApp FC PTerm [PArg]
496 | PMatchApp FC Name -- ^ Make an application by type matching
497 | PCase FC PTerm [(PTerm, PTerm)]
503 | PRewrite FC PTerm PTerm
504 | PPair FC PTerm PTerm
505 | PDPair FC PTerm PTerm PTerm
506 | PAlternative Bool [PTerm] -- True if only one may work
507 | PHidden PTerm -- ^ Irrelevant or hidden pattern
515 | PProof [PTactic] -- ^ Proof script
516 | PTactics [PTactic] -- ^ As PProof, but no auto solving
517 | PElabError Err -- ^ Error to report on elaboration
518 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
519 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
520 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
523deriving instance Binary PTerm
526mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
527mapPT f t = f (mpt t) where
528 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
529 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
530 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
531 mpt (PRewrite fc t s) = PRewrite fc (mapPT f t) (mapPT f s)
532 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
533 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
534 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
535 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
536 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
537 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
538 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
539 mpt (PHidden t) = PHidden (mapPT f t)
540 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
541 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
542 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
543 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
547data PTactic' t = Intro [Name] | Intros | Focus Name
548 | Refine Name [Bool] | Rewrite t
550 | LetTac Name t | LetTacTy Name t t
551 | Exact t | Compute | Trivial
554 | ProofState | ProofTerm | Undo
555 | Try (PTactic' t) (PTactic' t)
556 | TSeq (PTactic' t) (PTactic' t)
557 | ApplyTactic t -- see Language.Reflection module
560 | GoalType String (PTactic' t)
562 deriving (Show, Eq, Functor)
564deriving instance Binary PTactic'
567instance Sized a => Sized (PTactic' a) where
568 size (Intro nms) = 1 + size nms
570 size (Focus nm) = 1 + size nm
571 size (Refine nm bs) = 1 + size nm + length bs
572 size (Rewrite t) = 1 + size t
573 size (LetTac nm t) = 1 + size nm + size t
574 size (Exact t) = 1 + size t
582 size (Try l r) = 1 + size l + size r
583 size (TSeq l r) = 1 + size l + size r
584 size (ApplyTactic t) = 1 + size t
585 size (Reflect t) = 1 + size t
586 size (Fill t) = 1 + size t
590type PTactic = PTactic' PTerm
592data PDo' t = DoExp FC t
597 deriving (Eq, Functor)
599deriving instance Binary PDo'
602instance Sized a => Sized (PDo' a) where
603 size (DoExp fc t) = 1 + size fc + size t
604 size (DoBind fc nm t) = 1 + size fc + size nm + size t
605 size (DoBindP fc l r) = 1 + size fc + size l + size r
606 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
607 size (DoLetP fc l r) = 1 + size fc + size l + size r
611-- The priority gives a hint as to elaboration order. Best to elaborate
612-- things early which will help give a more concrete type to other
613-- variables, e.g. a before (interpTy a).
614-- TODO: priority no longer serves any purpose, drop it!
616data PArg' t = PImp { priority :: Int,
617 lazyarg :: Bool, pname :: Name, getTm :: t,
619 | PExp { priority :: Int,
620 lazyarg :: Bool, getTm :: t,
622 | PConstraint { priority :: Int,
623 lazyarg :: Bool, getTm :: t,
625 | PTacImplicit { priority :: Int,
626 lazyarg :: Bool, pname :: Name,
630 deriving (Show, Eq, Functor)
632instance Sized a => Sized (PArg' a) where
633 size (PImp p l nm trm _) = 1 + size nm + size trm
634 size (PExp p l trm _) = 1 + size trm
635 size (PConstraint p l trm _) = 1 + size trm
636 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
639deriving instance Binary PArg'
642pimp n t = PImp 0 True n t ""
643pexp t = PExp 0 False t ""
644pconst t = PConstraint 0 False t ""
645ptacimp n s t = PTacImplicit 0 True n s t ""
647type PArg = PArg' PTerm
651data ClassInfo = CI { instanceName :: Name,
652 class_methods :: [(Name, (FnOpts, PTerm))],
653 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
654 class_params :: [Name],
655 class_instances :: [Name] }
658deriving instance Binary ClassInfo
661data OptInfo = Optimise { collapsible :: Bool,
662 forceable :: [Int], -- argument positions
666deriving instance Binary OptInfo
670data TypeInfo = TI { con_names :: [Name],
675deriving instance Binary TypeInfo
678-- Syntactic sugar info
680data DSL' t = DSL { dsl_bind :: t,
685 index_first :: Maybe t,
686 index_next :: Maybe t,
687 dsl_lambda :: Maybe t,
690 deriving (Show, Functor)
692deriving instance Binary DSL'
697data SynContext = PatternSyntax | TermSyntax | AnySyntax
700deriving instance Binary SynContext
703data Syntax = Rule [SSymbol] PTerm SynContext
706deriving instance Binary Syntax
709data SSymbol = Keyword Name
716deriving instance Binary SSymbol
719initDSL = DSL (PRef f (UN ">>="))
720 (PRef f (UN "return"))
728 where f = FC "(builtin)" 0
730data Using = UImplicit Name PTerm
731 | UConstraint Name [Name]
734deriving instance Binary Using
737data SyntaxInfo = Syn { using :: [Using],
738 syn_params :: [(Name, PTerm)],
739 syn_namespace :: [String],
741 decoration :: Name -> Name,
743 implicitAllowed :: Bool,
747deriving instance Binary SyntaxInfo
750defaultSyntax = Syn [] [] [] [] id False False initDSL
752expandNS :: SyntaxInfo -> Name -> Name
753expandNS syn n@(NS _ _) = n
754expandNS syn n = case syn_namespace syn of
759--- Pretty printing declarations and terms
761instance Show PTerm where
762 show tm = showImp False tm
764instance Pretty PTerm where
765 pretty = prettyImp False
767instance Show PDecl where
768 show d = showDeclImp False d
770instance Show PClause where
771 show c = showCImp True c
773instance Show PData where
774 show d = showDImp False d
776showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
777showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
778showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
779showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
780showDeclImp _ (PData _ _ _ _ d) = show d
781showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
782 showSep "\n" (map show ps)
785showCImp :: Bool -> PClause -> String
786showCImp impl (PClause _ n l ws r w)
787 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
788 ++ " where " ++ show w
791 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
792showCImp impl (PWith _ n l ws r w)
793 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
794 ++ " { " ++ show w ++ " } "
797 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
800showDImp :: Bool -> PData -> String
801showDImp impl (PDatadecl n ty cons)
802 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
804 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
806getImps :: [PArg] -> [(Name, PTerm)]
808getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
809getImps (_ : xs) = getImps xs
811getExps :: [PArg] -> [PTerm]
813getExps (PExp _ _ tm _ : xs) = tm : getExps xs
814getExps (_ : xs) = getExps xs
816getConsts :: [PArg] -> [PTerm]
818getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
819getConsts (_ : xs) = getConsts xs
821getAll :: [PArg] -> [PTerm]
824prettyImp :: Bool -> PTerm -> Doc
825prettyImp impl = prettySe 10
827 prettySe p (PQuote r) =
828 if size r > breakingSize then
829 text "![" $$ pretty r <> text "]"
831 text "![" <> pretty r <> text "]"
832 prettySe p (PPatvar fc n) = pretty n
833 prettySe p (PRef fc n) =
839 prettyBasic n@(UN _) = pretty n
840 prettyBasic (MN _ s) = text s
841 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
842 prettySe p (PLam n ty sc) =
844 if size sc > breakingSize then
845 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
847 text "λ" <> pretty n <+> text "=>" <+> pretty sc
848 prettySe p (PLet n ty v sc) =
850 if size sc > breakingSize then
851 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
852 nest nestingSize (prettySe 10 sc)
854 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
856 prettySe p (PPi (Exp l s _) n ty sc)
857 | n `elem` allNamesIn sc || impl =
858 let open = if l then text "|" <> lparen else lparen in
860 if size sc > breakingSize then
861 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
862 st <+> text "->" $+$ prettySe 10 sc
864 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
865 st <+> text "->" <+> prettySe 10 sc
868 if size sc > breakingSize then
869 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
871 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
875 Static -> text "[static]"
877 prettySe p (PPi (Imp l s _) n ty sc)
879 let open = if l then text "|" <> lbrace else lbrace in
881 if size sc > breakingSize then
882 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
883 st <+> text "->" <+> prettySe 10 sc
885 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
886 st <+> text "->" <+> prettySe 10 sc
887 | otherwise = prettySe 10 sc
891 Static -> text $ "[static]"
893 prettySe p (PPi (Constraint _ _ _) n ty sc) =
895 if size sc > breakingSize then
896 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
898 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
899 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
901 if size sc > breakingSize then
902 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
903 rbrace <+> text "->" $+$ prettySe 10 sc
905 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
906 rbrace <+> text "->" <+> prettySe 10 sc
907 prettySe p (PApp _ (PRef _ f) [])
908 | not impl = pretty f
909 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
910 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
911 let [l, r] = getExps args in
913 if size r > breakingSize then
914 prettySe 1 l <+> pretty op $+$ prettySe 0 r
916 prettySe 1 l <+> pretty op <+> prettySe 0 r
917 prettySe p (PApp _ f as) =
918 let args = getExps as in
923 -- foldr (<+>) empty $ map prettyArgS as
925 foldl' fSe empty args
926 -- foldr (<+>) empty $ map prettyArgSe args
929 if size r > breakingSize then
930 l $+$ nest nestingSize (prettyArgS r)
935 if size r > breakingSize then
936 l $+$ nest nestingSize (prettyArgSe r)
939 prettySe p (PCase _ scr opts) =
940 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
942 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
944 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
945 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
946 prettySe p (PRefl _ _) = text "refl"
947 prettySe p (PResolveTC _) = text "resolvetc"
948 prettySe p (PTrue _) = text "()"
949 prettySe p (PFalse _) = text "_|_"
950 prettySe p (PEq _ l r) =
952 if size r > breakingSize then
953 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
955 prettySe 10 l <+> text "=" <+> prettySe 10 r
956 prettySe p (PRewrite _ l r) =
958 if size r > breakingSize then
959 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
961 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
962 prettySe p (PTyped l r) =
963 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
964 prettySe p (PPair _ l r) =
965 if size r > breakingSize then
966 lparen <> prettySe 10 l <> text "," $+$
967 prettySe 10 r <> rparen
969 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
970 prettySe p (PDPair _ l t r) =
971 if size r > breakingSize then
972 lparen <> prettySe 10 l <+> text "**" $+$
973 prettySe 10 r <> rparen
975 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
976 prettySe p (PAlternative a as) =
977 lparen <> text "|" <> prettyAs <> text "|" <> rparen
980 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
981 prettySe p PType = text "Type"
982 prettySe p (PConstant c) = pretty c
983 -- XXX: add pretty for tactics
984 prettySe p (PProof ts) =
985 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
986 prettySe p (PTactics ts) =
987 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
988 prettySe p (PMetavar n) = text "?" <> pretty n
989 prettySe p (PReturn f) = text "return"
990 prettySe p PImpossible = text "impossible"
991 prettySe p Placeholder = text "_"
992 prettySe p (PDoBlock _) = text "do block pretty not implemented"
993 prettySe p (PElabError s) = pretty s
995 prettySe p _ = text "test"
997 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
998 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
999 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1000 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1002 prettyArgSe arg = prettySe 0 arg
1003 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1004 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1005 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1007 bracket outer inner doc
1008 | inner > outer = lparen <> doc <> rparen
1011showImp :: Bool -> PTerm -> String
1012showImp impl tm = se 10 tm where
1013 se p (PQuote r) = "![" ++ show r ++ "]"
1014 se p (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1015 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1016 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
1018 where showbasic n@(UN _) = show n
1019 showbasic (MN _ s) = s
1020 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1021 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
1022 (if impl then " : " ++ se 10 ty else "") ++ " => "
1024 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
1026 se p (PPi (Exp l s _) n ty sc)
1027 | n `elem` allNamesIn sc || impl
1029 (if l then "|(" else "(") ++
1030 show n ++ " : " ++ se 10 ty ++
1033 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
1034 where st = case s of
1035 Static -> "[static] "
1037 se p (PPi (Imp l s _) n ty sc)
1038 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1039 show n ++ " : " ++ se 10 ty ++
1040 "} " ++ st ++ "-> " ++ se 10 sc
1041 | otherwise = se 10 sc
1042 where st = case s of
1043 Static -> "[static] "
1045 se p (PPi (Constraint _ _ _) n ty sc)
1046 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
1047 se p (PPi (TacImp _ _ s _) n ty sc)
1048 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
1050 | Just str <- slist p e = str
1051 | Just num <- snat p e = show num
1052 se p (PMatchApp _ f) = "match " ++ show f
1053 se p (PApp _ (PRef _ f) [])
1055 se p (PApp _ (PRef _ op@(UN (f:_))) args)
1056 | length (getExps args) == 2 && not impl && not (isAlpha f)
1057 = let [l, r] = getExps args in
1058 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
1060 = let args = getExps as in
1061 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
1062 else concatMap seArg args
1063 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
1064 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
1065 se p (PHidden tm) = "." ++ se 0 tm
1068 | otherwise = "refl {" ++ se 10 t ++ "}"
1069 se p (PResolveTC _) = "resolvetc"
1070 se p (PTrue _) = "()"
1071 se p (PFalse _) = "_|_"
1072 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
1073 se p (PRewrite _ l r) = bracket p 2 $ "rewrite " ++ se 10 l ++ " in " ++ se 10 r
1074 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
1075 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
1076 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
1077 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
1079 se p (PConstant c) = show c
1080 se p (PProof ts) = "proof { " ++ show ts ++ "}"
1081 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
1082 se p (PMetavar n) = "?" ++ show n
1083 se p (PReturn f) = "return"
1084 se p PImpossible = "impossible"
1085 se p Placeholder = "_"
1086 se p (PDoBlock _) = "do block show not implemented"
1087 se p (PElabError s) = show s
1088 se p (PCoerced t) = se p t
1089 se p (PUnifyLog t) = "%unifyLog " ++ se p t
1090-- se p x = "Not implemented"
1092 slist' p (PApp _ (PRef _ nil) _)
1093 | nsroot nil == UN "Nil" = Just []
1094 slist' p (PApp _ (PRef _ cons) args)
1095 | nsroot cons == UN "::",
1096 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1098 Just tl' <- slist' p tl
1101 isImp (PImp {}) = True
1103 slist' _ _ = Nothing
1105 slist p e | Just es <- slist' p e = Just $
1106 case es of [] -> "[]"
1107 [x] -> "[" ++ se p x ++ "]"
1108 xs -> "[" ++ intercalate "," (map (se p) xs) ++ "]"
1111 -- since Prelude is always imported, S & O are unqualified iff they're the
1114 | show o == (natns++"O") || show o == "O" = Just 0
1115 snat p (PApp _ s [PExp {getTm=n}])
1116 | show s == (natns++"S") || show s == "S",
1121 natns = "Prelude.Nat."
1123 sArg (PImp _ _ n tm _) = siArg (n, tm)
1124 sArg (PExp _ _ tm _) = seArg tm
1125 sArg (PConstraint _ _ tm _) = scArg tm
1126 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
1128 seArg arg = " " ++ se 0 arg
1129 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1130 scArg val = " {{" ++ se 10 val ++ "}}"
1131 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1133 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1137instance Sized PTerm where
1138 size (PQuote rawTerm) = size rawTerm
1139 size (PRef fc name) = size name
1140 size (PLam name ty bdy) = 1 + size ty + size bdy
1141 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1142 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1143 size (PTyped trm ty) = 1 + size trm + size ty
1144 size (PApp fc name args) = 1 + size args
1145 size (PCase fc trm bdy) = 1 + size trm + size bdy
1147 size (PFalse fc) = 1
1148 size (PRefl fc _) = 1
1149 size (PResolveTC fc) = 1
1150 size (PEq fc left right) = 1 + size left + size right
1151 size (PRewrite fc left right) = 1 + size left + size right
1152 size (PPair fc left right) = 1 + size left + size right
1153 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1154 size (PAlternative a alts) = 1 + size alts
1155 size (PHidden hidden) = size hidden
1156 size (PUnifyLog tm) = size tm
1158 size (PConstant const) = 1 + size const
1159 size Placeholder = 1
1160 size (PDoBlock dos) = 1 + size dos
1161 size (PIdiom fc term) = 1 + size term
1162 size (PReturn fc) = 1
1163 size (PMetavar name) = 1
1164 size (PProof tactics) = size tactics
1165 size (PElabError err) = size err
1166 size PImpossible = 1
1168getPArity :: PTerm -> Int
1169getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1172-- Return all names, free or globally bound, in the given term.
1174allNamesIn :: PTerm -> [Name]
1175allNamesIn tm = nub $ ni [] tm
1178 | not (n `elem` env) = [n]
1179 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1180 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1181 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1182 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1183 ni env (PHidden tm) = ni env tm
1184 ni env (PEq _ l r) = ni env l ++ ni env r
1185 ni env (PRewrite _ l r) = ni env l ++ ni env r
1186 ni env (PTyped l r) = ni env l ++ ni env r
1187 ni env (PPair _ l r) = ni env l ++ ni env r
1188 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1189 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1190 ni env (PAlternative a ls) = concatMap (ni env) ls
1191 ni env (PUnifyLog tm) = ni env tm
1194-- Return names which are free in the given term.
1196namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1197namesIn uvars ist tm = nub $ ni [] tm
1200 | not (n `elem` env)
1201 = case lookupTy n (tt_ctxt ist) of
1203 _ -> if n `elem` (map fst uvars) then [n] else []
1204 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1205 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1206 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1207 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1208 ni env (PEq _ l r) = ni env l ++ ni env r
1209 ni env (PRewrite _ l r) = ni env l ++ ni env r
1210 ni env (PTyped l r) = ni env l ++ ni env r
1211 ni env (PPair _ l r) = ni env l ++ ni env r
1212 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1213 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1214 ni env (PAlternative a as) = concatMap (ni env) as
1215 ni env (PHidden tm) = ni env tm
1216 ni env (PUnifyLog tm) = ni env tm
1219-- Return which of the given names are used in the given term.
1221usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1222usedNamesIn vars ist tm = nub $ ni [] tm
1225 | n `elem` vars && not (n `elem` env)
1226 = case lookupTy n (tt_ctxt ist) of
1229 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1230 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1231 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1232 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1233 ni env (PEq _ l r) = ni env l ++ ni env r
1234 ni env (PRewrite _ l r) = ni env l ++ ni env r
1235 ni env (PTyped l r) = ni env l ++ ni env r
1236 ni env (PPair _ l r) = ni env l ++ ni env r
1237 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1238 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1239 ni env (PAlternative a as) = concatMap (ni env) as
1240 ni env (PHidden tm) = ni env tm
1241 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