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