on 46256ab help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1385 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
20import System.IO
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 idris_outh :: Handle
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 | IBCLineApp FilePath Int PTerm
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 stdout
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 | CaseSplitAt Bool Int Name
244 | AddClauseFrom Bool Int Name
245 | SetOpt Opt
246 | UnsetOpt Opt
247 | NOP
248 | SetColour ColourType IdrisColour
249 | ColourOn
250 | ColourOff
252data Opt = Filename String
253 | Ver
254 | Usage
255 | Quiet
256 | ColourREPL Bool
257 | Ideslave
258 | ShowLibs
259 | ShowLibdir
260 | ShowIncs
261 | NoPrelude
262 | NoREPL
263 | OLogging Int
264 | Output String
265 | TypeCase
266 | TypeInType
267 | DefaultTotal
268 | DefaultPartial
269 | WarnPartial
270 | NoCoverage
271 | ErrContext
272 | ShowImpl
273 | Verbose
274 | IBCSubDir String
275 | ImportDir String
276 | PkgBuild String
277 | PkgInstall String
278 | PkgClean String
279 | WarnOnly
280 | Pkg String
281 | BCAsm String
282 | DumpDefun String
283 | DumpCases String
284 | FOVM String
285 | UseCodegen Codegen
286 | OutputTy OutputType
287 | Extension LanguageExt
288 | InterpretScript String
289 | TargetTriple String
290 | TargetCPU String
291 | OptLevel Word
292 | Client String
293 deriving (Show, Eq)
295-- Parsed declarations
297data Fixity = Infixl { prec :: Int }
298 | Infixr { prec :: Int }
299 | InfixN { prec :: Int }
300 | PrefixN { prec :: Int }
301 deriving Eq
302{-!
303deriving instance Binary Fixity
304!-}
306instance Show Fixity where
307 show (Infixl i) = "infixl " ++ show i
308 show (Infixr i) = "infixr " ++ show i
309 show (InfixN i) = "infix " ++ show i
310 show (PrefixN i) = "prefix " ++ show i
312data FixDecl = Fix Fixity String
313 deriving Eq
315instance Show FixDecl where
316 show (Fix f s) = show f ++ " " ++ s
318{-!
319deriving instance Binary FixDecl
320!-}
322instance Ord FixDecl where
323 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
326data Static = Static | Dynamic
327 deriving (Show, Eq)
328{-!
329deriving instance Binary Static
330!-}
332-- Mark bindings with their explicitness, and laziness
333data Plicity = Imp { plazy :: Bool,
334 pstatic :: Static,
335 pdocstr :: String,
336 pparam :: Bool }
337 | Exp { plazy :: Bool,
338 pstatic :: Static,
339 pdocstr :: String,
340 pparam :: Bool }
341 | Constraint { plazy :: Bool,
342 pstatic :: Static,
343 pdocstr :: String }
344 | TacImp { plazy :: Bool,
345 pstatic :: Static,
346 pscript :: PTerm,
347 pdocstr :: String }
348 deriving (Show, Eq)
350{-!
351deriving instance Binary Plicity
352!-}
354impl = Imp False Dynamic "" False
355expl = Exp False Dynamic "" False
356expl_param = Exp False Dynamic "" True
357constraint = Constraint False Dynamic ""
358tacimpl t = TacImp False Dynamic t ""
360data FnOpt = Inlinable -- always evaluate when simplifying
361 | TotalFn | PartialFn
362 | Coinductive | AssertTotal
363 | Dictionary -- type class dictionary, eval only when
364 -- a function argument, and further evaluation resutls
365 | Implicit -- implicit coercion
366 | CExport String -- export, with a C name
367 | Reflection -- a reflecting function, compile-time only
368 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
369 deriving (Show, Eq)
370{-!
371deriving instance Binary FnOpt
372!-}
374type FnOpts = [FnOpt]
376inlinable :: FnOpts -> Bool
377inlinable = elem Inlinable
379dictionary :: FnOpts -> Bool
380dictionary = elem Dictionary
382-- | Top-level declarations such as compiler directives, definitions,
383-- datatypes and typeclasses.
384data PDecl' t
385 = PFix FC Fixity [String] -- ^ Fixity declaration
386 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
387 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
388 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
389 | PCAF FC Name t -- ^ Top level constant
390 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
391 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
392 | PNamespace String [PDecl' t] -- ^ New namespace
393 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
394 | PClass String SyntaxInfo FC
395 [t] -- constraints
396 Name
397 [(Name, t)] -- parameters
398 [PDecl' t] -- declarations
399 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
400 -- class name, parameters, method declarations
401 | PInstance SyntaxInfo FC [t] -- constraints
402 Name -- class
403 [t] -- parameters
404 t -- full instance type
405 (Maybe Name) -- explicit name
406 [PDecl' t]
407 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
408 -- class name, parameters, full instance type, optional explicit name, and definitions
409 | PDSL Name (DSL' t) -- ^ DSL declaration
410 | PSyntax FC Syntax -- ^ Syntax definition
411 | PMutual FC [PDecl' t] -- ^ Mutual block
412 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
413 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
414 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
415 -- bool is True, lhs and rhs must be convertible
416 deriving Functor
417{-!
418deriving instance Binary PDecl'
419!-}
421-- | One clause of a top-level definition. Term arguments to constructors are:
422--
423-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
424--
425-- 2. The list of extra 'with' patterns
426--
427-- 3. The right-hand side
428--
429-- 4. The where block (PDecl' t)
431data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
432 | PWith FC Name t [t] t [PDecl' t]
433 | PClauseR FC [t] t [PDecl' t]
434 | PWithR FC [t] t [PDecl' t]
435 deriving Functor
436{-!
437deriving instance Binary PClause'
438!-}
440-- | Data declaration
441data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
442 d_tcon :: t, -- ^ Type constructor
443 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
444 }
445 -- ^ Data declaration
446 | PLaterdecl { d_name :: Name, d_tcon :: t }
447 -- ^ "Placeholder" for data whose constructors are defined later
448 deriving Functor
449{-!
450deriving instance Binary PData'
451!-}
453-- Handy to get a free function for applying PTerm -> PTerm functions
454-- across a program, by deriving Functor
456type PDecl = PDecl' PTerm
457type PData = PData' PTerm
458type PClause = PClause' PTerm
460-- get all the names declared in a decl
462declared :: PDecl -> [Name]
463declared (PFix _ _ _) = []
464declared (PTy _ _ _ _ n t) = [n]
465declared (PPostulate _ _ _ _ n t) = [n]
466declared (PClauses _ _ n _) = [] -- not a declaration
467declared (PCAF _ n _) = [n]
468declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
469 where fstt (_, a, _, _) = a
470declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
471declared (PParams _ _ ds) = concatMap declared ds
472declared (PNamespace _ ds) = concatMap declared ds
473declared (PRecord _ _ _ n _ _ c _) = [n, c]
474declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
475declared (PInstance _ _ _ _ _ _ _ _) = []
476declared (PDSL n _) = [n]
477declared (PSyntax _ _) = []
478declared (PMutual _ ds) = concatMap declared ds
479declared (PDirective _) = []
481-- get the names declared, not counting nested parameter blocks
482tldeclared :: PDecl -> [Name]
483tldeclared (PFix _ _ _) = []
484tldeclared (PTy _ _ _ _ n t) = [n]
485tldeclared (PPostulate _ _ _ _ n t) = [n]
486tldeclared (PClauses _ _ n _) = [] -- not a declaration
487tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
488tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
489 where fstt (_, a, _, _) = a
490tldeclared (PParams _ _ ds) = []
491tldeclared (PMutual _ ds) = concatMap tldeclared ds
492tldeclared (PNamespace _ ds) = concatMap tldeclared ds
493tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
494tldeclared (PInstance _ _ _ _ _ _ _ _) = []
495tldeclared _ = []
497defined :: PDecl -> [Name]
498defined (PFix _ _ _) = []
499defined (PTy _ _ _ _ n t) = []
500defined (PPostulate _ _ _ _ n t) = []
501defined (PClauses _ _ n _) = [n] -- not a declaration
502defined (PCAF _ n _) = [n]
503defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
504 where fstt (_, a, _, _) = a
505defined (PData _ _ _ _ (PLaterdecl n _)) = []
506defined (PParams _ _ ds) = concatMap defined ds
507defined (PNamespace _ ds) = concatMap defined ds
508defined (PRecord _ _ _ n _ _ c _) = [n, c]
509defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
510defined (PInstance _ _ _ _ _ _ _ _) = []
511defined (PDSL n _) = [n]
512defined (PSyntax _ _) = []
513defined (PMutual _ ds) = concatMap defined ds
514defined (PDirective _) = []
515--defined _ = []
517updateN :: [(Name, Name)] -> Name -> Name
518updateN ns n | Just n' <- lookup n ns = n'
519updateN _ n = n
521updateNs :: [(Name, Name)] -> PTerm -> PTerm
522updateNs [] t = t
523updateNs ns t = mapPT updateRef t
524 where updateRef (PRef fc f) = PRef fc (updateN ns f)
525 updateRef t = t
527-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
528-- updateDNs [] t = t
529-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
530-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
531-- where updateCNs ns (PClause n l ts r ds)
532-- = PClause (updateN ns n) (fmap (updateNs ns) l)
533-- (map (fmap (updateNs ns)) ts)
534-- (fmap (updateNs ns) r)
535-- (map (updateDNs ns) ds)
536-- updateDNs ns c = c
538-- | High level language terms
539data PTerm = PQuote Raw
540 | PRef FC Name
541 | PInferRef FC Name -- ^ A name to be defined later
542 | PPatvar FC Name
543 | PLam Name PTerm PTerm
544 | PPi Plicity Name PTerm PTerm
545 | PLet Name PTerm PTerm PTerm
546 | PTyped PTerm PTerm -- ^ Term with explicit type
547 | PApp FC PTerm [PArg]
548 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
549 | PMatchApp FC Name -- ^ Make an application by type matching
550 | PCase FC PTerm [(PTerm, PTerm)]
551 | PTrue FC
552 | PFalse FC
553 | PRefl FC PTerm
554 | PResolveTC FC
555 | PEq FC PTerm PTerm
556 | PRewrite FC PTerm PTerm (Maybe PTerm)
557 | PPair FC PTerm PTerm
558 | PDPair FC PTerm PTerm PTerm
559 | PAlternative Bool [PTerm] -- True if only one may work
560 | PHidden PTerm -- ^ Irrelevant or hidden pattern
561 | PType
562 | PGoal FC PTerm Name PTerm
563 | PConstant Const
564 | Placeholder
565 | PDoBlock [PDo]
566 | PIdiom FC PTerm
567 | PReturn FC
568 | PMetavar Name
569 | PProof [PTactic] -- ^ Proof script
570 | PTactics [PTactic] -- ^ As PProof, but no auto solving
571 | PElabError Err -- ^ Error to report on elaboration
572 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
573 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
574 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
575 | PNoImplicits PTerm -- ^ never run implicit converions on the term
576 deriving Eq
579{-!
580deriving instance Binary PTerm
581!-}
583mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
584mapPT f t = f (mpt t) where
585 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
586 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
587 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
588 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
589 (fmap (mapPT f) g)
590 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
591 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
592 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
593 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
594 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
595 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
596 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
597 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
598 mpt (PHidden t) = PHidden (mapPT f t)
599 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
600 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
601 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
602 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
603 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
604 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
605 mpt x = x
608data PTactic' t = Intro [Name] | Intros | Focus Name
609 | Refine Name [Bool] | Rewrite t
610 | Equiv t
611 | MatchRefine Name
612 | LetTac Name t | LetTacTy Name t t
613 | Exact t | Compute | Trivial
614 | Solve
615 | Attack
616 | ProofState | ProofTerm | Undo
617 | Try (PTactic' t) (PTactic' t)
618 | TSeq (PTactic' t) (PTactic' t)
619 | ApplyTactic t -- see Language.Reflection module
620 | Reflect t
621 | Fill t
622 | GoalType String (PTactic' t)
623 | Qed | Abandon
624 deriving (Show, Eq, Functor)
625{-!
626deriving instance Binary PTactic'
627!-}
629instance Sized a => Sized (PTactic' a) where
630 size (Intro nms) = 1 + size nms
631 size Intros = 1
632 size (Focus nm) = 1 + size nm
633 size (Refine nm bs) = 1 + size nm + length bs
634 size (Rewrite t) = 1 + size t
635 size (LetTac nm t) = 1 + size nm + size t
636 size (Exact t) = 1 + size t
637 size Compute = 1
638 size Trivial = 1
639 size Solve = 1
640 size Attack = 1
641 size ProofState = 1
642 size ProofTerm = 1
643 size Undo = 1
644 size (Try l r) = 1 + size l + size r
645 size (TSeq l r) = 1 + size l + size r
646 size (ApplyTactic t) = 1 + size t
647 size (Reflect t) = 1 + size t
648 size (Fill t) = 1 + size t
649 size Qed = 1
650 size Abandon = 1
652type PTactic = PTactic' PTerm
654data PDo' t = DoExp FC t
655 | DoBind FC Name t
656 | DoBindP FC t t
657 | DoLet FC Name t t
658 | DoLetP FC t t
659 deriving (Eq, Functor)
660{-!
661deriving instance Binary PDo'
662!-}
664instance Sized a => Sized (PDo' a) where
665 size (DoExp fc t) = 1 + size fc + size t
666 size (DoBind fc nm t) = 1 + size fc + size nm + size t
667 size (DoBindP fc l r) = 1 + size fc + size l + size r
668 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
669 size (DoLetP fc l r) = 1 + size fc + size l + size r
671type PDo = PDo' PTerm
673-- The priority gives a hint as to elaboration order. Best to elaborate
674-- things early which will help give a more concrete type to other
675-- variables, e.g. a before (interpTy a).
677data PArg' t = PImp { priority :: Int,
678 machine_inf :: Bool, -- true if the machine inferred it
679 lazyarg :: Bool, pname :: Name, getTm :: t,
680 pargdoc :: String }
681 | PExp { priority :: Int,
682 lazyarg :: Bool, getTm :: t,
683 pargdoc :: String }
684 | PConstraint { priority :: Int,
685 lazyarg :: Bool, getTm :: t,
686 pargdoc :: String }
687 | PTacImplicit { priority :: Int,
688 lazyarg :: Bool, pname :: Name,
689 getScript :: t,
690 getTm :: t,
691 pargdoc :: String }
692 deriving (Show, Eq, Functor)
694instance Sized a => Sized (PArg' a) where
695 size (PImp p _ l nm trm _) = 1 + size nm + size trm
696 size (PExp p l trm _) = 1 + size trm
697 size (PConstraint p l trm _) = 1 + size trm
698 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
700{-!
701deriving instance Binary PArg'
702!-}
704pimp n t mach = PImp 1 mach True n t ""
705pexp t = PExp 1 False t ""
706pconst t = PConstraint 1 False t ""
707ptacimp n s t = PTacImplicit 0 True n s t ""
709type PArg = PArg' PTerm
711-- Type class data
713data ClassInfo = CI { instanceName :: Name,
714 class_methods :: [(Name, (FnOpts, PTerm))],
715 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
716 class_params :: [Name],
717 class_instances :: [Name] }
718 deriving Show
719{-!
720deriving instance Binary ClassInfo
721!-}
723data OptInfo = Optimise { collapsible :: Bool,
724 isnewtype :: Bool,
725 forceable :: [Int], -- argument positions
726 recursive :: [Int] }
727 deriving Show
728{-!
729deriving instance Binary OptInfo
730!-}
733data TypeInfo = TI { con_names :: [Name],
734 codata :: Bool,
735 param_pos :: [Int] }
736 deriving Show
737{-!
738deriving instance Binary TypeInfo
739!-}
741-- Syntactic sugar info
743data DSL' t = DSL { dsl_bind :: t,
744 dsl_return :: t,
745 dsl_apply :: t,
746 dsl_pure :: t,
747 dsl_var :: Maybe t,
748 index_first :: Maybe t,
749 index_next :: Maybe t,
750 dsl_lambda :: Maybe t,
751 dsl_let :: Maybe t
752 }
753 deriving (Show, Functor)
754{-!
755deriving instance Binary DSL'
756!-}
758type DSL = DSL' PTerm
760data SynContext = PatternSyntax | TermSyntax | AnySyntax
761 deriving Show
762{-!
763deriving instance Binary SynContext
764!-}
766data Syntax = Rule [SSymbol] PTerm SynContext
767 deriving Show
768{-!
769deriving instance Binary Syntax
770!-}
772data SSymbol = Keyword Name
773 | Symbol String
774 | Binding Name
775 | Expr Name
776 | SimpleExpr Name
777 deriving Show
778{-!
779deriving instance Binary SSymbol
780!-}
782initDSL = DSL (PRef f (UN ">>="))
783 (PRef f (UN "return"))
784 (PRef f (UN "<$>"))
785 (PRef f (UN "pure"))
786 Nothing
787 Nothing
788 Nothing
789 Nothing
790 Nothing
791 where f = fileFC "(builtin)"
793data Using = UImplicit Name PTerm
794 | UConstraint Name [Name]
795 deriving (Show, Eq)
796{-!
797deriving instance Binary Using
798!-}
800data SyntaxInfo = Syn { using :: [Using],
801 syn_params :: [(Name, PTerm)],
802 syn_namespace :: [String],
803 no_imp :: [Name],
804 decoration :: Name -> Name,
805 inPattern :: Bool,
806 implicitAllowed :: Bool,
807 dsl_info :: DSL }
808 deriving Show
809{-!
810deriving instance Binary SyntaxInfo
811!-}
813defaultSyntax = Syn [] [] [] [] id False False initDSL
815expandNS :: SyntaxInfo -> Name -> Name
816expandNS syn n@(NS _ _) = n
817expandNS syn n = case syn_namespace syn of
818 [] -> n
819 xs -> NS n xs
822--- Pretty printing declarations and terms
824instance Show PTerm where
825 show tm = showImp Nothing False False tm
827instance Pretty PTerm where
828 pretty = prettyImp False
830instance Show PDecl where
831 show d = showDeclImp False d
833instance Show PClause where
834 show c = showCImp True c
836instance Show PData where
837 show d = showDImp False d
839showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
840showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
841showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
842showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
843showDeclImp _ (PData _ _ _ _ d) = show d
844showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
845 showSep "\n" (map show ps)
848showCImp :: Bool -> PClause -> String
849showCImp impl (PClause _ n l ws r w)
850 = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
851 ++ " where " ++ show w
852 where
853 showWs [] = ""
854 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
855showCImp impl (PWith _ n l ws r w)
856 = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
857 ++ " { " ++ show w ++ " } "
858 where
859 showWs [] = ""
860 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
863showDImp :: Bool -> PData -> String
864showDImp impl (PDatadecl n ty cons)
865 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
866 ++ showSep "\n\t| "
867 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
869getImps :: [PArg] -> [(Name, PTerm)]
870getImps [] = []
871getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
872getImps (_ : xs) = getImps xs
874getExps :: [PArg] -> [PTerm]
875getExps [] = []
876getExps (PExp _ _ tm _ : xs) = tm : getExps xs
877getExps (_ : xs) = getExps xs
879getConsts :: [PArg] -> [PTerm]
880getConsts [] = []
881getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
882getConsts (_ : xs) = getConsts xs
884getAll :: [PArg] -> [PTerm]
885getAll = map getTm
887-- | Pretty-print a high-level Idris term
888prettyImp :: Bool -- ^^ whether to show implicits
889 -> PTerm -- ^^ the term to pretty-print
890 -> Doc
891prettyImp impl = prettySe 10
892 where
893 prettySe p (PQuote r) =
894 if size r > breakingSize then
895 text "![" $$ pretty r <> text "]"
896 else
897 text "![" <> pretty r <> text "]"
898 prettySe p (PPatvar fc n) = pretty n
899 prettySe p (PRef fc n) =
900 if impl then
901 pretty n
902 else
903 prettyBasic n
904 where
905 prettyBasic n@(UN _) = pretty n
906 prettyBasic (MN _ s) = text s
907 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
908 prettyBasic (SN sn) = text (show sn)
909 prettySe p (PLam n ty sc) =
910 bracket p 2 $
911 if size sc > breakingSize then
912 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
913 else
914 text "λ" <> pretty n <+> text "=>" <+> pretty sc
915 prettySe p (PLet n ty v sc) =
916 bracket p 2 $
917 if size sc > breakingSize then
918 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
919 nest nestingSize (prettySe 10 sc)
920 else
921 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
922 prettySe 10 sc
923 prettySe p (PPi (Exp l s _ _) n ty sc)
924 | n `elem` allNamesIn sc || impl =
925 let open = if l then text "|" <> lparen else lparen in
926 bracket p 2 $
927 if size sc > breakingSize then
928 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
929 st <+> text "->" $+$ prettySe 10 sc
930 else
931 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
932 st <+> text "->" <+> prettySe 10 sc
933 | otherwise =
934 bracket p 2 $
935 if size sc > breakingSize then
936 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
937 else
938 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
939 where
940 st =
941 case s of
942 Static -> text "[static]"
943 _ -> empty
944 prettySe p (PPi (Imp l s _ _) n ty sc)
945 | impl =
946 let open = if l then text "|" <> lbrace else lbrace in
947 bracket p 2 $
948 if size sc > breakingSize then
949 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
950 st <+> text "->" <+> prettySe 10 sc
951 else
952 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
953 st <+> text "->" <+> prettySe 10 sc
954 | otherwise = prettySe 10 sc
955 where
956 st =
957 case s of
958 Static -> text $ "[static]"
959 _ -> empty
960 prettySe p (PPi (Constraint _ _ _) n ty sc) =
961 bracket p 2 $
962 if size sc > breakingSize then
963 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
964 else
965 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
966 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
967 bracket p 2 $
968 if size sc > breakingSize then
969 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
970 rbrace <+> text "->" $+$ prettySe 10 sc
971 else
972 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
973 rbrace <+> text "->" <+> prettySe 10 sc
974 prettySe p (PApp _ (PRef _ f) [])
975 | not impl = pretty f
976 prettySe p (PAppBind _ (PRef _ f) [])
977 | not impl = text "!" <+> pretty f
978 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
979 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
980 let [l, r] = getExps args in
981 bracket p 1 $
982 if size r > breakingSize then
983 prettySe 1 l <+> pretty op $+$ prettySe 0 r
984 else
985 prettySe 1 l <+> pretty op <+> prettySe 0 r
986 prettySe p (PApp _ f as) =
987 let args = getExps as in
988 bracket p 1 $
989 prettySe 1 f <+>
990 if impl then
991 foldl' fS empty as
992 -- foldr (<+>) empty $ map prettyArgS as
993 else
994 foldl' fSe empty args
995 -- foldr (<+>) empty $ map prettyArgSe args
996 where
997 fS l r =
998 if size r > breakingSize then
999 l $+$ nest nestingSize (prettyArgS r)
1000 else
1001 l <+> prettyArgS r
1003 fSe l r =
1004 if size r > breakingSize then
1005 l $+$ nest nestingSize (prettyArgSe r)
1006 else
1007 l <+> prettyArgSe r
1008 prettySe p (PCase _ scr opts) =
1009 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
1010 where
1011 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
1013 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
1014 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
1015 prettySe p (PRefl _ _) = text "refl"
1016 prettySe p (PResolveTC _) = text "resolvetc"
1017 prettySe p (PTrue _) = text "()"
1018 prettySe p (PFalse _) = text "_|_"
1019 prettySe p (PEq _ l r) =
1020 bracket p 2 $
1021 if size r > breakingSize then
1022 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
1023 else
1024 prettySe 10 l <+> text "=" <+> prettySe 10 r
1025 prettySe p (PRewrite _ l r _) =
1026 bracket p 2 $
1027 if size r > breakingSize then
1028 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
1029 else
1030 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1031 prettySe p (PTyped l r) =
1032 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1033 prettySe p (PPair _ l 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 (PDPair _ l t r) =
1040 if size r > breakingSize then
1041 lparen <> prettySe 10 l <+> text "**" $+$
1042 prettySe 10 r <> rparen
1043 else
1044 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1045 prettySe p (PAlternative a as) =
1046 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1047 where
1048 prettyAs =
1049 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1050 prettySe p PType = text "Type"
1051 prettySe p (PConstant c) = pretty c
1052 -- XXX: add pretty for tactics
1053 prettySe p (PProof ts) =
1054 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1055 prettySe p (PTactics ts) =
1056 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1057 prettySe p (PMetavar n) = text "?" <> pretty n
1058 prettySe p (PReturn f) = text "return"
1059 prettySe p PImpossible = text "impossible"
1060 prettySe p Placeholder = text "_"
1061 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1062 prettySe p (PElabError s) = pretty s
1064 prettySe p _ = text "test"
1066 prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1067 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1068 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1069 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1071 prettyArgSe arg = prettySe 0 arg
1072 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1073 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1074 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1076 bracket outer inner doc
1077 | inner > outer = lparen <> doc <> rparen
1078 | otherwise = doc
1080-- | Show Idris name
1081showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1082 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1083 -> Bool -- ^^ whether to show implicits
1084 -> Bool -- ^^ whether to colourise
1085 -> Name -- ^^ the term to show
1086 -> String
1087showName ist bnd impl colour n = case ist of
1088 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1089 Nothing -> showbasic n
1090 where name = if impl then show n else showbasic n
1091 showbasic n@(UN _) = show n
1092 showbasic (MN _ s) = s
1093 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1094 showbasic (SN s) = show s
1095 fst3 (x, _, _) = x
1096 colourise n t = let ctxt' = fmap tt_ctxt ist in
1097 case ctxt' of
1098 Nothing -> name
1099 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1100 else colouriseBound t name
1101 | isDConName n ctxt -> colouriseData t name
1102 | isFnName n ctxt -> colouriseFun t name
1103 | isTConName n ctxt -> colouriseType t name
1104 -- The assumption is that if a name is not bound and does not exist in the
1105 -- global context, then we're somewhere in which implicit info has been lost
1106 -- (like error messages). Thus, unknown vars are colourised as implicits.
1107 | otherwise -> colouriseImplicit t name
1109-- | Show Idris term
1110showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1111 -> Bool -- ^^ whether to show implicits
1112 -> Bool -- ^^ whether to colourise
1113 -> PTerm -- ^^ the term to show
1114 -> String
1115showImp ist impl colour tm = se 10 [] tm where
1116 perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1117 perhapsColourise col str = case ist of
1118 Just i -> if colour then col (idris_colourTheme i) str else str
1119 Nothing -> str
1121 se :: Int -> [(Name, Bool)] -> PTerm -> String
1122 se p bnd (PQuote r) = "![" ++ show r ++ "]"
1123 se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1124 se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1125 se p bnd e
1126 | Just str <- slist p bnd e = str
1127 | Just num <- snat p e = perhapsColourise colouriseData (show num)
1128 se p bnd (PRef fc n) = showName ist bnd impl colour n
1129 se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1130 (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1131 ++ se 10 ((n, False):bnd) sc
1132 se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1133 " = " ++ se 10 bnd v ++
1134 " in " ++ se 10 ((n, False):bnd) sc
1135 se p bnd (PPi (Exp l s _ param) n ty sc)
1136 | n `elem` allNamesIn sc || impl
1137 = bracket p 2 $
1138 (if l then "|(" else "(") ++
1139 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1140 ") " ++
1141 (if (impl && param) then "P" else "") ++
1142 st ++
1143 "-> " ++ se 10 ((n, False):bnd) sc
1144 | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1145 where st = case s of
1146 Static -> "[static] "
1147 _ -> ""
1148 se p bnd (PPi (Imp l s _ _) n ty sc)
1149 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1150 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1151 "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1152 | otherwise = se 10 ((n, True):bnd) sc
1153 where st = case s of
1154 Static -> "[static] "
1155 _ -> ""
1156 se p bnd (PPi (Constraint _ _ _) n ty sc)
1157 = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1158 se p bnd (PPi (TacImp _ _ s _) n ty sc)
1159 = bracket p 2 $
1160 "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1161 se 10 ((n, False):bnd) sc
1162 se p bnd (PMatchApp _ f) = "match " ++ show f
1163 se p bnd (PApp _ hd@(PRef _ f) [])
1164 | not impl = se p bnd hd
1165 se p bnd (PAppBind _ hd@(PRef _ f) [])
1166 | not impl = "!" ++ se p bnd hd
1167 se p bnd (PApp _ op@(PRef _ (UN (f:_))) args)
1168 | length (getExps args) == 2 && not impl && not (isAlpha f)
1169 = let [l, r] = getExps args in
1170 bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1171 se p bnd (PApp _ f as)
1172 = -- let args = getExps as in
1173 bracket p 1 $ se 1 bnd f ++
1174 if impl then concatMap (sArg bnd) as
1175 else concatMap (suiArg impl bnd) as
1176 se p bnd (PAppBind _ f as)
1177 = let args = getExps as in
1178 "!" ++ (bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1179 else concatMap (seArg bnd) args)
1180 se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1181 where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1182 se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1183 se p bnd (PRefl _ t)
1184 | not impl = perhapsColourise colouriseData "refl"
1185 | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1186 se p bnd (PResolveTC _) = "resolvetc"
1187 se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1188 se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1189 se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1190 se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1191 se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1192 se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1193 se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1194 se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1195 se p bnd PType = perhapsColourise colouriseType "Type"
1196 se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1197 where cfun (AType _) = colouriseType
1198 cfun StrType = colouriseType
1199 cfun PtrType = colouriseType
1200 cfun VoidType = colouriseType
1201 cfun _ = colouriseData
1202 se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1203 se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1204 se p bnd (PMetavar n) = "?" ++ show n
1205 se p bnd (PReturn f) = "return"
1206 se p bnd PImpossible = "impossible"
1207 se p bnd Placeholder = "_"
1208 se p bnd (PDoBlock _) = "do block show not implemented"
1209 se p bnd (PElabError s) = show s
1210 se p bnd (PCoerced t) = se p bnd t
1211 se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1212 se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1213-- se p bnd x = "Not implemented"
1215 slist' p bnd (PApp _ (PRef _ nil) _)
1216 | nsroot nil == UN "Nil" = Just []
1217 slist' p bnd (PApp _ (PRef _ cons) args)
1218 | nsroot cons == UN "::",
1219 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1220 all isImp imps,
1221 Just tl' <- slist' p bnd tl
1222 = Just (hd:tl')
1223 where
1224 isImp (PImp {}) = True
1225 isImp _ = False
1226 slist' _ _ _ = Nothing
1228 slist p bnd e | Just es <- slist' p bnd e = Just $
1229 case es of [] -> "[]"
1230 [x] -> "[" ++ se p bnd x ++ "]"
1231 xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1232 slist _ _ _ = Nothing
1234 -- since Prelude is always imported, S & Z are unqualified iff they're the
1235 -- Nat ones.
1236 snat p (PRef _ o)
1237 | show o == (natns++"Z") || show o == "Z" = Just 0
1238 snat p (PApp _ s [PExp {getTm=n}])
1239 | show s == (natns++"S") || show s == "S",
1240 Just n' <- snat p n
1241 = Just $ 1 + n'
1242 snat _ _ = Nothing
1244 natns = "Prelude.Nat."
1246 sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1247 sArg bnd (PExp _ _ tm _) = seArg bnd tm
1248 sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1249 sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1251 -- show argument, implicits given by the user also shown
1252 suiArg impl bnd (PImp _ mi _ n tm _)
1253 | impl || not mi = siArg bnd (n, tm)
1254 suiArg impl bnd (PExp _ _ tm _) = seArg bnd tm
1255 suiArg impl bnd _ = ""
1257 seArg bnd arg = " " ++ se 0 bnd arg
1258 siArg bnd (n, val) =
1259 let n' = show n
1260 val' = se 10 bnd val in
1261 if (n' == val')
1262 then " {" ++ n' ++ "}"
1263 else " {" ++ n' ++ " = " ++ val' ++ "}"
1264 scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1265 stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1267 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1268 | otherwise = str
1271instance Sized PTerm where
1272 size (PQuote rawTerm) = size rawTerm
1273 size (PRef fc name) = size name
1274 size (PLam name ty bdy) = 1 + size ty + size bdy
1275 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1276 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1277 size (PTyped trm ty) = 1 + size trm + size ty
1278 size (PApp fc name args) = 1 + size args
1279 size (PAppBind fc name args) = 1 + size args
1280 size (PCase fc trm bdy) = 1 + size trm + size bdy
1281 size (PTrue fc) = 1
1282 size (PFalse fc) = 1
1283 size (PRefl fc _) = 1
1284 size (PResolveTC fc) = 1
1285 size (PEq fc left right) = 1 + size left + size right
1286 size (PRewrite fc left right _) = 1 + size left + size right
1287 size (PPair fc left right) = 1 + size left + size right
1288 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1289 size (PAlternative a alts) = 1 + size alts
1290 size (PHidden hidden) = size hidden
1291 size (PUnifyLog tm) = size tm
1292 size (PNoImplicits tm) = size tm
1293 size PType = 1
1294 size (PConstant const) = 1 + size const
1295 size Placeholder = 1
1296 size (PDoBlock dos) = 1 + size dos
1297 size (PIdiom fc term) = 1 + size term
1298 size (PReturn fc) = 1
1299 size (PMetavar name) = 1
1300 size (PProof tactics) = size tactics
1301 size (PElabError err) = size err
1302 size PImpossible = 1
1304getPArity :: PTerm -> Int
1305getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1306getPArity _ = 0
1308-- Return all names, free or globally bound, in the given term.
1310allNamesIn :: PTerm -> [Name]
1311allNamesIn tm = nub $ ni [] tm
1312 where
1313 ni env (PRef _ n)
1314 | not (n `elem` env) = [n]
1315 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1316 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1317 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1318 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1319 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1320 ni env (PHidden tm) = ni env tm
1321 ni env (PEq _ l r) = ni env l ++ ni env r
1322 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1323 ni env (PTyped l r) = ni env l ++ ni env r
1324 ni env (PPair _ l r) = ni env l ++ ni env r
1325 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1326 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1327 ni env (PAlternative a ls) = concatMap (ni env) ls
1328 ni env (PUnifyLog tm) = ni env tm
1329 ni env (PNoImplicits tm) = ni env tm
1330 ni env _ = []
1332-- Return names which are free in the given term.
1334namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1335namesIn uvars ist tm = nub $ ni [] tm
1336 where
1337 ni env (PRef _ n)
1338 | not (n `elem` env)
1339 = case lookupTy n (tt_ctxt ist) of
1340 [] -> [n]
1341 _ -> if n `elem` (map fst uvars) then [n] else []
1342 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1343 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1344 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1345 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1346 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1347 ni env (PEq _ l r) = ni env l ++ ni env r
1348 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1349 ni env (PTyped l r) = ni env l ++ ni env r
1350 ni env (PPair _ l r) = ni env l ++ ni env r
1351 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1352 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1353 ni env (PAlternative a as) = concatMap (ni env) as
1354 ni env (PHidden tm) = ni env tm
1355 ni env (PUnifyLog tm) = ni env tm
1356 ni env (PNoImplicits tm) = ni env tm
1357 ni env _ = []
1359-- Return which of the given names are used in the given term.
1361usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1362usedNamesIn vars ist tm = nub $ ni [] tm
1363 where
1364 ni env (PRef _ n)
1365 | n `elem` vars && not (n `elem` env)
1366 = case lookupTy n (tt_ctxt ist) of
1367 [] -> [n]
1368 _ -> []
1369 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1370 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1371 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1372 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1373 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1374 ni env (PEq _ l r) = ni env l ++ ni env r
1375 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1376 ni env (PTyped l r) = ni env l ++ ni env r
1377 ni env (PPair _ l r) = ni env l ++ ni env r
1378 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1379 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1380 ni env (PAlternative a as) = concatMap (ni env) as
1381 ni env (PHidden tm) = ni env tm
1382 ni env (PUnifyLog tm) = ni env tm
1383 ni env (PNoImplicits tm) = ni env tm
1384 ni env _ = []