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