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,
37 opt_outputTy :: OutputType,
38 opt_ibcsubdir :: FilePath,
39 opt_importdirs :: [FilePath],
40 opt_cmdline :: [Opt] -- remember whole command line
44defaultOpts = IOption { opt_logLevel = 0
45 , opt_typecase = False
46 , opt_typeintype = False
49 , opt_errContext = False
54 , opt_outputTy = Executable
60data LanguageExt = TypeProviders deriving (Show, Eq, Read, Ord)
62-- | The output mode in use
63data OutputMode = RawOutput | IdeSlave Integer deriving Show
65-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
66-- without typechecking).
67-- This will include all the functions and data declarations, plus fixity declarations
70-- | The global state used in the Idris monad
72 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
73 idris_constraints :: [(UConstraint, FC)],
74 -- ^ A list of universe constraints and their corresponding source locations
75 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
76 idris_implicits :: Ctxt [PArg],
77 idris_statics :: Ctxt [Bool],
78 idris_classes :: Ctxt ClassInfo,
79 idris_dsls :: Ctxt DSL,
80 idris_optimisation :: Ctxt OptInfo,
81 idris_datatypes :: Ctxt TypeInfo,
82 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
83 -- ^ list of lhs/rhs, and a list of missing clauses
84 idris_flags :: Ctxt [FnOpt],
85 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
86 idris_calledgraph :: Ctxt [Name],
87 idris_docstrings :: Ctxt String,
88 idris_totcheck :: [(FC, Name)],
90 idris_options :: IOption,
92 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
93 idris_coercions :: [Name],
94 syntax_rules :: [Syntax],
95 syntax_keywords :: [String],
96 imported :: [FilePath], -- ^ The imported modules
97 idris_scprims :: [(Name, (Int, PrimFn))],
98 idris_objs :: [(Target, FilePath)],
99 idris_libs :: [(Target, String)],
100 idris_hdrs :: [(Target, String)],
101 proof_list :: [(Name, [String])],
102 errLine :: Maybe Int,
103 lastParse :: Maybe Name,
104 indent_stack :: [Int],
105 brace_stack :: [Maybe Int],
106 hide_list :: [(Name, Maybe Accessibility)],
107 default_access :: Accessibility,
108 default_total :: Bool,
109 ibc_write :: [IBCWrite],
110 compiled_so :: Maybe String,
111 idris_dynamic_libs :: [DynamicLib],
112 idris_language_extensions :: [LanguageExt],
113 idris_outputmode :: OutputMode
116data SizeChange = Smaller | Same | Bigger | Unknown
119deriving instance Binary SizeChange
122type SCGEntry = (Name, [Maybe (Int, SizeChange)])
124data CGInfo = CGInfo { argsdef :: [Name],
125 calls :: [(Name, [[Name]])],
131deriving instance Binary CGInfo
134primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
136-- information that needs writing for the current module's .ibc file
137data IBCWrite = IBCFix FixDecl
141 | IBCInstance Bool Name Name
148 | IBCObj Target FilePath
149 | IBCLib Target String
151 | IBCHeader Target String
152 | IBCAccess Name Accessibility
153 | IBCTotal Name Totality
154 | IBCFlags Name [FnOpt]
158 | IBCDef Name -- i.e. main context
161idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
162 emptyContext emptyContext emptyContext emptyContext
163 emptyContext emptyContext emptyContext emptyContext
164 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] []
165 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
167-- | The monad for the main REPL - reading and processing files and updating
168-- global state (hence the IO inner monad).
169--type Idris = WriterT [Either String (IO ())] (State IState a))
170type Idris = StateT IState IO
172-- Commands in the REPL
191 | ChangeDirectory FilePath
194 | Compile Target String
200 | AddProof (Maybe Name)
212 | DynamicLink FilePath
221data Opt = Filename String
255 | OutputTy OutputType
256 | Extension LanguageExt
257 | InterpretScript String
260-- Parsed declarations
262data Fixity = Infixl { prec :: Int }
263 | Infixr { prec :: Int }
264 | InfixN { prec :: Int }
265 | PrefixN { prec :: Int }
268deriving instance Binary Fixity
271instance Show Fixity where
272 show (Infixl i) = "infixl " ++ show i
273 show (Infixr i) = "infixr " ++ show i
274 show (InfixN i) = "infix " ++ show i
275 show (PrefixN i) = "prefix " ++ show i
277data FixDecl = Fix Fixity String
280instance Show FixDecl where
281 show (Fix f s) = show f ++ " " ++ s
284deriving instance Binary FixDecl
287instance Ord FixDecl where
288 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
291data Static = Static | Dynamic
294deriving instance Binary Static
297-- Mark bindings with their explicitness, and laziness
298data Plicity = Imp { plazy :: Bool,
301 | Exp { plazy :: Bool,
304 | Constraint { plazy :: Bool,
307 | TacImp { plazy :: Bool,
314deriving instance Binary Plicity
317impl = Imp False Dynamic ""
318expl = Exp False Dynamic ""
319constraint = Constraint False Dynamic ""
320tacimpl t = TacImp False Dynamic t ""
322data FnOpt = Inlinable | TotalFn | PartialFn
323 | Coinductive | AssertTotal | TCGen
324 | Implicit -- implicit coercion
325 | CExport String -- export, with a C name
326 | Specialise [Name] -- specialise it, freeze these names
329deriving instance Binary FnOpt
334inlinable :: FnOpts -> Bool
335inlinable = elem Inlinable
337-- | Top-level declarations such as compiler directives, definitions,
338-- datatypes and typeclasses.
340 = PFix FC Fixity [String] -- ^ Fixity declaration
341 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
342 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
343 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
344 | PCAF FC Name t -- ^ Top level constant
345 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
346 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
347 | PNamespace String [PDecl' t] -- ^ New namespace
348 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
349 | PClass String SyntaxInfo FC
352 [(Name, t)] -- parameters
353 [PDecl' t] -- declarations
354 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
355 -- class name, parameters, method declarations
356 | PInstance SyntaxInfo FC [t] -- constraints
359 t -- full instance type
360 (Maybe Name) -- explicit name
362 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
363 -- class name, parameters, full instance type, optional explicit name, and definitions
364 | PDSL Name (DSL' t) -- ^ DSL declaration
365 | PSyntax FC Syntax -- ^ Syntax definition
366 | PMutual FC [PDecl' t] -- ^ Mutual block
367 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
368 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
371deriving instance Binary PDecl'
374-- | One clause of a top-level definition. Term arguments to constructors are:
376-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
378-- 2. The list of patterns
380-- 3. The right-hand side
382-- 4. The where block (PDecl' t)
383data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
384 | PWith FC Name t [t] t [PDecl' t]
385 | PClauseR FC [t] t [PDecl' t]
386 | PWithR FC [t] t [PDecl' t]
389deriving instance Binary PClause'
393data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
394 d_tcon :: t, -- ^ Type constructor
395 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
397 -- ^ Data declaration
398 | PLaterdecl { d_name :: Name, d_tcon :: t }
399 -- ^ "Placeholder" for data whose constructors are defined later
402deriving instance Binary PData'
405-- Handy to get a free function for applying PTerm -> PTerm functions
406-- across a program, by deriving Functor
408type PDecl = PDecl' PTerm
409type PData = PData' PTerm
410type PClause = PClause' PTerm
412-- get all the names declared in a decl
414declared :: PDecl -> [Name]
415declared (PFix _ _ _) = []
416declared (PTy _ _ _ _ n t) = [n]
417declared (PPostulate _ _ _ _ n t) = [n]
418declared (PClauses _ _ n _) = [] -- not a declaration
419declared (PCAF _ n _) = [n]
420declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
421 where fstt (_, a, _, _) = a
422declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
423declared (PParams _ _ ds) = concatMap declared ds
424declared (PNamespace _ ds) = concatMap declared ds
425declared (PRecord _ _ _ n _ _ c _) = [n, c]
426declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
427declared (PInstance _ _ _ _ _ _ _ _) = []
428declared (PDSL n _) = [n]
429declared (PSyntax _ _) = []
430declared (PMutual _ ds) = concatMap declared ds
431declared (PDirective _) = []
433-- get the names declared, not counting nested parameter blocks
434tldeclared :: PDecl -> [Name]
435tldeclared (PFix _ _ _) = []
436tldeclared (PTy _ _ _ _ n t) = [n]
437tldeclared (PPostulate _ _ _ _ n t) = [n]
438tldeclared (PClauses _ _ n _) = [] -- not a declaration
439tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
440tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
441 where fstt (_, a, _, _) = a
442tldeclared (PParams _ _ ds) = []
443tldeclared (PMutual _ ds) = concatMap tldeclared ds
444tldeclared (PNamespace _ ds) = concatMap tldeclared ds
447defined :: PDecl -> [Name]
448defined (PFix _ _ _) = []
449defined (PTy _ _ _ _ n t) = []
450defined (PPostulate _ _ _ _ n t) = []
451defined (PClauses _ _ n _) = [n] -- not a declaration
452defined (PCAF _ n _) = [n]
453defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
454 where fstt (_, a, _, _) = a
455defined (PData _ _ _ _ (PLaterdecl n _)) = []
456defined (PParams _ _ ds) = concatMap defined ds
457defined (PNamespace _ ds) = concatMap defined ds
458defined (PRecord _ _ _ n _ _ c _) = [n, c]
459defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
460defined (PInstance _ _ _ _ _ _ _ _) = []
461defined (PDSL n _) = [n]
462defined (PSyntax _ _) = []
463defined (PMutual _ ds) = concatMap defined ds
464defined (PDirective _) = []
467updateN :: [(Name, Name)] -> Name -> Name
468updateN ns n | Just n' <- lookup n ns = n'
471updateNs :: [(Name, Name)] -> PTerm -> PTerm
473updateNs ns t = mapPT updateRef t
474 where updateRef (PRef fc f) = PRef fc (updateN ns f)
477-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
479-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
480-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
481-- where updateCNs ns (PClause n l ts r ds)
482-- = PClause (updateN ns n) (fmap (updateNs ns) l)
483-- (map (fmap (updateNs ns)) ts)
484-- (fmap (updateNs ns) r)
485-- (map (updateDNs ns) ds)
488-- | High level language terms
489data PTerm = PQuote Raw
491 | PInferRef FC Name -- ^ A name to be defined later
493 | PLam Name PTerm PTerm
494 | PPi Plicity Name PTerm PTerm
495 | PLet Name PTerm PTerm PTerm
496 | PTyped PTerm PTerm -- ^ Term with explicit type
497 | PApp FC PTerm [PArg]
498 | PMatchApp FC Name -- ^ Make an application by type matching
499 | PCase FC PTerm [(PTerm, PTerm)]
505 | PRewrite FC PTerm PTerm
506 | PPair FC PTerm PTerm
507 | PDPair FC PTerm PTerm PTerm
508 | PAlternative Bool [PTerm] -- True if only one may work
509 | PHidden PTerm -- ^ Irrelevant or hidden pattern
517 | PProof [PTactic] -- ^ Proof script
518 | PTactics [PTactic] -- ^ As PProof, but no auto solving
519 | PElabError Err -- ^ Error to report on elaboration
520 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
521 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
522 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
525deriving instance Binary PTerm
528mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
529mapPT f t = f (mpt t) where
530 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
531 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
532 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
533 mpt (PRewrite fc t s) = PRewrite fc (mapPT f t) (mapPT f s)
534 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
535 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
536 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
537 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
538 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
539 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
540 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
541 mpt (PHidden t) = PHidden (mapPT f t)
542 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
543 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
544 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
545 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
549data PTactic' t = Intro [Name] | Intros | Focus Name
550 | Refine Name [Bool] | Rewrite t
552 | LetTac Name t | LetTacTy Name t t
553 | Exact t | Compute | Trivial
556 | ProofState | ProofTerm | Undo
557 | Try (PTactic' t) (PTactic' t)
558 | TSeq (PTactic' t) (PTactic' t)
559 | ApplyTactic t -- see Language.Reflection module
562 | GoalType String (PTactic' t)
564 deriving (Show, Eq, Functor)
566deriving instance Binary PTactic'
569instance Sized a => Sized (PTactic' a) where
570 size (Intro nms) = 1 + size nms
572 size (Focus nm) = 1 + size nm
573 size (Refine nm bs) = 1 + size nm + length bs
574 size (Rewrite t) = 1 + size t
575 size (LetTac nm t) = 1 + size nm + size t
576 size (Exact t) = 1 + size t
584 size (Try l r) = 1 + size l + size r
585 size (TSeq l r) = 1 + size l + size r
586 size (ApplyTactic t) = 1 + size t
587 size (Reflect t) = 1 + size t
588 size (Fill t) = 1 + size t
592type PTactic = PTactic' PTerm
594data PDo' t = DoExp FC t
599 deriving (Eq, Functor)
601deriving instance Binary PDo'
604instance Sized a => Sized (PDo' a) where
605 size (DoExp fc t) = 1 + size fc + size t
606 size (DoBind fc nm t) = 1 + size fc + size nm + size t
607 size (DoBindP fc l r) = 1 + size fc + size l + size r
608 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
609 size (DoLetP fc l r) = 1 + size fc + size l + size r
613-- The priority gives a hint as to elaboration order. Best to elaborate
614-- things early which will help give a more concrete type to other
615-- variables, e.g. a before (interpTy a).
616-- TODO: priority no longer serves any purpose, drop it!
618data PArg' t = PImp { priority :: Int,
619 lazyarg :: Bool, pname :: Name, getTm :: t,
621 | PExp { priority :: Int,
622 lazyarg :: Bool, getTm :: t,
624 | PConstraint { priority :: Int,
625 lazyarg :: Bool, getTm :: t,
627 | PTacImplicit { priority :: Int,
628 lazyarg :: Bool, pname :: Name,
632 deriving (Show, Eq, Functor)
634instance Sized a => Sized (PArg' a) where
635 size (PImp p l nm trm _) = 1 + size nm + size trm
636 size (PExp p l trm _) = 1 + size trm
637 size (PConstraint p l trm _) = 1 + size trm
638 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
641deriving instance Binary PArg'
644pimp n t = PImp 0 True n t ""
645pexp t = PExp 0 False t ""
646pconst t = PConstraint 0 False t ""
647ptacimp n s t = PTacImplicit 0 True n s t ""
649type PArg = PArg' PTerm
653data ClassInfo = CI { instanceName :: Name,
654 class_methods :: [(Name, (FnOpts, PTerm))],
655 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
656 class_params :: [Name],
657 class_instances :: [Name] }
660deriving instance Binary ClassInfo
663data OptInfo = Optimise { collapsible :: Bool,
664 forceable :: [Int], -- argument positions
668deriving instance Binary OptInfo
672data TypeInfo = TI { con_names :: [Name],
677deriving instance Binary TypeInfo
680-- Syntactic sugar info
682data DSL' t = DSL { dsl_bind :: t,
687 index_first :: Maybe t,
688 index_next :: Maybe t,
689 dsl_lambda :: Maybe t,
692 deriving (Show, Functor)
694deriving instance Binary DSL'
699data SynContext = PatternSyntax | TermSyntax | AnySyntax
702deriving instance Binary SynContext
705data Syntax = Rule [SSymbol] PTerm SynContext
708deriving instance Binary Syntax
711data SSymbol = Keyword Name
718deriving instance Binary SSymbol
721initDSL = DSL (PRef f (UN ">>="))
722 (PRef f (UN "return"))
730 where f = FC "(builtin)" 0
732data Using = UImplicit Name PTerm
733 | UConstraint Name [Name]
736deriving instance Binary Using
739data SyntaxInfo = Syn { using :: [Using],
740 syn_params :: [(Name, PTerm)],
741 syn_namespace :: [String],
743 decoration :: Name -> Name,
745 implicitAllowed :: Bool,
749deriving instance Binary SyntaxInfo
752defaultSyntax = Syn [] [] [] [] id False False initDSL
754expandNS :: SyntaxInfo -> Name -> Name
755expandNS syn n@(NS _ _) = n
756expandNS syn n = case syn_namespace syn of
761--- Pretty printing declarations and terms
763instance Show PTerm where
764 show tm = showImp False tm
766instance Pretty PTerm where
767 pretty = prettyImp False
769instance Show PDecl where
770 show d = showDeclImp False d
772instance Show PClause where
773 show c = showCImp True c
775instance Show PData where
776 show d = showDImp False d
778showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
779showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
780showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
781showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
782showDeclImp _ (PData _ _ _ _ d) = show d
783showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
784 showSep "\n" (map show ps)
787showCImp :: Bool -> PClause -> String
788showCImp impl (PClause _ n l ws r w)
789 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
790 ++ " where " ++ show w
793 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
794showCImp impl (PWith _ n l ws r w)
795 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
796 ++ " { " ++ show w ++ " } "
799 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
802showDImp :: Bool -> PData -> String
803showDImp impl (PDatadecl n ty cons)
804 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
806 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
808getImps :: [PArg] -> [(Name, PTerm)]
810getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
811getImps (_ : xs) = getImps xs
813getExps :: [PArg] -> [PTerm]
815getExps (PExp _ _ tm _ : xs) = tm : getExps xs
816getExps (_ : xs) = getExps xs
818getConsts :: [PArg] -> [PTerm]
820getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
821getConsts (_ : xs) = getConsts xs
823getAll :: [PArg] -> [PTerm]
826prettyImp :: Bool -> PTerm -> Doc
827prettyImp impl = prettySe 10
829 prettySe p (PQuote r) =
830 if size r > breakingSize then
831 text "![" $$ pretty r <> text "]"
833 text "![" <> pretty r <> text "]"
834 prettySe p (PPatvar fc n) = pretty n
835 prettySe p (PRef fc n) =
841 prettyBasic n@(UN _) = pretty n
842 prettyBasic (MN _ s) = text s
843 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
844 prettySe p (PLam n ty sc) =
846 if size sc > breakingSize then
847 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
849 text "λ" <> pretty n <+> text "=>" <+> pretty sc
850 prettySe p (PLet n ty v sc) =
852 if size sc > breakingSize then
853 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
854 nest nestingSize (prettySe 10 sc)
856 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
858 prettySe p (PPi (Exp l s _) n ty sc)
859 | n `elem` allNamesIn sc || impl =
860 let open = if l then text "|" <> lparen else lparen in
862 if size sc > breakingSize then
863 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
864 st <+> text "->" $+$ prettySe 10 sc
866 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
867 st <+> text "->" <+> prettySe 10 sc
870 if size sc > breakingSize then
871 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
873 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
877 Static -> text "[static]"
879 prettySe p (PPi (Imp l s _) n ty sc)
881 let open = if l then text "|" <> lbrace else lbrace in
883 if size sc > breakingSize then
884 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
885 st <+> text "->" <+> prettySe 10 sc
887 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
888 st <+> text "->" <+> prettySe 10 sc
889 | otherwise = prettySe 10 sc
893 Static -> text $ "[static]"
895 prettySe p (PPi (Constraint _ _ _) n ty sc) =
897 if size sc > breakingSize then
898 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
900 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
901 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
903 if size sc > breakingSize then
904 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
905 rbrace <+> text "->" $+$ prettySe 10 sc
907 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
908 rbrace <+> text "->" <+> prettySe 10 sc
909 prettySe p (PApp _ (PRef _ f) [])
910 | not impl = pretty f
911 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
912 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
913 let [l, r] = getExps args in
915 if size r > breakingSize then
916 prettySe 1 l <+> pretty op $+$ prettySe 0 r
918 prettySe 1 l <+> pretty op <+> prettySe 0 r
919 prettySe p (PApp _ f as) =
920 let args = getExps as in
925 -- foldr (<+>) empty $ map prettyArgS as
927 foldl' fSe empty args
928 -- foldr (<+>) empty $ map prettyArgSe args
931 if size r > breakingSize then
932 l $+$ nest nestingSize (prettyArgS r)
937 if size r > breakingSize then
938 l $+$ nest nestingSize (prettyArgSe r)
941 prettySe p (PCase _ scr opts) =
942 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
944 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
946 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
947 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
948 prettySe p (PRefl _ _) = text "refl"
949 prettySe p (PResolveTC _) = text "resolvetc"
950 prettySe p (PTrue _) = text "()"
951 prettySe p (PFalse _) = text "_|_"
952 prettySe p (PEq _ l r) =
954 if size r > breakingSize then
955 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
957 prettySe 10 l <+> text "=" <+> prettySe 10 r
958 prettySe p (PRewrite _ l r) =
960 if size r > breakingSize then
961 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
963 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
964 prettySe p (PTyped l r) =
965 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
966 prettySe p (PPair _ l r) =
967 if size r > breakingSize then
968 lparen <> prettySe 10 l <> text "," $+$
969 prettySe 10 r <> rparen
971 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
972 prettySe p (PDPair _ l t r) =
973 if size r > breakingSize then
974 lparen <> prettySe 10 l <+> text "**" $+$
975 prettySe 10 r <> rparen
977 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
978 prettySe p (PAlternative a as) =
979 lparen <> text "|" <> prettyAs <> text "|" <> rparen
982 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
983 prettySe p PType = text "Type"
984 prettySe p (PConstant c) = pretty c
985 -- XXX: add pretty for tactics
986 prettySe p (PProof ts) =
987 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
988 prettySe p (PTactics ts) =
989 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
990 prettySe p (PMetavar n) = text "?" <> pretty n
991 prettySe p (PReturn f) = text "return"
992 prettySe p PImpossible = text "impossible"
993 prettySe p Placeholder = text "_"
994 prettySe p (PDoBlock _) = text "do block pretty not implemented"
995 prettySe p (PElabError s) = pretty s
997 prettySe p _ = text "test"
999 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
1000 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1001 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1002 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1004 prettyArgSe arg = prettySe 0 arg
1005 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1006 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1007 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1009 bracket outer inner doc
1010 | inner > outer = lparen <> doc <> rparen
1013showImp :: Bool -> PTerm -> String
1014showImp impl tm = se 10 tm where
1015 se p (PQuote r) = "![" ++ show r ++ "]"
1016 se p (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1017 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1018 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
1020 where showbasic n@(UN _) = show n
1021 showbasic (MN _ s) = s
1022 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1023 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
1024 (if impl then " : " ++ se 10 ty else "") ++ " => "
1026 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
1028 se p (PPi (Exp l s _) n ty sc)
1029 | n `elem` allNamesIn sc || impl
1031 (if l then "|(" else "(") ++
1032 show n ++ " : " ++ se 10 ty ++
1035 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
1036 where st = case s of
1037 Static -> "[static] "
1039 se p (PPi (Imp l s _) n ty sc)
1040 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1041 show n ++ " : " ++ se 10 ty ++
1042 "} " ++ st ++ "-> " ++ se 10 sc
1043 | otherwise = se 10 sc
1044 where st = case s of
1045 Static -> "[static] "
1047 se p (PPi (Constraint _ _ _) n ty sc)
1048 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
1049 se p (PPi (TacImp _ _ s _) n ty sc)
1050 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
1052 | Just str <- slist p e = str
1053 | Just num <- snat p e = show num
1054 se p (PMatchApp _ f) = "match " ++ show f
1055 se p (PApp _ (PRef _ f) [])
1057 se p (PApp _ (PRef _ op@(UN (f:_))) args)
1058 | length (getExps args) == 2 && not impl && not (isAlpha f)
1059 = let [l, r] = getExps args in
1060 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
1062 = let args = getExps as in
1063 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
1064 else concatMap seArg args
1065 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
1066 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
1067 se p (PHidden tm) = "." ++ se 0 tm
1070 | otherwise = "refl {" ++ se 10 t ++ "}"
1071 se p (PResolveTC _) = "resolvetc"
1072 se p (PTrue _) = "()"
1073 se p (PFalse _) = "_|_"
1074 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
1075 se p (PRewrite _ l r) = bracket p 2 $ "rewrite " ++ se 10 l ++ " in " ++ se 10 r
1076 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
1077 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
1078 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
1079 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
1081 se p (PConstant c) = show c
1082 se p (PProof ts) = "proof { " ++ show ts ++ "}"
1083 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
1084 se p (PMetavar n) = "?" ++ show n
1085 se p (PReturn f) = "return"
1086 se p PImpossible = "impossible"
1087 se p Placeholder = "_"
1088 se p (PDoBlock _) = "do block show not implemented"
1089 se p (PElabError s) = show s
1090 se p (PCoerced t) = se p t
1091 se p (PUnifyLog t) = "%unifyLog " ++ se p t
1092-- se p x = "Not implemented"
1094 slist' p (PApp _ (PRef _ nil) _)
1095 | nsroot nil == UN "Nil" = Just []
1096 slist' p (PApp _ (PRef _ cons) args)
1097 | nsroot cons == UN "::",
1098 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1100 Just tl' <- slist' p tl
1103 isImp (PImp {}) = True
1105 slist' _ _ = Nothing
1107 slist p e | Just es <- slist' p e = Just $
1108 case es of [] -> "[]"
1109 [x] -> "[" ++ se p x ++ "]"
1110 xs -> "[" ++ intercalate "," (map (se p) xs) ++ "]"
1113 -- since Prelude is always imported, S & O are unqualified iff they're the
1116 | show o == (natns++"O") || show o == "O" = Just 0
1117 snat p (PApp _ s [PExp {getTm=n}])
1118 | show s == (natns++"S") || show s == "S",
1123 natns = "Prelude.Nat."
1125 sArg (PImp _ _ n tm _) = siArg (n, tm)
1126 sArg (PExp _ _ tm _) = seArg tm
1127 sArg (PConstraint _ _ tm _) = scArg tm
1128 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
1130 seArg arg = " " ++ se 0 arg
1131 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1132 scArg val = " {{" ++ se 10 val ++ "}}"
1133 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1135 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1139instance Sized PTerm where
1140 size (PQuote rawTerm) = size rawTerm
1141 size (PRef fc name) = size name
1142 size (PLam name ty bdy) = 1 + size ty + size bdy
1143 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1144 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1145 size (PTyped trm ty) = 1 + size trm + size ty
1146 size (PApp fc name args) = 1 + size args
1147 size (PCase fc trm bdy) = 1 + size trm + size bdy
1149 size (PFalse fc) = 1
1150 size (PRefl fc _) = 1
1151 size (PResolveTC fc) = 1
1152 size (PEq fc left right) = 1 + size left + size right
1153 size (PRewrite fc left right) = 1 + size left + size right
1154 size (PPair fc left right) = 1 + size left + size right
1155 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1156 size (PAlternative a alts) = 1 + size alts
1157 size (PHidden hidden) = size hidden
1158 size (PUnifyLog tm) = size tm
1160 size (PConstant const) = 1 + size const
1161 size Placeholder = 1
1162 size (PDoBlock dos) = 1 + size dos
1163 size (PIdiom fc term) = 1 + size term
1164 size (PReturn fc) = 1
1165 size (PMetavar name) = 1
1166 size (PProof tactics) = size tactics
1167 size (PElabError err) = size err
1168 size PImpossible = 1
1170getPArity :: PTerm -> Int
1171getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1174-- Return all names, free or globally bound, in the given term.
1176allNamesIn :: PTerm -> [Name]
1177allNamesIn tm = nub $ ni [] tm
1180 | not (n `elem` env) = [n]
1181 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1182 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1183 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1184 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1185 ni env (PHidden tm) = ni env tm
1186 ni env (PEq _ l r) = ni env l ++ ni env r
1187 ni env (PRewrite _ l r) = ni env l ++ ni env r
1188 ni env (PTyped l r) = ni env l ++ ni env r
1189 ni env (PPair _ l r) = ni env l ++ ni env r
1190 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1191 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1192 ni env (PAlternative a ls) = concatMap (ni env) ls
1193 ni env (PUnifyLog tm) = ni env tm
1196-- Return names which are free in the given term.
1198namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1199namesIn uvars ist tm = nub $ ni [] tm
1202 | not (n `elem` env)
1203 = case lookupTy n (tt_ctxt ist) of
1205 _ -> if n `elem` (map fst uvars) then [n] else []
1206 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1207 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1208 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1209 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1210 ni env (PEq _ l r) = ni env l ++ ni env r
1211 ni env (PRewrite _ l r) = ni env l ++ ni env r
1212 ni env (PTyped l r) = ni env l ++ ni env r
1213 ni env (PPair _ l r) = ni env l ++ ni env r
1214 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1215 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1216 ni env (PAlternative a as) = concatMap (ni env) as
1217 ni env (PHidden tm) = ni env tm
1218 ni env (PUnifyLog tm) = ni env tm
1221-- Return which of the given names are used in the given term.
1223usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1224usedNamesIn vars ist tm = nub $ ni [] tm
1227 | n `elem` vars && not (n `elem` env)
1228 = case lookupTy n (tt_ctxt ist) of
1231 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1232 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1233 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1234 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1235 ni env (PEq _ l r) = ni env l ++ ni env r
1236 ni env (PRewrite _ l r) = ni env l ++ ni env r
1237 ni env (PTyped l r) = ni env l ++ ni env r
1238 ni env (PPair _ l r) = ni env l ++ ni env r
1239 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1240 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1241 ni env (PAlternative a as) = concatMap (ni env) as
1242 ni env (PHidden tm) = ni env tm
1243 ni env (PUnifyLog 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