on 02fa360 help sign in
Idris-dev/src/Idris/AbsSyntaxTree.hs
1170 lines 42 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
13import Util.DynamicLinker
15import Paths_idris
17import System.Console.Haskeline
19import Control.Monad.Trans.State.Strict
21import Data.List
22import Data.Char
23import Data.Either
25import Debug.Trace
27data IOption = IOption { opt_logLevel :: Int,
28 opt_typecase :: Bool,
29 opt_typeintype :: Bool,
30 opt_coverage :: Bool,
31 opt_showimp :: Bool,
32 opt_errContext :: Bool,
33 opt_repl :: Bool,
34 opt_verbose :: Bool,
35 opt_quiet :: Bool,
36 opt_target :: Target,
37 opt_outputTy :: OutputType,
38 opt_ibcsubdir :: FilePath,
39 opt_importdirs :: [FilePath],
40 opt_cmdline :: [Opt] -- remember whole command line
41 }
42 deriving (Show, Eq)
44defaultOpts = IOption 0 False False True False False True True False ViaC Executable "" [] []
46data LanguageExt = TypeProviders deriving (Show, Eq, Read, Ord)
48-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
49-- without typechecking).
50-- This will include all the functions and data declarations, plus fixity declarations
51-- and syntax macros.
53-- | The global state used in the Idris monad
54data IState = IState {
55 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
56 idris_constraints :: [(UConstraint, FC)],
57 -- ^ A list of universe constraints and their corresponding source locations
58 idris_infixes :: [FixDecl], -- ^ Currently defined infix operators
59 idris_implicits :: Ctxt [PArg],
60 idris_statics :: Ctxt [Bool],
61 idris_classes :: Ctxt ClassInfo,
62 idris_dsls :: Ctxt DSL,
63 idris_optimisation :: Ctxt OptInfo,
64 idris_datatypes :: Ctxt TypeInfo,
65 idris_patdefs :: Ctxt [([Name], Term, Term)], -- not exported
66 idris_flags :: Ctxt [FnOpt],
67 idris_callgraph :: Ctxt CGInfo, -- name, args used in each pos
68 idris_calledgraph :: Ctxt [Name],
69 idris_docstrings :: Ctxt String,
70 idris_totcheck :: [(FC, Name)],
71 idris_log :: String,
72 idris_options :: IOption,
73 idris_name :: Int,
74 idris_metavars :: [Name], -- ^ The currently defined but not proven metavariables
75 idris_coercions :: [Name],
76 syntax_rules :: [Syntax],
77 syntax_keywords :: [String],
78 imported :: [FilePath], -- ^ The imported modules
79 idris_scprims :: [(Name, (Int, PrimFn))],
80 idris_objs :: [FilePath],
81 idris_libs :: [String],
82 idris_hdrs :: [String],
83 proof_list :: [(Name, [String])],
84 errLine :: Maybe Int,
85 lastParse :: Maybe Name,
86 indent_stack :: [Int],
87 brace_stack :: [Maybe Int],
88 hide_list :: [(Name, Maybe Accessibility)],
89 default_access :: Accessibility,
90 default_total :: Bool,
91 ibc_write :: [IBCWrite],
92 compiled_so :: Maybe String,
93 idris_dynamic_libs :: [DynamicLib],
94 idris_language_extensions :: [LanguageExt]
95 }
97data SizeChange = Smaller | Same | Bigger | Unknown
98 deriving (Show, Eq)
99{-!
100deriving instance Binary SizeChange
101!-}
103type SCGEntry = (Name, [Maybe (Int, SizeChange)])
105data CGInfo = CGInfo { argsdef :: [Name],
106 calls :: [(Name, [[Name]])],
107 scg :: [SCGEntry],
108 argsused :: [Name],
109 unusedpos :: [Int] }
110 deriving Show
111{-!
112deriving instance Binary CGInfo
113!-}
115primDefs = [UN "unsafePerformIO", UN "mkLazyForeign", UN "mkForeign", UN "FalseElim"]
117-- information that needs writing for the current module's .ibc file
118data IBCWrite = IBCFix FixDecl
119 | IBCImp Name
120 | IBCStatic Name
121 | IBCClass Name
122 | IBCInstance Bool Name Name
123 | IBCDSL Name
124 | IBCData Name
125 | IBCOpt Name
126 | IBCSyntax Syntax
127 | IBCKeyword String
128 | IBCImport FilePath
129 | IBCObj FilePath
130 | IBCLib String
131 | IBCDyLib String
132 | IBCHeader String
133 | IBCAccess Name Accessibility
134 | IBCTotal Name Totality
135 | IBCFlags Name [FnOpt]
136 | IBCCG Name
137 | IBCDoc Name
138 | IBCCoercion Name
139 | IBCDef Name -- i.e. main context
140 deriving Show
142idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
143 emptyContext emptyContext emptyContext emptyContext
144 emptyContext emptyContext emptyContext emptyContext
145 [] "" defaultOpts 6 [] [] [] [] [] [] [] [] []
146 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] []
148-- | The monad for the main REPL - reading and processing files and updating
149-- global state (hence the IO inner monad).
150type Idris = StateT IState IO
152-- Commands in the REPL
154data Target = ViaC
155 | ViaJava
156 | ViaNode
157 | ViaJavaScript
158 | Bytecode
159 deriving (Show, Eq)
161-- | REPL commands
162data Command = Quit
163 | Help
164 | Eval PTerm
165 | Check PTerm
166 | DocStr Name
167 | TotCheck Name
168 | Reload
169 | Load FilePath
170 | ChangeDirectory FilePath
171 | ModImport String
172 | Edit
173 | Compile Target String
174 | Execute
175 | ExecVal PTerm
176 | NewCompile String
177 | Metavars
178 | Prove Name
179 | AddProof (Maybe Name)
180 | RmProof Name
181 | ShowProof Name
182 | Proofs
183 | Universes
184 | TTShell
185 | LogLvl Int
186 | Spec PTerm
187 | HNF PTerm
188 | Defn Name
189 | Info Name
190 | Missing Name
191 | DynamicLink FilePath
192 | ListDynamic
193 | Pattelab PTerm
194 | DebugInfo Name
195 | Search PTerm
196 | SetOpt Opt
197 | UnsetOpt Opt
198 | NOP
200data Opt = Filename String
201 | Ver
202 | Usage
203 | Quiet
204 | ShowLibs
205 | ShowLibdir
206 | ShowIncs
207 | NoPrelude
208 | NoREPL
209 | OLogging Int
210 | Output String
211 | NewOutput String
212 | TypeCase
213 | TypeInType
214 | DefaultTotal
215 | DefaultPartial
216 | WarnPartial
217 | NoCoverage
218 | ErrContext
219 | ShowImpl
220 | Verbose
221 | IBCSubDir String
222 | ImportDir String
223 | PkgBuild String
224 | PkgInstall String
225 | PkgClean String
226 | WarnOnly
227 | Pkg String
228 | BCAsm String
229 | DumpDefun String
230 | DumpCases String
231 | FOVM String
232 | UseTarget Target
233 | OutputTy OutputType
234 | Extension LanguageExt
235 deriving (Show, Eq)
237-- Parsed declarations
239data Fixity = Infixl { prec :: Int }
240 | Infixr { prec :: Int }
241 | InfixN { prec :: Int }
242 | PrefixN { prec :: Int }
243 deriving Eq
244{-!
245deriving instance Binary Fixity
246!-}
248instance Show Fixity where
249 show (Infixl i) = "infixl " ++ show i
250 show (Infixr i) = "infixr " ++ show i
251 show (InfixN i) = "infix " ++ show i
252 show (PrefixN i) = "prefix " ++ show i
254data FixDecl = Fix Fixity String
255 deriving Eq
257instance Show FixDecl where
258 show (Fix f s) = show f ++ " " ++ s
260{-!
261deriving instance Binary FixDecl
262!-}
264instance Ord FixDecl where
265 compare (Fix x _) (Fix y _) = compare (prec x) (prec y)
268data Static = Static | Dynamic
269 deriving (Show, Eq)
270{-!
271deriving instance Binary Static
272!-}
274-- Mark bindings with their explicitness, and laziness
275data Plicity = Imp { plazy :: Bool,
276 pstatic :: Static,
277 pdocstr :: String }
278 | Exp { plazy :: Bool,
279 pstatic :: Static,
280 pdocstr :: String }
281 | Constraint { plazy :: Bool,
282 pstatic :: Static,
283 pdocstr :: String }
284 | TacImp { plazy :: Bool,
285 pstatic :: Static,
286 pscript :: PTerm,
287 pdocstr :: String }
288 deriving (Show, Eq)
290{-!
291deriving instance Binary Plicity
292!-}
294impl = Imp False Dynamic ""
295expl = Exp False Dynamic ""
296constraint = Constraint False Dynamic ""
297tacimpl t = TacImp False Dynamic t ""
299data FnOpt = Inlinable | TotalFn | PartialFn
300 | Coinductive | AssertTotal | TCGen
301 | Implicit -- implicit coercion
302 | CExport String -- export, with a C name
303 | Specialise [Name] -- specialise it, freeze these names
304 deriving (Show, Eq)
305{-!
306deriving instance Binary FnOpt
307!-}
309type FnOpts = [FnOpt]
311inlinable :: FnOpts -> Bool
312inlinable = elem Inlinable
314-- | Top-level declarations such as compiler directives, definitions,
315-- datatypes and typeclasses.
316data PDecl' t
317 = PFix FC Fixity [String] -- ^ Fixity declaration
318 | PTy String SyntaxInfo FC FnOpts Name t -- ^ Type declaration
319 | PPostulate String SyntaxInfo FC FnOpts Name t -- ^ Postulate
320 | PClauses FC FnOpts Name [PClause' t] -- ^ Pattern clause
321 | PCAF FC Name t -- ^ Top level constant
322 | PData String SyntaxInfo FC Bool (PData' t) -- ^ Data declaration. The Bool argument is True for codata.
323 | PParams FC [(Name, t)] [PDecl' t] -- ^ Params block
324 | PNamespace String [PDecl' t] -- ^ New namespace
325 | PRecord String SyntaxInfo FC Name t String Name t -- ^ Record declaration
326 | PClass String SyntaxInfo FC
327 [t] -- constraints
328 Name
329 [(Name, t)] -- parameters
330 [PDecl' t] -- declarations
331 -- ^ Type class: arguments are documentation, syntax info, source location, constraints,
332 -- class name, parameters, method declarations
333 | PInstance SyntaxInfo FC [t] -- constraints
334 Name -- class
335 [t] -- parameters
336 t -- full instance type
337 (Maybe Name) -- explicit name
338 [PDecl' t]
339 -- ^ Instance declaration: arguments are syntax info, source location, constraints,
340 -- class name, parameters, full instance type, optional explicit name, and definitions
341 | PDSL Name (DSL' t) -- ^ DSL declaration
342 | PSyntax FC Syntax -- ^ Syntax definition
343 | PMutual FC [PDecl' t] -- ^ Mutual block
344 | PDirective (Idris ()) -- ^ Compiler directive. The parser inserts the corresponding action in the Idris monad.
345 | PProvider SyntaxInfo FC Name t t -- ^ Type provider. The first t is the type, the second is the term
346 deriving Functor
347{-!
348deriving instance Binary PDecl'
349!-}
351-- | One clause of a top-level definition. Term arguments to constructors are:
352--
353-- 1. The whole application (missing for PClauseR and PWithR because they're within a "with" clause)
354--
355-- 2. The list of patterns
356--
357-- 3. The right-hand side
358--
359-- 4. The where block (PDecl' t)
360data PClause' t = PClause FC Name t [t] t [PDecl' t] -- ^ A normal top-level definition.
361 | PWith FC Name t [t] t [PDecl' t]
362 | PClauseR FC [t] t [PDecl' t]
363 | PWithR FC [t] t [PDecl' t]
364 deriving Functor
365{-!
366deriving instance Binary PClause'
367!-}
369-- | Data declaration
370data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
371 d_tcon :: t, -- ^ Type constructor
372 d_cons :: [(String, Name, t, FC)] -- ^ Constructors
373 }
374 -- ^ Data declaration
375 | PLaterdecl { d_name :: Name, d_tcon :: t }
376 -- ^ "Placeholder" for data whose constructors are defined later
377 deriving Functor
378{-!
379deriving instance Binary PData'
380!-}
382-- Handy to get a free function for applying PTerm -> PTerm functions
383-- across a program, by deriving Functor
385type PDecl = PDecl' PTerm
386type PData = PData' PTerm
387type PClause = PClause' PTerm
389-- get all the names declared in a decl
391declared :: PDecl -> [Name]
392declared (PFix _ _ _) = []
393declared (PTy _ _ _ _ n t) = [n]
394declared (PPostulate _ _ _ _ n t) = [n]
395declared (PClauses _ _ n _) = [] -- not a declaration
396declared (PCAF _ n _) = [n]
397declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
398 where fstt (_, a, _, _) = a
399declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
400declared (PParams _ _ ds) = concatMap declared ds
401declared (PNamespace _ ds) = concatMap declared ds
402declared (PRecord _ _ _ n _ _ c _) = [n, c]
403declared (PClass _ _ _ _ n _ ms) = n : concatMap declared ms
404declared (PInstance _ _ _ _ _ _ _ _) = []
405declared (PDSL n _) = [n]
406declared (PSyntax _ _) = []
407declared (PMutual _ ds) = concatMap declared ds
408declared (PDirective _) = []
410-- get the names declared, not counting nested parameter blocks
411tldeclared :: PDecl -> [Name]
412tldeclared (PFix _ _ _) = []
413tldeclared (PTy _ _ _ _ n t) = [n]
414tldeclared (PPostulate _ _ _ _ n t) = [n]
415tldeclared (PClauses _ _ n _) = [] -- not a declaration
416tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
417tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
418 where fstt (_, a, _, _) = a
419tldeclared (PParams _ _ ds) = []
420tldeclared (PMutual _ ds) = concatMap tldeclared ds
421tldeclared (PNamespace _ ds) = concatMap tldeclared ds
424defined :: PDecl -> [Name]
425defined (PFix _ _ _) = []
426defined (PTy _ _ _ _ n t) = []
427defined (PPostulate _ _ _ _ n t) = []
428defined (PClauses _ _ n _) = [n] -- not a declaration
429defined (PCAF _ n _) = [n]
430defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
431 where fstt (_, a, _, _) = a
432defined (PData _ _ _ _ (PLaterdecl n _)) = []
433defined (PParams _ _ ds) = concatMap defined ds
434defined (PNamespace _ ds) = concatMap defined ds
435defined (PRecord _ _ _ n _ _ c _) = [n, c]
436defined (PClass _ _ _ _ n _ ms) = n : concatMap defined ms
437defined (PInstance _ _ _ _ _ _ _ _) = []
438defined (PDSL n _) = [n]
439defined (PSyntax _ _) = []
440defined (PMutual _ ds) = concatMap defined ds
441defined (PDirective _) = []
442--defined _ = []
444updateN :: [(Name, Name)] -> Name -> Name
445updateN ns n | Just n' <- lookup n ns = n'
446updateN _ n = n
448updateNs :: [(Name, Name)] -> PTerm -> PTerm
449updateNs [] t = t
450updateNs ns t = mapPT updateRef t
451 where updateRef (PRef fc f) = PRef fc (updateN ns f)
452 updateRef t = t
454-- updateDNs :: [(Name, Name)] -> PDecl -> PDecl
455-- updateDNs [] t = t
456-- updateDNs ns (PTy s f n t) | Just n' <- lookup n ns = PTy s f n' t
457-- updateDNs ns (PClauses f n c) | Just n' <- lookup n ns = PClauses f n' (map updateCNs c)
458-- where updateCNs ns (PClause n l ts r ds)
459-- = PClause (updateN ns n) (fmap (updateNs ns) l)
460-- (map (fmap (updateNs ns)) ts)
461-- (fmap (updateNs ns) r)
462-- (map (updateDNs ns) ds)
463-- updateDNs ns c = c
465-- | High level language terms
466data PTerm = PQuote Raw
467 | PRef FC Name
468 | PInferRef FC Name -- ^ A name to be defined later
469 | PPatvar FC Name
470 | PLam Name PTerm PTerm
471 | PPi Plicity Name PTerm PTerm
472 | PLet Name PTerm PTerm PTerm
473 | PTyped PTerm PTerm -- ^ Term with explicit type
474 | PApp FC PTerm [PArg]
475 | PCase FC PTerm [(PTerm, PTerm)]
476 | PTrue FC
477 | PFalse FC
478 | PRefl FC PTerm
479 | PResolveTC FC
480 | PEq FC PTerm PTerm
481 | PRewrite FC PTerm PTerm
482 | PPair FC PTerm PTerm
483 | PDPair FC PTerm PTerm PTerm
484 | PAlternative Bool [PTerm] -- True if only one may work
485 | PHidden PTerm -- ^ Irrelevant or hidden pattern
486 | PType
487 | PConstant Const
488 | Placeholder
489 | PDoBlock [PDo]
490 | PIdiom FC PTerm
491 | PReturn FC
492 | PMetavar Name
493 | PProof [PTactic] -- ^ Proof script
494 | PTactics [PTactic] -- ^ As PProof, but no auto solving
495 | PElabError Err -- ^ Error to report on elaboration
496 | PImpossible -- ^ Special case for declaring when an LHS can't typecheck
497 | PCoerced PTerm -- ^ To mark a coerced argument, so as not to coerce twice
498 deriving Eq
499{-!
500deriving instance Binary PTerm
501!-}
503mapPT :: (PTerm -> PTerm) -> PTerm -> PTerm
504mapPT f t = f (mpt t) where
505 mpt (PLam n t s) = PLam n (mapPT f t) (mapPT f s)
506 mpt (PPi p n t s) = PPi p n (mapPT f t) (mapPT f s)
507 mpt (PLet n ty v s) = PLet n (mapPT f ty) (mapPT f v) (mapPT f s)
508 mpt (PRewrite fc t s) = PRewrite fc (mapPT f t) (mapPT f s)
509 mpt (PApp fc t as) = PApp fc (mapPT f t) (map (fmap (mapPT f)) as)
510 mpt (PCase fc c os) = PCase fc (mapPT f c) (map (pmap (mapPT f)) os)
511 mpt (PEq fc l r) = PEq fc (mapPT f l) (mapPT f r)
512 mpt (PTyped l r) = PTyped (mapPT f l) (mapPT f r)
513 mpt (PPair fc l r) = PPair fc (mapPT f l) (mapPT f r)
514 mpt (PDPair fc l t r) = PDPair fc (mapPT f l) (mapPT f t) (mapPT f r)
515 mpt (PAlternative a as) = PAlternative a (map (mapPT f) as)
516 mpt (PHidden t) = PHidden (mapPT f t)
517 mpt (PDoBlock ds) = PDoBlock (map (fmap (mapPT f)) ds)
518 mpt (PProof ts) = PProof (map (fmap (mapPT f)) ts)
519 mpt (PTactics ts) = PTactics (map (fmap (mapPT f)) ts)
520 mpt x = x
523data PTactic' t = Intro [Name] | Intros | Focus Name
524 | Refine Name [Bool] | Rewrite t
525 | LetTac Name t | LetTacTy Name t t
526 | Exact t | Compute | Trivial
527 | Solve
528 | Attack
529 | ProofState | ProofTerm | Undo
530 | Try (PTactic' t) (PTactic' t)
531 | TSeq (PTactic' t) (PTactic' t)
532 | ApplyTactic t -- see Language.Reflection module
533 | Reflect t
534 | Fill t
535 | GoalType String (PTactic' t)
536 | Qed | Abandon
537 deriving (Show, Eq, Functor)
538{-!
539deriving instance Binary PTactic'
540!-}
542instance Sized a => Sized (PTactic' a) where
543 size (Intro nms) = 1 + size nms
544 size Intros = 1
545 size (Focus nm) = 1 + size nm
546 size (Refine nm bs) = 1 + size nm + length bs
547 size (Rewrite t) = 1 + size t
548 size (LetTac nm t) = 1 + size nm + size t
549 size (Exact t) = 1 + size t
550 size Compute = 1
551 size Trivial = 1
552 size Solve = 1
553 size Attack = 1
554 size ProofState = 1
555 size ProofTerm = 1
556 size Undo = 1
557 size (Try l r) = 1 + size l + size r
558 size (TSeq l r) = 1 + size l + size r
559 size (ApplyTactic t) = 1 + size t
560 size (Reflect t) = 1 + size t
561 size (Fill t) = 1 + size t
562 size Qed = 1
563 size Abandon = 1
565type PTactic = PTactic' PTerm
567data PDo' t = DoExp FC t
568 | DoBind FC Name t
569 | DoBindP FC t t
570 | DoLet FC Name t t
571 | DoLetP FC t t
572 deriving (Eq, Functor)
573{-!
574deriving instance Binary PDo'
575!-}
577instance Sized a => Sized (PDo' a) where
578 size (DoExp fc t) = 1 + size fc + size t
579 size (DoBind fc nm t) = 1 + size fc + size nm + size t
580 size (DoBindP fc l r) = 1 + size fc + size l + size r
581 size (DoLet fc nm l r) = 1 + size fc + size nm + size l + size r
582 size (DoLetP fc l r) = 1 + size fc + size l + size r
584type PDo = PDo' PTerm
586-- The priority gives a hint as to elaboration order. Best to elaborate
587-- things early which will help give a more concrete type to other
588-- variables, e.g. a before (interpTy a).
589-- TODO: priority no longer serves any purpose, drop it!
591data PArg' t = PImp { priority :: Int,
592 lazyarg :: Bool, pname :: Name, getTm :: t,
593 pargdoc :: String }
594 | PExp { priority :: Int,
595 lazyarg :: Bool, getTm :: t,
596 pargdoc :: String }
597 | PConstraint { priority :: Int,
598 lazyarg :: Bool, getTm :: t,
599 pargdoc :: String }
600 | PTacImplicit { priority :: Int,
601 lazyarg :: Bool, pname :: Name,
602 getScript :: t,
603 getTm :: t,
604 pargdoc :: String }
605 deriving (Show, Eq, Functor)
607instance Sized a => Sized (PArg' a) where
608 size (PImp p l nm trm _) = 1 + size nm + size trm
609 size (PExp p l trm _) = 1 + size trm
610 size (PConstraint p l trm _) = 1 + size trm
611 size (PTacImplicit p l nm scr trm _) = 1 + size nm + size scr + size trm
613{-!
614deriving instance Binary PArg'
615!-}
617pimp n t = PImp 0 True n t ""
618pexp t = PExp 0 False t ""
619pconst t = PConstraint 0 False t ""
620ptacimp n s t = PTacImplicit 0 True n s t ""
622type PArg = PArg' PTerm
624-- Type class data
626data ClassInfo = CI { instanceName :: Name,
627 class_methods :: [(Name, (FnOpts, PTerm))],
628 class_defaults :: [(Name, (Name, PDecl))], -- method name -> default impl
629 class_params :: [Name],
630 class_instances :: [Name] }
631 deriving Show
632{-!
633deriving instance Binary ClassInfo
634!-}
636data OptInfo = Optimise { collapsible :: Bool,
637 forceable :: [Int], -- argument positions
638 recursive :: [Int] }
639 deriving Show
640{-!
641deriving instance Binary OptInfo
642!-}
645data TypeInfo = TI { con_names :: [Name],
646 codata :: Bool,
647 param_pos :: [Int] }
648 deriving Show
649{-!
650deriving instance Binary TypeInfo
651!-}
653-- Syntactic sugar info
655data DSL' t = DSL { dsl_bind :: t,
656 dsl_return :: t,
657 dsl_apply :: t,
658 dsl_pure :: t,
659 dsl_var :: Maybe t,
660 index_first :: Maybe t,
661 index_next :: Maybe t,
662 dsl_lambda :: Maybe t,
663 dsl_let :: Maybe t
664 }
665 deriving (Show, Functor)
666{-!
667deriving instance Binary DSL'
668!-}
670type DSL = DSL' PTerm
672data SynContext = PatternSyntax | TermSyntax | AnySyntax
673 deriving Show
674{-!
675deriving instance Binary SynContext
676!-}
678data Syntax = Rule [SSymbol] PTerm SynContext
679 deriving Show
680{-!
681deriving instance Binary Syntax
682!-}
684data SSymbol = Keyword Name
685 | Symbol String
686 | Binding Name
687 | Expr Name
688 | SimpleExpr Name
689 deriving Show
690{-!
691deriving instance Binary SSymbol
692!-}
694initDSL = DSL (PRef f (UN ">>="))
695 (PRef f (UN "return"))
696 (PRef f (UN "<$>"))
697 (PRef f (UN "pure"))
698 Nothing
699 Nothing
700 Nothing
701 Nothing
702 Nothing
703 where f = FC "(builtin)" 0
705data SyntaxInfo = Syn { using :: [(Name, PTerm)],
706 syn_params :: [(Name, PTerm)],
707 syn_namespace :: [String],
708 no_imp :: [Name],
709 decoration :: Name -> Name,
710 inPattern :: Bool,
711 implicitAllowed :: Bool,
712 dsl_info :: DSL }
713 deriving Show
714{-!
715deriving instance Binary SyntaxInfo
716!-}
718defaultSyntax = Syn [] [] [] [] id False False initDSL
720expandNS :: SyntaxInfo -> Name -> Name
721expandNS syn n@(NS _ _) = n
722expandNS syn n = case syn_namespace syn of
723 [] -> n
724 xs -> NS n xs
727--- Pretty printing declarations and terms
729instance Show PTerm where
730 show tm = showImp False tm
732instance Pretty PTerm where
733 pretty = prettyImp False
735instance Show PDecl where
736 show d = showDeclImp False d
738instance Show PClause where
739 show c = showCImp True c
741instance Show PData where
742 show d = showDImp False d
744showDeclImp _ (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
745showDeclImp t (PTy _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
746showDeclImp t (PPostulate _ _ _ _ n ty) = show n ++ " : " ++ showImp t ty
747showDeclImp _ (PClauses _ _ n c) = showSep "\n" (map show c)
748showDeclImp _ (PData _ _ _ _ d) = show d
749showDeclImp _ (PParams f ns ps) = "parameters " ++ show ns ++ "\n" ++
750 showSep "\n" (map show ps)
753showCImp :: Bool -> PClause -> String
754showCImp impl (PClause _ n l ws r w)
755 = showImp impl l ++ showWs ws ++ " = " ++ showImp impl r
756 ++ " where " ++ show w
757 where
758 showWs [] = ""
759 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
760showCImp impl (PWith _ n l ws r w)
761 = showImp impl l ++ showWs ws ++ " with " ++ showImp impl r
762 ++ " { " ++ show w ++ " } "
763 where
764 showWs [] = ""
765 showWs (x : xs) = " | " ++ showImp impl x ++ showWs xs
768showDImp :: Bool -> PData -> String
769showDImp impl (PDatadecl n ty cons)
770 = "data " ++ show n ++ " : " ++ showImp impl ty ++ " where\n\t"
771 ++ showSep "\n\t| "
772 (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp impl t) cons)
774getImps :: [PArg] -> [(Name, PTerm)]
775getImps [] = []
776getImps (PImp _ _ n tm _ : xs) = (n, tm) : getImps xs
777getImps (_ : xs) = getImps xs
779getExps :: [PArg] -> [PTerm]
780getExps [] = []
781getExps (PExp _ _ tm _ : xs) = tm : getExps xs
782getExps (_ : xs) = getExps xs
784getConsts :: [PArg] -> [PTerm]
785getConsts [] = []
786getConsts (PConstraint _ _ tm _ : xs) = tm : getConsts xs
787getConsts (_ : xs) = getConsts xs
789getAll :: [PArg] -> [PTerm]
790getAll = map getTm
792prettyImp :: Bool -> PTerm -> Doc
793prettyImp impl = prettySe 10
794 where
795 prettySe p (PQuote r) =
796 if size r > breakingSize then
797 text "![" $$ pretty r <> text "]"
798 else
799 text "![" <> pretty r <> text "]"
800 prettySe p (PPatvar fc n) = pretty n
801 prettySe p (PRef fc n) =
802 if impl then
803 pretty n
804 else
805 prettyBasic n
806 where
807 prettyBasic n@(UN _) = pretty n
808 prettyBasic (MN _ s) = text s
809 prettyBasic (NS n s) = (foldr (<>) empty (intersperse (text ".") (map text $ reverse s))) <> prettyBasic n
810 prettySe p (PLam n ty sc) =
811 bracket p 2 $
812 if size sc > breakingSize then
813 text "λ" <> pretty n <+> text "=>" $+$ pretty sc
814 else
815 text "λ" <> pretty n <+> text "=>" <+> pretty sc
816 prettySe p (PLet n ty v sc) =
817 bracket p 2 $
818 if size sc > breakingSize then
819 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" $+$
820 nest nestingSize (prettySe 10 sc)
821 else
822 text "let" <+> pretty n <+> text "=" <+> prettySe 10 v <+> text "in" <+>
823 prettySe 10 sc
824 prettySe p (PPi (Exp l s _) n ty sc)
825 | n `elem` allNamesIn sc || impl =
826 let open = if l then text "|" <> lparen else lparen in
827 bracket p 2 $
828 if size sc > breakingSize then
829 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
830 st <+> text "->" $+$ prettySe 10 sc
831 else
832 open <> pretty n <+> colon <+> prettySe 10 ty <> rparen <+>
833 st <+> text "->" <+> prettySe 10 sc
834 | otherwise =
835 bracket p 2 $
836 if size sc > breakingSize then
837 prettySe 0 ty <+> st <+> text "->" $+$ prettySe 10 sc
838 else
839 prettySe 0 ty <+> st <+> text "->" <+> prettySe 10 sc
840 where
841 st =
842 case s of
843 Static -> text "[static]"
844 _ -> empty
845 prettySe p (PPi (Imp l s _) n ty sc)
846 | impl =
847 let open = if l then text "|" <> lbrace else lbrace in
848 bracket p 2 $
849 if size sc > breakingSize then
850 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
851 st <+> text "->" <+> prettySe 10 sc
852 else
853 open <> pretty n <+> colon <+> prettySe 10 ty <> rbrace <+>
854 st <+> text "->" <+> prettySe 10 sc
855 | otherwise = prettySe 10 sc
856 where
857 st =
858 case s of
859 Static -> text $ "[static]"
860 _ -> empty
861 prettySe p (PPi (Constraint _ _ _) n ty sc) =
862 bracket p 2 $
863 if size sc > breakingSize then
864 prettySe 10 ty <+> text "=>" <+> prettySe 10 sc
865 else
866 prettySe 10 ty <+> text "=>" $+$ prettySe 10 sc
867 prettySe p (PPi (TacImp _ _ s _) n ty sc) =
868 bracket p 2 $
869 if size sc > breakingSize then
870 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
871 rbrace <+> text "->" $+$ prettySe 10 sc
872 else
873 lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 ty <>
874 rbrace <+> text "->" <+> prettySe 10 sc
875 prettySe p (PApp _ (PRef _ f) [])
876 | not impl = pretty f
877 prettySe p (PApp _ (PRef _ op@(UN (f:_))) args)
878 | length (getExps args) == 2 && (not impl) && (not $ isAlpha f) =
879 let [l, r] = getExps args in
880 bracket p 1 $
881 if size r > breakingSize then
882 prettySe 1 l <+> pretty op $+$ prettySe 0 r
883 else
884 prettySe 1 l <+> pretty op <+> prettySe 0 r
885 prettySe p (PApp _ f as) =
886 let args = getExps as in
887 bracket p 1 $
888 prettySe 1 f <+>
889 if impl then
890 foldl fS empty as
891 -- foldr (<+>) empty $ map prettyArgS as
892 else
893 foldl fSe empty args
894 -- foldr (<+>) empty $ map prettyArgSe args
895 where
896 fS l r =
897 if size r > breakingSize then
898 l $+$ nest nestingSize (prettyArgS r)
899 else
900 l <+> prettyArgS r
902 fSe l r =
903 if size r > breakingSize then
904 l $+$ nest nestingSize (prettyArgSe r)
905 else
906 l <+> prettyArgSe r
907 prettySe p (PCase _ scr opts) =
908 text "case" <+> prettySe 10 scr <+> text "of" $+$ nest nestingSize prettyBody
909 where
910 prettyBody = foldr ($$) empty $ intersperse (text "|") $ map sc opts
912 sc (l, r) = prettySe 10 l <+> text "=>" <+> prettySe 10 r
913 prettySe p (PHidden tm) = text "." <> prettySe 0 tm
914 prettySe p (PRefl _ _) = text "refl"
915 prettySe p (PResolveTC _) = text "resolvetc"
916 prettySe p (PTrue _) = text "()"
917 prettySe p (PFalse _) = text "_|_"
918 prettySe p (PEq _ l r) =
919 bracket p 2 $
920 if size r > breakingSize then
921 prettySe 10 l <+> text "=" $$ nest nestingSize (prettySe 10 r)
922 else
923 prettySe 10 l <+> text "=" <+> prettySe 10 r
924 prettySe p (PRewrite _ l r) =
925 bracket p 2 $
926 if size r > breakingSize then
927 text "rewrite" <+> prettySe 10 l <+> text "in" $$ nest nestingSize (prettySe 10 r)
928 else
929 text "rewrite" <+> prettySe 10 l <+> text "in" <+> prettySe 10 r
930 prettySe p (PTyped l r) =
931 lparen <> prettySe 10 l <+> colon <+> prettySe 10 r <> rparen
932 prettySe p (PPair _ l r) =
933 if size r > breakingSize then
934 lparen <> prettySe 10 l <> text "," $+$
935 prettySe 10 r <> rparen
936 else
937 lparen <> prettySe 10 l <> text "," <+> prettySe 10 r <> rparen
938 prettySe p (PDPair _ l t r) =
939 if size r > breakingSize then
940 lparen <> prettySe 10 l <+> text "**" $+$
941 prettySe 10 r <> rparen
942 else
943 lparen <> prettySe 10 l <+> text "**" <+> prettySe 10 r <> rparen
944 prettySe p (PAlternative a as) =
945 lparen <> text "|" <> prettyAs <> text "|" <> rparen
946 where
947 prettyAs =
948 foldr (\l -> \r -> l <+> text "," <+> r) empty $ map (prettySe 10) as
949 prettySe p PType = text "Type"
950 prettySe p (PConstant c) = pretty c
951 -- XXX: add pretty for tactics
952 prettySe p (PProof ts) =
953 text "proof" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
954 prettySe p (PTactics ts) =
955 text "tactics" <+> lbrace $+$ nest nestingSize (text . show $ ts) $+$ rbrace
956 prettySe p (PMetavar n) = text "?" <> pretty n
957 prettySe p (PReturn f) = text "return"
958 prettySe p PImpossible = text "impossible"
959 prettySe p Placeholder = text "_"
960 prettySe p (PDoBlock _) = text "do block pretty not implemented"
961 prettySe p (PElabError s) = pretty s
963 prettySe p _ = text "test"
965 prettyArgS (PImp _ _ n tm _) = prettyArgSi (n, tm)
966 prettyArgS (PExp _ _ tm _) = prettyArgSe tm
967 prettyArgS (PConstraint _ _ tm _) = prettyArgSc tm
968 prettyArgS (PTacImplicit _ _ n _ tm _) = prettyArgSti (n, tm)
970 prettyArgSe arg = prettySe 0 arg
971 prettyArgSi (n, val) = lbrace <> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
972 prettyArgSc val = lbrace <> lbrace <> prettySe 10 val <> rbrace <> rbrace
973 prettyArgSti (n, val) = lbrace <> text "auto" <+> pretty n <+> text "=" <+> prettySe 10 val <> rbrace
975 bracket outer inner doc
976 | inner > outer = lparen <> doc <> rparen
977 | otherwise = doc
979showImp :: Bool -> PTerm -> String
980showImp impl tm = se 10 tm where
981 se p (PQuote r) = "![" ++ show r ++ "]"
982 se p (PPatvar fc n) = show n
983 se p (PInferRef fc n) = "!" ++ show n -- ++ "[" ++ show fc ++ "]"
984 se p (PRef fc n) = if impl then show n -- ++ "[" ++ show fc ++ "]"
985 else showbasic n
986 where showbasic n@(UN _) = show n
987 showbasic (MN _ s) = s
988 showbasic (NS n s) = showSep "." (reverse s) ++ "." ++ showbasic n
989 se p (PLam n ty sc) = bracket p 2 $ "\\ " ++ show n ++
990 (if impl then " : " ++ se 10 ty else "") ++ " => "
991 ++ se 10 sc
992 se p (PLet n ty v sc) = bracket p 2 $ "let " ++ show n ++ " = " ++ se 10 v ++
993 " in " ++ se 10 sc
994 se p (PPi (Exp l s _) n ty sc)
995 | n `elem` allNamesIn sc || impl
996 = bracket p 2 $
997 (if l then "|(" else "(") ++
998 show n ++ " : " ++ se 10 ty ++
999 ") " ++ st ++
1000 "-> " ++ se 10 sc
1001 | otherwise = bracket p 2 $ se 0 ty ++ " " ++ st ++ "-> " ++ se 10 sc
1002 where st = case s of
1003 Static -> "[static] "
1004 _ -> ""
1005 se p (PPi (Imp l s _) n ty sc)
1006 | impl = bracket p 2 $ (if l then "|{" else "{") ++
1007 show n ++ " : " ++ se 10 ty ++
1008 "} " ++ st ++ "-> " ++ se 10 sc
1009 | otherwise = se 10 sc
1010 where st = case s of
1011 Static -> "[static] "
1012 _ -> ""
1013 se p (PPi (Constraint _ _ _) n ty sc)
1014 = bracket p 2 $ se 10 ty ++ " => " ++ se 10 sc
1015 se p (PPi (TacImp _ _ s _) n ty sc)
1016 = bracket p 2 $ "{tacimp " ++ show n ++ " : " ++ se 10 ty ++ "} -> " ++ se 10 sc
1017 se p (PApp _ (PRef _ f) [])
1018 | not impl = show f
1019 se p (PApp _ (PRef _ op@(UN (f:_))) args)
1020 | length (getExps args) == 2 && not impl && not (isAlpha f)
1021 = let [l, r] = getExps args in
1022 bracket p 1 $ se 1 l ++ " " ++ show op ++ " " ++ se 0 r
1023 se p (PApp _ f as)
1024 = let args = getExps as in
1025 bracket p 1 $ se 1 f ++ if impl then concatMap sArg as
1026 else concatMap seArg args
1027 se p (PCase _ scr opts) = "case " ++ se 10 scr ++ " of " ++ showSep " | " (map sc opts)
1028 where sc (l, r) = se 10 l ++ " => " ++ se 10 r
1029 se p (PHidden tm) = "." ++ se 0 tm
1030 se p (PRefl _ t)
1031 | not impl = "refl"
1032 | otherwise = "refl {" ++ se 10 t ++ "}"
1033 se p (PResolveTC _) = "resolvetc"
1034 se p (PTrue _) = "()"
1035 se p (PFalse _) = "_|_"
1036 se p (PEq _ l r) = bracket p 2 $ se 10 l ++ " = " ++ se 10 r
1037 se p (PRewrite _ l r) = bracket p 2 $ "rewrite " ++ se 10 l ++ " in " ++ se 10 r
1038 se p (PTyped l r) = "(" ++ se 10 l ++ " : " ++ se 10 r ++ ")"
1039 se p (PPair _ l r) = "(" ++ se 10 l ++ ", " ++ se 10 r ++ ")"
1040 se p (PDPair _ l t r) = "(" ++ se 10 l ++ " ** " ++ se 10 r ++ ")"
1041 se p (PAlternative a as) = "(|" ++ showSep " , " (map (se 10) as) ++ "|)"
1042 se p PType = "Type"
1043 se p (PConstant c) = show c
1044 se p (PProof ts) = "proof { " ++ show ts ++ "}"
1045 se p (PTactics ts) = "tactics { " ++ show ts ++ "}"
1046 se p (PMetavar n) = "?" ++ show n
1047 se p (PReturn f) = "return"
1048 se p PImpossible = "impossible"
1049 se p Placeholder = "_"
1050 se p (PDoBlock _) = "do block show not implemented"
1051 se p (PElabError s) = show s
1052 se p (PCoerced t) = se p t
1053-- se p x = "Not implemented"
1055 sArg (PImp _ _ n tm _) = siArg (n, tm)
1056 sArg (PExp _ _ tm _) = seArg tm
1057 sArg (PConstraint _ _ tm _) = scArg tm
1058 sArg (PTacImplicit _ _ n _ tm _) = stiArg (n, tm)
1060 seArg arg = " " ++ se 0 arg
1061 siArg (n, val) = " {" ++ show n ++ " = " ++ se 10 val ++ "}"
1062 scArg val = " {{" ++ se 10 val ++ "}}"
1063 stiArg (n, val) = " {auto " ++ show n ++ " = " ++ se 10 val ++ "}"
1065 bracket outer inner str | inner > outer = "(" ++ str ++ ")"
1066 | otherwise = str
1069instance Sized PTerm where
1070 size (PQuote rawTerm) = size rawTerm
1071 size (PRef fc name) = size name
1072 size (PLam name ty bdy) = 1 + size ty + size bdy
1073 size (PPi plicity name ty bdy) = 1 + size ty + size bdy
1074 size (PLet name ty def bdy) = 1 + size ty + size def + size bdy
1075 size (PTyped trm ty) = 1 + size trm + size ty
1076 size (PApp fc name args) = 1 + size args
1077 size (PCase fc trm bdy) = 1 + size trm + size bdy
1078 size (PTrue fc) = 1
1079 size (PFalse fc) = 1
1080 size (PRefl fc _) = 1
1081 size (PResolveTC fc) = 1
1082 size (PEq fc left right) = 1 + size left + size right
1083 size (PRewrite fc left right) = 1 + size left + size right
1084 size (PPair fc left right) = 1 + size left + size right
1085 size (PDPair fs left ty right) = 1 + size left + size ty + size right
1086 size (PAlternative a alts) = 1 + size alts
1087 size (PHidden hidden) = size hidden
1088 size PType = 1
1089 size (PConstant const) = 1 + size const
1090 size Placeholder = 1
1091 size (PDoBlock dos) = 1 + size dos
1092 size (PIdiom fc term) = 1 + size term
1093 size (PReturn fc) = 1
1094 size (PMetavar name) = 1
1095 size (PProof tactics) = size tactics
1096 size (PElabError err) = size err
1097 size PImpossible = 1
1099getPArity :: PTerm -> Int
1100getPArity (PPi _ _ _ sc) = 1 + getPArity sc
1101getPArity _ = 0
1103-- Return all names, free or globally bound, in the given term.
1105allNamesIn :: PTerm -> [Name]
1106allNamesIn tm = nub $ ni [] tm
1107 where
1108 ni env (PRef _ n)
1109 | not (n `elem` env) = [n]
1110 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1111 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1112 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1113 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1114 ni env (PHidden tm) = ni env tm
1115 ni env (PEq _ l r) = ni env l ++ ni env r
1116 ni env (PRewrite _ l r) = ni env l ++ ni env r
1117 ni env (PTyped l r) = ni env l ++ ni env r
1118 ni env (PPair _ l r) = ni env l ++ ni env r
1119 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1120 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1121 ni env (PAlternative a ls) = concatMap (ni env) ls
1122 ni env _ = []
1124-- Return names which are free in the given term.
1126namesIn :: [(Name, PTerm)] -> IState -> PTerm -> [Name]
1127namesIn uvars ist tm = nub $ ni [] tm
1128 where
1129 ni env (PRef _ n)
1130 | not (n `elem` env)
1131 = case lookupTy n (tt_ctxt ist) of
1132 [] -> [n]
1133 _ -> if n `elem` (map fst uvars) then [n] else []
1134 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1135 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1136 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1137 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1138 ni env (PEq _ l r) = ni env l ++ ni env r
1139 ni env (PRewrite _ l r) = ni env l ++ ni env r
1140 ni env (PTyped l r) = ni env l ++ ni env r
1141 ni env (PPair _ l r) = ni env l ++ ni env r
1142 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1143 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1144 ni env (PAlternative a as) = concatMap (ni env) as
1145 ni env (PHidden tm) = ni env tm
1146 ni env _ = []
1148-- Return which of the given names are used in the given term.
1150usedNamesIn :: [Name] -> IState -> PTerm -> [Name]
1151usedNamesIn vars ist tm = nub $ ni [] tm
1152 where
1153 ni env (PRef _ n)
1154 | n `elem` vars && not (n `elem` env)
1155 = case lookupTy n (tt_ctxt ist) of
1156 [] -> [n]
1157 _ -> []
1158 ni env (PApp _ f as) = ni env f ++ concatMap (ni env) (map getTm as)
1159 ni env (PCase _ c os) = ni env c ++ concatMap (ni env) (map snd os)
1160 ni env (PLam n ty sc) = ni env ty ++ ni (n:env) sc
1161 ni env (PPi _ n ty sc) = ni env ty ++ ni (n:env) sc
1162 ni env (PEq _ l r) = ni env l ++ ni env r
1163 ni env (PRewrite _ l r) = ni env l ++ ni env r
1164 ni env (PTyped l r) = ni env l ++ ni env r
1165 ni env (PPair _ l r) = ni env l ++ ni env r
1166 ni env (PDPair _ (PRef _ n) t r) = ni env t ++ ni (n:env) r
1167 ni env (PDPair _ l t r) = ni env l ++ ni env t ++ ni env r
1168 ni env (PAlternative a as) = concatMap (ni env) as
1169 ni env (PHidden tm) = ni env tm
1170 ni env _ = []