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