on bba09bd help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1377 lines 52 kB history raw
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntaxTree where
6import Core.TT
7import Core.Evaluate
8import Core.Elaborate hiding (Tactic(..))
9import Core.Typecheck
10import IRTS.Lang
11import IRTS.CodegenCommon
12import Util.Pretty
13import Util.DynamicLinker
15import Idris.Colours
17import Paths_idris
19import System.Console.Haskeline
22import Control.Monad.Trans.State.Strict
23import Control.Monad.Trans.Error
25import Data.List
26import Data.Char
27import Data.Either
28import Data.Word (Word)
30import Debug.Trace
32data IOption = IOption { opt_logLevel :: Int,
33 opt_typecase :: Bool,
34 opt_typeintype :: Bool,
35 opt_coverage :: Bool,
36 opt_showimp :: Bool, -- ^^ show implicits
37 opt_errContext :: Bool,
38 opt_repl :: Bool,
39 opt_verbose :: Bool,
40 opt_quiet :: Bool,
41 opt_codegen :: Codegen,
42 opt_outputTy :: OutputType,
43 opt_ibcsubdir :: FilePath,
44 opt_importdirs :: [FilePath],
45 opt_triple :: String,
46 opt_cpu :: String,
47 opt_optLevel :: Word,
48 opt_cmdline :: [Opt] -- remember whole command line
49 }
50 deriving (Show, Eq)
52defaultOpts = IOption { opt_logLevel = 0
53 , opt_typecase = False
54 , opt_typeintype = False
55 , opt_coverage = True
56 , opt_showimp = False
57 , opt_errContext = False
58 , opt_repl = True
59 , opt_verbose = True
60 , opt_quiet = False
61 , opt_codegen = ViaC
62 , opt_outputTy = Executable
63 , opt_ibcsubdir = ""
64 , opt_importdirs = []
65 , opt_triple = ""
66 , opt_cpu = ""
67 , opt_optLevel = 2
68 , opt_cmdline = []
69 }
71data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord)
73-- | The output mode in use
74data OutputMode = RawOutput | IdeSlave Integer deriving Show
76-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
77-- without typechecking).
78-- This will include all the functions and data declarations, plus fixity declarations
79-- and syntax macros.
81-- | The global state used in the Idris monad
82data IState = IState {
83 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
84 idris_constraints :: [(UConstraint, FC)],
85 -- ^ A list of universe constraints and their corresponding source locations
86 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
87 idris_implicits :: Ctxt [PArg],
88 idris_statics :: Ctxt [Bool],
89 idris_classes :: Ctxt ClassInfo,
90 idris_dsls :: Ctxt DSL,
91 idris_optimisation :: Ctxt OptInfo,
92 idris_datatypes :: Ctxt TypeInfo,
93 idris_patdefs :: Ctxt ([([Name], Term, Term)], [PTerm]), -- not exported
94 -- ^ list of lhs/rhs, and a list of missing clauses
95 idris_flags :: Ctxt [FnOpt],
96 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
97 idris_calledgraph :: Ctxt [Name],
98 idris_docstrings :: Ctxt String,
99 idris_totcheck :: [(FC, Name)],
100 idris_log :: String,
101 idris_options :: IOption,
102 idris_name :: Int,
103 idris_lineapps :: [((FilePath, Int), PTerm)],
104 -- ^ Full application LHS on source line
105 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
106 idris_coercions :: [Name],
107 idris_transforms :: [(Term, Term)],
108 syntax_rules :: [Syntax],
109 syntax_keywords :: [String],
110 imported :: [FilePath], -- ^ The imported modules
111 idris_scprims :: [(Name, (Int, PrimFn))],
112 idris_objs :: [(Codegen, FilePath)],
113 idris_libs :: [(Codegen, String)],
114 idris_cgflags :: [(Codegen, String)],
115 idris_hdrs :: [(Codegen, String)],
116 proof_list :: [(Name, [String])],
117 errLine :: Maybe Int,
118 lastParse :: Maybe Name,
119 indent_stack :: [Int],
120 brace_stack :: [Maybe Int],
121 hide_list :: [(Name, Maybe Accessibility)],
122 default_access :: Accessibility,
123 default_total :: Bool,
124 ibc_write :: [IBCWrite],
125 compiled_so :: Maybe String,
126 idris_dynamic_libs :: [DynamicLib],
127 idris_language_extensions :: [LanguageExt],
128 idris_outputmode :: OutputMode,
129 idris_colourRepl :: Bool,
130 idris_colourTheme :: ColourTheme
131 }
133data SizeChange = Smaller | Same | Bigger | Unknown
134 deriving (Show, Eq)
135{-!
136deriving instance Binary SizeChange
137!-}
139type SCGEntry = (Name, [Maybe (Int, SizeChange)])
141data CGInfo = CGInfo { argsdef :: [Name],
142 calls :: [(Name, [[Name]])],
143 scg :: [SCGEntry],
144 argsused :: [Name],
145 unusedpos :: [Int] }
146 deriving Show
147{-!
148deriving instance Binary CGInfo
149!-}
151primDefs = [UN "unsafePerformPrimIO",
152 UN "mkLazyForeignPrim",
153 UN "mkForeignPrim",
154 UN "FalseElim"]
156-- information that needs writing for the current module's .ibc file
157data IBCWrite = IBCFix FixDecl
158 | IBCImp Name
159 | IBCStatic Name
160 | IBCClass Name
161 | IBCInstance Bool Name Name
162 | IBCDSL Name
163 | IBCData Name
164 | IBCOpt Name
165 | IBCSyntax Syntax
166 | IBCKeyword String
167 | IBCImport FilePath
168 | IBCObj Codegen FilePath
169 | IBCLib Codegen String
170 | IBCCGFlag Codegen String
171 | IBCDyLib String
172 | IBCHeader Codegen String
173 | IBCAccess Name Accessibility
174 | IBCTotal Name Totality
175 | IBCFlags Name [FnOpt]
176 | IBCTrans (Term, Term)
177 | IBCCG Name
178 | IBCDoc Name
179 | IBCCoercion Name
180 | IBCDef Name -- i.e. main context
181 | IBCLineApp FilePath Int PTerm
182 deriving Show
184idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
185 emptyContext emptyContext emptyContext emptyContext
186 emptyContext emptyContext emptyContext emptyContext
187 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] []
188 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
189 True defaultTheme
191-- | The monad for the main REPL - reading and processing files and updating
192-- global state (hence the IO inner monad).
193--type Idris = WriterT [Either String (IO ())] (State IState a))
194type Idris = StateT IState (ErrorT Err IO)
196-- Commands in the REPL
198data Codegen = ViaC
199 | ViaJava
200 | ViaNode
201 | ViaJavaScript
202 | ViaLLVM
203 | Bytecode
204 deriving (Show, Eq)
206-- | REPL commands
207data Command = Quit
208 | Help
209 | Eval PTerm
210 | Check PTerm
211 | DocStr Name
212 | TotCheck Name
213 | Reload
214 | Load FilePath
215 | ChangeDirectory FilePath
216 | ModImport String
217 | Edit
218 | Compile Codegen String
219 | Execute
220 | ExecVal PTerm
221 | Metavars
222 | Prove Name
223 | AddProof (Maybe Name)
224 | RmProof Name
225 | ShowProof Name
226 | Proofs
227 | Universes
228 | TTShell
229 | LogLvl Int
230 | Spec PTerm
231 | HNF PTerm
232 | TestInline PTerm
233 | Defn Name
234 | Info Name
235 | Missing Name
236 | DynamicLink FilePath
237 | ListDynamic
238 | Pattelab PTerm
239 | DebugInfo Name
240 | Search PTerm
241 | CaseSplit Name PTerm
242 | CaseSplitAt Int Name
243 | SetOpt Opt
244 | UnsetOpt Opt
245 | NOP
246 | SetColour ColourType IdrisColour
247 | ColourOn
248 | ColourOff
250data Opt = Filename String
251 | Ver
252 | Usage
253 | Quiet
254 | ColourREPL Bool
255 | Ideslave
256 | ShowLibs
257 | ShowLibdir
258 | ShowIncs
259 | NoPrelude
260 | NoREPL
261 | OLogging Int
262 | Output String
263 | TypeCase
264 | TypeInType
265 | DefaultTotal
266 | DefaultPartial
267 | WarnPartial
268 | NoCoverage
269 | ErrContext
270 | ShowImpl
271 | Verbose
272 | IBCSubDir String
273 | ImportDir String
274 | PkgBuild String
275 | PkgInstall String
276 | PkgClean String
277 | WarnOnly
278 | Pkg String
279 | BCAsm String
280 | DumpDefun String
281 | DumpCases String
282 | FOVM String
283 | UseCodegen Codegen
284 | OutputTy OutputType
285 | Extension LanguageExt
286 | InterpretScript String
287 | TargetTriple String
288 | TargetCPU String
289 | OptLevel Word
290 deriving (Show, Eq)
292-- Parsed declarations
294data Fixity = Infixl { prec :: Int }
295 | Infixr { prec :: Int }
296 | InfixN { prec :: Int }
297 | PrefixN { prec :: Int }
298 deriving Eq
299{-!
300deriving instance Binary Fixity
301!-}
303instance Show Fixity where
304 show (Infixl i) = "infixl " ++ show i
305 show (Infixr i) = "infixr " ++ show i
306 show (InfixN i) = "infix " ++ show i
307 show (PrefixN i) = "prefix " ++ show i
309data FixDecl = Fix Fixity String
310 deriving Eq
312instance Show FixDecl where
313 show (Fix f s) = show f ++ " " ++ s
315{-!
316deriving instance Binary FixDecl
317!-}
319instance Ord FixDecl where
320 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
323data Static = Static | Dynamic
324 deriving (Show, Eq)
325{-!
326deriving instance Binary Static
327!-}
329-- Mark bindings with their explicitness, and laziness
330data Plicity = Imp { plazy :: Bool,
331 pstatic :: Static,
332 pdocstr :: String }
333 | Exp { plazy :: Bool,
334 pstatic :: Static,
335 pdocstr :: String }
336 | Constraint { plazy :: Bool,
337 pstatic :: Static,
338 pdocstr :: String }
339 | TacImp { plazy :: Bool,
340 pstatic :: Static,
341 pscript :: PTerm,
342 pdocstr :: String }
343 deriving (Show, Eq)
345{-!
346deriving instance Binary Plicity
347!-}
349impl = Imp False Dynamic ""
350expl = Exp False Dynamic ""
351constraint = Constraint False Dynamic ""
352tacimpl t = TacImp False Dynamic t ""
354data FnOpt = Inlinable -- always evaluate when simplifying
355 | TotalFn | PartialFn
356 | Coinductive | AssertTotal
357 | Dictionary -- type class dictionary, eval only when
358 -- a function argument, and further evaluation resutls
359 | Implicit -- implicit coercion
360 | CExport String -- export, with a C name
361 | Reflection -- a reflecting function, compile-time only
362 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
363 deriving (Show, Eq)
364{-!
365deriving instance Binary FnOpt
366!-}
368type FnOpts = [FnOpt]
370inlinable :: FnOpts -> Bool
371inlinable = elem Inlinable
373dictionary :: FnOpts -> Bool
374dictionary = elem Dictionary
376-- | Top-level declarations such as compiler directives, definitions,
377-- datatypes and typeclasses.
378data PDecl' t
379 = PFix FC Fixity [String] -- ^ Fixity declaration
380 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
381 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
382 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
383 | PCAF FC Name t -- ^ Top level constant
384 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
385 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
386 | PNamespace String [PDecl' t] -- ^ New namespace
387 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
388 | PClass String SyntaxInfo FC
389 [t] -- constraints
390 Name
391 [(Name, t)] -- parameters
392 [PDecl' t] -- declarations
393 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
394 -- class name, parameters, method declarations
395 | PInstance SyntaxInfo FC [t] -- constraints
396 Name -- class
397 [t] -- parameters
398 t -- full instance type
399 (Maybe Name) -- explicit name
400 [PDecl' t]
401 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
402 -- class name, parameters, full instance type, optional explicit name, and definitions
403 | PDSL Name (DSL' t) -- ^ DSL declaration
404 | PSyntax FC Syntax -- ^ Syntax definition
405 | PMutual FC [PDecl' t] -- ^ Mutual block
406 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
407 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
408 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
409 -- bool is True, lhs and rhs must be convertible
410 deriving Functor
411{-!
412deriving instance Binary PDecl'
413!-}
415-- | One clause of a top-level definition. Term arguments to constructors are:
416--
417-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
418--
419-- 2. The list of extra 'with' patterns
420--
421-- 3. The right-hand side
422--
423-- 4. The where block (PDecl' t)
425data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
426 | PWith FC Name t [t] t [PDecl' t]
427 | PClauseR FC [t] t [PDecl' t]
428 | PWithR FC [t] t [PDecl' t]
429 deriving Functor
430{-!
431deriving instance Binary PClause'
432!-}
434-- | Data declaration
435data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
436 d_tcon :: t, -- ^ Type constructor
437 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
438 }
439 -- ^ Data declaration
440 | PLaterdecl { d_name :: Name, d_tcon :: t }
441 -- ^ "Placeholder" for data whose constructors are defined later
442 deriving Functor
443{-!
444deriving instance Binary PData'
445!-}
447-- Handy to get a free function for applying PTerm -> PTerm functions
448-- across a program, by deriving Functor
450type PDecl = PDecl' PTerm
451type PData = PData' PTerm
452type PClause = PClause' PTerm
454-- get all the names declared in a decl
456declared :: PDecl -> [Name]
457declared (PFix _ _ _) = []
458declared (PTy _ _ _ _ n t) = [n]
459declared (PPostulate _ _ _ _ n t) = [n]
460declared (PClauses _ _ n _) = [] -- not a declaration
461declared (PCAF _ n _) = [n]
462declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
463 where fstt (_, a, _, _) = a
464declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
465declared (PParams _ _ ds) = concatMap declared ds
466declared (PNamespace _ ds) = concatMap declared ds
467declared (PRecord _ _ _ n _ _ c _) = [n, c]
468declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
469declared (PInstance _ _ _ _ _ _ _ _) = []
470declared (PDSL n _) = [n]
471declared (PSyntax _ _) = []
472declared (PMutual _ ds) = concatMap declared ds
473declared (PDirective _) = []
475-- get the names declared, not counting nested parameter blocks
476tldeclared :: PDecl -> [Name]
477tldeclared (PFix _ _ _) = []
478tldeclared (PTy _ _ _ _ n t) = [n]
479tldeclared (PPostulate _ _ _ _ n t) = [n]
480tldeclared (PClauses _ _ n _) = [] -- not a declaration
481tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
482tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
483 where fstt (_, a, _, _) = a
484tldeclared (PParams _ _ ds) = []
485tldeclared (PMutual _ ds) = concatMap tldeclared ds
486tldeclared (PNamespace _ ds) = concatMap tldeclared ds
487tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
488tldeclared (PInstance _ _ _ _ _ _ _ _) = []
489tldeclared _ = []
491defined :: PDecl -> [Name]
492defined (PFix _ _ _) = []
493defined (PTy _ _ _ _ n t) = []
494defined (PPostulate _ _ _ _ n t) = []
495defined (PClauses _ _ n _) = [n] -- not a declaration
496defined (PCAF _ n _) = [n]
497defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
498 where fstt (_, a, _, _) = a
499defined (PData _ _ _ _ (PLaterdecl n _)) = []
500defined (PParams _ _ ds) = concatMap defined ds
501defined (PNamespace _ ds) = concatMap defined ds
502defined (PRecord _ _ _ n _ _ c _) = [n, c]
503defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
504defined (PInstance _ _ _ _ _ _ _ _) = []
505defined (PDSL n _) = [n]
506defined (PSyntax _ _) = []
507defined (PMutual _ ds) = concatMap defined ds
508defined (PDirective _) = []
509--defined _ = []
511updateN :: [(Name, Name)] -> Name -> Name
512updateN ns n | Just n' <- lookup n ns = n'
513updateN _ n = n
515updateNs :: [(Name, Name)] -> PTerm -> PTerm
516updateNs [] t = t
517updateNs ns t = mapPT updateRef t
518 where updateRef (PRef fc f) = PRef fc (updateN ns f)
519 updateRef t = t
521-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
522-- updateDNs [] t = t
523-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
524-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
525-- where updateCNs ns (PClause n l ts r ds)
526-- = PClause (updateN ns n) (fmap (updateNs ns) l)
527-- (map (fmap (updateNs ns)) ts)
528-- (fmap (updateNs ns) r)
529-- (map (updateDNs ns) ds)
530-- updateDNs ns c = c
532-- | High level language terms
533data PTerm = PQuote Raw
534 | PRef FC Name
535 | PInferRef FC Name -- ^ A name to be defined later
536 | PPatvar FC Name
537 | PLam Name PTerm PTerm
538 | PPi Plicity Name PTerm PTerm
539 | PLet Name PTerm PTerm PTerm
540 | PTyped PTerm PTerm -- ^ Term with explicit type
541 | PApp FC PTerm [PArg]
542 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
543 | PMatchApp FC Name -- ^ Make an application by type matching
544 | PCase FC PTerm [(PTerm, PTerm)]
545 | PTrue FC
546 | PFalse FC
547 | PRefl FC PTerm
548 | PResolveTC FC
549 | PEq FC PTerm PTerm
550 | PRewrite FC PTerm PTerm (Maybe PTerm)
551 | PPair FC PTerm PTerm
552 | PDPair FC PTerm PTerm PTerm
553 | PAlternative Bool [PTerm] -- True if only one may work
554 | PHidden PTerm -- ^ Irrelevant or hidden pattern
555 | PType
556 | PGoal FC PTerm Name PTerm
557 | PConstant Const
558 | Placeholder
559 | PDoBlock [PDo]
560 | PIdiom FC PTerm
561 | PReturn FC
562 | PMetavar Name
563 | PProof [PTactic] -- ^ Proof script
564 | PTactics [PTactic] -- ^ As PProof, but no auto solving
565 | PElabError Err -- ^ Error to report on elaboration
566 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
567 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
568 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
569 | PNoImplicits PTerm -- ^ never run implicit converions on the term
570 deriving Eq
573{-!
574deriving instance Binary PTerm
575!-}
577mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
578mapPT f t = f (mpt t) where
579 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
580 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
581 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
582 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
583 (fmap (mapPT f) g)
584 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
585 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
586 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
587 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
588 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
589 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
590 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
591 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
592 mpt (PHidden t) = PHidden (mapPT f t)
593 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
594 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
595 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
596 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
597 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
598 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
599 mpt x = x
602data PTactic' t = Intro [Name] | Intros | Focus Name
603 | Refine Name [Bool] | Rewrite t
604 | Equiv t
605 | MatchRefine Name
606 | LetTac Name t | LetTacTy Name t t
607 | Exact t | Compute | Trivial
608 | Solve
609 | Attack
610 | ProofState | ProofTerm | Undo
611 | Try (PTactic' t) (PTactic' t)
612 | TSeq (PTactic' t) (PTactic' t)
613 | ApplyTactic t -- see Language.Reflection module
614 | Reflect t
615 | Fill t
616 | GoalType String (PTactic' t)
617 | Qed | Abandon
618 deriving (Show, Eq, Functor)
619{-!
620deriving instance Binary PTactic'
621!-}
623instance Sized a => Sized (PTactic' a) where
624 size (Intro nms) = 1 + size nms
625 size Intros = 1
626 size (Focus nm) = 1 + size nm
627 size (Refine nm bs) = 1 + size nm + length bs
628 size (Rewrite t) = 1 + size t
629 size (LetTac nm t) = 1 + size nm + size t
630 size (Exact t) = 1 + size t
631 size Compute = 1
632 size Trivial = 1
633 size Solve = 1
634 size Attack = 1
635 size ProofState = 1
636 size ProofTerm = 1
637 size Undo = 1
638 size (Try l r) = 1 + size l + size r
639 size (TSeq l r) = 1 + size l + size r
640 size (ApplyTactic t) = 1 + size t
641 size (Reflect t) = 1 + size t
642 size (Fill t) = 1 + size t
643 size Qed = 1
644 size Abandon = 1
646type PTactic = PTactic' PTerm
648data PDo' t = DoExp FC t
649 | DoBind FC Name t
650 | DoBindP FC t t
651 | DoLet FC Name t t
652 | DoLetP FC t t
653 deriving (Eq, Functor)
654{-!
655deriving instance Binary PDo'
656!-}
658instance Sized a => Sized (PDo' a) where
659 size (DoExp fc t) = 1 + size fc + size t
660 size (DoBind fc nm t) = 1 + size fc + size nm + size t
661 size (DoBindP fc l r) = 1 + size fc + size l + size r
662 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
663 size (DoLetP fc l r) = 1 + size fc + size l + size r
665type PDo = PDo' PTerm
667-- The priority gives a hint as to elaboration order. Best to elaborate
668-- things early which will help give a more concrete type to other
669-- variables, e.g. a before (interpTy a).
671data PArg' t = PImp { priority :: Int,
672 machine_inf :: Bool, -- true if the machine inferred it
673 lazyarg :: Bool, pname :: Name, getTm :: t,
674 pargdoc :: String }
675 | PExp { priority :: Int,
676 lazyarg :: Bool, getTm :: t,
677 pargdoc :: String }
678 | PConstraint { priority :: Int,
679 lazyarg :: Bool, getTm :: t,
680 pargdoc :: String }
681 | PTacImplicit { priority :: Int,
682 lazyarg :: Bool, pname :: Name,
683 getScript :: t,
684 getTm :: t,
685 pargdoc :: String }
686 deriving (Show, Eq, Functor)
688instance Sized a => Sized (PArg' a) where
689 size (PImp p _ l nm trm _) = 1 + size nm + size trm
690 size (PExp p l trm _) = 1 + size trm
691 size (PConstraint p l trm _) = 1 + size trm
692 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
694{-!
695deriving instance Binary PArg'
696!-}
698pimp n t mach = PImp 1 mach True n t ""
699pexp t = PExp 1 False t ""
700pconst t = PConstraint 1 False t ""
701ptacimp n s t = PTacImplicit 0 True n s t ""
703type PArg = PArg' PTerm
705-- Type class data
707data ClassInfo = CI { instanceName :: Name,
708 class_methods :: [(Name, (FnOpts, PTerm))],
709 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
710 class_params :: [Name],
711 class_instances :: [Name] }
712 deriving Show
713{-!
714deriving instance Binary ClassInfo
715!-}
717data OptInfo = Optimise { collapsible :: Bool,
718 isnewtype :: Bool,
719 forceable :: [Int], -- argument positions
720 recursive :: [Int] }
721 deriving Show
722{-!
723deriving instance Binary OptInfo
724!-}
727data TypeInfo = TI { con_names :: [Name],
728 codata :: Bool,
729 param_pos :: [Int] }
730 deriving Show
731{-!
732deriving instance Binary TypeInfo
733!-}
735-- Syntactic sugar info
737data DSL' t = DSL { dsl_bind :: t,
738 dsl_return :: t,
739 dsl_apply :: t,
740 dsl_pure :: t,
741 dsl_var :: Maybe t,
742 index_first :: Maybe t,
743 index_next :: Maybe t,
744 dsl_lambda :: Maybe t,
745 dsl_let :: Maybe t
746 }
747 deriving (Show, Functor)
748{-!
749deriving instance Binary DSL'
750!-}
752type DSL = DSL' PTerm
754data SynContext = PatternSyntax | TermSyntax | AnySyntax
755 deriving Show
756{-!
757deriving instance Binary SynContext
758!-}
760data Syntax = Rule [SSymbol] PTerm SynContext
761 deriving Show
762{-!
763deriving instance Binary Syntax
764!-}
766data SSymbol = Keyword Name
767 | Symbol String
768 | Binding Name
769 | Expr Name
770 | SimpleExpr Name
771 deriving Show
772{-!
773deriving instance Binary SSymbol
774!-}
776initDSL = DSL (PRef f (UN ">>="))
777 (PRef f (UN "return"))
778 (PRef f (UN "<$>"))
779 (PRef f (UN "pure"))
780 Nothing
781 Nothing
782 Nothing
783 Nothing
784 Nothing
785 where f = fileFC "(builtin)"
787data Using = UImplicit Name PTerm
788 | UConstraint Name [Name]
789 deriving (Show, Eq)
790{-!
791deriving instance Binary Using
792!-}
794data SyntaxInfo = Syn { using :: [Using],
795 syn_params :: [(Name, PTerm)],
796 syn_namespace :: [String],
797 no_imp :: [Name],
798 decoration :: Name -> Name,
799 inPattern :: Bool,
800 implicitAllowed :: Bool,
801 dsl_info :: DSL }
802 deriving Show
803{-!
804deriving instance Binary SyntaxInfo
805!-}
807defaultSyntax = Syn [] [] [] [] id False False initDSL
809expandNS :: SyntaxInfo -> Name -> Name
810expandNS syn n@(NS _ _) = n
811expandNS syn n = case syn_namespace syn of
812 [] -> n
813 xs -> NS n xs
816--- Pretty printing declarations and terms
818instance Show PTerm where
819 show tm = showImp Nothing False False tm
821instance Pretty PTerm where
822 pretty = prettyImp False
824instance Show PDecl where
825 show d = showDeclImp False d
827instance Show PClause where
828 show c = showCImp True c
830instance Show PData where
831 show d = showDImp False d
833showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
834showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
835showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
836showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
837showDeclImp _ (PData _ _ _ _ d) = show d
838showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
839 showSep "\n" (map show ps)
842showCImp :: Bool -> PClause -> String
843showCImp impl (PClause _ n l ws r w)
844 = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
845 ++ " where " ++ show w
846 where
847 showWs [] = ""
848 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
849showCImp impl (PWith _ n l ws r w)
850 = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
851 ++ " { " ++ show w ++ " } "
852 where
853 showWs [] = ""
854 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
857showDImp :: Bool -> PData -> String
858showDImp impl (PDatadecl n ty cons)
859 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
860 ++ showSep "\n\t| "
861 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
863getImps :: [PArg] -> [(Name, PTerm)]
864getImps [] = []
865getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
866getImps (_ : xs) = getImps xs
868getExps :: [PArg] -> [PTerm]
869getExps [] = []
870getExps (PExp _ _ tm _ : xs) = tm : getExps xs
871getExps (_ : xs) = getExps xs
873getConsts :: [PArg] -> [PTerm]
874getConsts [] = []
875getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
876getConsts (_ : xs) = getConsts xs
878getAll :: [PArg] -> [PTerm]
879getAll = map getTm
881-- | Pretty-print a high-level Idris term
882prettyImp :: Bool -- ^^ whether to show implicits
883 -> PTerm -- ^^ the term to pretty-print
884 -> Doc
885prettyImp impl = prettySe 10
886 where
887 prettySe p (PQuote r) =
888 if size r > breakingSize then
889 text "![" $$ pretty r <> text "]"
890 else
891 text "![" <> pretty r <> text "]"
892 prettySe p (PPatvar fc n) = pretty n
893 prettySe p (PRef fc n) =
894 if impl then
895 pretty n
896 else
897 prettyBasic n
898 where
899 prettyBasic n@(UN _) = pretty n
900 prettyBasic (MN _ s) = text s
901 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
902 prettyBasic (SN sn) = text (show sn)
903 prettySe p (PLam n ty sc) =
904 bracket p 2 $
905 if size sc > breakingSize then
906 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
907 else
908 text "λ" <> pretty n <+> text "=>" <+> pretty sc
909 prettySe p (PLet n ty v sc) =
910 bracket p 2 $
911 if size sc > breakingSize then
912 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
913 nest nestingSize (prettySe 10 sc)
914 else
915 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
916 prettySe 10 sc
917 prettySe p (PPi (Exp l s _) n ty sc)
918 | n `elem` allNamesIn sc || impl =
919 let open = if l then text "|" <> lparen else lparen in
920 bracket p 2 $
921 if size sc > breakingSize then
922 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
923 st <+> text "->" $+$ prettySe 10 sc
924 else
925 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
926 st <+> text "->" <+> prettySe 10 sc
927 | otherwise =
928 bracket p 2 $
929 if size sc > breakingSize then
930 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
931 else
932 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
933 where
934 st =
935 case s of
936 Static -> text "[static]"
937 _ -> empty
938 prettySe p (PPi (Imp l s _) n ty sc)
939 | impl =
940 let open = if l then text "|" <> lbrace else lbrace in
941 bracket p 2 $
942 if size sc > breakingSize then
943 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
944 st <+> text "->" <+> prettySe 10 sc
945 else
946 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
947 st <+> text "->" <+> prettySe 10 sc
948 | otherwise = prettySe 10 sc
949 where
950 st =
951 case s of
952 Static -> text $ "[static]"
953 _ -> empty
954 prettySe p (PPi (Constraint _ _ _) n ty sc) =
955 bracket p 2 $
956 if size sc > breakingSize then
957 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
958 else
959 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
960 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
961 bracket p 2 $
962 if size sc > breakingSize then
963 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
964 rbrace <+> text "->" $+$ prettySe 10 sc
965 else
966 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
967 rbrace <+> text "->" <+> prettySe 10 sc
968 prettySe p (PApp _ (PRef _ f) [])
969 | not impl = pretty f
970 prettySe p (PAppBind _ (PRef _ f) [])
971 | not impl = text "!" <+> pretty f
972 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
973 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
974 let [l, r] = getExps args in
975 bracket p 1 $
976 if size r > breakingSize then
977 prettySe 1 l <+> pretty op $+$ prettySe 0 r
978 else
979 prettySe 1 l <+> pretty op <+> prettySe 0 r
980 prettySe p (PApp _ f as) =
981 let args = getExps as in
982 bracket p 1 $
983 prettySe 1 f <+>
984 if impl then
985 foldl' fS empty as
986 -- foldr (<+>) empty $ map prettyArgS as
987 else
988 foldl' fSe empty args
989 -- foldr (<+>) empty $ map prettyArgSe args
990 where
991 fS l r =
992 if size r > breakingSize then
993 l $+$ nest nestingSize (prettyArgS r)
994 else
995 l <+> prettyArgS r
997 fSe l r =
998 if size r > breakingSize then
999 l $+$ nest nestingSize (prettyArgSe r)
1000 else
1001 l <+> prettyArgSe r
1002 prettySe p (PCase _ scr opts) =
1003 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
1004 where
1005 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
1007 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
1008 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
1009 prettySe p (PRefl _ _) = text "refl"
1010 prettySe p (PResolveTC _) = text "resolvetc"
1011 prettySe p (PTrue _) = text "()"
1012 prettySe p (PFalse _) = text "_|_"
1013 prettySe p (PEq _ l r) =
1014 bracket p 2 $
1015 if size r > breakingSize then
1016 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
1017 else
1018 prettySe 10 l <+> text "=" <+> prettySe 10 r
1019 prettySe p (PRewrite _ l r _) =
1020 bracket p 2 $
1021 if size r > breakingSize then
1022 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
1023 else
1024 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1025 prettySe p (PTyped l r) =
1026 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1027 prettySe p (PPair _ l r) =
1028 if size r > breakingSize then
1029 lparen <> prettySe 10 l <> text "," $+$
1030 prettySe 10 r <> rparen
1031 else
1032 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
1033 prettySe p (PDPair _ l t r) =
1034 if size r > breakingSize then
1035 lparen <> prettySe 10 l <+> text "**" $+$
1036 prettySe 10 r <> rparen
1037 else
1038 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1039 prettySe p (PAlternative a as) =
1040 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1041 where
1042 prettyAs =
1043 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1044 prettySe p PType = text "Type"
1045 prettySe p (PConstant c) = pretty c
1046 -- XXX: add pretty for tactics
1047 prettySe p (PProof ts) =
1048 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1049 prettySe p (PTactics ts) =
1050 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1051 prettySe p (PMetavar n) = text "?" <> pretty n
1052 prettySe p (PReturn f) = text "return"
1053 prettySe p PImpossible = text "impossible"
1054 prettySe p Placeholder = text "_"
1055 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1056 prettySe p (PElabError s) = pretty s
1058 prettySe p _ = text "test"
1060 prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1061 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1062 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1063 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1065 prettyArgSe arg = prettySe 0 arg
1066 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1067 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1068 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1070 bracket outer inner doc
1071 | inner > outer = lparen <> doc <> rparen
1072 | otherwise = doc
1074-- | Show Idris name
1075showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1076 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1077 -> Bool -- ^^ whether to show implicits
1078 -> Bool -- ^^ whether to colourise
1079 -> Name -- ^^ the term to show
1080 -> String
1081showName ist bnd impl colour n = case ist of
1082 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1083 Nothing -> showbasic n
1084 where name = if impl then show n else showbasic n
1085 showbasic n@(UN _) = show n
1086 showbasic (MN _ s) = s
1087 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1088 showbasic (SN s) = show s
1089 fst3 (x, _, _) = x
1090 colourise n t = let ctxt' = fmap tt_ctxt ist in
1091 case ctxt' of
1092 Nothing -> name
1093 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1094 else colouriseBound t name
1095 | isDConName n ctxt -> colouriseData t name
1096 | isFnName n ctxt -> colouriseFun t name
1097 | isTConName n ctxt -> colouriseType t name
1098 -- The assumption is that if a name is not bound and does not exist in the
1099 -- global context, then we're somewhere in which implicit info has been lost
1100 -- (like error messages). Thus, unknown vars are colourised as implicits.
1101 | otherwise -> colouriseImplicit t name
1103-- | Show Idris term
1104showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1105 -> Bool -- ^^ whether to show implicits
1106 -> Bool -- ^^ whether to colourise
1107 -> PTerm -- ^^ the term to show
1108 -> String
1109showImp ist impl colour tm = se 10 [] tm where
1110 perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1111 perhapsColourise col str = case ist of
1112 Just i -> if colour then col (idris_colourTheme i) str else str
1113 Nothing -> str
1115 se :: Int -> [(Name, Bool)] -> PTerm -> String
1116 se p bnd (PQuote r) = "![" ++ show r ++ "]"
1117 se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1118 se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1119 se p bnd e
1120 | Just str <- slist p bnd e = str
1121 | Just num <- snat p e = perhapsColourise colouriseData (show num)
1122 se p bnd (PRef fc n) = showName ist bnd impl colour n
1123 se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1124 (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1125 ++ se 10 ((n, False):bnd) sc
1126 se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1127 " = " ++ se 10 bnd v ++
1128 " in " ++ se 10 ((n, False):bnd) sc
1129 se p bnd (PPi (Exp l s _) n ty sc)
1130 | n `elem` allNamesIn sc || impl
1131 = bracket p 2 $
1132 (if l then "|(" else "(") ++
1133 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1134 ") " ++ st ++
1135 "-> " ++ se 10 ((n, False):bnd) sc
1136 | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1137 where st = case s of
1138 Static -> "[static] "
1139 _ -> ""
1140 se p bnd (PPi (Imp l s _) n ty sc)
1141 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1142 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1143 "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1144 | otherwise = se 10 ((n, True):bnd) sc
1145 where st = case s of
1146 Static -> "[static] "
1147 _ -> ""
1148 se p bnd (PPi (Constraint _ _ _) n ty sc)
1149 = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1150 se p bnd (PPi (TacImp _ _ s _) n ty sc)
1151 = bracket p 2 $
1152 "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1153 se 10 ((n, False):bnd) sc
1154 se p bnd (PMatchApp _ f) = "match " ++ show f
1155 se p bnd (PApp _ hd@(PRef _ f) [])
1156 | not impl = se p bnd hd
1157 se p bnd (PAppBind _ hd@(PRef _ f) [])
1158 | not impl = "!" ++ se p bnd hd
1159 se p bnd (PApp _ op@(PRef _ (UN (f:_))) args)
1160 | length (getExps args) == 2 && not impl && not (isAlpha f)
1161 = let [l, r] = getExps args in
1162 bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1163 se p bnd (PApp _ f as)
1164 = -- let args = getExps as in
1165 bracket p 1 $ se 1 bnd f ++
1166 if impl then concatMap (sArg bnd) as
1167 else concatMap (suiArg impl bnd) as
1168 se p bnd (PAppBind _ f as)
1169 = let args = getExps as in
1170 "!" ++ (bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1171 else concatMap (seArg bnd) args)
1172 se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1173 where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1174 se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1175 se p bnd (PRefl _ t)
1176 | not impl = perhapsColourise colouriseData "refl"
1177 | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1178 se p bnd (PResolveTC _) = "resolvetc"
1179 se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1180 se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1181 se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1182 se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1183 se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1184 se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1185 se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1186 se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1187 se p bnd PType = perhapsColourise colouriseType "Type"
1188 se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1189 where cfun (AType _) = colouriseType
1190 cfun StrType = colouriseType
1191 cfun PtrType = colouriseType
1192 cfun VoidType = colouriseType
1193 cfun _ = colouriseData
1194 se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1195 se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1196 se p bnd (PMetavar n) = "?" ++ show n
1197 se p bnd (PReturn f) = "return"
1198 se p bnd PImpossible = "impossible"
1199 se p bnd Placeholder = "_"
1200 se p bnd (PDoBlock _) = "do block show not implemented"
1201 se p bnd (PElabError s) = show s
1202 se p bnd (PCoerced t) = se p bnd t
1203 se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1204 se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1205-- se p bnd x = "Not implemented"
1207 slist' p bnd (PApp _ (PRef _ nil) _)
1208 | nsroot nil == UN "Nil" = Just []
1209 slist' p bnd (PApp _ (PRef _ cons) args)
1210 | nsroot cons == UN "::",
1211 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1212 all isImp imps,
1213 Just tl' <- slist' p bnd tl
1214 = Just (hd:tl')
1215 where
1216 isImp (PImp {}) = True
1217 isImp _ = False
1218 slist' _ _ _ = Nothing
1220 slist p bnd e | Just es <- slist' p bnd e = Just $
1221 case es of [] -> "[]"
1222 [x] -> "[" ++ se p bnd x ++ "]"
1223 xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1224 slist _ _ _ = Nothing
1226 -- since Prelude is always imported, S & Z are unqualified iff they're the
1227 -- Nat ones.
1228 snat p (PRef _ o)
1229 | show o == (natns++"Z") || show o == "Z" = Just 0
1230 snat p (PApp _ s [PExp {getTm=n}])
1231 | show s == (natns++"S") || show s == "S",
1232 Just n' <- snat p n
1233 = Just $ 1 + n'
1234 snat _ _ = Nothing
1236 natns = "Prelude.Nat."
1238 sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1239 sArg bnd (PExp _ _ tm _) = seArg bnd tm
1240 sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1241 sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1243 -- show argument, implicits given by the user also shown
1244 suiArg impl bnd (PImp _ mi _ n tm _)
1245 | impl || not mi = siArg bnd (n, tm)
1246 suiArg impl bnd (PExp _ _ tm _) = seArg bnd tm
1247 suiArg impl bnd _ = ""
1249 seArg bnd arg = " " ++ se 0 bnd arg
1250 siArg bnd (n, val) =
1251 let n' = show n
1252 val' = se 10 bnd val in
1253 if (n' == val')
1254 then " {" ++ n' ++ "}"
1255 else " {" ++ n' ++ " = " ++ val' ++ "}"
1256 scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1257 stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1259 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1260 | otherwise = str
1263instance Sized PTerm where
1264 size (PQuote rawTerm) = size rawTerm
1265 size (PRef fc name) = size name
1266 size (PLam name ty bdy) = 1 + size ty + size bdy
1267 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1268 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1269 size (PTyped trm ty) = 1 + size trm + size ty
1270 size (PApp fc name args) = 1 + size args
1271 size (PAppBind fc name args) = 1 + size args
1272 size (PCase fc trm bdy) = 1 + size trm + size bdy
1273 size (PTrue fc) = 1
1274 size (PFalse fc) = 1
1275 size (PRefl fc _) = 1
1276 size (PResolveTC fc) = 1
1277 size (PEq fc left right) = 1 + size left + size right
1278 size (PRewrite fc left right _) = 1 + size left + size right
1279 size (PPair fc left right) = 1 + size left + size right
1280 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1281 size (PAlternative a alts) = 1 + size alts
1282 size (PHidden hidden) = size hidden
1283 size (PUnifyLog tm) = size tm
1284 size (PNoImplicits tm) = size tm
1285 size PType = 1
1286 size (PConstant const) = 1 + size const
1287 size Placeholder = 1
1288 size (PDoBlock dos) = 1 + size dos
1289 size (PIdiom fc term) = 1 + size term
1290 size (PReturn fc) = 1
1291 size (PMetavar name) = 1
1292 size (PProof tactics) = size tactics
1293 size (PElabError err) = size err
1294 size PImpossible = 1
1296getPArity :: PTerm -> Int
1297getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1298getPArity _ = 0
1300-- Return all names, free or globally bound, in the given term.
1302allNamesIn :: PTerm -> [Name]
1303allNamesIn tm = nub $ ni [] tm
1304 where
1305 ni env (PRef _ n)
1306 | not (n `elem` env) = [n]
1307 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1308 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1309 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1310 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1311 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1312 ni env (PHidden tm) = ni env tm
1313 ni env (PEq _ l r) = ni env l ++ ni env r
1314 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1315 ni env (PTyped l r) = ni env l ++ ni env r
1316 ni env (PPair _ l r) = ni env l ++ ni env r
1317 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1318 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1319 ni env (PAlternative a ls) = concatMap (ni env) ls
1320 ni env (PUnifyLog tm) = ni env tm
1321 ni env (PNoImplicits tm) = ni env tm
1322 ni env _ = []
1324-- Return names which are free in the given term.
1326namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1327namesIn uvars ist tm = nub $ ni [] tm
1328 where
1329 ni env (PRef _ n)
1330 | not (n `elem` env)
1331 = case lookupTy n (tt_ctxt ist) of
1332 [] -> [n]
1333 _ -> if n `elem` (map fst uvars) then [n] else []
1334 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1335 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1336 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1337 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1338 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1339 ni env (PEq _ l r) = ni env l ++ ni env r
1340 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1341 ni env (PTyped l r) = ni env l ++ ni env r
1342 ni env (PPair _ l r) = ni env l ++ ni env r
1343 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1344 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1345 ni env (PAlternative a as) = concatMap (ni env) as
1346 ni env (PHidden tm) = ni env tm
1347 ni env (PUnifyLog tm) = ni env tm
1348 ni env (PNoImplicits tm) = ni env tm
1349 ni env _ = []
1351-- Return which of the given names are used in the given term.
1353usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1354usedNamesIn vars ist tm = nub $ ni [] tm
1355 where
1356 ni env (PRef _ n)
1357 | n `elem` vars && not (n `elem` env)
1358 = case lookupTy n (tt_ctxt ist) of
1359 [] -> [n]
1360 _ -> []
1361 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1362 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1363 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1364 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1365 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1366 ni env (PEq _ l r) = ni env l ++ ni env r
1367 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1368 ni env (PTyped l r) = ni env l ++ ni env r
1369 ni env (PPair _ l r) = ni env l ++ ni env r
1370 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1371 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1372 ni env (PAlternative a as) = concatMap (ni env) as
1373 ni env (PHidden tm) = ni env tm
1374 ni env (PUnifyLog tm) = ni env tm
1375 ni env (PNoImplicits tm) = ni env tm
1376 ni env _ = []