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
49-- | The global state used in the Idris monad
51 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
52 idris_constraints :: [(UConstraint, FC)],
53 -- ^ A list of universe constraints and their corresponding source locations
54 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
55 idris_implicits :: Ctxt [PArg],
56 idris_statics :: Ctxt [Bool],
57 idris_classes :: Ctxt ClassInfo,
58 idris_dsls :: Ctxt DSL,
59 idris_optimisation :: Ctxt OptInfo,
60 idris_datatypes :: Ctxt TypeInfo,
61 idris_patdefs :: Ctxt [([Name], Term, Term)], -- not exported
62 idris_flags :: Ctxt [FnOpt],
63 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
64 idris_calledgraph :: Ctxt [Name],
65 idris_docstrings :: Ctxt String,
66 idris_totcheck :: [(FC, Name)],
68 idris_options :: IOption,
70 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
71 idris_coercions :: [Name],
72 syntax_rules :: [Syntax],
73 syntax_keywords :: [String],
74 imported :: [FilePath], -- ^ The imported modules
75 idris_scprims :: [(Name, (Int, PrimFn))],
76 idris_objs :: [FilePath],
77 idris_libs :: [String],
78 idris_hdrs :: [String],
79 proof_list :: [(Name, [String])],
81 lastParse :: Maybe Name,
82 indent_stack :: [Int],
83 brace_stack :: [Maybe Int],
84 hide_list :: [(Name, Maybe Accessibility)],
85 default_access :: Accessibility,
86 default_total :: Bool,
87 ibc_write :: [IBCWrite],
88 compiled_so :: Maybe String
91data SizeChange = Smaller | Same | Bigger | Unknown
94deriving instance Binary SizeChange
97type SCGEntry = (Name, [Maybe (Int, SizeChange)])
99data CGInfo = CGInfo { argsdef :: [Name],
100 calls :: [(Name, [[Name]])],
106deriving instance Binary CGInfo
109primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
111-- information that needs writing for the current module's .ibc file
112data IBCWrite = IBCFix FixDecl
116 | IBCInstance Bool Name Name
126 | IBCAccess Name Accessibility
127 | IBCTotal Name Totality
128 | IBCFlags Name [FnOpt]
132 | IBCDef Name -- i.e. main context
135idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
136 emptyContext emptyContext emptyContext emptyContext
137 emptyContext emptyContext emptyContext emptyContext
138 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] []
139 [] Nothing Nothing [] [] [] Hidden False [] Nothing
141-- | The monad for the main REPL - reading and processing files and updating
142-- global state (hence the IO inner monad).
143type Idris = StateT IState IO
145-- Commands in the REPL
165 | Compile Target String
171 | AddProof (Maybe Name)
190data Opt = Filename String
222 | OutputTy OutputType
225-- Parsed declarations
227data Fixity = Infixl { prec :: Int }
228 | Infixr { prec :: Int }
229 | InfixN { prec :: Int }
230 | PrefixN { prec :: Int }
233deriving instance Binary Fixity
236instance Show Fixity where
237 show (Infixl i) = "infixl " ++ show i
238 show (Infixr i) = "infixr " ++ show i
239 show (InfixN i) = "infix " ++ show i
240 show (PrefixN i) = "prefix " ++ show i
242data FixDecl = Fix Fixity String
245instance Show FixDecl where
246 show (Fix f s) = show f ++ " " ++ s
249deriving instance Binary FixDecl
252instance Ord FixDecl where
253 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
256data Static = Static | Dynamic
259deriving instance Binary Static
262-- Mark bindings with their explicitness, and laziness
263data Plicity = Imp { plazy :: Bool,
266 | Exp { plazy :: Bool,
269 | Constraint { plazy :: Bool,
272 | TacImp { plazy :: Bool,
279deriving instance Binary Plicity
282impl = Imp False Dynamic ""
283expl = Exp False Dynamic ""
284constraint = Constraint False Dynamic ""
285tacimpl t = TacImp False Dynamic t ""
287data FnOpt = Inlinable | TotalFn | PartialFn
288 | Coinductive | AssertTotal | TCGen
289 | Implicit -- implicit coercion
290 | CExport String -- export, with a C name
291 | Specialise [Name] -- specialise it, freeze these names
294deriving instance Binary FnOpt
299inlinable :: FnOpts -> Bool
300inlinable = elem Inlinable
302-- | Top-level declarations such as compiler directives, definitions,
303-- datatypes and typeclasses.
305 = PFix FC Fixity [String] -- ^ Fixity declaration
306 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
307 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
308 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
309 | PCAF FC Name t -- ^ Top level constant
310 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
311 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
312 | PNamespace String [PDecl' t] -- ^ New namespace
313 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
314 | PClass String SyntaxInfo FC
317 [(Name, t)] -- parameters
318 [PDecl' t] -- declarations
319 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
320 -- class name, parameters, method declarations
321 | PInstance SyntaxInfo FC [t] -- constraints
324 t -- full instance type
325 (Maybe Name) -- explicit name
327 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
328 -- class name, parameters, full instance type, optional explicit name, and definitions
329 | PDSL Name (DSL' t) -- ^ DSL declaration
330 | PSyntax FC Syntax -- ^ Syntax definition
331 | PMutual FC [PDecl' t] -- ^ Mutual block
332 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
333 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
336deriving instance Binary PDecl'
339-- | One clause of a top-level definition. Term arguments to constructors are:
341-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
343-- 2. The list of patterns
345-- 3. The right-hand side
347-- 4. The where block (PDecl' t)
348data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
349 | PWith FC Name t [t] t [PDecl' t]
350 | PClauseR FC [t] t [PDecl' t]
351 | PWithR FC [t] t [PDecl' t]
354deriving instance Binary PClause'
358data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
359 d_tcon :: t, -- ^ Type constructor
360 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
362 -- ^ Data declaration
363 | PLaterdecl { d_name :: Name, d_tcon :: t }
364 -- ^ "Placeholder" for data whose constructors are defined later
367deriving instance Binary PData'
370-- Handy to get a free function for applying PTerm -> PTerm functions
371-- across a program, by deriving Functor
373type PDecl = PDecl' PTerm
374type PData = PData' PTerm
375type PClause = PClause' PTerm
377-- get all the names declared in a decl
379declared :: PDecl -> [Name]
380declared (PFix _ _ _) = []
381declared (PTy _ _ _ _ n t) = [n]
382declared (PPostulate _ _ _ _ n t) = [n]
383declared (PClauses _ _ n _) = [] -- not a declaration
384declared (PRecord _ _ _ n _ _ c _) = [n, c]
385declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
386 where fstt (_, a, _, _) = a
387declared (PParams _ _ ds) = concatMap declared ds
388declared (PMutual _ ds) = concatMap declared ds
389declared (PNamespace _ ds) = concatMap declared ds
391-- get the names declared, not counting nested parameter blocks
392tldeclared :: PDecl -> [Name]
393tldeclared (PFix _ _ _) = []
394tldeclared (PTy _ _ _ _ n t) = [n]
395tldeclared (PPostulate _ _ _ _ n t) = [n]
396tldeclared (PClauses _ _ n _) = [] -- not a declaration
397tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
398tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
399 where fstt (_, a, _, _) = a
400tldeclared (PParams _ _ ds) = []
401tldeclared (PMutual _ ds) = concatMap tldeclared ds
402tldeclared (PNamespace _ ds) = concatMap tldeclared ds
403-- declared (PImport _) = []
405defined :: PDecl -> [Name]
406defined (PFix _ _ _) = []
407defined (PTy _ _ _ _ n t) = []
408defined (PPostulate _ _ _ _ n t) = []
409defined (PClauses _ _ n _) = [n] -- not a declaration
410defined (PRecord _ _ _ n _ _ c _) = [n, c]
411defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
412 where fstt (_, a, _, _) = a
413defined (PParams _ _ ds) = concatMap defined ds
414defined (PMutual _ ds) = concatMap defined ds
415defined (PNamespace _ ds) = concatMap defined ds
416-- declared (PImport _) = []
418updateN :: [(Name, Name)] -> Name -> Name
419updateN ns n | Just n' <- lookup n ns = n'
422updateNs :: [(Name, Name)] -> PTerm -> PTerm
424updateNs ns t = mapPT updateRef t
425 where updateRef (PRef fc f) = PRef fc (updateN ns f)
428-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
430-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
431-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
432-- where updateCNs ns (PClause n l ts r ds)
433-- = PClause (updateN ns n) (fmap (updateNs ns) l)
434-- (map (fmap (updateNs ns)) ts)
435-- (fmap (updateNs ns) r)
436-- (map (updateDNs ns) ds)
439-- | High level language terms
440data PTerm = PQuote Raw
442 | PInferRef FC Name -- ^ A name to be defined later
444 | PLam Name PTerm PTerm
445 | PPi Plicity Name PTerm PTerm
446 | PLet Name PTerm PTerm PTerm
447 | PTyped PTerm PTerm -- ^ Term with explicit type
448 | PApp FC PTerm [PArg]
449 | PCase FC PTerm [(PTerm, PTerm)]
455 | PPair FC PTerm PTerm
456 | PDPair FC PTerm PTerm PTerm
457 | PAlternative Bool [PTerm] -- True if only one may work
458 | PHidden PTerm -- ^ Irrelevant or hidden pattern
466 | PProof [PTactic] -- ^ Proof script
467 | PTactics [PTactic] -- ^ As PProof, but no auto solving
468 | PElabError Err -- ^ Error to report on elaboration
469 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
470 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
473deriving instance Binary PTerm
476mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
477mapPT f t = f (mpt t) where
478 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
479 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
480 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
481 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
482 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
483 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
484 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
485 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
486 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
487 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
488 mpt (PHidden t) = PHidden (mapPT f t)
489 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
490 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
491 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
495data PTactic' t = Intro [Name] | Intros | Focus Name
496 | Refine Name [Bool] | Rewrite t
497 | LetTac Name t | LetTacTy Name t t
498 | Exact t | Compute | Trivial
501 | ProofState | ProofTerm | Undo
502 | Try (PTactic' t) (PTactic' t)
503 | TSeq (PTactic' t) (PTactic' t)
504 | ReflectTac t -- see Language.Reflection module
505 | GoalType String (PTactic' t)
507 deriving (Show, Eq, Functor)
509deriving instance Binary PTactic'
512instance Sized a => Sized (PTactic' a) where
513 size (Intro nms) = 1 + size nms
515 size (Focus nm) = 1 + size nm
516 size (Refine nm bs) = 1 + size nm + length bs
517 size (Rewrite t) = 1 + size t
518 size (LetTac nm t) = 1 + size nm + size t
519 size (Exact t) = 1 + size t
527 size (Try l r) = 1 + size l + size r
528 size (TSeq l r) = 1 + size l + size r
532type PTactic = PTactic' PTerm
534data PDo' t = DoExp FC t
539 deriving (Eq, Functor)
541deriving instance Binary PDo'
544instance Sized a => Sized (PDo' a) where
545 size (DoExp fc t) = 1 + size fc + size t
546 size (DoBind fc nm t) = 1 + size fc + size nm + size t
547 size (DoBindP fc l r) = 1 + size fc + size l + size r
548 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
549 size (DoLetP fc l r) = 1 + size fc + size l + size r
553-- The priority gives a hint as to elaboration order. Best to elaborate
554-- things early which will help give a more concrete type to other
555-- variables, e.g. a before (interpTy a).
556-- TODO: priority no longer serves any purpose, drop it!
558data PArg' t = PImp { priority :: Int,
559 lazyarg :: Bool, pname :: Name, getTm :: t,
561 | PExp { priority :: Int,
562 lazyarg :: Bool, getTm :: t,
564 | PConstraint { priority :: Int,
565 lazyarg :: Bool, getTm :: t,
567 | PTacImplicit { priority :: Int,
568 lazyarg :: Bool, pname :: Name,
572 deriving (Show, Eq, Functor)
574instance Sized a => Sized (PArg' a) where
575 size (PImp p l nm trm _) = 1 + size nm + size trm
576 size (PExp p l trm _) = 1 + size trm
577 size (PConstraint p l trm _) = 1 + size trm
578 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
581deriving instance Binary PArg'
584pimp n t = PImp 0 True n t ""
585pexp t = PExp 0 False t ""
586pconst t = PConstraint 0 False t ""
587ptacimp n s t = PTacImplicit 0 True n s t ""
589type PArg = PArg' PTerm
593data ClassInfo = CI { instanceName :: Name,
594 class_methods :: [(Name, (FnOpts, PTerm))],
595 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
596 class_params :: [Name],
597 class_instances :: [Name] }
600deriving instance Binary ClassInfo
603data OptInfo = Optimise { collapsible :: Bool,
604 forceable :: [Int], -- argument positions
608deriving instance Binary OptInfo
612data TypeInfo = TI { con_names :: [Name],
617deriving instance Binary TypeInfo
620-- Syntactic sugar info
622data DSL' t = DSL { dsl_bind :: t,
627 index_first :: Maybe t,
628 index_next :: Maybe t,
629 dsl_lambda :: Maybe t,
632 deriving (Show, Functor)
634deriving instance Binary DSL'
639data SynContext = PatternSyntax | TermSyntax | AnySyntax
642deriving instance Binary SynContext
645data Syntax = Rule [SSymbol] PTerm SynContext
648deriving instance Binary Syntax
651data SSymbol = Keyword Name
658deriving instance Binary SSymbol
661initDSL = DSL (PRef f (UN ">>="))
662 (PRef f (UN "return"))
670 where f = FC "(builtin)" 0
672data SyntaxInfo = Syn { using :: [(Name, PTerm)],
673 syn_params :: [(Name, PTerm)],
674 syn_namespace :: [String],
676 decoration :: Name -> Name,
678 implicitAllowed :: Bool,
682deriving instance Binary SyntaxInfo
685defaultSyntax = Syn [] [] [] [] id False False initDSL
687expandNS :: SyntaxInfo -> Name -> Name
688expandNS syn n@(NS _ _) = n
689expandNS syn n = case syn_namespace syn of
694--- Pretty printing declarations and terms
696instance Show PTerm where
697 show tm = showImp False tm
699instance Pretty PTerm where
700 pretty = prettyImp False
702instance Show PDecl where
703 show d = showDeclImp False d
705instance Show PClause where
706 show c = showCImp True c
708instance Show PData where
709 show d = showDImp False d
711showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
712showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
713showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
714showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
715showDeclImp _ (PData _ _ _ _ d) = show d
716showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
717 showSep "\n" (map show ps)
720showCImp :: Bool -> PClause -> String
721showCImp impl (PClause _ n l ws r w)
722 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
723 ++ " where " ++ show w
726 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
727showCImp impl (PWith _ n l ws r w)
728 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
729 ++ " { " ++ show w ++ " } "
732 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
735showDImp :: Bool -> PData -> String
736showDImp impl (PDatadecl n ty cons)
737 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
739 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
741getImps :: [PArg] -> [(Name, PTerm)]
743getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
744getImps (_ : xs) = getImps xs
746getExps :: [PArg] -> [PTerm]
748getExps (PExp _ _ tm _ : xs) = tm : getExps xs
749getExps (_ : xs) = getExps xs
751getConsts :: [PArg] -> [PTerm]
753getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
754getConsts (_ : xs) = getConsts xs
756getAll :: [PArg] -> [PTerm]
759prettyImp :: Bool -> PTerm -> Doc
760prettyImp impl = prettySe 10
762 prettySe p (PQuote r) =
763 if size r > breakingSize then
764 text "![" $$ pretty r <> text "]"
766 text "![" <> pretty r <> text "]"
767 prettySe p (PPatvar fc n) = pretty n
768 prettySe p (PRef fc n) =
774 prettyBasic n@(UN _) = pretty n
775 prettyBasic (MN _ s) = text s
776 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
777 prettySe p (PLam n ty sc) =
779 if size sc > breakingSize then
780 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
782 text "λ" <> pretty n <+> text "=>" <+> pretty sc
783 prettySe p (PLet n ty v sc) =
785 if size sc > breakingSize then
786 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
787 nest nestingSize (prettySe 10 sc)
789 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
791 prettySe p (PPi (Exp l s _) n ty sc)
792 | n `elem` allNamesIn sc || impl =
793 let open = if l then text "|" <> lparen else lparen in
795 if size sc > breakingSize then
796 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
797 st <+> text "->" $+$ prettySe 10 sc
799 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
800 st <+> text "->" <+> prettySe 10 sc
803 if size sc > breakingSize then
804 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
806 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
810 Static -> text "[static]"
812 prettySe p (PPi (Imp l s _) n ty sc)
814 let open = if l then text "|" <> lbrace else lbrace in
816 if size sc > breakingSize then
817 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
818 st <+> text "->" <+> prettySe 10 sc
820 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
821 st <+> text "->" <+> prettySe 10 sc
822 | otherwise = prettySe 10 sc
826 Static -> text $ "[static]"
828 prettySe p (PPi (Constraint _ _ _) n ty sc) =
830 if size sc > breakingSize then
831 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
833 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
834 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
836 if size sc > breakingSize then
837 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
838 rbrace <+> text "->" $+$ prettySe 10 sc
840 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
841 rbrace <+> text "->" <+> prettySe 10 sc
842 prettySe p (PApp _ (PRef _ f) [])
843 | not impl = pretty f
844 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
845 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
846 let [l, r] = getExps args in
848 if size r > breakingSize then
849 prettySe 1 l <+> pretty op $+$ prettySe 0 r
851 prettySe 1 l <+> pretty op <+> prettySe 0 r
852 prettySe p (PApp _ f as) =
853 let args = getExps as in
858 -- foldr (<+>) empty $ map prettyArgS as
861 -- foldr (<+>) empty $ map prettyArgSe args
864 if size r > breakingSize then
865 l $+$ nest nestingSize (prettyArgS r)
870 if size r > breakingSize then
871 l $+$ nest nestingSize (prettyArgSe r)
874 prettySe p (PCase _ scr opts) =
875 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
877 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
879 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
880 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
881 prettySe p (PRefl _ _) = text "refl"
882 prettySe p (PResolveTC _) = text "resolvetc"
883 prettySe p (PTrue _) = text "()"
884 prettySe p (PFalse _) = text "_|_"
885 prettySe p (PEq _ l r) =
887 if size r > breakingSize then
888 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
890 prettySe 10 l <+> text "=" <+> prettySe 10 r
891 prettySe p (PTyped l r) =
892 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
893 prettySe p (PPair _ l r) =
894 if size r > breakingSize then
895 lparen <> prettySe 10 l <> text "," $+$
896 prettySe 10 r <> rparen
898 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
899 prettySe p (PDPair _ l t r) =
900 if size r > breakingSize then
901 lparen <> prettySe 10 l <+> text "**" $+$
902 prettySe 10 r <> rparen
904 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
905 prettySe p (PAlternative a as) =
906 lparen <> text "|" <> prettyAs <> text "|" <> rparen
909 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
910 prettySe p PType = text "Type"
911 prettySe p (PConstant c) = pretty c
912 -- XXX: add pretty for tactics
913 prettySe p (PProof ts) =
914 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
915 prettySe p (PTactics ts) =
916 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
917 prettySe p (PMetavar n) = text "?" <> pretty n
918 prettySe p (PReturn f) = text "return"
919 prettySe p PImpossible = text "impossible"
920 prettySe p Placeholder = text "_"
921 prettySe p (PDoBlock _) = text "do block pretty not implemented"
922 prettySe p (PElabError s) = pretty s
924 prettySe p _ = text "test"
926 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
927 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
928 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
929 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
931 prettyArgSe arg = prettySe 0 arg
932 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
933 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
934 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
936 bracket outer inner doc
937 | inner > outer = lparen <> doc <> rparen
940showImp :: Bool -> PTerm -> String
941showImp impl tm = se 10 tm where
942 se p (PQuote r) = "![" ++ show r ++ "]"
943 se p (PPatvar fc n) = show n
944 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
945 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
947 where showbasic n@(UN _) = show n
948 showbasic (MN _ s) = s
949 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
950 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
951 (if impl then " : " ++ se 10 ty else "") ++ " => "
953 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
955 se p (PPi (Exp l s _) n ty sc)
956 | n `elem` allNamesIn sc || impl
958 (if l then "|(" else "(") ++
959 show n ++ " : " ++ se 10 ty ++
962 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
964 Static -> "[static] "
966 se p (PPi (Imp l s _) n ty sc)
967 | impl = bracket p 2 $ (if l then "|{" else "{") ++
968 show n ++ " : " ++ se 10 ty ++
969 "} " ++ st ++ "-> " ++ se 10 sc
970 | otherwise = se 10 sc
972 Static -> "[static] "
974 se p (PPi (Constraint _ _ _) n ty sc)
975 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
976 se p (PPi (TacImp _ _ s _) n ty sc)
977 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
978 se p (PApp _ (PRef _ f) [])
980 se p (PApp _ (PRef _ op@(UN (f:_))) args)
981 | length (getExps args) == 2 && not impl && not (isAlpha f)
982 = let [l, r] = getExps args in
983 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
985 = let args = getExps as in
986 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
987 else concatMap seArg args
988 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
989 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
990 se p (PHidden tm) = "." ++ se 0 tm
993 | otherwise = "refl {" ++ se 10 t ++ "}"
994 se p (PResolveTC _) = "resolvetc"
995 se p (PTrue _) = "()"
996 se p (PFalse _) = "_|_"
997 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
998 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
999 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
1000 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
1001 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
1003 se p (PConstant c) = show c
1004 se p (PProof ts) = "proof { " ++ show ts ++ "}"
1005 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
1006 se p (PMetavar n) = "?" ++ show n
1007 se p (PReturn f) = "return"
1008 se p PImpossible = "impossible"
1009 se p Placeholder = "_"
1010 se p (PDoBlock _) = "do block show not implemented"
1011 se p (PElabError s) = show s
1012 se p (PCoerced t) = se p t
1013-- se p x = "Not implemented"
1015 sArg (PImp _ _ n tm _) = siArg (n, tm)
1016 sArg (PExp _ _ tm _) = seArg tm
1017 sArg (PConstraint _ _ tm _) = scArg tm
1018 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
1020 seArg arg = " " ++ se 0 arg
1021 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1022 scArg val = " {{" ++ se 10 val ++ "}}"
1023 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1025 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1029instance Sized PTerm where
1030 size (PQuote rawTerm) = size rawTerm
1031 size (PRef fc name) = size name
1032 size (PLam name ty bdy) = 1 + size ty + size bdy
1033 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1034 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1035 size (PTyped trm ty) = 1 + size trm + size ty
1036 size (PApp fc name args) = 1 + size args
1037 size (PCase fc trm bdy) = 1 + size trm + size bdy
1039 size (PFalse fc) = 1
1040 size (PRefl fc _) = 1
1041 size (PResolveTC fc) = 1
1042 size (PEq fc left right) = 1 + size left + size right
1043 size (PPair fc left right) = 1 + size left + size right
1044 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1045 size (PAlternative a alts) = 1 + size alts
1046 size (PHidden hidden) = size hidden
1048 size (PConstant const) = 1 + size const
1049 size Placeholder = 1
1050 size (PDoBlock dos) = 1 + size dos
1051 size (PIdiom fc term) = 1 + size term
1052 size (PReturn fc) = 1
1053 size (PMetavar name) = 1
1054 size (PProof tactics) = size tactics
1055 size (PElabError err) = size err
1056 size PImpossible = 1
1058getPArity :: PTerm -> Int
1059getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1062-- Return all names, free or globally bound, in the given term.
1064allNamesIn :: PTerm -> [Name]
1065allNamesIn tm = nub $ ni [] tm
1068 | not (n `elem` env) = [n]
1069 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1070 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1071 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1072 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1073 ni env (PHidden tm) = ni env tm
1074 ni env (PEq _ l r) = ni env l ++ ni env r
1075 ni env (PTyped l r) = ni env l ++ ni env r
1076 ni env (PPair _ l r) = ni env l ++ ni env r
1077 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1078 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1079 ni env (PAlternative a ls) = concatMap (ni env) ls
1082-- Return names which are free in the given term.
1084namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1085namesIn uvars ist tm = nub $ ni [] tm
1088 | not (n `elem` env)
1089 = case lookupTy Nothing n (tt_ctxt ist) of
1091 _ -> if n `elem` (map fst uvars) then [n] else []
1092 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1093 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1094 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1095 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1096 ni env (PEq _ l r) = ni env l ++ ni env r
1097 ni env (PTyped l r) = ni env l ++ ni env r
1098 ni env (PPair _ l r) = ni env l ++ ni env r
1099 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1100 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1101 ni env (PAlternative a as) = concatMap (ni env) as
1102 ni env (PHidden tm) = ni env tm
1105-- Return which of the given names are used in the given term.
1107usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1108usedNamesIn vars ist tm = nub $ ni [] tm
1111 | n `elem` vars && not (n `elem` env)
1112 = case lookupTy Nothing n (tt_ctxt ist) of
1115 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1116 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1117 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1118 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1119 ni env (PEq _ l r) = ni env l ++ ni env r
1120 ni env (PTyped l r) = ni env l ++ ni env r
1121 ni env (PPair _ l r) = ni env l ++ ni env r
1122 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1123 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1124 ni env (PAlternative a as) = concatMap (ni env) as
1125 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