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