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