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