1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
8import Core.Elaborate hiding (Tactic(..))
15import System.Console.Haskeline
17import Control.Monad.State
25data IOption = IOption { opt_logLevel :: Int,
27 opt_typeintype :: Bool,
30 opt_errContext :: Bool,
33 opt_ibcsubdir :: FilePath,
34 opt_importdirs :: [FilePath],
35 opt_cmdline :: [Opt] -- remember whole command line
39defaultOpts = IOption 0 False False True False False True True "" [] []
41-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
42-- without typechecking).
43-- This will include all the functions and data declarations, plus fixity declarations
48 idris_constraints :: [(UConstraint, FC)],
49 idris_infixes :: [FixDecl],
50 idris_implicits :: Ctxt [PArg],
51 idris_statics :: Ctxt [Bool],
52 idris_classes :: Ctxt ClassInfo,
53 idris_dsls :: Ctxt DSL,
54 idris_optimisation :: Ctxt OptInfo,
55 idris_datatypes :: Ctxt TypeInfo,
56 idris_patdefs :: Ctxt [([Name], Term, Term)], -- not exported
57 idris_flags :: Ctxt [FnOpt],
58 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
59 idris_calledgraph :: Ctxt [Name],
60 idris_totcheck :: [(FC, Name)],
62 idris_options :: IOption,
64 idris_metavars :: [Name],
65 syntax_rules :: [Syntax],
66 syntax_keywords :: [String],
67 imported :: [FilePath],
68 idris_scprims :: [(Name, (Int, PrimFn))],
69 idris_objs :: [FilePath],
70 idris_libs :: [String],
71 idris_hdrs :: [String],
72 proof_list :: [(Name, [String])],
74 lastParse :: Maybe Name,
75 indent_stack :: [Int],
76 brace_stack :: [Maybe Int],
77 hide_list :: [(Name, Maybe Accessibility)],
78 default_access :: Accessibility,
79 ibc_write :: [IBCWrite],
80 compiled_so :: Maybe String
83data CGInfo = CGInfo { argsdef :: [Name],
84 calls :: [(Name, [[Name]])],
89deriving instance Binary CGInfo
92primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
94-- information that needs writing for the current module's .ibc file
95data IBCWrite = IBCFix FixDecl
99 | IBCInstance Bool Name Name
109 | IBCAccess Name Accessibility
110 | IBCTotal Name Totality
111 | IBCFlags Name [FnOpt]
113 | IBCDef Name -- i.e. main context
116idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
117 emptyContext emptyContext emptyContext emptyContext
118 emptyContext emptyContext emptyContext
119 [] "" defaultOpts 6 [] [] [] [] [] [] [] []
120 [] Nothing Nothing [] [] [] Hidden [] Nothing
122-- The monad for the main REPL - reading and processing files and updating
123-- global state (hence the IO inner monad).
124type Idris = StateT IState (InputT IO)
126-- Commands in the REPL
128data Target = ViaC | ViaJava
137 | Compile Target String
162data Opt = Filename String
194-- Parsed declarations
196data Fixity = Infixl { prec :: Int }
197 | Infixr { prec :: Int }
198 | InfixN { prec :: Int }
199 | PrefixN { prec :: Int }
202deriving instance Binary Fixity
205instance Show Fixity where
206 show (Infixl i) = "infixl " ++ show i
207 show (Infixr i) = "infixr " ++ show i
208 show (InfixN i) = "infix " ++ show i
209 show (PrefixN i) = "prefix " ++ show i
211data FixDecl = Fix Fixity String
214deriving instance Binary FixDecl
217instance Ord FixDecl where
218 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
221data Static = Static | Dynamic
224deriving instance Binary Static
227-- Mark bindings with their explicitness, and laziness
228data Plicity = Imp { plazy :: Bool,
230 | Exp { plazy :: Bool,
232 | Constraint { plazy :: Bool,
234 | TacImp { plazy :: Bool,
240deriving instance Binary Plicity
243impl = Imp False Dynamic
244expl = Exp False Dynamic
245constraint = Constraint False Dynamic
246tacimpl = TacImp False Dynamic
248data FnOpt = Inlinable | TotalFn | AssertTotal | TCGen
249 | CExport String -- export, with a C name
250 | Specialise [Name] -- specialise it, freeze these names
253deriving instance Binary FnOpt
258inlinable :: FnOpts -> Bool
259inlinable = elem Inlinable
261data PDecl' t = PFix FC Fixity [String] -- fixity declaration
262 | PTy SyntaxInfo FC FnOpts Name t -- type declaration
263 | PClauses FC FnOpts Name [PClause' t] -- pattern clause
264 | PCAF FC Name t -- top level constant
265 | PData SyntaxInfo FC (PData' t) -- data declaration
266 | PParams FC [(Name, t)] [PDecl' t] -- params block
267 | PNamespace String [PDecl' t] -- new namespace
268 | PRecord SyntaxInfo FC Name t Name t -- record declaration
269 | PClass SyntaxInfo FC
272 [(Name, t)] -- parameters
273 [PDecl' t] -- declarations
274 | PInstance SyntaxInfo FC [t] -- constraints
277 t -- full instance type
278 (Maybe Name) -- explicit name
282 | PDirective (Idris ())
285deriving instance Binary PDecl'
288data PClause' t = PClause FC Name t [t] t [PDecl' t]
289 | PWith FC Name t [t] t [PDecl' t]
290 | PClauseR FC [t] t [PDecl' t]
291 | PWithR FC [t] t [PDecl' t]
294deriving instance Binary PClause'
297data PData' t = PDatadecl { d_name :: Name,
299 d_cons :: [(Name, t, FC)] }
302deriving instance Binary PData'
305-- Handy to get a free function for applying PTerm -> PTerm functions
306-- across a program, by deriving Functor
308type PDecl = PDecl' PTerm
309type PData = PData' PTerm
310type PClause = PClause' PTerm
312-- get all the names declared in a decl
314declared :: PDecl -> [Name]
315declared (PFix _ _ _) = []
316declared (PTy _ _ _ n t) = [n]
317declared (PClauses _ _ n _) = [] -- not a declaration
318declared (PData _ _ (PDatadecl n _ ts)) = n : map fstt ts
319 where fstt (a, _, _) = a
320declared (PParams _ _ ds) = concatMap declared ds
321declared (PNamespace _ ds) = concatMap declared ds
322-- declared (PImport _) = []
324defined :: PDecl -> [Name]
325defined (PFix _ _ _) = []
326defined (PTy _ _ _ n t) = []
327defined (PClauses _ _ n _) = [n] -- not a declaration
328defined (PData _ _ (PDatadecl n _ ts)) = n : map fstt ts
329 where fstt (a, _, _) = a
330defined (PParams _ _ ds) = concatMap defined ds
331defined (PNamespace _ ds) = concatMap defined ds
332-- declared (PImport _) = []
334updateN :: [(Name, Name)] -> Name -> Name
335updateN ns n | Just n' <- lookup n ns = n'
338updateNs :: [(Name, Name)] -> PTerm -> PTerm
340updateNs ns t = mapPT updateRef t
341 where updateRef (PRef fc f) = PRef fc (updateN ns f)
344-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
346-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
347-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
348-- where updateCNs ns (PClause n l ts r ds)
349-- = PClause (updateN ns n) (fmap (updateNs ns) l)
350-- (map (fmap (updateNs ns)) ts)
351-- (fmap (updateNs ns) r)
352-- (map (updateDNs ns) ds)
355-- High level language terms
357data PTerm = PQuote Raw
360 | PLam Name PTerm PTerm
361 | PPi Plicity Name PTerm PTerm
362 | PLet Name PTerm PTerm PTerm
363 | PTyped PTerm PTerm -- term with explicit type
364 | PApp FC PTerm [PArg]
365 | PCase FC PTerm [(PTerm, PTerm)]
371 | PPair FC PTerm PTerm
372 | PDPair FC PTerm PTerm PTerm
373 | PAlternative Bool [PTerm] -- True if only one may work
374 | PHidden PTerm -- irrelevant or hidden pattern
383 | PTactics [PTactic] -- as PProof, but no auto solving
384 | PElabError Err -- error to report on elaboration
385 | PImpossible -- special case for declaring when an LHS can't typecheck
388deriving instance Binary PTerm
391mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
392mapPT f t = f (mpt t) where
393 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
394 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
395 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
396 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
397 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
398 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
399 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
400 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
401 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
402 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
403 mpt (PHidden t) = PHidden (mapPT f t)
404 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
405 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
406 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
410data PTactic' t = Intro [Name] | Intros | Focus Name
411 | Refine Name [Bool] | Rewrite t | LetTac Name t
412 | Exact t | Compute | Trivial
415 | ProofState | ProofTerm | Undo
416 | Try (PTactic' t) (PTactic' t)
417 | TSeq (PTactic' t) (PTactic' t)
419 deriving (Show, Eq, Functor)
421deriving instance Binary PTactic'
424instance Sized a => Sized (PTactic' a) where
425 size (Intro nms) = 1 + size nms
427 size (Focus nm) = 1 + size nm
428 size (Refine nm bs) = 1 + size nm + length bs
429 size (Rewrite t) = 1 + size t
430 size (LetTac nm t) = 1 + size nm + size t
431 size (Exact t) = 1 + size t
439 size (Try l r) = 1 + size l + size r
440 size (TSeq l r) = 1 + size l + size r
444type PTactic = PTactic' PTerm
446data PDo' t = DoExp FC t
451 deriving (Eq, Functor)
453deriving instance Binary PDo'
456instance Sized a => Sized (PDo' a) where
457 size (DoExp fc t) = 1 + size fc + size t
458 size (DoBind fc nm t) = 1 + size fc + size nm + size t
459 size (DoBindP fc l r) = 1 + size fc + size l + size r
460 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
461 size (DoLetP fc l r) = 1 + size fc + size l + size r
465-- The priority gives a hint as to elaboration order. Best to elaborate
466-- things early which will help give a more concrete type to other
467-- variables, e.g. a before (interpTy a).
469data PArg' t = PImp { priority :: Int,
470 lazyarg :: Bool, pname :: Name, getTm :: t }
471 | PExp { priority :: Int,
472 lazyarg :: Bool, getTm :: t }
473 | PConstraint { priority :: Int,
474 lazyarg :: Bool, getTm :: t }
475 | PTacImplicit { priority :: Int,
476 lazyarg :: Bool, pname :: Name,
479 deriving (Show, Eq, Functor)
481instance Sized a => Sized (PArg' a) where
482 size (PImp p l nm trm) = 1 + size nm + size trm
483 size (PExp p l trm) = 1 + size trm
484 size (PConstraint p l trm) = 1 + size trm
485 size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
488deriving instance Binary PArg'
493pconst = PConstraint 0 False
494ptacimp = PTacImplicit 0 True
496type PArg = PArg' PTerm
500data ClassInfo = CI { instanceName :: Name,
501 class_methods :: [(Name, (FnOpts, PTerm))],
502 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
503 class_params :: [Name],
504 class_instances :: [Name] }
507deriving instance Binary ClassInfo
510data OptInfo = Optimise { collapsible :: Bool,
511 forceable :: [Int], -- argument positions
515deriving instance Binary OptInfo
519data TypeInfo = TI { con_names :: [Name] }
522deriving instance Binary TypeInfo
525-- Syntactic sugar info
527data DSL' t = DSL { dsl_bind :: t,
532 index_first :: Maybe t,
533 index_next :: Maybe t,
534 dsl_lambda :: Maybe t,
537 deriving (Show, Functor)
539deriving instance Binary DSL'
544data SynContext = PatternSyntax | TermSyntax | AnySyntax
547deriving instance Binary SynContext
550data Syntax = Rule [SSymbol] PTerm SynContext
553deriving instance Binary Syntax
556data SSymbol = Keyword Name
563deriving instance Binary SSymbol
566initDSL = DSL (PRef f (UN ">>="))
567 (PRef f (UN "return"))
575 where f = FC "(builtin)" 0
577data SyntaxInfo = Syn { using :: [(Name, PTerm)],
578 syn_params :: [(Name, PTerm)],
579 syn_namespace :: [String],
581 decoration :: Name -> Name,
583 implicitAllowed :: Bool,
587deriving instance Binary SyntaxInfo
590defaultSyntax = Syn [] [] [] [] id False False initDSL
592expandNS :: SyntaxInfo -> Name -> Name
593expandNS syn n@(NS _ _) = n
594expandNS syn n = case syn_namespace syn of
599--- Pretty printing declarations and terms
601instance Show PTerm where
602 show tm = showImp False tm
604instance Pretty PTerm where
605 pretty = prettyImp False
607instance Show PDecl where
608 show d = showDeclImp False d
610instance Show PClause where
611 show c = showCImp True c
613instance Show PData where
614 show d = showDImp False d
616showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
617showDeclImp t (PTy _ _ _ n ty) = show n ++ " : " ++ showImp t ty
618showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
619showDeclImp _ (PData _ _ d) = show d
620showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
621 showSep "\n" (map show ps)
624showCImp :: Bool -> PClause -> String
625showCImp impl (PClause _ n l ws r w)
626 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
627 ++ " where " ++ show w
630 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
631showCImp impl (PWith _ n l ws r w)
632 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
633 ++ " { " ++ show w ++ " } "
636 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
639showDImp :: Bool -> PData -> String
640showDImp impl (PDatadecl n ty cons)
641 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
643 (map (\ (n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
645getImps :: [PArg] -> [(Name, PTerm)]
647getImps (PImp _ _ n tm : xs) = (n, tm) : getImps xs
648getImps (_ : xs) = getImps xs
650getExps :: [PArg] -> [PTerm]
652getExps (PExp _ _ tm : xs) = tm : getExps xs
653getExps (_ : xs) = getExps xs
655getConsts :: [PArg] -> [PTerm]
657getConsts (PConstraint _ _ tm : xs) = tm : getConsts xs
658getConsts (_ : xs) = getConsts xs
660getAll :: [PArg] -> [PTerm]
663prettyImp :: Bool -> PTerm -> Doc
664prettyImp impl = prettySe 10
666 prettySe p (PQuote r) =
667 if size r > breakingSize then
668 text "![" $$ pretty r <> text "]"
670 text "![" <> pretty r <> text "]"
671 prettySe p (PPatvar fc n) = pretty n
672 prettySe p (PRef fc n) =
678 prettyBasic n@(UN _) = pretty n
679 prettyBasic (MN _ s) = text s
680 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
681 prettySe p (PLam n ty sc) =
683 if size sc > breakingSize then
684 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
686 text "λ" <> pretty n <+> text "=>" <+> pretty sc
687 prettySe p (PLet n ty v sc) =
689 if size sc > breakingSize then
690 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
691 nest nestingSize (prettySe 10 sc)
693 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
695 prettySe p (PPi (Exp l s) n ty sc)
696 | n `elem` allNamesIn sc || impl =
697 let open = if l then text "|" <> lparen else lparen in
699 if size sc > breakingSize then
700 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
701 st <+> text "->" $+$ prettySe 10 sc
703 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
704 st <+> text "->" <+> prettySe 10 sc
707 if size sc > breakingSize then
708 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
710 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
714 Static -> text "[static]"
716 prettySe p (PPi (Imp l s) n ty sc)
718 let open = if l then text "|" <> lbrace else lbrace in
720 if size sc > breakingSize then
721 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
722 st <+> text "->" <+> prettySe 10 sc
724 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
725 st <+> text "->" <+> prettySe 10 sc
726 | otherwise = prettySe 10 sc
730 Static -> text $ "[static]"
732 prettySe p (PPi (Constraint _ _) n ty sc) =
734 if size sc > breakingSize then
735 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
737 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
738 prettySe p (PPi (TacImp _ _ s) n ty sc) =
740 if size sc > breakingSize then
741 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
742 rbrace <+> text "->" $+$ prettySe 10 sc
744 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
745 rbrace <+> text "->" <+> prettySe 10 sc
746 prettySe p (PApp _ (PRef _ f) [])
747 | not impl = pretty f
748 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
749 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
750 let [l, r] = getExps args in
752 if size r > breakingSize then
753 prettySe 1 l <+> pretty op $+$ prettySe 0 r
755 prettySe 1 l <+> pretty op <+> prettySe 0 r
756 prettySe p (PApp _ f as) =
757 let args = getExps as in
762 -- foldr (<+>) empty $ map prettyArgS as
765 -- foldr (<+>) empty $ map prettyArgSe args
768 if size r > breakingSize then
769 l $+$ nest nestingSize (prettyArgS r)
774 if size r > breakingSize then
775 l $+$ nest nestingSize (prettyArgSe r)
778 prettySe p (PCase _ scr opts) =
779 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
781 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
783 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
784 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
785 prettySe p (PRefl _ _) = text "refl"
786 prettySe p (PResolveTC _) = text "resolvetc"
787 prettySe p (PTrue _) = text "()"
788 prettySe p (PFalse _) = text "_|_"
789 prettySe p (PEq _ l r) =
791 if size r > breakingSize then
792 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
794 prettySe 10 l <+> text "=" <+> prettySe 10 r
795 prettySe p (PTyped l r) =
796 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
797 prettySe p (PPair _ l r) =
798 if size r > breakingSize then
799 lparen <> prettySe 10 l <> text "," $+$
800 prettySe 10 r <> rparen
802 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
803 prettySe p (PDPair _ l t r) =
804 if size r > breakingSize then
805 lparen <> prettySe 10 l <+> text "**" $+$
806 prettySe 10 r <> rparen
808 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
809 prettySe p (PAlternative a as) =
810 lparen <> text "|" <> prettyAs <> text "|" <> rparen
813 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
814 prettySe p PSet = text "Set"
815 prettySe p (PConstant c) = pretty c
816 -- XXX: add pretty for tactics
817 prettySe p (PProof ts) =
818 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
819 prettySe p (PTactics ts) =
820 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
821 prettySe p (PMetavar n) = text "?" <> pretty n
822 prettySe p (PReturn f) = text "return"
823 prettySe p PImpossible = text "impossible"
824 prettySe p Placeholder = text "_"
825 prettySe p (PDoBlock _) = text "do block pretty not implemented"
826 prettySe p (PElabError s) = pretty s
828 prettySe p _ = text "test"
830 prettyArgS (PImp _ _ n tm) = prettyArgSi (n, tm)
831 prettyArgS (PExp _ _ tm) = prettyArgSe tm
832 prettyArgS (PConstraint _ _ tm) = prettyArgSc tm
833 prettyArgS (PTacImplicit _ _ n _ tm) = prettyArgSti (n, tm)
835 prettyArgSe arg = prettySe 0 arg
836 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
837 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
838 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
840 bracket outer inner doc
841 | inner > outer = lparen <> doc <> rparen
844showImp :: Bool -> PTerm -> String
845showImp impl tm = se 10 tm where
846 se p (PQuote r) = "![" ++ show r ++ "]"
847 se p (PPatvar fc n) = show n
848 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
850 where showbasic n@(UN _) = show n
851 showbasic (MN _ s) = s
852 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
853 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
854 (if impl then " : " ++ se 10 ty else "") ++ " => "
856 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
858 se p (PPi (Exp l s) n ty sc)
859 | n `elem` allNamesIn sc || impl
861 if l then "|(" else "(" ++
862 show n ++ " : " ++ se 10 ty ++
865 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
867 Static -> "[static] "
869 se p (PPi (Imp l s) n ty sc)
870 | impl = bracket p 2 $ if l then "|{" else "{" ++
871 show n ++ " : " ++ se 10 ty ++
872 "} " ++ st ++ "-> " ++ se 10 sc
873 | otherwise = se 10 sc
875 Static -> "[static] "
877 se p (PPi (Constraint _ _) n ty sc)
878 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
879 se p (PPi (TacImp _ _ s) n ty sc)
880 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
881 se p (PApp _ (PRef _ f) [])
883 se p (PApp _ (PRef _ op@(UN (f:_))) args)
884 | length (getExps args) == 2 && not impl && not (isAlpha f)
885 = let [l, r] = getExps args in
886 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
888 = let args = getExps as in
889 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
890 else concatMap seArg args
891 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
892 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
893 se p (PHidden tm) = "." ++ se 0 tm
896 | otherwise = "refl {" ++ se 10 t ++ "}"
897 se p (PResolveTC _) = "resolvetc"
898 se p (PTrue _) = "()"
899 se p (PFalse _) = "_|_"
900 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
901 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
902 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
903 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
904 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
906 se p (PConstant c) = show c
907 se p (PProof ts) = "proof { " ++ show ts ++ "}"
908 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
909 se p (PMetavar n) = "?" ++ show n
910 se p (PReturn f) = "return"
911 se p PImpossible = "impossible"
912 se p Placeholder = "_"
913 se p (PDoBlock _) = "do block show not implemented"
914 se p (PElabError s) = show s
915-- se p x = "Not implemented"
917 sArg (PImp _ _ n tm) = siArg (n, tm)
918 sArg (PExp _ _ tm) = seArg tm
919 sArg (PConstraint _ _ tm) = scArg tm
920 sArg (PTacImplicit _ _ n _ tm) = stiArg (n, tm)
922 seArg arg = " " ++ se 0 arg
923 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
924 scArg val = " {{" ++ se 10 val ++ "}}"
925 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
927 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
933 | PLam Name PTerm PTerm
934 | PPi Plicity Name PTerm PTerm
935 | PLet Name PTerm PTerm PTerm
936 | PTyped PTerm PTerm -- term with explicit type
937 | PApp FC PTerm [PArg]
938 | PCase FC PTerm [(PTerm, PTerm)]
944 | PPair FC PTerm PTerm
945 | PDPair FC PTerm PTerm PTerm
946 | PAlternative [PTerm]
947 | PHidden PTerm -- irrelevant or hidden pattern
956 | PTactics [PTactic] -- as PProof, but no auto solving
957 | PElabError Err -- error to report on elaboration
958 | PImpossible -- special case for declaring when an LHS can't typecheck
961instance Sized PTerm where
962 size (PQuote rawTerm) = size rawTerm
963 size (PRef fc name) = size name
964 size (PLam name ty bdy) = 1 + size ty + size bdy
965 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
966 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
967 size (PTyped trm ty) = 1 + size trm + size ty
968 size (PApp fc name args) = 1 + size args
969 size (PCase fc trm bdy) = 1 + size trm + size bdy
972 size (PRefl fc _) = 1
973 size (PResolveTC fc) = 1
974 size (PEq fc left right) = 1 + size left + size right
975 size (PPair fc left right) = 1 + size left + size right
976 size (PDPair fs left ty right) = 1 + size left + size ty + size right
977 size (PAlternative a alts) = 1 + size alts
978 size (PHidden hidden) = size hidden
980 size (PConstant const) = 1 + size const
982 size (PDoBlock dos) = 1 + size dos
983 size (PIdiom fc term) = 1 + size term
984 size (PReturn fc) = 1
985 size (PMetavar name) = 1
986 size (PProof tactics) = size tactics
987 size (PElabError err) = size err
990allNamesIn :: PTerm -> [Name]
991allNamesIn tm = nub $ ni [] tm
994 | not (n `elem` env) = [n]
995 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
996 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
997 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
998 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
999 ni env (PHidden tm) = ni env tm
1000 ni env (PEq _ l r) = ni env l ++ ni env r
1001 ni env (PTyped l r) = ni env l ++ ni env r
1002 ni env (PPair _ l r) = ni env l ++ ni env r
1003 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1004 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1005 ni env (PAlternative a ls) = concatMap (ni env) ls
1008namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1009namesIn uvars ist tm = nub $ ni [] tm
1012 | not (n `elem` env)
1013 = case lookupTy Nothing n (tt_ctxt ist) of
1015 _ -> if n `elem` (map fst uvars) then [n] else []
1016 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1017 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1018 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1019 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1020 ni env (PEq _ l r) = ni env l ++ ni env r
1021 ni env (PTyped l r) = ni env l ++ ni env r
1022 ni env (PPair _ l r) = ni env l ++ ni env r
1023 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1024 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1025 ni env (PAlternative a as) = concatMap (ni env) as
1026 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