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
12import IRTS.CodegenCommon
13import Util.DynamicLinker
17import System.Console.Haskeline
19import Control.Monad.State
30getContext :: Idris Context
31getContext = do i <- getIState; return (tt_ctxt i)
33forCodegen :: Codegen -> [(Codegen, a)] -> [a]
34forCodegen tgt xs = [x | (tgt', x) <- xs, tgt == tgt']
36getObjectFiles :: Codegen -> Idris [FilePath]
37getObjectFiles tgt = do i <- getIState; return (forCodegen tgt $ idris_objs i)
39addObjectFile :: Codegen -> FilePath -> Idris ()
40addObjectFile tgt f = do i <- getIState; putIState $ i { idris_objs = (tgt, f) : idris_objs i }
42getLibs :: Codegen -> Idris [String]
43getLibs tgt = do i <- getIState; return (forCodegen tgt $ idris_libs i)
45addLib :: Codegen -> String -> Idris ()
46addLib tgt f = do i <- getIState; putIState $ i { idris_libs = (tgt, f) : idris_libs i }
48addDyLib :: [String] -> Idris (Either DynamicLib String)
49addDyLib libs = do i <- getIState
50 handle <- lift $ mapM (\l -> catchIO (tryLoadLib l) (\_ -> return Nothing)) libs
52 Nothing -> return (Right $ "Could not load dynamic alternatives \"" ++
53 concat (intersperse "," libs) ++ "\"")
54 Just x -> do let ls = idris_dynamic_libs i
55 putIState $ i { idris_dynamic_libs = x:ls }
58addHdr :: Codegen -> String -> Idris ()
59addHdr tgt f = do i <- getIState; putIState $ i { idris_hdrs = (tgt, f) : idris_hdrs i }
61addLangExt :: LanguageExt -> Idris ()
62addLangExt TypeProviders = do i <- getIState ; putIState $ i { idris_language_extensions = [TypeProviders] }
64totcheck :: (FC, Name) -> Idris ()
65totcheck n = do i <- getIState; putIState $ i { idris_totcheck = idris_totcheck i ++ [n] }
67setFlags :: Name -> [FnOpt] -> Idris ()
68setFlags n fs = do i <- getIState; putIState $ i { idris_flags = addDef n fs (idris_flags i) }
70setAccessibility :: Name -> Accessibility -> Idris ()
73 let ctxt = setAccess n a (tt_ctxt i)
74 putIState $ i { tt_ctxt = ctxt }
76setTotality :: Name -> Totality -> Idris ()
79 let ctxt = setTotal n a (tt_ctxt i)
80 putIState $ i { tt_ctxt = ctxt }
82getTotality :: Name -> Idris Totality
85 case lookupTotal n (tt_ctxt i) of
87 _ -> return (Total [])
89-- Get coercions which might return the required type
90getCoercionsTo :: IState -> Type -> [Name]
92 let cs = idris_coercions i
93 (fn,_) = unApply (getRetTy ty) in
95 where findCoercions t [] = []
96 findCoercions t (n : ns) =
97 let ps = case lookupTy n (tt_ctxt i) of
98 [ty] -> case unApply (getRetTy ty) of
100 if t == t' then [n] else []
102 ps ++ findCoercions t ns
104addToCG :: Name -> CGInfo -> Idris ()
107 putIState $ i { idris_callgraph = addDef n cg (idris_callgraph i) }
109addCoercion :: Name -> Idris ()
110addCoercion n = do i <- getIState
111 putIState $ i { idris_coercions = n : idris_coercions i }
113addDocStr :: Name -> String -> Idris ()
116 putIState $ i { idris_docstrings = addDef n doc (idris_docstrings i) }
118addToCalledG :: Name -> [Name] -> Idris ()
119addToCalledG n ns = return () -- TODO
121-- Add a class instance function. Dodgy hack: Put integer instances first in the
122-- list so they are resolved by default.
123-- Dodgy hack 2: put constraint chasers (@@) last
125addInstance :: Bool -> Name -> Name -> Idris ()
127 = do ist <- getIState
128 case lookupCtxt n (idris_classes ist) of
130 do let cs = addDef n (CI a b c d (addI i ins)) (idris_classes ist)
131 putIState $ ist { idris_classes = cs }
132 _ -> do let cs = addDef n (CI (MN 0 "none") [] [] [] [i]) (idris_classes ist)
133 putIState $ ist { idris_classes = cs }
134 where addI i ins | int = i : ins
135 | chaser n = ins ++ [i]
136 | otherwise = insI i ins
138 insI i (n : ns) | chaser n = i : n : ns
139 | otherwise = n : insI i ns
141 chaser (UN ('@':'@':_)) = True
142 chaser (NS n _) = chaser n
145addClass :: Name -> ClassInfo -> Idris ()
147 = do ist <- getIState
148 let i' = case lookupCtxt n (idris_classes ist) of
149 [c] -> c { class_instances = class_instances i }
151 putIState $ ist { idris_classes = addDef n i' (idris_classes ist) }
153addIBC :: IBCWrite -> Idris ()
156 when (notDef (ibc_write i)) $
157 putIState $ i { ibc_write = ibc : ibc_write i }
158 where notDef [] = True
159 notDef (IBCDef n': is) | n == n' = False
160 notDef (_ : is) = notDef is
161addIBC ibc = do i <- getIState; putIState $ i { ibc_write = ibc : ibc_write i }
164clearIBC = do i <- getIState; putIState $ i { ibc_write = [] }
166getHdrs :: Codegen -> Idris [String]
167getHdrs tgt = do i <- getIState; return (forCodegen tgt $ idris_hdrs i)
169setErrLine :: Int -> Idris ()
170setErrLine x = do i <- getIState;
172 Nothing -> putIState $ i { errLine = Just x }
176clearErr = do i <- getIState
177 putIState $ i { errLine = Nothing }
179getSO :: Idris (Maybe String)
180getSO = do i <- getIState
181 return (compiled_so i)
183setSO :: Maybe String -> Idris ()
184setSO s = do i <- getIState
185 putIState $ (i { compiled_so = s })
187getIState :: Idris IState
190putIState :: IState -> Idris ()
194getName = do i <- getIState;
195 let idx = idris_name i;
196 putIState $ (i { idris_name = idx + 1 })
199checkUndefined :: FC -> Name -> Idris ()
203 (_:_) -> fail $ show fc ++ ":" ++
204 show n ++ " already defined"
207isUndefined :: FC -> Name -> Idris Bool
211 (_:_) -> return False
214setContext :: Context -> Idris ()
215setContext ctxt = do i <- getIState; putIState $ (i { tt_ctxt = ctxt } )
217updateContext :: (Context -> Context) -> Idris ()
218updateContext f = do i <- getIState; putIState $ (i { tt_ctxt = f (tt_ctxt i) } )
220addConstraints :: FC -> (Int, [UConstraint]) -> Idris ()
221addConstraints fc (v, cs)
224 let ctxt' = ctxt { uconstraints = cs ++ uconstraints ctxt,
226 let ics = zip cs (repeat fc) ++ idris_constraints i
227 putIState $ i { tt_ctxt = ctxt', idris_constraints = ics }
229addDeferred = addDeferred' Ref
230addDeferredTyCon = addDeferred' (TCon 0 0)
232addDeferred' :: NameType -> [(Name, Type)] -> Idris ()
234 = do mapM_ (\(n, t) -> updateContext (addTyDecl n nt (tidyNames [] t))) ns
236 putIState $ i { idris_metavars = map fst ns ++ idris_metavars i }
237 where tidyNames used (Bind (MN i x) b sc)
238 = let n' = uniqueName (UN x) used in
239 Bind n' b $ tidyNames (n':used) sc
240 tidyNames used (Bind n b sc)
241 = let n' = uniqueName n used in
242 Bind n' b $ tidyNames (n':used) sc
245solveDeferred :: Name -> Idris ()
246solveDeferred n = do i <- getIState
247 putIState $ i { idris_metavars = idris_metavars i \\ [n] }
249iResult :: String -> Idris ()
250iResult s = do i <- getIState
251 case idris_outputmode i of
252 RawOutput -> case s of
254 s -> liftIO $ putStrLn s
256 let good = SexpList [SymbolAtom "ok", toSExp s] in
257 liftIO $ putStrLn $ convSExp "return" good n
259iFail :: String -> Idris ()
260iFail s = do i <- getIState
261 case idris_outputmode i of
262 RawOutput -> case s of
264 s -> liftIO $ putStrLn s
266 let good = SexpList [SymbolAtom "error", toSExp s] in
267 liftIO . putStrLn $ convSExp "return" good n
269iputStrLn :: String -> Idris ()
270iputStrLn s = do i <- getIState
271 case idris_outputmode i of
272 RawOutput -> liftIO $ putStrLn s
274 case span (/=':') s of
275 (fn, ':':rest) -> case span isDigit rest of
276 ([], ':':msg) -> write
278 (num, ':':msg) -> iWarn (FC fn (read num)) msg
280 where write = liftIO . putStrLn $ convSExp "write-string" s n
282ideslavePutSExp :: SExpable a => String -> a -> Idris ()
283ideslavePutSExp cmd info = do i <- getIState
284 case idris_outputmode i of
285 IdeSlave n -> liftIO . putStrLn $ convSExp cmd info n
288-- this needs some typing magic and more structured output towards emacs
289iputGoal :: String -> Idris ()
290iputGoal s = do i <- getIState
291 case idris_outputmode i of
292 RawOutput -> liftIO $ putStrLn s
293 IdeSlave n -> liftIO . putStrLn $ convSExp "write-goal" s n
295isetPrompt :: String -> Idris ()
296isetPrompt p = do i <- getIState
297 case idris_outputmode i of
298 IdeSlave n -> liftIO . putStrLn $ convSExp "set-prompt" p n
300iWarn :: FC -> String -> Idris ()
301iWarn fc err = do i <- getIState
302 case idris_outputmode i of
303 RawOutput -> liftIO $ putStrLn (show fc ++ ":" ++ err)
304 IdeSlave n -> liftIO . putStrLn $ convSExp "warning" (fc_fname fc, fc_line fc, err) n
306setLogLevel :: Int -> Idris ()
307setLogLevel l = do i <- getIState
308 let opts = idris_options i
309 let opt' = opts { opt_logLevel = l }
310 putIState $ i { idris_options = opt' }
312setCmdLine :: [Opt] -> Idris ()
313setCmdLine opts = do i <- getIState
314 let iopts = idris_options i
315 putIState $ i { idris_options = iopts { opt_cmdline = opts } }
317getDumpDefun :: Idris (Maybe FilePath)
318getDumpDefun = do i <- getIState
319 return $ findC (opt_cmdline (idris_options i))
320 where findC [] = Nothing
321 findC (DumpDefun x : _) = Just x
322 findC (_ : xs) = findC xs
324getDumpCases :: Idris (Maybe FilePath)
325getDumpCases = do i <- getIState
326 return $ findC (opt_cmdline (idris_options i))
327 where findC [] = Nothing
328 findC (DumpCases x : _) = Just x
329 findC (_ : xs) = findC xs
332logLevel = do i <- getIState
333 return (opt_logLevel (idris_options i))
335setErrContext :: Bool -> Idris ()
336setErrContext t = do i <- getIState
337 let opts = idris_options i
338 let opt' = opts { opt_errContext = t }
339 putIState $ i { idris_options = opt' }
341errContext :: Idris Bool
342errContext = do i <- getIState
343 return (opt_errContext (idris_options i))
346useREPL = do i <- getIState
347 return (opt_repl (idris_options i))
349setREPL :: Bool -> Idris ()
350setREPL t = do i <- getIState
351 let opts = idris_options i
352 let opt' = opts { opt_repl = t }
353 putIState $ i { idris_options = opt' }
355setQuiet :: Bool -> Idris ()
356setQuiet q = do i <- getIState
357 let opts = idris_options i
358 let opt' = opts { opt_quiet = q }
359 putIState $ i { idris_options = opt' }
361getQuiet :: Idris Bool
362getQuiet = do i <- getIState
363 let opts = idris_options i
364 return (opt_quiet opts)
366setCodegen :: Codegen -> Idris ()
367setCodegen t = do i <- getIState
368 let opts = idris_options i
369 let opt' = opts { opt_codegen = t }
370 putIState $ i { idris_options = opt' }
372codegen :: Idris Codegen
373codegen = do i <- getIState
374 return (opt_codegen (idris_options i))
376setOutputTy :: OutputType -> Idris ()
377setOutputTy t = do i <- getIState
378 let opts = idris_options i
379 let opt' = opts { opt_outputTy = t }
380 putIState $ i { idris_options = opt' }
382outputTy :: Idris OutputType
383outputTy = do i <- getIState
384 return $ opt_outputTy $ idris_options i
386setIdeSlave :: Bool -> Idris ()
387setIdeSlave True = do i <- getIState
388 putIState $ i { idris_outputmode = (IdeSlave 0) }
389setIdeSlave False = return ()
391setTargetTriple :: String -> Idris ()
392setTargetTriple t = do i <- getIState
393 let opts = idris_options i
394 opt' = opts { opt_triple = t }
395 putIState $ i { idris_options = opt' }
397targetTriple :: Idris String
398targetTriple = do i <- getIState
399 return (opt_triple (idris_options i))
401setTargetCPU :: String -> Idris ()
402setTargetCPU t = do i <- getIState
403 let opts = idris_options i
404 opt' = opts { opt_cpu = t }
405 putIState $ i { idris_options = opt' }
407targetCPU :: Idris String
408targetCPU = do i <- getIState
409 return (opt_cpu (idris_options i))
411setOptLevel :: Int -> Idris ()
412setOptLevel t = do i <- getIState
413 let opts = idris_options i
414 opt' = opts { opt_optLevel = t }
415 putIState $ i { idris_options = opt' }
418optLevel = do i <- getIState
419 return (opt_optLevel (idris_options i))
422verbose = do i <- getIState
423 return (opt_verbose (idris_options i))
425setVerbose :: Bool -> Idris ()
426setVerbose t = do i <- getIState
427 let opts = idris_options i
428 let opt' = opts { opt_verbose = t }
429 putIState $ i { idris_options = opt' }
431typeInType :: Idris Bool
432typeInType = do i <- getIState
433 return (opt_typeintype (idris_options i))
435setTypeInType :: Bool -> Idris ()
436setTypeInType t = do i <- getIState
437 let opts = idris_options i
438 let opt' = opts { opt_typeintype = t }
439 putIState $ i { idris_options = opt' }
441coverage :: Idris Bool
442coverage = do i <- getIState
443 return (opt_coverage (idris_options i))
445setCoverage :: Bool -> Idris ()
446setCoverage t = do i <- getIState
447 let opts = idris_options i
448 let opt' = opts { opt_coverage = t }
449 putIState $ i { idris_options = opt' }
451setIBCSubDir :: FilePath -> Idris ()
452setIBCSubDir fp = do i <- getIState
453 let opts = idris_options i
454 let opt' = opts { opt_ibcsubdir = fp }
455 putIState $ i { idris_options = opt' }
457valIBCSubDir :: IState -> Idris FilePath
458valIBCSubDir i = return (opt_ibcsubdir (idris_options i))
460addImportDir :: FilePath -> Idris ()
461addImportDir fp = do i <- getIState
462 let opts = idris_options i
463 let opt' = opts { opt_importdirs = fp : opt_importdirs opts }
464 putIState $ i { idris_options = opt' }
466setImportDirs :: [FilePath] -> Idris ()
467setImportDirs fps = do i <- getIState
468 let opts = idris_options i
469 let opt' = opts { opt_importdirs = fps }
470 putIState $ i { idris_options = opt' }
472allImportDirs :: IState -> Idris [FilePath]
473allImportDirs i = do let optdirs = opt_importdirs (idris_options i)
474 return ("." : reverse optdirs)
477impShow = do i <- getIState
478 return (opt_showimp (idris_options i))
480setImpShow :: Bool -> Idris ()
481setImpShow t = do i <- getIState
482 let opts = idris_options i
483 let opt' = opts { opt_showimp = t }
484 putIState $ i { idris_options = opt' }
486logLvl :: Int -> String -> Idris ()
487logLvl l str = do i <- getIState
488 let lvl = opt_logLevel (idris_options i)
490 $ do liftIO (putStrLn str)
491 putIState $ i { idris_log = idris_log i ++ str ++ "\n" }
493cmdOptType :: Opt -> Idris Bool
494cmdOptType x = do i <- getIState
495 return $ x `elem` opt_cmdline (idris_options i)
497iLOG :: String -> Idris ()
500noErrors :: Idris Bool
501noErrors = do i <- getIState
503 Nothing -> return True
506setTypeCase :: Bool -> Idris ()
507setTypeCase t = do i <- getIState
508 let opts = idris_options i
509 let opt' = opts { opt_typecase = t }
510 putIState $ i { idris_options = opt' }
513-- For inferring types of things
517inferTy = MN 0 "__Infer"
518inferCon = MN 0 "__infer"
519inferDecl = PDatadecl inferTy
521 [("", inferCon, PPi impl (MN 0 "A") PType (
522 PPi expl (MN 0 "a") (PRef bi (MN 0 "A"))
523 (PRef bi inferTy)), bi)]
525infTerm t = PApp bi (PRef bi inferCon) [pimp (MN 0 "A") Placeholder, pexp t]
526infP = P (TCon 6 0) inferTy (TType (UVal 0))
528getInferTerm, getInferType :: Term -> Term
529getInferTerm (Bind n b sc) = Bind n b $ getInferTerm sc
530getInferTerm (App (App _ _) tm) = tm
531getInferTerm tm = tm -- error ("getInferTerm " ++ show tm)
533getInferType (Bind n b sc) = Bind n b $ getInferType sc
534getInferType (App (App _ ty) _) = ty
536-- Handy primitives: Unit, False, Pair, MkPair, =, mkForeign
538primNames = [unitTy, unitCon,
539 falseTy, pairTy, pairCon,
540 eqTy, eqCon, inferTy, inferCon]
542unitTy = MN 0 "__Unit"
544unitDecl = PDatadecl unitTy PType
545 [("", unitCon, PRef bi unitTy, bi)]
547falseTy = MN 0 "__False"
548falseDecl = PDatadecl falseTy PType []
550pairTy = MN 0 "__Pair"
551pairCon = MN 0 "__MkPair"
552pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
553 [("", pairCon, PPi impl (n "A") PType (
554 PPi impl (n "B") PType (
555 PPi expl (n "a") (PRef bi (n "A")) (
556 PPi expl (n "b") (PRef bi (n "B"))
557 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
558 pexp (PRef bi (n "B"))])))), bi)]
563eqDecl = PDatadecl eqTy (piBind [(n "a", PType), (n "b", PType),
564 (n "x", PRef bi (n "a")), (n "y", PRef bi (n "b"))]
566 [("", eqCon, PPi impl (n "a") PType (
567 PPi impl (n "x") (PRef bi (n "a"))
568 (PApp bi (PRef bi eqTy) [pimp (n "a") Placeholder,
569 pimp (n "b") Placeholder,
570 pexp (PRef bi (n "x")),
571 pexp (PRef bi (n "x"))])), bi)]
574-- Defined in builtins.idr
576existsCon = UN "Ex_intro"
578piBind :: [(Name, PTerm)] -> PTerm -> PTerm
581piBindp :: Plicity -> [(Name, PTerm)] -> PTerm -> PTerm
583piBindp p ((n, ty):ns) t = PPi p n ty (piBind ns t)
585-- Dealing with parameters
587expandParams :: (Name -> Name) -> [(Name, PTerm)] ->
588 [Name] -> -- all names
589 [Name] -> -- names with no declaration
591expandParams dec ps ns infs tm = en tm
593 -- if we shadow a name (say in a lambda binding) that is used in a call to
594 -- a lifted function, we need access to both names - once in the scope of the
595 -- binding and once to call the lifted functions. So we'll explicitly shadow
596 -- it. (Yes, it's a hack. The alternative would be to resolve names earlier
599 mkShadow (UN n) = MN 0 n
600 mkShadow (MN i n) = MN (i+1) n
601 mkShadow (NS x s) = NS (mkShadow x) s
604 | n `elem` map fst ps
605 = let n' = mkShadow n in
606 PLam n' (en t) (en (shadow n n' s))
607 | otherwise = PLam n (en t) (en s)
609 | n `elem` map fst ps
610 = let n' = mkShadow n in
611 PPi p n' (en t) (en (shadow n n' s))
612 | otherwise = PPi p n (en t) (en s)
614 | n `elem` map fst ps
615 = let n' = mkShadow n in
616 PLet n' (en ty) (en v) (en (shadow n n' s))
617 | otherwise = PLet n (en ty) (en v) (en s)
618 en (PEq f l r) = PEq f (en l) (en r)
619 en (PRewrite f l r g) = PRewrite f (en l) (en r) (fmap en g)
620 en (PTyped l r) = PTyped (en l) (en r)
621 en (PPair f l r) = PPair f (en l) (en r)
622 en (PDPair f l t r) = PDPair f (en l) (en t) (en r)
623 en (PAlternative a as) = PAlternative a (map en as)
624 en (PHidden t) = PHidden (en t)
625 en (PUnifyLog t) = PUnifyLog (en t)
626 en (PDoBlock ds) = PDoBlock (map (fmap en) ds)
627 en (PProof ts) = PProof (map (fmap en) ts)
628 en (PTactics ts) = PTactics (map (fmap en) ts)
631 | n `nselem` ns = PQuote (Var (dec n))
632 en (PApp fc (PInferRef fc' n) as)
633 | n `nselem` ns = PApp fc (PInferRef fc' (dec n))
634 (map (pexp . (PRef fc)) (map fst ps) ++ (map (fmap en) as))
635 en (PApp fc (PRef fc' n) as)
636 | n `elem` infs = PApp fc (PInferRef fc' (dec n))
637 (map (pexp . (PRef fc)) (map fst ps) ++ (map (fmap en) as))
638 | n `nselem` ns = PApp fc (PRef fc' (dec n))
639 (map (pexp . (PRef fc)) (map fst ps) ++ (map (fmap en) as))
641 | n `elem` infs = PApp fc (PInferRef fc (dec n))
642 (map (pexp . (PRef fc)) (map fst ps))
643 | n `nselem` ns = PApp fc (PRef fc (dec n))
644 (map (pexp . (PRef fc)) (map fst ps))
646 | n `nselem` ns = PApp fc (PInferRef fc (dec n))
647 (map (pexp . (PRef fc)) (map fst ps))
648 en (PApp fc f as) = PApp fc (en f) (map (fmap en) as)
649 en (PCase fc c os) = PCase fc (en c) (map (pmap en) os)
653 nselem x (y : xs) | nseq x y = True
654 | otherwise = nselem x xs
656 nseq x y = nsroot x == nsroot y
658expandParamsD :: Bool -> -- True = RHS only
660 (Name -> Name) -> [(Name, PTerm)] -> [Name] -> PDecl -> PDecl
661expandParamsD rhsonly ist dec ps ns (PTy doc syn fc o n ty)
662 = if n `elem` ns && (not rhsonly)
663 then -- trace (show (n, expandParams dec ps ns ty)) $
664 PTy doc syn fc o (dec n) (piBind ps (expandParams dec ps ns [] ty))
665 else --trace (show (n, expandParams dec ps ns ty)) $
666 PTy doc syn fc o n (expandParams dec ps ns [] ty)
667expandParamsD rhsonly ist dec ps ns (PPostulate doc syn fc o n ty)
668 = if n `elem` ns && (not rhsonly)
669 then -- trace (show (n, expandParams dec ps ns ty)) $
670 PPostulate doc syn fc o (dec n) (piBind ps
671 (expandParams dec ps ns [] ty))
672 else --trace (show (n, expandParams dec ps ns ty)) $
673 PPostulate doc syn fc o n (expandParams dec ps ns [] ty)
674expandParamsD rhsonly ist dec ps ns (PClauses fc opts n cs)
675 = let n' = if n `elem` ns then dec n else n in
676 PClauses fc opts n' (map expandParamsC cs)
678 expandParamsC (PClause fc n lhs ws rhs ds)
679 = let -- ps' = updateps True (namesIn ist rhs) (zip ps [0..])
680 ps'' = updateps False (namesIn [] ist lhs) (zip ps [0..])
681 lhs' = if rhsonly then lhs else (expandParams dec ps'' ns [] lhs)
682 n' = if n `elem` ns then dec n else n in
684 (map (expandParams dec ps'' ns []) ws)
685 (expandParams dec ps'' ns [] rhs)
686 (map (expandParamsD True ist dec ps'' ns) ds)
687 expandParamsC (PWith fc n lhs ws wval ds)
688 = let -- ps' = updateps True (namesIn ist wval) (zip ps [0..])
689 ps'' = updateps False (namesIn [] ist lhs) (zip ps [0..])
690 lhs' = if rhsonly then lhs else (expandParams dec ps'' ns [] lhs)
691 n' = if n `elem` ns then dec n else n in
693 (map (expandParams dec ps'' ns []) ws)
694 (expandParams dec ps'' ns [] wval)
695 (map (expandParamsD rhsonly ist dec ps'' ns) ds)
696 updateps yn nm [] = []
697 updateps yn nm (((a, t), i):as)
698 | (a `elem` nm) == yn = (a, t) : updateps yn nm as
699 | otherwise = (MN i (show n ++ "_u"), t) : updateps yn nm as
700expandParamsD rhs ist dec ps ns (PData doc syn fc co pd)
701 = PData doc syn fc co (expandPData pd)
703 -- just do the type decl, leave constructors alone (parameters will be
705 expandPData (PDatadecl n ty cons)
707 then PDatadecl (dec n) (piBind ps (expandParams dec ps ns [] ty))
709 else PDatadecl n (expandParams dec ps ns [] ty) (map econ cons)
711 = (doc, dec n, piBindp expl ps (expandParams dec ps ns [] t), fc)
712expandParamsD rhs ist dec ps ns (PParams f params pds)
713 = PParams f (ps ++ map (mapsnd (expandParams dec ps ns [])) params)
714 (map (expandParamsD True ist dec ps ns) pds)
715-- (map (expandParamsD ist dec ps ns) pds)
716expandParamsD rhs ist dec ps ns (PMutual f pds)
717 = PMutual f (map (expandParamsD rhs ist dec ps ns) pds)
718expandParamsD rhs ist dec ps ns (PClass doc info f cs n params decls)
720 (map (expandParams dec ps ns []) cs)
722 (map (mapsnd (expandParams dec ps ns [])) params)
723 (map (expandParamsD rhs ist dec ps ns) decls)
724expandParamsD rhs ist dec ps ns (PInstance info f cs n params ty cn decls)
726 (map (expandParams dec ps ns []) cs)
728 (map (expandParams dec ps ns []) params)
729 (expandParams dec ps ns [] ty)
731 (map (expandParamsD rhs ist dec ps ns) decls)
732expandParamsD rhs ist dec ps ns d = d
734mapsnd f (x, t) = (x, f t)
736-- Calculate a priority for a type, for deciding elaboration order
737-- * if it's just a type variable or concrete type, do it early (0)
738-- * if there's only type variables and injective constructors, do it next (1)
739-- * if there's a function type, next (2)
740-- * finally, everything else (3)
742getPriority :: IState -> PTerm -> Int
743getPriority i tm = 1 -- pri tm
746 case lookupP n (tt_ctxt i) of
747 ((P (DCon _ _) _ _):_) -> 1
748 ((P (TCon _ _) _ _):_) -> 1
750 [] -> 0 -- must be locally bound, if it's not an error...
751 pri (PPi _ _ x y) = max 5 (max (pri x) (pri y))
755 pri (PEq _ l r) = max 1 (max (pri l) (pri r))
756 pri (PRewrite _ l r _) = max 1 (max (pri l) (pri r))
757 pri (PApp _ f as) = max 1 (max (pri f) (foldr max 0 (map (pri.getTm) as)))
758 pri (PCase _ f as) = max 1 (max (pri f) (foldr max 0 (map (pri.snd) as)))
759 pri (PTyped l r) = pri l
760 pri (PPair _ l r) = max 1 (max (pri l) (pri r))
761 pri (PDPair _ l t r) = max 1 (max (pri l) (max (pri t) (pri r)))
762 pri (PAlternative a as) = maximum (map pri as)
763 pri (PConstant _) = 0
767addStatics :: Name -> Term -> PTerm -> Idris ()
769 do let (statics, dynamics) = initStatics tm ptm
770 let stnames = nub $ concatMap freeNames (map snd statics)
771 let dnames = nub $ concatMap freeNames (map snd dynamics)
772 when (not (null statics)) $
773 logLvl 7 $ show n ++ " " ++ show statics ++ "\n" ++ show dynamics
774 ++ "\n" ++ show stnames ++ "\n" ++ show dnames
775 let statics' = nub $ map fst statics ++
776 filter (\x -> not (elem x dnames)) stnames
777 let stpos = staticList statics' tm
779 putIState $ i { idris_statics = addDef n stpos (idris_statics i) }
782 initStatics (Bind n (Pi ty) sc) (PPi p _ _ s)
783 = let (static, dynamic) = initStatics (instantiate (P Bound n ty) sc) s in
784 if pstatic p == Static then ((n, ty) : static, dynamic)
785 else (static, (n, ty) : dynamic)
786 initStatics t pt = ([], [])
788 staticList sts (Bind n (Pi _) sc) = (n `elem` sts) : staticList sts sc
791-- Dealing with implicit arguments
793-- Add constraint bindings from using block
795addUsingConstraints :: SyntaxInfo -> FC -> PTerm -> Idris PTerm
796addUsingConstraints syn fc t
798 let ns = namesIn [] ist t
799 let cs = getConstraints t -- check declared constraints
800 let addconsts = uconsts \\ cs
801 -- if all names in the arguments of addconsts appear in ns,
802 -- add the constraint implicitly
803 return (doAdd addconsts ns t)
804 where uconsts = filter uconst (using syn)
805 uconst (UConstraint _ _) = True
809 -- if all of args in ns, then add it
810 doAdd (UConstraint c args : cs) ns t
811 | all (\n -> elem n ns) args
812 = PPi (Constraint False Dynamic "") (MN 0 "cu")
813 (mkConst c args) (doAdd cs ns t)
814 | otherwise = doAdd cs ns t
816 mkConst c args = PApp fc (PRef fc c)
817 (map (\n -> PExp 0 False (PRef fc n) "") args)
819 getConstraints (PPi (Constraint _ _ _) _ c sc)
820 = getcapp c ++ getConstraints sc
821 getConstraints (PPi _ _ c sc) = getConstraints sc
822 getConstraints _ = []
824 getcapp (PApp _ (PRef _ c) args)
825 = do ns <- mapM getName args
826 return (UConstraint c ns)
829 getName (PExp _ _ (PRef _ n) _) = return n
832-- Add implicit Pi bindings for any names in the term which appear in an
835-- This has become a right mess already. Better redo it some time...
837implicit :: SyntaxInfo -> Name -> PTerm -> Idris PTerm
838implicit syn n ptm = implicit' syn [] n ptm
840implicit' :: SyntaxInfo -> [Name] -> Name -> PTerm -> Idris PTerm
841implicit' syn ignore n ptm
843 let (tm', impdata) = implicitise syn ignore i ptm
844-- let (tm'', spos) = findStatics i tm'
845 putIState $ i { idris_implicits = addDef n impdata (idris_implicits i) }
847 logLvl 5 ("Implicit " ++ show n ++ " " ++ show impdata)
849-- putIState $ i { idris_statics = addDef n spos (idris_statics i) }
852implicitise :: SyntaxInfo -> [Name] -> IState -> PTerm -> (PTerm, [PArg])
853implicitise syn ignore ist tm = -- trace ("INCOMING " ++ showImp True tm) $
854 let (declimps, ns') = execState (imps True [] tm) ([], [])
855 ns = filter (\n -> implicitable n || elem n (map fst uvars)) $
856 ns' \\ (map fst pvars ++ no_imp syn ++ ignore)
857 nsOrder = filter (not . inUsing) ns ++ filter inUsing ns in
859 then (tm, reverse declimps)
860 else implicitise syn ignore ist (pibind uvars nsOrder tm)
862 uvars = map ipair (filter uimplicit (using syn))
863 pvars = syn_params syn
865 inUsing n = n `elem` map fst uvars
867 ipair (UImplicit x y) = (x, y)
868 uimplicit (UImplicit _ _) = True
871 dropAll (x:xs) ys | x `elem` ys = dropAll xs ys
872 | otherwise = x : dropAll xs ys
875 imps top env (PApp _ f as)
876 = do (decls, ns) <- get
877 let isn = concatMap (namesIn uvars ist) (map getTm as)
878 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
879 imps top env (PPi (Imp l _ doc) n ty sc)
880 = do let isn = nub (namesIn uvars ist ty) `dropAll` [n]
882 put (PImp (getPriority ist ty) l n ty doc : decls,
883 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
885 imps top env (PPi (Exp l _ doc) n ty sc)
886 = do let isn = nub (namesIn uvars ist ty ++ case sc of
887 (PRef _ x) -> namesIn uvars ist sc `dropAll` [n]
889 (decls, ns) <- get -- ignore decls in HO types
890 put (PExp (getPriority ist ty) l ty doc : decls,
891 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
893 imps top env (PPi (Constraint l _ doc) n ty sc)
894 = do let isn = nub (namesIn uvars ist ty ++ case sc of
895 (PRef _ x) -> namesIn uvars ist sc `dropAll` [n]
897 (decls, ns) <- get -- ignore decls in HO types
898 put (PConstraint 10 l ty doc : decls,
899 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
901 imps top env (PPi (TacImp l _ scr doc) n ty sc)
902 = do let isn = nub (namesIn uvars ist ty ++ case sc of
903 (PRef _ x) -> namesIn uvars ist sc `dropAll` [n]
905 (decls, ns) <- get -- ignore decls in HO types
906 put (PTacImplicit 10 l n scr ty doc : decls,
907 nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
909 imps top env (PEq _ l r)
910 = do (decls, ns) <- get
911 let isn = namesIn uvars ist l ++ namesIn uvars ist r
912 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
913 imps top env (PRewrite _ l r _)
914 = do (decls, ns) <- get
915 let isn = namesIn uvars ist l ++ namesIn uvars ist r
916 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
917 imps top env (PTyped l r)
919 imps top env (PPair _ l r)
920 = do (decls, ns) <- get
921 let isn = namesIn uvars ist l ++ namesIn uvars ist r
922 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
923 imps top env (PDPair _ (PRef _ n) t r)
924 = do (decls, ns) <- get
925 let isn = nub (namesIn uvars ist t ++ namesIn uvars ist r) \\ [n]
926 put (decls, nub (ns ++ (isn \\ (env ++ map fst (getImps decls)))))
927 imps top env (PDPair _ l t r)
928 = do (decls, ns) <- get
929 let isn = namesIn uvars ist l ++ namesIn uvars ist t ++
931 put (decls, nub (ns ++ (isn \\ (env ++ map fst (getImps decls)))))
932 imps top env (PAlternative a as)
933 = do (decls, ns) <- get
934 let isn = concatMap (namesIn uvars ist) as
935 put (decls, nub (ns ++ (isn `dropAll` (env ++ map fst (getImps decls)))))
936 imps top env (PLam n ty sc)
937 = do imps False env ty
938 imps False (n:env) sc
939 imps top env (PHidden tm) = imps False env tm
940 imps top env (PUnifyLog tm) = imps False env tm
941 imps top env _ = return ()
943 pibind using [] sc = sc
944 pibind using (n:ns) sc
945 = case lookup n using of
946 Just ty -> PPi (Imp False Dynamic "") n ty (pibind using ns sc)
947 Nothing -> PPi (Imp False Dynamic "") n Placeholder
950-- Add implicit arguments in function calls
951addImplPat :: IState -> PTerm -> PTerm
952addImplPat = addImpl' True [] []
954addImplBound :: IState -> [Name] -> PTerm -> PTerm
955addImplBound ist ns = addImpl' False ns [] ist
957addImplBoundInf :: IState -> [Name] -> [Name] -> PTerm -> PTerm
958addImplBoundInf ist ns inf = addImpl' False ns inf ist
960addImpl :: IState -> PTerm -> PTerm
961addImpl = addImpl' False [] []
963-- TODO: in patterns, don't add implicits to function names guarded by constructors
964-- and *not* inside a PHidden
966addImpl' :: Bool -> [Name] -> [Name] -> IState -> PTerm -> PTerm
967addImpl' inpat env infns ist ptm = ai (zip env (repeat Nothing)) ptm
970 | f `elem` infns = PInferRef fc f
971 | not (f `elem` map fst env) = handleErr $ aiFn inpat inpat ist fc f []
972 ai env (PHidden (PRef fc f))
973 | not (f `elem` map fst env) = handleErr $ aiFn inpat False ist fc f []
974 ai env (PEq fc l r) = let l' = ai env l
977 ai env (PRewrite fc l r g) = let l' = ai env l
979 g' = fmap (ai env) g in
981 ai env (PTyped l r) = let l' = ai env l
984 ai env (PPair fc l r) = let l' = ai env l
987 ai env (PDPair fc l t r) = let l' = ai env l
991 ai env (PAlternative a as) = let as' = map (ai env) as in
993 ai env (PApp fc (PInferRef _ f) as)
994 = let as' = map (fmap (ai env)) as in
995 PApp fc (PInferRef fc f) as'
996 ai env (PApp fc ftm@(PRef _ f) as)
997 | f `elem` infns = ai env (PApp fc (PInferRef fc f) as)
998 | not (f `elem` map fst env)
999 = let as' = map (fmap (ai env)) as in
1000 handleErr $ aiFn inpat False ist fc f as'
1001 | Just (Just ty) <- lookup f env
1002 = let as' = map (fmap (ai env)) as
1003 arity = getPArity ty in
1004 mkPApp fc arity ftm as'
1005 ai env (PApp fc f as) = let f' = ai env f
1006 as' = map (fmap (ai env)) as in
1008 ai env (PCase fc c os) = let c' = ai env c
1009 os' = map (pmap (ai env)) os in
1011 ai env (PLam n ty sc) = let ty' = ai env ty
1012 sc' = ai ((n, Just ty):env) sc in
1014 ai env (PLet n ty val sc)
1015 = let ty' = ai env ty
1017 sc' = ai ((n, Just ty):env) sc in
1019 ai env (PPi p n ty sc) = let ty' = ai env ty
1020 sc' = ai ((n, Just ty):env) sc in
1022 ai env (PHidden tm) = PHidden (ai env tm)
1023 ai env (PProof ts) = PProof (map (fmap (ai env)) ts)
1024 ai env (PTactics ts) = PTactics (map (fmap (ai env)) ts)
1025 ai env (PUnifyLog tm) = PUnifyLog (ai env tm)
1028 handleErr (Left err) = PElabError err
1029 handleErr (Right x) = x
1031-- if in a pattern, and there are no arguments, and there's no possible
1032-- names with zero explicit arguments, don't add implicits.
1034aiFn :: Bool -> Bool -> IState -> FC -> Name -> [PArg] -> Either Err PTerm
1035aiFn inpat True ist fc f []
1036 = case lookupDef f (tt_ctxt ist) of
1037 [] -> Right $ PPatvar fc f
1038 alts -> let ialts = lookupCtxt f (idris_implicits ist) in
1039 -- trace (show f ++ " " ++ show (fc, any (all imp) ialts, ialts, any constructor alts)) $
1040 if (not (vname f) || tcname f
1041 || any constructor alts || any allImp ialts)
1042 then aiFn inpat False ist fc f [] -- use it as a constructor
1043 else Right $ PPatvar fc f
1044 where imp (PExp _ _ _ _) = False
1047 allImp xs = all imp xs
1048 constructor (TyDecl (DCon _ _) _) = True
1049 constructor _ = False
1051 vname (UN n) = True -- non qualified
1054aiFn inpat expat ist fc f as
1055 | f `elem` primNames = Right $ PApp fc (PRef fc f) as
1056aiFn inpat expat ist fc f as
1057 -- This is where namespaces get resolved by adding PAlternative
1058 = case lookupCtxtName f (idris_implicits ist) of
1059 [(f',ns)] -> Right $ mkPApp fc (length ns) (PRef fc f') (insertImpl ns as)
1060 [] -> if f `elem` idris_metavars ist
1061 then Right $ PApp fc (PRef fc f) as
1062 else Right $ mkPApp fc (length as) (PRef fc f) as
1065 map (\(f', ns) -> mkPApp fc (length ns) (PRef fc f')
1066 (insertImpl ns as)) alts
1068 insertImpl :: [PArg] -> [PArg] -> [PArg]
1069 insertImpl (PExp p l ty _ : ps) (PExp _ _ tm d : given) =
1070 PExp p l tm d : insertImpl ps given
1071 insertImpl (PConstraint p l ty _ : ps) (PConstraint _ _ tm d : given) =
1072 PConstraint p l tm d : insertImpl ps given
1073 insertImpl (PConstraint p l ty d : ps) given =
1074 PConstraint p l (PResolveTC fc) d : insertImpl ps given
1075 insertImpl (PImp p l n ty d : ps) given =
1076 case find n given [] of
1077 Just (tm, given') -> PImp p l n tm "" : insertImpl ps given'
1078 Nothing -> PImp p l n Placeholder "" : insertImpl ps given
1079 insertImpl (PTacImplicit p l n sc ty d : ps) given =
1080 case find n given [] of
1081 Just (tm, given') -> PTacImplicit p l n sc tm "" : insertImpl ps given'
1083 then PTacImplicit p l n sc Placeholder "" : insertImpl ps given
1084 else PTacImplicit p l n sc sc "" : insertImpl ps given
1085 insertImpl expected [] = []
1086 insertImpl _ given = given
1088 find n [] acc = Nothing
1089 find n (PImp _ _ n' t _ : gs) acc
1090 | n == n' = Just (t, reverse acc ++ gs)
1091 find n (PTacImplicit _ _ n' _ t _ : gs) acc
1092 | n == n' = Just (t, reverse acc ++ gs)
1093 find n (g : gs) acc = find n gs (g : acc)
1095-- replace non-linear occurrences with _
1096-- ASSUMPTION: This is called before adding 'alternatives' because otherwise
1097-- it is hard to get right!
1099stripLinear :: IState -> PTerm -> PTerm
1100stripLinear i tm = evalState (sl tm) [] where
1101 sl :: PTerm -> State [Name] PTerm
1103 | (_:_) <- lookupTy f (tt_ctxt i)
1104 = return $ PRef fc f
1105 | otherwise = do ns <- get
1107 then return Placeholder
1108 else do put (f : ns)
1113 then return Placeholder
1114 else do put (f : ns)
1115 return (PPatvar fc f)
1116 sl (PApp fc fn args) = do fn' <- sl fn
1117 args' <- mapM slA args
1118 return $ PApp fc fn' args'
1119 where slA (PImp p l n t d) = do t' <- sl t
1120 return $ PImp p l n t' d
1121 slA (PExp p l t d) = do t' <- sl t
1122 return $ PExp p l t' d
1123 slA (PConstraint p l t d)
1125 return $ PConstraint p l t' d
1126 slA (PTacImplicit p l n sc t d)
1128 return $ PTacImplicit p l n sc t' d
1132mkPApp fc a f as = let rest = drop a as in
1133 appRest fc (PApp fc f (take a as)) rest
1136 appRest fc f (a : as) = appRest fc (PApp fc f [a]) as
1138-- Find 'static' argument positions
1139-- (the declared ones, plus any names in argument position in the declared
1141-- FIXME: It's possible that this really has to happen after elaboration
1143findStatics :: IState -> PTerm -> (PTerm, [Bool])
1144findStatics ist tm = trace (showImp True tm) $
1145 let (ns, ss) = fs tm in
1146 runState (pos ns ss tm) []
1147 where fs (PPi p n t sc)
1148 | Static <- pstatic p
1149 = let (ns, ss) = fs sc in
1150 (namesIn [] ist t : ns, n : ss)
1151 | otherwise = let (ns, ss) = fs sc in
1155 inOne n ns = length (filter id (map (elem n) ns)) == 1
1157 pos ns ss (PPi p n t sc)
1158 | elem n ss = do sc' <- pos ns ss sc
1161 return (PPi (p { pstatic = Static }) n t sc')
1162 | otherwise = do sc' <- pos ns ss sc
1165 return (PPi p n t sc')
1166 pos ns ss t = return t
1168-- Debugging/logging stuff
1170dumpDecls :: [PDecl] -> String
1172dumpDecls (d:ds) = dumpDecl d ++ "\n" ++ dumpDecls ds
1174dumpDecl (PFix _ f ops) = show f ++ " " ++ showSep ", " ops
1175dumpDecl (PTy _ _ _ _ n t) = "tydecl " ++ show n ++ " : " ++ showImp True t
1176dumpDecl (PClauses _ _ n cs) = "pat " ++ show n ++ "\t" ++ showSep "\n\t" (map (showCImp True) cs)
1177dumpDecl (PData _ _ _ _ d) = showDImp True d
1178dumpDecl (PParams _ ns ps) = "params {" ++ show ns ++ "\n" ++ dumpDecls ps ++ "}\n"
1179dumpDecl (PNamespace n ps) = "namespace {" ++ n ++ "\n" ++ dumpDecls ps ++ "}\n"
1180dumpDecl (PSyntax _ syn) = "syntax " ++ show syn
1181dumpDecl (PClass _ _ _ cs n ps ds)
1182 = "class " ++ show cs ++ " " ++ show n ++ " " ++ show ps ++ "\n" ++ dumpDecls ds
1183dumpDecl (PInstance _ _ cs n _ t _ ds)
1184 = "instance " ++ show cs ++ " " ++ show n ++ " " ++ show t ++ "\n" ++ dumpDecls ds
1186-- dumpDecl (PImport i) = "import " ++ i
1188-- for 6.12/7 compatibility
1189data EitherErr a b = LeftErr a | RightOK b
1191instance Monad (EitherErr a) where
1194 (LeftErr e) >>= k = LeftErr e
1195 RightOK v >>= k = k v
1197toEither (LeftErr e) = Left e
1198toEither (RightOK ho) = Right ho
1200-- syntactic match of a against b, returning pair of variables in a
1201-- and what they match. Returns the pair that failed if not a match.
1203matchClause :: IState -> PTerm -> PTerm -> Either (PTerm, PTerm) [(Name, PTerm)]
1204matchClause = matchClause' False
1206matchClause' :: Bool -> IState -> PTerm -> PTerm -> Either (PTerm, PTerm) [(Name, PTerm)]
1207matchClause' names i x y = checkRpts $ match (fullApp x) (fullApp y) where
1208 matchArg x y = match (fullApp (getTm x)) (fullApp (getTm y))
1210 fullApp (PApp _ (PApp fc f args) xs) = fullApp (PApp fc f (args ++ xs))
1213 match' x y = match (fullApp x) (fullApp y)
1214 match (PApp _ (PRef _ (NS (UN "fromInteger") ["builtins"])) [_,_,x]) x'
1215 | PConstant (I _) <- getTm x = match (getTm x) x'
1216 match x' (PApp _ (PRef _ (NS (UN "fromInteger") ["builtins"])) [_,_,x])
1217 | PConstant (I _) <- getTm x = match (getTm x) x'
1218 match (PApp _ (PRef _ (UN "lazy")) [_,x]) x' = match (getTm x) x'
1219 match x (PApp _ (PRef _ (UN "lazy")) [_,x']) = match x (getTm x')
1220 match (PApp _ f args) (PApp _ f' args')
1221 | length args == length args'
1222 = do mf <- match' f f'
1223 ms <- zipWithM matchArg args args'
1224 return (mf ++ concat ms)
1225-- match (PRef _ n) (PRef _ n') | n == n' = return []
1226-- | otherwise = Nothing
1227 match (PRef f n) (PApp _ x []) = match (PRef f n) x
1228 match (PPatvar f n) xr = match (PRef f n) xr
1229 match xr (PPatvar f n) = match xr (PRef f n)
1230 match (PApp _ x []) (PRef f n) = match x (PRef f n)
1231 match (PRef _ n) tm@(PRef _ n')
1232 | n == n' && not names &&
1233 (not (isConName n (tt_ctxt i)) || tm == Placeholder)
1235 | n == n' = return []
1237 | not names && (not (isConName n (tt_ctxt i)) || tm == Placeholder)
1239 match (PEq _ l r) (PEq _ l' r') = do ml <- match' l l'
1242 match (PRewrite _ l r _) (PRewrite _ l' r' _)
1243 = do ml <- match' l l'
1246 match (PTyped l r) (PTyped l' r') = do ml <- match l l'
1249 match (PTyped l r) x = match l x
1250 match x (PTyped l r) = match x l
1251 match (PPair _ l r) (PPair _ l' r') = do ml <- match' l l'
1254 match (PDPair _ l t r) (PDPair _ l' t' r') = do ml <- match' l l'
1257 return (ml ++ mt ++ mr)
1258 match (PAlternative a as) (PAlternative a' as')
1259 = do ms <- zipWithM match' as as'
1261 match a@(PAlternative _ as) b
1262 = do let ms = zipWith match' as (repeat b)
1263 case (rights (map toEither ms)) of
1266 match (PCase _ _ _) _ = return [] -- lifted out
1267 match (PMetavar _) _ = return [] -- modified
1268 match (PInferRef _ _) _ = return [] -- modified
1269 match (PQuote _) _ = return []
1270 match (PProof _) _ = return []
1271 match (PTactics _) _ = return []
1272 match (PRefl _ _) (PRefl _ _) = return []
1273 match (PResolveTC _) (PResolveTC _) = return []
1274 match (PTrue _) (PTrue _) = return []
1275 match (PFalse _) (PFalse _) = return []
1276 match (PReturn _) (PReturn _) = return []
1277 match (PPi _ _ t s) (PPi _ _ t' s') = do mt <- match' t t'
1280 match (PLam _ t s) (PLam _ t' s') = do mt <- match' t t'
1283 match (PLet _ t ty s) (PLet _ t' ty' s') = do mt <- match' t t'
1284 mty <- match' ty ty'
1286 return (mt ++ mty ++ ms)
1287 match (PHidden x) (PHidden y) = match' x y
1288 match (PUnifyLog x) y = match' x y
1289 match x (PUnifyLog y) = match' x y
1290 match Placeholder _ = return []
1291 match _ Placeholder = return []
1292 match (PResolveTC _) _ = return []
1293 match a b | a == b = return []
1294 | otherwise = LeftErr (a, b)
1296 checkRpts (RightOK ms) = check ms where
1298 | Just t' <- lookup n xs = if t/=t' && t/=Placeholder && t'/=Placeholder
1301 check (_:xs) = check xs
1303 checkRpts (LeftErr x) = Left x
1305substMatches :: [(Name, PTerm)] -> PTerm -> PTerm
1306substMatches ms = substMatchesShadow ms []
1308substMatchesShadow :: [(Name, PTerm)] -> [Name] -> PTerm -> PTerm
1309substMatchesShadow [] shs t = t
1310substMatchesShadow ((n,tm):ns) shs t
1311 = substMatchShadow n shs tm (substMatchesShadow ns shs t)
1313substMatch :: Name -> PTerm -> PTerm -> PTerm
1314substMatch n = substMatchShadow n []
1316substMatchShadow :: Name -> [Name] -> PTerm -> PTerm -> PTerm
1317substMatchShadow n shs tm t = sm shs t where
1318 sm xs (PRef _ n') | n == n' = tm
1319 sm xs (PLam x t sc) = PLam x (sm xs t) (sm xs sc)
1320 sm xs (PPi p x t sc)
1322 = let x' = nextName x in
1323 PPi p x' (sm (x':xs) (substMatch x (PRef (FC "" 0) x') t))
1324 (sm (x':xs) (substMatch x (PRef (FC "" 0) x') sc))
1325 | otherwise = PPi p x (sm xs t) (sm (x : xs) sc)
1326 sm xs (PApp f x as) = PApp f (sm xs x) (map (fmap (sm xs)) as)
1327 sm xs (PCase f x as) = PCase f (sm xs x) (map (pmap (sm xs)) as)
1328 sm xs (PEq f x y) = PEq f (sm xs x) (sm xs y)
1329 sm xs (PRewrite f x y tm) = PRewrite f (sm xs x) (sm xs y)
1331 sm xs (PTyped x y) = PTyped (sm xs x) (sm xs y)
1332 sm xs (PPair f x y) = PPair f (sm xs x) (sm xs y)
1333 sm xs (PDPair f x t y) = PDPair f (sm xs x) (sm xs t) (sm xs y)
1334 sm xs (PAlternative a as) = PAlternative a (map (sm xs) as)
1335 sm xs (PHidden x) = PHidden (sm xs x)
1336 sm xs (PUnifyLog x) = PUnifyLog (sm xs x)
1339shadow :: Name -> Name -> PTerm -> PTerm
1340shadow n n' t = sm t where
1341 sm (PRef fc x) | n == x = PRef fc n'
1342 sm (PLam x t sc) = PLam x (sm t) (sm sc)
1343 sm (PPi p x t sc) = PPi p x (sm t) (sm sc)
1344 sm (PApp f x as) = PApp f (sm x) (map (fmap sm) as)
1345 sm (PCase f x as) = PCase f (sm x) (map (pmap sm) as)
1346 sm (PEq f x y) = PEq f (sm x) (sm y)
1347 sm (PRewrite f x y tm) = PRewrite f (sm x) (sm y) (fmap sm tm)
1348 sm (PTyped x y) = PTyped (sm x) (sm y)
1349 sm (PPair f x y) = PPair f (sm x) (sm y)
1350 sm (PDPair f x t y) = PDPair f (sm x) (sm t) (sm y)
1351 sm (PAlternative a as) = PAlternative a (map sm as)
1352 sm (PHidden x) = PHidden (sm x)
1353 sm (PUnifyLog x) = PUnifyLog (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