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