Skip to main content

f1r3fly_models/rust/
utils.rs

1use expr::ExprInstance;
2use serde::{Deserialize, Serialize};
3use std::collections::BTreeMap;
4
5use super::par_map::ParMap;
6use super::par_map_type_mapper::ParMapTypeMapper;
7use super::par_set::ParSet;
8use super::par_set_type_mapper::ParSetTypeMapper;
9use super::rholang::implicits::vector_par;
10use crate::rhoapi::g_unforgeable::UnfInstance;
11use crate::rust::utils::connective::ConnectiveInstance::*;
12use crate::rust::utils::expr::ExprInstance::EVarBody;
13use crate::rust::utils::expr::ExprInstance::*;
14use crate::rust::utils::var::VarInstance::{BoundVar, FreeVar, Wildcard};
15use crate::rust::utils::var::WildcardMsg;
16use crate::{create_bit_vector, rhoapi::*};
17
18#[derive(Serialize, Deserialize, Debug, Clone)]
19pub struct OptionResult<A, K> {
20    pub continuation: K,
21    pub data: A,
22}
23
24// Adding helper functions 'with_*' to protobuf message 'Par'
25impl Par {
26    pub fn with_sends(&self, new_sends: Vec<Send>) -> Par {
27        Par {
28            sends: new_sends,
29            ..self.clone()
30        }
31    }
32
33    pub fn with_receives(&self, new_receives: Vec<Receive>) -> Par {
34        Par {
35            receives: new_receives,
36            ..self.clone()
37        }
38    }
39
40    pub fn with_news(&self, new_news: Vec<New>) -> Par {
41        Par {
42            news: new_news,
43            ..self.clone()
44        }
45    }
46
47    pub fn with_exprs(&self, new_exprs: Vec<Expr>) -> Par {
48        Par {
49            exprs: new_exprs,
50            ..self.clone()
51        }
52    }
53
54    pub fn with_matches(&self, new_matches: Vec<Match>) -> Par {
55        Par {
56            matches: new_matches,
57            ..self.clone()
58        }
59    }
60
61    pub fn with_bundles(&self, new_bundles: Vec<Bundle>) -> Par {
62        Par {
63            bundles: new_bundles,
64            ..self.clone()
65        }
66    }
67
68    pub fn with_unforgeables(&self, new_unforgeables: Vec<GUnforgeable>) -> Par {
69        Par {
70            unforgeables: new_unforgeables,
71            ..self.clone()
72        }
73    }
74
75    pub fn with_connectives(&self, new_connectives: Vec<Connective>) -> Par {
76        Par {
77            connectives: new_connectives,
78            ..self.clone()
79        }
80    }
81
82    pub fn with_locally_free(&self, new_locally_free: Vec<u8>) -> Par {
83        Par {
84            locally_free: new_locally_free,
85            ..self.clone()
86        }
87    }
88
89    pub fn with_connective_used(&self, new_connective_used: bool) -> Par {
90        Par {
91            connective_used: new_connective_used,
92            ..self.clone()
93        }
94    }
95
96    // See models/src/main/scala/coop/rchain/models/rholang/implicits.scala - prepend
97    pub fn prepend_send(&mut self, s: Send) -> Par {
98        let mut new_sends = vec![s.clone()];
99        new_sends.append(&mut self.sends);
100
101        Par {
102            sends: new_sends,
103            locally_free: union(self.locally_free.clone(), s.locally_free),
104            connective_used: self.connective_used || s.connective_used,
105            ..self.clone()
106        }
107    }
108
109    pub fn prepend_receive(&mut self, r: Receive) -> Par {
110        let mut new_receives = vec![r.clone()];
111        new_receives.append(&mut self.receives);
112
113        Par {
114            receives: new_receives,
115            locally_free: union(self.locally_free.clone(), r.locally_free),
116            connective_used: self.connective_used || r.connective_used,
117            ..self.clone()
118        }
119    }
120
121    pub fn prepend_match(&mut self, m: Match) -> Par {
122        let mut new_matches = vec![m.clone()];
123        new_matches.append(&mut self.matches);
124
125        Par {
126            matches: new_matches,
127            locally_free: union(self.locally_free.clone(), m.locally_free),
128            connective_used: self.connective_used || m.connective_used,
129            ..self.clone()
130        }
131    }
132
133    pub fn is_empty(&self) -> bool {
134        self.sends.is_empty()
135            && self.receives.is_empty()
136            && self.news.is_empty()
137            && self.matches.is_empty()
138            && self.bundles.is_empty()
139            && self.exprs.is_empty()
140    }
141
142    pub fn single_connective(&self) -> Option<Connective> {
143        if self.sends.is_empty()
144            && self.receives.is_empty()
145            && self.news.is_empty()
146            && self.exprs.is_empty()
147            && self.matches.is_empty()
148            && self.bundles.is_empty()
149            && self.connectives.len() == 1
150        {
151            Some(self.connectives[0].clone())
152        } else {
153            None
154        }
155    }
156
157    pub fn single_bundle(&self) -> Option<Bundle> {
158        if self.sends.is_empty()
159            && self.receives.is_empty()
160            && self.news.is_empty()
161            && self.exprs.is_empty()
162            && self.matches.is_empty()
163            && self.unforgeables.is_empty()
164            && self.connectives.is_empty()
165        {
166            match self.bundles.as_slice() {
167                [single] => Some(single.clone()),
168                _ => None,
169            }
170        } else {
171            None
172        }
173    }
174
175    pub fn append(&self, other: Par) -> Par {
176        Par {
177            sends: [self.sends.clone(), other.sends].concat(),
178            receives: [self.receives.clone(), other.receives].concat(),
179            news: [self.news.clone(), other.news].concat(),
180            exprs: [self.exprs.clone(), other.exprs].concat(),
181            matches: [self.matches.clone(), other.matches].concat(),
182            unforgeables: [self.unforgeables.clone(), other.unforgeables].concat(),
183            bundles: [self.bundles.clone(), other.bundles].concat(),
184            connectives: [self.connectives.clone(), other.connectives].concat(),
185            locally_free: union(self.locally_free.clone(), other.locally_free),
186            connective_used: self.connective_used || other.connective_used,
187        }
188    }
189}
190
191// See rholang/src/main/scala/coop/rchain/rholang/interpreter/matcher/package.scala - FreeMap
192pub type FreeMap = BTreeMap<i32, Par>;
193pub fn new_free_map() -> FreeMap {
194    BTreeMap::new()
195}
196
197// See rholang/src/main/scala/coop/rchain/rholang/interpreter/matcher/package.scala - runFirst
198// STUBBED OUT
199pub fn run_first<A>() -> Option<(FreeMap, A)> {
200    None
201}
202
203// See rholang/src/main/scala/coop/rchain/rholang/interpreter/matcher/package.scala - attemptOpt
204// NOT FULLY IMPLEMENTED
205pub fn attempt_opt(operation: Option<()>) -> Option<()> {
206    operation.map(|_| ())
207}
208
209// See rholang/src/main/scala/coop/rchain/rholang/interpreter/storage/package.scala - toSeq
210pub fn to_vec(fm: FreeMap, max: i32) -> Vec<Par> {
211    (0..max)
212        .map(|i| match fm.get(&i) {
213            Some(par) => par.clone(),
214            None => Par::default(),
215        })
216        .collect()
217}
218
219pub fn union(bitset1: Vec<u8>, bitset2: Vec<u8>) -> Vec<u8> {
220    let max_len = bitset1.len().max(bitset2.len());
221    let mut result = vec![0; max_len];
222
223    for i in 0..max_len {
224        let bit1 = if i < bitset1.len() { bitset1[i] } else { 0 };
225        let bit2 = if i < bitset2.len() { bitset2[i] } else { 0 };
226        result[i] = bit1 | bit2;
227    }
228
229    result
230}
231
232// See rholang/src/main/scala/coop/rchain/rholang/interpreter/matcher/ParSpatialMatcherUtils.scala - noFrees[Par]
233pub fn no_frees(par: Par) -> Par {
234    par.with_exprs(no_frees_exprs(par.exprs.clone()))
235}
236
237// See rholang/src/main/scala/coop/rchain/rholang/interpreter/matcher/ParSpatialMatcherUtils.scala - noFrees[Seq[Expr]]
238pub fn no_frees_exprs(exprs: Vec<Expr>) -> Vec<Expr> {
239    exprs
240        .iter()
241        .filter(|expr| match expr.expr_instance.clone() {
242            Some(EVarBody(EVar { v })) => match v.unwrap().var_instance {
243                Some(FreeVar(_)) => false,
244                Some(Wildcard(_)) => false,
245                _ => true,
246            },
247
248            _ => true,
249        })
250        .cloned()
251        .collect()
252}
253
254// See shared/src/main/scala/coop/rchain/catscontrib/Alternative_.scala - guard
255pub fn guard(condition: bool) -> Option<()> {
256    if condition {
257        Some(())
258    } else {
259        None
260    }
261}
262
263// Helper functions
264pub fn new_conn_and_body_par(
265    _ps: Vec<Par>,
266    _locally_free_par: Vec<u8>,
267    _connective_used_par: bool,
268) -> Par {
269    vector_par(_locally_free_par, _connective_used_par).with_connectives(vec![Connective {
270        connective_instance: Some(ConnAndBody(ConnectiveBody { ps: _ps })),
271    }])
272}
273
274pub fn new_conn_or_body_par(
275    _ps: Vec<Par>,
276    _locally_free_par: Vec<u8>,
277    _connective_used_par: bool,
278) -> Par {
279    vector_par(_locally_free_par, _connective_used_par).with_connectives(vec![Connective {
280        connective_instance: Some(ConnOrBody(ConnectiveBody { ps: _ps })),
281    }])
282}
283
284pub fn new_conn_not_body_par(
285    _body: Par,
286    _locally_free_par: Vec<u8>,
287    _connective_used_par: bool,
288) -> Par {
289    vector_par(_locally_free_par, _connective_used_par).with_connectives(vec![Connective {
290        connective_instance: Some(ConnNotBody(_body)),
291    }])
292}
293
294pub fn new_send(
295    _chan: Par,
296    _data: Vec<Par>,
297    _persistent: bool,
298    _locally_free: Vec<u8>,
299    _connective_used: bool,
300) -> Send {
301    Send {
302        chan: Some(_chan),
303        data: _data,
304        persistent: _persistent,
305        locally_free: _locally_free,
306        connective_used: _connective_used,
307    }
308}
309
310pub fn new_send_par(
311    _chan: Par,
312    _data: Vec<Par>,
313    _persistent: bool,
314    _locally_free: Vec<u8>,
315    _connective_used: bool,
316    _locally_free_par: Vec<u8>,
317    _connective_used_par: bool,
318) -> Par {
319    vector_par(_locally_free_par, _connective_used_par).with_sends(vec![Send {
320        chan: Some(_chan),
321        data: _data,
322        persistent: _persistent,
323        locally_free: _locally_free,
324        connective_used: _connective_used,
325    }])
326}
327
328pub fn new_match_par(
329    _target: Par,
330    _cases: Vec<MatchCase>,
331    _locally_free: Vec<u8>,
332    _connective_used: bool,
333    _locally_free_par: Vec<u8>,
334    _connective_used_par: bool,
335) -> Par {
336    vector_par(_locally_free_par, _connective_used_par).with_matches(vec![Match {
337        target: Some(_target),
338        cases: _cases,
339        locally_free: _locally_free,
340        connective_used: _connective_used,
341    }])
342}
343
344pub fn new_receive_par(
345    _binds: Vec<ReceiveBind>,
346    _body: Par,
347    _persistent: bool,
348    _peek: bool,
349    _bind_count: i32,
350    _locally_free: Vec<u8>,
351    _connective_used: bool,
352    _locally_free_par: Vec<u8>,
353    _connective_used_par: bool,
354) -> Par {
355    vector_par(_locally_free_par, _connective_used_par).with_receives(vec![Receive {
356        binds: _binds,
357        body: Some(_body),
358        persistent: _persistent,
359        peek: _peek,
360        bind_count: _bind_count,
361        locally_free: _locally_free,
362        connective_used: _connective_used,
363    }])
364}
365
366pub fn new_new_par(
367    _bind_count: i32,
368    _p: Par,
369    _uri: Vec<String>,
370    _injections: BTreeMap<String, Par>,
371    _locally_free: Vec<u8>,
372    _locally_free_par: Vec<u8>,
373    _connective_used_par: bool,
374) -> Par {
375    vector_par(_locally_free_par, _connective_used_par).with_news(vec![New {
376        bind_count: _bind_count,
377        p: Some(_p),
378        uri: _uri,
379        injections: _injections,
380        locally_free: _locally_free,
381    }])
382}
383
384pub fn new_eset_par(
385    _ps: Vec<Par>,
386    _locally_free: Vec<u8>,
387    _connective_used: bool,
388    _remainder: Option<Var>,
389    _locally_free_par: Vec<u8>,
390    _connective_used_par: bool,
391) -> Par {
392    vector_par(_locally_free_par, _connective_used_par).with_exprs(vec![new_eset_expr(
393        _ps,
394        _locally_free,
395        _connective_used,
396        _remainder,
397    )])
398}
399
400pub fn new_eset_expr(
401    _ps: Vec<Par>,
402    _locally_free: Vec<u8>,
403    _connective_used: bool,
404    _remainder: Option<Var>,
405) -> Expr {
406    // println!("new_eset_expr: _ps: {:?}", _ps);
407    // println!("new_eset_expr: _locally_free: {:?}", _locally_free);
408    // println!("new_eset_expr: _connective_used: {:?}", _connective_used);
409    // println!("new_eset_expr: _remainder: {:?}", _remainder);
410    Expr {
411        expr_instance: Some(ESetBody(ParSetTypeMapper::par_set_to_eset(ParSet::new(
412            _ps,
413            _connective_used,
414            _locally_free,
415            _remainder,
416        )))),
417    }
418}
419
420pub fn new_emap_par(
421    _kvs: Vec<KeyValuePair>,
422    _locally_free: Vec<u8>,
423    _connective_used: bool,
424    _remainder: Option<Var>,
425    _locally_free_par: Vec<u8>,
426    _connective_used_par: bool,
427) -> Par {
428    vector_par(_locally_free_par, _connective_used_par).with_exprs(vec![new_emap_expr(
429        _kvs,
430        _locally_free,
431        _connective_used,
432        _remainder,
433    )])
434}
435
436pub fn new_emap_expr(
437    _kvs: Vec<KeyValuePair>,
438    _locally_free: Vec<u8>,
439    _connective_used: bool,
440    _remainder: Option<Var>,
441) -> Expr {
442    Expr {
443        expr_instance: Some(EMapBody(ParMapTypeMapper::par_map_to_emap(ParMap::new(
444            _kvs.into_iter()
445                .filter_map(|kv| {
446                    if let (Some(key), Some(value)) = (kv.key, kv.value) {
447                        Some((key, value))
448                    } else {
449                        None
450                    }
451                })
452                .collect(),
453            _connective_used,
454            _locally_free,
455            _remainder,
456        )))),
457    }
458}
459
460pub fn new_key_value_pair(_key: Par, _value: Par) -> KeyValuePair {
461    KeyValuePair {
462        key: Some(_key),
463        value: Some(_value),
464    }
465}
466
467pub fn new_gint_par(value: i64, _locally_free_par: Vec<u8>, _connective_used_par: bool) -> Par {
468    vector_par(_locally_free_par, _connective_used_par).with_exprs(vec![new_gint_expr(value)])
469}
470
471pub fn new_gint_expr(value: i64) -> Expr {
472    Expr {
473        expr_instance: Some(GInt(value)),
474    }
475}
476
477pub fn new_gbool_par(value: bool, _locally_free_par: Vec<u8>, _connective_used_par: bool) -> Par {
478    vector_par(_locally_free_par, _connective_used_par).with_exprs(vec![new_gbool_expr(value)])
479}
480
481pub fn new_gbool_expr(value: bool) -> Expr {
482    Expr {
483        expr_instance: Some(GBool(value)),
484    }
485}
486
487pub fn new_gstring_par(
488    value: String,
489    _locally_free_par: Vec<u8>,
490    _connective_used_par: bool,
491) -> Par {
492    vector_par(_locally_free_par, _connective_used_par).with_exprs(vec![new_gstring_expr(value)])
493}
494
495pub fn new_gstring_expr(value: String) -> Expr {
496    Expr {
497        expr_instance: Some(GString(value)),
498    }
499}
500
501pub fn new_guri_par(value: String, _locally_free_par: Vec<u8>, _connective_used_par: bool) -> Par {
502    vector_par(_locally_free_par, _connective_used_par).with_exprs(vec![new_guri_expr(value)])
503}
504
505pub fn new_guri_expr(value: String) -> Expr {
506    Expr {
507        expr_instance: Some(GUri(value)),
508    }
509}
510
511pub fn new_wildcard_par(_locally_free_par: Vec<u8>, _connective_used_par: bool) -> Par {
512    vector_par(_locally_free_par, _connective_used_par).with_exprs(vec![Expr {
513        expr_instance: Some(EVarBody(EVar {
514            v: Some(Var {
515                var_instance: Some(Wildcard(WildcardMsg {})),
516            }),
517        })),
518    }])
519}
520
521pub fn new_wildcard_expr() -> Expr {
522    Expr {
523        expr_instance: Some(EVarBody(EVar {
524            v: Some(Var {
525                var_instance: Some(Wildcard(WildcardMsg {})),
526            }),
527        })),
528    }
529}
530
531pub fn new_wildcard_var() -> Var {
532    Var {
533        var_instance: Some(Wildcard(WildcardMsg {})),
534    }
535}
536
537pub fn new_boundvar_par(value: i32, _locally_free_par: Vec<u8>, _connective_used_par: bool) -> Par {
538    vector_par(
539        create_bit_vector(&vec![value as usize]),
540        _connective_used_par,
541    )
542    .with_exprs(vec![new_boundvar_expr(value)])
543}
544
545pub fn new_boundvar_expr(value: i32) -> Expr {
546    Expr {
547        expr_instance: Some(EVarBody(EVar {
548            v: Some(Var {
549                var_instance: Some(BoundVar(value)),
550            }),
551        })),
552    }
553}
554
555// "connective_used" is always "true" on "freevar"
556pub fn new_freevar_par(value: i32, _locally_free_par: Vec<u8>) -> Par {
557    vector_par(_locally_free_par, true).with_exprs(vec![new_freevar_expr(value)])
558}
559
560pub fn new_freevar_expr(value: i32) -> Expr {
561    Expr {
562        expr_instance: Some(EVarBody(EVar {
563            v: Some(Var {
564                var_instance: Some(FreeVar(value)),
565            }),
566        })),
567    }
568}
569
570pub fn new_freevar_var(value: i32) -> Var {
571    Var {
572        var_instance: Some(FreeVar(value)),
573    }
574}
575
576pub fn new_elist_par(
577    _ps: Vec<Par>,
578    _locally_free: Vec<u8>,
579    _connective_used_elist: bool,
580    _remainder: Option<Var>,
581    _locally_free_par: Vec<u8>,
582    _connective_used_par: bool,
583) -> Par {
584    vector_par(_locally_free_par, _connective_used_par).with_exprs(vec![new_elist_expr(
585        _ps,
586        _locally_free,
587        _connective_used_elist,
588        _remainder,
589    )])
590}
591
592pub fn new_elist_expr(
593    _ps: Vec<Par>,
594    _locally_free: Vec<u8>,
595    _connective_used: bool,
596    _remainder: Option<Var>,
597) -> Expr {
598    Expr {
599        expr_instance: Some(EListBody(EList {
600            ps: _ps,
601            locally_free: _locally_free,
602            connective_used: _connective_used,
603            remainder: _remainder,
604        })),
605    }
606}
607
608pub fn new_etuple_par(_ps: Vec<Par>) -> Par {
609    vector_par(Vec::new(), false).with_exprs(vec![new_etuple_expr(_ps, Vec::new(), false)])
610}
611
612pub fn new_etuple_expr(_ps: Vec<Par>, _locally_free: Vec<u8>, _connective_used: bool) -> Expr {
613    Expr {
614        expr_instance: Some(ETupleBody(ETuple {
615            ps: _ps,
616            locally_free: _locally_free,
617            connective_used: _connective_used,
618        })),
619    }
620}
621
622pub fn new_eplus_par_gint(
623    lhs_value: i64,
624    rhs_value: i64,
625    locally_free_par: Vec<u8>,
626    connective_used_par: bool,
627) -> Par {
628    Par::default().with_exprs(vec![Expr {
629        expr_instance: Some(ExprInstance::EPlusBody(EPlus {
630            p1: Some(new_gint_par(
631                lhs_value,
632                locally_free_par.clone(),
633                connective_used_par,
634            )),
635            p2: Some(new_gint_par(
636                rhs_value,
637                locally_free_par,
638                connective_used_par,
639            )),
640        })),
641    }])
642}
643
644pub fn new_eplus_par(lhs_value: Par, rhs_value: Par) -> Par {
645    let locally_free = union(
646        lhs_value.locally_free.clone(),
647        rhs_value.locally_free.clone(),
648    );
649    let connective_used = lhs_value.connective_used || rhs_value.connective_used;
650
651    Par::default()
652        .with_exprs(vec![Expr {
653            expr_instance: Some(EPlusBody(EPlus {
654                p1: Some(lhs_value),
655                p2: Some(rhs_value),
656            })),
657        }])
658        .with_locally_free(locally_free)
659        .with_connective_used(connective_used)
660}
661
662pub fn new_bundle_par(body: Par, write_flag: bool, read_flag: bool) -> Par {
663    Par::default().with_bundles(vec![Bundle {
664        body: Some(body),
665        write_flag,
666        read_flag,
667    }])
668}
669
670pub fn new_eminus_expr_gint(
671    lhs_value: i64,
672    rhs_value: i64,
673    locally_free_par: Vec<u8>,
674    connective_used_par: bool,
675) -> Expr {
676    Expr {
677        expr_instance: Some(EMinusBody(EMinus {
678            p1: Some(new_gint_par(
679                lhs_value,
680                locally_free_par.clone(),
681                connective_used_par,
682            )),
683            p2: Some(new_gint_par(
684                rhs_value,
685                locally_free_par,
686                connective_used_par,
687            )),
688        })),
689    }
690}
691
692pub fn new_ediv_expr_gint(
693    lhs_value: i64,
694    rhs_value: i64,
695    locally_free_par: Vec<u8>,
696    connective_used_par: bool,
697) -> Expr {
698    Expr {
699        expr_instance: Some(EDivBody(EDiv {
700            p1: Some(new_gint_par(
701                lhs_value,
702                locally_free_par.clone(),
703                connective_used_par,
704            )),
705            p2: Some(new_gint_par(
706                rhs_value,
707                locally_free_par,
708                connective_used_par,
709            )),
710        })),
711    }
712}
713
714pub fn new_eplus_expr_gint(
715    lhs_value: i64,
716    rhs_value: i64,
717    locally_free_par: Vec<u8>,
718    connective_used_par: bool,
719) -> Expr {
720    Expr {
721        expr_instance: Some(EPlusBody(EPlus {
722            p1: Some(new_gint_par(
723                lhs_value,
724                locally_free_par.clone(),
725                connective_used_par,
726            )),
727            p2: Some(new_gint_par(
728                rhs_value,
729                locally_free_par,
730                connective_used_par,
731            )),
732        })),
733    }
734}
735
736pub fn new_emult_expr_gint(
737    lhs_value: i64,
738    rhs_value: i64,
739    locally_free_par: Vec<u8>,
740    connective_used_par: bool,
741) -> Expr {
742    Expr {
743        expr_instance: Some(EMultBody(EMult {
744            p1: Some(new_gint_par(
745                lhs_value,
746                locally_free_par.clone(),
747                connective_used_par,
748            )),
749            p2: Some(new_gint_par(
750                rhs_value,
751                locally_free_par,
752                connective_used_par,
753            )),
754        })),
755    }
756}
757
758pub fn new_eeq_expr_gint(
759    lhs_value: i64,
760    rhs_value: i64,
761    locally_free_par: Vec<u8>,
762    connective_used_par: bool,
763) -> Expr {
764    Expr {
765        expr_instance: Some(EEqBody(EEq {
766            p1: Some(new_gint_par(
767                lhs_value,
768                locally_free_par.clone(),
769                connective_used_par,
770            )),
771            p2: Some(new_gint_par(
772                rhs_value,
773                locally_free_par,
774                connective_used_par,
775            )),
776        })),
777    }
778}
779
780pub fn new_eneq_expr_gint(
781    lhs_value: i64,
782    rhs_value: i64,
783    locally_free_par: Vec<u8>,
784    connective_used_par: bool,
785) -> Expr {
786    Expr {
787        expr_instance: Some(ENeqBody(ENeq {
788            p1: Some(new_gint_par(
789                lhs_value,
790                locally_free_par.clone(),
791                connective_used_par,
792            )),
793            p2: Some(new_gint_par(
794                rhs_value,
795                locally_free_par,
796                connective_used_par,
797            )),
798        })),
799    }
800}
801
802pub fn new_elt_expr_gint(
803    lhs_value: i64,
804    rhs_value: i64,
805    locally_free_par: Vec<u8>,
806    connective_used_par: bool,
807) -> Expr {
808    Expr {
809        expr_instance: Some(ELtBody(ELt {
810            p1: Some(new_gint_par(
811                lhs_value,
812                locally_free_par.clone(),
813                connective_used_par,
814            )),
815            p2: Some(new_gint_par(
816                rhs_value,
817                locally_free_par,
818                connective_used_par,
819            )),
820        })),
821    }
822}
823
824pub fn new_elte_expr_gint(
825    lhs_value: i64,
826    rhs_value: i64,
827    locally_free_par: Vec<u8>,
828    connective_used_par: bool,
829) -> Expr {
830    Expr {
831        expr_instance: Some(ELteBody(ELte {
832            p1: Some(new_gint_par(
833                lhs_value,
834                locally_free_par.clone(),
835                connective_used_par,
836            )),
837            p2: Some(new_gint_par(
838                rhs_value,
839                locally_free_par,
840                connective_used_par,
841            )),
842        })),
843    }
844}
845
846pub fn new_egt_expr_gbool(
847    lhs_value: bool,
848    rhs_value: bool,
849    locally_free_par: Vec<u8>,
850    connective_used_par: bool,
851) -> Expr {
852    Expr {
853        expr_instance: Some(EGtBody(EGt {
854            p1: Some(new_gbool_par(
855                lhs_value,
856                locally_free_par.clone(),
857                connective_used_par,
858            )),
859            p2: Some(new_gbool_par(
860                rhs_value,
861                locally_free_par,
862                connective_used_par,
863            )),
864        })),
865    }
866}
867
868pub fn new_egte_expr_gbool(
869    lhs_value: bool,
870    rhs_value: bool,
871    locally_free_par: Vec<u8>,
872    connective_used_par: bool,
873) -> Expr {
874    Expr {
875        expr_instance: Some(EGteBody(EGte {
876            p1: Some(new_gbool_par(
877                lhs_value,
878                locally_free_par.clone(),
879                connective_used_par,
880            )),
881            p2: Some(new_gbool_par(
882                rhs_value,
883                locally_free_par,
884                connective_used_par,
885            )),
886        })),
887    }
888}
889
890pub fn new_eor_expr(lhs: Par, rhs: Par) -> Expr {
891    Expr {
892        expr_instance: Some(EOrBody(EOr {
893            p1: Some(lhs),
894            p2: Some(rhs),
895        })),
896    }
897}
898
899pub fn new_emethod_expr(
900    method_name: String,
901    target: Par,
902    arguments: Vec<Par>,
903    locally_free: Vec<u8>,
904) -> Expr {
905    Expr {
906        expr_instance: Some(EMethodBody(EMethod {
907            method_name,
908            target: Some(target),
909            arguments,
910            locally_free,
911            connective_used: false,
912        })),
913    }
914}
915
916pub fn new_par_from_par_set(
917    elements: Vec<Par>,
918    locally_free: Vec<u8>,
919    connective_used: bool,
920    remainder: Option<Var>,
921) -> Par {
922    let par_set = ParSet::new(elements, connective_used, locally_free, remainder);
923
924    Par {
925        exprs: vec![Expr {
926            expr_instance: Some(ESetBody(ParSetTypeMapper::par_set_to_eset(par_set))),
927        }],
928        ..Default::default()
929    }
930}
931
932pub fn new_gbytearray_par(bytes: Vec<u8>, locally_free: Vec<u8>, connective_used: bool) -> Par {
933    Par {
934        exprs: vec![Expr {
935            expr_instance: Some(GByteArray(bytes)),
936        }],
937        locally_free,
938        connective_used,
939        ..Default::default()
940    }
941}
942
943pub fn new_gsys_auth_token_par(locally_free: Vec<u8>, connective_used: bool) -> Par {
944    Par {
945        unforgeables: vec![GUnforgeable {
946            unf_instance: Some(UnfInstance::GSysAuthTokenBody(GSysAuthToken {})),
947        }],
948        locally_free,
949        connective_used,
950        ..Default::default()
951    }
952}