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