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