1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
8import Core.Elaborate hiding (Tactic(..))
11import IRTS.CodegenCommon
13import Util.DynamicLinker
17import System.Console.Haskeline
19import Control.Monad.Trans.State.Strict
27data IOption = IOption { opt_logLevel :: Int,
29 opt_typeintype :: Bool,
32 opt_errContext :: Bool,
38 opt_outputTy :: OutputType,
39 opt_ibcsubdir :: FilePath,
40 opt_importdirs :: [FilePath],
41 opt_cmdline :: [Opt] -- remember whole command line
45defaultOpts = IOption 0 False False True False False True True False False ViaC Executable "" [] []
47data LanguageExt = TypeProviders deriving (Show, Eq, Read, Ord)
49-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
50-- without typechecking).
51-- This will include all the functions and data declarations, plus fixity declarations
54-- | The global state used in the Idris monad
56 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
57 idris_constraints :: [(UConstraint, FC)],
58 -- ^ A list of universe constraints and their corresponding source locations
59 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
60 idris_implicits :: Ctxt [PArg],
61 idris_statics :: Ctxt [Bool],
62 idris_classes :: Ctxt ClassInfo,
63 idris_dsls :: Ctxt DSL,
64 idris_optimisation :: Ctxt OptInfo,
65 idris_datatypes :: Ctxt TypeInfo,
66 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
67 -- ^ list of lhs/rhs, and a list of missing clauses
68 idris_flags :: Ctxt [FnOpt],
69 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
70 idris_calledgraph :: Ctxt [Name],
71 idris_docstrings :: Ctxt String,
72 idris_totcheck :: [(FC, Name)],
74 idris_options :: IOption,
76 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
77 idris_coercions :: [Name],
78 syntax_rules :: [Syntax],
79 syntax_keywords :: [String],
80 imported :: [FilePath], -- ^ The imported modules
81 idris_scprims :: [(Name, (Int, PrimFn))],
82 idris_objs :: [FilePath],
83 idris_libs :: [String],
84 idris_hdrs :: [String],
85 proof_list :: [(Name, [String])],
87 lastParse :: Maybe Name,
88 indent_stack :: [Int],
89 brace_stack :: [Maybe Int],
90 hide_list :: [(Name, Maybe Accessibility)],
91 default_access :: Accessibility,
92 default_total :: Bool,
93 ibc_write :: [IBCWrite],
94 compiled_so :: Maybe String,
95 idris_dynamic_libs :: [DynamicLib],
96 idris_language_extensions :: [LanguageExt]
99data SizeChange = Smaller | Same | Bigger | Unknown
102deriving instance Binary SizeChange
105type SCGEntry = (Name, [Maybe (Int, SizeChange)])
107data CGInfo = CGInfo { argsdef :: [Name],
108 calls :: [(Name, [[Name]])],
114deriving instance Binary CGInfo
117primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
119-- information that needs writing for the current module's .ibc file
120data IBCWrite = IBCFix FixDecl
124 | IBCInstance Bool Name Name
135 | IBCAccess Name Accessibility
136 | IBCTotal Name Totality
137 | IBCFlags Name [FnOpt]
141 | IBCDef Name -- i.e. main context
144idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
145 emptyContext emptyContext emptyContext emptyContext
146 emptyContext emptyContext emptyContext emptyContext
147 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] []
148 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] []
150-- | The monad for the main REPL - reading and processing files and updating
151-- global state (hence the IO inner monad).
152type Idris = StateT IState IO
154-- Commands in the REPL
172 | ChangeDirectory FilePath
175 | Compile Target String
181 | AddProof (Maybe Name)
193 | DynamicLink FilePath
202data Opt = Filename String
236 | OutputTy OutputType
237 | Extension LanguageExt
240-- Parsed declarations
242data Fixity = Infixl { prec :: Int }
243 | Infixr { prec :: Int }
244 | InfixN { prec :: Int }
245 | PrefixN { prec :: Int }
248deriving instance Binary Fixity
251instance Show Fixity where
252 show (Infixl i) = "infixl " ++ show i
253 show (Infixr i) = "infixr " ++ show i
254 show (InfixN i) = "infix " ++ show i
255 show (PrefixN i) = "prefix " ++ show i
257data FixDecl = Fix Fixity String
260instance Show FixDecl where
261 show (Fix f s) = show f ++ " " ++ s
264deriving instance Binary FixDecl
267instance Ord FixDecl where
268 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
271data Static = Static | Dynamic
274deriving instance Binary Static
277-- Mark bindings with their explicitness, and laziness
278data Plicity = Imp { plazy :: Bool,
281 | Exp { plazy :: Bool,
284 | Constraint { plazy :: Bool,
287 | TacImp { plazy :: Bool,
294deriving instance Binary Plicity
297impl = Imp False Dynamic ""
298expl = Exp False Dynamic ""
299constraint = Constraint False Dynamic ""
300tacimpl t = TacImp False Dynamic t ""
302data FnOpt = Inlinable | TotalFn | PartialFn
303 | Coinductive | AssertTotal | TCGen
304 | Implicit -- implicit coercion
305 | CExport String -- export, with a C name
306 | Specialise [Name] -- specialise it, freeze these names
309deriving instance Binary FnOpt
314inlinable :: FnOpts -> Bool
315inlinable = elem Inlinable
317-- | Top-level declarations such as compiler directives, definitions,
318-- datatypes and typeclasses.
320 = PFix FC Fixity [String] -- ^ Fixity declaration
321 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
322 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
323 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
324 | PCAF FC Name t -- ^ Top level constant
325 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
326 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
327 | PNamespace String [PDecl' t] -- ^ New namespace
328 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
329 | PClass String SyntaxInfo FC
332 [(Name, t)] -- parameters
333 [PDecl' t] -- declarations
334 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
335 -- class name, parameters, method declarations
336 | PInstance SyntaxInfo FC [t] -- constraints
339 t -- full instance type
340 (Maybe Name) -- explicit name
342 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
343 -- class name, parameters, full instance type, optional explicit name, and definitions
344 | PDSL Name (DSL' t) -- ^ DSL declaration
345 | PSyntax FC Syntax -- ^ Syntax definition
346 | PMutual FC [PDecl' t] -- ^ Mutual block
347 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
348 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
351deriving instance Binary PDecl'
354-- | One clause of a top-level definition. Term arguments to constructors are:
356-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
358-- 2. The list of patterns
360-- 3. The right-hand side
362-- 4. The where block (PDecl' t)
363data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
364 | PWith FC Name t [t] t [PDecl' t]
365 | PClauseR FC [t] t [PDecl' t]
366 | PWithR FC [t] t [PDecl' t]
369deriving instance Binary PClause'
373data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
374 d_tcon :: t, -- ^ Type constructor
375 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
377 -- ^ Data declaration
378 | PLaterdecl { d_name :: Name, d_tcon :: t }
379 -- ^ "Placeholder" for data whose constructors are defined later
382deriving instance Binary PData'
385-- Handy to get a free function for applying PTerm -> PTerm functions
386-- across a program, by deriving Functor
388type PDecl = PDecl' PTerm
389type PData = PData' PTerm
390type PClause = PClause' PTerm
392-- get all the names declared in a decl
394declared :: PDecl -> [Name]
395declared (PFix _ _ _) = []
396declared (PTy _ _ _ _ n t) = [n]
397declared (PPostulate _ _ _ _ n t) = [n]
398declared (PClauses _ _ n _) = [] -- not a declaration
399declared (PCAF _ n _) = [n]
400declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
401 where fstt (_, a, _, _) = a
402declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
403declared (PParams _ _ ds) = concatMap declared ds
404declared (PNamespace _ ds) = concatMap declared ds
405declared (PRecord _ _ _ n _ _ c _) = [n, c]
406declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
407declared (PInstance _ _ _ _ _ _ _ _) = []
408declared (PDSL n _) = [n]
409declared (PSyntax _ _) = []
410declared (PMutual _ ds) = concatMap declared ds
411declared (PDirective _) = []
413-- get the names declared, not counting nested parameter blocks
414tldeclared :: PDecl -> [Name]
415tldeclared (PFix _ _ _) = []
416tldeclared (PTy _ _ _ _ n t) = [n]
417tldeclared (PPostulate _ _ _ _ n t) = [n]
418tldeclared (PClauses _ _ n _) = [] -- not a declaration
419tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
420tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
421 where fstt (_, a, _, _) = a
422tldeclared (PParams _ _ ds) = []
423tldeclared (PMutual _ ds) = concatMap tldeclared ds
424tldeclared (PNamespace _ ds) = concatMap tldeclared ds
427defined :: PDecl -> [Name]
428defined (PFix _ _ _) = []
429defined (PTy _ _ _ _ n t) = []
430defined (PPostulate _ _ _ _ n t) = []
431defined (PClauses _ _ n _) = [n] -- not a declaration
432defined (PCAF _ n _) = [n]
433defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
434 where fstt (_, a, _, _) = a
435defined (PData _ _ _ _ (PLaterdecl n _)) = []
436defined (PParams _ _ ds) = concatMap defined ds
437defined (PNamespace _ ds) = concatMap defined ds
438defined (PRecord _ _ _ n _ _ c _) = [n, c]
439defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
440defined (PInstance _ _ _ _ _ _ _ _) = []
441defined (PDSL n _) = [n]
442defined (PSyntax _ _) = []
443defined (PMutual _ ds) = concatMap defined ds
444defined (PDirective _) = []
447updateN :: [(Name, Name)] -> Name -> Name
448updateN ns n | Just n' <- lookup n ns = n'
451updateNs :: [(Name, Name)] -> PTerm -> PTerm
453updateNs ns t = mapPT updateRef t
454 where updateRef (PRef fc f) = PRef fc (updateN ns f)
457-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
459-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
460-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
461-- where updateCNs ns (PClause n l ts r ds)
462-- = PClause (updateN ns n) (fmap (updateNs ns) l)
463-- (map (fmap (updateNs ns)) ts)
464-- (fmap (updateNs ns) r)
465-- (map (updateDNs ns) ds)
468-- | High level language terms
469data PTerm = PQuote Raw
471 | PInferRef FC Name -- ^ A name to be defined later
473 | PLam Name PTerm PTerm
474 | PPi Plicity Name PTerm PTerm
475 | PLet Name PTerm PTerm PTerm
476 | PTyped PTerm PTerm -- ^ Term with explicit type
477 | PApp FC PTerm [PArg]
478 | PCase FC PTerm [(PTerm, PTerm)]
484 | PRewrite FC PTerm PTerm
485 | PPair FC PTerm PTerm
486 | PDPair FC PTerm PTerm PTerm
487 | PAlternative Bool [PTerm] -- True if only one may work
488 | PHidden PTerm -- ^ Irrelevant or hidden pattern
496 | PProof [PTactic] -- ^ Proof script
497 | PTactics [PTactic] -- ^ As PProof, but no auto solving
498 | PElabError Err -- ^ Error to report on elaboration
499 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
500 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
503deriving instance Binary PTerm
506mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
507mapPT f t = f (mpt t) where
508 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
509 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
510 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
511 mpt (PRewrite fc t s) = PRewrite fc (mapPT f t) (mapPT f s)
512 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
513 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
514 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
515 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
516 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
517 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
518 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
519 mpt (PHidden t) = PHidden (mapPT f t)
520 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
521 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
522 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
526data PTactic' t = Intro [Name] | Intros | Focus Name
527 | Refine Name [Bool] | Rewrite t
528 | LetTac Name t | LetTacTy Name t t
529 | Exact t | Compute | Trivial
532 | ProofState | ProofTerm | Undo
533 | Try (PTactic' t) (PTactic' t)
534 | TSeq (PTactic' t) (PTactic' t)
535 | ApplyTactic t -- see Language.Reflection module
538 | GoalType String (PTactic' t)
540 deriving (Show, Eq, Functor)
542deriving instance Binary PTactic'
545instance Sized a => Sized (PTactic' a) where
546 size (Intro nms) = 1 + size nms
548 size (Focus nm) = 1 + size nm
549 size (Refine nm bs) = 1 + size nm + length bs
550 size (Rewrite t) = 1 + size t
551 size (LetTac nm t) = 1 + size nm + size t
552 size (Exact t) = 1 + size t
560 size (Try l r) = 1 + size l + size r
561 size (TSeq l r) = 1 + size l + size r
562 size (ApplyTactic t) = 1 + size t
563 size (Reflect t) = 1 + size t
564 size (Fill t) = 1 + size t
568type PTactic = PTactic' PTerm
570data PDo' t = DoExp FC t
575 deriving (Eq, Functor)
577deriving instance Binary PDo'
580instance Sized a => Sized (PDo' a) where
581 size (DoExp fc t) = 1 + size fc + size t
582 size (DoBind fc nm t) = 1 + size fc + size nm + size t
583 size (DoBindP fc l r) = 1 + size fc + size l + size r
584 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
585 size (DoLetP fc l r) = 1 + size fc + size l + size r
589-- The priority gives a hint as to elaboration order. Best to elaborate
590-- things early which will help give a more concrete type to other
591-- variables, e.g. a before (interpTy a).
592-- TODO: priority no longer serves any purpose, drop it!
594data PArg' t = PImp { priority :: Int,
595 lazyarg :: Bool, pname :: Name, getTm :: t,
597 | PExp { priority :: Int,
598 lazyarg :: Bool, getTm :: t,
600 | PConstraint { priority :: Int,
601 lazyarg :: Bool, getTm :: t,
603 | PTacImplicit { priority :: Int,
604 lazyarg :: Bool, pname :: Name,
608 deriving (Show, Eq, Functor)
610instance Sized a => Sized (PArg' a) where
611 size (PImp p l nm trm _) = 1 + size nm + size trm
612 size (PExp p l trm _) = 1 + size trm
613 size (PConstraint p l trm _) = 1 + size trm
614 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
617deriving instance Binary PArg'
620pimp n t = PImp 0 True n t ""
621pexp t = PExp 0 False t ""
622pconst t = PConstraint 0 False t ""
623ptacimp n s t = PTacImplicit 0 True n s t ""
625type PArg = PArg' PTerm
629data ClassInfo = CI { instanceName :: Name,
630 class_methods :: [(Name, (FnOpts, PTerm))],
631 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
632 class_params :: [Name],
633 class_instances :: [Name] }
636deriving instance Binary ClassInfo
639data OptInfo = Optimise { collapsible :: Bool,
640 forceable :: [Int], -- argument positions
644deriving instance Binary OptInfo
648data TypeInfo = TI { con_names :: [Name],
653deriving instance Binary TypeInfo
656-- Syntactic sugar info
658data DSL' t = DSL { dsl_bind :: t,
663 index_first :: Maybe t,
664 index_next :: Maybe t,
665 dsl_lambda :: Maybe t,
668 deriving (Show, Functor)
670deriving instance Binary DSL'
675data SynContext = PatternSyntax | TermSyntax | AnySyntax
678deriving instance Binary SynContext
681data Syntax = Rule [SSymbol] PTerm SynContext
684deriving instance Binary Syntax
687data SSymbol = Keyword Name
694deriving instance Binary SSymbol
697initDSL = DSL (PRef f (UN ">>="))
698 (PRef f (UN "return"))
706 where f = FC "(builtin)" 0
708data SyntaxInfo = Syn { using :: [(Name, PTerm)],
709 syn_params :: [(Name, PTerm)],
710 syn_namespace :: [String],
712 decoration :: Name -> Name,
714 implicitAllowed :: Bool,
718deriving instance Binary SyntaxInfo
721defaultSyntax = Syn [] [] [] [] id False False initDSL
723expandNS :: SyntaxInfo -> Name -> Name
724expandNS syn n@(NS _ _) = n
725expandNS syn n = case syn_namespace syn of
730--- Pretty printing declarations and terms
732instance Show PTerm where
733 show tm = showImp False tm
735instance Pretty PTerm where
736 pretty = prettyImp False
738instance Show PDecl where
739 show d = showDeclImp False d
741instance Show PClause where
742 show c = showCImp True c
744instance Show PData where
745 show d = showDImp False d
747showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
748showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
749showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
750showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
751showDeclImp _ (PData _ _ _ _ d) = show d
752showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
753 showSep "\n" (map show ps)
756showCImp :: Bool -> PClause -> String
757showCImp impl (PClause _ n l ws r w)
758 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
759 ++ " where " ++ show w
762 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
763showCImp impl (PWith _ n l ws r w)
764 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
765 ++ " { " ++ show w ++ " } "
768 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
771showDImp :: Bool -> PData -> String
772showDImp impl (PDatadecl n ty cons)
773 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
775 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
777getImps :: [PArg] -> [(Name, PTerm)]
779getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
780getImps (_ : xs) = getImps xs
782getExps :: [PArg] -> [PTerm]
784getExps (PExp _ _ tm _ : xs) = tm : getExps xs
785getExps (_ : xs) = getExps xs
787getConsts :: [PArg] -> [PTerm]
789getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
790getConsts (_ : xs) = getConsts xs
792getAll :: [PArg] -> [PTerm]
795prettyImp :: Bool -> PTerm -> Doc
796prettyImp impl = prettySe 10
798 prettySe p (PQuote r) =
799 if size r > breakingSize then
800 text "![" $$ pretty r <> text "]"
802 text "![" <> pretty r <> text "]"
803 prettySe p (PPatvar fc n) = pretty n
804 prettySe p (PRef fc n) =
810 prettyBasic n@(UN _) = pretty n
811 prettyBasic (MN _ s) = text s
812 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
813 prettySe p (PLam n ty sc) =
815 if size sc > breakingSize then
816 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
818 text "λ" <> pretty n <+> text "=>" <+> pretty sc
819 prettySe p (PLet n ty v sc) =
821 if size sc > breakingSize then
822 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
823 nest nestingSize (prettySe 10 sc)
825 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
827 prettySe p (PPi (Exp l s _) n ty sc)
828 | n `elem` allNamesIn sc || impl =
829 let open = if l then text "|" <> lparen else lparen in
831 if size sc > breakingSize then
832 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
833 st <+> text "->" $+$ prettySe 10 sc
835 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
836 st <+> text "->" <+> prettySe 10 sc
839 if size sc > breakingSize then
840 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
842 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
846 Static -> text "[static]"
848 prettySe p (PPi (Imp l s _) n ty sc)
850 let open = if l then text "|" <> lbrace else lbrace in
852 if size sc > breakingSize then
853 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
854 st <+> text "->" <+> prettySe 10 sc
856 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
857 st <+> text "->" <+> prettySe 10 sc
858 | otherwise = prettySe 10 sc
862 Static -> text $ "[static]"
864 prettySe p (PPi (Constraint _ _ _) n ty sc) =
866 if size sc > breakingSize then
867 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
869 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
870 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
872 if size sc > breakingSize then
873 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
874 rbrace <+> text "->" $+$ prettySe 10 sc
876 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
877 rbrace <+> text "->" <+> prettySe 10 sc
878 prettySe p (PApp _ (PRef _ f) [])
879 | not impl = pretty f
880 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
881 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
882 let [l, r] = getExps args in
884 if size r > breakingSize then
885 prettySe 1 l <+> pretty op $+$ prettySe 0 r
887 prettySe 1 l <+> pretty op <+> prettySe 0 r
888 prettySe p (PApp _ f as) =
889 let args = getExps as in
894 -- foldr (<+>) empty $ map prettyArgS as
896 foldl' fSe empty args
897 -- foldr (<+>) empty $ map prettyArgSe args
900 if size r > breakingSize then
901 l $+$ nest nestingSize (prettyArgS r)
906 if size r > breakingSize then
907 l $+$ nest nestingSize (prettyArgSe r)
910 prettySe p (PCase _ scr opts) =
911 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
913 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
915 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
916 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
917 prettySe p (PRefl _ _) = text "refl"
918 prettySe p (PResolveTC _) = text "resolvetc"
919 prettySe p (PTrue _) = text "()"
920 prettySe p (PFalse _) = text "_|_"
921 prettySe p (PEq _ l r) =
923 if size r > breakingSize then
924 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
926 prettySe 10 l <+> text "=" <+> prettySe 10 r
927 prettySe p (PRewrite _ l r) =
929 if size r > breakingSize then
930 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
932 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
933 prettySe p (PTyped l r) =
934 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
935 prettySe p (PPair _ l r) =
936 if size r > breakingSize then
937 lparen <> prettySe 10 l <> text "," $+$
938 prettySe 10 r <> rparen
940 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
941 prettySe p (PDPair _ l t r) =
942 if size r > breakingSize then
943 lparen <> prettySe 10 l <+> text "**" $+$
944 prettySe 10 r <> rparen
946 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
947 prettySe p (PAlternative a as) =
948 lparen <> text "|" <> prettyAs <> text "|" <> rparen
951 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
952 prettySe p PType = text "Type"
953 prettySe p (PConstant c) = pretty c
954 -- XXX: add pretty for tactics
955 prettySe p (PProof ts) =
956 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
957 prettySe p (PTactics ts) =
958 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
959 prettySe p (PMetavar n) = text "?" <> pretty n
960 prettySe p (PReturn f) = text "return"
961 prettySe p PImpossible = text "impossible"
962 prettySe p Placeholder = text "_"
963 prettySe p (PDoBlock _) = text "do block pretty not implemented"
964 prettySe p (PElabError s) = pretty s
966 prettySe p _ = text "test"
968 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
969 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
970 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
971 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
973 prettyArgSe arg = prettySe 0 arg
974 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
975 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
976 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
978 bracket outer inner doc
979 | inner > outer = lparen <> doc <> rparen
982showImp :: Bool -> PTerm -> String
983showImp impl tm = se 10 tm where
984 se p (PQuote r) = "![" ++ show r ++ "]"
985 se p (PPatvar fc n) = if impl then show n ++ "[p]" else show n
986 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
987 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
989 where showbasic n@(UN _) = show n
990 showbasic (MN _ s) = s
991 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
992 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
993 (if impl then " : " ++ se 10 ty else "") ++ " => "
995 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
997 se p (PPi (Exp l s _) n ty sc)
998 | n `elem` allNamesIn sc || impl
1000 (if l then "|(" else "(") ++
1001 show n ++ " : " ++ se 10 ty ++
1004 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
1005 where st = case s of
1006 Static -> "[static] "
1008 se p (PPi (Imp l s _) n ty sc)
1009 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1010 show n ++ " : " ++ se 10 ty ++
1011 "} " ++ st ++ "-> " ++ se 10 sc
1012 | otherwise = se 10 sc
1013 where st = case s of
1014 Static -> "[static] "
1016 se p (PPi (Constraint _ _ _) n ty sc)
1017 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
1018 se p (PPi (TacImp _ _ s _) n ty sc)
1019 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
1021 | Just str <- slist p e = str
1022 | Just num <- snat p e = show num
1023 se p (PApp _ (PRef _ f) [])
1025 se p (PApp _ (PRef _ op@(UN (f:_))) args)
1026 | length (getExps args) == 2 && not impl && not (isAlpha f)
1027 = let [l, r] = getExps args in
1028 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
1030 = let args = getExps as in
1031 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
1032 else concatMap seArg args
1033 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
1034 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
1035 se p (PHidden tm) = "." ++ se 0 tm
1038 | otherwise = "refl {" ++ se 10 t ++ "}"
1039 se p (PResolveTC _) = "resolvetc"
1040 se p (PTrue _) = "()"
1041 se p (PFalse _) = "_|_"
1042 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
1043 se p (PRewrite _ l r) = bracket p 2 $ "rewrite " ++ se 10 l ++ " in " ++ se 10 r
1044 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
1045 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
1046 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
1047 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
1049 se p (PConstant c) = show c
1050 se p (PProof ts) = "proof { " ++ show ts ++ "}"
1051 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
1052 se p (PMetavar n) = "?" ++ show n
1053 se p (PReturn f) = "return"
1054 se p PImpossible = "impossible"
1055 se p Placeholder = "_"
1056 se p (PDoBlock _) = "do block show not implemented"
1057 se p (PElabError s) = show s
1058 se p (PCoerced t) = se p t
1059-- se p x = "Not implemented"
1061 slist' p (PApp _ (PRef _ nil) _)
1062 | nsroot nil == UN "Nil" = Just []
1063 slist' p (PApp _ (PRef _ cons) args)
1064 | nsroot cons == UN "::",
1065 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1067 Just tl' <- slist' p tl
1070 isImp (PImp {}) = True
1072 slist' _ _ = Nothing
1074 slist p e | Just es <- slist' p e = Just $
1075 case es of [] -> "[]"
1076 [x] -> "[" ++ se p x ++ "]"
1077 xs -> "[" ++ intercalate "," (map (se p) xs) ++ "]"
1080 -- since Prelude is always imported, S & O are unqualified iff they're the
1083 | show o == (natns++"O") || show o == "O" = Just 0
1084 snat p (PApp _ s [PExp {getTm=n}])
1085 | show s == (natns++"S") || show s == "S",
1090 natns = "Prelude.Nat."
1092 sArg (PImp _ _ n tm _) = siArg (n, tm)
1093 sArg (PExp _ _ tm _) = seArg tm
1094 sArg (PConstraint _ _ tm _) = scArg tm
1095 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
1097 seArg arg = " " ++ se 0 arg
1098 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1099 scArg val = " {{" ++ se 10 val ++ "}}"
1100 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1102 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1106instance Sized PTerm where
1107 size (PQuote rawTerm) = size rawTerm
1108 size (PRef fc name) = size name
1109 size (PLam name ty bdy) = 1 + size ty + size bdy
1110 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1111 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1112 size (PTyped trm ty) = 1 + size trm + size ty
1113 size (PApp fc name args) = 1 + size args
1114 size (PCase fc trm bdy) = 1 + size trm + size bdy
1116 size (PFalse fc) = 1
1117 size (PRefl fc _) = 1
1118 size (PResolveTC fc) = 1
1119 size (PEq fc left right) = 1 + size left + size right
1120 size (PRewrite fc left right) = 1 + size left + size right
1121 size (PPair fc left right) = 1 + size left + size right
1122 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1123 size (PAlternative a alts) = 1 + size alts
1124 size (PHidden hidden) = size hidden
1126 size (PConstant const) = 1 + size const
1127 size Placeholder = 1
1128 size (PDoBlock dos) = 1 + size dos
1129 size (PIdiom fc term) = 1 + size term
1130 size (PReturn fc) = 1
1131 size (PMetavar name) = 1
1132 size (PProof tactics) = size tactics
1133 size (PElabError err) = size err
1134 size PImpossible = 1
1136getPArity :: PTerm -> Int
1137getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1140-- Return all names, free or globally bound, in the given term.
1142allNamesIn :: PTerm -> [Name]
1143allNamesIn tm = nub $ ni [] tm
1146 | not (n `elem` env) = [n]
1147 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1148 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1149 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1150 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1151 ni env (PHidden tm) = ni env tm
1152 ni env (PEq _ l r) = ni env l ++ ni env r
1153 ni env (PRewrite _ l r) = ni env l ++ ni env r
1154 ni env (PTyped l r) = ni env l ++ ni env r
1155 ni env (PPair _ l r) = ni env l ++ ni env r
1156 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1157 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1158 ni env (PAlternative a ls) = concatMap (ni env) ls
1161-- Return names which are free in the given term.
1163namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1164namesIn uvars ist tm = nub $ ni [] tm
1167 | not (n `elem` env)
1168 = case lookupTy n (tt_ctxt ist) of
1170 _ -> if n `elem` (map fst uvars) then [n] else []
1171 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1172 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1173 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1174 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1175 ni env (PEq _ l r) = ni env l ++ ni env r
1176 ni env (PRewrite _ l r) = ni env l ++ ni env r
1177 ni env (PTyped l r) = ni env l ++ ni env r
1178 ni env (PPair _ l r) = ni env l ++ ni env r
1179 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1180 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1181 ni env (PAlternative a as) = concatMap (ni env) as
1182 ni env (PHidden tm) = ni env tm
1185-- Return which of the given names are used in the given term.
1187usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1188usedNamesIn vars ist tm = nub $ ni [] tm
1191 | n `elem` vars && not (n `elem` env)
1192 = case lookupTy n (tt_ctxt ist) of
1195 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1196 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1197 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1198 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1199 ni env (PEq _ l r) = ni env l ++ ni env r
1200 ni env (PRewrite _ l r) = ni env l ++ ni env r
1201 ni env (PTyped l r) = ni env l ++ ni env r
1202 ni env (PPair _ l r) = ni env l ++ ni env r
1203 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1204 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1205 ni env (PAlternative a as) = concatMap (ni env) as
1206 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