on b641ec8 help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1016 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 | PMutual FC [PDecl' t]
300 | PDirective (Idris ())
301 deriving Functor
302{-!
303deriving instance Binary PDecl'
304!-}
306data PClause' t = PClause FC Name t [t] t [PDecl' t]
307 | PWith FC Name t [t] t [PDecl' t]
308 | PClauseR FC [t] t [PDecl' t]
309 | PWithR FC [t] t [PDecl' t]
310 deriving Functor
311{-!
312deriving instance Binary PClause'
313!-}
315data PData' t = PDatadecl { d_name :: Name,
316 d_tcon :: t,
317 d_cons :: [(Name, t, FC)] }
318 | PLaterdecl { d_name :: Name, d_tcon :: t }
319 deriving Functor
320{-!
321deriving instance Binary PData'
322!-}
324-- Handy to get a free function for applying PTerm -> PTerm functions
325-- across a program, by deriving Functor
327type PDecl = PDecl' PTerm
328type PData = PData' PTerm
329type PClause = PClause' PTerm
331-- get all the names declared in a decl
333declared :: PDecl -> [Name]
334declared (PFix _ _ _) = []
335declared (PTy _ _ _ n t) = [n]
336declared (PClauses _ _ n _) = [] -- not a declaration
337declared (PData _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
338 where fstt (a, _, _) = a
339declared (PParams _ _ ds) = concatMap declared ds
340declared (PNamespace _ ds) = concatMap declared ds
341-- declared (PImport _) = []
343defined :: PDecl -> [Name]
344defined (PFix _ _ _) = []
345defined (PTy _ _ _ n t) = []
346defined (PClauses _ _ n _) = [n] -- not a declaration
347defined (PData _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
348 where fstt (a, _, _) = a
349defined (PParams _ _ ds) = concatMap defined ds
350defined (PNamespace _ ds) = concatMap defined ds
351-- declared (PImport _) = []
353updateN :: [(Name, Name)] -> Name -> Name
354updateN ns n | Just n' <- lookup n ns = n'
355updateN _ n = n
357updateNs :: [(Name, Name)] -> PTerm -> PTerm
358updateNs [] t = t
359updateNs ns t = mapPT updateRef t
360 where updateRef (PRef fc f) = PRef fc (updateN ns f)
361 updateRef t = t
363-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
364-- updateDNs [] t = t
365-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
366-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
367-- where updateCNs ns (PClause n l ts r ds)
368-- = PClause (updateN ns n) (fmap (updateNs ns) l)
369-- (map (fmap (updateNs ns)) ts)
370-- (fmap (updateNs ns) r)
371-- (map (updateDNs ns) ds)
372-- updateDNs ns c = c
374-- High level language terms
376data PTerm = PQuote Raw
377 | PRef FC Name
378 | PPatvar FC Name
379 | PLam Name PTerm PTerm
380 | PPi Plicity Name PTerm PTerm
381 | PLet Name PTerm PTerm PTerm
382 | PTyped PTerm PTerm -- term with explicit type
383 | PApp FC PTerm [PArg]
384 | PCase FC PTerm [(PTerm, PTerm)]
385 | PTrue FC
386 | PFalse FC
387 | PRefl FC PTerm
388 | PResolveTC FC
389 | PEq FC PTerm PTerm
390 | PPair FC PTerm PTerm
391 | PDPair FC PTerm PTerm PTerm
392 | PAlternative Bool [PTerm] -- True if only one may work
393 | PHidden PTerm -- irrelevant or hidden pattern
394 | PSet
395 | PConstant Const
396 | Placeholder
397 | PDoBlock [PDo]
398 | PIdiom FC PTerm
399 | PReturn FC
400 | PMetavar Name
401 | PProof [PTactic]
402 | PTactics [PTactic] -- as PProof, but no auto solving
403 | PElabError Err -- error to report on elaboration
404 | PImpossible -- special case for declaring when an LHS can't typecheck
405 deriving Eq
406{-!
407deriving instance Binary PTerm
408!-}
410mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
411mapPT f t = f (mpt t) where
412 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
413 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
414 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
415 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
416 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
417 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
418 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
419 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
420 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
421 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
422 mpt (PHidden t) = PHidden (mapPT f t)
423 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
424 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
425 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
426 mpt x = x
429data PTactic' t = Intro [Name] | Intros | Focus Name
430 | Refine Name [Bool] | Rewrite t | LetTac Name t
431 | Exact t | Compute | Trivial
432 | Solve
433 | Attack
434 | ProofState | ProofTerm | Undo
435 | Try (PTactic' t) (PTactic' t)
436 | TSeq (PTactic' t) (PTactic' t)
437 | Qed | Abandon
438 deriving (Show, Eq, Functor)
439{-!
440deriving instance Binary PTactic'
441!-}
443instance Sized a => Sized (PTactic' a) where
444 size (Intro nms) = 1 + size nms
445 size Intros = 1
446 size (Focus nm) = 1 + size nm
447 size (Refine nm bs) = 1 + size nm + length bs
448 size (Rewrite t) = 1 + size t
449 size (LetTac nm t) = 1 + size nm + size t
450 size (Exact t) = 1 + size t
451 size Compute = 1
452 size Trivial = 1
453 size Solve = 1
454 size Attack = 1
455 size ProofState = 1
456 size ProofTerm = 1
457 size Undo = 1
458 size (Try l r) = 1 + size l + size r
459 size (TSeq l r) = 1 + size l + size r
460 size Qed = 1
461 size Abandon = 1
463type PTactic = PTactic' PTerm
465data PDo' t = DoExp FC t
466 | DoBind FC Name t
467 | DoBindP FC t t
468 | DoLet FC Name t t
469 | DoLetP FC t t
470 deriving (Eq, Functor)
471{-!
472deriving instance Binary PDo'
473!-}
475instance Sized a => Sized (PDo' a) where
476 size (DoExp fc t) = 1 + size fc + size t
477 size (DoBind fc nm t) = 1 + size fc + size nm + size t
478 size (DoBindP fc l r) = 1 + size fc + size l + size r
479 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
480 size (DoLetP fc l r) = 1 + size fc + size l + size r
482type PDo = PDo' PTerm
484-- The priority gives a hint as to elaboration order. Best to elaborate
485-- things early which will help give a more concrete type to other
486-- variables, e.g. a before (interpTy a).
488data PArg' t = PImp { priority :: Int,
489 lazyarg :: Bool, pname :: Name, getTm :: t }
490 | PExp { priority :: Int,
491 lazyarg :: Bool, getTm :: t }
492 | PConstraint { priority :: Int,
493 lazyarg :: Bool, getTm :: t }
494 | PTacImplicit { priority :: Int,
495 lazyarg :: Bool, pname :: Name,
496 getScript :: t,
497 getTm :: t }
498 deriving (Show, Eq, Functor)
500instance Sized a => Sized (PArg' a) where
501 size (PImp p l nm trm) = 1 + size nm + size trm
502 size (PExp p l trm) = 1 + size trm
503 size (PConstraint p l trm) = 1 + size trm
504 size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
506{-!
507deriving instance Binary PArg'
508!-}
510pimp = PImp 0 True
511pexp = PExp 0 False
512pconst = PConstraint 0 False
513ptacimp = PTacImplicit 0 True
515type PArg = PArg' PTerm
517-- Type class data
519data ClassInfo = CI { instanceName :: Name,
520 class_methods :: [(Name, (FnOpts, PTerm))],
521 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
522 class_params :: [Name],
523 class_instances :: [Name] }
524 deriving Show
525{-!
526deriving instance Binary ClassInfo
527!-}
529data OptInfo = Optimise { collapsible :: Bool,
530 forceable :: [Int], -- argument positions
531 recursive :: [Int] }
532 deriving Show
533{-!
534deriving instance Binary OptInfo
535!-}
538data TypeInfo = TI { con_names :: [Name], codata :: Bool }
539 deriving Show
540{-!
541deriving instance Binary TypeInfo
542!-}
544-- Syntactic sugar info
546data DSL' t = DSL { dsl_bind :: t,
547 dsl_return :: t,
548 dsl_apply :: t,
549 dsl_pure :: t,
550 dsl_var :: Maybe t,
551 index_first :: Maybe t,
552 index_next :: Maybe t,
553 dsl_lambda :: Maybe t,
554 dsl_let :: Maybe t
555 }
556 deriving (Show, Functor)
557{-!
558deriving instance Binary DSL'
559!-}
561type DSL = DSL' PTerm
563data SynContext = PatternSyntax | TermSyntax | AnySyntax
564 deriving Show
565{-!
566deriving instance Binary SynContext
567!-}
569data Syntax = Rule [SSymbol] PTerm SynContext
570 deriving Show
571{-!
572deriving instance Binary Syntax
573!-}
575data SSymbol = Keyword Name
576 | Symbol String
577 | Binding Name
578 | Expr Name
579 | SimpleExpr Name
580 deriving Show
581{-!
582deriving instance Binary SSymbol
583!-}
585initDSL = DSL (PRef f (UN ">>="))
586 (PRef f (UN "return"))
587 (PRef f (UN "<$>"))
588 (PRef f (UN "pure"))
589 Nothing
590 Nothing
591 Nothing
592 Nothing
593 Nothing
594 where f = FC "(builtin)" 0
596data SyntaxInfo = Syn { using :: [(Name, PTerm)],
597 syn_params :: [(Name, PTerm)],
598 syn_namespace :: [String],
599 no_imp :: [Name],
600 decoration :: Name -> Name,
601 inPattern :: Bool,
602 implicitAllowed :: Bool,
603 dsl_info :: DSL }
604 deriving Show
605{-!
606deriving instance Binary SyntaxInfo
607!-}
609defaultSyntax = Syn [] [] [] [] id False False initDSL
611expandNS :: SyntaxInfo -> Name -> Name
612expandNS syn n@(NS _ _) = n
613expandNS syn n = case syn_namespace syn of
614 [] -> n
615 xs -> NS n xs
618--- Pretty printing declarations and terms
620instance Show PTerm where
621 show tm = showImp False tm
623instance Pretty PTerm where
624 pretty = prettyImp False
626instance Show PDecl where
627 show d = showDeclImp False d
629instance Show PClause where
630 show c = showCImp True c
632instance Show PData where
633 show d = showDImp False d
635showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
636showDeclImp t (PTy _ _ _ n ty) = show n ++ " : " ++ showImp t ty
637showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
638showDeclImp _ (PData _ _ _ d) = show d
639showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
640 showSep "\n" (map show ps)
643showCImp :: Bool -> PClause -> String
644showCImp impl (PClause _ n l ws r w)
645 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
646 ++ " where " ++ show w
647 where
648 showWs [] = ""
649 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
650showCImp impl (PWith _ n l ws r w)
651 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
652 ++ " { " ++ show w ++ " } "
653 where
654 showWs [] = ""
655 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
658showDImp :: Bool -> PData -> String
659showDImp impl (PDatadecl n ty cons)
660 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
661 ++ showSep "\n\t| "
662 (map (\ (n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
664getImps :: [PArg] -> [(Name, PTerm)]
665getImps [] = []
666getImps (PImp _ _ n tm : xs) = (n, tm) : getImps xs
667getImps (_ : xs) = getImps xs
669getExps :: [PArg] -> [PTerm]
670getExps [] = []
671getExps (PExp _ _ tm : xs) = tm : getExps xs
672getExps (_ : xs) = getExps xs
674getConsts :: [PArg] -> [PTerm]
675getConsts [] = []
676getConsts (PConstraint _ _ tm : xs) = tm : getConsts xs
677getConsts (_ : xs) = getConsts xs
679getAll :: [PArg] -> [PTerm]
680getAll = map getTm
682prettyImp :: Bool -> PTerm -> Doc
683prettyImp impl = prettySe 10
684 where
685 prettySe p (PQuote r) =
686 if size r > breakingSize then
687 text "![" $$ pretty r <> text "]"
688 else
689 text "![" <> pretty r <> text "]"
690 prettySe p (PPatvar fc n) = pretty n
691 prettySe p (PRef fc n) =
692 if impl then
693 pretty n
694 else
695 prettyBasic n
696 where
697 prettyBasic n@(UN _) = pretty n
698 prettyBasic (MN _ s) = text s
699 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
700 prettySe p (PLam n ty sc) =
701 bracket p 2 $
702 if size sc > breakingSize then
703 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
704 else
705 text "λ" <> pretty n <+> text "=>" <+> pretty sc
706 prettySe p (PLet n ty v sc) =
707 bracket p 2 $
708 if size sc > breakingSize then
709 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
710 nest nestingSize (prettySe 10 sc)
711 else
712 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
713 prettySe 10 sc
714 prettySe p (PPi (Exp l s) n ty sc)
715 | n `elem` allNamesIn sc || impl =
716 let open = if l then text "|" <> lparen else lparen in
717 bracket p 2 $
718 if size sc > breakingSize then
719 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
720 st <+> text "->" $+$ prettySe 10 sc
721 else
722 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
723 st <+> text "->" <+> prettySe 10 sc
724 | otherwise =
725 bracket p 2 $
726 if size sc > breakingSize then
727 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
728 else
729 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
730 where
731 st =
732 case s of
733 Static -> text "[static]"
734 _ -> empty
735 prettySe p (PPi (Imp l s) n ty sc)
736 | impl =
737 let open = if l then text "|" <> lbrace else lbrace in
738 bracket p 2 $
739 if size sc > breakingSize then
740 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
741 st <+> text "->" <+> prettySe 10 sc
742 else
743 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
744 st <+> text "->" <+> prettySe 10 sc
745 | otherwise = prettySe 10 sc
746 where
747 st =
748 case s of
749 Static -> text $ "[static]"
750 _ -> empty
751 prettySe p (PPi (Constraint _ _) n ty sc) =
752 bracket p 2 $
753 if size sc > breakingSize then
754 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
755 else
756 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
757 prettySe p (PPi (TacImp _ _ s) n ty sc) =
758 bracket p 2 $
759 if size sc > breakingSize then
760 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
761 rbrace <+> text "->" $+$ prettySe 10 sc
762 else
763 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
764 rbrace <+> text "->" <+> prettySe 10 sc
765 prettySe p (PApp _ (PRef _ f) [])
766 | not impl = pretty f
767 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
768 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
769 let [l, r] = getExps args in
770 bracket p 1 $
771 if size r > breakingSize then
772 prettySe 1 l <+> pretty op $+$ prettySe 0 r
773 else
774 prettySe 1 l <+> pretty op <+> prettySe 0 r
775 prettySe p (PApp _ f as) =
776 let args = getExps as in
777 bracket p 1 $
778 prettySe 1 f <+>
779 if impl then
780 foldl fS empty as
781 -- foldr (<+>) empty $ map prettyArgS as
782 else
783 foldl fSe empty args
784 -- foldr (<+>) empty $ map prettyArgSe args
785 where
786 fS l r =
787 if size r > breakingSize then
788 l $+$ nest nestingSize (prettyArgS r)
789 else
790 l <+> prettyArgS r
792 fSe l r =
793 if size r > breakingSize then
794 l $+$ nest nestingSize (prettyArgSe r)
795 else
796 l <+> prettyArgSe r
797 prettySe p (PCase _ scr opts) =
798 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
799 where
800 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
802 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
803 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
804 prettySe p (PRefl _ _) = text "refl"
805 prettySe p (PResolveTC _) = text "resolvetc"
806 prettySe p (PTrue _) = text "()"
807 prettySe p (PFalse _) = text "_|_"
808 prettySe p (PEq _ l r) =
809 bracket p 2 $
810 if size r > breakingSize then
811 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
812 else
813 prettySe 10 l <+> text "=" <+> prettySe 10 r
814 prettySe p (PTyped l r) =
815 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
816 prettySe p (PPair _ l r) =
817 if size r > breakingSize then
818 lparen <> prettySe 10 l <> text "," $+$
819 prettySe 10 r <> rparen
820 else
821 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
822 prettySe p (PDPair _ l t r) =
823 if size r > breakingSize then
824 lparen <> prettySe 10 l <+> text "**" $+$
825 prettySe 10 r <> rparen
826 else
827 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
828 prettySe p (PAlternative a as) =
829 lparen <> text "|" <> prettyAs <> text "|" <> rparen
830 where
831 prettyAs =
832 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
833 prettySe p PSet = text "Set"
834 prettySe p (PConstant c) = pretty c
835 -- XXX: add pretty for tactics
836 prettySe p (PProof ts) =
837 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
838 prettySe p (PTactics ts) =
839 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
840 prettySe p (PMetavar n) = text "?" <> pretty n
841 prettySe p (PReturn f) = text "return"
842 prettySe p PImpossible = text "impossible"
843 prettySe p Placeholder = text "_"
844 prettySe p (PDoBlock _) = text "do block pretty not implemented"
845 prettySe p (PElabError s) = pretty s
847 prettySe p _ = text "test"
849 prettyArgS (PImp _ _ n tm) = prettyArgSi (n, tm)
850 prettyArgS (PExp _ _ tm) = prettyArgSe tm
851 prettyArgS (PConstraint _ _ tm) = prettyArgSc tm
852 prettyArgS (PTacImplicit _ _ n _ tm) = prettyArgSti (n, tm)
854 prettyArgSe arg = prettySe 0 arg
855 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
856 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
857 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
859 bracket outer inner doc
860 | inner > outer = lparen <> doc <> rparen
861 | otherwise = doc
863showImp :: Bool -> PTerm -> String
864showImp impl tm = se 10 tm where
865 se p (PQuote r) = "![" ++ show r ++ "]"
866 se p (PPatvar fc n) = show n
867 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
868 else showbasic n
869 where showbasic n@(UN _) = show n
870 showbasic (MN _ s) = s
871 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
872 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
873 (if impl then " : " ++ se 10 ty else "") ++ " => "
874 ++ se 10 sc
875 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
876 " in " ++ se 10 sc
877 se p (PPi (Exp l s) n ty sc)
878 | n `elem` allNamesIn sc || impl
879 = bracket p 2 $
880 if l then "|(" else "(" ++
881 show n ++ " : " ++ se 10 ty ++
882 ") " ++ st ++
883 "-> " ++ se 10 sc
884 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
885 where st = case s of
886 Static -> "[static] "
887 _ -> ""
888 se p (PPi (Imp l s) n ty sc)
889 | impl = bracket p 2 $ if l then "|{" else "{" ++
890 show n ++ " : " ++ se 10 ty ++
891 "} " ++ st ++ "-> " ++ se 10 sc
892 | otherwise = se 10 sc
893 where st = case s of
894 Static -> "[static] "
895 _ -> ""
896 se p (PPi (Constraint _ _) n ty sc)
897 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
898 se p (PPi (TacImp _ _ s) n ty sc)
899 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
900 se p (PApp _ (PRef _ f) [])
901 | not impl = show f
902 se p (PApp _ (PRef _ op@(UN (f:_))) args)
903 | length (getExps args) == 2 && not impl && not (isAlpha f)
904 = let [l, r] = getExps args in
905 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
906 se p (PApp _ f as)
907 = let args = getExps as in
908 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
909 else concatMap seArg args
910 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
911 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
912 se p (PHidden tm) = "." ++ se 0 tm
913 se p (PRefl _ t)
914 | not impl = "refl"
915 | otherwise = "refl {" ++ se 10 t ++ "}"
916 se p (PResolveTC _) = "resolvetc"
917 se p (PTrue _) = "()"
918 se p (PFalse _) = "_|_"
919 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
920 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
921 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
922 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
923 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
924 se p PSet = "Set"
925 se p (PConstant c) = show c
926 se p (PProof ts) = "proof { " ++ show ts ++ "}"
927 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
928 se p (PMetavar n) = "?" ++ show n
929 se p (PReturn f) = "return"
930 se p PImpossible = "impossible"
931 se p Placeholder = "_"
932 se p (PDoBlock _) = "do block show not implemented"
933 se p (PElabError s) = show s
934-- se p x = "Not implemented"
936 sArg (PImp _ _ n tm) = siArg (n, tm)
937 sArg (PExp _ _ tm) = seArg tm
938 sArg (PConstraint _ _ tm) = scArg tm
939 sArg (PTacImplicit _ _ n _ tm) = stiArg (n, tm)
941 seArg arg = " " ++ se 0 arg
942 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
943 scArg val = " {{" ++ se 10 val ++ "}}"
944 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
946 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
947 | otherwise = str
950instance Sized PTerm where
951 size (PQuote rawTerm) = size rawTerm
952 size (PRef fc name) = size name
953 size (PLam name ty bdy) = 1 + size ty + size bdy
954 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
955 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
956 size (PTyped trm ty) = 1 + size trm + size ty
957 size (PApp fc name args) = 1 + size args
958 size (PCase fc trm bdy) = 1 + size trm + size bdy
959 size (PTrue fc) = 1
960 size (PFalse fc) = 1
961 size (PRefl fc _) = 1
962 size (PResolveTC fc) = 1
963 size (PEq fc left right) = 1 + size left + size right
964 size (PPair fc left right) = 1 + size left + size right
965 size (PDPair fs left ty right) = 1 + size left + size ty + size right
966 size (PAlternative a alts) = 1 + size alts
967 size (PHidden hidden) = size hidden
968 size PSet = 1
969 size (PConstant const) = 1 + size const
970 size Placeholder = 1
971 size (PDoBlock dos) = 1 + size dos
972 size (PIdiom fc term) = 1 + size term
973 size (PReturn fc) = 1
974 size (PMetavar name) = 1
975 size (PProof tactics) = size tactics
976 size (PElabError err) = size err
977 size PImpossible = 1
979allNamesIn :: PTerm -> [Name]
980allNamesIn tm = nub $ ni [] tm
981 where
982 ni env (PRef _ n)
983 | not (n `elem` env) = [n]
984 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
985 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
986 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
987 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
988 ni env (PHidden tm) = ni env tm
989 ni env (PEq _ l r) = ni env l ++ ni env r
990 ni env (PTyped l r) = ni env l ++ ni env r
991 ni env (PPair _ l r) = ni env l ++ ni env r
992 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
993 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
994 ni env (PAlternative a ls) = concatMap (ni env) ls
995 ni env _ = []
997namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
998namesIn uvars ist tm = nub $ ni [] tm
999 where
1000 ni env (PRef _ n)
1001 | not (n `elem` env)
1002 = case lookupTy Nothing n (tt_ctxt ist) of
1003 [] -> [n]
1004 _ -> if n `elem` (map fst uvars) then [n] else []
1005 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1006 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1007 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1008 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1009 ni env (PEq _ l r) = ni env l ++ ni env r
1010 ni env (PTyped l r) = ni env l ++ ni env r
1011 ni env (PPair _ l r) = ni env l ++ ni env r
1012 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1013 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1014 ni env (PAlternative a as) = concatMap (ni env) as
1015 ni env (PHidden tm) = ni env tm
1016 ni env _ = []