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