on b894841 help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1023 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 | DumpCases String
189 | FOVM String
190 deriving (Show, Eq)
193-- Parsed declarations
195data Fixity = Infixl { prec :: Int }
196 | Infixr { prec :: Int }
197 | InfixN { prec :: Int }
198 | PrefixN { prec :: Int }
199 deriving Eq
200{-!
201deriving instance Binary Fixity
202!-}
204instance Show Fixity where
205 show (Infixl i) = "infixl " ++ show i
206 show (Infixr i) = "infixr " ++ show i
207 show (InfixN i) = "infix " ++ show i
208 show (PrefixN i) = "prefix " ++ show i
210data FixDecl = Fix Fixity String
211 deriving (Show, Eq)
212{-!
213deriving instance Binary FixDecl
214!-}
216instance Ord FixDecl where
217 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
220data Static = Static | Dynamic
221 deriving (Show, Eq)
222{-!
223deriving instance Binary Static
224!-}
226-- Mark bindings with their explicitness, and laziness
227data Plicity = Imp { plazy :: Bool,
228 pstatic :: Static }
229 | Exp { plazy :: Bool,
230 pstatic :: Static }
231 | Constraint { plazy :: Bool,
232 pstatic :: Static }
233 | TacImp { plazy :: Bool,
234 pstatic :: Static,
235 pscript :: PTerm }
236 deriving (Show, Eq)
238{-!
239deriving instance Binary Plicity
240!-}
242impl = Imp False Dynamic
243expl = Exp False Dynamic
244constraint = Constraint False Dynamic
245tacimpl = TacImp False Dynamic
247data FnOpt = Inlinable | TotalFn | AssertTotal | TCGen
248 | CExport String -- export, with a C name
249 | Specialise [Name] -- specialise it, freeze these names
250 deriving (Show, Eq)
251{-!
252deriving instance Binary FnOpt
253!-}
255type FnOpts = [FnOpt]
257inlinable :: FnOpts -> Bool
258inlinable = elem Inlinable
260data PDecl' t = PFix FC Fixity [String] -- fixity declaration
261 | PTy SyntaxInfo FC FnOpts Name t -- type declaration
262 | PClauses FC FnOpts Name [PClause' t] -- pattern clause
263 | PCAF FC Name t -- top level constant
264 | PData SyntaxInfo FC (PData' t) -- data declaration
265 | PParams FC [(Name, t)] [PDecl' t] -- params block
266 | PNamespace String [PDecl' t] -- new namespace
267 | PRecord SyntaxInfo FC Name t Name t -- record declaration
268 | PClass SyntaxInfo FC
269 [t] -- constraints
270 Name
271 [(Name, t)] -- parameters
272 [PDecl' t] -- declarations
273 | PInstance SyntaxInfo FC [t] -- constraints
274 Name -- class
275 [t] -- parameters
276 t -- full instance type
277 (Maybe Name) -- explicit name
278 [PDecl' t]
279 | PDSL Name (DSL' t)
280 | PSyntax FC Syntax
281 | PDirective (Idris ())
282 deriving Functor
283{-!
284deriving instance Binary PDecl'
285!-}
287data PClause' t = PClause FC Name t [t] t [PDecl' t]
288 | PWith FC Name t [t] t [PDecl' t]
289 | PClauseR FC [t] t [PDecl' t]
290 | PWithR FC [t] t [PDecl' t]
291 deriving Functor
292{-!
293deriving instance Binary PClause'
294!-}
296data PData' t = PDatadecl { d_name :: Name,
297 d_tcon :: t,
298 d_cons :: [(Name, t, FC)] }
299 deriving Functor
300{-!
301deriving instance Binary PData'
302!-}
304-- Handy to get a free function for applying PTerm -> PTerm functions
305-- across a program, by deriving Functor
307type PDecl = PDecl' PTerm
308type PData = PData' PTerm
309type PClause = PClause' PTerm
311-- get all the names declared in a decl
313declared :: PDecl -> [Name]
314declared (PFix _ _ _) = []
315declared (PTy _ _ _ n t) = [n]
316declared (PClauses _ _ n _) = [] -- not a declaration
317declared (PData _ _ (PDatadecl n _ ts)) = n : map fstt ts
318 where fstt (a, _, _) = a
319declared (PParams _ _ ds) = concatMap declared ds
320declared (PNamespace _ ds) = concatMap declared ds
321-- declared (PImport _) = []
323defined :: PDecl -> [Name]
324defined (PFix _ _ _) = []
325defined (PTy _ _ _ n t) = []
326defined (PClauses _ _ n _) = [n] -- not a declaration
327defined (PData _ _ (PDatadecl n _ ts)) = n : map fstt ts
328 where fstt (a, _, _) = a
329defined (PParams _ _ ds) = concatMap defined ds
330defined (PNamespace _ ds) = concatMap defined ds
331-- declared (PImport _) = []
333updateN :: [(Name, Name)] -> Name -> Name
334updateN ns n | Just n' <- lookup n ns = n'
335updateN _ n = n
337updateNs :: [(Name, Name)] -> PTerm -> PTerm
338updateNs [] t = t
339updateNs ns t = mapPT updateRef t
340 where updateRef (PRef fc f) = PRef fc (updateN ns f)
341 updateRef t = t
343-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
344-- updateDNs [] t = t
345-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
346-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
347-- where updateCNs ns (PClause n l ts r ds)
348-- = PClause (updateN ns n) (fmap (updateNs ns) l)
349-- (map (fmap (updateNs ns)) ts)
350-- (fmap (updateNs ns) r)
351-- (map (updateDNs ns) ds)
352-- updateDNs ns c = c
354-- High level language terms
356data PTerm = PQuote Raw
357 | PRef FC Name
358 | PPatvar FC Name
359 | PLam Name PTerm PTerm
360 | PPi Plicity Name PTerm PTerm
361 | PLet Name PTerm PTerm PTerm
362 | PTyped PTerm PTerm -- term with explicit type
363 | PApp FC PTerm [PArg]
364 | PCase FC PTerm [(PTerm, PTerm)]
365 | PTrue FC
366 | PFalse FC
367 | PRefl FC PTerm
368 | PResolveTC FC
369 | PEq FC PTerm PTerm
370 | PPair FC PTerm PTerm
371 | PDPair FC PTerm PTerm PTerm
372 | PAlternative Bool [PTerm] -- True if only one may work
373 | PHidden PTerm -- irrelevant or hidden pattern
374 | PSet
375 | PConstant Const
376 | Placeholder
377 | PDoBlock [PDo]
378 | PIdiom FC PTerm
379 | PReturn FC
380 | PMetavar Name
381 | PProof [PTactic]
382 | PTactics [PTactic] -- as PProof, but no auto solving
383 | PElabError Err -- error to report on elaboration
384 | PImpossible -- special case for declaring when an LHS can't typecheck
385 deriving Eq
386{-!
387deriving instance Binary PTerm
388!-}
390mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
391mapPT f t = f (mpt t) where
392 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
393 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
394 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
395 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
396 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
397 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
398 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
399 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
400 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
401 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
402 mpt (PHidden t) = PHidden (mapPT f t)
403 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
404 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
405 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
406 mpt x = x
409data PTactic' t = Intro [Name] | Intros | Focus Name
410 | Refine Name [Bool] | Rewrite t | LetTac Name t
411 | Exact t | Compute | Trivial
412 | Solve
413 | Attack
414 | ProofState | ProofTerm | Undo
415 | Try (PTactic' t) (PTactic' t)
416 | TSeq (PTactic' t) (PTactic' t)
417 | Qed | Abandon
418 deriving (Show, Eq, Functor)
419{-!
420deriving instance Binary PTactic'
421!-}
423instance Sized a => Sized (PTactic' a) where
424 size (Intro nms) = 1 + size nms
425 size Intros = 1
426 size (Focus nm) = 1 + size nm
427 size (Refine nm bs) = 1 + size nm + length bs
428 size (Rewrite t) = 1 + size t
429 size (LetTac nm t) = 1 + size nm + size t
430 size (Exact t) = 1 + size t
431 size Compute = 1
432 size Trivial = 1
433 size Solve = 1
434 size Attack = 1
435 size ProofState = 1
436 size ProofTerm = 1
437 size Undo = 1
438 size (Try l r) = 1 + size l + size r
439 size (TSeq l r) = 1 + size l + size r
440 size Qed = 1
441 size Abandon = 1
443type PTactic = PTactic' PTerm
445data PDo' t = DoExp FC t
446 | DoBind FC Name t
447 | DoBindP FC t t
448 | DoLet FC Name t t
449 | DoLetP FC t t
450 deriving (Eq, Functor)
451{-!
452deriving instance Binary PDo'
453!-}
455instance Sized a => Sized (PDo' a) where
456 size (DoExp fc t) = 1 + size fc + size t
457 size (DoBind fc nm t) = 1 + size fc + size nm + size t
458 size (DoBindP fc l r) = 1 + size fc + size l + size r
459 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
460 size (DoLetP fc l r) = 1 + size fc + size l + size r
462type PDo = PDo' PTerm
464-- The priority gives a hint as to elaboration order. Best to elaborate
465-- things early which will help give a more concrete type to other
466-- variables, e.g. a before (interpTy a).
468data PArg' t = PImp { priority :: Int,
469 lazyarg :: Bool, pname :: Name, getTm :: t }
470 | PExp { priority :: Int,
471 lazyarg :: Bool, getTm :: t }
472 | PConstraint { priority :: Int,
473 lazyarg :: Bool, getTm :: t }
474 | PTacImplicit { priority :: Int,
475 lazyarg :: Bool, pname :: Name,
476 getScript :: t,
477 getTm :: t }
478 deriving (Show, Eq, Functor)
480instance Sized a => Sized (PArg' a) where
481 size (PImp p l nm trm) = 1 + size nm + size trm
482 size (PExp p l trm) = 1 + size trm
483 size (PConstraint p l trm) = 1 + size trm
484 size (PTacImplicit p l nm scr trm) = 1 + size nm + size scr + size trm
486{-!
487deriving instance Binary PArg'
488!-}
490pimp = PImp 0 True
491pexp = PExp 0 False
492pconst = PConstraint 0 False
493ptacimp = PTacImplicit 0 True
495type PArg = PArg' PTerm
497-- Type class data
499data ClassInfo = CI { instanceName :: Name,
500 class_methods :: [(Name, (FnOpts, PTerm))],
501 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
502 class_params :: [Name],
503 class_instances :: [Name] }
504 deriving Show
505{-!
506deriving instance Binary ClassInfo
507!-}
509data OptInfo = Optimise { collapsible :: Bool,
510 forceable :: [Int], -- argument positions
511 recursive :: [Int] }
512 deriving Show
513{-!
514deriving instance Binary OptInfo
515!-}
518data TypeInfo = TI { con_names :: [Name] }
519 deriving Show
520{-!
521deriving instance Binary TypeInfo
522!-}
524-- Syntactic sugar info
526data DSL' t = DSL { dsl_bind :: t,
527 dsl_return :: t,
528 dsl_apply :: t,
529 dsl_pure :: t,
530 dsl_var :: Maybe t,
531 index_first :: Maybe t,
532 index_next :: Maybe t,
533 dsl_lambda :: Maybe t,
534 dsl_let :: Maybe t
535 }
536 deriving (Show, Functor)
537{-!
538deriving instance Binary DSL'
539!-}
541type DSL = DSL' PTerm
543data SynContext = PatternSyntax | TermSyntax | AnySyntax
544 deriving Show
545{-!
546deriving instance Binary SynContext
547!-}
549data Syntax = Rule [SSymbol] PTerm SynContext
550 deriving Show
551{-!
552deriving instance Binary Syntax
553!-}
555data SSymbol = Keyword Name
556 | Symbol String
557 | Binding Name
558 | Expr Name
559 | SimpleExpr Name
560 deriving Show
561{-!
562deriving instance Binary SSymbol
563!-}
565initDSL = DSL (PRef f (UN ">>="))
566 (PRef f (UN "return"))
567 (PRef f (UN "<$>"))
568 (PRef f (UN "pure"))
569 Nothing
570 Nothing
571 Nothing
572 Nothing
573 Nothing
574 where f = FC "(builtin)" 0
576data SyntaxInfo = Syn { using :: [(Name, PTerm)],
577 syn_params :: [(Name, PTerm)],
578 syn_namespace :: [String],
579 no_imp :: [Name],
580 decoration :: Name -> Name,
581 inPattern :: Bool,
582 implicitAllowed :: Bool,
583 dsl_info :: DSL }
584 deriving Show
585{-!
586deriving instance Binary SyntaxInfo
587!-}
589defaultSyntax = Syn [] [] [] [] id False False initDSL
591expandNS :: SyntaxInfo -> Name -> Name
592expandNS syn n@(NS _ _) = n
593expandNS syn n = case syn_namespace syn of
594 [] -> n
595 xs -> NS n xs
598--- Pretty printing declarations and terms
600instance Show PTerm where
601 show tm = showImp False tm
603instance Pretty PTerm where
604 pretty = prettyImp False
606instance Show PDecl where
607 show d = showDeclImp False d
609instance Show PClause where
610 show c = showCImp True c
612instance Show PData where
613 show d = showDImp False d
615showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
616showDeclImp t (PTy _ _ _ n ty) = show n ++ " : " ++ showImp t ty
617showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
618showDeclImp _ (PData _ _ d) = show d
620showCImp :: Bool -> PClause -> String
621showCImp impl (PClause _ n l ws r w)
622 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
623 ++ " where " ++ show w
624 where
625 showWs [] = ""
626 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
627showCImp impl (PWith _ n l ws r w)
628 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
629 ++ " { " ++ show w ++ " } "
630 where
631 showWs [] = ""
632 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
635showDImp :: Bool -> PData -> String
636showDImp impl (PDatadecl n ty cons)
637 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
638 ++ showSep "\n\t| "
639 (map (\ (n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
641getImps :: [PArg] -> [(Name, PTerm)]
642getImps [] = []
643getImps (PImp _ _ n tm : xs) = (n, tm) : getImps xs
644getImps (_ : xs) = getImps xs
646getExps :: [PArg] -> [PTerm]
647getExps [] = []
648getExps (PExp _ _ tm : xs) = tm : getExps xs
649getExps (_ : xs) = getExps xs
651getConsts :: [PArg] -> [PTerm]
652getConsts [] = []
653getConsts (PConstraint _ _ tm : xs) = tm : getConsts xs
654getConsts (_ : xs) = getConsts xs
656getAll :: [PArg] -> [PTerm]
657getAll = map getTm
659prettyImp :: Bool -> PTerm -> Doc
660prettyImp impl = prettySe 10
661 where
662 prettySe p (PQuote r) =
663 if size r > breakingSize then
664 text "![" $$ pretty r <> text "]"
665 else
666 text "![" <> pretty r <> text "]"
667 prettySe p (PPatvar fc n) = pretty n
668 prettySe p (PRef fc n) =
669 if impl then
670 pretty n
671 else
672 prettyBasic n
673 where
674 prettyBasic n@(UN _) = pretty n
675 prettyBasic (MN _ s) = text s
676 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
677 prettySe p (PLam n ty sc) =
678 bracket p 2 $
679 if size sc > breakingSize then
680 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
681 else
682 text "λ" <> pretty n <+> text "=>" <+> pretty sc
683 prettySe p (PLet n ty v sc) =
684 bracket p 2 $
685 if size sc > breakingSize then
686 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
687 nest nestingSize (prettySe 10 sc)
688 else
689 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
690 prettySe 10 sc
691 prettySe p (PPi (Exp l s) n ty sc)
692 | n `elem` allNamesIn sc || impl =
693 let open = if l then text "|" <> lparen else lparen in
694 bracket p 2 $
695 if size sc > breakingSize then
696 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
697 st <+> text "->" $+$ prettySe 10 sc
698 else
699 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
700 st <+> text "->" <+> prettySe 10 sc
701 | otherwise =
702 bracket p 2 $
703 if size sc > breakingSize then
704 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
705 else
706 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
707 where
708 st =
709 case s of
710 Static -> text "[static]"
711 _ -> empty
712 prettySe p (PPi (Imp l s) n ty sc)
713 | impl =
714 let open = if l then text "|" <> lbrace else lbrace in
715 bracket p 2 $
716 if size sc > breakingSize then
717 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
718 st <+> text "->" <+> prettySe 10 sc
719 else
720 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
721 st <+> text "->" <+> prettySe 10 sc
722 | otherwise = prettySe 10 sc
723 where
724 st =
725 case s of
726 Static -> text $ "[static]"
727 _ -> empty
728 prettySe p (PPi (Constraint _ _) n ty sc) =
729 bracket p 2 $
730 if size sc > breakingSize then
731 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
732 else
733 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
734 prettySe p (PPi (TacImp _ _ s) n ty sc) =
735 bracket p 2 $
736 if size sc > breakingSize then
737 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
738 rbrace <+> text "->" $+$ prettySe 10 sc
739 else
740 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
741 rbrace <+> text "->" <+> prettySe 10 sc
742 prettySe p (PApp _ (PRef _ f) [])
743 | not impl = pretty f
744 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
745 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
746 let [l, r] = getExps args in
747 bracket p 1 $
748 if size r > breakingSize then
749 prettySe 1 l <+> pretty op $+$ prettySe 0 r
750 else
751 prettySe 1 l <+> pretty op <+> prettySe 0 r
752 prettySe p (PApp _ f as) =
753 let args = getExps as in
754 bracket p 1 $
755 prettySe 1 f <+>
756 if impl then
757 foldl fS empty as
758 -- foldr (<+>) empty $ map prettyArgS as
759 else
760 foldl fSe empty args
761 -- foldr (<+>) empty $ map prettyArgSe args
762 where
763 fS l r =
764 if size r > breakingSize then
765 l $+$ nest nestingSize (prettyArgS r)
766 else
767 l <+> prettyArgS r
769 fSe l r =
770 if size r > breakingSize then
771 l $+$ nest nestingSize (prettyArgSe r)
772 else
773 l <+> prettyArgSe r
774 prettySe p (PCase _ scr opts) =
775 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
776 where
777 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
779 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
780 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
781 prettySe p (PRefl _ _) = text "refl"
782 prettySe p (PResolveTC _) = text "resolvetc"
783 prettySe p (PTrue _) = text "()"
784 prettySe p (PFalse _) = text "_|_"
785 prettySe p (PEq _ l r) =
786 bracket p 2 $
787 if size r > breakingSize then
788 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
789 else
790 prettySe 10 l <+> text "=" <+> prettySe 10 r
791 prettySe p (PTyped l r) =
792 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
793 prettySe p (PPair _ l r) =
794 if size r > breakingSize then
795 lparen <> prettySe 10 l <> text "," $+$
796 prettySe 10 r <> rparen
797 else
798 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
799 prettySe p (PDPair _ l t r) =
800 if size r > breakingSize then
801 lparen <> prettySe 10 l <+> text "**" $+$
802 prettySe 10 r <> rparen
803 else
804 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
805 prettySe p (PAlternative a as) =
806 lparen <> text "|" <> prettyAs <> text "|" <> rparen
807 where
808 prettyAs =
809 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
810 prettySe p PSet = text "Set"
811 prettySe p (PConstant c) = pretty c
812 -- XXX: add pretty for tactics
813 prettySe p (PProof ts) =
814 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
815 prettySe p (PTactics ts) =
816 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
817 prettySe p (PMetavar n) = text "?" <> pretty n
818 prettySe p (PReturn f) = text "return"
819 prettySe p PImpossible = text "impossible"
820 prettySe p Placeholder = text "_"
821 prettySe p (PDoBlock _) = text "do block pretty not implemented"
822 prettySe p (PElabError s) = pretty s
824 prettySe p _ = text "test"
826 prettyArgS (PImp _ _ n tm) = prettyArgSi (n, tm)
827 prettyArgS (PExp _ _ tm) = prettyArgSe tm
828 prettyArgS (PConstraint _ _ tm) = prettyArgSc tm
829 prettyArgS (PTacImplicit _ _ n _ tm) = prettyArgSti (n, tm)
831 prettyArgSe arg = prettySe 0 arg
832 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
833 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
834 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
836 bracket outer inner doc
837 | inner > outer = lparen <> doc <> rparen
838 | otherwise = doc
840showImp :: Bool -> PTerm -> String
841showImp impl tm = se 10 tm where
842 se p (PQuote r) = "![" ++ show r ++ "]"
843 se p (PPatvar fc n) = show n
844 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
845 else showbasic n
846 where showbasic n@(UN _) = show n
847 showbasic (MN _ s) = s
848 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
849 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
850 (if impl then " : " ++ se 10 ty else "") ++ " => "
851 ++ se 10 sc
852 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
853 " in " ++ se 10 sc
854 se p (PPi (Exp l s) n ty sc)
855 | n `elem` allNamesIn sc || impl
856 = bracket p 2 $
857 if l then "|(" else "(" ++
858 show n ++ " : " ++ se 10 ty ++
859 ") " ++ st ++
860 "-> " ++ se 10 sc
861 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
862 where st = case s of
863 Static -> "[static] "
864 _ -> ""
865 se p (PPi (Imp l s) n ty sc)
866 | impl = bracket p 2 $ if l then "|{" else "{" ++
867 show n ++ " : " ++ se 10 ty ++
868 "} " ++ st ++ "-> " ++ se 10 sc
869 | otherwise = se 10 sc
870 where st = case s of
871 Static -> "[static] "
872 _ -> ""
873 se p (PPi (Constraint _ _) n ty sc)
874 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
875 se p (PPi (TacImp _ _ s) n ty sc)
876 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
877 se p (PApp _ (PRef _ f) [])
878 | not impl = show f
879 se p (PApp _ (PRef _ op@(UN (f:_))) args)
880 | length (getExps args) == 2 && not impl && not (isAlpha f)
881 = let [l, r] = getExps args in
882 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
883 se p (PApp _ f as)
884 = let args = getExps as in
885 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
886 else concatMap seArg args
887 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
888 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
889 se p (PHidden tm) = "." ++ se 0 tm
890 se p (PRefl _ t)
891 | not impl = "refl"
892 | otherwise = "refl {" ++ se 10 t ++ "}"
893 se p (PResolveTC _) = "resolvetc"
894 se p (PTrue _) = "()"
895 se p (PFalse _) = "_|_"
896 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
897 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
898 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
899 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
900 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
901 se p PSet = "Set"
902 se p (PConstant c) = show c
903 se p (PProof ts) = "proof { " ++ show ts ++ "}"
904 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
905 se p (PMetavar n) = "?" ++ show n
906 se p (PReturn f) = "return"
907 se p PImpossible = "impossible"
908 se p Placeholder = "_"
909 se p (PDoBlock _) = "do block show not implemented"
910 se p (PElabError s) = show s
911-- se p x = "Not implemented"
913 sArg (PImp _ _ n tm) = siArg (n, tm)
914 sArg (PExp _ _ tm) = seArg tm
915 sArg (PConstraint _ _ tm) = scArg tm
916 sArg (PTacImplicit _ _ n _ tm) = stiArg (n, tm)
918 seArg arg = " " ++ se 0 arg
919 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
920 scArg val = " {{" ++ se 10 val ++ "}}"
921 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
923 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
924 | otherwise = str
926{-
927 PQuote Raw
928 | PRef FC Name
929 | PLam Name PTerm PTerm
930 | PPi Plicity Name PTerm PTerm
931 | PLet Name PTerm PTerm PTerm
932 | PTyped PTerm PTerm -- term with explicit type
933 | PApp FC PTerm [PArg]
934 | PCase FC PTerm [(PTerm, PTerm)]
935 | PTrue FC
936 | PFalse FC
937 | PRefl FC
938 | PResolveTC FC
939 | PEq FC PTerm PTerm
940 | PPair FC PTerm PTerm
941 | PDPair FC PTerm PTerm PTerm
942 | PAlternative [PTerm]
943 | PHidden PTerm -- irrelevant or hidden pattern
944 | PSet
945 | PConstant Const
946 | Placeholder
947 | PDoBlock [PDo]
948 | PIdiom FC PTerm
949 | PReturn FC
950 | PMetavar Name
951 | PProof [PTactic]
952 | PTactics [PTactic] -- as PProof, but no auto solving
953 | PElabError Err -- error to report on elaboration
954 | PImpossible -- special case for declaring when an LHS can't typecheck
955-}
957instance Sized PTerm where
958 size (PQuote rawTerm) = size rawTerm
959 size (PRef fc name) = size name
960 size (PLam name ty bdy) = 1 + size ty + size bdy
961 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
962 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
963 size (PTyped trm ty) = 1 + size trm + size ty
964 size (PApp fc name args) = 1 + size args
965 size (PCase fc trm bdy) = 1 + size trm + size bdy
966 size (PTrue fc) = 1
967 size (PFalse fc) = 1
968 size (PRefl fc _) = 1
969 size (PResolveTC fc) = 1
970 size (PEq fc left right) = 1 + size left + size right
971 size (PPair fc left right) = 1 + size left + size right
972 size (PDPair fs left ty right) = 1 + size left + size ty + size right
973 size (PAlternative a alts) = 1 + size alts
974 size (PHidden hidden) = size hidden
975 size PSet = 1
976 size (PConstant const) = 1 + size const
977 size Placeholder = 1
978 size (PDoBlock dos) = 1 + size dos
979 size (PIdiom fc term) = 1 + size term
980 size (PReturn fc) = 1
981 size (PMetavar name) = 1
982 size (PProof tactics) = size tactics
983 size (PElabError err) = size err
984 size PImpossible = 1
986allNamesIn :: PTerm -> [Name]
987allNamesIn tm = nub $ ni [] tm
988 where
989 ni env (PRef _ n)
990 | not (n `elem` env) = [n]
991 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
992 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
993 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
994 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
995 ni env (PHidden tm) = ni env tm
996 ni env (PEq _ l r) = ni env l ++ ni env r
997 ni env (PTyped l r) = ni env l ++ ni env r
998 ni env (PPair _ l r) = ni env l ++ ni env r
999 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1000 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1001 ni env (PAlternative a ls) = concatMap (ni env) ls
1002 ni env _ = []
1004namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1005namesIn uvars ist tm = nub $ ni [] tm
1006 where
1007 ni env (PRef _ n)
1008 | not (n `elem` env)
1009 = case lookupTy Nothing n (tt_ctxt ist) of
1010 [] -> [n]
1011 _ -> if n `elem` (map fst uvars) then [n] else []
1012 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1013 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1014 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1015 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1016 ni env (PEq _ l r) = ni env l ++ ni env r
1017 ni env (PTyped l r) = ni env l ++ ni env r
1018 ni env (PPair _ l r) = ni env l ++ ni env r
1019 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1020 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1021 ni env (PAlternative a as) = concatMap (ni env) as
1022 ni env (PHidden tm) = ni env tm
1023 ni env _ = []