on master help sign in
Merge branch 'ozgurakgun-improve-universe-solver'
commit1bd70a10cc11b44bcb0ae7c624f3c67594837695 authorEdwin Brady <ecb10@st-andrews.ac.uk> dateSun 22 Mar 2015, 13:19 (12 years ago) parentd57d60acef8780
5 files changed+127−41
src/Idris/Core/Constraints.hs+70−20 src/Idris/Core/Typecheck.hs+35−7 src/Idris/Elab/Data.hs+13−4 test/error001/expected+7−8 test/quasiquote001/expected+2−2
src/Idris/Core/Constraints.hs+70−20
@@ -62,14 +62,34 @@ solve maxUniverseLevel ucs =
6262 ]
6363 }
6464
65− -- | a map from variables to the list of constraints the variable occurs in.
66− constraints :: M.Map Var (S.Set ConstraintFC)
67− constraints = M.fromListWith S.union
65+ lhs (ULT (UVar x) _) = Just (Var x)
66+ lhs (ULE (UVar x) _) = Just (Var x)
67+ lhs _ = Nothing
68+
69+ rhs (ULT _ (UVar x)) = Just (Var x)
70+ rhs (ULE _ (UVar x)) = Just (Var x)
71+ rhs _ = Nothing
72+
73+ -- | a map from variables to the list of constraints the variable occurs in. (in the LHS of a constraint)
74+ constraintsLHS :: M.Map Var (S.Set ConstraintFC)
75+ constraintsLHS = M.fromListWith S.union
6876 [ (v, S.singleton (ConstraintFC c fc))
69− | ConstraintFC c fc <- inpConstraints
77+ | (ConstraintFC c fc) <- inpConstraints
7078 , let vars = varsIn c
7179 , length vars > 1 -- do not register unary constraints
7280 , v <- vars
81+ , lhs c == Just v
82+ ]
83+
84+ -- | a map from variables to the list of constraints the variable occurs in. (in the RHS of a constraint)
85+ constraintsRHS :: M.Map Var (S.Set ConstraintFC)
86+ constraintsRHS = M.fromListWith S.union
87+ [ (v, S.singleton (ConstraintFC c fc))
88+ | (ConstraintFC c fc) <- inpConstraints
89+ , let vars = varsIn c
90+ , length vars > 1 -- do not register unary constraints
91+ , v <- vars
92+ , rhs c == Just v
7393 ]
7494
7595 -- | this is where the actual work is done.
@@ -87,15 +107,15 @@ solve maxUniverseLevel ucs =
87107 ULE a b -> do
88108 Domain lowerA upperA <- domainOf a
89109 Domain lowerB upperB <- domainOf b
90− when (upperB < upperA) $ updateDomainOf (ConstraintFC cons fc) a (Domain lowerA upperB)
91− when (lowerA > lowerB) $ updateDomainOf (ConstraintFC cons fc) b (Domain lowerA upperB)
110+ when (upperB < upperA) $ updateUpperBoundOf (ConstraintFC cons fc) a upperB
111+ when (lowerA > lowerB) $ updateLowerBoundOf (ConstraintFC cons fc) b lowerA
92112 ULT a b -> do
93113 Domain lowerA upperA <- domainOf a
94114 Domain lowerB upperB <- domainOf b
95115 let upperB_pred = pred upperB
96116 let lowerA_succ = succ lowerA
97− when (upperB_pred < upperA) $ updateDomainOf (ConstraintFC cons fc) a (Domain lowerA upperB_pred)
98− when (lowerA_succ > lowerB) $ updateDomainOf (ConstraintFC cons fc) b (Domain lowerA_succ upperB)
117+ when (upperB_pred < upperA) $ updateUpperBoundOf (ConstraintFC cons fc) a upperB_pred
118+ when (lowerA_succ > lowerB) $ updateLowerBoundOf (ConstraintFC cons fc) b lowerA_succ
99119 propagate
100120
101121 -- | extract a solution from the state.
@@ -121,12 +141,11 @@ solve maxUniverseLevel ucs =
121141 domainOf (UVar var) = gets (fst . (M.! Var var) . domainStore)
122142 domainOf (UVal val) = return (Domain val val)
123143
124− -- | updates the domain of a variable.
125− -- this function is also where we fail, inc ase of a domain wipe-out.
126− updateDomainOf :: ConstraintFC -> UExp -> Domain -> StateT SolverState TC ()
127− updateDomainOf suspect (UVar var) newDom = do
144+ updateUpperBoundOf :: ConstraintFC -> UExp -> Int -> StateT SolverState TC ()
145+ updateUpperBoundOf suspect (UVar var) upper = do
128146 doms <- gets domainStore
129− let (oldDom, suspects) = doms M.! Var var
147+ let (oldDom@(Domain lower _), suspects) = doms M.! Var var
148+ let newDom = Domain lower upper
130149 when (wipeOut newDom) $ lift $ Error $ At (ufc suspect) $ Msg $ unlines
131150 $ "Universe inconsistency."
132151 : ("Working on: " ++ show (UVar var))
@@ -135,17 +154,48 @@ solve maxUniverseLevel ucs =
135154 : "Involved constraints: "
136155 : map (("\t"++) . show) (suspect : S.toList suspects)
137156 modify $ \ st -> st { domainStore = M.insert (Var var) (newDom, S.insert suspect suspects) doms }
138− addToQueue (Var var)
139− updateDomainOf _ UVal{} _ = return ()
157+ addToQueueRHS (uconstraint suspect) (Var var)
158+ updateUpperBoundOf _ UVal{} _ = return ()
159+
160+ updateLowerBoundOf :: ConstraintFC -> UExp -> Int -> StateT SolverState TC ()
161+ updateLowerBoundOf suspect (UVar var) lower = do
162+ doms <- gets domainStore
163+ let (oldDom@(Domain _ upper), suspects) = doms M.! Var var
164+ let newDom = Domain lower upper
165+ when (wipeOut newDom) $ lift $ Error $ At (ufc suspect) $ Msg $ unlines
166+ $ "Universe inconsistency."
167+ : ("Working on: " ++ show (UVar var))
168+ : ("Old domain: " ++ show oldDom)
169+ : ("New domain: " ++ show newDom)
170+ : "Involved constraints: "
171+ : map (("\t"++) . show) (suspect : S.toList suspects)
172+ modify $ \ st -> st { domainStore = M.insert (Var var) (newDom, S.insert suspect suspects) doms }
173+ addToQueueLHS (uconstraint suspect) (Var var)
174+ updateLowerBoundOf _ UVal{} _ = return ()
175+
176+ -- | add all constraints (with the given var on the lhs) to the queue
177+ addToQueueLHS :: MonadState SolverState m => UConstraint -> Var -> m ()
178+ addToQueueLHS thisCons var =
179+ case M.lookup var constraintsLHS of
180+ Nothing -> return ()
181+ Just cs -> do
182+ Queue list set <- gets queue
183+ let set' = S.insert thisCons set
184+ let newCons = [ c | c <- S.toList cs, uconstraint c `S.notMember` set' ]
185+ if null newCons
186+ then return ()
187+ else modify $ \ st -> st { queue = Queue (list ++ newCons)
188+ (S.union set (S.fromList (map uconstraint newCons))) }
140189
141− -- | add all constraints related to a variable.
142− addToQueue :: MonadState SolverState m => Var -> m ()
143− addToQueue var =
144− case M.lookup var constraints of
190+ -- | add all constraints (with the given var on the rhs) to the queue
191+ addToQueueRHS :: MonadState SolverState m => UConstraint -> Var -> m ()
192+ addToQueueRHS thisCons var =
193+ case M.lookup var constraintsRHS of
145194 Nothing -> return ()
146195 Just cs -> do
147196 Queue list set <- gets queue
148− let newCons = [ c | c <- S.toList cs, uconstraint c `S.notMember` set ]
197+ let set' = S.insert thisCons set
198+ let newCons = [ c | c <- S.toList cs, uconstraint c `S.notMember` set' ]
149199 if null newCons
150200 then return ()
151201 else modify $ \ st -> st { queue = Queue (list ++ newCons)
src/Idris/Core/Typecheck.hs+35−7
@@ -86,15 +86,38 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
8686 | (P nt n' ty : _) <- lookupP_all (not holes) n ctxt
8787 = return (P nt n' ty, ty)
8888 | otherwise = do lift $ tfail $ NoSuchVariable n
89+ chk u env ap@(RApp f RType) | not holes
90+ -- special case to reduce constraintss
91+ = do (fv, fty) <- chk u env f
92+ let fty' = case uniqueBinders (map fst env) (finalise fty) of
93+ ty@(Bind x (Pi i s k) t) -> ty
94+ _ -> uniqueBinders (map fst env)
95+ $ case hnf ctxt env fty of
96+ ty@(Bind x (Pi i s k) t) -> ty
97+ _ -> normalise ctxt env fty
98+ case fty' of
99+ Bind x (Pi i (TType v') k) t ->
100+ do (v, cs) <- get
101+ put (v+1, ULT (UVar v) v' : cs)
102+ let apty = simplify initContext env
103+ (Bind x (Let (TType v') (TType (UVar v))) t)
104+ return (App fv (TType (UVar v)), apty)
105+ Bind x (Pi i s k) t ->
106+ do (av, aty) <- chk u env RType
107+ convertsC ctxt env aty s
108+ let apty = simplify initContext env
109+ (Bind x (Let aty av) t)
110+ return (App fv av, apty)
111+ t -> lift $ tfail $ NonFunctionType fv fty
89112 chk u env ap@(RApp f a)
90113 = do (fv, fty) <- chk u env f
91− (av, aty) <- chk u env a
92114 let fty' = case uniqueBinders (map fst env) (finalise fty) of
93115 ty@(Bind x (Pi i s k) t) -> ty
94116 _ -> uniqueBinders (map fst env)
95117 $ case hnf ctxt env fty of
96118 ty@(Bind x (Pi i s k) t) -> ty
97119 _ -> normalise ctxt env fty
120+ (av, aty) <- chk u env a
98121 case fty' of
99122 Bind x (Pi i s k) t ->
100123 do convertsC ctxt env aty s
@@ -135,17 +158,22 @@ check' holes ctxt env top = chk (TType (UVar (-5))) env top where
135158 constType TheWorld = Constant WorldType
136159 constType Forgot = Erased
137160 constType _ = TType (UVal 0)
138− chk u env (RForce t) = do (_, ty) <- chk u env t
139− return (Erased, ty)
161+ chk u env (RForce t)
162+ = do (_, ty) <- chk u env t
163+ return (Erased, ty)
140164 chk u env (RBind n (Pi i s k) t)
141165 = do (sv, st) <- chk u env s
142− (kv, kt) <- chk u env k
143− (tv, tt) <- chk st ((n, Pi i sv kv) : env) t
144166 (v, cs) <- get
167+ (kv, kt) <- chk u env k -- no need to validate these constraints, they are independent
168+ put (v+1, cs)
169+ let maxu = UVar v
170+ (tv, tt) <- chk st ((n, Pi i sv kv) : env) t
145171 case (normalise ctxt env st, normalise ctxt env tt) of
146172 (TType su, TType tu) -> do
147− when (not holes) $ put (v+1, ULE su (UVar v):ULE tu (UVar v):cs)
148− let k' = st `smaller` kv `smaller` TType (UVar v) `smaller` u
173+ when (not holes) $ do (v, cs) <- get
174+ put (v, ULE su maxu :
175+ ULE tu maxu : cs)
176+ let k' = TType (UVar v) `smaller` st `smaller` kv `smaller` u
149177 return (Bind n (Pi i (uniqueBinders (map fst env) sv) k')
150178 (pToV n tv), k')
151179 (un, un') ->
src/Idris/Elab/Data.hs+13−4
@@ -64,7 +64,7 @@ elabData info syn doc argDocs fc opts (PDatadecl n t_in dcons)
6464 = do let codata = Codata `elem` opts
6565 iLOG (show fc)
6666 undef <- isUndefined fc n
67− (cty, _, t, inacc) <- buildType info syn fc [] n t_in
67+ (cty, ckind, t, inacc) <- buildType info syn fc [] n t_in
6868 -- if n is defined already, make sure it is just a type declaration
6969 -- with the same type we've just elaborated, and no constructors
7070 -- yet
@@ -76,7 +76,7 @@ elabData info syn doc argDocs fc opts (PDatadecl n t_in dcons)
7676 let unique = case getRetTy cty of
7777 UType UniqueType -> True
7878 _ -> False
79− cons <- mapM (elabCon cnameinfo syn n codata (getRetTy cty)) dcons
79+ cons <- mapM (elabCon cnameinfo syn n codata (getRetTy cty) ckind) dcons
8080 ttag <- getName
8181 i <- getIState
8282 let as = map (const (Left (Msg ""))) (getArgTys cty)
@@ -210,16 +210,18 @@ elabData info syn doc argDocs fc opts (PDatadecl n t_in dcons)
210210 }
211211
212212elabCon :: ElabInfo -> SyntaxInfo -> Name -> Bool ->
213− Type -> -- for kind checking
213+ Type -> -- for unique kind checking
214+ Type -> -- data type's kind
214215 (Docstring (Either Err PTerm), [(Name, Docstring (Either Err PTerm))], Name, PTerm, FC, [Name]) ->
215216 Idris (Name, Type)
216−elabCon info syn tn codata expkind (doc, argDocs, n, t_in, fc, forcenames)
217+elabCon info syn tn codata expkind dkind (doc, argDocs, n, t_in, fc, forcenames)
217218 = do checkUndefined fc n
218219 logLvl 2 $ show fc ++ ":Constructor " ++ show n ++ " : " ++ show t_in
219220 (cty, ckind, t, inacc) <- buildType info syn fc [Constructor] n (if codata then mkLazy t_in else t_in)
220221 ctxt <- getContext
221222 let cty' = normalise ctxt [] cty
222223 checkUniqueKind ckind expkind
224+ addDataConstraint ckind dkind
223225
224226 -- Check that the constructor type is, in fact, a part of the family being defined
225227 tyIs n cty'
@@ -280,6 +282,13 @@ elabCon info syn tn codata expkind (doc, argDocs, n, t_in, fc, forcenames)
280282 checkUniqueKind (UType AllTypes) _ = return ()
281283 checkUniqueKind _ _ = return ()
282284
285+ -- Constructor's kind must be <= expected kind
286+ addDataConstraint (TType con) (TType exp)
287+ = do ctxt <- getContext
288+ let v = next_tvar ctxt
289+ addConstraints fc (v, [ULT con exp])
290+ addDataConstraint _ _ = return ()
291+
283292type EliminatorState = StateT (Map.Map String Int) Idris
284293
285294-- -- Issue #1616 in the issue tracker.
test/error001/expected+7−8
@@ -1,10 +1,9 @@
1−test002.idr:5:6:Universe inconsistency.
2−Working on: f2
3−Old domain: Domain 7 7
4−New domain: Domain 7 6
1+test002.idr:1:6:Universe inconsistency.
2+Working on: z
3+Old domain: Domain 6 6
4+New domain: Domain 6 5
55Involved constraints:
6− (f2 <= i1,test002.idr:5:6)
7− (e2 < f2,test002.idr:5:6)
8− (j1 <= f2,test002.idr:5:6)
9− (f2 <= i1,test002.idr:5:6)
6+ ConstraintFC {uconstraint = z <= a1, ufc = test002.idr:1:6}
7+ ConstraintFC {uconstraint = y < z, ufc = test002.idr:1:6}
8+ ConstraintFC {uconstraint = z <= a1, ufc = test002.idr:1:6}
109
test/quasiquote001/expected+2−2
@@ -1,5 +1,5 @@
11(App (App (App (P (DCon 1 3) (NS (UN ::) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (Bind (MN 0 "_t") (Pi (V 0)) (Bind (MN 2 "_t") (Pi (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 2)))))) (P (TCon 8 0) (UN Unit) (TType (UVar -1)))) (P (DCon 0 0) (UN MkUnit) (P (TCon 0 0) (UN Unit) Erased))) (App (App (App (P (DCon 1 3) (NS (UN ::) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (Bind (MN 0 "_t") (Pi (V 0)) (Bind (MN 2 "_t") (Pi (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 2)))))) (P (TCon 8 0) (UN Unit) (TType (UVar -1)))) (P (DCon 0 0) (UN MkUnit) (P (TCon 0 0) (UN Unit) Erased))) (App (P (DCon 0 1) (NS (UN Nil) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 0)))) (P (TCon 8 0) (UN Unit) (TType (UVar -1))))))
22--------------
3−(App (App (App (P (DCon 1 3) (NS (UN ::) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (Bind (MN 0 "_t") (Pi (V 0)) (Bind (MN 2 "_t") (Pi (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 2)))))) (TType (UVar 32))) (TType (UVar 34))) (App (App (App (P (DCon 1 3) (NS (UN ::) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (Bind (MN 0 "_t") (Pi (V 0)) (Bind (MN 2 "_t") (Pi (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 2)))))) (TType (UVar 36))) (App (App (App (App (P (DCon 0 4) (NS (UN MkPair) ["Builtins"]) (Bind (UN A) (Pi (TType (UVar -1))) (Bind (UN B) (Pi (TType (UVar -1))) (Bind (UN a) (Pi (V 1)) (Bind (UN b) (Pi (V 1)) (App (App (P (TCon 0 0) (NS (UN Pair) ["Builtins"]) Erased) (V 3)) (V 2))))))) (TType (UVar 30))) (TType (UVar 32))) (P (TCon 8 0) (NS (UN Nat) ["Nat", "Prelude"]) (TType (UVar -1)))) (P (TCon 8 0) (NS (UN Nat) ["Nat", "Prelude"]) (TType (UVar -1))))) (App (P (DCon 0 1) (NS (UN Nil) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 0)))) (TType (UVar 38)))))
3+(App (App (App (P (DCon 1 3) (NS (UN ::) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (Bind (MN 0 "_t") (Pi (V 0)) (Bind (MN 2 "_t") (Pi (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 2)))))) (TType (UVar 24))) (TType (UVar 25))) (App (App (App (P (DCon 1 3) (NS (UN ::) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (Bind (MN 0 "_t") (Pi (V 0)) (Bind (MN 2 "_t") (Pi (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 2)))))) (TType (UVar 26))) (App (App (App (App (P (DCon 0 4) (NS (UN MkPair) ["Builtins"]) (Bind (UN A) (Pi (TType (UVar -1))) (Bind (UN B) (Pi (TType (UVar -1))) (Bind (UN a) (Pi (V 1)) (Bind (UN b) (Pi (V 1)) (App (App (P (TCon 0 0) (NS (UN Pair) ["Builtins"]) Erased) (V 3)) (V 2))))))) (TType (UVar 22))) (TType (UVar 23))) (P (TCon 8 0) (NS (UN Nat) ["Nat", "Prelude"]) (TType (UVar -1)))) (P (TCon 8 0) (NS (UN Nat) ["Nat", "Prelude"]) (TType (UVar -1))))) (App (P (DCon 0 1) (NS (UN Nil) ["List", "Prelude"]) (Bind (UN a) (Pi (TType (UVar -1))) (App (P (TCon 0 0) (NS (UN List) ["List", "Prelude"]) Erased) (V 0)))) (TType (UVar 27)))))
44--------------
5−(App (App (App (App (P (DCon 0 4) (NS (UN MkPair) ["Builtins"]) (Bind (UN A) (Pi (TType (UVar -1))) (Bind (UN B) (Pi (TType (UVar -1))) (Bind (UN a) (Pi (V 1)) (Bind (UN b) (Pi (V 1)) (App (App (P (TCon 0 0) (NS (UN Pair) ["Builtins"]) Erased) (V 3)) (V 2))))))) (TType (UVar 30))) (TType (UVar 32))) (App (App (App (App (P (DCon 0 4) (NS (UN MkPair) ["Builtins"]) (Bind (UN A) (Pi (TType (UVar -1))) (Bind (UN B) (Pi (TType (UVar -1))) (Bind (UN a) (Pi (V 1)) (Bind (UN b) (Pi (V 1)) (App (App (P (TCon 0 0) (NS (UN Pair) ["Builtins"]) Erased) (V 3)) (V 2))))))) (TType (UVar 30))) (TType (UVar 32))) (TType (UVar 32))) (TType (UVar 32)))) (App (App (App (App (P (DCon 0 4) (NS (UN MkPair) ["Builtins"]) (Bind (UN A) (Pi (TType (UVar -1))) (Bind (UN B) (Pi (TType (UVar -1))) (Bind (UN a) (Pi (V 1)) (Bind (UN b) (Pi (V 1)) (App (App (P (TCon 0 0) (NS (UN Pair) ["Builtins"]) Erased) (V 3)) (V 2))))))) (TType (UVar 30))) (TType (UVar 32))) (TType (UVar 32))) (TType (UVar 32))))
5+(App (App (App (App (P (DCon 0 4) (NS (UN MkPair) ["Builtins"]) (Bind (UN A) (Pi (TType (UVar -1))) (Bind (UN B) (Pi (TType (UVar -1))) (Bind (UN a) (Pi (V 1)) (Bind (UN b) (Pi (V 1)) (App (App (P (TCon 0 0) (NS (UN Pair) ["Builtins"]) Erased) (V 3)) (V 2))))))) (TType (UVar 22))) (TType (UVar 23))) (App (App (App (App (P (DCon 0 4) (NS (UN MkPair) ["Builtins"]) (Bind (UN A) (Pi (TType (UVar -1))) (Bind (UN B) (Pi (TType (UVar -1))) (Bind (UN a) (Pi (V 1)) (Bind (UN b) (Pi (V 1)) (App (App (P (TCon 0 0) (NS (UN Pair) ["Builtins"]) Erased) (V 3)) (V 2))))))) (TType (UVar 22))) (TType (UVar 23))) (TType (UVar 24))) (TType (UVar 24)))) (App (App (App (App (P (DCon 0 4) (NS (UN MkPair) ["Builtins"]) (Bind (UN A) (Pi (TType (UVar -1))) (Bind (UN B) (Pi (TType (UVar -1))) (Bind (UN a) (Pi (V 1)) (Bind (UN b) (Pi (V 1)) (App (App (P (TCon 0 0) (NS (UN Pair) ["Builtins"]) Erased) (V 3)) (V 2))))))) (TType (UVar 22))) (TType (UVar 23))) (TType (UVar 24))) (TType (UVar 24))))