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