1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
8import Core.Elaborate hiding (Tactic(..))
11import IRTS.CodegenCommon
16import System.Console.Haskeline
18import Control.Monad.Trans.State.Strict
26data IOption = IOption { opt_logLevel :: Int,
28 opt_typeintype :: Bool,
31 opt_errContext :: Bool,
35 opt_outputTy :: OutputType,
36 opt_ibcsubdir :: FilePath,
37 opt_importdirs :: [FilePath],
38 opt_cmdline :: [Opt] -- remember whole command line
42defaultOpts = IOption 0 False False True False False True True ViaC Executable "" [] []
44-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
45-- without typechecking).
46-- This will include all the functions and data declarations, plus fixity declarations
51 idris_constraints :: [(UConstraint, FC)],
52 idris_infixes :: [FixDecl],
53 idris_implicits :: Ctxt [PArg],
54 idris_statics :: Ctxt [Bool],
55 idris_classes :: Ctxt ClassInfo,
56 idris_dsls :: Ctxt DSL,
57 idris_optimisation :: Ctxt OptInfo,
58 idris_datatypes :: Ctxt TypeInfo,
59 idris_patdefs :: Ctxt [([Name], Term, Term)], -- not exported
60 idris_flags :: Ctxt [FnOpt],
61 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
62 idris_calledgraph :: Ctxt [Name],
63 idris_docstrings :: Ctxt String,
64 idris_totcheck :: [(FC, Name)],
66 idris_options :: IOption,
68 idris_metavars :: [Name],
69 idris_coercions :: [Name],
70 syntax_rules :: [Syntax],
71 syntax_keywords :: [String],
72 imported :: [FilePath],
73 idris_scprims :: [(Name, (Int, PrimFn))],
74 idris_objs :: [FilePath],
75 idris_libs :: [String],
76 idris_hdrs :: [String],
77 proof_list :: [(Name, [String])],
79 lastParse :: Maybe Name,
80 indent_stack :: [Int],
81 brace_stack :: [Maybe Int],
82 hide_list :: [(Name, Maybe Accessibility)],
83 default_access :: Accessibility,
84 default_total :: Bool,
85 ibc_write :: [IBCWrite],
86 compiled_so :: Maybe String
89data SizeChange = Smaller | Same | Bigger | Unknown
92deriving instance Binary SizeChange
95type SCGEntry = (Name, [Maybe (Int, SizeChange)])
97data CGInfo = CGInfo { argsdef :: [Name],
98 calls :: [(Name, [[Name]])],
104deriving instance Binary CGInfo
107primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
109-- information that needs writing for the current module's .ibc file
110data IBCWrite = IBCFix FixDecl
114 | IBCInstance Bool Name Name
124 | IBCAccess Name Accessibility
125 | IBCTotal Name Totality
126 | IBCFlags Name [FnOpt]
130 | IBCDef Name -- i.e. main context
133idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
134 emptyContext emptyContext emptyContext emptyContext
135 emptyContext emptyContext emptyContext emptyContext
136 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] []
137 [] Nothing Nothing [] [] [] Hidden False [] Nothing
139-- The monad for the main REPL - reading and processing files and updating
140-- global state (hence the IO inner monad).
141type Idris = StateT IState IO
143-- Commands in the REPL
162 | Compile Target String
168 | AddProof (Maybe Name)
187data Opt = Filename String
219 | OutputTy OutputType
222-- Parsed declarations
224data Fixity = Infixl { prec :: Int }
225 | Infixr { prec :: Int }
226 | InfixN { prec :: Int }
227 | PrefixN { prec :: Int }
230deriving instance Binary Fixity
233instance Show Fixity where
234 show (Infixl i) = "infixl " ++ show i
235 show (Infixr i) = "infixr " ++ show i
236 show (InfixN i) = "infix " ++ show i
237 show (PrefixN i) = "prefix " ++ show i
239data FixDecl = Fix Fixity String
242instance Show FixDecl where
243 show (Fix f s) = show f ++ " " ++ s
246deriving instance Binary FixDecl
249instance Ord FixDecl where
250 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
253data Static = Static | Dynamic
256deriving instance Binary Static
259-- Mark bindings with their explicitness, and laziness
260data Plicity = Imp { plazy :: Bool,
263 | Exp { plazy :: Bool,
266 | Constraint { plazy :: Bool,
269 | TacImp { plazy :: Bool,
276deriving instance Binary Plicity
279impl = Imp False Dynamic ""
280expl = Exp False Dynamic ""
281constraint = Constraint False Dynamic ""
282tacimpl t = TacImp False Dynamic t ""
284data FnOpt = Inlinable | TotalFn | PartialFn
285 | Coinductive | AssertTotal | TCGen
286 | Implicit -- implicit coercion
287 | CExport String -- export, with a C name
288 | Specialise [Name] -- specialise it, freeze these names
291deriving instance Binary FnOpt
296inlinable :: FnOpts -> Bool
297inlinable = elem Inlinable
300 = PFix FC Fixity [String] -- fixity declaration
301 | PTy String SyntaxInfo FC FnOpts Name t -- type declaration
302 | PPostulate String SyntaxInfo FC FnOpts Name t
303 | PClauses FC FnOpts Name [PClause' t] -- pattern clause
304 | PCAF FC Name t -- top level constant
305 | PData String SyntaxInfo FC Bool -- codata
306 (PData' t) -- data declaration
307 | PParams FC [(Name, t)] [PDecl' t] -- params block
308 | PNamespace String [PDecl' t] -- new namespace
309 | PRecord String SyntaxInfo FC Name t String Name t -- record declaration
310 | PClass String SyntaxInfo FC
313 [(Name, t)] -- parameters
314 [PDecl' t] -- declarations
315 | PInstance SyntaxInfo FC [t] -- constraints
318 t -- full instance type
319 (Maybe Name) -- explicit name
323 | PMutual FC [PDecl' t]
324 | PDirective (Idris ())
327deriving instance Binary PDecl'
330data PClause' t = PClause FC Name t [t] t [PDecl' t]
331 | PWith FC Name t [t] t [PDecl' t]
332 | PClauseR FC [t] t [PDecl' t]
333 | PWithR FC [t] t [PDecl' t]
336deriving instance Binary PClause'
339data PData' t = PDatadecl { d_name :: Name,
341 d_cons :: [(String, Name, t, FC)] }
342 | PLaterdecl { d_name :: Name, d_tcon :: t }
345deriving instance Binary PData'
348-- Handy to get a free function for applying PTerm -> PTerm functions
349-- across a program, by deriving Functor
351type PDecl = PDecl' PTerm
352type PData = PData' PTerm
353type PClause = PClause' PTerm
355-- get all the names declared in a decl
357declared :: PDecl -> [Name]
358declared (PFix _ _ _) = []
359declared (PTy _ _ _ _ n t) = [n]
360declared (PPostulate _ _ _ _ n t) = [n]
361declared (PClauses _ _ n _) = [] -- not a declaration
362declared (PRecord _ _ _ n _ _ c _) = [n, c]
363declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
364 where fstt (_, a, _, _) = a
365declared (PParams _ _ ds) = concatMap declared ds
366declared (PMutual _ ds) = concatMap declared ds
367declared (PNamespace _ ds) = concatMap declared ds
369-- get the names declared, not counting nested parameter blocks
370tldeclared :: PDecl -> [Name]
371tldeclared (PFix _ _ _) = []
372tldeclared (PTy _ _ _ _ n t) = [n]
373tldeclared (PPostulate _ _ _ _ n t) = [n]
374tldeclared (PClauses _ _ n _) = [] -- not a declaration
375tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
376tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
377 where fstt (_, a, _, _) = a
378tldeclared (PParams _ _ ds) = []
379tldeclared (PMutual _ ds) = concatMap tldeclared ds
380tldeclared (PNamespace _ ds) = concatMap tldeclared ds
381-- declared (PImport _) = []
383defined :: PDecl -> [Name]
384defined (PFix _ _ _) = []
385defined (PTy _ _ _ _ n t) = []
386defined (PPostulate _ _ _ _ n t) = []
387defined (PClauses _ _ n _) = [n] -- not a declaration
388defined (PRecord _ _ _ n _ _ c _) = [n, c]
389defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
390 where fstt (_, a, _, _) = a
391defined (PParams _ _ ds) = concatMap defined ds
392defined (PMutual _ ds) = concatMap defined ds
393defined (PNamespace _ ds) = concatMap defined ds
394-- declared (PImport _) = []
396updateN :: [(Name, Name)] -> Name -> Name
397updateN ns n | Just n' <- lookup n ns = n'
400updateNs :: [(Name, Name)] -> PTerm -> PTerm
402updateNs ns t = mapPT updateRef t
403 where updateRef (PRef fc f) = PRef fc (updateN ns f)
406-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
408-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
409-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
410-- where updateCNs ns (PClause n l ts r ds)
411-- = PClause (updateN ns n) (fmap (updateNs ns) l)
412-- (map (fmap (updateNs ns)) ts)
413-- (fmap (updateNs ns) r)
414-- (map (updateDNs ns) ds)
417-- High level language terms
419data PTerm = PQuote Raw
421 | PInferRef FC Name -- a name to be defined later
423 | PLam Name PTerm PTerm
424 | PPi Plicity Name PTerm PTerm
425 | PLet Name PTerm PTerm PTerm
426 | PTyped PTerm PTerm -- term with explicit type
427 | PApp FC PTerm [PArg]
428 | PCase FC PTerm [(PTerm, PTerm)]
434 | PPair FC PTerm PTerm
435 | PDPair FC PTerm PTerm PTerm
436 | PAlternative Bool [PTerm] -- True if only one may work
437 | PHidden PTerm -- irrelevant or hidden pattern
446 | PTactics [PTactic] -- as PProof, but no auto solving
447 | PElabError Err -- error to report on elaboration
448 | PImpossible -- special case for declaring when an LHS can't typecheck
449 | PCoerced PTerm -- to mark a coerced argument, so as not to coerce twice
452deriving instance Binary PTerm
455mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
456mapPT f t = f (mpt t) where
457 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
458 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
459 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
460 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
461 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
462 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
463 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
464 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
465 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
466 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
467 mpt (PHidden t) = PHidden (mapPT f t)
468 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
469 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
470 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
474data PTactic' t = Intro [Name] | Intros | Focus Name
475 | Refine Name [Bool] | Rewrite t
476 | LetTac Name t | LetTacTy Name t t
477 | Exact t | Compute | Trivial
480 | ProofState | ProofTerm | Undo
481 | Try (PTactic' t) (PTactic' t)
482 | TSeq (PTactic' t) (PTactic' t)
483 | ReflectTac t -- see Language.Reflection module
484 | GoalType String (PTactic' t)
486 deriving (Show, Eq, Functor)
488deriving instance Binary PTactic'
491instance Sized a => Sized (PTactic' a) where
492 size (Intro nms) = 1 + size nms
494 size (Focus nm) = 1 + size nm
495 size (Refine nm bs) = 1 + size nm + length bs
496 size (Rewrite t) = 1 + size t
497 size (LetTac nm t) = 1 + size nm + size t
498 size (Exact t) = 1 + size t
506 size (Try l r) = 1 + size l + size r
507 size (TSeq l r) = 1 + size l + size r
511type PTactic = PTactic' PTerm
513data PDo' t = DoExp FC t
518 deriving (Eq, Functor)
520deriving instance Binary PDo'
523instance Sized a => Sized (PDo' a) where
524 size (DoExp fc t) = 1 + size fc + size t
525 size (DoBind fc nm t) = 1 + size fc + size nm + size t
526 size (DoBindP fc l r) = 1 + size fc + size l + size r
527 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
528 size (DoLetP fc l r) = 1 + size fc + size l + size r
532-- The priority gives a hint as to elaboration order. Best to elaborate
533-- things early which will help give a more concrete type to other
534-- variables, e.g. a before (interpTy a).
535-- TODO: priority no longer serves any purpose, drop it!
537data PArg' t = PImp { priority :: Int,
538 lazyarg :: Bool, pname :: Name, getTm :: t,
540 | PExp { priority :: Int,
541 lazyarg :: Bool, getTm :: t,
543 | PConstraint { priority :: Int,
544 lazyarg :: Bool, getTm :: t,
546 | PTacImplicit { priority :: Int,
547 lazyarg :: Bool, pname :: Name,
551 deriving (Show, Eq, Functor)
553instance Sized a => Sized (PArg' a) where
554 size (PImp p l nm trm _) = 1 + size nm + size trm
555 size (PExp p l trm _) = 1 + size trm
556 size (PConstraint p l trm _) = 1 + size trm
557 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
560deriving instance Binary PArg'
563pimp n t = PImp 0 True n t ""
564pexp t = PExp 0 False t ""
565pconst t = PConstraint 0 False t ""
566ptacimp n s t = PTacImplicit 0 True n s t ""
568type PArg = PArg' PTerm
572data ClassInfo = CI { instanceName :: Name,
573 class_methods :: [(Name, (FnOpts, PTerm))],
574 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
575 class_params :: [Name],
576 class_instances :: [Name] }
579deriving instance Binary ClassInfo
582data OptInfo = Optimise { collapsible :: Bool,
583 forceable :: [Int], -- argument positions
587deriving instance Binary OptInfo
591data TypeInfo = TI { con_names :: [Name],
596deriving instance Binary TypeInfo
599-- Syntactic sugar info
601data DSL' t = DSL { dsl_bind :: t,
606 index_first :: Maybe t,
607 index_next :: Maybe t,
608 dsl_lambda :: Maybe t,
611 deriving (Show, Functor)
613deriving instance Binary DSL'
618data SynContext = PatternSyntax | TermSyntax | AnySyntax
621deriving instance Binary SynContext
624data Syntax = Rule [SSymbol] PTerm SynContext
627deriving instance Binary Syntax
630data SSymbol = Keyword Name
637deriving instance Binary SSymbol
640initDSL = DSL (PRef f (UN ">>="))
641 (PRef f (UN "return"))
649 where f = FC "(builtin)" 0
651data SyntaxInfo = Syn { using :: [(Name, PTerm)],
652 syn_params :: [(Name, PTerm)],
653 syn_namespace :: [String],
655 decoration :: Name -> Name,
657 implicitAllowed :: Bool,
661deriving instance Binary SyntaxInfo
664defaultSyntax = Syn [] [] [] [] id False False initDSL
666expandNS :: SyntaxInfo -> Name -> Name
667expandNS syn n@(NS _ _) = n
668expandNS syn n = case syn_namespace syn of
673--- Pretty printing declarations and terms
675instance Show PTerm where
676 show tm = showImp False tm
678instance Pretty PTerm where
679 pretty = prettyImp False
681instance Show PDecl where
682 show d = showDeclImp False d
684instance Show PClause where
685 show c = showCImp True c
687instance Show PData where
688 show d = showDImp False d
690showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
691showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
692showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
693showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
694showDeclImp _ (PData _ _ _ _ d) = show d
695showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
696 showSep "\n" (map show ps)
699showCImp :: Bool -> PClause -> String
700showCImp impl (PClause _ n l ws r w)
701 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
702 ++ " where " ++ show w
705 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
706showCImp impl (PWith _ n l ws r w)
707 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
708 ++ " { " ++ show w ++ " } "
711 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
714showDImp :: Bool -> PData -> String
715showDImp impl (PDatadecl n ty cons)
716 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
718 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
720getImps :: [PArg] -> [(Name, PTerm)]
722getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
723getImps (_ : xs) = getImps xs
725getExps :: [PArg] -> [PTerm]
727getExps (PExp _ _ tm _ : xs) = tm : getExps xs
728getExps (_ : xs) = getExps xs
730getConsts :: [PArg] -> [PTerm]
732getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
733getConsts (_ : xs) = getConsts xs
735getAll :: [PArg] -> [PTerm]
738prettyImp :: Bool -> PTerm -> Doc
739prettyImp impl = prettySe 10
741 prettySe p (PQuote r) =
742 if size r > breakingSize then
743 text "![" $$ pretty r <> text "]"
745 text "![" <> pretty r <> text "]"
746 prettySe p (PPatvar fc n) = pretty n
747 prettySe p (PRef fc n) =
753 prettyBasic n@(UN _) = pretty n
754 prettyBasic (MN _ s) = text s
755 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
756 prettySe p (PLam n ty sc) =
758 if size sc > breakingSize then
759 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
761 text "λ" <> pretty n <+> text "=>" <+> pretty sc
762 prettySe p (PLet n ty v sc) =
764 if size sc > breakingSize then
765 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
766 nest nestingSize (prettySe 10 sc)
768 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
770 prettySe p (PPi (Exp l s _) n ty sc)
771 | n `elem` allNamesIn sc || impl =
772 let open = if l then text "|" <> lparen else lparen in
774 if size sc > breakingSize then
775 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
776 st <+> text "->" $+$ prettySe 10 sc
778 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
779 st <+> text "->" <+> prettySe 10 sc
782 if size sc > breakingSize then
783 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
785 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
789 Static -> text "[static]"
791 prettySe p (PPi (Imp l s _) n ty sc)
793 let open = if l then text "|" <> lbrace else lbrace in
795 if size sc > breakingSize then
796 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
797 st <+> text "->" <+> prettySe 10 sc
799 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
800 st <+> text "->" <+> prettySe 10 sc
801 | otherwise = prettySe 10 sc
805 Static -> text $ "[static]"
807 prettySe p (PPi (Constraint _ _ _) n ty sc) =
809 if size sc > breakingSize then
810 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
812 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
813 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
815 if size sc > breakingSize then
816 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
817 rbrace <+> text "->" $+$ prettySe 10 sc
819 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
820 rbrace <+> text "->" <+> prettySe 10 sc
821 prettySe p (PApp _ (PRef _ f) [])
822 | not impl = pretty f
823 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
824 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
825 let [l, r] = getExps args in
827 if size r > breakingSize then
828 prettySe 1 l <+> pretty op $+$ prettySe 0 r
830 prettySe 1 l <+> pretty op <+> prettySe 0 r
831 prettySe p (PApp _ f as) =
832 let args = getExps as in
837 -- foldr (<+>) empty $ map prettyArgS as
840 -- foldr (<+>) empty $ map prettyArgSe args
843 if size r > breakingSize then
844 l $+$ nest nestingSize (prettyArgS r)
849 if size r > breakingSize then
850 l $+$ nest nestingSize (prettyArgSe r)
853 prettySe p (PCase _ scr opts) =
854 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
856 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
858 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
859 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
860 prettySe p (PRefl _ _) = text "refl"
861 prettySe p (PResolveTC _) = text "resolvetc"
862 prettySe p (PTrue _) = text "()"
863 prettySe p (PFalse _) = text "_|_"
864 prettySe p (PEq _ l r) =
866 if size r > breakingSize then
867 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
869 prettySe 10 l <+> text "=" <+> prettySe 10 r
870 prettySe p (PTyped l r) =
871 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
872 prettySe p (PPair _ l r) =
873 if size r > breakingSize then
874 lparen <> prettySe 10 l <> text "," $+$
875 prettySe 10 r <> rparen
877 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
878 prettySe p (PDPair _ l t r) =
879 if size r > breakingSize then
880 lparen <> prettySe 10 l <+> text "**" $+$
881 prettySe 10 r <> rparen
883 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
884 prettySe p (PAlternative a as) =
885 lparen <> text "|" <> prettyAs <> text "|" <> rparen
888 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
889 prettySe p PType = text "Type"
890 prettySe p (PConstant c) = pretty c
891 -- XXX: add pretty for tactics
892 prettySe p (PProof ts) =
893 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
894 prettySe p (PTactics ts) =
895 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
896 prettySe p (PMetavar n) = text "?" <> pretty n
897 prettySe p (PReturn f) = text "return"
898 prettySe p PImpossible = text "impossible"
899 prettySe p Placeholder = text "_"
900 prettySe p (PDoBlock _) = text "do block pretty not implemented"
901 prettySe p (PElabError s) = pretty s
903 prettySe p _ = text "test"
905 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
906 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
907 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
908 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
910 prettyArgSe arg = prettySe 0 arg
911 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
912 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
913 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
915 bracket outer inner doc
916 | inner > outer = lparen <> doc <> rparen
919showImp :: Bool -> PTerm -> String
920showImp impl tm = se 10 tm where
921 se p (PQuote r) = "![" ++ show r ++ "]"
922 se p (PPatvar fc n) = show n
923 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
924 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
926 where showbasic n@(UN _) = show n
927 showbasic (MN _ s) = s
928 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
929 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
930 (if impl then " : " ++ se 10 ty else "") ++ " => "
932 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
934 se p (PPi (Exp l s _) n ty sc)
935 | n `elem` allNamesIn sc || impl
937 (if l then "|(" else "(") ++
938 show n ++ " : " ++ se 10 ty ++
941 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
943 Static -> "[static] "
945 se p (PPi (Imp l s _) n ty sc)
946 | impl = bracket p 2 $ (if l then "|{" else "{") ++
947 show n ++ " : " ++ se 10 ty ++
948 "} " ++ st ++ "-> " ++ se 10 sc
949 | otherwise = se 10 sc
951 Static -> "[static] "
953 se p (PPi (Constraint _ _ _) n ty sc)
954 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
955 se p (PPi (TacImp _ _ s _) n ty sc)
956 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
957 se p (PApp _ (PRef _ f) [])
959 se p (PApp _ (PRef _ op@(UN (f:_))) args)
960 | length (getExps args) == 2 && not impl && not (isAlpha f)
961 = let [l, r] = getExps args in
962 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
964 = let args = getExps as in
965 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
966 else concatMap seArg args
967 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
968 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
969 se p (PHidden tm) = "." ++ se 0 tm
972 | otherwise = "refl {" ++ se 10 t ++ "}"
973 se p (PResolveTC _) = "resolvetc"
974 se p (PTrue _) = "()"
975 se p (PFalse _) = "_|_"
976 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
977 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
978 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
979 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
980 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
982 se p (PConstant c) = show c
983 se p (PProof ts) = "proof { " ++ show ts ++ "}"
984 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
985 se p (PMetavar n) = "?" ++ show n
986 se p (PReturn f) = "return"
987 se p PImpossible = "impossible"
988 se p Placeholder = "_"
989 se p (PDoBlock _) = "do block show not implemented"
990 se p (PElabError s) = show s
991 se p (PCoerced t) = se p t
992-- se p x = "Not implemented"
994 sArg (PImp _ _ n tm _) = siArg (n, tm)
995 sArg (PExp _ _ tm _) = seArg tm
996 sArg (PConstraint _ _ tm _) = scArg tm
997 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
999 seArg arg = " " ++ se 0 arg
1000 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1001 scArg val = " {{" ++ se 10 val ++ "}}"
1002 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1004 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1008instance Sized PTerm where
1009 size (PQuote rawTerm) = size rawTerm
1010 size (PRef fc name) = size name
1011 size (PLam name ty bdy) = 1 + size ty + size bdy
1012 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1013 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1014 size (PTyped trm ty) = 1 + size trm + size ty
1015 size (PApp fc name args) = 1 + size args
1016 size (PCase fc trm bdy) = 1 + size trm + size bdy
1018 size (PFalse fc) = 1
1019 size (PRefl fc _) = 1
1020 size (PResolveTC fc) = 1
1021 size (PEq fc left right) = 1 + size left + size right
1022 size (PPair fc left right) = 1 + size left + size right
1023 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1024 size (PAlternative a alts) = 1 + size alts
1025 size (PHidden hidden) = size hidden
1027 size (PConstant const) = 1 + size const
1028 size Placeholder = 1
1029 size (PDoBlock dos) = 1 + size dos
1030 size (PIdiom fc term) = 1 + size term
1031 size (PReturn fc) = 1
1032 size (PMetavar name) = 1
1033 size (PProof tactics) = size tactics
1034 size (PElabError err) = size err
1035 size PImpossible = 1
1037getPArity :: PTerm -> Int
1038getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1041-- Return all names, free or globally bound, in the given term.
1043allNamesIn :: PTerm -> [Name]
1044allNamesIn tm = nub $ ni [] tm
1047 | not (n `elem` env) = [n]
1048 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1049 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1050 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1051 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1052 ni env (PHidden tm) = ni env tm
1053 ni env (PEq _ l r) = ni env l ++ ni env r
1054 ni env (PTyped l r) = ni env l ++ ni env r
1055 ni env (PPair _ l r) = ni env l ++ ni env r
1056 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1057 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1058 ni env (PAlternative a ls) = concatMap (ni env) ls
1061-- Return names which are free in the given term.
1063namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1064namesIn uvars ist tm = nub $ ni [] tm
1067 | not (n `elem` env)
1068 = case lookupTy Nothing n (tt_ctxt ist) of
1070 _ -> if n `elem` (map fst uvars) then [n] else []
1071 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1072 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1073 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1074 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1075 ni env (PEq _ l r) = ni env l ++ ni env r
1076 ni env (PTyped l r) = ni env l ++ ni env r
1077 ni env (PPair _ l r) = ni env l ++ ni env r
1078 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1079 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1080 ni env (PAlternative a as) = concatMap (ni env) as
1081 ni env (PHidden tm) = ni env tm
1084-- Return which of the given names are used in the given term.
1086usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1087usedNamesIn vars ist tm = nub $ ni [] tm
1090 | n `elem` vars && not (n `elem` env)
1091 = case lookupTy Nothing n (tt_ctxt ist) of
1094 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1095 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1096 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1097 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1098 ni env (PEq _ l r) = ni env l ++ ni env r
1099 ni env (PTyped l r) = ni env l ++ ni env r
1100 ni env (PPair _ l r) = ni env l ++ ni env r
1101 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1102 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1103 ni env (PAlternative a as) = concatMap (ni env) as
1104 ni env (PHidden 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