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