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