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