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