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