on 592d6c0 help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1014 lines 35 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 Util.Pretty
13import Paths_idris
15import System.Console.Haskeline
17import Control.Monad.Trans.State.Strict
19import Data.List
20import Data.Char
21import Data.Either
23import Debug.Trace
25data IOption = IOption { opt_logLevel :: Int,
26 opt_typecase :: Bool,
27 opt_typeintype :: Bool,
28 opt_coverage :: Bool,
29 opt_showimp :: Bool,
30 opt_errContext :: Bool,
31 opt_repl :: Bool,
32 opt_verbose :: Bool,
33 opt_ibcsubdir :: FilePath,
34 opt_importdirs :: [FilePath],
35 opt_cmdline :: [Opt] -- remember whole command line
36 }
37 deriving (Show, Eq)
39defaultOpts = IOption 0 False False True False False True True "" [] []
41-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
42-- without typechecking).
43-- This will include all the functions and data declarations, plus fixity declarations
44-- and syntax macros.
46data IState = IState {
47 tt_ctxt :: Context,
48 idris_constraints :: [(UConstraint, FC)],
49 idris_infixes :: [FixDecl],
50 idris_implicits :: Ctxt [PArg],
51 idris_statics :: Ctxt [Bool],
52 idris_classes :: Ctxt ClassInfo,
53 idris_dsls :: Ctxt DSL,
54 idris_optimisation :: Ctxt OptInfo,
55 idris_datatypes :: Ctxt TypeInfo,
56 idris_patdefs :: Ctxt [([Name], Term, Term)], -- not exported
57 idris_flags :: Ctxt [FnOpt],
58 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
59 idris_calledgraph :: Ctxt [Name],
60 idris_totcheck :: [(FC, Name)],
61 idris_log :: String,
62 idris_options :: IOption,
63 idris_name :: Int,
64 idris_metavars :: [Name],
65 syntax_rules :: [Syntax],
66 syntax_keywords :: [String],
67 imported :: [FilePath],
68 idris_scprims :: [(Name, (Int, PrimFn))],
69 idris_objs :: [FilePath],
70 idris_libs :: [String],
71 idris_hdrs :: [String],
72 proof_list :: [(Name, [String])],
73 errLine :: Maybe Int,
74 lastParse :: Maybe Name,
75 indent_stack :: [Int],
76 brace_stack :: [Maybe Int],
77 hide_list :: [(Name, Maybe Accessibility)],
78 default_access :: Accessibility,
79 default_total :: Bool,
80 ibc_write :: [IBCWrite],
81 compiled_so :: Maybe String
82 }
84data SizeChange = Smaller | Same | Bigger | Unknown
85 deriving (Show, Eq)
86{-!
87deriving instance Binary SizeChange
88!-}
90type SCGEntry = (Name, [Maybe (Int, SizeChange)])
92data CGInfo = CGInfo { argsdef :: [Name],
93 calls :: [(Name, [[Name]])],
94 scg :: [SCGEntry],
95 argsused :: [Name],
96 unusedpos :: [Int] }
97 deriving Show
98{-!
99deriving instance Binary CGInfo
100!-}
102primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
104-- information that needs writing for the current module's .ibc file
105data IBCWrite = IBCFix FixDecl
106 | IBCImp Name
107 | IBCStatic Name
108 | IBCClass Name
109 | IBCInstance Bool Name Name
110 | IBCDSL Name
111 | IBCData Name
112 | IBCOpt Name
113 | IBCSyntax Syntax
114 | IBCKeyword String
115 | IBCImport FilePath
116 | IBCObj FilePath
117 | IBCLib String
118 | IBCHeader String
119 | IBCAccess Name Accessibility
120 | IBCTotal Name Totality
121 | IBCFlags Name [FnOpt]
122 | IBCCG Name
123 | IBCDef Name -- i.e. main context
124 deriving Show
126idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
127 emptyContext emptyContext emptyContext emptyContext
128 emptyContext emptyContext emptyContext
129 [] "" defaultOpts 6 [] [] [] [] [] [] [] []
130 [] Nothing Nothing [] [] [] Hidden False [] Nothing
132-- The monad for the main REPL - reading and processing files and updating
133-- global state (hence the IO inner monad).
134type Idris = StateT IState (InputT IO)
136-- Commands in the REPL
138data Target = ViaC | ViaJava
140data Command = Quit
141 | Help
142 | Eval PTerm
143 | Check PTerm
144 | TotCheck Name
145 | Reload
146 | Load FilePath
147 | ModImport String
148 | Edit
149 | Compile Target String
150 | Execute
151 | ExecVal PTerm
152 | NewCompile String
153 | Metavars
154 | Prove Name
155 | AddProof (Maybe Name)
156 | RmProof Name
157 | ShowProof Name
158 | Proofs
159 | Universes
160 | TTShell
161 | LogLvl Int
162 | Spec PTerm
163 | HNF PTerm
164 | Defn Name
165 | Info Name
166 | Missing Name
167 | Pattelab PTerm
168 | DebugInfo Name
169 | Search PTerm
170 | SetOpt Opt
171 | UnsetOpt Opt
172 | NOP
174data Opt = Filename String
175 | Ver
176 | Usage
177 | ShowLibs
178 | ShowLibdir
179 | ShowIncs
180 | NoPrelude
181 | NoREPL
182 | OLogging Int
183 | Output String
184 | NewOutput String
185 | TypeCase
186 | TypeInType
187 | DefaultTotal
188 | DefaultPartial
189 | WarnPartial
190 | NoCoverage
191 | ErrContext
192 | ShowImpl
193 | Verbose
194 | IBCSubDir String
195 | ImportDir String
196 | PkgBuild String
197 | PkgInstall String
198 | PkgClean String
199 | WarnOnly
200 | Pkg String
201 | BCAsm String
202 | DumpC String
203 | DumpDefun String
204 | DumpCases String
205 | FOVM String
206 deriving (Show, Eq)
209-- Parsed declarations
211data Fixity = Infixl { prec :: Int }
212 | Infixr { prec :: Int }
213 | InfixN { prec :: Int }
214 | PrefixN { prec :: Int }
215 deriving Eq
216{-!
217deriving instance Binary Fixity
218!-}
220instance Show Fixity where
221 show (Infixl i) = "infixl " ++ show i
222 show (Infixr i) = "infixr " ++ show i
223 show (InfixN i) = "infix " ++ show i
224 show (PrefixN i) = "prefix " ++ show i
226data FixDecl = Fix Fixity String
227 deriving (Show, Eq)
228{-!
229deriving instance Binary FixDecl
230!-}
232instance Ord FixDecl where
233 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
236data Static = Static | Dynamic
237 deriving (Show, Eq)
238{-!
239deriving instance Binary Static
240!-}
242-- Mark bindings with their explicitness, and laziness
243data Plicity = Imp { plazy :: Bool,
244 pstatic :: Static }
245 | Exp { plazy :: Bool,
246 pstatic :: Static }
247 | Constraint { plazy :: Bool,
248 pstatic :: Static }
249 | TacImp { plazy :: Bool,
250 pstatic :: Static,
251 pscript :: PTerm }
252 deriving (Show, Eq)
254{-!
255deriving instance Binary Plicity
256!-}
258impl = Imp False Dynamic
259expl = Exp False Dynamic
260constraint = Constraint False Dynamic
261tacimpl = TacImp False Dynamic
263data FnOpt = Inlinable | TotalFn | PartialFn
264 | Coinductive | AssertTotal | TCGen
265 | CExport String -- export, with a C name
266 | Specialise [Name] -- specialise it, freeze these names
267 deriving (Show, Eq)
268{-!
269deriving instance Binary FnOpt
270!-}
272type FnOpts = [FnOpt]
274inlinable :: FnOpts -> Bool
275inlinable = elem Inlinable
277data PDecl' t = PFix FC Fixity [String] -- fixity declaration
278 | PTy SyntaxInfo FC FnOpts Name t -- type declaration
279 | PClauses FC FnOpts Name [PClause' t] -- pattern clause
280 | PCAF FC Name t -- top level constant
281 | PData SyntaxInfo FC Bool -- codata
282 (PData' t) -- data declaration
283 | PParams FC [(Name, t)] [PDecl' t] -- params block
284 | PNamespace String [PDecl' t] -- new namespace
285 | PRecord SyntaxInfo FC Name t Name t -- record declaration
286 | PClass SyntaxInfo FC
287 [t] -- constraints
288 Name
289 [(Name, t)] -- parameters
290 [PDecl' t] -- declarations
291 | PInstance SyntaxInfo FC [t] -- constraints
292 Name -- class
293 [t] -- parameters
294 t -- full instance type
295 (Maybe Name) -- explicit name
296 [PDecl' t]
297 | PDSL Name (DSL' t)
298 | PSyntax FC Syntax
299 | PDirective (Idris ())
300 deriving Functor
301{-!
302deriving instance Binary PDecl'
303!-}
305data PClause' t = PClause FC Name t [t] t [PDecl' t]
306 | PWith FC Name t [t] t [PDecl' t]
307 | PClauseR FC [t] t [PDecl' t]
308 | PWithR FC [t] t [PDecl' t]
309 deriving Functor
310{-!
311deriving instance Binary PClause'
312!-}
314data PData' t = PDatadecl { d_name :: Name,
315 d_tcon :: t,
316 d_cons :: [(Name, t, FC)] }
317 deriving Functor
318{-!
319deriving instance Binary PData'
320!-}
322-- Handy to get a free function for applying PTerm -> PTerm functions
323-- across a program, by deriving Functor
325type PDecl = PDecl' PTerm
326type PData = PData' PTerm
327type PClause = PClause' PTerm
329-- get all the names declared in a decl
331declared :: PDecl -> [Name]
332declared (PFix _ _ _) = []
333declared (PTy _ _ _ n t) = [n]
334declared (PClauses _ _ n _) = [] -- not a declaration
335declared (PData _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
336 where fstt (a, _, _) = a
337declared (PParams _ _ ds) = concatMap declared ds
338declared (PNamespace _ ds) = concatMap declared ds
339-- declared (PImport _) = []
341defined :: PDecl -> [Name]
342defined (PFix _ _ _) = []
343defined (PTy _ _ _ n t) = []
344defined (PClauses _ _ n _) = [n] -- not a declaration
345defined (PData _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
346 where fstt (a, _, _) = a
347defined (PParams _ _ ds) = concatMap defined ds
348defined (PNamespace _ ds) = concatMap defined ds
349-- declared (PImport _) = []
351updateN :: [(Name, Name)] -> Name -> Name
352updateN ns n | Just n' <- lookup n ns = n'
353updateN _ n = n
355updateNs :: [(Name, Name)] -> PTerm -> PTerm
356updateNs [] t = t
357updateNs ns t = mapPT updateRef t
358 where updateRef (PRef fc f) = PRef fc (updateN ns f)
359 updateRef t = t
361-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
362-- updateDNs [] t = t
363-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
364-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
365-- where updateCNs ns (PClause n l ts r ds)
366-- = PClause (updateN ns n) (fmap (updateNs ns) l)
367-- (map (fmap (updateNs ns)) ts)
368-- (fmap (updateNs ns) r)
369-- (map (updateDNs ns) ds)
370-- updateDNs ns c = c
372-- High level language terms
374data PTerm = PQuote Raw
375 | PRef FC Name
376 | PPatvar FC Name
377 | PLam Name PTerm PTerm
378 | PPi Plicity Name PTerm PTerm
379 | PLet Name PTerm PTerm PTerm
380 | PTyped PTerm PTerm -- term with explicit type
381 | PApp FC PTerm [PArg]
382 | PCase FC PTerm [(PTerm, PTerm)]
383 | PTrue FC
384 | PFalse FC
385 | PRefl FC PTerm
386 | PResolveTC FC
387 | PEq FC PTerm PTerm
388 | PPair FC PTerm PTerm
389 | PDPair FC PTerm PTerm PTerm
390 | PAlternative Bool [PTerm] -- True if only one may work
391 | PHidden PTerm -- irrelevant or hidden pattern
392 | PSet
393 | PConstant Const
394 | Placeholder
395 | PDoBlock [PDo]
396 | PIdiom FC PTerm
397 | PReturn FC
398 | PMetavar Name
399 | PProof [PTactic]
400 | PTactics [PTactic] -- as PProof, but no auto solving
401 | PElabError Err -- error to report on elaboration
402 | PImpossible -- special case for declaring when an LHS can't typecheck
403 deriving Eq
404{-!
405deriving instance Binary PTerm
406!-}
408mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
409mapPT f t = f (mpt t) where
410 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
411 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
412 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
413 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
414 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
415 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
416 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
417 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
418 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
419 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
420 mpt (PHidden t) = PHidden (mapPT f t)
421 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
422 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
423 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
424 mpt x = x
427data PTactic' t = Intro [Name] | Intros | Focus Name
428 | Refine Name [Bool] | Rewrite t | LetTac Name t
429 | Exact t | Compute | Trivial
430 | Solve
431 | Attack
432 | ProofState | ProofTerm | Undo
433 | Try (PTactic' t) (PTactic' t)
434 | TSeq (PTactic' t) (PTactic' t)
435 | Qed | Abandon
436 deriving (Show, Eq, Functor)
437{-!
438deriving instance Binary PTactic'
439!-}
441instance Sized a => Sized (PTactic' a) where
442 size (Intro nms) = 1 + size nms
443 size Intros = 1
444 size (Focus nm) = 1 + size nm
445 size (Refine nm bs) = 1 + size nm + length bs
446 size (Rewrite t) = 1 + size t
447 size (LetTac nm t) = 1 + size nm + size t
448 size (Exact t) = 1 + size t
449 size Compute = 1
450 size Trivial = 1
451 size Solve = 1
452 size Attack = 1
453 size ProofState = 1
454 size ProofTerm = 1
455 size Undo = 1
456 size (Try l r) = 1 + size l + size r
457 size (TSeq l r) = 1 + size l + size r
458 size Qed = 1
459 size Abandon = 1
461type PTactic = PTactic' PTerm
463data PDo' t = DoExp FC t
464 | DoBind FC Name t
465 | DoBindP FC t t
466 | DoLet FC Name t t
467 | DoLetP FC t t
468 deriving (Eq, Functor)
469{-!
470deriving instance Binary PDo'
471!-}
473instance Sized a => Sized (PDo' a) where
474 size (DoExp fc t) = 1 + size fc + size t
475 size (DoBind fc nm t) = 1 + size fc + size nm + size t
476 size (DoBindP fc l r) = 1 + size fc + size l + size r
477 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
478 size (DoLetP fc l r) = 1 + size fc + size l + size r
480type PDo = PDo' PTerm
482-- The priority gives a hint as to elaboration order. Best to elaborate
483-- things early which will help give a more concrete type to other
484-- variables, e.g. a before (interpTy a).
486data PArg' t = PImp { priority :: Int,
487 lazyarg :: Bool, pname :: Name, getTm :: t }
488 | PExp { priority :: Int,
489 lazyarg :: Bool, getTm :: t }
490 | PConstraint { priority :: Int,
491 lazyarg :: Bool, getTm :: t }
492 | PTacImplicit { priority :: Int,
493 lazyarg :: Bool, pname :: Name,
494 getScript :: t,
495 getTm :: t }
496 deriving (Show, Eq, Functor)
498instance Sized a => Sized (PArg' a) where
499 size (PImp p l nm trm) = 1 + size nm + size trm
500 size (PExp p l trm) = 1 + size trm
501 size (PConstraint p l trm) = 1 + size trm
502 size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
504{-!
505deriving instance Binary PArg'
506!-}
508pimp = PImp 0 True
509pexp = PExp 0 False
510pconst = PConstraint 0 False
511ptacimp = PTacImplicit 0 True
513type PArg = PArg' PTerm
515-- Type class data
517data ClassInfo = CI { instanceName :: Name,
518 class_methods :: [(Name, (FnOpts, PTerm))],
519 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
520 class_params :: [Name],
521 class_instances :: [Name] }
522 deriving Show
523{-!
524deriving instance Binary ClassInfo
525!-}
527data OptInfo = Optimise { collapsible :: Bool,
528 forceable :: [Int], -- argument positions
529 recursive :: [Int] }
530 deriving Show
531{-!
532deriving instance Binary OptInfo
533!-}
536data TypeInfo = TI { con_names :: [Name], codata :: Bool }
537 deriving Show
538{-!
539deriving instance Binary TypeInfo
540!-}
542-- Syntactic sugar info
544data DSL' t = DSL { dsl_bind :: t,
545 dsl_return :: t,
546 dsl_apply :: t,
547 dsl_pure :: t,
548 dsl_var :: Maybe t,
549 index_first :: Maybe t,
550 index_next :: Maybe t,
551 dsl_lambda :: Maybe t,
552 dsl_let :: Maybe t
553 }
554 deriving (Show, Functor)
555{-!
556deriving instance Binary DSL'
557!-}
559type DSL = DSL' PTerm
561data SynContext = PatternSyntax | TermSyntax | AnySyntax
562 deriving Show
563{-!
564deriving instance Binary SynContext
565!-}
567data Syntax = Rule [SSymbol] PTerm SynContext
568 deriving Show
569{-!
570deriving instance Binary Syntax
571!-}
573data SSymbol = Keyword Name
574 | Symbol String
575 | Binding Name
576 | Expr Name
577 | SimpleExpr Name
578 deriving Show
579{-!
580deriving instance Binary SSymbol
581!-}
583initDSL = DSL (PRef f (UN ">>="))
584 (PRef f (UN "return"))
585 (PRef f (UN "<$>"))
586 (PRef f (UN "pure"))
587 Nothing
588 Nothing
589 Nothing
590 Nothing
591 Nothing
592 where f = FC "(builtin)" 0
594data SyntaxInfo = Syn { using :: [(Name, PTerm)],
595 syn_params :: [(Name, PTerm)],
596 syn_namespace :: [String],
597 no_imp :: [Name],
598 decoration :: Name -> Name,
599 inPattern :: Bool,
600 implicitAllowed :: Bool,
601 dsl_info :: DSL }
602 deriving Show
603{-!
604deriving instance Binary SyntaxInfo
605!-}
607defaultSyntax = Syn [] [] [] [] id False False initDSL
609expandNS :: SyntaxInfo -> Name -> Name
610expandNS syn n@(NS _ _) = n
611expandNS syn n = case syn_namespace syn of
612 [] -> n
613 xs -> NS n xs
616--- Pretty printing declarations and terms
618instance Show PTerm where
619 show tm = showImp False tm
621instance Pretty PTerm where
622 pretty = prettyImp False
624instance Show PDecl where
625 show d = showDeclImp False d
627instance Show PClause where
628 show c = showCImp True c
630instance Show PData where
631 show d = showDImp False d
633showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
634showDeclImp t (PTy _ _ _ n ty) = show n ++ " : " ++ showImp t ty
635showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
636showDeclImp _ (PData _ _ _ d) = show d
637showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
638 showSep "\n" (map show ps)
641showCImp :: Bool -> PClause -> String
642showCImp impl (PClause _ n l ws r w)
643 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
644 ++ " where " ++ show w
645 where
646 showWs [] = ""
647 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
648showCImp impl (PWith _ n l ws r w)
649 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
650 ++ " { " ++ show w ++ " } "
651 where
652 showWs [] = ""
653 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
656showDImp :: Bool -> PData -> String
657showDImp impl (PDatadecl n ty cons)
658 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
659 ++ showSep "\n\t| "
660 (map (\ (n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
662getImps :: [PArg] -> [(Name, PTerm)]
663getImps [] = []
664getImps (PImp _ _ n tm : xs) = (n, tm) : getImps xs
665getImps (_ : xs) = getImps xs
667getExps :: [PArg] -> [PTerm]
668getExps [] = []
669getExps (PExp _ _ tm : xs) = tm : getExps xs
670getExps (_ : xs) = getExps xs
672getConsts :: [PArg] -> [PTerm]
673getConsts [] = []
674getConsts (PConstraint _ _ tm : xs) = tm : getConsts xs
675getConsts (_ : xs) = getConsts xs
677getAll :: [PArg] -> [PTerm]
678getAll = map getTm
680prettyImp :: Bool -> PTerm -> Doc
681prettyImp impl = prettySe 10
682 where
683 prettySe p (PQuote r) =
684 if size r > breakingSize then
685 text "![" $$ pretty r <> text "]"
686 else
687 text "![" <> pretty r <> text "]"
688 prettySe p (PPatvar fc n) = pretty n
689 prettySe p (PRef fc n) =
690 if impl then
691 pretty n
692 else
693 prettyBasic n
694 where
695 prettyBasic n@(UN _) = pretty n
696 prettyBasic (MN _ s) = text s
697 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
698 prettySe p (PLam n ty sc) =
699 bracket p 2 $
700 if size sc > breakingSize then
701 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
702 else
703 text "λ" <> pretty n <+> text "=>" <+> pretty sc
704 prettySe p (PLet n ty v sc) =
705 bracket p 2 $
706 if size sc > breakingSize then
707 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
708 nest nestingSize (prettySe 10 sc)
709 else
710 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
711 prettySe 10 sc
712 prettySe p (PPi (Exp l s) n ty sc)
713 | n `elem` allNamesIn sc || impl =
714 let open = if l then text "|" <> lparen else lparen in
715 bracket p 2 $
716 if size sc > breakingSize then
717 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
718 st <+> text "->" $+$ prettySe 10 sc
719 else
720 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
721 st <+> text "->" <+> prettySe 10 sc
722 | otherwise =
723 bracket p 2 $
724 if size sc > breakingSize then
725 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
726 else
727 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
728 where
729 st =
730 case s of
731 Static -> text "[static]"
732 _ -> empty
733 prettySe p (PPi (Imp l s) n ty sc)
734 | impl =
735 let open = if l then text "|" <> lbrace else lbrace in
736 bracket p 2 $
737 if size sc > breakingSize then
738 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
739 st <+> text "->" <+> prettySe 10 sc
740 else
741 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
742 st <+> text "->" <+> prettySe 10 sc
743 | otherwise = prettySe 10 sc
744 where
745 st =
746 case s of
747 Static -> text $ "[static]"
748 _ -> empty
749 prettySe p (PPi (Constraint _ _) n ty sc) =
750 bracket p 2 $
751 if size sc > breakingSize then
752 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
753 else
754 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
755 prettySe p (PPi (TacImp _ _ s) n ty sc) =
756 bracket p 2 $
757 if size sc > breakingSize then
758 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
759 rbrace <+> text "->" $+$ prettySe 10 sc
760 else
761 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
762 rbrace <+> text "->" <+> prettySe 10 sc
763 prettySe p (PApp _ (PRef _ f) [])
764 | not impl = pretty f
765 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
766 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
767 let [l, r] = getExps args in
768 bracket p 1 $
769 if size r > breakingSize then
770 prettySe 1 l <+> pretty op $+$ prettySe 0 r
771 else
772 prettySe 1 l <+> pretty op <+> prettySe 0 r
773 prettySe p (PApp _ f as) =
774 let args = getExps as in
775 bracket p 1 $
776 prettySe 1 f <+>
777 if impl then
778 foldl fS empty as
779 -- foldr (<+>) empty $ map prettyArgS as
780 else
781 foldl fSe empty args
782 -- foldr (<+>) empty $ map prettyArgSe args
783 where
784 fS l r =
785 if size r > breakingSize then
786 l $+$ nest nestingSize (prettyArgS r)
787 else
788 l <+> prettyArgS r
790 fSe l r =
791 if size r > breakingSize then
792 l $+$ nest nestingSize (prettyArgSe r)
793 else
794 l <+> prettyArgSe r
795 prettySe p (PCase _ scr opts) =
796 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
797 where
798 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
800 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
801 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
802 prettySe p (PRefl _ _) = text "refl"
803 prettySe p (PResolveTC _) = text "resolvetc"
804 prettySe p (PTrue _) = text "()"
805 prettySe p (PFalse _) = text "_|_"
806 prettySe p (PEq _ l r) =
807 bracket p 2 $
808 if size r > breakingSize then
809 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
810 else
811 prettySe 10 l <+> text "=" <+> prettySe 10 r
812 prettySe p (PTyped l r) =
813 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
814 prettySe p (PPair _ l r) =
815 if size r > breakingSize then
816 lparen <> prettySe 10 l <> text "," $+$
817 prettySe 10 r <> rparen
818 else
819 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
820 prettySe p (PDPair _ l t r) =
821 if size r > breakingSize then
822 lparen <> prettySe 10 l <+> text "**" $+$
823 prettySe 10 r <> rparen
824 else
825 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
826 prettySe p (PAlternative a as) =
827 lparen <> text "|" <> prettyAs <> text "|" <> rparen
828 where
829 prettyAs =
830 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
831 prettySe p PSet = text "Set"
832 prettySe p (PConstant c) = pretty c
833 -- XXX: add pretty for tactics
834 prettySe p (PProof ts) =
835 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
836 prettySe p (PTactics ts) =
837 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
838 prettySe p (PMetavar n) = text "?" <> pretty n
839 prettySe p (PReturn f) = text "return"
840 prettySe p PImpossible = text "impossible"
841 prettySe p Placeholder = text "_"
842 prettySe p (PDoBlock _) = text "do block pretty not implemented"
843 prettySe p (PElabError s) = pretty s
845 prettySe p _ = text "test"
847 prettyArgS (PImp _ _ n tm) = prettyArgSi (n, tm)
848 prettyArgS (PExp _ _ tm) = prettyArgSe tm
849 prettyArgS (PConstraint _ _ tm) = prettyArgSc tm
850 prettyArgS (PTacImplicit _ _ n _ tm) = prettyArgSti (n, tm)
852 prettyArgSe arg = prettySe 0 arg
853 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
854 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
855 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
857 bracket outer inner doc
858 | inner > outer = lparen <> doc <> rparen
859 | otherwise = doc
861showImp :: Bool -> PTerm -> String
862showImp impl tm = se 10 tm where
863 se p (PQuote r) = "![" ++ show r ++ "]"
864 se p (PPatvar fc n) = show n
865 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
866 else showbasic n
867 where showbasic n@(UN _) = show n
868 showbasic (MN _ s) = s
869 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
870 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
871 (if impl then " : " ++ se 10 ty else "") ++ " => "
872 ++ se 10 sc
873 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
874 " in " ++ se 10 sc
875 se p (PPi (Exp l s) n ty sc)
876 | n `elem` allNamesIn sc || impl
877 = bracket p 2 $
878 if l then "|(" else "(" ++
879 show n ++ " : " ++ se 10 ty ++
880 ") " ++ st ++
881 "-> " ++ se 10 sc
882 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
883 where st = case s of
884 Static -> "[static] "
885 _ -> ""
886 se p (PPi (Imp l s) n ty sc)
887 | impl = bracket p 2 $ if l then "|{" else "{" ++
888 show n ++ " : " ++ se 10 ty ++
889 "} " ++ st ++ "-> " ++ se 10 sc
890 | otherwise = se 10 sc
891 where st = case s of
892 Static -> "[static] "
893 _ -> ""
894 se p (PPi (Constraint _ _) n ty sc)
895 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
896 se p (PPi (TacImp _ _ s) n ty sc)
897 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
898 se p (PApp _ (PRef _ f) [])
899 | not impl = show f
900 se p (PApp _ (PRef _ op@(UN (f:_))) args)
901 | length (getExps args) == 2 && not impl && not (isAlpha f)
902 = let [l, r] = getExps args in
903 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
904 se p (PApp _ f as)
905 = let args = getExps as in
906 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
907 else concatMap seArg args
908 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
909 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
910 se p (PHidden tm) = "." ++ se 0 tm
911 se p (PRefl _ t)
912 | not impl = "refl"
913 | otherwise = "refl {" ++ se 10 t ++ "}"
914 se p (PResolveTC _) = "resolvetc"
915 se p (PTrue _) = "()"
916 se p (PFalse _) = "_|_"
917 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
918 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
919 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
920 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
921 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
922 se p PSet = "Set"
923 se p (PConstant c) = show c
924 se p (PProof ts) = "proof { " ++ show ts ++ "}"
925 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
926 se p (PMetavar n) = "?" ++ show n
927 se p (PReturn f) = "return"
928 se p PImpossible = "impossible"
929 se p Placeholder = "_"
930 se p (PDoBlock _) = "do block show not implemented"
931 se p (PElabError s) = show s
932-- se p x = "Not implemented"
934 sArg (PImp _ _ n tm) = siArg (n, tm)
935 sArg (PExp _ _ tm) = seArg tm
936 sArg (PConstraint _ _ tm) = scArg tm
937 sArg (PTacImplicit _ _ n _ tm) = stiArg (n, tm)
939 seArg arg = " " ++ se 0 arg
940 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
941 scArg val = " {{" ++ se 10 val ++ "}}"
942 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
944 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
945 | otherwise = str
948instance Sized PTerm where
949 size (PQuote rawTerm) = size rawTerm
950 size (PRef fc name) = size name
951 size (PLam name ty bdy) = 1 + size ty + size bdy
952 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
953 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
954 size (PTyped trm ty) = 1 + size trm + size ty
955 size (PApp fc name args) = 1 + size args
956 size (PCase fc trm bdy) = 1 + size trm + size bdy
957 size (PTrue fc) = 1
958 size (PFalse fc) = 1
959 size (PRefl fc _) = 1
960 size (PResolveTC fc) = 1
961 size (PEq fc left right) = 1 + size left + size right
962 size (PPair fc left right) = 1 + size left + size right
963 size (PDPair fs left ty right) = 1 + size left + size ty + size right
964 size (PAlternative a alts) = 1 + size alts
965 size (PHidden hidden) = size hidden
966 size PSet = 1
967 size (PConstant const) = 1 + size const
968 size Placeholder = 1
969 size (PDoBlock dos) = 1 + size dos
970 size (PIdiom fc term) = 1 + size term
971 size (PReturn fc) = 1
972 size (PMetavar name) = 1
973 size (PProof tactics) = size tactics
974 size (PElabError err) = size err
975 size PImpossible = 1
977allNamesIn :: PTerm -> [Name]
978allNamesIn tm = nub $ ni [] tm
979 where
980 ni env (PRef _ n)
981 | not (n `elem` env) = [n]
982 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
983 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
984 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
985 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
986 ni env (PHidden tm) = ni env tm
987 ni env (PEq _ l r) = ni env l ++ ni env r
988 ni env (PTyped l r) = ni env l ++ ni env r
989 ni env (PPair _ l r) = ni env l ++ ni env r
990 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
991 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
992 ni env (PAlternative a ls) = concatMap (ni env) ls
993 ni env _ = []
995namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
996namesIn uvars ist tm = nub $ ni [] tm
997 where
998 ni env (PRef _ n)
999 | not (n `elem` env)
1000 = case lookupTy Nothing n (tt_ctxt ist) of
1001 [] -> [n]
1002 _ -> if n `elem` (map fst uvars) then [n] else []
1003 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1004 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1005 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1006 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1007 ni env (PEq _ l r) = ni env l ++ ni env r
1008 ni env (PTyped l r) = ni env l ++ ni env r
1009 ni env (PPair _ l r) = ni env l ++ ni env r
1010 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1011 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1012 ni env (PAlternative a as) = concatMap (ni env) as
1013 ni env (PHidden tm) = ni env tm
1014 ni env _ = []