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
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
292data PDecl' t = PFix FC Fixity [String] -- fixity declaration
293 | PTy String SyntaxInfo FC FnOpts Name t -- type declaration
294 | PClauses FC FnOpts Name [PClause' t] -- pattern clause
295 | PCAF FC Name t -- top level constant
296 | PData String SyntaxInfo FC Bool -- codata
297 (PData' t) -- data declaration
298 | PParams FC [(Name, t)] [PDecl' t] -- params block
299 | PNamespace String [PDecl' t] -- new namespace
300 | PRecord String SyntaxInfo FC Name t String Name t -- record declaration
301 | PClass String SyntaxInfo FC
304 [(Name, t)] -- parameters
305 [PDecl' t] -- declarations
306 | PInstance SyntaxInfo FC [t] -- constraints
309 t -- full instance type
310 (Maybe Name) -- explicit name
314 | PMutual FC [PDecl' t]
315 | PDirective (Idris ())
318deriving instance Binary PDecl'
321data PClause' t = PClause FC Name t [t] t [PDecl' t]
322 | PWith FC Name t [t] t [PDecl' t]
323 | PClauseR FC [t] t [PDecl' t]
324 | PWithR FC [t] t [PDecl' t]
327deriving instance Binary PClause'
330data PData' t = PDatadecl { d_name :: Name,
332 d_cons :: [(String, Name, t, FC)] }
333 | PLaterdecl { d_name :: Name, d_tcon :: t }
336deriving instance Binary PData'
339-- Handy to get a free function for applying PTerm -> PTerm functions
340-- across a program, by deriving Functor
342type PDecl = PDecl' PTerm
343type PData = PData' PTerm
344type PClause = PClause' PTerm
346-- get all the names declared in a decl
348declared :: PDecl -> [Name]
349declared (PFix _ _ _) = []
350declared (PTy _ _ _ _ n t) = [n]
351declared (PClauses _ _ n _) = [] -- not a declaration
352declared (PRecord _ _ _ n _ _ c _) = [n, c]
353declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
354 where fstt (_, a, _, _) = a
355declared (PParams _ _ ds) = concatMap declared ds
356declared (PMutual _ ds) = concatMap declared ds
357declared (PNamespace _ ds) = concatMap declared ds
358-- declared (PImport _) = []
360defined :: PDecl -> [Name]
361defined (PFix _ _ _) = []
362defined (PTy _ _ _ _ n t) = []
363defined (PClauses _ _ n _) = [n] -- not a declaration
364defined (PRecord _ _ _ n _ _ c _) = [n, c]
365defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
366 where fstt (_, a, _, _) = a
367defined (PParams _ _ ds) = concatMap defined ds
368defined (PMutual _ ds) = concatMap defined ds
369defined (PNamespace _ ds) = concatMap defined ds
370-- declared (PImport _) = []
372updateN :: [(Name, Name)] -> Name -> Name
373updateN ns n | Just n' <- lookup n ns = n'
376updateNs :: [(Name, Name)] -> PTerm -> PTerm
378updateNs ns t = mapPT updateRef t
379 where updateRef (PRef fc f) = PRef fc (updateN ns f)
382-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
384-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
385-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
386-- where updateCNs ns (PClause n l ts r ds)
387-- = PClause (updateN ns n) (fmap (updateNs ns) l)
388-- (map (fmap (updateNs ns)) ts)
389-- (fmap (updateNs ns) r)
390-- (map (updateDNs ns) ds)
393-- High level language terms
395data PTerm = PQuote Raw
398 | PLam Name PTerm PTerm
399 | PPi Plicity Name PTerm PTerm
400 | PLet Name PTerm PTerm PTerm
401 | PTyped PTerm PTerm -- term with explicit type
402 | PApp FC PTerm [PArg]
403 | PCase FC PTerm [(PTerm, PTerm)]
409 | PPair FC PTerm PTerm
410 | PDPair FC PTerm PTerm PTerm
411 | PAlternative Bool [PTerm] -- True if only one may work
412 | PHidden PTerm -- irrelevant or hidden pattern
421 | PTactics [PTactic] -- as PProof, but no auto solving
422 | PElabError Err -- error to report on elaboration
423 | PImpossible -- special case for declaring when an LHS can't typecheck
426deriving instance Binary PTerm
429mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
430mapPT f t = f (mpt t) where
431 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
432 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
433 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
434 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
435 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
436 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
437 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
438 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
439 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
440 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
441 mpt (PHidden t) = PHidden (mapPT f t)
442 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
443 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
444 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
448data PTactic' t = Intro [Name] | Intros | Focus Name
449 | Refine Name [Bool] | Rewrite t | LetTac Name t
450 | Exact t | Compute | Trivial
453 | ProofState | ProofTerm | Undo
454 | Try (PTactic' t) (PTactic' t)
455 | TSeq (PTactic' t) (PTactic' t)
457 deriving (Show, Eq, Functor)
459deriving instance Binary PTactic'
462instance Sized a => Sized (PTactic' a) where
463 size (Intro nms) = 1 + size nms
465 size (Focus nm) = 1 + size nm
466 size (Refine nm bs) = 1 + size nm + length bs
467 size (Rewrite t) = 1 + size t
468 size (LetTac nm t) = 1 + size nm + size t
469 size (Exact t) = 1 + size t
477 size (Try l r) = 1 + size l + size r
478 size (TSeq l r) = 1 + size l + size r
482type PTactic = PTactic' PTerm
484data PDo' t = DoExp FC t
489 deriving (Eq, Functor)
491deriving instance Binary PDo'
494instance Sized a => Sized (PDo' a) where
495 size (DoExp fc t) = 1 + size fc + size t
496 size (DoBind fc nm t) = 1 + size fc + size nm + size t
497 size (DoBindP fc l r) = 1 + size fc + size l + size r
498 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
499 size (DoLetP fc l r) = 1 + size fc + size l + size r
503-- The priority gives a hint as to elaboration order. Best to elaborate
504-- things early which will help give a more concrete type to other
505-- variables, e.g. a before (interpTy a).
507data PArg' t = PImp { priority :: Int,
508 lazyarg :: Bool, pname :: Name, getTm :: t,
510 | PExp { priority :: Int,
511 lazyarg :: Bool, getTm :: t,
513 | PConstraint { priority :: Int,
514 lazyarg :: Bool, getTm :: t,
516 | PTacImplicit { priority :: Int,
517 lazyarg :: Bool, pname :: Name,
521 deriving (Show, Eq, Functor)
523instance Sized a => Sized (PArg' a) where
524 size (PImp p l nm trm _) = 1 + size nm + size trm
525 size (PExp p l trm _) = 1 + size trm
526 size (PConstraint p l trm _) = 1 + size trm
527 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
530deriving instance Binary PArg'
533pimp n t = PImp 0 True n t ""
534pexp t = PExp 0 False t ""
535pconst t = PConstraint 0 False t ""
536ptacimp n s t = PTacImplicit 0 True n s t ""
538type PArg = PArg' PTerm
542data ClassInfo = CI { instanceName :: Name,
543 class_methods :: [(Name, (FnOpts, PTerm))],
544 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
545 class_params :: [Name],
546 class_instances :: [Name] }
549deriving instance Binary ClassInfo
552data OptInfo = Optimise { collapsible :: Bool,
553 forceable :: [Int], -- argument positions
557deriving instance Binary OptInfo
561data TypeInfo = TI { con_names :: [Name], codata :: Bool }
564deriving instance Binary TypeInfo
567-- Syntactic sugar info
569data DSL' t = DSL { dsl_bind :: t,
574 index_first :: Maybe t,
575 index_next :: Maybe t,
576 dsl_lambda :: Maybe t,
579 deriving (Show, Functor)
581deriving instance Binary DSL'
586data SynContext = PatternSyntax | TermSyntax | AnySyntax
589deriving instance Binary SynContext
592data Syntax = Rule [SSymbol] PTerm SynContext
595deriving instance Binary Syntax
598data SSymbol = Keyword Name
605deriving instance Binary SSymbol
608initDSL = DSL (PRef f (UN ">>="))
609 (PRef f (UN "return"))
617 where f = FC "(builtin)" 0
619data SyntaxInfo = Syn { using :: [(Name, PTerm)],
620 syn_params :: [(Name, PTerm)],
621 syn_namespace :: [String],
623 decoration :: Name -> Name,
625 implicitAllowed :: Bool,
629deriving instance Binary SyntaxInfo
632defaultSyntax = Syn [] [] [] [] id False False initDSL
634expandNS :: SyntaxInfo -> Name -> Name
635expandNS syn n@(NS _ _) = n
636expandNS syn n = case syn_namespace syn of
641--- Pretty printing declarations and terms
643instance Show PTerm where
644 show tm = showImp False tm
646instance Pretty PTerm where
647 pretty = prettyImp False
649instance Show PDecl where
650 show d = showDeclImp False d
652instance Show PClause where
653 show c = showCImp True c
655instance Show PData where
656 show d = showDImp False d
658showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
659showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
660showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
661showDeclImp _ (PData _ _ _ _ d) = show d
662showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
663 showSep "\n" (map show ps)
666showCImp :: Bool -> PClause -> String
667showCImp impl (PClause _ n l ws r w)
668 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
669 ++ " where " ++ show w
672 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
673showCImp impl (PWith _ n l ws r w)
674 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
675 ++ " { " ++ show w ++ " } "
678 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
681showDImp :: Bool -> PData -> String
682showDImp impl (PDatadecl n ty cons)
683 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
685 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
687getImps :: [PArg] -> [(Name, PTerm)]
689getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
690getImps (_ : xs) = getImps xs
692getExps :: [PArg] -> [PTerm]
694getExps (PExp _ _ tm _ : xs) = tm : getExps xs
695getExps (_ : xs) = getExps xs
697getConsts :: [PArg] -> [PTerm]
699getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
700getConsts (_ : xs) = getConsts xs
702getAll :: [PArg] -> [PTerm]
705prettyImp :: Bool -> PTerm -> Doc
706prettyImp impl = prettySe 10
708 prettySe p (PQuote r) =
709 if size r > breakingSize then
710 text "![" $$ pretty r <> text "]"
712 text "![" <> pretty r <> text "]"
713 prettySe p (PPatvar fc n) = pretty n
714 prettySe p (PRef fc n) =
720 prettyBasic n@(UN _) = pretty n
721 prettyBasic (MN _ s) = text s
722 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
723 prettySe p (PLam n ty sc) =
725 if size sc > breakingSize then
726 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
728 text "λ" <> pretty n <+> text "=>" <+> pretty sc
729 prettySe p (PLet n ty v sc) =
731 if size sc > breakingSize then
732 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
733 nest nestingSize (prettySe 10 sc)
735 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
737 prettySe p (PPi (Exp l s _) n ty sc)
738 | n `elem` allNamesIn sc || impl =
739 let open = if l then text "|" <> lparen else lparen in
741 if size sc > breakingSize then
742 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
743 st <+> text "->" $+$ prettySe 10 sc
745 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
746 st <+> text "->" <+> prettySe 10 sc
749 if size sc > breakingSize then
750 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
752 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
756 Static -> text "[static]"
758 prettySe p (PPi (Imp l s _) n ty sc)
760 let open = if l then text "|" <> lbrace else lbrace in
762 if size sc > breakingSize then
763 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
764 st <+> text "->" <+> prettySe 10 sc
766 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
767 st <+> text "->" <+> prettySe 10 sc
768 | otherwise = prettySe 10 sc
772 Static -> text $ "[static]"
774 prettySe p (PPi (Constraint _ _ _) n ty sc) =
776 if size sc > breakingSize then
777 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
779 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
780 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
782 if size sc > breakingSize then
783 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
784 rbrace <+> text "->" $+$ prettySe 10 sc
786 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
787 rbrace <+> text "->" <+> prettySe 10 sc
788 prettySe p (PApp _ (PRef _ f) [])
789 | not impl = pretty f
790 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
791 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
792 let [l, r] = getExps args in
794 if size r > breakingSize then
795 prettySe 1 l <+> pretty op $+$ prettySe 0 r
797 prettySe 1 l <+> pretty op <+> prettySe 0 r
798 prettySe p (PApp _ f as) =
799 let args = getExps as in
804 -- foldr (<+>) empty $ map prettyArgS as
807 -- foldr (<+>) empty $ map prettyArgSe args
810 if size r > breakingSize then
811 l $+$ nest nestingSize (prettyArgS r)
816 if size r > breakingSize then
817 l $+$ nest nestingSize (prettyArgSe r)
820 prettySe p (PCase _ scr opts) =
821 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
823 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
825 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
826 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
827 prettySe p (PRefl _ _) = text "refl"
828 prettySe p (PResolveTC _) = text "resolvetc"
829 prettySe p (PTrue _) = text "()"
830 prettySe p (PFalse _) = text "_|_"
831 prettySe p (PEq _ l r) =
833 if size r > breakingSize then
834 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
836 prettySe 10 l <+> text "=" <+> prettySe 10 r
837 prettySe p (PTyped l r) =
838 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
839 prettySe p (PPair _ l r) =
840 if size r > breakingSize then
841 lparen <> prettySe 10 l <> text "," $+$
842 prettySe 10 r <> rparen
844 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
845 prettySe p (PDPair _ l t r) =
846 if size r > breakingSize then
847 lparen <> prettySe 10 l <+> text "**" $+$
848 prettySe 10 r <> rparen
850 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
851 prettySe p (PAlternative a as) =
852 lparen <> text "|" <> prettyAs <> text "|" <> rparen
855 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
856 prettySe p PSet = text "Set"
857 prettySe p (PConstant c) = pretty c
858 -- XXX: add pretty for tactics
859 prettySe p (PProof ts) =
860 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
861 prettySe p (PTactics ts) =
862 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
863 prettySe p (PMetavar n) = text "?" <> pretty n
864 prettySe p (PReturn f) = text "return"
865 prettySe p PImpossible = text "impossible"
866 prettySe p Placeholder = text "_"
867 prettySe p (PDoBlock _) = text "do block pretty not implemented"
868 prettySe p (PElabError s) = pretty s
870 prettySe p _ = text "test"
872 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
873 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
874 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
875 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
877 prettyArgSe arg = prettySe 0 arg
878 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
879 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
880 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
882 bracket outer inner doc
883 | inner > outer = lparen <> doc <> rparen
886showImp :: Bool -> PTerm -> String
887showImp impl tm = se 10 tm where
888 se p (PQuote r) = "![" ++ show r ++ "]"
889 se p (PPatvar fc n) = show n
890 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
892 where showbasic n@(UN _) = show n
893 showbasic (MN _ s) = s
894 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
895 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
896 (if impl then " : " ++ se 10 ty else "") ++ " => "
898 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
900 se p (PPi (Exp l s _) n ty sc)
901 | n `elem` allNamesIn sc || impl
903 if l then "|(" else "(" ++
904 show n ++ " : " ++ se 10 ty ++
907 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
909 Static -> "[static] "
911 se p (PPi (Imp l s _) n ty sc)
912 | impl = bracket p 2 $ if l then "|{" else "{" ++
913 show n ++ " : " ++ se 10 ty ++
914 "} " ++ st ++ "-> " ++ se 10 sc
915 | otherwise = se 10 sc
917 Static -> "[static] "
919 se p (PPi (Constraint _ _ _) n ty sc)
920 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
921 se p (PPi (TacImp _ _ s _) n ty sc)
922 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
923 se p (PApp _ (PRef _ f) [])
925 se p (PApp _ (PRef _ op@(UN (f:_))) args)
926 | length (getExps args) == 2 && not impl && not (isAlpha f)
927 = let [l, r] = getExps args in
928 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
930 = let args = getExps as in
931 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
932 else concatMap seArg args
933 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
934 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
935 se p (PHidden tm) = "." ++ se 0 tm
938 | otherwise = "refl {" ++ se 10 t ++ "}"
939 se p (PResolveTC _) = "resolvetc"
940 se p (PTrue _) = "()"
941 se p (PFalse _) = "_|_"
942 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
943 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
944 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
945 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
946 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
948 se p (PConstant c) = show c
949 se p (PProof ts) = "proof { " ++ show ts ++ "}"
950 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
951 se p (PMetavar n) = "?" ++ show n
952 se p (PReturn f) = "return"
953 se p PImpossible = "impossible"
954 se p Placeholder = "_"
955 se p (PDoBlock _) = "do block show not implemented"
956 se p (PElabError s) = show s
957-- se p x = "Not implemented"
959 sArg (PImp _ _ n tm _) = siArg (n, tm)
960 sArg (PExp _ _ tm _) = seArg tm
961 sArg (PConstraint _ _ tm _) = scArg tm
962 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
964 seArg arg = " " ++ se 0 arg
965 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
966 scArg val = " {{" ++ se 10 val ++ "}}"
967 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
969 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
973instance Sized PTerm where
974 size (PQuote rawTerm) = size rawTerm
975 size (PRef fc name) = size name
976 size (PLam name ty bdy) = 1 + size ty + size bdy
977 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
978 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
979 size (PTyped trm ty) = 1 + size trm + size ty
980 size (PApp fc name args) = 1 + size args
981 size (PCase fc trm bdy) = 1 + size trm + size bdy
984 size (PRefl fc _) = 1
985 size (PResolveTC fc) = 1
986 size (PEq fc left right) = 1 + size left + size right
987 size (PPair fc left right) = 1 + size left + size right
988 size (PDPair fs left ty right) = 1 + size left + size ty + size right
989 size (PAlternative a alts) = 1 + size alts
990 size (PHidden hidden) = size hidden
992 size (PConstant const) = 1 + size const
994 size (PDoBlock dos) = 1 + size dos
995 size (PIdiom fc term) = 1 + size term
996 size (PReturn fc) = 1
997 size (PMetavar name) = 1
998 size (PProof tactics) = size tactics
999 size (PElabError err) = size err
1000 size PImpossible = 1
1002allNamesIn :: PTerm -> [Name]
1003allNamesIn tm = nub $ ni [] tm
1006 | not (n `elem` env) = [n]
1007 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1008 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1009 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1010 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1011 ni env (PHidden tm) = ni env tm
1012 ni env (PEq _ l r) = ni env l ++ ni env r
1013 ni env (PTyped l r) = ni env l ++ ni env r
1014 ni env (PPair _ l r) = ni env l ++ ni env r
1015 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1016 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1017 ni env (PAlternative a ls) = concatMap (ni env) ls
1020namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1021namesIn uvars ist tm = nub $ ni [] tm
1024 | not (n `elem` env)
1025 = case lookupTy Nothing n (tt_ctxt ist) of
1027 _ -> if n `elem` (map fst uvars) then [n] else []
1028 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1029 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1030 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1031 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1032 ni env (PEq _ l r) = ni env l ++ ni env r
1033 ni env (PTyped l r) = ni env l ++ ni env r
1034 ni env (PPair _ l r) = ni env l ++ ni env r
1035 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1036 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1037 ni env (PAlternative a as) = concatMap (ni env) as
1038 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