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