Skip to main content

litex/fact/
fact.rs

1use crate::prelude::*;
2use std::fmt;
3#[derive(Clone)]
4pub enum Fact {
5    AtomicFact(AtomicFact),
6    ExistFact(ExistFact),
7    OrFact(OrFact),
8    AndFact(AndFact),
9    ChainFact(ChainFact),
10    ForallFact(ForallFact),
11    ForallFactWithIff(ForallFactWithIff),
12}
13
14impl fmt::Debug for Fact {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        write!(f, "{}", self)
17    }
18}
19
20impl Fact {
21    pub fn fact_type_string(&self) -> String {
22        match self {
23            Fact::AtomicFact(_) => "AtomicFact".to_string(),
24            Fact::ExistFact(_) => "ExistFact".to_string(),
25            Fact::OrFact(_) => "OrFact".to_string(),
26            Fact::AndFact(_) => "AndFact".to_string(),
27            Fact::ChainFact(_) => "ChainFact".to_string(),
28            Fact::ForallFact(_) => "ForallFact".to_string(),
29            Fact::ForallFactWithIff(_) => "ForallFactWithIff".to_string(),
30        }
31    }
32}
33
34impl fmt::Display for Fact {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Fact::AtomicFact(atomic_fact) => write!(f, "{}", atomic_fact),
38            Fact::ExistFact(exist_fact) => write!(f, "{}", exist_fact),
39            Fact::OrFact(or_fact) => write!(f, "{}", or_fact),
40            Fact::AndFact(and_fact) => write!(f, "{}", and_fact),
41            Fact::ChainFact(chain_fact) => write!(f, "{}", chain_fact),
42            Fact::ForallFact(forall_fact) => write!(f, "{}", forall_fact),
43            Fact::ForallFactWithIff(forall_fact_with_iff) => write!(f, "{}", forall_fact_with_iff),
44        }
45    }
46}
47
48impl Fact {
49    pub fn line_file(&self) -> LineFile {
50        match self {
51            Fact::AtomicFact(a) => a.line_file(),
52            Fact::ExistFact(e) => e.line_file(),
53            Fact::OrFact(o) => o.line_file.clone(),
54            Fact::AndFact(a) => a.line_file(),
55            Fact::ChainFact(c) => c.line_file(),
56            Fact::ForallFact(f) => f.line_file.clone(),
57            Fact::ForallFactWithIff(f) => f.line_file.clone(),
58        }
59    }
60
61    pub fn into_stmt(self) -> Stmt {
62        self.into()
63    }
64}
65
66impl Fact {
67    // TODO 未来删了比较好,因为默认所有stmt里的东西都不能变化了
68    pub fn with_new_line_file(self, line_file: LineFile) -> Fact {
69        match self {
70            Fact::AtomicFact(atomic_fact) => {
71                Fact::AtomicFact(atomic_fact.with_new_line_file(line_file))
72            }
73            Fact::ExistFact(e) => Fact::ExistFact(ExistFact::new(
74                e.params_def_with_type,
75                e.facts
76                    .into_iter()
77                    .map(|x| x.with_new_line_file(line_file.clone()))
78                    .collect(),
79                e.is_exist_unique,
80                line_file,
81            )),
82            Fact::OrFact(or_fact) => Fact::OrFact(OrFact::new(
83                or_fact
84                    .facts
85                    .into_iter()
86                    .map(|x| x.with_new_line_file(line_file.clone()))
87                    .collect(),
88                line_file,
89            )),
90            Fact::AndFact(and_fact) => Fact::AndFact(AndFact::new(
91                and_fact
92                    .facts
93                    .into_iter()
94                    .map(|x| x.with_new_line_file(line_file.clone()))
95                    .collect(),
96                line_file,
97            )),
98            Fact::ChainFact(chain_fact) => Fact::ChainFact(ChainFact::new(
99                chain_fact.objs,
100                chain_fact.prop_names,
101                line_file,
102            )),
103            Fact::ForallFact(f) => Fact::ForallFact(ForallFact::new(
104                f.params_def_with_type,
105                f.dom_facts
106                    .into_iter()
107                    .map(|x| x.with_new_line_file(line_file.clone()))
108                    .collect(),
109                f.then_facts
110                    .into_iter()
111                    .map(|x| x.with_new_line_file(line_file.clone()))
112                    .collect(),
113                line_file,
114            )),
115            Fact::ForallFactWithIff(f) => {
116                let inner_forall = f.forall_fact;
117                Fact::ForallFactWithIff(ForallFactWithIff::new(
118                    ForallFact::new(
119                        inner_forall.params_def_with_type,
120                        inner_forall
121                            .dom_facts
122                            .into_iter()
123                            .map(|x| x.with_new_line_file(line_file.clone()))
124                            .collect(),
125                        inner_forall
126                            .then_facts
127                            .into_iter()
128                            .map(|x| x.with_new_line_file(line_file.clone()))
129                            .collect(),
130                        line_file.clone(),
131                    ),
132                    f.iff_facts
133                        .into_iter()
134                        .map(|x| x.with_new_line_file(line_file.clone()))
135                        .collect(),
136                    line_file,
137                ))
138            }
139        }
140    }
141}
142
143impl From<AtomicFact> for Fact {
144    fn from(atomic_fact: AtomicFact) -> Self {
145        Fact::AtomicFact(atomic_fact)
146    }
147}
148
149impl From<OrFact> for Fact {
150    fn from(or_fact: OrFact) -> Self {
151        Fact::OrFact(or_fact)
152    }
153}
154
155impl From<ForallFact> for Fact {
156    fn from(forall_fact: ForallFact) -> Self {
157        Fact::ForallFact(forall_fact)
158    }
159}
160
161impl From<ExistFact> for Fact {
162    fn from(exist_fact: ExistFact) -> Self {
163        Fact::ExistFact(exist_fact)
164    }
165}
166
167impl From<AndFact> for Fact {
168    fn from(and_fact: AndFact) -> Self {
169        Fact::AndFact(and_fact)
170    }
171}
172
173impl From<ChainFact> for Fact {
174    fn from(chain_fact: ChainFact) -> Self {
175        Fact::ChainFact(chain_fact)
176    }
177}
178
179impl From<AndChainAtomicFact> for Fact {
180    fn from(f: AndChainAtomicFact) -> Self {
181        match f {
182            AndChainAtomicFact::AtomicFact(a) => a.into(),
183            AndChainAtomicFact::AndFact(a) => a.into(),
184            AndChainAtomicFact::ChainFact(c) => c.into(),
185        }
186    }
187}
188
189impl From<OrAndChainAtomicFact> for Fact {
190    fn from(f: OrAndChainAtomicFact) -> Self {
191        match f {
192            OrAndChainAtomicFact::AtomicFact(a) => a.into(),
193            OrAndChainAtomicFact::AndFact(a) => a.into(),
194            OrAndChainAtomicFact::ChainFact(c) => c.into(),
195            OrAndChainAtomicFact::OrFact(o) => o.into(),
196        }
197    }
198}
199
200impl From<ForallFactWithIff> for Fact {
201    fn from(forall_fact_with_iff: ForallFactWithIff) -> Self {
202        Fact::ForallFactWithIff(forall_fact_with_iff)
203    }
204}