on 76722bf help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1342 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 machine_inf :: Bool, -- true if the machine inferred it
663 lazyarg :: Bool, pname :: Name, getTm :: t,
664 pargdoc :: String }
665 | PExp { priority :: Int,
666 lazyarg :: Bool, getTm :: t,
667 pargdoc :: String }
668 | PConstraint { priority :: Int,
669 lazyarg :: Bool, getTm :: t,
670 pargdoc :: String }
671 | PTacImplicit { priority :: Int,
672 lazyarg :: Bool, pname :: Name,
673 getScript :: t,
674 getTm :: t,
675 pargdoc :: String }
676 deriving (Show, Eq, Functor)
678instance Sized a => Sized (PArg' a) where
679 size (PImp p _ l nm trm _) = 1 + size nm + size trm
680 size (PExp p l trm _) = 1 + size trm
681 size (PConstraint p l trm _) = 1 + size trm
682 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
684{-!
685deriving instance Binary PArg'
686!-}
688pimp n t mach = PImp 1 mach True n t ""
689pexp t = PExp 1 False t ""
690pconst t = PConstraint 1 False t ""
691ptacimp n s t = PTacImplicit 0 True n s t ""
693type PArg = PArg' PTerm
695-- Type class data
697data ClassInfo = CI { instanceName :: Name,
698 class_methods :: [(Name, (FnOpts, PTerm))],
699 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
700 class_params :: [Name],
701 class_instances :: [Name] }
702 deriving Show
703{-!
704deriving instance Binary ClassInfo
705!-}
707data OptInfo = Optimise { collapsible :: Bool,
708 isnewtype :: Bool,
709 forceable :: [Int], -- argument positions
710 recursive :: [Int] }
711 deriving Show
712{-!
713deriving instance Binary OptInfo
714!-}
717data TypeInfo = TI { con_names :: [Name],
718 codata :: Bool,
719 param_pos :: [Int] }
720 deriving Show
721{-!
722deriving instance Binary TypeInfo
723!-}
725-- Syntactic sugar info
727data DSL' t = DSL { dsl_bind :: t,
728 dsl_return :: t,
729 dsl_apply :: t,
730 dsl_pure :: t,
731 dsl_var :: Maybe t,
732 index_first :: Maybe t,
733 index_next :: Maybe t,
734 dsl_lambda :: Maybe t,
735 dsl_let :: Maybe t
736 }
737 deriving (Show, Functor)
738{-!
739deriving instance Binary DSL'
740!-}
742type DSL = DSL' PTerm
744data SynContext = PatternSyntax | TermSyntax | AnySyntax
745 deriving Show
746{-!
747deriving instance Binary SynContext
748!-}
750data Syntax = Rule [SSymbol] PTerm SynContext
751 deriving Show
752{-!
753deriving instance Binary Syntax
754!-}
756data SSymbol = Keyword Name
757 | Symbol String
758 | Binding Name
759 | Expr Name
760 | SimpleExpr Name
761 deriving Show
762{-!
763deriving instance Binary SSymbol
764!-}
766initDSL = DSL (PRef f (UN ">>="))
767 (PRef f (UN "return"))
768 (PRef f (UN "<$>"))
769 (PRef f (UN "pure"))
770 Nothing
771 Nothing
772 Nothing
773 Nothing
774 Nothing
775 where f = FC "(builtin)" 0
777data Using = UImplicit Name PTerm
778 | UConstraint Name [Name]
779 deriving (Show, Eq)
780{-!
781deriving instance Binary Using
782!-}
784data SyntaxInfo = Syn { using :: [Using],
785 syn_params :: [(Name, PTerm)],
786 syn_namespace :: [String],
787 no_imp :: [Name],
788 decoration :: Name -> Name,
789 inPattern :: Bool,
790 implicitAllowed :: Bool,
791 dsl_info :: DSL }
792 deriving Show
793{-!
794deriving instance Binary SyntaxInfo
795!-}
797defaultSyntax = Syn [] [] [] [] id False False initDSL
799expandNS :: SyntaxInfo -> Name -> Name
800expandNS syn n@(NS _ _) = n
801expandNS syn n = case syn_namespace syn of
802 [] -> n
803 xs -> NS n xs
806--- Pretty printing declarations and terms
808instance Show PTerm where
809 show tm = showImp Nothing False False tm
811instance Pretty PTerm where
812 pretty = prettyImp False
814instance Show PDecl where
815 show d = showDeclImp False d
817instance Show PClause where
818 show c = showCImp True c
820instance Show PData where
821 show d = showDImp False d
823showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
824showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
825showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp Nothing t False ty
826showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
827showDeclImp _ (PData _ _ _ _ d) = show d
828showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
829 showSep "\n" (map show ps)
832showCImp :: Bool -> PClause -> String
833showCImp impl (PClause _ n l ws r w)
834 = showImp Nothing impl False l ++ showWs ws ++ " = " ++ showImp Nothing impl False r
835 ++ " where " ++ show w
836 where
837 showWs [] = ""
838 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
839showCImp impl (PWith _ n l ws r w)
840 = showImp Nothing impl False l ++ showWs ws ++ " with " ++ showImp Nothing impl False r
841 ++ " { " ++ show w ++ " } "
842 where
843 showWs [] = ""
844 showWs (x : xs) = " | " ++ showImp Nothing impl False x ++ showWs xs
847showDImp :: Bool -> PData -> String
848showDImp impl (PDatadecl n ty cons)
849 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
850 ++ showSep "\n\t| "
851 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
853getImps :: [PArg] -> [(Name, PTerm)]
854getImps [] = []
855getImps (PImp _ _ _ n tm _ : xs) = (n, tm) : getImps xs
856getImps (_ : xs) = getImps xs
858getExps :: [PArg] -> [PTerm]
859getExps [] = []
860getExps (PExp _ _ tm _ : xs) = tm : getExps xs
861getExps (_ : xs) = getExps xs
863getConsts :: [PArg] -> [PTerm]
864getConsts [] = []
865getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
866getConsts (_ : xs) = getConsts xs
868getAll :: [PArg] -> [PTerm]
869getAll = map getTm
871-- | Pretty-print a high-level Idris term
872prettyImp :: Bool -- ^^ whether to show implicits
873 -> PTerm -- ^^ the term to pretty-print
874 -> Doc
875prettyImp impl = prettySe 10
876 where
877 prettySe p (PQuote r) =
878 if size r > breakingSize then
879 text "![" $$ pretty r <> text "]"
880 else
881 text "![" <> pretty r <> text "]"
882 prettySe p (PPatvar fc n) = pretty n
883 prettySe p (PRef fc n) =
884 if impl then
885 pretty n
886 else
887 prettyBasic n
888 where
889 prettyBasic n@(UN _) = pretty n
890 prettyBasic (MN _ s) = text s
891 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
892 prettySe p (PLam n ty sc) =
893 bracket p 2 $
894 if size sc > breakingSize then
895 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
896 else
897 text "λ" <> pretty n <+> text "=>" <+> pretty sc
898 prettySe p (PLet n ty v sc) =
899 bracket p 2 $
900 if size sc > breakingSize then
901 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
902 nest nestingSize (prettySe 10 sc)
903 else
904 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
905 prettySe 10 sc
906 prettySe p (PPi (Exp l s _) n ty sc)
907 | n `elem` allNamesIn sc || impl =
908 let open = if l then text "|" <> lparen else lparen in
909 bracket p 2 $
910 if size sc > breakingSize then
911 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
912 st <+> text "->" $+$ prettySe 10 sc
913 else
914 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
915 st <+> text "->" <+> prettySe 10 sc
916 | otherwise =
917 bracket p 2 $
918 if size sc > breakingSize then
919 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
920 else
921 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
922 where
923 st =
924 case s of
925 Static -> text "[static]"
926 _ -> empty
927 prettySe p (PPi (Imp l s _) n ty sc)
928 | impl =
929 let open = if l then text "|" <> lbrace else lbrace in
930 bracket p 2 $
931 if size sc > breakingSize then
932 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
933 st <+> text "->" <+> prettySe 10 sc
934 else
935 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
936 st <+> text "->" <+> prettySe 10 sc
937 | otherwise = prettySe 10 sc
938 where
939 st =
940 case s of
941 Static -> text $ "[static]"
942 _ -> empty
943 prettySe p (PPi (Constraint _ _ _) n ty sc) =
944 bracket p 2 $
945 if size sc > breakingSize then
946 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
947 else
948 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
949 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
950 bracket p 2 $
951 if size sc > breakingSize then
952 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
953 rbrace <+> text "->" $+$ prettySe 10 sc
954 else
955 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
956 rbrace <+> text "->" <+> prettySe 10 sc
957 prettySe p (PApp _ (PRef _ f) [])
958 | not impl = pretty f
959 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
960 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
961 let [l, r] = getExps args in
962 bracket p 1 $
963 if size r > breakingSize then
964 prettySe 1 l <+> pretty op $+$ prettySe 0 r
965 else
966 prettySe 1 l <+> pretty op <+> prettySe 0 r
967 prettySe p (PApp _ f as) =
968 let args = getExps as in
969 bracket p 1 $
970 prettySe 1 f <+>
971 if impl then
972 foldl' fS empty as
973 -- foldr (<+>) empty $ map prettyArgS as
974 else
975 foldl' fSe empty args
976 -- foldr (<+>) empty $ map prettyArgSe args
977 where
978 fS l r =
979 if size r > breakingSize then
980 l $+$ nest nestingSize (prettyArgS r)
981 else
982 l <+> prettyArgS r
984 fSe l r =
985 if size r > breakingSize then
986 l $+$ nest nestingSize (prettyArgSe r)
987 else
988 l <+> prettyArgSe r
989 prettySe p (PCase _ scr opts) =
990 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
991 where
992 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
994 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
995 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
996 prettySe p (PRefl _ _) = text "refl"
997 prettySe p (PResolveTC _) = text "resolvetc"
998 prettySe p (PTrue _) = text "()"
999 prettySe p (PFalse _) = text "_|_"
1000 prettySe p (PEq _ l r) =
1001 bracket p 2 $
1002 if size r > breakingSize then
1003 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
1004 else
1005 prettySe 10 l <+> text "=" <+> prettySe 10 r
1006 prettySe p (PRewrite _ l r _) =
1007 bracket p 2 $
1008 if size r > breakingSize then
1009 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
1010 else
1011 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
1012 prettySe p (PTyped l r) =
1013 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
1014 prettySe p (PPair _ l r) =
1015 if size r > breakingSize then
1016 lparen <> prettySe 10 l <> text "," $+$
1017 prettySe 10 r <> rparen
1018 else
1019 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
1020 prettySe p (PDPair _ l t r) =
1021 if size r > breakingSize then
1022 lparen <> prettySe 10 l <+> text "**" $+$
1023 prettySe 10 r <> rparen
1024 else
1025 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
1026 prettySe p (PAlternative a as) =
1027 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1028 where
1029 prettyAs =
1030 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
1031 prettySe p PType = text "Type"
1032 prettySe p (PConstant c) = pretty c
1033 -- XXX: add pretty for tactics
1034 prettySe p (PProof ts) =
1035 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1036 prettySe p (PTactics ts) =
1037 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
1038 prettySe p (PMetavar n) = text "?" <> pretty n
1039 prettySe p (PReturn f) = text "return"
1040 prettySe p PImpossible = text "impossible"
1041 prettySe p Placeholder = text "_"
1042 prettySe p (PDoBlock _) = text "do block pretty not implemented"
1043 prettySe p (PElabError s) = pretty s
1045 prettySe p _ = text "test"
1047 prettyArgS (PImp _ _ _ n tm _) = prettyArgSi (n, tm)
1048 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1049 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1050 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1052 prettyArgSe arg = prettySe 0 arg
1053 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1054 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1055 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1057 bracket outer inner doc
1058 | inner > outer = lparen <> doc <> rparen
1059 | otherwise = doc
1061-- | Show Idris name
1062showName :: Maybe IState -- ^^ the Idris state, for information about names and colours
1063 -> [(Name, Bool)] -- ^^ the bound variables and whether they're implicit
1064 -> Bool -- ^^ whether to show implicits
1065 -> Bool -- ^^ whether to colourise
1066 -> Name -- ^^ the term to show
1067 -> String
1068showName ist bnd impl colour n = case ist of
1069 Just i -> if colour then colourise n (idris_colourTheme i) else showbasic n
1070 Nothing -> showbasic n
1071 where name = if impl then show n else showbasic n
1072 showbasic n@(UN _) = show n
1073 showbasic (MN _ s) = s
1074 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1075 showbasic (SN s) = show s
1076 fst3 (x, _, _) = x
1077 colourise n t = let ctxt' = fmap tt_ctxt ist in
1078 case ctxt' of
1079 Nothing -> name
1080 Just ctxt | Just impl <- lookup n bnd -> if impl then colouriseImplicit t name
1081 else colouriseBound t name
1082 | isDConName n ctxt -> colouriseData t name
1083 | isFnName n ctxt -> colouriseFun t name
1084 | isTConName n ctxt -> colouriseType t name
1085 -- The assumption is that if a name is not bound and does not exist in the
1086 -- global context, then we're somewhere in which implicit info has been lost
1087 -- (like error messages). Thus, unknown vars are colourised as implicits.
1088 | otherwise -> colouriseImplicit t name
1090-- | Show Idris term
1091showImp :: Maybe IState -- ^^ the Idris state, for information about identifiers and colours
1092 -> Bool -- ^^ whether to show implicits
1093 -> Bool -- ^^ whether to colourise
1094 -> PTerm -- ^^ the term to show
1095 -> String
1096showImp ist impl colour tm = se 10 [] tm where
1097 perhapsColourise :: (ColourTheme -> String -> String) -> String -> String
1098 perhapsColourise col str = case ist of
1099 Just i -> if colour then col (idris_colourTheme i) str else str
1100 Nothing -> str
1102 se :: Int -> [(Name, Bool)] -> PTerm -> String
1103 se p bnd (PQuote r) = "![" ++ show r ++ "]"
1104 se p bnd (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1105 se p bnd (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1106 se p bnd e
1107 | Just str <- slist p bnd e = str
1108 | Just num <- snat p e = perhapsColourise colouriseData (show num)
1109 se p bnd (PRef fc n) = showName ist bnd impl colour n
1110 se p bnd (PLam n ty sc) = bracket p 2 $ "\\ " ++ perhapsColourise colouriseBound (show n) ++
1111 (if impl then " : " ++ se 10 bnd ty else "") ++ " => "
1112 ++ se 10 ((n, False):bnd) sc
1113 se p bnd (PLet n ty v sc) = bracket p 2 $ "let " ++ perhapsColourise colouriseBound (show n) ++
1114 " = " ++ se 10 bnd v ++
1115 " in " ++ se 10 ((n, False):bnd) sc
1116 se p bnd (PPi (Exp l s _) n ty sc)
1117 | n `elem` allNamesIn sc || impl
1118 = bracket p 2 $
1119 (if l then "|(" else "(") ++
1120 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1121 ") " ++ st ++
1122 "-> " ++ se 10 ((n, False):bnd) sc
1123 | otherwise = bracket p 2 $ se 0 bnd ty ++ " " ++ st ++ "-> " ++ se 10 bnd sc
1124 where st = case s of
1125 Static -> "[static] "
1126 _ -> ""
1127 se p bnd (PPi (Imp l s _) n ty sc)
1128 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1129 perhapsColourise colouriseBound (show n) ++ " : " ++ se 10 bnd ty ++
1130 "} " ++ st ++ "-> " ++ se 10 ((n, True):bnd) sc
1131 | otherwise = se 10 ((n, True):bnd) sc
1132 where st = case s of
1133 Static -> "[static] "
1134 _ -> ""
1135 se p bnd (PPi (Constraint _ _ _) n ty sc)
1136 = bracket p 2 $ se 10 bnd ty ++ " => " ++ se 10 bnd sc
1137 se p bnd (PPi (TacImp _ _ s _) n ty sc)
1138 = bracket p 2 $
1139 "{tacimp " ++ (perhapsColourise colouriseBound (show n)) ++ " : " ++ se 10 bnd ty ++ "} -> " ++
1140 se 10 ((n, False):bnd) sc
1141 se p bnd (PMatchApp _ f) = "match " ++ show f
1142 se p bnd (PApp _ hd@(PRef _ f) [])
1143 | not impl = se p bnd hd
1144 se p bnd (PApp _ op@(PRef _ (UN (f:_))) args)
1145 | length (getExps args) == 2 && not impl && not (isAlpha f)
1146 = let [l, r] = getExps args in
1147 bracket p 1 $ se 1 bnd l ++ " " ++ se p bnd op ++ " " ++ se 0 bnd r
1148 se p bnd (PApp _ f as)
1149 = let args = getExps as in
1150 bracket p 1 $ se 1 bnd f ++ if impl then concatMap (sArg bnd) as
1151 else concatMap (seArg bnd) args
1152 se p bnd (PCase _ scr opts) = "case " ++ se 10 bnd scr ++ " of " ++ showSep " | " (map sc opts)
1153 where sc (l, r) = se 10 bnd l ++ " => " ++ se 10 bnd r
1154 se p bnd (PHidden tm) = "." ++ se 0 bnd tm
1155 se p bnd (PRefl _ t)
1156 | not impl = perhapsColourise colouriseData "refl"
1157 | otherwise = perhapsColourise colouriseData $ "refl {" ++ se 10 bnd t ++ "}"
1158 se p bnd (PResolveTC _) = "resolvetc"
1159 se p bnd (PTrue _) = perhapsColourise colouriseType "()"
1160 se p bnd (PFalse _) = perhapsColourise colouriseType "_|_"
1161 se p bnd (PEq _ l r) = bracket p 2 $ se 10 bnd l ++ perhapsColourise colouriseType " = " ++ se 10 bnd r
1162 se p bnd (PRewrite _ l r _) = bracket p 2 $ "rewrite " ++ se 10 bnd l ++ " in " ++ se 10 bnd r
1163 se p bnd (PTyped l r) = "(" ++ se 10 bnd l ++ " : " ++ se 10 bnd r ++ ")"
1164 se p bnd (PPair _ l r) = "(" ++ se 10 bnd l ++ ", " ++ se 10 bnd r ++ ")"
1165 se p bnd (PDPair _ l t r) = "(" ++ se 10 bnd l ++ " ** " ++ se 10 bnd r ++ ")"
1166 se p bnd (PAlternative a as) = "(|" ++ showSep " , " (map (se 10 bnd) as) ++ "|)"
1167 se p bnd PType = perhapsColourise colouriseType "Type"
1168 se p bnd (PConstant c) = perhapsColourise (cfun c) (show c)
1169 where cfun (AType _) = colouriseType
1170 cfun StrType = colouriseType
1171 cfun PtrType = colouriseType
1172 cfun VoidType = colouriseType
1173 cfun _ = colouriseData
1174 se p bnd (PProof ts) = "proof { " ++ show ts ++ "}"
1175 se p bnd (PTactics ts) = "tactics { " ++ show ts ++ "}"
1176 se p bnd (PMetavar n) = "?" ++ show n
1177 se p bnd (PReturn f) = "return"
1178 se p bnd PImpossible = "impossible"
1179 se p bnd Placeholder = "_"
1180 se p bnd (PDoBlock _) = "do block show not implemented"
1181 se p bnd (PElabError s) = show s
1182 se p bnd (PCoerced t) = se p bnd t
1183 se p bnd (PUnifyLog t) = "%unifyLog " ++ se p bnd t
1184 se p bnd (PNoImplicits t) = "%noimplicit " ++ se p bnd t
1185-- se p bnd x = "Not implemented"
1187 slist' p bnd (PApp _ (PRef _ nil) _)
1188 | nsroot nil == UN "Nil" = Just []
1189 slist' p bnd (PApp _ (PRef _ cons) args)
1190 | nsroot cons == UN "::",
1191 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1192 all isImp imps,
1193 Just tl' <- slist' p bnd tl
1194 = Just (hd:tl')
1195 where
1196 isImp (PImp {}) = True
1197 isImp _ = False
1198 slist' _ _ _ = Nothing
1200 slist p bnd e | Just es <- slist' p bnd e = Just $
1201 case es of [] -> "[]"
1202 [x] -> "[" ++ se p bnd x ++ "]"
1203 xs -> "[" ++ intercalate "," (map (se p bnd ) xs) ++ "]"
1204 slist _ _ _ = Nothing
1206 -- since Prelude is always imported, S & Z are unqualified iff they're the
1207 -- Nat ones.
1208 snat p (PRef _ o)
1209 | show o == (natns++"Z") || show o == "Z" = Just 0
1210 snat p (PApp _ s [PExp {getTm=n}])
1211 | show s == (natns++"S") || show s == "S",
1212 Just n' <- snat p n
1213 = Just $ 1 + n'
1214 snat _ _ = Nothing
1216 natns = "Prelude.Nat."
1218 sArg bnd (PImp _ _ _ n tm _) = siArg bnd (n, tm)
1219 sArg bnd (PExp _ _ tm _) = seArg bnd tm
1220 sArg bnd (PConstraint _ _ tm _) = scArg bnd tm
1221 sArg bnd (PTacImplicit _ _ n _ tm _) = stiArg bnd (n, tm)
1223 seArg bnd arg = " " ++ se 0 bnd arg
1224 siArg bnd (n, val) = " {" ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1225 scArg bnd val = " {{" ++ se 10 bnd val ++ "}}"
1226 stiArg bnd (n, val) = " {auto " ++ show n ++ " = " ++ se 10 bnd val ++ "}"
1228 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1229 | otherwise = str
1232instance Sized PTerm where
1233 size (PQuote rawTerm) = size rawTerm
1234 size (PRef fc name) = size name
1235 size (PLam name ty bdy) = 1 + size ty + size bdy
1236 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1237 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1238 size (PTyped trm ty) = 1 + size trm + size ty
1239 size (PApp fc name args) = 1 + size args
1240 size (PCase fc trm bdy) = 1 + size trm + size bdy
1241 size (PTrue fc) = 1
1242 size (PFalse fc) = 1
1243 size (PRefl fc _) = 1
1244 size (PResolveTC fc) = 1
1245 size (PEq fc left right) = 1 + size left + size right
1246 size (PRewrite fc left right _) = 1 + size left + size right
1247 size (PPair fc left right) = 1 + size left + size right
1248 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1249 size (PAlternative a alts) = 1 + size alts
1250 size (PHidden hidden) = size hidden
1251 size (PUnifyLog tm) = size tm
1252 size (PNoImplicits tm) = size tm
1253 size PType = 1
1254 size (PConstant const) = 1 + size const
1255 size Placeholder = 1
1256 size (PDoBlock dos) = 1 + size dos
1257 size (PIdiom fc term) = 1 + size term
1258 size (PReturn fc) = 1
1259 size (PMetavar name) = 1
1260 size (PProof tactics) = size tactics
1261 size (PElabError err) = size err
1262 size PImpossible = 1
1264getPArity :: PTerm -> Int
1265getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1266getPArity _ = 0
1268-- Return all names, free or globally bound, in the given term.
1270allNamesIn :: PTerm -> [Name]
1271allNamesIn tm = nub $ ni [] tm
1272 where
1273 ni env (PRef _ n)
1274 | not (n `elem` env) = [n]
1275 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1276 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1277 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1278 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1279 ni env (PHidden tm) = ni env tm
1280 ni env (PEq _ l r) = ni env l ++ ni env r
1281 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1282 ni env (PTyped l r) = ni env l ++ ni env r
1283 ni env (PPair _ l r) = ni env l ++ ni env r
1284 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1285 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1286 ni env (PAlternative a ls) = concatMap (ni env) ls
1287 ni env (PUnifyLog tm) = ni env tm
1288 ni env (PNoImplicits tm) = ni env tm
1289 ni env _ = []
1291-- Return names which are free in the given term.
1293namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1294namesIn uvars ist tm = nub $ ni [] tm
1295 where
1296 ni env (PRef _ n)
1297 | not (n `elem` env)
1298 = case lookupTy n (tt_ctxt ist) of
1299 [] -> [n]
1300 _ -> if n `elem` (map fst uvars) then [n] else []
1301 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1302 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1303 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1304 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1305 ni env (PEq _ l r) = ni env l ++ ni env r
1306 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1307 ni env (PTyped l r) = ni env l ++ ni env r
1308 ni env (PPair _ l r) = ni env l ++ ni env r
1309 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1310 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1311 ni env (PAlternative a as) = concatMap (ni env) as
1312 ni env (PHidden tm) = ni env tm
1313 ni env (PUnifyLog tm) = ni env tm
1314 ni env (PNoImplicits tm) = ni env tm
1315 ni env _ = []
1317-- Return which of the given names are used in the given term.
1319usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1320usedNamesIn vars ist tm = nub $ ni [] tm
1321 where
1322 ni env (PRef _ n)
1323 | n `elem` vars && not (n `elem` env)
1324 = case lookupTy n (tt_ctxt ist) of
1325 [] -> [n]
1326 _ -> []
1327 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1328 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1329 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1330 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1331 ni env (PEq _ l r) = ni env l ++ ni env r
1332 ni env (PRewrite _ l r _) = ni env l ++ ni env r
1333 ni env (PTyped l r) = ni env l ++ ni env r
1334 ni env (PPair _ l r) = ni env l ++ ni env r
1335 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1336 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1337 ni env (PAlternative a as) = concatMap (ni env) as
1338 ni env (PHidden tm) = ni env tm
1339 ni env (PUnifyLog tm) = ni env tm
1340 ni env (PNoImplicits tm) = ni env tm
1341 ni env _ = []