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 syntax_rules :: [Syntax],
70 syntax_keywords :: [String],
71 imported :: [FilePath],
72 idris_scprims :: [(Name, (Int, PrimFn))],
73 idris_objs :: [FilePath],
74 idris_libs :: [String],
75 idris_hdrs :: [String],
76 proof_list :: [(Name, [String])],
78 lastParse :: Maybe Name,
79 indent_stack :: [Int],
80 brace_stack :: [Maybe Int],
81 hide_list :: [(Name, Maybe Accessibility)],
82 default_access :: Accessibility,
83 default_total :: Bool,
84 ibc_write :: [IBCWrite],
85 compiled_so :: Maybe String
88data SizeChange = Smaller | Same | Bigger | Unknown
91deriving instance Binary SizeChange
94type SCGEntry = (Name, [Maybe (Int, SizeChange)])
96data CGInfo = CGInfo { argsdef :: [Name],
97 calls :: [(Name, [[Name]])],
103deriving instance Binary CGInfo
106primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
108-- information that needs writing for the current module's .ibc file
109data IBCWrite = IBCFix FixDecl
113 | IBCInstance Bool Name Name
123 | IBCAccess Name Accessibility
124 | IBCTotal Name Totality
125 | IBCFlags Name [FnOpt]
128 | IBCDef Name -- i.e. main context
131idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
132 emptyContext emptyContext emptyContext emptyContext
133 emptyContext emptyContext emptyContext emptyContext
134 [] "" defaultOpts 6 [] [] [] [] [] [] [] []
135 [] Nothing Nothing [] [] [] Hidden False [] Nothing
137-- The monad for the main REPL - reading and processing files and updating
138-- global state (hence the IO inner monad).
139type Idris = StateT IState (InputT IO)
141-- Commands in the REPL
143data Target = ViaC | ViaJava | Bytecode | ToJavaScript
156 | Compile Target String
162 | AddProof (Maybe Name)
181data Opt = Filename String
213 | OutputTy OutputType
216-- Parsed declarations
218data Fixity = Infixl { prec :: Int }
219 | Infixr { prec :: Int }
220 | InfixN { prec :: Int }
221 | PrefixN { prec :: Int }
224deriving instance Binary Fixity
227instance Show Fixity where
228 show (Infixl i) = "infixl " ++ show i
229 show (Infixr i) = "infixr " ++ show i
230 show (InfixN i) = "infix " ++ show i
231 show (PrefixN i) = "prefix " ++ show i
233data FixDecl = Fix Fixity String
236instance Show FixDecl where
237 show (Fix f s) = show f ++ " " ++ s
240deriving instance Binary FixDecl
243instance Ord FixDecl where
244 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
247data Static = Static | Dynamic
250deriving instance Binary Static
253-- Mark bindings with their explicitness, and laziness
254data Plicity = Imp { plazy :: Bool,
257 | Exp { plazy :: Bool,
260 | Constraint { plazy :: Bool,
263 | TacImp { plazy :: Bool,
270deriving instance Binary Plicity
273impl = Imp False Dynamic ""
274expl = Exp False Dynamic ""
275constraint = Constraint False Dynamic ""
276tacimpl t = TacImp False Dynamic t ""
278data FnOpt = Inlinable | TotalFn | PartialFn
279 | Coinductive | AssertTotal | TCGen
280 | CExport String -- export, with a C name
281 | Specialise [Name] -- specialise it, freeze these names
284deriving instance Binary FnOpt
289inlinable :: FnOpts -> Bool
290inlinable = elem Inlinable
293 = PFix FC Fixity [String] -- fixity declaration
294 | PTy String SyntaxInfo FC FnOpts Name t -- type declaration
295 | PPostulate String SyntaxInfo FC FnOpts Name t
296 | PClauses FC FnOpts Name [PClause' t] -- pattern clause
297 | PCAF FC Name t -- top level constant
298 | PData String SyntaxInfo FC Bool -- codata
299 (PData' t) -- data declaration
300 | PParams FC [(Name, t)] [PDecl' t] -- params block
301 | PNamespace String [PDecl' t] -- new namespace
302 | PRecord String SyntaxInfo FC Name t String Name t -- record declaration
303 | PClass String SyntaxInfo FC
306 [(Name, t)] -- parameters
307 [PDecl' t] -- declarations
308 | PInstance SyntaxInfo FC [t] -- constraints
311 t -- full instance type
312 (Maybe Name) -- explicit name
316 | PMutual FC [PDecl' t]
317 | PDirective (Idris ())
320deriving instance Binary PDecl'
323data PClause' t = PClause FC Name t [t] t [PDecl' t]
324 | PWith FC Name t [t] t [PDecl' t]
325 | PClauseR FC [t] t [PDecl' t]
326 | PWithR FC [t] t [PDecl' t]
329deriving instance Binary PClause'
332data PData' t = PDatadecl { d_name :: Name,
334 d_cons :: [(String, Name, t, FC)] }
335 | PLaterdecl { d_name :: Name, d_tcon :: t }
338deriving instance Binary PData'
341-- Handy to get a free function for applying PTerm -> PTerm functions
342-- across a program, by deriving Functor
344type PDecl = PDecl' PTerm
345type PData = PData' PTerm
346type PClause = PClause' PTerm
348-- get all the names declared in a decl
350declared :: PDecl -> [Name]
351declared (PFix _ _ _) = []
352declared (PTy _ _ _ _ n t) = [n]
353declared (PPostulate _ _ _ _ n t) = [n]
354declared (PClauses _ _ n _) = [] -- not a declaration
355declared (PRecord _ _ _ n _ _ c _) = [n, c]
356declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
357 where fstt (_, a, _, _) = a
358declared (PParams _ _ ds) = concatMap declared ds
359declared (PMutual _ ds) = concatMap declared ds
360declared (PNamespace _ ds) = concatMap declared ds
362-- get the names declared, not counting nested parameter blocks
363tldeclared :: PDecl -> [Name]
364tldeclared (PFix _ _ _) = []
365tldeclared (PTy _ _ _ _ n t) = [n]
366tldeclared (PPostulate _ _ _ _ n t) = [n]
367tldeclared (PClauses _ _ n _) = [] -- not a declaration
368tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
369tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
370 where fstt (_, a, _, _) = a
371tldeclared (PParams _ _ ds) = []
372tldeclared (PMutual _ ds) = concatMap tldeclared ds
373tldeclared (PNamespace _ ds) = concatMap tldeclared ds
374-- declared (PImport _) = []
376defined :: PDecl -> [Name]
377defined (PFix _ _ _) = []
378defined (PTy _ _ _ _ n t) = []
379defined (PPostulate _ _ _ _ n t) = []
380defined (PClauses _ _ n _) = [n] -- not a declaration
381defined (PRecord _ _ _ n _ _ c _) = [n, c]
382defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
383 where fstt (_, a, _, _) = a
384defined (PParams _ _ ds) = concatMap defined ds
385defined (PMutual _ ds) = concatMap defined ds
386defined (PNamespace _ ds) = concatMap defined ds
387-- declared (PImport _) = []
389updateN :: [(Name, Name)] -> Name -> Name
390updateN ns n | Just n' <- lookup n ns = n'
393updateNs :: [(Name, Name)] -> PTerm -> PTerm
395updateNs ns t = mapPT updateRef t
396 where updateRef (PRef fc f) = PRef fc (updateN ns f)
399-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
401-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
402-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
403-- where updateCNs ns (PClause n l ts r ds)
404-- = PClause (updateN ns n) (fmap (updateNs ns) l)
405-- (map (fmap (updateNs ns)) ts)
406-- (fmap (updateNs ns) r)
407-- (map (updateDNs ns) ds)
410-- High level language terms
412data PTerm = PQuote Raw
414 | PInferRef FC Name -- a name to be defined later
416 | PLam Name PTerm PTerm
417 | PPi Plicity Name PTerm PTerm
418 | PLet Name PTerm PTerm PTerm
419 | PTyped PTerm PTerm -- term with explicit type
420 | PApp FC PTerm [PArg]
421 | PCase FC PTerm [(PTerm, PTerm)]
427 | PPair FC PTerm PTerm
428 | PDPair FC PTerm PTerm PTerm
429 | PAlternative Bool [PTerm] -- True if only one may work
430 | PHidden PTerm -- irrelevant or hidden pattern
439 | PTactics [PTactic] -- as PProof, but no auto solving
440 | PElabError Err -- error to report on elaboration
441 | PImpossible -- special case for declaring when an LHS can't typecheck
444deriving instance Binary PTerm
447mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
448mapPT f t = f (mpt t) where
449 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
450 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
451 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
452 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
453 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
454 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
455 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
456 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
457 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
458 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
459 mpt (PHidden t) = PHidden (mapPT f t)
460 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
461 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
462 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
466data PTactic' t = Intro [Name] | Intros | Focus Name
467 | Refine Name [Bool] | Rewrite t
468 | LetTac Name t | LetTacTy Name t t
469 | Exact t | Compute | Trivial
472 | ProofState | ProofTerm | Undo
473 | Try (PTactic' t) (PTactic' t)
474 | TSeq (PTactic' t) (PTactic' t)
475 | ReflectTac t -- see Language.Reflection module
476 | GoalType String (PTactic' t)
478 deriving (Show, Eq, Functor)
480deriving instance Binary PTactic'
483instance Sized a => Sized (PTactic' a) where
484 size (Intro nms) = 1 + size nms
486 size (Focus nm) = 1 + size nm
487 size (Refine nm bs) = 1 + size nm + length bs
488 size (Rewrite t) = 1 + size t
489 size (LetTac nm t) = 1 + size nm + size t
490 size (Exact t) = 1 + size t
498 size (Try l r) = 1 + size l + size r
499 size (TSeq l r) = 1 + size l + size r
503type PTactic = PTactic' PTerm
505data PDo' t = DoExp FC t
510 deriving (Eq, Functor)
512deriving instance Binary PDo'
515instance Sized a => Sized (PDo' a) where
516 size (DoExp fc t) = 1 + size fc + size t
517 size (DoBind fc nm t) = 1 + size fc + size nm + size t
518 size (DoBindP fc l r) = 1 + size fc + size l + size r
519 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
520 size (DoLetP fc l r) = 1 + size fc + size l + size r
524-- The priority gives a hint as to elaboration order. Best to elaborate
525-- things early which will help give a more concrete type to other
526-- variables, e.g. a before (interpTy a).
528data PArg' t = PImp { priority :: Int,
529 lazyarg :: Bool, pname :: Name, getTm :: t,
531 | PExp { priority :: Int,
532 lazyarg :: Bool, getTm :: t,
534 | PConstraint { priority :: Int,
535 lazyarg :: Bool, getTm :: t,
537 | PTacImplicit { priority :: Int,
538 lazyarg :: Bool, pname :: Name,
542 deriving (Show, Eq, Functor)
544instance Sized a => Sized (PArg' a) where
545 size (PImp p l nm trm _) = 1 + size nm + size trm
546 size (PExp p l trm _) = 1 + size trm
547 size (PConstraint p l trm _) = 1 + size trm
548 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
551deriving instance Binary PArg'
554pimp n t = PImp 0 True n t ""
555pexp t = PExp 0 False t ""
556pconst t = PConstraint 0 False t ""
557ptacimp n s t = PTacImplicit 0 True n s t ""
559type PArg = PArg' PTerm
563data ClassInfo = CI { instanceName :: Name,
564 class_methods :: [(Name, (FnOpts, PTerm))],
565 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
566 class_params :: [Name],
567 class_instances :: [Name] }
570deriving instance Binary ClassInfo
573data OptInfo = Optimise { collapsible :: Bool,
574 forceable :: [Int], -- argument positions
578deriving instance Binary OptInfo
582data TypeInfo = TI { con_names :: [Name],
587deriving instance Binary TypeInfo
590-- Syntactic sugar info
592data DSL' t = DSL { dsl_bind :: t,
597 index_first :: Maybe t,
598 index_next :: Maybe t,
599 dsl_lambda :: Maybe t,
602 deriving (Show, Functor)
604deriving instance Binary DSL'
609data SynContext = PatternSyntax | TermSyntax | AnySyntax
612deriving instance Binary SynContext
615data Syntax = Rule [SSymbol] PTerm SynContext
618deriving instance Binary Syntax
621data SSymbol = Keyword Name
628deriving instance Binary SSymbol
631initDSL = DSL (PRef f (UN ">>="))
632 (PRef f (UN "return"))
640 where f = FC "(builtin)" 0
642data SyntaxInfo = Syn { using :: [(Name, PTerm)],
643 syn_params :: [(Name, PTerm)],
644 syn_namespace :: [String],
646 decoration :: Name -> Name,
648 implicitAllowed :: Bool,
652deriving instance Binary SyntaxInfo
655defaultSyntax = Syn [] [] [] [] id False False initDSL
657expandNS :: SyntaxInfo -> Name -> Name
658expandNS syn n@(NS _ _) = n
659expandNS syn n = case syn_namespace syn of
664--- Pretty printing declarations and terms
666instance Show PTerm where
667 show tm = showImp False tm
669instance Pretty PTerm where
670 pretty = prettyImp False
672instance Show PDecl where
673 show d = showDeclImp False d
675instance Show PClause where
676 show c = showCImp True c
678instance Show PData where
679 show d = showDImp False d
681showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
682showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
683showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
684showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
685showDeclImp _ (PData _ _ _ _ d) = show d
686showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
687 showSep "\n" (map show ps)
690showCImp :: Bool -> PClause -> String
691showCImp impl (PClause _ n l ws r w)
692 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
693 ++ " where " ++ show w
696 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
697showCImp impl (PWith _ n l ws r w)
698 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
699 ++ " { " ++ show w ++ " } "
702 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
705showDImp :: Bool -> PData -> String
706showDImp impl (PDatadecl n ty cons)
707 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
709 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
711getImps :: [PArg] -> [(Name, PTerm)]
713getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
714getImps (_ : xs) = getImps xs
716getExps :: [PArg] -> [PTerm]
718getExps (PExp _ _ tm _ : xs) = tm : getExps xs
719getExps (_ : xs) = getExps xs
721getConsts :: [PArg] -> [PTerm]
723getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
724getConsts (_ : xs) = getConsts xs
726getAll :: [PArg] -> [PTerm]
729prettyImp :: Bool -> PTerm -> Doc
730prettyImp impl = prettySe 10
732 prettySe p (PQuote r) =
733 if size r > breakingSize then
734 text "![" $$ pretty r <> text "]"
736 text "![" <> pretty r <> text "]"
737 prettySe p (PPatvar fc n) = pretty n
738 prettySe p (PRef fc n) =
744 prettyBasic n@(UN _) = pretty n
745 prettyBasic (MN _ s) = text s
746 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
747 prettySe p (PLam n ty sc) =
749 if size sc > breakingSize then
750 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
752 text "λ" <> pretty n <+> text "=>" <+> pretty sc
753 prettySe p (PLet n ty v sc) =
755 if size sc > breakingSize then
756 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
757 nest nestingSize (prettySe 10 sc)
759 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
761 prettySe p (PPi (Exp l s _) n ty sc)
762 | n `elem` allNamesIn sc || impl =
763 let open = if l then text "|" <> lparen else lparen in
765 if size sc > breakingSize then
766 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
767 st <+> text "->" $+$ prettySe 10 sc
769 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
770 st <+> text "->" <+> prettySe 10 sc
773 if size sc > breakingSize then
774 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
776 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
780 Static -> text "[static]"
782 prettySe p (PPi (Imp l s _) n ty sc)
784 let open = if l then text "|" <> lbrace else lbrace in
786 if size sc > breakingSize then
787 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
788 st <+> text "->" <+> prettySe 10 sc
790 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
791 st <+> text "->" <+> prettySe 10 sc
792 | otherwise = prettySe 10 sc
796 Static -> text $ "[static]"
798 prettySe p (PPi (Constraint _ _ _) n ty sc) =
800 if size sc > breakingSize then
801 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
803 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
804 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
806 if size sc > breakingSize then
807 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
808 rbrace <+> text "->" $+$ prettySe 10 sc
810 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
811 rbrace <+> text "->" <+> prettySe 10 sc
812 prettySe p (PApp _ (PRef _ f) [])
813 | not impl = pretty f
814 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
815 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
816 let [l, r] = getExps args in
818 if size r > breakingSize then
819 prettySe 1 l <+> pretty op $+$ prettySe 0 r
821 prettySe 1 l <+> pretty op <+> prettySe 0 r
822 prettySe p (PApp _ f as) =
823 let args = getExps as in
828 -- foldr (<+>) empty $ map prettyArgS as
831 -- foldr (<+>) empty $ map prettyArgSe args
834 if size r > breakingSize then
835 l $+$ nest nestingSize (prettyArgS r)
840 if size r > breakingSize then
841 l $+$ nest nestingSize (prettyArgSe r)
844 prettySe p (PCase _ scr opts) =
845 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
847 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
849 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
850 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
851 prettySe p (PRefl _ _) = text "refl"
852 prettySe p (PResolveTC _) = text "resolvetc"
853 prettySe p (PTrue _) = text "()"
854 prettySe p (PFalse _) = text "_|_"
855 prettySe p (PEq _ l r) =
857 if size r > breakingSize then
858 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
860 prettySe 10 l <+> text "=" <+> prettySe 10 r
861 prettySe p (PTyped l r) =
862 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
863 prettySe p (PPair _ l r) =
864 if size r > breakingSize then
865 lparen <> prettySe 10 l <> text "," $+$
866 prettySe 10 r <> rparen
868 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
869 prettySe p (PDPair _ l t r) =
870 if size r > breakingSize then
871 lparen <> prettySe 10 l <+> text "**" $+$
872 prettySe 10 r <> rparen
874 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
875 prettySe p (PAlternative a as) =
876 lparen <> text "|" <> prettyAs <> text "|" <> rparen
879 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
880 prettySe p PType = text "Type"
881 prettySe p (PConstant c) = pretty c
882 -- XXX: add pretty for tactics
883 prettySe p (PProof ts) =
884 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
885 prettySe p (PTactics ts) =
886 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
887 prettySe p (PMetavar n) = text "?" <> pretty n
888 prettySe p (PReturn f) = text "return"
889 prettySe p PImpossible = text "impossible"
890 prettySe p Placeholder = text "_"
891 prettySe p (PDoBlock _) = text "do block pretty not implemented"
892 prettySe p (PElabError s) = pretty s
894 prettySe p _ = text "test"
896 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
897 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
898 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
899 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
901 prettyArgSe arg = prettySe 0 arg
902 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
903 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
904 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
906 bracket outer inner doc
907 | inner > outer = lparen <> doc <> rparen
910showImp :: Bool -> PTerm -> String
911showImp impl tm = se 10 tm where
912 se p (PQuote r) = "![" ++ show r ++ "]"
913 se p (PPatvar fc n) = show n
914 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
915 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
917 where showbasic n@(UN _) = show n
918 showbasic (MN _ s) = s
919 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
920 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
921 (if impl then " : " ++ se 10 ty else "") ++ " => "
923 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
925 se p (PPi (Exp l s _) n ty sc)
926 | n `elem` allNamesIn sc || impl
928 (if l then "|(" else "(") ++
929 show n ++ " : " ++ se 10 ty ++
932 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
934 Static -> "[static] "
936 se p (PPi (Imp l s _) n ty sc)
937 | impl = bracket p 2 $ (if l then "|{" else "{") ++
938 show n ++ " : " ++ se 10 ty ++
939 "} " ++ st ++ "-> " ++ se 10 sc
940 | otherwise = se 10 sc
942 Static -> "[static] "
944 se p (PPi (Constraint _ _ _) n ty sc)
945 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
946 se p (PPi (TacImp _ _ s _) n ty sc)
947 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
948 se p (PApp _ (PRef _ f) [])
950 se p (PApp _ (PRef _ op@(UN (f:_))) args)
951 | length (getExps args) == 2 && not impl && not (isAlpha f)
952 = let [l, r] = getExps args in
953 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
955 = let args = getExps as in
956 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
957 else concatMap seArg args
958 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
959 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
960 se p (PHidden tm) = "." ++ se 0 tm
963 | otherwise = "refl {" ++ se 10 t ++ "}"
964 se p (PResolveTC _) = "resolvetc"
965 se p (PTrue _) = "()"
966 se p (PFalse _) = "_|_"
967 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
968 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
969 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
970 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
971 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
973 se p (PConstant c) = show c
974 se p (PProof ts) = "proof { " ++ show ts ++ "}"
975 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
976 se p (PMetavar n) = "?" ++ show n
977 se p (PReturn f) = "return"
978 se p PImpossible = "impossible"
979 se p Placeholder = "_"
980 se p (PDoBlock _) = "do block show not implemented"
981 se p (PElabError s) = show s
982-- se p x = "Not implemented"
984 sArg (PImp _ _ n tm _) = siArg (n, tm)
985 sArg (PExp _ _ tm _) = seArg tm
986 sArg (PConstraint _ _ tm _) = scArg tm
987 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
989 seArg arg = " " ++ se 0 arg
990 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
991 scArg val = " {{" ++ se 10 val ++ "}}"
992 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
994 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
998instance Sized PTerm where
999 size (PQuote rawTerm) = size rawTerm
1000 size (PRef fc name) = size name
1001 size (PLam name ty bdy) = 1 + size ty + size bdy
1002 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1003 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1004 size (PTyped trm ty) = 1 + size trm + size ty
1005 size (PApp fc name args) = 1 + size args
1006 size (PCase fc trm bdy) = 1 + size trm + size bdy
1008 size (PFalse fc) = 1
1009 size (PRefl fc _) = 1
1010 size (PResolveTC fc) = 1
1011 size (PEq fc left right) = 1 + size left + size right
1012 size (PPair fc left right) = 1 + size left + size right
1013 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1014 size (PAlternative a alts) = 1 + size alts
1015 size (PHidden hidden) = size hidden
1017 size (PConstant const) = 1 + size const
1018 size Placeholder = 1
1019 size (PDoBlock dos) = 1 + size dos
1020 size (PIdiom fc term) = 1 + size term
1021 size (PReturn fc) = 1
1022 size (PMetavar name) = 1
1023 size (PProof tactics) = size tactics
1024 size (PElabError err) = size err
1025 size PImpossible = 1
1027getPArity :: PTerm -> Int
1028getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1031-- Return all names, free or globally bound, in the given term.
1033allNamesIn :: PTerm -> [Name]
1034allNamesIn tm = nub $ ni [] tm
1037 | not (n `elem` env) = [n]
1038 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1039 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1040 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1041 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1042 ni env (PHidden tm) = ni env tm
1043 ni env (PEq _ l r) = ni env l ++ ni env r
1044 ni env (PTyped l r) = ni env l ++ ni env r
1045 ni env (PPair _ l r) = ni env l ++ ni env r
1046 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1047 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1048 ni env (PAlternative a ls) = concatMap (ni env) ls
1051-- Return names which are free in the given term.
1053namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1054namesIn uvars ist tm = nub $ ni [] tm
1057 | not (n `elem` env)
1058 = case lookupTy Nothing n (tt_ctxt ist) of
1060 _ -> if n `elem` (map fst uvars) then [n] else []
1061 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1062 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1063 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1064 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1065 ni env (PEq _ l r) = ni env l ++ ni env r
1066 ni env (PTyped l r) = ni env l ++ ni env r
1067 ni env (PPair _ l r) = ni env l ++ ni env r
1068 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1069 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1070 ni env (PAlternative a as) = concatMap (ni env) as
1071 ni env (PHidden tm) = ni env tm
1074-- Return which of the given names are used in the given term.
1076usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1077usedNamesIn vars ist tm = nub $ ni [] tm
1080 | n `elem` vars && not (n `elem` env)
1081 = case lookupTy Nothing n (tt_ctxt ist) of
1084 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1085 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1086 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1087 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1088 ni env (PEq _ l r) = ni env l ++ ni env r
1089 ni env (PTyped l r) = ni env l ++ ni env r
1090 ni env (PPair _ l r) = ni env l ++ ni env r
1091 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1092 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1093 ni env (PAlternative a as) = concatMap (ni env) as
1094 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