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