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