1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, DeriveFunctor,
2 TypeSynonymInstances, PatternGuards #-}
4module Idris.AbsSyntax(module Idris.AbsSyntax, module Idris.AbsSyntaxTree) where
8import Core.Elaborate hiding (Tactic(..))
10import Idris.AbsSyntaxTree
11import IRTS.CodegenCommon
12import Util.DynamicLinker
16import System.Console.Haskeline
18import Control.Monad.State
29getContext :: Idris Context
30getContext = do i <- getIState; return (tt_ctxt i)
32getObjectFiles :: Idris [FilePath]
33getObjectFiles = do i <- getIState; return (idris_objs i)
35addObjectFile :: FilePath -> Idris ()
36addObjectFile f = do i <- getIState; putIState $ i { idris_objs = f : idris_objs i }
38getLibs :: Idris [String]
39getLibs = do i <- getIState; return (idris_libs i)
41addLib :: String -> Idris ()
42addLib f = do i <- getIState; putIState $ i { idris_libs = f : idris_libs i }
44addDyLib :: [String] -> Idris (Either DynamicLib String)
45addDyLib libs = do i <- getIState
46 handle <- lift $ mapM (\l -> catchIO (tryLoadLib l) (\_ -> return Nothing)) libs
48 Nothing -> return (Right $ "Could not load dynamic alternatives \"" ++
49 concat (intersperse "," libs) ++ "\"")
50 Just x -> do let ls = idris_dynamic_libs i
51 putIState $ i { idris_dynamic_libs = x:ls }
54addHdr :: String -> Idris ()
55addHdr f = do i <- getIState; putIState $ i { idris_hdrs = f : idris_hdrs i }
57addLangExt :: LanguageExt -> Idris ()
58addLangExt TypeProviders = do i <- getIState ; putIState $ i { idris_language_extensions = [TypeProviders] }
60totcheck :: (FC, Name) -> Idris ()
61totcheck n = do i <- getIState; putIState $ i { idris_totcheck = idris_totcheck i ++ [n] }
63setFlags :: Name -> [FnOpt] -> Idris ()
64setFlags n fs = do i <- getIState; putIState $ i { idris_flags = addDef n fs (idris_flags i) }
66setAccessibility :: Name -> Accessibility -> Idris ()
69 let ctxt = setAccess n a (tt_ctxt i)
70 putIState $ i { tt_ctxt = ctxt }
72setTotality :: Name -> Totality -> Idris ()
75 let ctxt = setTotal n a (tt_ctxt i)
76 putIState $ i { tt_ctxt = ctxt }
78getTotality :: Name -> Idris Totality
81 case lookupTotal n (tt_ctxt i) of
83 _ -> return (Total [])
85-- Get coercions which might return the required type
86getCoercionsTo :: IState -> Type -> [Name]
88 let cs = idris_coercions i
89 (fn,_) = unApply (getRetTy ty) in
91 where findCoercions t [] = []
92 findCoercions t (n : ns) =
93 let ps = case lookupTy n (tt_ctxt i) of
94 [ty] -> case unApply (getRetTy ty) of
96 if t == t' then [n] else []
98 ps ++ findCoercions t ns
100addToCG :: Name -> CGInfo -> Idris ()
103 putIState $ i { idris_callgraph = addDef n cg (idris_callgraph i) }
105addCoercion :: Name -> Idris ()
106addCoercion n = do i <- getIState
107 putIState $ i { idris_coercions = n : idris_coercions i }
109addDocStr :: Name -> String -> Idris ()
112 putIState $ i { idris_docstrings = addDef n doc (idris_docstrings i) }
114addToCalledG :: Name -> [Name] -> Idris ()
115addToCalledG n ns = return () -- TODO
117-- Add a class instance function. Dodgy hack: Put integer instances first in the
118-- list so they are resolved by default.
119-- Dodgy hack 2: put constraint chasers (@@) last
121addInstance :: Bool -> Name -> Name -> Idris ()
123 = do ist <- getIState
124 case lookupCtxt n (idris_classes ist) of
126 do let cs = addDef n (CI a b c d (addI i ins)) (idris_classes ist)
127 putIState $ ist { idris_classes = cs }
128 _ -> do let cs = addDef n (CI (MN 0 "none") [] [] [] [i]) (idris_classes ist)
129 putIState $ ist { idris_classes = cs }
130 where addI i ins | int = i : ins
131 | chaser n = ins ++ [i]
132 | otherwise = insI i ins
134 insI i (n : ns) | chaser n = i : n : ns
135 | otherwise = n : insI i ns
137 chaser (UN ('@':'@':_)) = True
138 chaser (NS n _) = chaser n
141addClass :: Name -> ClassInfo -> Idris ()
143 = do ist <- getIState
144 let i' = case lookupCtxt n (idris_classes ist) of
145 [c] -> c { class_instances = class_instances i }
147 putIState $ ist { idris_classes = addDef n i' (idris_classes ist) }
149addIBC :: IBCWrite -> Idris ()
152 when (notDef (ibc_write i)) $
153 putIState $ i { ibc_write = ibc : ibc_write i }
154 where notDef [] = True
155 notDef (IBCDef n': is) | n == n' = False
156 notDef (_ : is) = notDef is
157addIBC ibc = do i <- getIState; putIState $ i { ibc_write = ibc : ibc_write i }
160clearIBC = do i <- getIState; putIState $ i { ibc_write = [] }
162getHdrs :: Idris [String]
163getHdrs = do i <- getIState; return (idris_hdrs i)
165setErrLine :: Int -> Idris ()
166setErrLine x = do i <- getIState;
168 Nothing -> putIState $ i { errLine = Just x }
172clearErr = do i <- getIState
173 putIState $ i { errLine = Nothing }
175getSO :: Idris (Maybe String)
176getSO = do i <- getIState
177 return (compiled_so i)
179setSO :: Maybe String -> Idris ()
180setSO s = do i <- getIState
181 putIState $ (i { compiled_so = s })
183getIState :: Idris IState
186putIState :: IState -> Idris ()
190getName = do i <- getIState;
191 let idx = idris_name i;
192 putIState $ (i { idris_name = idx + 1 })
195checkUndefined :: FC -> Name -> Idris ()
199 (_:_) -> fail $ show fc ++ ":" ++
200 show n ++ " already defined"
203isUndefined :: FC -> Name -> Idris Bool
207 (_:_) -> return False
210setContext :: Context -> Idris ()
211setContext ctxt = do i <- getIState; putIState $ (i { tt_ctxt = ctxt } )
213updateContext :: (Context -> Context) -> Idris ()
214updateContext f = do i <- getIState; putIState $ (i { tt_ctxt = f (tt_ctxt i) } )
216addConstraints :: FC -> (Int, [UConstraint]) -> Idris ()
217addConstraints fc (v, cs)
220 let ctxt' = ctxt { uconstraints = cs ++ uconstraints ctxt,
222 let ics = zip cs (repeat fc) ++ idris_constraints i
223 putIState $ i { tt_ctxt = ctxt', idris_constraints = ics }
225addDeferred :: [(Name, Type)] -> Idris ()
226addDeferred ns = do mapM_ (\(n, t) -> updateContext (addTyDecl n (tidyNames [] t))) ns
228 putIState $ i { idris_metavars = map fst ns ++ idris_metavars i }
229 where tidyNames used (Bind (MN i x) b sc)
230 = let n' = uniqueName (UN x) used in
231 Bind n' b $ tidyNames (n':used) sc
232 tidyNames used (Bind n b sc)
233 = let n' = uniqueName n used in
234 Bind n' b $ tidyNames (n':used) sc
237solveDeferred :: Name -> Idris ()
238solveDeferred n = do i <- getIState
239 putIState $ i { idris_metavars = idris_metavars i \\ [n] }
241iputStrLn :: String -> Idris ()
242iputStrLn = liftIO . putStrLn
244iWarn :: FC -> String -> Idris ()
245iWarn fc err = liftIO $ putStrLn (show fc ++ ":" ++ err)
247setLogLevel :: Int -> Idris ()
248setLogLevel l = do i <- getIState
249 let opts = idris_options i
250 let opt' = opts { opt_logLevel = l }
251 putIState $ i { idris_options = opt' }
253setCmdLine :: [Opt] -> Idris ()
254setCmdLine opts = do i <- getIState
255 let iopts = idris_options i
256 putIState $ i { idris_options = iopts { opt_cmdline = opts } }
258getDumpDefun :: Idris (Maybe FilePath)
259getDumpDefun = do i <- getIState
260 return $ findC (opt_cmdline (idris_options i))
261 where findC [] = Nothing
262 findC (DumpDefun x : _) = Just x
263 findC (_ : xs) = findC xs
265getDumpCases :: Idris (Maybe FilePath)
266getDumpCases = do i <- getIState
267 return $ findC (opt_cmdline (idris_options i))
268 where findC [] = Nothing
269 findC (DumpCases x : _) = Just x
270 findC (_ : xs) = findC xs
273logLevel = do i <- getIState
274 return (opt_logLevel (idris_options i))
276setErrContext :: Bool -> Idris ()
277setErrContext t = do i <- getIState
278 let opts = idris_options i
279 let opt' = opts { opt_errContext = t }
280 putIState $ i { idris_options = opt' }
282errContext :: Idris Bool
283errContext = do i <- getIState
284 return (opt_errContext (idris_options i))
287useREPL = do i <- getIState
288 return (opt_repl (idris_options i))
290setREPL :: Bool -> Idris ()
291setREPL t = do i <- getIState
292 let opts = idris_options i
293 let opt' = opts { opt_repl = t }
294 putIState $ i { idris_options = opt' }
296setQuiet :: Bool -> Idris ()
297setQuiet q = do i <- getIState
298 let opts = idris_options i
299 let opt' = opts { opt_quiet = q }
300 putIState $ i { idris_options = opt' }
302setTarget :: Target -> Idris ()
303setTarget t = do i <- getIState
304 let opts = idris_options i
305 let opt' = opts { opt_target = t }
306 putIState $ i { idris_options = opt' }
308target :: Idris Target
309target = do i <- getIState
310 return (opt_target (idris_options i))
312setOutputTy :: OutputType -> Idris ()
313setOutputTy t = do i <- getIState
314 let opts = idris_options i
315 let opt' = opts { opt_outputTy = t }
316 putIState $ i { idris_options = opt' }
318outputTy :: Idris OutputType
319outputTy = do i <- getIState
320 return $ opt_outputTy $ idris_options i
323verbose = do i <- getIState
324 return (opt_verbose (idris_options i))
326setVerbose :: Bool -> Idris ()
327setVerbose t = do i <- getIState
328 let opts = idris_options i
329 let opt' = opts { opt_verbose = t }
330 putIState $ i { idris_options = opt' }
332typeInType :: Idris Bool
333typeInType = do i <- getIState
334 return (opt_typeintype (idris_options i))
336setTypeInType :: Bool -> Idris ()
337setTypeInType t = do i <- getIState
338 let opts = idris_options i
339 let opt' = opts { opt_typeintype = t }
340 putIState $ i { idris_options = opt' }
342coverage :: Idris Bool
343coverage = do i <- getIState
344 return (opt_coverage (idris_options i))
346setCoverage :: Bool -> Idris ()
347setCoverage t = do i <- getIState
348 let opts = idris_options i
349 let opt' = opts { opt_coverage = t }
350 putIState $ i { idris_options = opt' }
352setIBCSubDir :: FilePath -> Idris ()
353setIBCSubDir fp = do i <- getIState
354 let opts = idris_options i
355 let opt' = opts { opt_ibcsubdir = fp }
356 putIState $ i { idris_options = opt' }
358valIBCSubDir :: IState -> Idris FilePath
359valIBCSubDir i = return (opt_ibcsubdir (idris_options i))
361addImportDir :: FilePath -> Idris ()
362addImportDir fp = do i <- getIState
363 let opts = idris_options i
364 let opt' = opts { opt_importdirs = fp : opt_importdirs opts }
365 putIState $ i { idris_options = opt' }
367setImportDirs :: [FilePath] -> Idris ()
368setImportDirs fps = do i <- getIState
369 let opts = idris_options i
370 let opt' = opts { opt_importdirs = fps }
371 putIState $ i { idris_options = opt' }
373allImportDirs :: IState -> Idris [FilePath]
374allImportDirs i = do let optdirs = opt_importdirs (idris_options i)
375 return ("." : reverse optdirs)
378impShow = do i <- getIState
379 return (opt_showimp (idris_options i))
381setImpShow :: Bool -> Idris ()
382setImpShow t = do i <- getIState
383 let opts = idris_options i
384 let opt' = opts { opt_showimp = t }
385 putIState $ i { idris_options = opt' }
387logLvl :: Int -> String -> Idris ()
388logLvl l str = do i <- getIState
389 let lvl = opt_logLevel (idris_options i)
391 $ do liftIO (putStrLn str)
392 putIState $ i { idris_log = idris_log i ++ str ++ "\n" }
394cmdOptType :: Opt -> Idris Bool
395cmdOptType x = do i <- getIState
396 return $ x `elem` opt_cmdline (idris_options i)
398iLOG :: String -> Idris ()
401noErrors :: Idris Bool
402noErrors = do i <- getIState
404 Nothing -> return True
407setTypeCase :: Bool -> Idris ()
408setTypeCase t = do i <- getIState
409 let opts = idris_options i
410 let opt' = opts { opt_typecase = t }
411 putIState $ i { idris_options = opt' }
414-- For inferring types of things
418inferTy = MN 0 "__Infer"
419inferCon = MN 0 "__infer"
420inferDecl = PDatadecl inferTy
422 [("", inferCon, PPi impl (MN 0 "A") PType (
423 PPi expl (MN 0 "a") (PRef bi (MN 0 "A"))
424 (PRef bi inferTy)), bi)]
426infTerm t = PApp bi (PRef bi inferCon) [pimp (MN 0 "A") Placeholder, pexp t]
427infP = P (TCon 6 0) inferTy (TType (UVal 0))
429getInferTerm, getInferType :: Term -> Term
430getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
431getInferTerm (App (App _ _) tm) = tm
432getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
434getInferType (Bind n b sc) = Bind n b $ getInferType sc
435getInferType (App (App _ ty) _) = ty
437-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign
439primNames = [unitTy, unitCon,
440 falseTy, pairTy, pairCon,
441 eqTy, eqCon, inferTy, inferCon]
443unitTy = MN 0 "__Unit"
445unitDecl = PDatadecl unitTy PType
446 [("", unitCon, PRef bi unitTy, bi)]
448falseTy = MN 0 "__False"
449falseDecl = PDatadecl falseTy PType []
451pairTy = MN 0 "__Pair"
452pairCon = MN 0 "__MkPair"
453pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
454 [("", pairCon, PPi impl (n "A") PType (
455 PPi impl (n "B") PType (
456 PPi expl (n "a") (PRef bi (n "A")) (
457 PPi expl (n "b") (PRef bi (n "B"))
458 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
459 pexp (PRef bi (n "B"))])))), bi)]
464eqDecl = PDatadecl eqTy (piBind [(n "a", PType), (n "b", PType),
465 (n "x", PRef bi (n "a")), (n "y", PRef bi (n "b"))]
467 [("", eqCon, PPi impl (n "a") PType (
468 PPi impl (n "x") (PRef bi (n "a"))
469 (PApp bi (PRef bi eqTy) [pimp (n "a") Placeholder,
470 pimp (n "b") Placeholder,
471 pexp (PRef bi (n "x")),
472 pexp (PRef bi (n "x"))])), bi)]
475-- Defined in builtins.idr
477existsCon = UN "Ex_intro"
479piBind :: [(Name, PTerm)] -> PTerm -> PTerm
482piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
484piBindp p ((n, ty):ns) t = PPi p n ty (piBind ns t)
486-- Dealing with parameters
488expandParams :: (Name -> Name) -> [(Name, PTerm)] ->
489 [Name] -> -- all names
490 [Name] -> -- names with no declaration
492expandParams dec ps ns infs tm = en tm
494 -- if we shadow a name (say in a lambda binding) that is used in a call to
495 -- a lifted function, we need access to both names - once in the scope of the
496 -- binding and once to call the lifted functions. So we'll explicitly shadow
497 -- it. (Yes, it's a hack. The alternative would be to resolve names earlier
500 mkShadow (UN n) = MN 0 n
501 mkShadow (MN i n) = MN (i+1) n
502 mkShadow (NS x s) = NS (mkShadow x) s
505 | n `elem` map fst ps
506 = let n' = mkShadow n in
507 PLam n' (en t) (en (shadow n n' s))
508 | otherwise = PLam n (en t) (en s)
510 | n `elem` map fst ps
511 = let n' = mkShadow n in
512 PPi p n' (en t) (en (shadow n n' s))
513 | otherwise = PPi p n (en t) (en s)
515 | n `elem` map fst ps
516 = let n' = mkShadow n in
517 PLet n' (en ty) (en v) (en (shadow n n' s))
518 | otherwise = PLet n (en ty) (en v) (en s)
519 en (PEq f l r) = PEq f (en l) (en r)
520 en (PRewrite f l r) = PRewrite f (en l) (en r)
521 en (PTyped l r) = PTyped (en l) (en r)
522 en (PPair f l r) = PPair f (en l) (en r)
523 en (PDPair f l t r) = PDPair f (en l) (en t) (en r)
524 en (PAlternative a as) = PAlternative a (map en as)
525 en (PHidden t) = PHidden (en t)
526 en (PDoBlock ds) = PDoBlock (map (fmap en) ds)
527 en (PProof ts) = PProof (map (fmap en) ts)
528 en (PTactics ts) = PTactics (map (fmap en) ts)
531 | n `nselem` ns = PQuote (Var (dec n))
532 en (PApp fc (PInferRef fc' n) as)
533 | n `nselem` ns = PApp fc (PInferRef fc' (dec n))
534 (map (pexp . (PRef fc)) (map fst ps) ++ (map (fmap en) as))
535 en (PApp fc (PRef fc' n) as)
536 | n `elem` infs = PApp fc (PInferRef fc' (dec n))
537 (map (pexp . (PRef fc)) (map fst ps) ++ (map (fmap en) as))
538 | n `nselem` ns = PApp fc (PRef fc' (dec n))
539 (map (pexp . (PRef fc)) (map fst ps) ++ (map (fmap en) as))
541 | n `elem` infs = PApp fc (PInferRef fc (dec n))
542 (map (pexp . (PRef fc)) (map fst ps))
543 | n `nselem` ns = PApp fc (PRef fc (dec n))
544 (map (pexp . (PRef fc)) (map fst ps))
546 | n `nselem` ns = PApp fc (PInferRef fc (dec n))
547 (map (pexp . (PRef fc)) (map fst ps))
548 en (PApp fc f as) = PApp fc (en f) (map (fmap en) as)
549 en (PCase fc c os) = PCase fc (en c) (map (pmap en) os)
553 nselem x (y : xs) | nseq x y = True
554 | otherwise = nselem x xs
556 nseq x y = nsroot x == nsroot y
558expandParamsD :: Bool -> -- True = RHS only
560 (Name -> Name) -> [(Name, PTerm)] -> [Name] -> PDecl -> PDecl
561expandParamsD rhsonly ist dec ps ns (PTy doc syn fc o n ty)
562 = if n `elem` ns && (not rhsonly)
563 then -- trace (show (n, expandParams dec ps ns ty)) $
564 PTy doc syn fc o (dec n) (piBind ps (expandParams dec ps ns [] ty))
565 else --trace (show (n, expandParams dec ps ns ty)) $
566 PTy doc syn fc o n (expandParams dec ps ns [] ty)
567expandParamsD rhsonly ist dec ps ns (PPostulate doc syn fc o n ty)
568 = if n `elem` ns && (not rhsonly)
569 then -- trace (show (n, expandParams dec ps ns ty)) $
570 PPostulate doc syn fc o (dec n) (piBind ps
571 (expandParams dec ps ns [] ty))
572 else --trace (show (n, expandParams dec ps ns ty)) $
573 PPostulate doc syn fc o n (expandParams dec ps ns [] ty)
574expandParamsD rhsonly ist dec ps ns (PClauses fc opts n cs)
575 = let n' = if n `elem` ns then dec n else n in
576 PClauses fc opts n' (map expandParamsC cs)
578 expandParamsC (PClause fc n lhs ws rhs ds)
579 = let -- ps' = updateps True (namesIn ist rhs) (zip ps [0..])
580 ps'' = updateps False (namesIn [] ist lhs) (zip ps [0..])
581 lhs' = if rhsonly then lhs else (expandParams dec ps'' ns [] lhs)
582 n' = if n `elem` ns then dec n else n in
584 (map (expandParams dec ps'' ns []) ws)
585 (expandParams dec ps'' ns [] rhs)
586 (map (expandParamsD True ist dec ps'' ns) ds)
587 expandParamsC (PWith fc n lhs ws wval ds)
588 = let -- ps' = updateps True (namesIn ist wval) (zip ps [0..])
589 ps'' = updateps False (namesIn [] ist lhs) (zip ps [0..])
590 lhs' = if rhsonly then lhs else (expandParams dec ps'' ns [] lhs)
591 n' = if n `elem` ns then dec n else n in
593 (map (expandParams dec ps'' ns []) ws)
594 (expandParams dec ps'' ns [] wval)
595 (map (expandParamsD rhsonly ist dec ps'' ns) ds)
596 updateps yn nm [] = []
597 updateps yn nm (((a, t), i):as)
598 | (a `elem` nm) == yn = (a, t) : updateps yn nm as
599 | otherwise = (MN i (show n ++ "_u"), t) : updateps yn nm as
600expandParamsD rhs ist dec ps ns (PData doc syn fc co pd)
601 = PData doc syn fc co (expandPData pd)
603 -- just do the type decl, leave constructors alone (parameters will be
605 expandPData (PDatadecl n ty cons)
607 then PDatadecl (dec n) (piBind ps (expandParams dec ps ns [] ty))
609 else PDatadecl n (expandParams dec ps ns [] ty) (map econ cons)
611 = (doc, dec n, piBindp expl ps (expandParams dec ps ns [] t), fc)
612expandParamsD rhs ist dec ps ns (PParams f params pds)
613 = PParams f (ps ++ map (mapsnd (expandParams dec ps ns [])) params)
614 (map (expandParamsD True ist dec ps ns) pds)
615-- (map (expandParamsD ist dec ps ns) pds)
616expandParamsD rhs ist dec ps ns (PMutual f pds)
617 = PMutual f (map (expandParamsD rhs ist dec ps ns) pds)
618expandParamsD rhs ist dec ps ns (PClass doc info f cs n params decls)
620 (map (expandParams dec ps ns []) cs)
622 (map (mapsnd (expandParams dec ps ns [])) params)
623 (map (expandParamsD rhs ist dec ps ns) decls)
624expandParamsD rhs ist dec ps ns (PInstance info f cs n params ty cn decls)
626 (map (expandParams dec ps ns []) cs)
628 (map (expandParams dec ps ns []) params)
629 (expandParams dec ps ns [] ty)
631 (map (expandParamsD rhs ist dec ps ns) decls)
632expandParamsD rhs ist dec ps ns d = d
634mapsnd f (x, t) = (x, f t)
636-- Calculate a priority for a type, for deciding elaboration order
637-- * if it's just a type variable or concrete type, do it early (0)
638-- * if there's only type variables and injective constructors, do it next (1)
639-- * if there's a function type, next (2)
640-- * finally, everything else (3)
642getPriority :: IState -> PTerm -> Int
643getPriority i tm = 1 -- pri tm
646 case lookupP n (tt_ctxt i) of
647 ((P (DCon _ _) _ _):_) -> 1
648 ((P (TCon _ _) _ _):_) -> 1
650 [] -> 0 -- must be locally bound, if it's not an error...
651 pri (PPi _ _ x y) = max 5 (max (pri x) (pri y))
655 pri (PEq _ l r) = max 1 (max (pri l) (pri r))
656 pri (PRewrite _ l r) = max 1 (max (pri l) (pri r))
657 pri (PApp _ f as) = max 1 (max (pri f) (foldr max 0 (map (pri.getTm) as)))
658 pri (PCase _ f as) = max 1 (max (pri f) (foldr max 0 (map (pri.snd) as)))
659 pri (PTyped l r) = pri l
660 pri (PPair _ l r) = max 1 (max (pri l) (pri r))
661 pri (PDPair _ l t r) = max 1 (max (pri l) (max (pri t) (pri r)))
662 pri (PAlternative a as) = maximum (map pri as)
663 pri (PConstant _) = 0
667addStatics :: Name -> Term -> PTerm -> Idris ()
669 do let (statics, dynamics) = initStatics tm ptm
670 let stnames = nub $ concatMap freeNames (map snd statics)
671 let dnames = nub $ concatMap freeNames (map snd dynamics)
672 when (not (null statics)) $
673 logLvl 7 $ show n ++ " " ++ show statics ++ "\n" ++ show dynamics
674 ++ "\n" ++ show stnames ++ "\n" ++ show dnames
675 let statics' = nub $ map fst statics ++
676 filter (\x -> not (elem x dnames)) stnames
677 let stpos = staticList statics' tm
679 putIState $ i { idris_statics = addDef n stpos (idris_statics i) }
682 initStatics (Bind n (Pi ty) sc) (PPi p _ _ s)
683 = let (static, dynamic) = initStatics (instantiate (P Bound n ty) sc) s in
684 if pstatic p == Static then ((n, ty) : static, dynamic)
685 else (static, (n, ty) : dynamic)
686 initStatics t pt = ([], [])
688 staticList sts (Bind n (Pi _) sc) = (n `elem` sts) : staticList sts sc
691-- Dealing with implicit arguments
693-- Add implicit Pi bindings for any names in the term which appear in an
696-- This has become a right mess already. Better redo it some time...
698implicit :: SyntaxInfo -> Name -> PTerm -> Idris PTerm
699implicit syn n ptm = implicit' syn [] n ptm
701implicit' :: SyntaxInfo -> [Name] -> Name -> PTerm -> Idris PTerm
702implicit' syn ignore n ptm
704 let (tm', impdata) = implicitise syn ignore i ptm
705-- let (tm'', spos) = findStatics i tm'
706 putIState $ i { idris_implicits = addDef n impdata (idris_implicits i) }
708 logLvl 5 ("Implicit " ++ show n ++ " " ++ show impdata)
710-- putIState $ i { idris_statics = addDef n spos (idris_statics i) }
713implicitise :: SyntaxInfo -> [Name] -> IState -> PTerm -> (PTerm, [PArg])
714implicitise syn ignore ist tm
715 = let (declimps, ns') = execState (imps True [] tm) ([], [])
716 ns = filter (\n -> implicitable n || elem n (map fst uvars)) $
717 ns' \\ (map fst pvars ++ no_imp syn ++ ignore) in
719 then (tm, reverse declimps)
720 else implicitise syn ignore ist (pibind uvars ns tm)
723 pvars = syn_params syn
725 dropAll (x:xs) ys | x `elem` ys = dropAll xs ys
726 | otherwise = x : dropAll xs ys
729 imps top env (PApp _ f as)
730 = do (decls, ns) <- get
731 let isn = concatMap (namesIn uvars ist) (map getTm as)
732 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
733 imps top env (PPi (Imp l _ doc) n ty sc)
734 = do let isn = nub (namesIn uvars ist ty) `dropAll` [n]
736 put (PImp (getPriority ist ty) l n ty doc : decls,
737 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
739 imps top env (PPi (Exp l _ doc) n ty sc)
740 = do let isn = nub (namesIn uvars ist ty ++ case sc of
741 (PRef _ x) -> namesIn uvars ist sc `dropAll` [n]
743 (decls, ns) <- get -- ignore decls in HO types
744 put (PExp (getPriority ist ty) l ty doc : decls,
745 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
747 imps top env (PPi (Constraint l _ doc) n ty sc)
748 = do let isn = nub (namesIn uvars ist ty ++ case sc of
749 (PRef _ x) -> namesIn uvars ist sc `dropAll` [n]
751 (decls, ns) <- get -- ignore decls in HO types
752 put (PConstraint 10 l ty doc : decls,
753 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
755 imps top env (PPi (TacImp l _ scr doc) n ty sc)
756 = do let isn = nub (namesIn uvars ist ty ++ case sc of
757 (PRef _ x) -> namesIn uvars ist sc `dropAll` [n]
759 (decls, ns) <- get -- ignore decls in HO types
760 put (PTacImplicit 10 l n scr ty doc : decls,
761 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
763 imps top env (PEq _ l r)
764 = do (decls, ns) <- get
765 let isn = namesIn uvars ist l ++ namesIn uvars ist r
766 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
767 imps top env (PRewrite _ l r)
768 = do (decls, ns) <- get
769 let isn = namesIn uvars ist l ++ namesIn uvars ist r
770 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
771 imps top env (PTyped l r)
773 imps top env (PPair _ l r)
774 = do (decls, ns) <- get
775 let isn = namesIn uvars ist l ++ namesIn uvars ist r
776 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
777 imps top env (PDPair _ (PRef _ n) t r)
778 = do (decls, ns) <- get
779 let isn = nub (namesIn uvars ist t ++ namesIn uvars ist r) \\ [n]
780 put (decls, nub (ns ++ (isn \\ (env ++ map fst (getImps decls)))))
781 imps top env (PDPair _ l t r)
782 = do (decls, ns) <- get
783 let isn = namesIn uvars ist l ++ namesIn uvars ist t ++
785 put (decls, nub (ns ++ (isn \\ (env ++ map fst (getImps decls)))))
786 imps top env (PAlternative a as)
787 = do (decls, ns) <- get
788 let isn = concatMap (namesIn uvars ist) as
789 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
790 imps top env (PLam n ty sc)
791 = do imps False env ty
792 imps False (n:env) sc
793 imps top env (PHidden tm) = imps False env tm
794 imps top env _ = return ()
796 pibind using [] sc = sc
797 pibind using (n:ns) sc
798 = case lookup n using of
799 Just ty -> PPi (Imp False Dynamic "") n ty (pibind using ns sc)
800 Nothing -> PPi (Imp False Dynamic "") n Placeholder
803-- Add implicit arguments in function calls
804addImplPat :: IState -> PTerm -> PTerm
805addImplPat = addImpl' True [] []
807addImplBound :: IState -> [Name] -> PTerm -> PTerm
808addImplBound ist ns = addImpl' False ns [] ist
810addImplBoundInf :: IState -> [Name] -> [Name] -> PTerm -> PTerm
811addImplBoundInf ist ns inf = addImpl' False ns inf ist
813addImpl :: IState -> PTerm -> PTerm
814addImpl = addImpl' False [] []
816-- TODO: in patterns, don't add implicits to function names guarded by constructors
817-- and *not* inside a PHidden
819addImpl' :: Bool -> [Name] -> [Name] -> IState -> PTerm -> PTerm
820addImpl' inpat env infns ist ptm = ai (zip env (repeat Nothing)) ptm
823 | f `elem` infns = PInferRef fc f
824 | not (f `elem` map fst env) = handleErr $ aiFn inpat inpat ist fc f []
825 ai env (PHidden (PRef fc f))
826 | not (f `elem` map fst env) = handleErr $ aiFn inpat False ist fc f []
827 ai env (PEq fc l r) = let l' = ai env l
830 ai env (PRewrite fc l r) = let l' = ai env l
833 ai env (PTyped l r) = let l' = ai env l
836 ai env (PPair fc l r) = let l' = ai env l
839 ai env (PDPair fc l t r) = let l' = ai env l
843 ai env (PAlternative a as) = let as' = map (ai env) as in
845 ai env (PApp fc (PInferRef _ f) as)
846 = let as' = map (fmap (ai env)) as in
847 PApp fc (PInferRef fc f) as'
848 ai env (PApp fc ftm@(PRef _ f) as)
849 | f `elem` infns = ai env (PApp fc (PInferRef fc f) as)
850 | not (f `elem` map fst env)
851 = let as' = map (fmap (ai env)) as in
852 handleErr $ aiFn inpat False ist fc f as'
853 | Just (Just ty) <- lookup f env
854 = let as' = map (fmap (ai env)) as
855 arity = getPArity ty in
856 mkPApp fc arity ftm as'
857 ai env (PApp fc f as) = let f' = ai env f
858 as' = map (fmap (ai env)) as in
860 ai env (PCase fc c os) = let c' = ai env c
861 os' = map (pmap (ai env)) os in
863 ai env (PLam n ty sc) = let ty' = ai env ty
864 sc' = ai ((n, Just ty):env) sc in
866 ai env (PLet n ty val sc)
867 = let ty' = ai env ty
869 sc' = ai ((n, Just ty):env) sc in
871 ai env (PPi p n ty sc) = let ty' = ai env ty
872 sc' = ai ((n, Just ty):env) sc in
874 ai env (PHidden tm) = PHidden (ai env tm)
875 ai env (PProof ts) = PProof (map (fmap (ai env)) ts)
876 ai env (PTactics ts) = PTactics (map (fmap (ai env)) ts)
879 handleErr (Left err) = PElabError err
880 handleErr (Right x) = x
882-- if in a pattern, and there are no arguments, and there's no possible
883-- names with zero explicit arguments, don't add implicits.
885aiFn :: Bool -> Bool -> IState -> FC -> Name -> [PArg] -> Either Err PTerm
886aiFn inpat True ist fc f []
887 = case lookupDef f (tt_ctxt ist) of
888 [] -> Right $ PPatvar fc f
889 alts -> let ialts = lookupCtxt f (idris_implicits ist) in
890 -- trace (show f ++ " " ++ show (fc, any (all imp) ialts, ialts, any constructor alts)) $
891 if (not (vname f) || tcname f
892 || any constructor alts || any allImp ialts)
893 then aiFn inpat False ist fc f [] -- use it as a constructor
894 else Right $ PPatvar fc f
895 where imp (PExp _ _ _ _) = False
898 allImp xs = all imp xs
899 constructor (TyDecl (DCon _ _) _) = True
900 constructor _ = False
902 vname (UN n) = True -- non qualified
905aiFn inpat expat ist fc f as
906 | f `elem` primNames = Right $ PApp fc (PRef fc f) as
907aiFn inpat expat ist fc f as
908 -- This is where namespaces get resolved by adding PAlternative
909 = case lookupCtxtName f (idris_implicits ist) of
910 [(f',ns)] -> Right $ mkPApp fc (length ns) (PRef fc f') (insertImpl ns as)
911 [] -> if f `elem` idris_metavars ist
912 then Right $ PApp fc (PRef fc f) as
913 else Right $ mkPApp fc (length as) (PRef fc f) as
916 map (\(f', ns) -> mkPApp fc (length ns) (PRef fc f')
917 (insertImpl ns as)) alts
919 insertImpl :: [PArg] -> [PArg] -> [PArg]
920 insertImpl (PExp p l ty _ : ps) (PExp _ _ tm d : given) =
921 PExp p l tm d : insertImpl ps given
922 insertImpl (PConstraint p l ty _ : ps) (PConstraint _ _ tm d : given) =
923 PConstraint p l tm d : insertImpl ps given
924 insertImpl (PConstraint p l ty d : ps) given =
925 PConstraint p l (PResolveTC fc) d : insertImpl ps given
926 insertImpl (PImp p l n ty d : ps) given =
927 case find n given [] of
928 Just (tm, given') -> PImp p l n tm "" : insertImpl ps given'
929 Nothing -> PImp p l n Placeholder "" : insertImpl ps given
930 insertImpl (PTacImplicit p l n sc ty d : ps) given =
931 case find n given [] of
932 Just (tm, given') -> PTacImplicit p l n sc tm "" : insertImpl ps given'
934 then PTacImplicit p l n sc Placeholder "" : insertImpl ps given
935 else PTacImplicit p l n sc sc "" : insertImpl ps given
936 insertImpl expected [] = []
937 insertImpl _ given = given
939 find n [] acc = Nothing
940 find n (PImp _ _ n' t _ : gs) acc
941 | n == n' = Just (t, reverse acc ++ gs)
942 find n (PTacImplicit _ _ n' _ t _ : gs) acc
943 | n == n' = Just (t, reverse acc ++ gs)
944 find n (g : gs) acc = find n gs (g : acc)
946-- replace non-linear occurrences with _
947-- ASSUMPTION: This is called before adding 'alternatives' because otherwise
948-- it is hard to get right!
950stripLinear :: IState -> PTerm -> PTerm
951stripLinear i tm = evalState (sl tm) [] where
952 sl :: PTerm -> State [Name] PTerm
954 | (_:_) <- lookupTy f (tt_ctxt i)
956 | otherwise = do ns <- get
958 then return Placeholder
964 then return Placeholder
966 return (PPatvar fc f)
967 sl (PApp fc fn args) = do fn' <- sl fn
968 args' <- mapM slA args
969 return $ PApp fc fn' args'
970 where slA (PImp p l n t d) = do t' <- sl t
971 return $ PImp p l n t' d
972 slA (PExp p l t d) = do t' <- sl t
973 return $ PExp p l t' d
974 slA (PConstraint p l t d)
976 return $ PConstraint p l t' d
977 slA (PTacImplicit p l n sc t d)
979 return $ PTacImplicit p l n sc t' d
983mkPApp fc a f as = let rest = drop a as in
984 appRest fc (PApp fc f (take a as)) rest
987 appRest fc f (a : as) = appRest fc (PApp fc f [a]) as
989-- Find 'static' argument positions
990-- (the declared ones, plus any names in argument position in the declared
992-- FIXME: It's possible that this really has to happen after elaboration
994findStatics :: IState -> PTerm -> (PTerm, [Bool])
995findStatics ist tm = trace (showImp True tm) $
996 let (ns, ss) = fs tm in
997 runState (pos ns ss tm) []
998 where fs (PPi p n t sc)
999 | Static <- pstatic p
1000 = let (ns, ss) = fs sc in
1001 (namesIn [] ist t : ns, n : ss)
1002 | otherwise = let (ns, ss) = fs sc in
1006 inOne n ns = length (filter id (map (elem n) ns)) == 1
1008 pos ns ss (PPi p n t sc)
1009 | elem n ss = do sc' <- pos ns ss sc
1012 return (PPi (p { pstatic = Static }) n t sc')
1013 | otherwise = do sc' <- pos ns ss sc
1016 return (PPi p n t sc')
1017 pos ns ss t = return t
1019-- Debugging/logging stuff
1021dumpDecls :: [PDecl] -> String
1023dumpDecls (d:ds) = dumpDecl d ++ "\n" ++ dumpDecls ds
1025dumpDecl (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
1026dumpDecl (PTy _ _ _ _ n t) = "tydecl " ++ show n ++ " : " ++ showImp True t
1027dumpDecl (PClauses _ _ n cs) = "pat " ++ show n ++ "\t" ++ showSep "\n\t" (map (showCImp True) cs)
1028dumpDecl (PData _ _ _ _ d) = showDImp True d
1029dumpDecl (PParams _ ns ps) = "params {" ++ show ns ++ "\n" ++ dumpDecls ps ++ "}\n"
1030dumpDecl (PNamespace n ps) = "namespace {" ++ n ++ "\n" ++ dumpDecls ps ++ "}\n"
1031dumpDecl (PSyntax _ syn) = "syntax " ++ show syn
1032dumpDecl (PClass _ _ _ cs n ps ds)
1033 = "class " ++ show cs ++ " " ++ show n ++ " " ++ show ps ++ "\n" ++ dumpDecls ds
1034dumpDecl (PInstance _ _ cs n _ t _ ds)
1035 = "instance " ++ show cs ++ " " ++ show n ++ " " ++ show t ++ "\n" ++ dumpDecls ds
1037-- dumpDecl (PImport i) = "import " ++ i
1039-- for 6.12/7 compatibility
1040data EitherErr a b = LeftErr a | RightOK b
1042instance Monad (EitherErr a) where
1045 (LeftErr e) >>= k = LeftErr e
1046 RightOK v >>= k = k v
1048toEither (LeftErr e) = Left e
1049toEither (RightOK ho) = Right ho
1051-- syntactic match of a against b, returning pair of variables in a
1052-- and what they match. Returns the pair that failed if not a match.
1054matchClause :: IState -> PTerm -> PTerm -> Either (PTerm, PTerm) [(Name, PTerm)]
1055matchClause = matchClause' False
1057matchClause' :: Bool -> IState -> PTerm -> PTerm -> Either (PTerm, PTerm) [(Name, PTerm)]
1058matchClause' names i x y = checkRpts $ match (fullApp x) (fullApp y) where
1059 matchArg x y = match (fullApp (getTm x)) (fullApp (getTm y))
1061 fullApp (PApp _ (PApp fc f args) xs) = fullApp (PApp fc f (args ++ xs))
1064 match' x y = match (fullApp x) (fullApp y)
1065 match (PApp _ (PRef _ (NS (UN "fromInteger") ["builtins"])) [_,_,x]) x'
1066 | PConstant (I _) <- getTm x = match (getTm x) x'
1067 match x' (PApp _ (PRef _ (NS (UN "fromInteger") ["builtins"])) [_,_,x])
1068 | PConstant (I _) <- getTm x = match (getTm x) x'
1069 match (PApp _ (PRef _ (UN "lazy")) [_,x]) x' = match (getTm x) x'
1070 match x (PApp _ (PRef _ (UN "lazy")) [_,x']) = match x (getTm x')
1071 match (PApp _ f args) (PApp _ f' args')
1072 | length args == length args'
1073 = do mf <- match' f f'
1074 ms <- zipWithM matchArg args args'
1075 return (mf ++ concat ms)
1076-- match (PRef _ n) (PRef _ n') | n == n' = return []
1077-- | otherwise = Nothing
1078 match (PRef f n) (PApp _ x []) = match (PRef f n) x
1079 match (PPatvar f n) xr = match (PRef f n) xr
1080 match xr (PPatvar f n) = match xr (PRef f n)
1081 match (PApp _ x []) (PRef f n) = match x (PRef f n)
1082 match (PRef _ n) tm@(PRef _ n')
1083 | n == n' && not names &&
1084 (not (isConName n (tt_ctxt i)) || tm == Placeholder)
1086 | n == n' = return []
1088 | not names && (not (isConName n (tt_ctxt i)) || tm == Placeholder)
1090 match (PEq _ l r) (PEq _ l' r') = do ml <- match' l l'
1093 match (PRewrite _ l r) (PRewrite _ l' r')
1094 = do ml <- match' l l'
1097 match (PTyped l r) (PTyped l' r') = do ml <- match l l'
1100 match (PTyped l r) x = match l x
1101 match x (PTyped l r) = match x l
1102 match (PPair _ l r) (PPair _ l' r') = do ml <- match' l l'
1105 match (PDPair _ l t r) (PDPair _ l' t' r') = do ml <- match' l l'
1108 return (ml ++ mt ++ mr)
1109 match (PAlternative a as) (PAlternative a' as')
1110 = do ms <- zipWithM match' as as'
1112 match a@(PAlternative _ as) b
1113 = do let ms = zipWith match' as (repeat b)
1114 case (rights (map toEither ms)) of
1117 match (PCase _ _ _) _ = return [] -- lifted out
1118 match (PMetavar _) _ = return [] -- modified
1119 match (PInferRef _ _) _ = return [] -- modified
1120 match (PQuote _) _ = return []
1121 match (PProof _) _ = return []
1122 match (PTactics _) _ = return []
1123 match (PRefl _ _) (PRefl _ _) = return []
1124 match (PResolveTC _) (PResolveTC _) = return []
1125 match (PTrue _) (PTrue _) = return []
1126 match (PFalse _) (PFalse _) = return []
1127 match (PReturn _) (PReturn _) = return []
1128 match (PPi _ _ t s) (PPi _ _ t' s') = do mt <- match' t t'
1131 match (PLam _ t s) (PLam _ t' s') = do mt <- match' t t'
1134 match (PLet _ t ty s) (PLet _ t' ty' s') = do mt <- match' t t'
1135 mty <- match' ty ty'
1137 return (mt ++ mty ++ ms)
1138 match (PHidden x) (PHidden y) = match' x y
1139 match Placeholder _ = return []
1140 match _ Placeholder = return []
1141 match (PResolveTC _) _ = return []
1142 match a b | a == b = return []
1143 | otherwise = LeftErr (a, b)
1145 checkRpts (RightOK ms) = check ms where
1147 | Just t' <- lookup n xs = if t/=t' && t/=Placeholder && t'/=Placeholder
1150 check (_:xs) = check xs
1152 checkRpts (LeftErr x) = Left x
1154substMatches :: [(Name, PTerm)] -> PTerm -> PTerm
1155substMatches ms = substMatchesShadow ms []
1157substMatchesShadow :: [(Name, PTerm)] -> [Name] -> PTerm -> PTerm
1158substMatchesShadow [] shs t = t
1159substMatchesShadow ((n,tm):ns) shs t
1160 = substMatchShadow n shs tm (substMatchesShadow ns shs t)
1162substMatch :: Name -> PTerm -> PTerm -> PTerm
1163substMatch n = substMatchShadow n []
1165substMatchShadow :: Name -> [Name] -> PTerm -> PTerm -> PTerm
1166substMatchShadow n shs tm t = sm shs t where
1167 sm xs (PRef _ n') | n == n' = tm
1168 sm xs (PLam x t sc) = PLam x (sm xs t) (sm xs sc)
1169 sm xs (PPi p x t sc)
1171 = let x' = nextName x in
1172 PPi p x' (sm (x':xs) (substMatch x (PRef (FC "" 0) x') t))
1173 (sm (x':xs) (substMatch x (PRef (FC "" 0) x') sc))
1174 | otherwise = PPi p x (sm xs t) (sm (x : xs) sc)
1175 sm xs (PApp f x as) = PApp f (sm xs x) (map (fmap (sm xs)) as)
1176 sm xs (PCase f x as) = PCase f (sm xs x) (map (pmap (sm xs)) as)
1177 sm xs (PEq f x y) = PEq f (sm xs x) (sm xs y)
1178 sm xs (PRewrite f x y) = PRewrite f (sm xs x) (sm xs y)
1179 sm xs (PTyped x y) = PTyped (sm xs x) (sm xs y)
1180 sm xs (PPair f x y) = PPair f (sm xs x) (sm xs y)
1181 sm xs (PDPair f x t y) = PDPair f (sm xs x) (sm xs t) (sm xs y)
1182 sm xs (PAlternative a as) = PAlternative a (map (sm xs) as)
1183 sm xs (PHidden x) = PHidden (sm xs x)
1186shadow :: Name -> Name -> PTerm -> PTerm
1187shadow n n' t = sm t where
1188 sm (PRef fc x) | n == x = PRef fc n'
1189 sm (PLam x t sc) = PLam x (sm t) (sm sc)
1190 sm (PPi p x t sc) = PPi p x (sm t) (sm sc)
1191 sm (PApp f x as) = PApp f (sm x) (map (fmap sm) as)
1192 sm (PCase f x as) = PCase f (sm x) (map (pmap sm) as)
1193 sm (PEq f x y) = PEq f (sm x) (sm y)
1194 sm (PRewrite f x y) = PRewrite f (sm x) (sm y)
1195 sm (PTyped x y) = PTyped (sm x) (sm y)
1196 sm (PPair f x y) = PPair f (sm x) (sm y)
1197 sm (PDPair f x t y) = PDPair f (sm x) (sm t) (sm y)
1198 sm (PAlternative a as) = PAlternative a (map sm as)
1199 sm (PHidden x) = PHidden (sm x)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list