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