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