|
1 |
| -use formality_core::judgment_fn; |
2 |
| -use formality_types::grammar::{Predicate, Relation, Wc, WcData, Wcs}; |
3 |
| - |
4 |
| -use crate::{ |
5 |
| - decls::Decls, |
6 |
| - prove::{ |
7 |
| - env::Env, |
8 |
| - is_local::{is_local_trait_ref, may_be_remote}, |
9 |
| - prove, |
10 |
| - prove_after::prove_after, |
11 |
| - prove_eq::prove_eq, |
12 |
| - prove_via::prove_via, |
13 |
| - prove_wf::prove_wf, |
14 |
| - }, |
| 1 | +use crate::prove; |
| 2 | +use crate::prove::is_local::is_local_trait_ref; |
| 3 | +use crate::prove::is_local::may_be_remote; |
| 4 | +use crate::prove::prove_after::prove_after; |
| 5 | +use crate::prove::prove_eq::prove_eq; |
| 6 | +use crate::prove::prove_via::prove_via; |
| 7 | +use crate::prove::prove_wf::prove_wf; |
| 8 | +use crate::Decls; |
| 9 | +use crate::Env; |
| 10 | + |
| 11 | +use crate::Constraints; |
| 12 | +use formality_core::{judgment_fn, ProvenSet, Upcasted}; |
| 13 | +use formality_types::grammar::{ |
| 14 | + Const, ExhaustiveState, Parameter, Predicate, Relation, Scalar, Ty, Wc, WcData, Wcs, |
15 | 15 | };
|
16 | 16 |
|
17 |
| -use super::constraints::Constraints; |
| 17 | +pub fn is_covering(vals: &[ExhaustiveState], params: &[Parameter]) -> Wcs { |
| 18 | + assert_eq!(vals.len(), params.len()); |
| 19 | + vals.iter() |
| 20 | + .zip(params.iter()) |
| 21 | + .filter_map(|(a, b)| match a { |
| 22 | + ExhaustiveState::ExactMatch => None, |
| 23 | + ExhaustiveState::ConstCover(cs) => { |
| 24 | + let Parameter::Const(c) = b else { |
| 25 | + todo!(); |
| 26 | + }; |
| 27 | + Some(Predicate::Covers(cs.clone(), c.clone())) |
| 28 | + } |
| 29 | + }) |
| 30 | + .upcasted() |
| 31 | + .collect() |
| 32 | +} |
18 | 33 |
|
19 | 34 | judgment_fn! {
|
20 | 35 | pub fn prove_wc(
|
@@ -46,6 +61,34 @@ judgment_fn! {
|
46 | 61 | (prove_wc(decls, env, assumptions, WcData::PR(goal)) => c)
|
47 | 62 | )
|
48 | 63 |
|
| 64 | + ( |
| 65 | + (let mut covering_consts = vec![ExhaustiveState::ExactMatch; trait_ref.parameters.len()]) |
| 66 | + (let asmp = &assumptions) |
| 67 | + (let d = &decls) |
| 68 | + (d.impl_decls(&trait_ref.trait_id).flat_map(|i| { |
| 69 | + |
| 70 | + let (env, subst) = env.clone().universal_substitution(&i.binder); |
| 71 | + let i = i.binder.instantiate_with(&subst).unwrap(); |
| 72 | + let co_assumptions = (asmp, &trait_ref); |
| 73 | + let cs = prove( |
| 74 | + &decls, env, &co_assumptions, |
| 75 | + Wcs::eq_or_cover( |
| 76 | + &i.trait_ref.parameters, &trait_ref.parameters, &mut covering_consts |
| 77 | + ) |
| 78 | + ); |
| 79 | + let cs = cs.flat_map(move |c| prove_after(d, c, &co_assumptions, &i.where_clause)); |
| 80 | + let cs = cs.flat_map(move |c| { |
| 81 | + let t = d.trait_decl(&i.trait_ref.trait_id) |
| 82 | + .binder.instantiate_with(&i.trait_ref.parameters).unwrap(); |
| 83 | + prove_after(d, c, asmp, &t.where_clause) |
| 84 | + }); |
| 85 | + cs.into_iter() |
| 86 | + }).into_iter().collect::<ProvenSet<_>>() => c) |
| 87 | + (prove_after(d, c, asmp, is_covering(&covering_consts, &trait_ref.parameters)) => c) |
| 88 | + ----------------------------- ("exhaustive positive impl") |
| 89 | + (prove_wc(decls, env, assumptions, Predicate::IsImplemented(trait_ref)) => c) |
| 90 | + ) |
| 91 | + |
49 | 92 | (
|
50 | 93 | (decls.impl_decls(&trait_ref.trait_id) => i)!
|
51 | 94 | (let (env, subst) = env.existential_substitution(&i.binder))
|
@@ -112,6 +155,23 @@ judgment_fn! {
|
112 | 155 | (prove_wc(decls, env, assumptions, Predicate::IsLocal(trait_ref)) => c)
|
113 | 156 | )
|
114 | 157 |
|
| 158 | + ( |
| 159 | + (let () = vals.sort_unstable()) |
| 160 | + (prove(&decls, env, &assumptions, Predicate::ConstHasType(var, Ty::bool())) => c) |
| 161 | + (vals.iter().cloned() => v) |
| 162 | + (prove_after(&decls, &c, &assumptions, Predicate::ConstHasType(v, Ty::bool())) => c) |
| 163 | + (if vals.len() == 2) |
| 164 | + (vals.clone().into_iter().enumerate().flat_map(|(i, v)| { |
| 165 | + prove_after( |
| 166 | + &decls, &c, &assumptions, |
| 167 | + Relation::Equals(Parameter::Const(Const::valtree(Scalar::new(i as u128), |
| 168 | + Ty::bool())), Parameter::Const(v)) |
| 169 | + ).into_iter() |
| 170 | + }).collect::<ProvenSet<_>>() => c) |
| 171 | + ----------------------------- ("exhaustive bool values cover variable") |
| 172 | + (prove_wc(decls, env, assumptions, Predicate::Covers(mut vals, var)) => c) |
| 173 | + ) |
| 174 | + |
115 | 175 |
|
116 | 176 | (
|
117 | 177 | (prove_wf(decls, env, assumptions, p) => c)
|
|
0 commit comments