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