on 8c19e30 help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1376 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_linename :: [((FilePath, Int), (Name, Int))],
104 -- ^ internal name for function defined on that line,
105 -- number of 'with' arguments
106 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
107 idris_coercions :: [Name],
108 idris_transforms :: [(Term, Term)],
109 syntax_rules :: [Syntax],
110 syntax_keywords :: [String],
111 imported :: [FilePath], -- ^ The imported modules
112 idris_scprims :: [(Name, (Int, PrimFn))],
113 idris_objs :: [(Codegen, FilePath)],
114 idris_libs :: [(Codegen, String)],
115 idris_cgflags :: [(Codegen, String)],
116 idris_hdrs :: [(Codegen, String)],
117 proof_list :: [(Name, [String])],
118 errLine :: Maybe Int,
119 lastParse :: Maybe Name,
120 indent_stack :: [Int],
121 brace_stack :: [Maybe Int],
122 hide_list :: [(Name, Maybe Accessibility)],
123 default_access :: Accessibility,
124 default_total :: Bool,
125 ibc_write :: [IBCWrite],
126 compiled_so :: Maybe String,
127 idris_dynamic_libs :: [DynamicLib],
128 idris_language_extensions :: [LanguageExt],
129 idris_outputmode :: OutputMode,
130 idris_colourRepl :: Bool,
131 idris_colourTheme :: ColourTheme
132 }
134data SizeChange = Smaller | Same | Bigger | Unknown
135 deriving (Show, Eq)
136{-!
137deriving instance Binary SizeChange
138!-}
140type SCGEntry = (Name, [Maybe (Int, SizeChange)])
142data CGInfo = CGInfo { argsdef :: [Name],
143 calls :: [(Name, [[Name]])],
144 scg :: [SCGEntry],
145 argsused :: [Name],
146 unusedpos :: [Int] }
147 deriving Show
148{-!
149deriving instance Binary CGInfo
150!-}
152primDefs = [UN "unsafePerformPrimIO",
153 UN "mkLazyForeignPrim",
154 UN "mkForeignPrim",
155 UN "FalseElim"]
157-- information that needs writing for the current module's .ibc file
158data IBCWrite = IBCFix FixDecl
159 | IBCImp Name
160 | IBCStatic Name
161 | IBCClass Name
162 | IBCInstance Bool Name Name
163 | IBCDSL Name
164 | IBCData Name
165 | IBCOpt Name
166 | IBCSyntax Syntax
167 | IBCKeyword String
168 | IBCImport FilePath
169 | IBCObj Codegen FilePath
170 | IBCLib Codegen String
171 | IBCCGFlag Codegen String
172 | IBCDyLib String
173 | IBCHeader Codegen String
174 | IBCAccess Name Accessibility
175 | IBCTotal Name Totality
176 | IBCFlags Name [FnOpt]
177 | IBCTrans (Term, Term)
178 | IBCCG Name
179 | IBCDoc Name
180 | IBCCoercion Name
181 | IBCDef Name -- i.e. main context
182 | IBCLineName FilePath Int Name Int
183 deriving Show
185idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
186 emptyContext emptyContext emptyContext emptyContext
187 emptyContext emptyContext emptyContext emptyContext
188 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] []
189 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
190 True defaultTheme
192-- | The monad for the main REPL - reading and processing files and updating
193-- global state (hence the IO inner monad).
194--type Idris = WriterT [Either String (IO ())] (State IState a))
195type Idris = StateT IState (ErrorT Err IO)
197-- Commands in the REPL
199data Codegen = ViaC
200 | ViaJava
201 | ViaNode
202 | ViaJavaScript
203 | ViaLLVM
204 | Bytecode
205 deriving (Show, Eq)
207-- | REPL commands
208data Command = Quit
209 | Help
210 | Eval PTerm
211 | Check PTerm
212 | DocStr Name
213 | TotCheck Name
214 | Reload
215 | Load FilePath
216 | ChangeDirectory FilePath
217 | ModImport String
218 | Edit
219 | Compile Codegen String
220 | Execute
221 | ExecVal PTerm
222 | Metavars
223 | Prove Name
224 | AddProof (Maybe Name)
225 | RmProof Name
226 | ShowProof Name
227 | Proofs
228 | Universes
229 | TTShell
230 | LogLvl Int
231 | Spec PTerm
232 | HNF PTerm
233 | TestInline PTerm
234 | Defn Name
235 | Info Name
236 | Missing Name
237 | DynamicLink FilePath
238 | ListDynamic
239 | Pattelab PTerm
240 | DebugInfo Name
241 | Search PTerm
242 | CaseSplit Name PTerm
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 patterns
420--
421-- 3. The right-hand side
422--
423-- 4. The where block (PDecl' t)
424data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
425 | PWith FC Name t [t] t [PDecl' t]
426 | PClauseR FC [t] t [PDecl' t]
427 | PWithR FC [t] t [PDecl' t]
428 deriving Functor
429{-!
430deriving instance Binary PClause'
431!-}
433-- | Data declaration
434data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
435 d_tcon :: t, -- ^ Type constructor
436 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
437 }
438 -- ^ Data declaration
439 | PLaterdecl { d_name :: Name, d_tcon :: t }
440 -- ^ "Placeholder" for data whose constructors are defined later
441 deriving Functor
442{-!
443deriving instance Binary PData'
444!-}
446-- Handy to get a free function for applying PTerm -> PTerm functions
447-- across a program, by deriving Functor
449type PDecl = PDecl' PTerm
450type PData = PData' PTerm
451type PClause = PClause' PTerm
453-- get all the names declared in a decl
455declared :: PDecl -> [Name]
456declared (PFix _ _ _) = []
457declared (PTy _ _ _ _ n t) = [n]
458declared (PPostulate _ _ _ _ n t) = [n]
459declared (PClauses _ _ n _) = [] -- not a declaration
460declared (PCAF _ n _) = [n]
461declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
462 where fstt (_, a, _, _) = a
463declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
464declared (PParams _ _ ds) = concatMap declared ds
465declared (PNamespace _ ds) = concatMap declared ds
466declared (PRecord _ _ _ n _ _ c _) = [n, c]
467declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
468declared (PInstance _ _ _ _ _ _ _ _) = []
469declared (PDSL n _) = [n]
470declared (PSyntax _ _) = []
471declared (PMutual _ ds) = concatMap declared ds
472declared (PDirective _) = []
474-- get the names declared, not counting nested parameter blocks
475tldeclared :: PDecl -> [Name]
476tldeclared (PFix _ _ _) = []
477tldeclared (PTy _ _ _ _ n t) = [n]
478tldeclared (PPostulate _ _ _ _ n t) = [n]
479tldeclared (PClauses _ _ n _) = [] -- not a declaration
480tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
481tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
482 where fstt (_, a, _, _) = a
483tldeclared (PParams _ _ ds) = []
484tldeclared (PMutual _ ds) = concatMap tldeclared ds
485tldeclared (PNamespace _ ds) = concatMap tldeclared ds
486tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
487tldeclared (PInstance _ _ _ _ _ _ _ _) = []
488tldeclared _ = []
490defined :: PDecl -> [Name]
491defined (PFix _ _ _) = []
492defined (PTy _ _ _ _ n t) = []
493defined (PPostulate _ _ _ _ n t) = []
494defined (PClauses _ _ n _) = [n] -- not a declaration
495defined (PCAF _ n _) = [n]
496defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
497 where fstt (_, a, _, _) = a
498defined (PData _ _ _ _ (PLaterdecl n _)) = []
499defined (PParams _ _ ds) = concatMap defined ds
500defined (PNamespace _ ds) = concatMap defined ds
501defined (PRecord _ _ _ n _ _ c _) = [n, c]
502defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
503defined (PInstance _ _ _ _ _ _ _ _) = []
504defined (PDSL n _) = [n]
505defined (PSyntax _ _) = []
506defined (PMutual _ ds) = concatMap defined ds
507defined (PDirective _) = []
508--defined _ = []
510updateN :: [(Name, Name)] -> Name -> Name
511updateN ns n | Just n' <- lookup n ns = n'
512updateN _ n = n
514updateNs :: [(Name, Name)] -> PTerm -> PTerm
515updateNs [] t = t
516updateNs ns t = mapPT updateRef t
517 where updateRef (PRef fc f) = PRef fc (updateN ns f)
518 updateRef t = t
520-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
521-- updateDNs [] t = t
522-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
523-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
524-- where updateCNs ns (PClause n l ts r ds)
525-- = PClause (updateN ns n) (fmap (updateNs ns) l)
526-- (map (fmap (updateNs ns)) ts)
527-- (fmap (updateNs ns) r)
528-- (map (updateDNs ns) ds)
529-- updateDNs ns c = c
531-- | High level language terms
532data PTerm = PQuote Raw
533 | PRef FC Name
534 | PInferRef FC Name -- ^ A name to be defined later
535 | PPatvar FC Name
536 | PLam Name PTerm PTerm
537 | PPi Plicity Name PTerm PTerm
538 | PLet Name PTerm PTerm PTerm
539 | PTyped PTerm PTerm -- ^ Term with explicit type
540 | PApp FC PTerm [PArg]
541 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
542 | PMatchApp FC Name -- ^ Make an application by type matching
543 | PCase FC PTerm [(PTerm, PTerm)]
544 | PTrue FC
545 | PFalse FC
546 | PRefl FC PTerm
547 | PResolveTC FC
548 | PEq FC PTerm PTerm
549 | PRewrite FC PTerm PTerm (Maybe PTerm)
550 | PPair FC PTerm PTerm
551 | PDPair FC PTerm PTerm PTerm
552 | PAlternative Bool [PTerm] -- True if only one may work
553 | PHidden PTerm -- ^ Irrelevant or hidden pattern
554 | PType
555 | PGoal FC PTerm Name PTerm
556 | PConstant Const
557 | Placeholder
558 | PDoBlock [PDo]
559 | PIdiom FC PTerm
560 | PReturn FC
561 | PMetavar Name
562 | PProof [PTactic] -- ^ Proof script
563 | PTactics [PTactic] -- ^ As PProof, but no auto solving
564 | PElabError Err -- ^ Error to report on elaboration
565 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
566 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
567 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
568 | PNoImplicits PTerm -- ^ never run implicit converions on the term
569 deriving Eq
572{-!
573deriving instance Binary PTerm
574!-}
576mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
577mapPT f t = f (mpt t) where
578 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
579 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
580 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
581 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
582 (fmap (mapPT f) g)
583 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
584 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
585 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
586 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
587 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
588 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
589 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
590 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
591 mpt (PHidden t) = PHidden (mapPT f t)
592 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
593 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
594 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
595 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
596 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
597 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
598 mpt x = x
601data PTactic' t = Intro [Name] | Intros | Focus Name
602 | Refine Name [Bool] | Rewrite t
603 | Equiv t
604 | MatchRefine Name
605 | LetTac Name t | LetTacTy Name t t
606 | Exact t | Compute | Trivial
607 | Solve
608 | Attack
609 | ProofState | ProofTerm | Undo
610 | Try (PTactic' t) (PTactic' t)
611 | TSeq (PTactic' t) (PTactic' t)
612 | ApplyTactic t -- see Language.Reflection module
613 | Reflect t
614 | Fill t
615 | GoalType String (PTactic' t)
616 | Qed | Abandon
617 deriving (Show, Eq, Functor)
618{-!
619deriving instance Binary PTactic'
620!-}
622instance Sized a => Sized (PTactic' a) where
623 size (Intro nms) = 1 + size nms
624 size Intros = 1
625 size (Focus nm) = 1 + size nm
626 size (Refine nm bs) = 1 + size nm + length bs
627 size (Rewrite t) = 1 + size t
628 size (LetTac nm t) = 1 + size nm + size t
629 size (Exact t) = 1 + size t
630 size Compute = 1
631 size Trivial = 1
632 size Solve = 1
633 size Attack = 1
634 size ProofState = 1
635 size ProofTerm = 1
636 size Undo = 1
637 size (Try l r) = 1 + size l + size r
638 size (TSeq l r) = 1 + size l + size r
639 size (ApplyTactic t) = 1 + size t
640 size (Reflect t) = 1 + size t
641 size (Fill t) = 1 + size t
642 size Qed = 1
643 size Abandon = 1
645type PTactic = PTactic' PTerm
647data PDo' t = DoExp FC t
648 | DoBind FC Name t
649 | DoBindP FC t t
650 | DoLet FC Name t t
651 | DoLetP FC t t
652 deriving (Eq, Functor)
653{-!
654deriving instance Binary PDo'
655!-}
657instance Sized a => Sized (PDo' a) where
658 size (DoExp fc t) = 1 + size fc + size t
659 size (DoBind fc nm t) = 1 + size fc + size nm + size t
660 size (DoBindP fc l r) = 1 + size fc + size l + size r
661 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
662 size (DoLetP fc l r) = 1 + size fc + size l + size r
664type PDo = PDo' PTerm
666-- The priority gives a hint as to elaboration order. Best to elaborate
667-- things early which will help give a more concrete type to other
668-- variables, e.g. a before (interpTy a).
670data PArg' t = PImp { priority :: Int,
671 machine_inf :: Bool, -- true if the machine inferred it
672 lazyarg :: Bool, pname :: Name, getTm :: t,
673 pargdoc :: String }
674 | PExp { priority :: Int,
675 lazyarg :: Bool, getTm :: t,
676 pargdoc :: String }
677 | PConstraint { priority :: Int,
678 lazyarg :: Bool, getTm :: t,
679 pargdoc :: String }
680 | PTacImplicit { priority :: Int,
681 lazyarg :: Bool, pname :: Name,
682 getScript :: t,
683 getTm :: t,
684 pargdoc :: String }
685 deriving (Show, Eq, Functor)
687instance Sized a => Sized (PArg' a) where
688 size (PImp p _ l nm trm _) = 1 + size nm + size trm
689 size (PExp p l trm _) = 1 + size trm
690 size (PConstraint p l trm _) = 1 + size trm
691 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
693{-!
694deriving instance Binary PArg'
695!-}
697pimp n t mach = PImp 1 mach True n t ""
698pexp t = PExp 1 False t ""
699pconst t = PConstraint 1 False t ""
700ptacimp n s t = PTacImplicit 0 True n s t ""
702type PArg = PArg' PTerm
704-- Type class data
706data ClassInfo = CI { instanceName :: Name,
707 class_methods :: [(Name, (FnOpts, PTerm))],
708 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
709 class_params :: [Name],
710 class_instances :: [Name] }
711 deriving Show
712{-!
713deriving instance Binary ClassInfo
714!-}
716data OptInfo = Optimise { collapsible :: Bool,
717 isnewtype :: Bool,
718 forceable :: [Int], -- argument positions
719 recursive :: [Int] }
720 deriving Show
721{-!
722deriving instance Binary OptInfo
723!-}
726data TypeInfo = TI { con_names :: [Name],
727 codata :: Bool,
728 param_pos :: [Int] }
729 deriving Show
730{-!
731deriving instance Binary TypeInfo
732!-}
734-- Syntactic sugar info
736data DSL' t = DSL { dsl_bind :: t,
737 dsl_return :: t,
738 dsl_apply :: t,
739 dsl_pure :: t,
740 dsl_var :: Maybe t,
741 index_first :: Maybe t,
742 index_next :: Maybe t,
743 dsl_lambda :: Maybe t,
744 dsl_let :: Maybe t
745 }
746 deriving (Show, Functor)
747{-!
748deriving instance Binary DSL'
749!-}
751type DSL = DSL' PTerm
753data SynContext = PatternSyntax | TermSyntax | AnySyntax
754 deriving Show
755{-!
756deriving instance Binary SynContext
757!-}
759data Syntax = Rule [SSymbol] PTerm SynContext
760 deriving Show
761{-!
762deriving instance Binary Syntax
763!-}
765data SSymbol = Keyword Name
766 | Symbol String
767 | Binding Name
768 | Expr Name
769 | SimpleExpr Name
770 deriving Show
771{-!
772deriving instance Binary SSymbol
773!-}
775initDSL = DSL (PRef f (UN ">>="))
776 (PRef f (UN "return"))
777 (PRef f (UN "<$>"))
778 (PRef f (UN "pure"))
779 Nothing
780 Nothing
781 Nothing
782 Nothing
783 Nothing
784 where f = fileFC "(builtin)"
786data Using = UImplicit Name PTerm
787 | UConstraint Name [Name]
788 deriving (Show, Eq)
789{-!
790deriving instance Binary Using
791!-}
793data SyntaxInfo = Syn { using :: [Using],
794 syn_params :: [(Name, PTerm)],
795 syn_namespace :: [String],
796 no_imp :: [Name],
797 decoration :: Name -> Name,
798 inPattern :: Bool,
799 implicitAllowed :: Bool,
800 dsl_info :: DSL }
801 deriving Show
802{-!
803deriving instance Binary SyntaxInfo
804!-}
806defaultSyntax = Syn [] [] [] [] id False False initDSL
808expandNS :: SyntaxInfo -> Name -> Name
809expandNS syn n@(NS _ _) = n
810expandNS syn n = case syn_namespace syn of
811 [] -> n
812 xs -> NS n xs
815--- Pretty printing declarations and terms
817instance Show PTerm where
818 show tm = showImp Nothing False False tm
820instance Pretty PTerm where
821 pretty = prettyImp False
823instance Show PDecl where
824 show d = showDeclImp False d
826instance Show PClause where
827 show c = showCImp True c
829instance Show PData where
830 show d = showDImp False d
832showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
833showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
834showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
835showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
836showDeclImp _ (PData _ _ _ _ d) = show d
837showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
838 showSep "\n" (map show ps)
841showCImp :: Bool -> PClause -> String
842showCImp impl (PClause _ n l ws r w)
843 = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
844 ++ " where " ++ show w
845 where
846 showWs [] = ""
847 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
848showCImp impl (PWith _ n l ws r w)
849 = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
850 ++ " { " ++ show w ++ " } "
851 where
852 showWs [] = ""
853 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
856showDImp :: Bool -> PData -> String
857showDImp impl (PDatadecl n ty cons)
858 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
859 ++ showSep "\n\t| "
860 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
862getImps :: [PArg] -> [(Name, PTerm)]
863getImps [] = []
864getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
865getImps (_ : xs) = getImps xs
867getExps :: [PArg] -> [PTerm]
868getExps [] = []
869getExps (PExp _ _ tm _ : xs) = tm : getExps xs
870getExps (_ : xs) = getExps xs
872getConsts :: [PArg] -> [PTerm]
873getConsts [] = []
874getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
875getConsts (_ : xs) = getConsts xs
877getAll :: [PArg] -> [PTerm]
878getAll = map getTm
880-- | Pretty-print a high-level Idris term
881prettyImp :: Bool -- ^^ whether to show implicits
882 -> PTerm -- ^^ the term to pretty-print
883 -> Doc
884prettyImp impl = prettySe 10
885 where
886 prettySe p (PQuote r) =
887 if size r > breakingSize then
888 text "![" $$ pretty r <> text "]"
889 else
890 text "![" <> pretty r <> text "]"
891 prettySe p (PPatvar fc n) = pretty n
892 prettySe p (PRef fc n) =
893 if impl then
894 pretty n
895 else
896 prettyBasic n
897 where
898 prettyBasic n@(UN _) = pretty n
899 prettyBasic (MN _ s) = text s
900 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
901 prettyBasic (SN sn) = text (show sn)
902 prettySe p (PLam n ty sc) =
903 bracket p 2 $
904 if size sc > breakingSize then
905 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
906 else
907 text "λ" <> pretty n <+> text "=>" <+> pretty sc
908 prettySe p (PLet n ty v sc) =
909 bracket p 2 $
910 if size sc > breakingSize then
911 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
912 nest nestingSize (prettySe 10 sc)
913 else
914 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
915 prettySe 10 sc
916 prettySe p (PPi (Exp l s _) n ty sc)
917 | n `elem` allNamesIn sc || impl =
918 let open = if l then text "|" <> lparen else lparen in
919 bracket p 2 $
920 if size sc > breakingSize then
921 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
922 st <+> text "->" $+$ prettySe 10 sc
923 else
924 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
925 st <+> text "->" <+> prettySe 10 sc
926 | otherwise =
927 bracket p 2 $
928 if size sc > breakingSize then
929 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
930 else
931 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
932 where
933 st =
934 case s of
935 Static -> text "[static]"
936 _ -> empty
937 prettySe p (PPi (Imp l s _) n ty sc)
938 | impl =
939 let open = if l then text "|" <> lbrace else lbrace in
940 bracket p 2 $
941 if size sc > breakingSize then
942 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
943 st <+> text "->" <+> prettySe 10 sc
944 else
945 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
946 st <+> text "->" <+> prettySe 10 sc
947 | otherwise = prettySe 10 sc
948 where
949 st =
950 case s of
951 Static -> text $ "[static]"
952 _ -> empty
953 prettySe p (PPi (Constraint _ _ _) n ty sc) =
954 bracket p 2 $
955 if size sc > breakingSize then
956 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
957 else
958 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
959 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
960 bracket p 2 $
961 if size sc > breakingSize then
962 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
963 rbrace <+> text "->" $+$ prettySe 10 sc
964 else
965 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
966 rbrace <+> text "->" <+> prettySe 10 sc
967 prettySe p (PApp _ (PRef _ f) [])
968 | not impl = pretty f
969 prettySe p (PAppBind _ (PRef _ f) [])
970 | not impl = text "!" <+> pretty f
971 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
972 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
973 let [l, r] = getExps args in
974 bracket p 1 $
975 if size r > breakingSize then
976 prettySe 1 l <+> pretty op $+$ prettySe 0 r
977 else
978 prettySe 1 l <+> pretty op <+> prettySe 0 r
979 prettySe p (PApp _ f as) =
980 let args = getExps as in
981 bracket p 1 $
982 prettySe 1 f <+>
983 if impl then
984 foldl' fS empty as
985 -- foldr (<+>) empty $ map prettyArgS as
986 else
987 foldl' fSe empty args
988 -- foldr (<+>) empty $ map prettyArgSe args
989 where
990 fS l r =
991 if size r > breakingSize then
992 l $+$ nest nestingSize (prettyArgS r)
993 else
994 l <+> prettyArgS r
996 fSe l r =
997 if size r > breakingSize then
998 l $+$ nest nestingSize (prettyArgSe r)
999 else
1000 l <+> prettyArgSe r
1001 prettySe p (PCase _ scr opts) =
1002 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
1003 where
1004 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
1006 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
1007 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
1008 prettySe p (PRefl _ _) = text "refl"
1009 prettySe p (PResolveTC _) = text "resolvetc"
1010 prettySe p (PTrue _) = text "()"
1011 prettySe p (PFalse _) = text "_|_"
1012 prettySe p (PEq _ l r) =
1013 bracket p 2 $
1014 if size r > breakingSize then
1015 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
1016 else
1017 prettySe 10 l <+> text "=" <+> prettySe 10 r
1018 prettySe p (PRewrite _ l r _) =
1019 bracket p 2 $
1020 if size r > breakingSize then
1021 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
1022 else
1023 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1024 prettySe p (PTyped l r) =
1025 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1026 prettySe p (PPair _ l r) =
1027 if size r > breakingSize then
1028 lparen <> prettySe 10 l <> text "," $+$
1029 prettySe 10 r <> rparen
1030 else
1031 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
1032 prettySe p (PDPair _ l t r) =
1033 if size r > breakingSize then
1034 lparen <> prettySe 10 l <+> text "**" $+$
1035 prettySe 10 r <> rparen
1036 else
1037 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1038 prettySe p (PAlternative a as) =
1039 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1040 where
1041 prettyAs =
1042 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1043 prettySe p PType = text "Type"
1044 prettySe p (PConstant c) = pretty c
1045 -- XXX: add pretty for tactics
1046 prettySe p (PProof ts) =
1047 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1048 prettySe p (PTactics ts) =
1049 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1050 prettySe p (PMetavar n) = text "?" <> pretty n
1051 prettySe p (PReturn f) = text "return"
1052 prettySe p PImpossible = text "impossible"
1053 prettySe p Placeholder = text "_"
1054 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1055 prettySe p (PElabError s) = pretty s
1057 prettySe p _ = text "test"
1059 prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1060 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1061 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1062 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1064 prettyArgSe arg = prettySe 0 arg
1065 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1066 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1067 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1069 bracket outer inner doc
1070 | inner > outer = lparen <> doc <> rparen
1071 | otherwise = doc
1073-- | Show Idris name
1074showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1075 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1076 -> Bool -- ^^ whether to show implicits
1077 -> Bool -- ^^ whether to colourise
1078 -> Name -- ^^ the term to show
1079 -> String
1080showName ist bnd impl colour n = case ist of
1081 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1082 Nothing -> showbasic n
1083 where name = if impl then show n else showbasic n
1084 showbasic n@(UN _) = show n
1085 showbasic (MN _ s) = s
1086 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1087 showbasic (SN s) = show s
1088 fst3 (x, _, _) = x
1089 colourise n t = let ctxt' = fmap tt_ctxt ist in
1090 case ctxt' of
1091 Nothing -> name
1092 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1093 else colouriseBound t name
1094 | isDConName n ctxt -> colouriseData t name
1095 | isFnName n ctxt -> colouriseFun t name
1096 | isTConName n ctxt -> colouriseType t name
1097 -- The assumption is that if a name is not bound and does not exist in the
1098 -- global context, then we're somewhere in which implicit info has been lost
1099 -- (like error messages). Thus, unknown vars are colourised as implicits.
1100 | otherwise -> colouriseImplicit t name
1102-- | Show Idris term
1103showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1104 -> Bool -- ^^ whether to show implicits
1105 -> Bool -- ^^ whether to colourise
1106 -> PTerm -- ^^ the term to show
1107 -> String
1108showImp ist impl colour tm = se 10 [] tm where
1109 perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1110 perhapsColourise col str = case ist of
1111 Just i -> if colour then col (idris_colourTheme i) str else str
1112 Nothing -> str
1114 se :: Int -> [(Name, Bool)] -> PTerm -> String
1115 se p bnd (PQuote r) = "![" ++ show r ++ "]"
1116 se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1117 se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1118 se p bnd e
1119 | Just str <- slist p bnd e = str
1120 | Just num <- snat p e = perhapsColourise colouriseData (show num)
1121 se p bnd (PRef fc n) = showName ist bnd impl colour n
1122 se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1123 (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1124 ++ se 10 ((n, False):bnd) sc
1125 se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1126 " = " ++ se 10 bnd v ++
1127 " in " ++ se 10 ((n, False):bnd) sc
1128 se p bnd (PPi (Exp l s _) n ty sc)
1129 | n `elem` allNamesIn sc || impl
1130 = bracket p 2 $
1131 (if l then "|(" else "(") ++
1132 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1133 ") " ++ st ++
1134 "-> " ++ se 10 ((n, False):bnd) sc
1135 | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1136 where st = case s of
1137 Static -> "[static] "
1138 _ -> ""
1139 se p bnd (PPi (Imp l s _) n ty sc)
1140 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1141 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1142 "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1143 | otherwise = se 10 ((n, True):bnd) sc
1144 where st = case s of
1145 Static -> "[static] "
1146 _ -> ""
1147 se p bnd (PPi (Constraint _ _ _) n ty sc)
1148 = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1149 se p bnd (PPi (TacImp _ _ s _) n ty sc)
1150 = bracket p 2 $
1151 "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1152 se 10 ((n, False):bnd) sc
1153 se p bnd (PMatchApp _ f) = "match " ++ show f
1154 se p bnd (PApp _ hd@(PRef _ f) [])
1155 | not impl = se p bnd hd
1156 se p bnd (PAppBind _ hd@(PRef _ f) [])
1157 | not impl = "!" ++ se p bnd hd
1158 se p bnd (PApp _ op@(PRef _ (UN (f:_))) args)
1159 | length (getExps args) == 2 && not impl && not (isAlpha f)
1160 = let [l, r] = getExps args in
1161 bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1162 se p bnd (PApp _ f as)
1163 = -- let args = getExps as in
1164 bracket p 1 $ se 1 bnd f ++
1165 if impl then concatMap (sArg bnd) as
1166 else concatMap (suiArg impl bnd) as
1167 se p bnd (PAppBind _ f as)
1168 = let args = getExps as in
1169 "!" ++ (bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1170 else concatMap (seArg bnd) args)
1171 se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1172 where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1173 se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1174 se p bnd (PRefl _ t)
1175 | not impl = perhapsColourise colouriseData "refl"
1176 | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1177 se p bnd (PResolveTC _) = "resolvetc"
1178 se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1179 se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1180 se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1181 se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1182 se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1183 se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1184 se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1185 se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1186 se p bnd PType = perhapsColourise colouriseType "Type"
1187 se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1188 where cfun (AType _) = colouriseType
1189 cfun StrType = colouriseType
1190 cfun PtrType = colouriseType
1191 cfun VoidType = colouriseType
1192 cfun _ = colouriseData
1193 se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1194 se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1195 se p bnd (PMetavar n) = "?" ++ show n
1196 se p bnd (PReturn f) = "return"
1197 se p bnd PImpossible = "impossible"
1198 se p bnd Placeholder = "_"
1199 se p bnd (PDoBlock _) = "do block show not implemented"
1200 se p bnd (PElabError s) = show s
1201 se p bnd (PCoerced t) = se p bnd t
1202 se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1203 se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1204-- se p bnd x = "Not implemented"
1206 slist' p bnd (PApp _ (PRef _ nil) _)
1207 | nsroot nil == UN "Nil" = Just []
1208 slist' p bnd (PApp _ (PRef _ cons) args)
1209 | nsroot cons == UN "::",
1210 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1211 all isImp imps,
1212 Just tl' <- slist' p bnd tl
1213 = Just (hd:tl')
1214 where
1215 isImp (PImp {}) = True
1216 isImp _ = False
1217 slist' _ _ _ = Nothing
1219 slist p bnd e | Just es <- slist' p bnd e = Just $
1220 case es of [] -> "[]"
1221 [x] -> "[" ++ se p bnd x ++ "]"
1222 xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1223 slist _ _ _ = Nothing
1225 -- since Prelude is always imported, S & Z are unqualified iff they're the
1226 -- Nat ones.
1227 snat p (PRef _ o)
1228 | show o == (natns++"Z") || show o == "Z" = Just 0
1229 snat p (PApp _ s [PExp {getTm=n}])
1230 | show s == (natns++"S") || show s == "S",
1231 Just n' <- snat p n
1232 = Just $ 1 + n'
1233 snat _ _ = Nothing
1235 natns = "Prelude.Nat."
1237 sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1238 sArg bnd (PExp _ _ tm _) = seArg bnd tm
1239 sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1240 sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1242 -- show argument, implicits given by the user also shown
1243 suiArg impl bnd (PImp _ mi _ n tm _)
1244 | impl || not mi = siArg bnd (n, tm)
1245 suiArg impl bnd (PExp _ _ tm _) = seArg bnd tm
1246 suiArg impl bnd _ = ""
1248 seArg bnd arg = " " ++ se 0 bnd arg
1249 siArg bnd (n, val) =
1250 let n' = show n
1251 val' = se 10 bnd val in
1252 if (n' == val')
1253 then " {" ++ n' ++ "}"
1254 else " {" ++ n' ++ " = " ++ val' ++ "}"
1255 scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1256 stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1258 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1259 | otherwise = str
1262instance Sized PTerm where
1263 size (PQuote rawTerm) = size rawTerm
1264 size (PRef fc name) = size name
1265 size (PLam name ty bdy) = 1 + size ty + size bdy
1266 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1267 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1268 size (PTyped trm ty) = 1 + size trm + size ty
1269 size (PApp fc name args) = 1 + size args
1270 size (PAppBind fc name args) = 1 + size args
1271 size (PCase fc trm bdy) = 1 + size trm + size bdy
1272 size (PTrue fc) = 1
1273 size (PFalse fc) = 1
1274 size (PRefl fc _) = 1
1275 size (PResolveTC fc) = 1
1276 size (PEq fc left right) = 1 + size left + size right
1277 size (PRewrite fc left right _) = 1 + size left + size right
1278 size (PPair fc left right) = 1 + size left + size right
1279 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1280 size (PAlternative a alts) = 1 + size alts
1281 size (PHidden hidden) = size hidden
1282 size (PUnifyLog tm) = size tm
1283 size (PNoImplicits tm) = size tm
1284 size PType = 1
1285 size (PConstant const) = 1 + size const
1286 size Placeholder = 1
1287 size (PDoBlock dos) = 1 + size dos
1288 size (PIdiom fc term) = 1 + size term
1289 size (PReturn fc) = 1
1290 size (PMetavar name) = 1
1291 size (PProof tactics) = size tactics
1292 size (PElabError err) = size err
1293 size PImpossible = 1
1295getPArity :: PTerm -> Int
1296getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1297getPArity _ = 0
1299-- Return all names, free or globally bound, in the given term.
1301allNamesIn :: PTerm -> [Name]
1302allNamesIn tm = nub $ ni [] tm
1303 where
1304 ni env (PRef _ n)
1305 | not (n `elem` env) = [n]
1306 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1307 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1308 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1309 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1310 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1311 ni env (PHidden tm) = ni env tm
1312 ni env (PEq _ l r) = ni env l ++ ni env r
1313 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1314 ni env (PTyped l r) = ni env l ++ ni env r
1315 ni env (PPair _ l r) = ni env l ++ ni env r
1316 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1317 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1318 ni env (PAlternative a ls) = concatMap (ni env) ls
1319 ni env (PUnifyLog tm) = ni env tm
1320 ni env (PNoImplicits tm) = ni env tm
1321 ni env _ = []
1323-- Return names which are free in the given term.
1325namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1326namesIn uvars ist tm = nub $ ni [] tm
1327 where
1328 ni env (PRef _ n)
1329 | not (n `elem` env)
1330 = case lookupTy n (tt_ctxt ist) of
1331 [] -> [n]
1332 _ -> if n `elem` (map fst uvars) then [n] else []
1333 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1334 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1335 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1336 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1337 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1338 ni env (PEq _ l r) = ni env l ++ ni env r
1339 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1340 ni env (PTyped l r) = ni env l ++ ni env r
1341 ni env (PPair _ l r) = ni env l ++ ni env r
1342 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1343 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1344 ni env (PAlternative a as) = concatMap (ni env) as
1345 ni env (PHidden tm) = ni env tm
1346 ni env (PUnifyLog tm) = ni env tm
1347 ni env (PNoImplicits tm) = ni env tm
1348 ni env _ = []
1350-- Return which of the given names are used in the given term.
1352usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1353usedNamesIn vars ist tm = nub $ ni [] tm
1354 where
1355 ni env (PRef _ n)
1356 | n `elem` vars && not (n `elem` env)
1357 = case lookupTy n (tt_ctxt ist) of
1358 [] -> [n]
1359 _ -> []
1360 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1361 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1362 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1363 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1364 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1365 ni env (PEq _ l r) = ni env l ++ ni env r
1366 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1367 ni env (PTyped l r) = ni env l ++ ni env r
1368 ni env (PPair _ l r) = ni env l ++ ni env r
1369 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1370 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1371 ni env (PAlternative a as) = concatMap (ni env) as
1372 ni env (PHidden tm) = ni env tm
1373 ni env (PUnifyLog tm) = ni env tm
1374 ni env (PNoImplicits tm) = ni env tm
1375 ni env _ = []