on 5570cee help sign in
Idris-dev/src/Core/Unify.hs
137 lines 5.4 kB history raw
1module Core.Unify(unify, Fails) where
3import Core.TT
4import Core.Evaluate
6import Control.Monad
7import Control.Monad.State
8import Debug.Trace
10-- Unification is applied inside the theorem prover. We're looking for holes
11-- which can be filled in, by matching one term's normal form against another.
12-- Returns a list of hole names paired with the term which solves them, and
13-- a list of things which need to be injective.
15-- terms which need to be injective, with the things we're trying to unify
16-- at the time
18type Injs = [(TT Name, TT Name, TT Name)]
19type Fails = [(TT Name, TT Name, Env, Err)]
21data UInfo = UI Int Injs Fails
23unify :: Context -> Env -> TT Name -> TT Name -> TC ([(Name, TT Name)],
24 Injs, Fails)
25unify ctxt env topx topy
26 = case runStateT
27 (un' False [] (normalise ctxt env topx) (normalise ctxt env topy))
28 (UI 0 [] []) of
29 OK (v, UI _ inj fails) -> return (filter notTrivial v, inj, reverse fails)
30-- OK (_, UI s _ ((_,_,f):fs)) -> tfail $ CantUnify topx topy f s
31 Error e -> tfail e
32 where
33 notTrivial (x, P _ x' _) = x /= x'
34 notTrivial _ = True
36 injective (P (DCon _ _) _ _) = True
37 injective (P (TCon _ _) _ _) = True
38 injective (App f a) = injective f
39 injective _ = False
41 notP (P _ _ _) = False
42 notP _ = True
44 sc i = do UI s x f <- get
45 put (UI (s+i) x f)
47 uplus u1 u2 = do UI s i f <- get
48 r <- u1
49 UI s _ f' <- get
50 if (length f == length f')
51 then return r
52 else do put (UI s i f); u2
54 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
55 StateT UInfo
56 TC [(Name, TT Name)]
57 un' fn bnames (P Bound x _) (P Bound y _)
58 | (x,y) `elem` bnames = do sc 1; return []
59 un' fn bnames (P Bound x _) tm
60 | holeIn env x = do UI s i f <- get
61 when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i) f)
62 sc 1
63 return [(x, tm)]
64 un' fn bnames tm (P Bound y _)
65 | holeIn env y = do UI s i f <- get
66 when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i) f)
67 sc 1
68 return [(y, tm)]
69 un' fn bnames (V i) (P Bound x _)
70 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
71 un' fn bnames (P Bound x _) (V i)
72 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
74 un' fn bnames (App fx ax) (App fy ay)
75 = do uplus -- do the second one if the first adds any errors
76 (do hf <- un' True bnames fx fy
77 let ax' = normalise ctxt env (substNames hf ax)
78 let ay' = normalise ctxt env (substNames hf ay)
79 ha <- un' False bnames ax' ay'
80 sc 1
81 combine bnames hf ha)
82 (do ha <- un' False bnames ax ay
83 let fx' = normalise ctxt env (substNames ha fx)
84 let fy' = normalise ctxt env (substNames ha fy)
85 hf <- un' False bnames fx' fy'
86 sc 1
87 combine bnames hf ha)
89 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
90 | n == n' = un' False bnames x y
91 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
92 | n == n' = un' False bnames x y
93 un' fn bnames (Bind x bx sx) (Bind y by sy)
94 = do h1 <- uB bnames bx by
95 h2 <- un' False ((x,y):bnames) sx sy
96 combine bnames h1 h2
97 un' fn bnames x y
98 | x == y = do sc 1; return []
99 | otherwise = do UI s i f <- get
100 let err = CantUnify topx topy (CantUnify x y (Msg "") s) s
101 put (UI s i ((x, y, env, err) : f))
102 return [] -- lift $ tfail err
104 uB bnames (Let tx vx) (Let ty vy)
105 = do h1 <- un' False bnames tx ty
106 h2 <- un' False bnames ty vy
107 sc 1
108 combine bnames h1 h2
109 uB bnames (Guess tx vx) (Guess ty vy)
110 = do h1 <- un' False bnames tx ty
111 h2 <- un' False bnames ty vy
112 sc 1
113 combine bnames h1 h2
114 uB bnames (Lam tx) (Lam ty) = do sc 1; un' False bnames tx ty
115 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
116 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
117 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
118 uB bnames x y = do UI s i f <- get
119 let err = CantUnify topx topy
120 (CantUnify (binderTy x) (binderTy y) (Msg "") s)
121 s
122 put (UI s i ((binderTy x, binderTy y, env, err) : f))
123 return [] -- lift $ tfail err
125 combine bnames as [] = return as
126 combine bnames as ((n, t) : bs)
127 = case lookup n as of
128 Nothing -> combine bnames (as ++ [(n,t)]) bs
129 Just t' -> do un' False bnames t t'
130 sc 1
131 combine bnames as bs
133holeIn :: Env -> Name -> Bool
134holeIn env n = case lookup n env of
135 Just (Hole _) -> True
136 _ -> False