on 86c9dec help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1379 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 | SetOpt Opt
245 | UnsetOpt Opt
246 | NOP
247 | SetColour ColourType IdrisColour
248 | ColourOn
249 | ColourOff
251data Opt = Filename String
252 | Ver
253 | Usage
254 | Quiet
255 | ColourREPL Bool
256 | Ideslave
257 | ShowLibs
258 | ShowLibdir
259 | ShowIncs
260 | NoPrelude
261 | NoREPL
262 | OLogging Int
263 | Output String
264 | TypeCase
265 | TypeInType
266 | DefaultTotal
267 | DefaultPartial
268 | WarnPartial
269 | NoCoverage
270 | ErrContext
271 | ShowImpl
272 | Verbose
273 | IBCSubDir String
274 | ImportDir String
275 | PkgBuild String
276 | PkgInstall String
277 | PkgClean String
278 | WarnOnly
279 | Pkg String
280 | BCAsm String
281 | DumpDefun String
282 | DumpCases String
283 | FOVM String
284 | UseCodegen Codegen
285 | OutputTy OutputType
286 | Extension LanguageExt
287 | InterpretScript String
288 | TargetTriple String
289 | TargetCPU String
290 | OptLevel Word
291 | Client String
292 deriving (Show, Eq)
294-- Parsed declarations
296data Fixity = Infixl { prec :: Int }
297 | Infixr { prec :: Int }
298 | InfixN { prec :: Int }
299 | PrefixN { prec :: Int }
300 deriving Eq
301{-!
302deriving instance Binary Fixity
303!-}
305instance Show Fixity where
306 show (Infixl i) = "infixl " ++ show i
307 show (Infixr i) = "infixr " ++ show i
308 show (InfixN i) = "infix " ++ show i
309 show (PrefixN i) = "prefix " ++ show i
311data FixDecl = Fix Fixity String
312 deriving Eq
314instance Show FixDecl where
315 show (Fix f s) = show f ++ " " ++ s
317{-!
318deriving instance Binary FixDecl
319!-}
321instance Ord FixDecl where
322 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
325data Static = Static | Dynamic
326 deriving (Show, Eq)
327{-!
328deriving instance Binary Static
329!-}
331-- Mark bindings with their explicitness, and laziness
332data Plicity = Imp { plazy :: Bool,
333 pstatic :: Static,
334 pdocstr :: String }
335 | Exp { plazy :: Bool,
336 pstatic :: Static,
337 pdocstr :: String }
338 | Constraint { plazy :: Bool,
339 pstatic :: Static,
340 pdocstr :: String }
341 | TacImp { plazy :: Bool,
342 pstatic :: Static,
343 pscript :: PTerm,
344 pdocstr :: String }
345 deriving (Show, Eq)
347{-!
348deriving instance Binary Plicity
349!-}
351impl = Imp False Dynamic ""
352expl = Exp False Dynamic ""
353constraint = Constraint False Dynamic ""
354tacimpl t = TacImp False Dynamic t ""
356data FnOpt = Inlinable -- always evaluate when simplifying
357 | TotalFn | PartialFn
358 | Coinductive | AssertTotal
359 | Dictionary -- type class dictionary, eval only when
360 -- a function argument, and further evaluation resutls
361 | Implicit -- implicit coercion
362 | CExport String -- export, with a C name
363 | Reflection -- a reflecting function, compile-time only
364 | Specialise [(Name, Maybe Int)] -- specialise it, freeze these names
365 deriving (Show, Eq)
366{-!
367deriving instance Binary FnOpt
368!-}
370type FnOpts = [FnOpt]
372inlinable :: FnOpts -> Bool
373inlinable = elem Inlinable
375dictionary :: FnOpts -> Bool
376dictionary = elem Dictionary
378-- | Top-level declarations such as compiler directives, definitions,
379-- datatypes and typeclasses.
380data PDecl' t
381 = PFix FC Fixity [String] -- ^ Fixity declaration
382 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
383 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
384 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
385 | PCAF FC Name t -- ^ Top level constant
386 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
387 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
388 | PNamespace String [PDecl' t] -- ^ New namespace
389 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
390 | PClass String SyntaxInfo FC
391 [t] -- constraints
392 Name
393 [(Name, t)] -- parameters
394 [PDecl' t] -- declarations
395 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
396 -- class name, parameters, method declarations
397 | PInstance SyntaxInfo FC [t] -- constraints
398 Name -- class
399 [t] -- parameters
400 t -- full instance type
401 (Maybe Name) -- explicit name
402 [PDecl' t]
403 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
404 -- class name, parameters, full instance type, optional explicit name, and definitions
405 | PDSL Name (DSL' t) -- ^ DSL declaration
406 | PSyntax FC Syntax -- ^ Syntax definition
407 | PMutual FC [PDecl' t] -- ^ Mutual block
408 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
409 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
410 | PTransform FC Bool t t -- ^ Source-to-source transformation rule. If
411 -- bool is True, lhs and rhs must be convertible
412 deriving Functor
413{-!
414deriving instance Binary PDecl'
415!-}
417-- | One clause of a top-level definition. Term arguments to constructors are:
418--
419-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
420--
421-- 2. The list of extra 'with' patterns
422--
423-- 3. The right-hand side
424--
425-- 4. The where block (PDecl' t)
427data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
428 | PWith FC Name t [t] t [PDecl' t]
429 | PClauseR FC [t] t [PDecl' t]
430 | PWithR FC [t] t [PDecl' t]
431 deriving Functor
432{-!
433deriving instance Binary PClause'
434!-}
436-- | Data declaration
437data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
438 d_tcon :: t, -- ^ Type constructor
439 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
440 }
441 -- ^ Data declaration
442 | PLaterdecl { d_name :: Name, d_tcon :: t }
443 -- ^ "Placeholder" for data whose constructors are defined later
444 deriving Functor
445{-!
446deriving instance Binary PData'
447!-}
449-- Handy to get a free function for applying PTerm -> PTerm functions
450-- across a program, by deriving Functor
452type PDecl = PDecl' PTerm
453type PData = PData' PTerm
454type PClause = PClause' PTerm
456-- get all the names declared in a decl
458declared :: PDecl -> [Name]
459declared (PFix _ _ _) = []
460declared (PTy _ _ _ _ n t) = [n]
461declared (PPostulate _ _ _ _ n t) = [n]
462declared (PClauses _ _ n _) = [] -- not a declaration
463declared (PCAF _ n _) = [n]
464declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
465 where fstt (_, a, _, _) = a
466declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
467declared (PParams _ _ ds) = concatMap declared ds
468declared (PNamespace _ ds) = concatMap declared ds
469declared (PRecord _ _ _ n _ _ c _) = [n, c]
470declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
471declared (PInstance _ _ _ _ _ _ _ _) = []
472declared (PDSL n _) = [n]
473declared (PSyntax _ _) = []
474declared (PMutual _ ds) = concatMap declared ds
475declared (PDirective _) = []
477-- get the names declared, not counting nested parameter blocks
478tldeclared :: PDecl -> [Name]
479tldeclared (PFix _ _ _) = []
480tldeclared (PTy _ _ _ _ n t) = [n]
481tldeclared (PPostulate _ _ _ _ n t) = [n]
482tldeclared (PClauses _ _ n _) = [] -- not a declaration
483tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
484tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
485 where fstt (_, a, _, _) = a
486tldeclared (PParams _ _ ds) = []
487tldeclared (PMutual _ ds) = concatMap tldeclared ds
488tldeclared (PNamespace _ ds) = concatMap tldeclared ds
489tldeclared (PClass _ _ _ _ n _ ms) = concatMap tldeclared ms
490tldeclared (PInstance _ _ _ _ _ _ _ _) = []
491tldeclared _ = []
493defined :: PDecl -> [Name]
494defined (PFix _ _ _) = []
495defined (PTy _ _ _ _ n t) = []
496defined (PPostulate _ _ _ _ n t) = []
497defined (PClauses _ _ n _) = [n] -- not a declaration
498defined (PCAF _ n _) = [n]
499defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
500 where fstt (_, a, _, _) = a
501defined (PData _ _ _ _ (PLaterdecl n _)) = []
502defined (PParams _ _ ds) = concatMap defined ds
503defined (PNamespace _ ds) = concatMap defined ds
504defined (PRecord _ _ _ n _ _ c _) = [n, c]
505defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
506defined (PInstance _ _ _ _ _ _ _ _) = []
507defined (PDSL n _) = [n]
508defined (PSyntax _ _) = []
509defined (PMutual _ ds) = concatMap defined ds
510defined (PDirective _) = []
511--defined _ = []
513updateN :: [(Name, Name)] -> Name -> Name
514updateN ns n | Just n' <- lookup n ns = n'
515updateN _ n = n
517updateNs :: [(Name, Name)] -> PTerm -> PTerm
518updateNs [] t = t
519updateNs ns t = mapPT updateRef t
520 where updateRef (PRef fc f) = PRef fc (updateN ns f)
521 updateRef t = t
523-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
524-- updateDNs [] t = t
525-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
526-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
527-- where updateCNs ns (PClause n l ts r ds)
528-- = PClause (updateN ns n) (fmap (updateNs ns) l)
529-- (map (fmap (updateNs ns)) ts)
530-- (fmap (updateNs ns) r)
531-- (map (updateDNs ns) ds)
532-- updateDNs ns c = c
534-- | High level language terms
535data PTerm = PQuote Raw
536 | PRef FC Name
537 | PInferRef FC Name -- ^ A name to be defined later
538 | PPatvar FC Name
539 | PLam Name PTerm PTerm
540 | PPi Plicity Name PTerm PTerm
541 | PLet Name PTerm PTerm PTerm
542 | PTyped PTerm PTerm -- ^ Term with explicit type
543 | PApp FC PTerm [PArg]
544 | PAppBind FC PTerm [PArg] -- ^ implicitly bound application
545 | PMatchApp FC Name -- ^ Make an application by type matching
546 | PCase FC PTerm [(PTerm, PTerm)]
547 | PTrue FC
548 | PFalse FC
549 | PRefl FC PTerm
550 | PResolveTC FC
551 | PEq FC PTerm PTerm
552 | PRewrite FC PTerm PTerm (Maybe PTerm)
553 | PPair FC PTerm PTerm
554 | PDPair FC PTerm PTerm PTerm
555 | PAlternative Bool [PTerm] -- True if only one may work
556 | PHidden PTerm -- ^ Irrelevant or hidden pattern
557 | PType
558 | PGoal FC PTerm Name PTerm
559 | PConstant Const
560 | Placeholder
561 | PDoBlock [PDo]
562 | PIdiom FC PTerm
563 | PReturn FC
564 | PMetavar Name
565 | PProof [PTactic] -- ^ Proof script
566 | PTactics [PTactic] -- ^ As PProof, but no auto solving
567 | PElabError Err -- ^ Error to report on elaboration
568 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
569 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
570 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
571 | PNoImplicits PTerm -- ^ never run implicit converions on the term
572 deriving Eq
575{-!
576deriving instance Binary PTerm
577!-}
579mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
580mapPT f t = f (mpt t) where
581 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
582 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
583 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
584 mpt (PRewrite fc t s g) = PRewrite fc (mapPT f t) (mapPT f s)
585 (fmap (mapPT f) g)
586 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
587 mpt (PAppBind fc t as) = PAppBind fc (mapPT f t) (map (fmap (mapPT f)) as)
588 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
589 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
590 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
591 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
592 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
593 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
594 mpt (PHidden t) = PHidden (mapPT f t)
595 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
596 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
597 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
598 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
599 mpt (PNoImplicits tm) = PNoImplicits (mapPT f tm)
600 mpt (PGoal fc r n sc) = PGoal fc (mapPT f r) n (mapPT f sc)
601 mpt x = x
604data PTactic' t = Intro [Name] | Intros | Focus Name
605 | Refine Name [Bool] | Rewrite t
606 | Equiv t
607 | MatchRefine Name
608 | LetTac Name t | LetTacTy Name t t
609 | Exact t | Compute | Trivial
610 | Solve
611 | Attack
612 | ProofState | ProofTerm | Undo
613 | Try (PTactic' t) (PTactic' t)
614 | TSeq (PTactic' t) (PTactic' t)
615 | ApplyTactic t -- see Language.Reflection module
616 | Reflect t
617 | Fill t
618 | GoalType String (PTactic' t)
619 | Qed | Abandon
620 deriving (Show, Eq, Functor)
621{-!
622deriving instance Binary PTactic'
623!-}
625instance Sized a => Sized (PTactic' a) where
626 size (Intro nms) = 1 + size nms
627 size Intros = 1
628 size (Focus nm) = 1 + size nm
629 size (Refine nm bs) = 1 + size nm + length bs
630 size (Rewrite t) = 1 + size t
631 size (LetTac nm t) = 1 + size nm + size t
632 size (Exact t) = 1 + size t
633 size Compute = 1
634 size Trivial = 1
635 size Solve = 1
636 size Attack = 1
637 size ProofState = 1
638 size ProofTerm = 1
639 size Undo = 1
640 size (Try l r) = 1 + size l + size r
641 size (TSeq l r) = 1 + size l + size r
642 size (ApplyTactic t) = 1 + size t
643 size (Reflect t) = 1 + size t
644 size (Fill t) = 1 + size t
645 size Qed = 1
646 size Abandon = 1
648type PTactic = PTactic' PTerm
650data PDo' t = DoExp FC t
651 | DoBind FC Name t
652 | DoBindP FC t t
653 | DoLet FC Name t t
654 | DoLetP FC t t
655 deriving (Eq, Functor)
656{-!
657deriving instance Binary PDo'
658!-}
660instance Sized a => Sized (PDo' a) where
661 size (DoExp fc t) = 1 + size fc + size t
662 size (DoBind fc nm t) = 1 + size fc + size nm + size t
663 size (DoBindP fc l r) = 1 + size fc + size l + size r
664 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
665 size (DoLetP fc l r) = 1 + size fc + size l + size r
667type PDo = PDo' PTerm
669-- The priority gives a hint as to elaboration order. Best to elaborate
670-- things early which will help give a more concrete type to other
671-- variables, e.g. a before (interpTy a).
673data PArg' t = PImp { priority :: Int,
674 machine_inf :: Bool, -- true if the machine inferred it
675 lazyarg :: Bool, pname :: Name, getTm :: t,
676 pargdoc :: String }
677 | PExp { priority :: Int,
678 lazyarg :: Bool, getTm :: t,
679 pargdoc :: String }
680 | PConstraint { priority :: Int,
681 lazyarg :: Bool, getTm :: t,
682 pargdoc :: String }
683 | PTacImplicit { priority :: Int,
684 lazyarg :: Bool, pname :: Name,
685 getScript :: t,
686 getTm :: t,
687 pargdoc :: String }
688 deriving (Show, Eq, Functor)
690instance Sized a => Sized (PArg' a) where
691 size (PImp p _ l nm trm _) = 1 + size nm + size trm
692 size (PExp p l trm _) = 1 + size trm
693 size (PConstraint p l trm _) = 1 + size trm
694 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
696{-!
697deriving instance Binary PArg'
698!-}
700pimp n t mach = PImp 1 mach True n t ""
701pexp t = PExp 1 False t ""
702pconst t = PConstraint 1 False t ""
703ptacimp n s t = PTacImplicit 0 True n s t ""
705type PArg = PArg' PTerm
707-- Type class data
709data ClassInfo = CI { instanceName :: Name,
710 class_methods :: [(Name, (FnOpts, PTerm))],
711 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
712 class_params :: [Name],
713 class_instances :: [Name] }
714 deriving Show
715{-!
716deriving instance Binary ClassInfo
717!-}
719data OptInfo = Optimise { collapsible :: Bool,
720 isnewtype :: Bool,
721 forceable :: [Int], -- argument positions
722 recursive :: [Int] }
723 deriving Show
724{-!
725deriving instance Binary OptInfo
726!-}
729data TypeInfo = TI { con_names :: [Name],
730 codata :: Bool,
731 param_pos :: [Int] }
732 deriving Show
733{-!
734deriving instance Binary TypeInfo
735!-}
737-- Syntactic sugar info
739data DSL' t = DSL { dsl_bind :: t,
740 dsl_return :: t,
741 dsl_apply :: t,
742 dsl_pure :: t,
743 dsl_var :: Maybe t,
744 index_first :: Maybe t,
745 index_next :: Maybe t,
746 dsl_lambda :: Maybe t,
747 dsl_let :: Maybe t
748 }
749 deriving (Show, Functor)
750{-!
751deriving instance Binary DSL'
752!-}
754type DSL = DSL' PTerm
756data SynContext = PatternSyntax | TermSyntax | AnySyntax
757 deriving Show
758{-!
759deriving instance Binary SynContext
760!-}
762data Syntax = Rule [SSymbol] PTerm SynContext
763 deriving Show
764{-!
765deriving instance Binary Syntax
766!-}
768data SSymbol = Keyword Name
769 | Symbol String
770 | Binding Name
771 | Expr Name
772 | SimpleExpr Name
773 deriving Show
774{-!
775deriving instance Binary SSymbol
776!-}
778initDSL = DSL (PRef f (UN ">>="))
779 (PRef f (UN "return"))
780 (PRef f (UN "<$>"))
781 (PRef f (UN "pure"))
782 Nothing
783 Nothing
784 Nothing
785 Nothing
786 Nothing
787 where f = fileFC "(builtin)"
789data Using = UImplicit Name PTerm
790 | UConstraint Name [Name]
791 deriving (Show, Eq)
792{-!
793deriving instance Binary Using
794!-}
796data SyntaxInfo = Syn { using :: [Using],
797 syn_params :: [(Name, PTerm)],
798 syn_namespace :: [String],
799 no_imp :: [Name],
800 decoration :: Name -> Name,
801 inPattern :: Bool,
802 implicitAllowed :: Bool,
803 dsl_info :: DSL }
804 deriving Show
805{-!
806deriving instance Binary SyntaxInfo
807!-}
809defaultSyntax = Syn [] [] [] [] id False False initDSL
811expandNS :: SyntaxInfo -> Name -> Name
812expandNS syn n@(NS _ _) = n
813expandNS syn n = case syn_namespace syn of
814 [] -> n
815 xs -> NS n xs
818--- Pretty printing declarations and terms
820instance Show PTerm where
821 show tm = showImp Nothing False False tm
823instance Pretty PTerm where
824 pretty = prettyImp False
826instance Show PDecl where
827 show d = showDeclImp False d
829instance Show PClause where
830 show c = showCImp True c
832instance Show PData where
833 show d = showDImp False d
835showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
836showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
837showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
838showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
839showDeclImp _ (PData _ _ _ _ d) = show d
840showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
841 showSep "\n" (map show ps)
844showCImp :: Bool -> PClause -> String
845showCImp impl (PClause _ n l ws r w)
846 = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
847 ++ " where " ++ show w
848 where
849 showWs [] = ""
850 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
851showCImp impl (PWith _ n l ws r w)
852 = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
853 ++ " { " ++ show w ++ " } "
854 where
855 showWs [] = ""
856 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
859showDImp :: Bool -> PData -> String
860showDImp impl (PDatadecl n ty cons)
861 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
862 ++ showSep "\n\t| "
863 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
865getImps :: [PArg] -> [(Name, PTerm)]
866getImps [] = []
867getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
868getImps (_ : xs) = getImps xs
870getExps :: [PArg] -> [PTerm]
871getExps [] = []
872getExps (PExp _ _ tm _ : xs) = tm : getExps xs
873getExps (_ : xs) = getExps xs
875getConsts :: [PArg] -> [PTerm]
876getConsts [] = []
877getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
878getConsts (_ : xs) = getConsts xs
880getAll :: [PArg] -> [PTerm]
881getAll = map getTm
883-- | Pretty-print a high-level Idris term
884prettyImp :: Bool -- ^^ whether to show implicits
885 -> PTerm -- ^^ the term to pretty-print
886 -> Doc
887prettyImp impl = prettySe 10
888 where
889 prettySe p (PQuote r) =
890 if size r > breakingSize then
891 text "![" $$ pretty r <> text "]"
892 else
893 text "![" <> pretty r <> text "]"
894 prettySe p (PPatvar fc n) = pretty n
895 prettySe p (PRef fc n) =
896 if impl then
897 pretty n
898 else
899 prettyBasic n
900 where
901 prettyBasic n@(UN _) = pretty n
902 prettyBasic (MN _ s) = text s
903 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
904 prettyBasic (SN sn) = text (show sn)
905 prettySe p (PLam n ty sc) =
906 bracket p 2 $
907 if size sc > breakingSize then
908 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
909 else
910 text "λ" <> pretty n <+> text "=>" <+> pretty sc
911 prettySe p (PLet n ty v sc) =
912 bracket p 2 $
913 if size sc > breakingSize then
914 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
915 nest nestingSize (prettySe 10 sc)
916 else
917 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
918 prettySe 10 sc
919 prettySe p (PPi (Exp l s _) n ty sc)
920 | n `elem` allNamesIn sc || impl =
921 let open = if l then text "|" <> lparen else lparen in
922 bracket p 2 $
923 if size sc > breakingSize then
924 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
925 st <+> text "->" $+$ prettySe 10 sc
926 else
927 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
928 st <+> text "->" <+> prettySe 10 sc
929 | otherwise =
930 bracket p 2 $
931 if size sc > breakingSize then
932 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
933 else
934 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
935 where
936 st =
937 case s of
938 Static -> text "[static]"
939 _ -> empty
940 prettySe p (PPi (Imp l s _) n ty sc)
941 | impl =
942 let open = if l then text "|" <> lbrace else lbrace in
943 bracket p 2 $
944 if size sc > breakingSize then
945 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
946 st <+> text "->" <+> prettySe 10 sc
947 else
948 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
949 st <+> text "->" <+> prettySe 10 sc
950 | otherwise = prettySe 10 sc
951 where
952 st =
953 case s of
954 Static -> text $ "[static]"
955 _ -> empty
956 prettySe p (PPi (Constraint _ _ _) n ty sc) =
957 bracket p 2 $
958 if size sc > breakingSize then
959 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
960 else
961 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
962 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
963 bracket p 2 $
964 if size sc > breakingSize then
965 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
966 rbrace <+> text "->" $+$ prettySe 10 sc
967 else
968 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
969 rbrace <+> text "->" <+> prettySe 10 sc
970 prettySe p (PApp _ (PRef _ f) [])
971 | not impl = pretty f
972 prettySe p (PAppBind _ (PRef _ f) [])
973 | not impl = text "!" <+> pretty f
974 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
975 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
976 let [l, r] = getExps args in
977 bracket p 1 $
978 if size r > breakingSize then
979 prettySe 1 l <+> pretty op $+$ prettySe 0 r
980 else
981 prettySe 1 l <+> pretty op <+> prettySe 0 r
982 prettySe p (PApp _ f as) =
983 let args = getExps as in
984 bracket p 1 $
985 prettySe 1 f <+>
986 if impl then
987 foldl' fS empty as
988 -- foldr (<+>) empty $ map prettyArgS as
989 else
990 foldl' fSe empty args
991 -- foldr (<+>) empty $ map prettyArgSe args
992 where
993 fS l r =
994 if size r > breakingSize then
995 l $+$ nest nestingSize (prettyArgS r)
996 else
997 l <+> prettyArgS r
999 fSe l r =
1000 if size r > breakingSize then
1001 l $+$ nest nestingSize (prettyArgSe r)
1002 else
1003 l <+> prettyArgSe r
1004 prettySe p (PCase _ scr opts) =
1005 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
1006 where
1007 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
1009 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
1010 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
1011 prettySe p (PRefl _ _) = text "refl"
1012 prettySe p (PResolveTC _) = text "resolvetc"
1013 prettySe p (PTrue _) = text "()"
1014 prettySe p (PFalse _) = text "_|_"
1015 prettySe p (PEq _ l r) =
1016 bracket p 2 $
1017 if size r > breakingSize then
1018 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
1019 else
1020 prettySe 10 l <+> text "=" <+> prettySe 10 r
1021 prettySe p (PRewrite _ l r _) =
1022 bracket p 2 $
1023 if size r > breakingSize then
1024 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
1025 else
1026 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1027 prettySe p (PTyped l r) =
1028 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1029 prettySe p (PPair _ l r) =
1030 if size r > breakingSize then
1031 lparen <> prettySe 10 l <> text "," $+$
1032 prettySe 10 r <> rparen
1033 else
1034 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
1035 prettySe p (PDPair _ l t r) =
1036 if size r > breakingSize then
1037 lparen <> prettySe 10 l <+> text "**" $+$
1038 prettySe 10 r <> rparen
1039 else
1040 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1041 prettySe p (PAlternative a as) =
1042 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1043 where
1044 prettyAs =
1045 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1046 prettySe p PType = text "Type"
1047 prettySe p (PConstant c) = pretty c
1048 -- XXX: add pretty for tactics
1049 prettySe p (PProof ts) =
1050 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1051 prettySe p (PTactics ts) =
1052 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1053 prettySe p (PMetavar n) = text "?" <> pretty n
1054 prettySe p (PReturn f) = text "return"
1055 prettySe p PImpossible = text "impossible"
1056 prettySe p Placeholder = text "_"
1057 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1058 prettySe p (PElabError s) = pretty s
1060 prettySe p _ = text "test"
1062 prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1063 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1064 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1065 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1067 prettyArgSe arg = prettySe 0 arg
1068 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1069 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1070 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1072 bracket outer inner doc
1073 | inner > outer = lparen <> doc <> rparen
1074 | otherwise = doc
1076-- | Show Idris name
1077showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1078 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1079 -> Bool -- ^^ whether to show implicits
1080 -> Bool -- ^^ whether to colourise
1081 -> Name -- ^^ the term to show
1082 -> String
1083showName ist bnd impl colour n = case ist of
1084 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1085 Nothing -> showbasic n
1086 where name = if impl then show n else showbasic n
1087 showbasic n@(UN _) = show n
1088 showbasic (MN _ s) = s
1089 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1090 showbasic (SN s) = show s
1091 fst3 (x, _, _) = x
1092 colourise n t = let ctxt' = fmap tt_ctxt ist in
1093 case ctxt' of
1094 Nothing -> name
1095 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1096 else colouriseBound t name
1097 | isDConName n ctxt -> colouriseData t name
1098 | isFnName n ctxt -> colouriseFun t name
1099 | isTConName n ctxt -> colouriseType t name
1100 -- The assumption is that if a name is not bound and does not exist in the
1101 -- global context, then we're somewhere in which implicit info has been lost
1102 -- (like error messages). Thus, unknown vars are colourised as implicits.
1103 | otherwise -> colouriseImplicit t name
1105-- | Show Idris term
1106showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1107 -> Bool -- ^^ whether to show implicits
1108 -> Bool -- ^^ whether to colourise
1109 -> PTerm -- ^^ the term to show
1110 -> String
1111showImp ist impl colour tm = se 10 [] tm where
1112 perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1113 perhapsColourise col str = case ist of
1114 Just i -> if colour then col (idris_colourTheme i) str else str
1115 Nothing -> str
1117 se :: Int -> [(Name, Bool)] -> PTerm -> String
1118 se p bnd (PQuote r) = "![" ++ show r ++ "]"
1119 se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1120 se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1121 se p bnd e
1122 | Just str <- slist p bnd e = str
1123 | Just num <- snat p e = perhapsColourise colouriseData (show num)
1124 se p bnd (PRef fc n) = showName ist bnd impl colour n
1125 se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1126 (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1127 ++ se 10 ((n, False):bnd) sc
1128 se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1129 " = " ++ se 10 bnd v ++
1130 " in " ++ se 10 ((n, False):bnd) sc
1131 se p bnd (PPi (Exp l s _) n ty sc)
1132 | n `elem` allNamesIn sc || impl
1133 = bracket p 2 $
1134 (if l then "|(" else "(") ++
1135 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1136 ") " ++ st ++
1137 "-> " ++ se 10 ((n, False):bnd) sc
1138 | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1139 where st = case s of
1140 Static -> "[static] "
1141 _ -> ""
1142 se p bnd (PPi (Imp l s _) n ty sc)
1143 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1144 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1145 "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1146 | otherwise = se 10 ((n, True):bnd) sc
1147 where st = case s of
1148 Static -> "[static] "
1149 _ -> ""
1150 se p bnd (PPi (Constraint _ _ _) n ty sc)
1151 = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1152 se p bnd (PPi (TacImp _ _ s _) n ty sc)
1153 = bracket p 2 $
1154 "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1155 se 10 ((n, False):bnd) sc
1156 se p bnd (PMatchApp _ f) = "match " ++ show f
1157 se p bnd (PApp _ hd@(PRef _ f) [])
1158 | not impl = se p bnd hd
1159 se p bnd (PAppBind _ hd@(PRef _ f) [])
1160 | not impl = "!" ++ se p bnd hd
1161 se p bnd (PApp _ op@(PRef _ (UN (f:_))) args)
1162 | length (getExps args) == 2 && not impl && not (isAlpha f)
1163 = let [l, r] = getExps args in
1164 bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1165 se p bnd (PApp _ f as)
1166 = -- let args = getExps as in
1167 bracket p 1 $ se 1 bnd f ++
1168 if impl then concatMap (sArg bnd) as
1169 else concatMap (suiArg impl bnd) as
1170 se p bnd (PAppBind _ f as)
1171 = let args = getExps as in
1172 "!" ++ (bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1173 else concatMap (seArg bnd) args)
1174 se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1175 where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1176 se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1177 se p bnd (PRefl _ t)
1178 | not impl = perhapsColourise colouriseData "refl"
1179 | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1180 se p bnd (PResolveTC _) = "resolvetc"
1181 se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1182 se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1183 se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1184 se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1185 se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1186 se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1187 se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1188 se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1189 se p bnd PType = perhapsColourise colouriseType "Type"
1190 se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1191 where cfun (AType _) = colouriseType
1192 cfun StrType = colouriseType
1193 cfun PtrType = colouriseType
1194 cfun VoidType = colouriseType
1195 cfun _ = colouriseData
1196 se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1197 se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1198 se p bnd (PMetavar n) = "?" ++ show n
1199 se p bnd (PReturn f) = "return"
1200 se p bnd PImpossible = "impossible"
1201 se p bnd Placeholder = "_"
1202 se p bnd (PDoBlock _) = "do block show not implemented"
1203 se p bnd (PElabError s) = show s
1204 se p bnd (PCoerced t) = se p bnd t
1205 se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1206 se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1207-- se p bnd x = "Not implemented"
1209 slist' p bnd (PApp _ (PRef _ nil) _)
1210 | nsroot nil == UN "Nil" = Just []
1211 slist' p bnd (PApp _ (PRef _ cons) args)
1212 | nsroot cons == UN "::",
1213 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1214 all isImp imps,
1215 Just tl' <- slist' p bnd tl
1216 = Just (hd:tl')
1217 where
1218 isImp (PImp {}) = True
1219 isImp _ = False
1220 slist' _ _ _ = Nothing
1222 slist p bnd e | Just es <- slist' p bnd e = Just $
1223 case es of [] -> "[]"
1224 [x] -> "[" ++ se p bnd x ++ "]"
1225 xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1226 slist _ _ _ = Nothing
1228 -- since Prelude is always imported, S & Z are unqualified iff they're the
1229 -- Nat ones.
1230 snat p (PRef _ o)
1231 | show o == (natns++"Z") || show o == "Z" = Just 0
1232 snat p (PApp _ s [PExp {getTm=n}])
1233 | show s == (natns++"S") || show s == "S",
1234 Just n' <- snat p n
1235 = Just $ 1 + n'
1236 snat _ _ = Nothing
1238 natns = "Prelude.Nat."
1240 sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1241 sArg bnd (PExp _ _ tm _) = seArg bnd tm
1242 sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1243 sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1245 -- show argument, implicits given by the user also shown
1246 suiArg impl bnd (PImp _ mi _ n tm _)
1247 | impl || not mi = siArg bnd (n, tm)
1248 suiArg impl bnd (PExp _ _ tm _) = seArg bnd tm
1249 suiArg impl bnd _ = ""
1251 seArg bnd arg = " " ++ se 0 bnd arg
1252 siArg bnd (n, val) =
1253 let n' = show n
1254 val' = se 10 bnd val in
1255 if (n' == val')
1256 then " {" ++ n' ++ "}"
1257 else " {" ++ n' ++ " = " ++ val' ++ "}"
1258 scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1259 stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1261 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1262 | otherwise = str
1265instance Sized PTerm where
1266 size (PQuote rawTerm) = size rawTerm
1267 size (PRef fc name) = size name
1268 size (PLam name ty bdy) = 1 + size ty + size bdy
1269 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1270 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1271 size (PTyped trm ty) = 1 + size trm + size ty
1272 size (PApp fc name args) = 1 + size args
1273 size (PAppBind fc name args) = 1 + size args
1274 size (PCase fc trm bdy) = 1 + size trm + size bdy
1275 size (PTrue fc) = 1
1276 size (PFalse fc) = 1
1277 size (PRefl fc _) = 1
1278 size (PResolveTC fc) = 1
1279 size (PEq fc left right) = 1 + size left + size right
1280 size (PRewrite fc left right _) = 1 + size left + size right
1281 size (PPair fc left right) = 1 + size left + size right
1282 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1283 size (PAlternative a alts) = 1 + size alts
1284 size (PHidden hidden) = size hidden
1285 size (PUnifyLog tm) = size tm
1286 size (PNoImplicits tm) = size tm
1287 size PType = 1
1288 size (PConstant const) = 1 + size const
1289 size Placeholder = 1
1290 size (PDoBlock dos) = 1 + size dos
1291 size (PIdiom fc term) = 1 + size term
1292 size (PReturn fc) = 1
1293 size (PMetavar name) = 1
1294 size (PProof tactics) = size tactics
1295 size (PElabError err) = size err
1296 size PImpossible = 1
1298getPArity :: PTerm -> Int
1299getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1300getPArity _ = 0
1302-- Return all names, free or globally bound, in the given term.
1304allNamesIn :: PTerm -> [Name]
1305allNamesIn tm = nub $ ni [] tm
1306 where
1307 ni env (PRef _ n)
1308 | not (n `elem` env) = [n]
1309 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1310 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1311 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1312 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1313 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1314 ni env (PHidden tm) = ni env tm
1315 ni env (PEq _ l r) = ni env l ++ ni env r
1316 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1317 ni env (PTyped l r) = ni env l ++ ni env r
1318 ni env (PPair _ l r) = ni env l ++ ni env r
1319 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1320 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1321 ni env (PAlternative a ls) = concatMap (ni env) ls
1322 ni env (PUnifyLog tm) = ni env tm
1323 ni env (PNoImplicits tm) = ni env tm
1324 ni env _ = []
1326-- Return names which are free in the given term.
1328namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1329namesIn uvars ist tm = nub $ ni [] tm
1330 where
1331 ni env (PRef _ n)
1332 | not (n `elem` env)
1333 = case lookupTy n (tt_ctxt ist) of
1334 [] -> [n]
1335 _ -> if n `elem` (map fst uvars) then [n] else []
1336 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1337 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1338 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1339 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1340 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1341 ni env (PEq _ l r) = ni env l ++ ni env r
1342 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1343 ni env (PTyped l r) = ni env l ++ ni env r
1344 ni env (PPair _ l r) = ni env l ++ ni env r
1345 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1346 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1347 ni env (PAlternative a as) = concatMap (ni env) as
1348 ni env (PHidden tm) = ni env tm
1349 ni env (PUnifyLog tm) = ni env tm
1350 ni env (PNoImplicits tm) = ni env tm
1351 ni env _ = []
1353-- Return which of the given names are used in the given term.
1355usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1356usedNamesIn vars ist tm = nub $ ni [] tm
1357 where
1358 ni env (PRef _ n)
1359 | n `elem` vars && not (n `elem` env)
1360 = case lookupTy n (tt_ctxt ist) of
1361 [] -> [n]
1362 _ -> []
1363 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1364 ni env (PAppBind _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1365 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1366 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1367 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1368 ni env (PEq _ l r) = ni env l ++ ni env r
1369 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1370 ni env (PTyped l r) = ni env l ++ ni env r
1371 ni env (PPair _ l r) = ni env l ++ ni env r
1372 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1373 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1374 ni env (PAlternative a as) = concatMap (ni env) as
1375 ni env (PHidden tm) = ni env tm
1376 ni env (PUnifyLog tm) = ni env tm
1377 ni env (PNoImplicits tm) = ni env tm
1378 ni env _ = []