on f8ea178 help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1243 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 :: [(Target, FilePath)],
99 idris_libs :: [(Target, String)],
100 idris_hdrs :: [(Target, 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 Target FilePath
149 | IBCLib Target String
150 | IBCDyLib String
151 | IBCHeader Target 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 | InterpretScript String
257 deriving (Show, Eq)
259-- Parsed declarations
261data Fixity = Infixl { prec :: Int }
262 | Infixr { prec :: Int }
263 | InfixN { prec :: Int }
264 | PrefixN { prec :: Int }
265 deriving Eq
266{-!
267deriving instance Binary Fixity
268!-}
270instance Show Fixity where
271 show (Infixl i) = "infixl " ++ show i
272 show (Infixr i) = "infixr " ++ show i
273 show (InfixN i) = "infix " ++ show i
274 show (PrefixN i) = "prefix " ++ show i
276data FixDecl = Fix Fixity String
277 deriving Eq
279instance Show FixDecl where
280 show (Fix f s) = show f ++ " " ++ s
282{-!
283deriving instance Binary FixDecl
284!-}
286instance Ord FixDecl where
287 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
290data Static = Static | Dynamic
291 deriving (Show, Eq)
292{-!
293deriving instance Binary Static
294!-}
296-- Mark bindings with their explicitness, and laziness
297data Plicity = Imp { plazy :: Bool,
298 pstatic :: Static,
299 pdocstr :: String }
300 | Exp { plazy :: Bool,
301 pstatic :: Static,
302 pdocstr :: String }
303 | Constraint { plazy :: Bool,
304 pstatic :: Static,
305 pdocstr :: String }
306 | TacImp { plazy :: Bool,
307 pstatic :: Static,
308 pscript :: PTerm,
309 pdocstr :: String }
310 deriving (Show, Eq)
312{-!
313deriving instance Binary Plicity
314!-}
316impl = Imp False Dynamic ""
317expl = Exp False Dynamic ""
318constraint = Constraint False Dynamic ""
319tacimpl t = TacImp False Dynamic t ""
321data FnOpt = Inlinable | TotalFn | PartialFn
322 | Coinductive | AssertTotal | TCGen
323 | Implicit -- implicit coercion
324 | CExport String -- export, with a C name
325 | Specialise [Name] -- specialise it, freeze these names
326 deriving (Show, Eq)
327{-!
328deriving instance Binary FnOpt
329!-}
331type FnOpts = [FnOpt]
333inlinable :: FnOpts -> Bool
334inlinable = elem Inlinable
336-- | Top-level declarations such as compiler directives, definitions,
337-- datatypes and typeclasses.
338data PDecl' t
339 = PFix FC Fixity [String] -- ^ Fixity declaration
340 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
341 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
342 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
343 | PCAF FC Name t -- ^ Top level constant
344 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
345 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
346 | PNamespace String [PDecl' t] -- ^ New namespace
347 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
348 | PClass String SyntaxInfo FC
349 [t] -- constraints
350 Name
351 [(Name, t)] -- parameters
352 [PDecl' t] -- declarations
353 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
354 -- class name, parameters, method declarations
355 | PInstance SyntaxInfo FC [t] -- constraints
356 Name -- class
357 [t] -- parameters
358 t -- full instance type
359 (Maybe Name) -- explicit name
360 [PDecl' t]
361 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
362 -- class name, parameters, full instance type, optional explicit name, and definitions
363 | PDSL Name (DSL' t) -- ^ DSL declaration
364 | PSyntax FC Syntax -- ^ Syntax definition
365 | PMutual FC [PDecl' t] -- ^ Mutual block
366 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
367 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
368 deriving Functor
369{-!
370deriving instance Binary PDecl'
371!-}
373-- | One clause of a top-level definition. Term arguments to constructors are:
374--
375-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
376--
377-- 2. The list of patterns
378--
379-- 3. The right-hand side
380--
381-- 4. The where block (PDecl' t)
382data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
383 | PWith FC Name t [t] t [PDecl' t]
384 | PClauseR FC [t] t [PDecl' t]
385 | PWithR FC [t] t [PDecl' t]
386 deriving Functor
387{-!
388deriving instance Binary PClause'
389!-}
391-- | Data declaration
392data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
393 d_tcon :: t, -- ^ Type constructor
394 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
395 }
396 -- ^ Data declaration
397 | PLaterdecl { d_name :: Name, d_tcon :: t }
398 -- ^ "Placeholder" for data whose constructors are defined later
399 deriving Functor
400{-!
401deriving instance Binary PData'
402!-}
404-- Handy to get a free function for applying PTerm -> PTerm functions
405-- across a program, by deriving Functor
407type PDecl = PDecl' PTerm
408type PData = PData' PTerm
409type PClause = PClause' PTerm
411-- get all the names declared in a decl
413declared :: PDecl -> [Name]
414declared (PFix _ _ _) = []
415declared (PTy _ _ _ _ n t) = [n]
416declared (PPostulate _ _ _ _ n t) = [n]
417declared (PClauses _ _ n _) = [] -- not a declaration
418declared (PCAF _ n _) = [n]
419declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
420 where fstt (_, a, _, _) = a
421declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
422declared (PParams _ _ ds) = concatMap declared ds
423declared (PNamespace _ ds) = concatMap declared ds
424declared (PRecord _ _ _ n _ _ c _) = [n, c]
425declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
426declared (PInstance _ _ _ _ _ _ _ _) = []
427declared (PDSL n _) = [n]
428declared (PSyntax _ _) = []
429declared (PMutual _ ds) = concatMap declared ds
430declared (PDirective _) = []
432-- get the names declared, not counting nested parameter blocks
433tldeclared :: PDecl -> [Name]
434tldeclared (PFix _ _ _) = []
435tldeclared (PTy _ _ _ _ n t) = [n]
436tldeclared (PPostulate _ _ _ _ n t) = [n]
437tldeclared (PClauses _ _ n _) = [] -- not a declaration
438tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
439tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
440 where fstt (_, a, _, _) = a
441tldeclared (PParams _ _ ds) = []
442tldeclared (PMutual _ ds) = concatMap tldeclared ds
443tldeclared (PNamespace _ ds) = concatMap tldeclared ds
446defined :: PDecl -> [Name]
447defined (PFix _ _ _) = []
448defined (PTy _ _ _ _ n t) = []
449defined (PPostulate _ _ _ _ n t) = []
450defined (PClauses _ _ n _) = [n] -- not a declaration
451defined (PCAF _ n _) = [n]
452defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
453 where fstt (_, a, _, _) = a
454defined (PData _ _ _ _ (PLaterdecl n _)) = []
455defined (PParams _ _ ds) = concatMap defined ds
456defined (PNamespace _ ds) = concatMap defined ds
457defined (PRecord _ _ _ n _ _ c _) = [n, c]
458defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
459defined (PInstance _ _ _ _ _ _ _ _) = []
460defined (PDSL n _) = [n]
461defined (PSyntax _ _) = []
462defined (PMutual _ ds) = concatMap defined ds
463defined (PDirective _) = []
464--defined _ = []
466updateN :: [(Name, Name)] -> Name -> Name
467updateN ns n | Just n' <- lookup n ns = n'
468updateN _ n = n
470updateNs :: [(Name, Name)] -> PTerm -> PTerm
471updateNs [] t = t
472updateNs ns t = mapPT updateRef t
473 where updateRef (PRef fc f) = PRef fc (updateN ns f)
474 updateRef t = t
476-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
477-- updateDNs [] t = t
478-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
479-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
480-- where updateCNs ns (PClause n l ts r ds)
481-- = PClause (updateN ns n) (fmap (updateNs ns) l)
482-- (map (fmap (updateNs ns)) ts)
483-- (fmap (updateNs ns) r)
484-- (map (updateDNs ns) ds)
485-- updateDNs ns c = c
487-- | High level language terms
488data PTerm = PQuote Raw
489 | PRef FC Name
490 | PInferRef FC Name -- ^ A name to be defined later
491 | PPatvar FC Name
492 | PLam Name PTerm PTerm
493 | PPi Plicity Name PTerm PTerm
494 | PLet Name PTerm PTerm PTerm
495 | PTyped PTerm PTerm -- ^ Term with explicit type
496 | PApp FC PTerm [PArg]
497 | PMatchApp FC Name -- ^ Make an application by type matching
498 | PCase FC PTerm [(PTerm, PTerm)]
499 | PTrue FC
500 | PFalse FC
501 | PRefl FC PTerm
502 | PResolveTC FC
503 | PEq FC PTerm PTerm
504 | PRewrite FC PTerm PTerm
505 | PPair FC PTerm PTerm
506 | PDPair FC PTerm PTerm PTerm
507 | PAlternative Bool [PTerm] -- True if only one may work
508 | PHidden PTerm -- ^ Irrelevant or hidden pattern
509 | PType
510 | PConstant Const
511 | Placeholder
512 | PDoBlock [PDo]
513 | PIdiom FC PTerm
514 | PReturn FC
515 | PMetavar Name
516 | PProof [PTactic] -- ^ Proof script
517 | PTactics [PTactic] -- ^ As PProof, but no auto solving
518 | PElabError Err -- ^ Error to report on elaboration
519 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
520 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
521 | PUnifyLog PTerm -- ^ dump a trace of unifications when building term
522 deriving Eq
523{-!
524deriving instance Binary PTerm
525!-}
527mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
528mapPT f t = f (mpt t) where
529 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
530 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
531 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
532 mpt (PRewrite fc t s) = PRewrite fc (mapPT f t) (mapPT f s)
533 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
534 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
535 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
536 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
537 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
538 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
539 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
540 mpt (PHidden t) = PHidden (mapPT f t)
541 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
542 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
543 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
544 mpt (PUnifyLog tm) = PUnifyLog (mapPT f tm)
545 mpt x = x
548data PTactic' t = Intro [Name] | Intros | Focus Name
549 | Refine Name [Bool] | Rewrite t
550 | MatchRefine Name
551 | LetTac Name t | LetTacTy Name t t
552 | Exact t | Compute | Trivial
553 | Solve
554 | Attack
555 | ProofState | ProofTerm | Undo
556 | Try (PTactic' t) (PTactic' t)
557 | TSeq (PTactic' t) (PTactic' t)
558 | ApplyTactic t -- see Language.Reflection module
559 | Reflect t
560 | Fill t
561 | GoalType String (PTactic' t)
562 | Qed | Abandon
563 deriving (Show, Eq, Functor)
564{-!
565deriving instance Binary PTactic'
566!-}
568instance Sized a => Sized (PTactic' a) where
569 size (Intro nms) = 1 + size nms
570 size Intros = 1
571 size (Focus nm) = 1 + size nm
572 size (Refine nm bs) = 1 + size nm + length bs
573 size (Rewrite t) = 1 + size t
574 size (LetTac nm t) = 1 + size nm + size t
575 size (Exact t) = 1 + size t
576 size Compute = 1
577 size Trivial = 1
578 size Solve = 1
579 size Attack = 1
580 size ProofState = 1
581 size ProofTerm = 1
582 size Undo = 1
583 size (Try l r) = 1 + size l + size r
584 size (TSeq l r) = 1 + size l + size r
585 size (ApplyTactic t) = 1 + size t
586 size (Reflect t) = 1 + size t
587 size (Fill t) = 1 + size t
588 size Qed = 1
589 size Abandon = 1
591type PTactic = PTactic' PTerm
593data PDo' t = DoExp FC t
594 | DoBind FC Name t
595 | DoBindP FC t t
596 | DoLet FC Name t t
597 | DoLetP FC t t
598 deriving (Eq, Functor)
599{-!
600deriving instance Binary PDo'
601!-}
603instance Sized a => Sized (PDo' a) where
604 size (DoExp fc t) = 1 + size fc + size t
605 size (DoBind fc nm t) = 1 + size fc + size nm + size t
606 size (DoBindP fc l r) = 1 + size fc + size l + size r
607 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
608 size (DoLetP fc l r) = 1 + size fc + size l + size r
610type PDo = PDo' PTerm
612-- The priority gives a hint as to elaboration order. Best to elaborate
613-- things early which will help give a more concrete type to other
614-- variables, e.g. a before (interpTy a).
615-- TODO: priority no longer serves any purpose, drop it!
617data PArg' t = PImp { priority :: Int,
618 lazyarg :: Bool, pname :: Name, getTm :: t,
619 pargdoc :: String }
620 | PExp { priority :: Int,
621 lazyarg :: Bool, getTm :: t,
622 pargdoc :: String }
623 | PConstraint { priority :: Int,
624 lazyarg :: Bool, getTm :: t,
625 pargdoc :: String }
626 | PTacImplicit { priority :: Int,
627 lazyarg :: Bool, pname :: Name,
628 getScript :: t,
629 getTm :: t,
630 pargdoc :: String }
631 deriving (Show, Eq, Functor)
633instance Sized a => Sized (PArg' a) where
634 size (PImp p l nm trm _) = 1 + size nm + size trm
635 size (PExp p l trm _) = 1 + size trm
636 size (PConstraint p l trm _) = 1 + size trm
637 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
639{-!
640deriving instance Binary PArg'
641!-}
643pimp n t = PImp 0 True n t ""
644pexp t = PExp 0 False t ""
645pconst t = PConstraint 0 False t ""
646ptacimp n s t = PTacImplicit 0 True n s t ""
648type PArg = PArg' PTerm
650-- Type class data
652data ClassInfo = CI { instanceName :: Name,
653 class_methods :: [(Name, (FnOpts, PTerm))],
654 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
655 class_params :: [Name],
656 class_instances :: [Name] }
657 deriving Show
658{-!
659deriving instance Binary ClassInfo
660!-}
662data OptInfo = Optimise { collapsible :: Bool,
663 forceable :: [Int], -- argument positions
664 recursive :: [Int] }
665 deriving Show
666{-!
667deriving instance Binary OptInfo
668!-}
671data TypeInfo = TI { con_names :: [Name],
672 codata :: Bool,
673 param_pos :: [Int] }
674 deriving Show
675{-!
676deriving instance Binary TypeInfo
677!-}
679-- Syntactic sugar info
681data DSL' t = DSL { dsl_bind :: t,
682 dsl_return :: t,
683 dsl_apply :: t,
684 dsl_pure :: t,
685 dsl_var :: Maybe t,
686 index_first :: Maybe t,
687 index_next :: Maybe t,
688 dsl_lambda :: Maybe t,
689 dsl_let :: Maybe t
690 }
691 deriving (Show, Functor)
692{-!
693deriving instance Binary DSL'
694!-}
696type DSL = DSL' PTerm
698data SynContext = PatternSyntax | TermSyntax | AnySyntax
699 deriving Show
700{-!
701deriving instance Binary SynContext
702!-}
704data Syntax = Rule [SSymbol] PTerm SynContext
705 deriving Show
706{-!
707deriving instance Binary Syntax
708!-}
710data SSymbol = Keyword Name
711 | Symbol String
712 | Binding Name
713 | Expr Name
714 | SimpleExpr Name
715 deriving Show
716{-!
717deriving instance Binary SSymbol
718!-}
720initDSL = DSL (PRef f (UN ">>="))
721 (PRef f (UN "return"))
722 (PRef f (UN "<$>"))
723 (PRef f (UN "pure"))
724 Nothing
725 Nothing
726 Nothing
727 Nothing
728 Nothing
729 where f = FC "(builtin)" 0
731data Using = UImplicit Name PTerm
732 | UConstraint Name [Name]
733 deriving (Show, Eq)
734{-!
735deriving instance Binary Using
736!-}
738data SyntaxInfo = Syn { using :: [Using],
739 syn_params :: [(Name, PTerm)],
740 syn_namespace :: [String],
741 no_imp :: [Name],
742 decoration :: Name -> Name,
743 inPattern :: Bool,
744 implicitAllowed :: Bool,
745 dsl_info :: DSL }
746 deriving Show
747{-!
748deriving instance Binary SyntaxInfo
749!-}
751defaultSyntax = Syn [] [] [] [] id False False initDSL
753expandNS :: SyntaxInfo -> Name -> Name
754expandNS syn n@(NS _ _) = n
755expandNS syn n = case syn_namespace syn of
756 [] -> n
757 xs -> NS n xs
760--- Pretty printing declarations and terms
762instance Show PTerm where
763 show tm = showImp False tm
765instance Pretty PTerm where
766 pretty = prettyImp False
768instance Show PDecl where
769 show d = showDeclImp False d
771instance Show PClause where
772 show c = showCImp True c
774instance Show PData where
775 show d = showDImp False d
777showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
778showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
779showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
780showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
781showDeclImp _ (PData _ _ _ _ d) = show d
782showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
783 showSep "\n" (map show ps)
786showCImp :: Bool -> PClause -> String
787showCImp impl (PClause _ n l ws r w)
788 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
789 ++ " where " ++ show w
790 where
791 showWs [] = ""
792 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
793showCImp impl (PWith _ n l ws r w)
794 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
795 ++ " { " ++ show w ++ " } "
796 where
797 showWs [] = ""
798 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
801showDImp :: Bool -> PData -> String
802showDImp impl (PDatadecl n ty cons)
803 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
804 ++ showSep "\n\t| "
805 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
807getImps :: [PArg] -> [(Name, PTerm)]
808getImps [] = []
809getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
810getImps (_ : xs) = getImps xs
812getExps :: [PArg] -> [PTerm]
813getExps [] = []
814getExps (PExp _ _ tm _ : xs) = tm : getExps xs
815getExps (_ : xs) = getExps xs
817getConsts :: [PArg] -> [PTerm]
818getConsts [] = []
819getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
820getConsts (_ : xs) = getConsts xs
822getAll :: [PArg] -> [PTerm]
823getAll = map getTm
825prettyImp :: Bool -> PTerm -> Doc
826prettyImp impl = prettySe 10
827 where
828 prettySe p (PQuote r) =
829 if size r > breakingSize then
830 text "![" $$ pretty r <> text "]"
831 else
832 text "![" <> pretty r <> text "]"
833 prettySe p (PPatvar fc n) = pretty n
834 prettySe p (PRef fc n) =
835 if impl then
836 pretty n
837 else
838 prettyBasic n
839 where
840 prettyBasic n@(UN _) = pretty n
841 prettyBasic (MN _ s) = text s
842 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
843 prettySe p (PLam n ty sc) =
844 bracket p 2 $
845 if size sc > breakingSize then
846 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
847 else
848 text "λ" <> pretty n <+> text "=>" <+> pretty sc
849 prettySe p (PLet n ty v sc) =
850 bracket p 2 $
851 if size sc > breakingSize then
852 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
853 nest nestingSize (prettySe 10 sc)
854 else
855 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
856 prettySe 10 sc
857 prettySe p (PPi (Exp l s _) n ty sc)
858 | n `elem` allNamesIn sc || impl =
859 let open = if l then text "|" <> lparen else lparen in
860 bracket p 2 $
861 if size sc > breakingSize then
862 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
863 st <+> text "->" $+$ prettySe 10 sc
864 else
865 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
866 st <+> text "->" <+> prettySe 10 sc
867 | otherwise =
868 bracket p 2 $
869 if size sc > breakingSize then
870 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
871 else
872 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
873 where
874 st =
875 case s of
876 Static -> text "[static]"
877 _ -> empty
878 prettySe p (PPi (Imp l s _) n ty sc)
879 | impl =
880 let open = if l then text "|" <> lbrace else lbrace in
881 bracket p 2 $
882 if size sc > breakingSize then
883 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
884 st <+> text "->" <+> prettySe 10 sc
885 else
886 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
887 st <+> text "->" <+> prettySe 10 sc
888 | otherwise = prettySe 10 sc
889 where
890 st =
891 case s of
892 Static -> text $ "[static]"
893 _ -> empty
894 prettySe p (PPi (Constraint _ _ _) n ty sc) =
895 bracket p 2 $
896 if size sc > breakingSize then
897 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
898 else
899 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
900 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
901 bracket p 2 $
902 if size sc > breakingSize then
903 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
904 rbrace <+> text "->" $+$ prettySe 10 sc
905 else
906 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
907 rbrace <+> text "->" <+> prettySe 10 sc
908 prettySe p (PApp _ (PRef _ f) [])
909 | not impl = pretty f
910 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
911 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
912 let [l, r] = getExps args in
913 bracket p 1 $
914 if size r > breakingSize then
915 prettySe 1 l <+> pretty op $+$ prettySe 0 r
916 else
917 prettySe 1 l <+> pretty op <+> prettySe 0 r
918 prettySe p (PApp _ f as) =
919 let args = getExps as in
920 bracket p 1 $
921 prettySe 1 f <+>
922 if impl then
923 foldl' fS empty as
924 -- foldr (<+>) empty $ map prettyArgS as
925 else
926 foldl' fSe empty args
927 -- foldr (<+>) empty $ map prettyArgSe args
928 where
929 fS l r =
930 if size r > breakingSize then
931 l $+$ nest nestingSize (prettyArgS r)
932 else
933 l <+> prettyArgS r
935 fSe l r =
936 if size r > breakingSize then
937 l $+$ nest nestingSize (prettyArgSe r)
938 else
939 l <+> prettyArgSe r
940 prettySe p (PCase _ scr opts) =
941 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
942 where
943 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
945 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
946 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
947 prettySe p (PRefl _ _) = text "refl"
948 prettySe p (PResolveTC _) = text "resolvetc"
949 prettySe p (PTrue _) = text "()"
950 prettySe p (PFalse _) = text "_|_"
951 prettySe p (PEq _ l r) =
952 bracket p 2 $
953 if size r > breakingSize then
954 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
955 else
956 prettySe 10 l <+> text "=" <+> prettySe 10 r
957 prettySe p (PRewrite _ l r) =
958 bracket p 2 $
959 if size r > breakingSize then
960 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
961 else
962 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
963 prettySe p (PTyped l r) =
964 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
965 prettySe p (PPair _ l r) =
966 if size r > breakingSize then
967 lparen <> prettySe 10 l <> text "," $+$
968 prettySe 10 r <> rparen
969 else
970 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
971 prettySe p (PDPair _ l t r) =
972 if size r > breakingSize then
973 lparen <> prettySe 10 l <+> text "**" $+$
974 prettySe 10 r <> rparen
975 else
976 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
977 prettySe p (PAlternative a as) =
978 lparen <> text "|" <> prettyAs <> text "|" <> rparen
979 where
980 prettyAs =
981 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
982 prettySe p PType = text "Type"
983 prettySe p (PConstant c) = pretty c
984 -- XXX: add pretty for tactics
985 prettySe p (PProof ts) =
986 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
987 prettySe p (PTactics ts) =
988 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
989 prettySe p (PMetavar n) = text "?" <> pretty n
990 prettySe p (PReturn f) = text "return"
991 prettySe p PImpossible = text "impossible"
992 prettySe p Placeholder = text "_"
993 prettySe p (PDoBlock _) = text "do block pretty not implemented"
994 prettySe p (PElabError s) = pretty s
996 prettySe p _ = text "test"
998 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
999 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
1000 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
1001 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
1003 prettyArgSe arg = prettySe 0 arg
1004 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1005 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
1006 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
1008 bracket outer inner doc
1009 | inner > outer = lparen <> doc <> rparen
1010 | otherwise = doc
1012showImp :: Bool -> PTerm -> String
1013showImp impl tm = se 10 tm where
1014 se p (PQuote r) = "![" ++ show r ++ "]"
1015 se p (PPatvar fc n) = if impl then show n ++ "[p]" else show n
1016 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
1017 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
1018 else showbasic n
1019 where showbasic n@(UN _) = show n
1020 showbasic (MN _ s) = s
1021 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
1022 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
1023 (if impl then " : " ++ se 10 ty else "") ++ " => "
1024 ++ se 10 sc
1025 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
1026 " in " ++ se 10 sc
1027 se p (PPi (Exp l s _) n ty sc)
1028 | n `elem` allNamesIn sc || impl
1029 = bracket p 2 $
1030 (if l then "|(" else "(") ++
1031 show n ++ " : " ++ se 10 ty ++
1032 ") " ++ st ++
1033 "-> " ++ se 10 sc
1034 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
1035 where st = case s of
1036 Static -> "[static] "
1037 _ -> ""
1038 se p (PPi (Imp l s _) n ty sc)
1039 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1040 show n ++ " : " ++ se 10 ty ++
1041 "} " ++ st ++ "-> " ++ se 10 sc
1042 | otherwise = se 10 sc
1043 where st = case s of
1044 Static -> "[static] "
1045 _ -> ""
1046 se p (PPi (Constraint _ _ _) n ty sc)
1047 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
1048 se p (PPi (TacImp _ _ s _) n ty sc)
1049 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
1050 se p e
1051 | Just str <- slist p e = str
1052 | Just num <- snat p e = show num
1053 se p (PMatchApp _ f) = "match " ++ show f
1054 se p (PApp _ (PRef _ f) [])
1055 | not impl = show f
1056 se p (PApp _ (PRef _ op@(UN (f:_))) args)
1057 | length (getExps args) == 2 && not impl && not (isAlpha f)
1058 = let [l, r] = getExps args in
1059 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
1060 se p (PApp _ f as)
1061 = let args = getExps as in
1062 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
1063 else concatMap seArg args
1064 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
1065 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
1066 se p (PHidden tm) = "." ++ se 0 tm
1067 se p (PRefl _ t)
1068 | not impl = "refl"
1069 | otherwise = "refl {" ++ se 10 t ++ "}"
1070 se p (PResolveTC _) = "resolvetc"
1071 se p (PTrue _) = "()"
1072 se p (PFalse _) = "_|_"
1073 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
1074 se p (PRewrite _ l r) = bracket p 2 $ "rewrite " ++ se 10 l ++ " in " ++ se 10 r
1075 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
1076 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
1077 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
1078 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
1079 se p PType = "Type"
1080 se p (PConstant c) = show c
1081 se p (PProof ts) = "proof { " ++ show ts ++ "}"
1082 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
1083 se p (PMetavar n) = "?" ++ show n
1084 se p (PReturn f) = "return"
1085 se p PImpossible = "impossible"
1086 se p Placeholder = "_"
1087 se p (PDoBlock _) = "do block show not implemented"
1088 se p (PElabError s) = show s
1089 se p (PCoerced t) = se p t
1090 se p (PUnifyLog t) = "%unifyLog " ++ se p t
1091-- se p x = "Not implemented"
1093 slist' p (PApp _ (PRef _ nil) _)
1094 | nsroot nil == UN "Nil" = Just []
1095 slist' p (PApp _ (PRef _ cons) args)
1096 | nsroot cons == UN "::",
1097 (PExp {getTm=tl}):(PExp {getTm=hd}):imps <- reverse args,
1098 all isImp imps,
1099 Just tl' <- slist' p tl
1100 = Just (hd:tl')
1101 where
1102 isImp (PImp {}) = True
1103 isImp _ = False
1104 slist' _ _ = Nothing
1106 slist p e | Just es <- slist' p e = Just $
1107 case es of [] -> "[]"
1108 [x] -> "[" ++ se p x ++ "]"
1109 xs -> "[" ++ intercalate "," (map (se p) xs) ++ "]"
1110 slist _ _ = Nothing
1112 -- since Prelude is always imported, S & O are unqualified iff they're the
1113 -- Nat ones.
1114 snat p (PRef _ o)
1115 | show o == (natns++"O") || show o == "O" = Just 0
1116 snat p (PApp _ s [PExp {getTm=n}])
1117 | show s == (natns++"S") || show s == "S",
1118 Just n' <- snat p n
1119 = Just $ 1 + n'
1120 snat _ _ = Nothing
1122 natns = "Prelude.Nat."
1124 sArg (PImp _ _ n tm _) = siArg (n, tm)
1125 sArg (PExp _ _ tm _) = seArg tm
1126 sArg (PConstraint _ _ tm _) = scArg tm
1127 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
1129 seArg arg = " " ++ se 0 arg
1130 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1131 scArg val = " {{" ++ se 10 val ++ "}}"
1132 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1134 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1135 | otherwise = str
1138instance Sized PTerm where
1139 size (PQuote rawTerm) = size rawTerm
1140 size (PRef fc name) = size name
1141 size (PLam name ty bdy) = 1 + size ty + size bdy
1142 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1143 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1144 size (PTyped trm ty) = 1 + size trm + size ty
1145 size (PApp fc name args) = 1 + size args
1146 size (PCase fc trm bdy) = 1 + size trm + size bdy
1147 size (PTrue fc) = 1
1148 size (PFalse fc) = 1
1149 size (PRefl fc _) = 1
1150 size (PResolveTC fc) = 1
1151 size (PEq fc left right) = 1 + size left + size right
1152 size (PRewrite fc left right) = 1 + size left + size right
1153 size (PPair fc left right) = 1 + size left + size right
1154 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1155 size (PAlternative a alts) = 1 + size alts
1156 size (PHidden hidden) = size hidden
1157 size (PUnifyLog tm) = size tm
1158 size PType = 1
1159 size (PConstant const) = 1 + size const
1160 size Placeholder = 1
1161 size (PDoBlock dos) = 1 + size dos
1162 size (PIdiom fc term) = 1 + size term
1163 size (PReturn fc) = 1
1164 size (PMetavar name) = 1
1165 size (PProof tactics) = size tactics
1166 size (PElabError err) = size err
1167 size PImpossible = 1
1169getPArity :: PTerm -> Int
1170getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1171getPArity _ = 0
1173-- Return all names, free or globally bound, in the given term.
1175allNamesIn :: PTerm -> [Name]
1176allNamesIn tm = nub $ ni [] tm
1177 where
1178 ni env (PRef _ n)
1179 | not (n `elem` env) = [n]
1180 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1181 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1182 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1183 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1184 ni env (PHidden tm) = ni env tm
1185 ni env (PEq _ l r) = ni env l ++ ni env r
1186 ni env (PRewrite _ l r) = ni env l ++ ni env r
1187 ni env (PTyped l r) = ni env l ++ ni env r
1188 ni env (PPair _ l r) = ni env l ++ ni env r
1189 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1190 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1191 ni env (PAlternative a ls) = concatMap (ni env) ls
1192 ni env (PUnifyLog tm) = ni env tm
1193 ni env _ = []
1195-- Return names which are free in the given term.
1197namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1198namesIn uvars ist tm = nub $ ni [] tm
1199 where
1200 ni env (PRef _ n)
1201 | not (n `elem` env)
1202 = case lookupTy n (tt_ctxt ist) of
1203 [] -> [n]
1204 _ -> if n `elem` (map fst uvars) then [n] else []
1205 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1206 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1207 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1208 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1209 ni env (PEq _ l r) = ni env l ++ ni env r
1210 ni env (PRewrite _ l r) = ni env l ++ ni env r
1211 ni env (PTyped l r) = ni env l ++ ni env r
1212 ni env (PPair _ l r) = ni env l ++ ni env r
1213 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1214 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1215 ni env (PAlternative a as) = concatMap (ni env) as
1216 ni env (PHidden tm) = ni env tm
1217 ni env (PUnifyLog tm) = ni env tm
1218 ni env _ = []
1220-- Return which of the given names are used in the given term.
1222usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1223usedNamesIn vars ist tm = nub $ ni [] tm
1224 where
1225 ni env (PRef _ n)
1226 | n `elem` vars && not (n `elem` env)
1227 = case lookupTy n (tt_ctxt ist) of
1228 [] -> [n]
1229 _ -> []
1230 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1231 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1232 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1233 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1234 ni env (PEq _ l r) = ni env l ++ ni env r
1235 ni env (PRewrite _ l r) = ni env l ++ ni env r
1236 ni env (PTyped l r) = ni env l ++ ni env r
1237 ni env (PPair _ l r) = ni env l ++ ni env r
1238 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1239 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1240 ni env (PAlternative a as) = concatMap (ni env) as
1241 ni env (PHidden tm) = ni env tm
1242 ni env (PUnifyLog tm) = ni env tm
1243 ni env _ = []