mers_lib 0.9.29

library to use the mers language in other projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
use std::{
    fmt::Display,
    sync::{Arc, Mutex},
};

use crate::{
    data::{
        self,
        function::{Function, FunctionT},
        int::{Int, IntT, INT_MAX},
        Data, MersData, MersType, MersTypeWInfo, Type,
    },
    errors::CheckError,
    info::DisplayInfo,
    program::{self, run::CheckInfo},
};

use super::Config;

impl Config {
    /// Adds functions to deal with iterables
    /// `for_each: fn` executes a function once for each element of the iterable
    /// `map: fn` maps each value in the iterable to a new one by applying a transformation function
    /// `filter: fn` filters the iterable by removing all elements where the filter function doesn't return true
    /// `filter_map: fn` combines filter and map. requires that the function returns ()/(t).
    /// `map_while: fn` maps while the map-function returns (d), ends the iterator once () is returned.
    /// `take: fn` takes at most so many elements from the iterator.
    /// `enumerate: fn` transforms an iterator over T into one over (Int, T), where Int is the index of the element
    /// `any: fn` returns true if any element of the iterator are true
    /// `all: fn` returns true if all elements of the iterator are true
    /// `range_inc: fn` returns an iterable `Range` starting at the first argument, counting up to the second one (inclusive).
    /// `range_exc: fn` returns an iterable `Range` starting at the first argument, counting up to the second one (exclusive).
    pub fn with_iters(self) -> Self {
        self
            .add_type("Range".to_owned(), Err(Arc::new(|str, _i| {
                if let Some((val, end)) = str.split_once("..") {
                    if let (Ok(val), Ok(end)) = (val.trim().parse(), end.trim().parse()) {
                        Ok(RangeT(val, end))
                    } else {
                        Err(CheckError::from(format!("bad Range type, got <{str}> but expected <start..end> where start and end are Ints")))
                    }
                } else {
                    Err(CheckError::from(format!("bad Range type, got <{str}> but expected <start..end>")))
                }.map(|v| Arc::new(Type::new(v)))
            })))
            .add_var("range_inc", Function::new_generic(
                |a, i| {
                    let mut o = Type::empty();
                    for a in &a.types {
                        let a = a.as_any().downcast_ref::<data::tuple::TupleT>().ok_or_else(|| CheckError::from(format!("expected 2- or 3-tuple, but found {}", a.with_info(i))))?;
                        if a.0.len() == 2 {
                            let mut min = None;
                            let mut max = None;
                            for v in &a.0[0].types {
                                let v = v.as_any().downcast_ref::<data::int::IntT>().ok_or_else(|| CheckError::from(format!("expected int as first argument, but got {}", v.with_info(i))))?;
                                if min.is_none_or(|min| min > v.0) {
                                    min = Some(v.0);
                                }
                            }
                            for v in &a.0[1].types {
                                let v = v.as_any().downcast_ref::<data::int::IntT>().ok_or_else(|| CheckError::from(format!("expected int as second argument, but got {}", v.with_info(i))))?;
                                if max.is_none_or(|max| max < v.1) {
                                    max = Some(v.1);
                                }
                            }
                            if let (Some(min), Some(max)) = (min, max) {
                                o.add(Arc::new(RangeT(min, max)));
                            }
                        } else {
                            return Err(CheckError::from(format!("expected 2-tuple, but found {}", a.with_info(i))));
                        }
                    }
                    Ok(o)
                }, |a, _| {
                    let a = a.get();
                    let a = a.as_any().downcast_ref::<data::tuple::Tuple>().unwrap();
                    let (v, e) = (a.0[0].read(), a.0[1].read());
                    let (v, e) = (v.get(), e.get());
                    let (v, e) = (v.as_any().downcast_ref::<data::int::Int>().unwrap(), e.as_any().downcast_ref::<data::int::Int>().unwrap());
                    Ok(Data::new(Range(v.0, e.0)))
                }
            ))
            .add_var("range_exc", Function::new_generic(
                |a, i| {
                    let mut o = Type::empty();
                    for a in &a.types {
                        let a = a.as_any().downcast_ref::<data::tuple::TupleT>().ok_or_else(|| CheckError::from(format!("expected 2- or 3-tuple, but found {}", a.with_info(i))))?;
                        if a.0.len() == 2 {
                            let mut min = None;
                            let mut max = None;
                            for v in &a.0[0].types {
                                let v = v.as_any().downcast_ref::<data::int::IntT>().ok_or_else(|| CheckError::from(format!("expected int as first argument, but got {}", v.with_info(i))))?;
                                if min.is_none_or(|min| min > v.0) {
                                    min = Some(v.0);
                                }
                            }
                            for v in &a.0[1].types {
                                let v = v.as_any().downcast_ref::<data::int::IntT>().ok_or_else(|| CheckError::from(format!("expected int as second argument, but got {}", v.with_info(i))))?;
                                if max.is_none_or(|max| max < v.1) {
                                    max = Some(v.1);
                                }
                            }
                            if let (Some(min), Some(max)) = (min, max) {
                                if let Some(max) = max.checked_sub(1) {
                                    o.add(Arc::new(RangeT(min, max)));
                                } else {
                                    o.add(Arc::new(RangeT(if min == isize::MIN { min + 1 } else { min }, isize::MIN)));
                                }
                            }
                        } else {
                            return Err(CheckError::from(format!("expected 2-tuple, but found {}", a.with_info(i))));
                        }
                    }
                    Ok(o)
                }, |a, _| {
                    let a = a.get();
                    let a = a.as_any().downcast_ref::<data::tuple::Tuple>().unwrap();
                    let (v, e) = (a.0[0].read(), a.0[1].read());
                    let (v, e) = (v.get(), e.get());
                    let (v, e) = (v.as_any().downcast_ref::<data::int::Int>().unwrap(), e.as_any().downcast_ref::<data::int::Int>().unwrap());
                    if let Some(e) = e.0.checked_sub(1) {
                        Ok(Data::new(Range(v.0, e)))
                    } else {
                        Ok(Data::new(Range(v.0.saturating_add(1), e.0)))
                    }
                }
            ))
            .add_var("any", genfunc_iter_in_val_out("all".to_string(), data::bool::bool_type(), data::bool::bool_type(), |a, i| {
                for v in a.get().iterable(&i.global).unwrap().map(|v| v.map(|v| v.get().as_any().downcast_ref::<data::bool::Bool>().is_some_and(|v| v.0))) {
                    if v? {
                        return Ok(Data::new(data::bool::Bool(true)));
                    }
                }
                Ok(Data::new(data::bool::Bool(false)))
            }))
            .add_var("all", genfunc_iter_in_val_out("all".to_string(), data::bool::bool_type(), data::bool::bool_type(), |a, i| {
                for v in a.get().iterable(&i.global).unwrap().map(|v| v.map(|v| v.get().as_any().downcast_ref::<data::bool::Bool>().is_some_and(|v| v.0))) {
                    if !v? {
                        return Ok(Data::new(data::bool::Bool(false)));
                    }
                }
                Ok(Data::new(data::bool::Bool(true)))
            }))
            .add_var(
            "for_each",
            data::function::Function {
                info: program::run::Info::neverused(),
                info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
                fixed_type: None,
                fixed_type_out: Arc::new(Mutex::new(None)),
                out: Ok(Arc::new(|a, i| {
                    for a in &a.types {
                        if let Some(tuple) = a.as_any().downcast_ref::<data::tuple::TupleT>() {
                            if let (Some(v), Some(f)) = (tuple.0.get(0), tuple.0.get(1)) {
                                if let (Some(iter), Some(f)) = (
                                    v.iterable(),
                                    f.types
                                        .iter()
                                        .map(|t| {
                                            t.executable()
                                        })
                                        .collect::<Option<Vec<_>>>(),
                                ) {
                                    for f in f {
                                        let _ret = f.o(&iter)?;
                                        // if !ret.is_zero_tuple() {
                                        //     return Err(format!("for_each function must return (), not {ret}").into());
                                        // }
                                    }
                                } else {
                                    return Err(format!(
                                        "for_each called on tuple not containing iterable and function: {} is {}",
                                        v.with_info(i),
                                        if v.iterable().is_some() { "iterable" } else { "not iterable" },
                                    ).into());
                                }
                            } else {
                                return Err(format!(
                                    "for_each called on tuple with len < 2"
                                ).into());
                            }
                        } else {
                            return Err(format!("for_each called on non-tuple").into());
                        }
                    }
                    Ok(Type::empty_tuple())
                })),
                run: Arc::new(|a, i| {
                    if let Some(tuple) = a.get().as_any().downcast_ref::<data::tuple::Tuple>() {
                        if let (Some(v), Some(f)) = (tuple.get(0), tuple.get(1)) {
                            if let Some(iter) = v.get().iterable(&i.global) {
                                let f = f.get();
                                for v in iter {
                                    f.execute(v?, &i.global).unwrap()?;
                                }
                                Ok(Data::empty_tuple())
                            } else {
                                return Err(
                                    "for_each called on tuple not containing iterable and function (not an iterable)".into()
                                );
                            }
                        } else {
                            return Err("for_each called on tuple with len < 2".into());
                        }
                    } else {
                        return Err("for_each called on non-tuple".into());
                    }
                }),
                inner_statements: None,
            },
        )
        .add_var(
            "map",
            genfunc_iter_and_func("map", ItersT::Map, Iters::Map)
        )
        .add_var(
            "filter",
            genfunc_iter_and_func("filter", ItersT::Filter, Iters::Filter),
        )
        .add_var(
            "filter_map",
            genfunc_iter_and_func("filter_map", ItersT::FilterMap, Iters::FilterMap),
        )
        .add_var(
            "map_while",
            genfunc_iter_and_func("map_while", ItersT::MapWhile, Iters::MapWhile),
        )
        .add_var("take", genfunc_iter_and_arg("take", |_: &data::int::IntT| ItersT::Take, |v: &data::int::Int| {
            Iters::Take(v.0.max(0).try_into().unwrap_or(usize::MAX))
        }, &data::int::IntT(0, INT_MAX)))
        .add_var(
            "enumerate",
            data::function::Function {
                info: program::run::Info::neverused(),
                info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
                fixed_type: None,
                fixed_type_out: Arc::new(Mutex::new(None)),
                out: Ok(Arc::new(|a, i| {
                    let data = if let Some(a) = a.iterable() {
                        a
                    } else {
                        return Err(format!("cannot call enumerate on non-iterable type {}.", a.with_info(i)).into());
                    };
                    Ok(Type::new(IterT::new(ItersT::Enumerate, data, i)?))
                })),
                run: Arc::new(|a, _i| Ok(Data::new(Iter(Iters::Enumerate, a.clone())))),
                inner_statements: None,
            },
        )
        .add_var(
            "chain",
            data::function::Function {
                info: program::run::Info::neverused(),
                info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
                fixed_type: None,
                fixed_type_out: Arc::new(Mutex::new(None)),
                out: Ok(Arc::new(|a, i| {
                    let data = if let Some(a) = a.iterable() {
                        a
                    } else {
                        return Err(format!("cannot call chain on non-iterable type {}.", a.with_info(i)).into());
                    };
                    Ok(Type::new(IterT::new(ItersT::Chained, data, i)?))
                })),
                run: Arc::new(|a, _i| Ok(Data::new(Iter(Iters::Chained, a.clone())))),
                inner_statements: None,
            },
        )
    }
}

fn genfunc_iter_and_func(
    name: &'static str,
    ft: impl Fn(FunctionT) -> ItersT + Send + Sync + 'static,
    fd: impl Fn(Data) -> Iters + Send + Sync + 'static,
) -> data::function::Function {
    fn iter_out_arg(
        a: &Type,
        i: &mut CheckInfo,
        name: &str,
        func: impl Fn(FunctionT) -> ItersT + Sync + Send,
    ) -> Result<Type, CheckError> {
        let mut out = Type::empty();
        for t in a.types.iter() {
            if let Some(t) = t.as_any().downcast_ref::<data::tuple::TupleT>() {
                if t.0.len() != 2 {
                    return Err(format!("cannot call {name} on tuple where len != 2").into());
                }
                if let Some(v) = t.0[0].iterable() {
                    for f in t.0[1].types.iter() {
                        if let Some(f) = f.executable() {
                            out.add(Arc::new(IterT::new(func(f), v.clone(), i)?));
                        } else {
                            return Err(format!("cannot call {name} on tuple that isn't (_, function): got {} instead of function as part of {}", t.0[1].with_info(i), a.with_info(i)).into());
                        }
                    }
                } else {
                    return Err(format!(
                        "cannot call {name} on non-iterable type {}, which is part of {}.",
                        t.with_info(i),
                        a.with_info(i)
                    )
                    .into());
                }
            }
        }
        Ok(out)
    }
    data::function::Function {
        info: program::run::Info::neverused(),
        info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
        fixed_type: None,
        fixed_type_out: Arc::new(Mutex::new(None)),
        out: Ok(Arc::new(move |a, i| iter_out_arg(a, i, name, |f| ft(f)))),
        run: Arc::new(move |a, _i| {
            if let Some(tuple) = a.get().as_any().downcast_ref::<data::tuple::Tuple>() {
                if let (Some(v), Some(f)) = (tuple.get(0), tuple.get(1)) {
                    Ok(Data::new(Iter(fd(f.clone()), v.clone())))
                } else {
                    return Err("{name} called on tuple with len < 2".into());
                }
            } else {
                return Err("{name} called on non-tuple".into());
            }
        }),
        inner_statements: None,
    }
}
fn genfunc_iter_and_arg<T: MersType, D: MersData>(
    name: &'static str,
    ft: impl Fn(&T) -> ItersT + Send + Sync + 'static,
    fd: impl Fn(&D) -> Iters + Send + Sync + 'static,
    type_sample: &'static T,
) -> data::function::Function {
    fn iter_out_arg<T: MersType>(
        a: &Type,
        i: &mut CheckInfo,
        name: &str,
        func: impl Fn(&T) -> ItersT + Sync + Send,
        type_sample: &T,
    ) -> Result<Type, CheckError> {
        let type_sample = type_sample.with_info(i);
        let mut out = Type::empty();
        for t in a.types.iter() {
            if let Some(t) = t.as_any().downcast_ref::<data::tuple::TupleT>() {
                if t.0.len() != 2 {
                    return Err(format!("cannot call {name} on tuple where len != 2").into());
                }
                if let Some(v) = t.0[0].iterable() {
                    for f in t.0[1].types.iter() {
                        if let Some(f) = f.as_any().downcast_ref::<T>() {
                            out.add(Arc::new(IterT::new(func(f), v.clone(), i)?));
                        } else {
                            return Err(format!("cannot call {name} on tuple that isn't (_, {type_sample}): got {} instead of {type_sample} as part of {}", t.0[1].with_info(i), a.with_info(i)).into());
                        }
                    }
                } else {
                    return Err(format!(
                        "cannot call {name} on non-iterable type {}, which is part of {}.",
                        t.with_info(i),
                        a.with_info(i)
                    )
                    .into());
                }
            }
        }
        Ok(out)
    }
    data::function::Function {
        info: program::run::Info::neverused(),
        info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
        fixed_type: None,
        fixed_type_out: Arc::new(Mutex::new(None)),
        out: Ok(Arc::new(move |a, i| {
            iter_out_arg(a, i, name, |f: &T| ft(f), type_sample)
        })),
        run: Arc::new(move |a, _i| {
            if let Some(tuple) = a.get().as_any().downcast_ref::<data::tuple::Tuple>() {
                if let (Some(v), Some(f)) = (tuple.get(0), tuple.get(1)) {
                    if let Some(f) = f.get().as_any().downcast_ref::<D>() {
                        Ok(Data::new(Iter(fd(f), v.clone())))
                    } else {
                        return Err("{name} called on tuple not containing function".into());
                    }
                } else {
                    return Err("{name} called on tuple with len < 2".into());
                }
            } else {
                return Err("{name} called on non-tuple".into());
            }
        }),
        inner_statements: None,
    }
}

#[derive(Clone, Debug)]
pub enum Iters {
    Map(Data),
    Filter(Data),
    FilterMap(Data),
    MapWhile(Data),
    Take(usize),
    Enumerate,
    Chained,
}
#[derive(Clone, Debug)]
pub enum ItersT {
    Map(data::function::FunctionT),
    Filter(data::function::FunctionT),
    FilterMap(data::function::FunctionT),
    MapWhile(data::function::FunctionT),
    Take,
    Enumerate,
    Chained,
}
#[derive(Clone, Debug)]
pub struct Iter(pub Iters, pub Data);
#[derive(Clone, Debug)]
pub struct IterT(pub ItersT, pub Type, pub Type);
impl MersData for Iter {
    fn display(&self, _info: &DisplayInfo<'_>, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{self}")
    }
    fn is_eq(&self, _other: &dyn MersData) -> bool {
        false
    }
    fn iterable(
        &self,
        gi: &crate::program::run::RunLocalGlobalInfo,
    ) -> Option<Box<dyn Iterator<Item = Result<Data, CheckError>>>> {
        let gi = gi.clone();
        Some(match &self.0 {
            Iters::Map(f) => {
                let f = Clone::clone(f);
                Box::new(self.1.get().iterable(&gi)?.map(move |v| {
                    f.get()
                        .execute(v?, &gi)
                        .ok_or_else(|| CheckError::from("called map with non-function argument"))?
                }))
            }
            Iters::Filter(f) => {
                let f = Clone::clone(f);
                Box::new(self.1.get().iterable(&gi)?.filter_map(move |v| {
                    match v {
                        Ok(v) => match f.get().execute(v.clone(), &gi) {
                            Some(Ok(f)) => {
                                if f.get()
                                    .as_any()
                                    .downcast_ref::<data::bool::Bool>()
                                    .is_some_and(|b| b.0)
                                {
                                    Some(Ok(v))
                                } else {
                                    None
                                }
                            }
                            Some(Err(e)) => Some(Err(e)),
                            None => Some(Err(CheckError::from(
                                "called filter with non-function argument",
                            ))),
                        },
                        Err(e) => Some(Err(e)),
                    }
                }))
            }
            Iters::FilterMap(f) => {
                let f = Clone::clone(f);
                Box::new(self.1.get().iterable(&gi)?.filter_map(move |v| match v {
                    Ok(v) => match f.get().execute(v, &gi) {
                        Some(Ok(r)) => Some(Ok(r.one_tuple_content()?)),
                        Some(Err(e)) => Some(Err(e)),
                        None => Some(Err(CheckError::from(
                            "called filter_map with non-function argument",
                        ))),
                    },
                    Err(e) => Some(Err(e)),
                }))
            }
            Iters::MapWhile(f) => {
                let f = Clone::clone(f);
                Box::new(self.1.get().iterable(&gi)?.map_while(move |v| match v {
                    Ok(v) => match f.get().execute(v, &gi) {
                        Some(Ok(r)) => Some(Ok(r.one_tuple_content()?)),
                        Some(Err(e)) => Some(Err(e)),
                        None => Some(Err(CheckError::from(
                            "called map_while with non-function argument",
                        ))),
                    },
                    Err(e) => Some(Err(e)),
                }))
            }
            Iters::Take(limit) => Box::new(self.1.get().iterable(&gi)?.take(*limit)),
            Iters::Enumerate => {
                Box::new(
                    self.1
                        .get()
                        .iterable(&gi)?
                        .enumerate()
                        .map(|(i, v)| match v {
                            Ok(v) => Ok(Data::new(data::tuple::Tuple::from([
                                Data::new(data::int::Int(i as _)),
                                v,
                            ]))),
                            Err(e) => Err(e),
                        }),
                )
            }
            Iters::Chained => {
                match self
                    .1
                    .get()
                    .iterable(&gi)?
                    .map(move |v| Ok(v?.get().iterable(&gi)))
                    .collect::<Result<Option<Vec<_>>, CheckError>>()
                {
                    Ok(Some(iters)) => Box::new(iters.into_iter().flatten()),
                    Ok(None) => return None,
                    Err(e) => Box::new([Err(e)].into_iter()),
                }
            }
        })
    }
    fn clone(&self) -> Box<dyn MersData> {
        Box::new(Clone::clone(self))
    }
    fn as_type(&self) -> data::Type {
        Type::new(
            IterT::new(
                self.0.as_type(),
                self.1.get().as_type(),
                &crate::info::Info::neverused(),
            )
            .unwrap(),
        )
    }
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    fn mut_any(&mut self) -> &mut dyn std::any::Any {
        self
    }
    fn to_any(self) -> Box<dyn std::any::Any> {
        Box::new(self)
    }
}
impl IterT {
    /// `i` is only used for errors (this is important for `as_type()`)
    pub fn new(iter: ItersT, data: Type, i: &CheckInfo) -> Result<Self, CheckError> {
        let t = match &iter {
            ItersT::Map(f) => f.o(&data)?,
            ItersT::Filter(f) => {
                if f.o(&data)?.is_included_in(&data::bool::bool_type()) {
                    data.clone()
                } else {
                    return Err(format!(
                        "Iter:Filter, but function doesn't return bool for argument {}.",
                        data.with_info(&i)
                    )
                    .into());
                }
            }
            ItersT::FilterMap(f) => {
                if let Some(v) = f.o(&data)?.one_tuple_possible_content() {
                    v
                } else {
                    return Err(
                        format!("Iter:FilterMap, but function doesn't return ()/(t).").into(),
                    );
                }
            }
            ItersT::MapWhile(f) => {
                if let Some(t) = f.o(&data)?.one_tuple_possible_content() {
                    t
                } else {
                    return Err(
                        format!("Iter:MapWhile, but function doesn't return ()/(t).").into(),
                    );
                }
            }
            ItersT::Take => data.clone(),
            ItersT::Enumerate => Type::new(data::tuple::TupleT(vec![
                Type::new(data::int::IntT(0, INT_MAX)),
                data.clone(),
            ])),
            ItersT::Chained => {
                if let Some(out) = data.iterable() {
                    out
                } else {
                    return Err(format!(
                        "Cannot create a chain from an iterator over the non-iterator type {}.",
                        data.with_info(i)
                    )
                    .into());
                }
            }
        };
        Ok(Self(iter, data, t))
    }
}
impl MersType for IterT {
    fn display(
        &self,
        info: &crate::info::DisplayInfo<'_>,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        write!(f, "<Iter: {}>", self.2.with_display(info))
    }
    fn is_same_type_as(&self, other: &dyn MersType) -> bool {
        if let Some(other) = other.as_any().downcast_ref::<Self>() {
            self.2.is_same_type_as(&other.2)
        } else {
            false
        }
    }
    fn is_included_in(&self, target: &dyn MersType) -> bool {
        if let Some(target) = target.as_any().downcast_ref::<Self>() {
            self.2.is_included_in(&target.2)
        } else {
            false
        }
    }
    fn without(&self, remove: &dyn MersType) -> Option<Type> {
        if self.is_included_in(remove) {
            Some(Type::empty())
        } else {
            None
        }
    }
    fn iterable(&self) -> Option<Type> {
        Some(self.2.clone())
    }
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    fn mut_any(&mut self) -> &mut dyn std::any::Any {
        self
    }
    fn to_any(self) -> Box<dyn std::any::Any> {
        Box::new(self)
    }
}
impl Display for Iter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "<Iter>")
    }
}
impl Iters {
    fn as_type(&self) -> ItersT {
        match self {
            Self::Map(f) => ItersT::Map(f.get().executable().unwrap()),
            Self::Filter(f) => ItersT::Filter(f.get().executable().unwrap()),
            Self::FilterMap(f) => ItersT::FilterMap(f.get().executable().unwrap()),
            Self::MapWhile(f) => ItersT::MapWhile(f.get().executable().unwrap()),
            Self::Take(_) => ItersT::Take,
            Self::Enumerate => ItersT::Enumerate,
            Self::Chained => ItersT::Chained,
        }
    }
}

fn genfunc_iter_in_val_out(
    name: String,
    iter_type: Type,
    out_type: Type,
    run: impl Fn(Data, &mut crate::info::Info<program::run::RunLocal>) -> Result<Data, CheckError>
        + Send
        + Sync
        + 'static,
) -> Function {
    Function {
        info: crate::info::Info::neverused(),
        info_check: Arc::new(Mutex::new(crate::info::Info::neverused())),
        fixed_type: None,
        fixed_type_out: Arc::new(Mutex::new(None)),
        out: Ok(Arc::new(move |a, i| {
            if let Some(iter_over) = a.iterable() {
                if iter_over.is_included_in(&iter_type) {
                    Ok(out_type.clone())
                } else {
                    Err(format!(
                        "Cannot call function {name} on iterator over type {}, which isn't {}.",
                        a.with_info(i),
                        iter_type.with_info(i)
                    )
                    .into())
                }
            } else {
                Err(format!(
                    "Cannot call function {name} on non-iterable type {}.",
                    a.with_info(i)
                )
                .into())
            }
        })),
        run: Arc::new(run),
        inner_statements: None,
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Range(isize, isize);
#[derive(Clone, Debug, PartialEq)]
pub struct RangeT(isize, isize);
impl MersData for Range {
    fn display(&self, _info: &DisplayInfo, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}.range_inc({})", self.0, self.1)
    }
    fn iterable(
        &self,
        _gi: &crate::program::run::RunLocalGlobalInfo,
    ) -> Option<Box<dyn Iterator<Item = Result<Data, CheckError>>>> {
        Some(Box::new(
            RangeInt(self.0, self.1, false).map(|v| Ok(Data::new(Int(v)))),
        ))
    }
    fn clone(&self) -> Box<dyn MersData> {
        Box::new(Clone::clone(self))
    }
    fn is_eq(&self, other: &dyn MersData) -> bool {
        other
            .as_any()
            .downcast_ref::<Self>()
            .is_some_and(|other| other == self)
    }
    fn as_type(&self) -> Type {
        Type::new(RangeT(self.0, self.1))
    }
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    fn mut_any(&mut self) -> &mut dyn std::any::Any {
        self
    }
    fn to_any(self) -> Box<dyn std::any::Any> {
        Box::new(self)
    }
}
impl MersType for RangeT {
    fn display(
        &self,
        _info: &crate::info::DisplayInfo<'_>,
        f: &mut std::fmt::Formatter,
    ) -> std::fmt::Result {
        write!(f, "Range<{}..{}>", self.0, self.1)
    }
    fn iterable(&self) -> Option<Type> {
        Some(if self.is_empty() {
            Type::empty()
        } else {
            Type::new(IntT(self.0, self.1))
        })
    }
    fn is_same_type_as(&self, other: &dyn MersType) -> bool {
        other
            .as_any()
            .downcast_ref::<Self>()
            .is_some_and(|other| *self == *other)
    }
    fn is_included_in(&self, target: &dyn MersType) -> bool {
        target
            .as_any()
            .downcast_ref::<Self>()
            .is_some_and(|target| {
                // prolly good
                self.is_empty() || (!target.is_empty() && self.0 >= target.0 && self.1 <= target.1)
            })
    }
    fn without(&self, remove: &dyn MersType) -> Option<Type> {
        if self.is_included_in(remove) {
            Some(Type::empty())
        } else {
            None
        }
    }
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    fn mut_any(&mut self) -> &mut dyn std::any::Any {
        self
    }
    fn to_any(self) -> Box<dyn std::any::Any> {
        Box::new(self)
    }
}
impl RangeT {
    pub fn is_empty(&self) -> bool {
        self.1 < self.0
    }
}

struct RangeInt(isize, isize, bool);
impl Iterator for RangeInt {
    type Item = isize;
    fn next(&mut self) -> Option<Self::Item> {
        if !self.2 && self.0 <= self.1 {
            let o = self.0;
            if self.0 < self.1 {
                self.0 += 1;
            } else {
                self.2 = true;
            }
            Some(o)
        } else {
            None
        }
    }
}