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