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