diffsl 0.11.2

A compiler for a domain-specific language for ordinary differential equations (ODE).
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
use std::collections::HashMap;

use log::{debug, log_enabled, Level};
use ndarray::s;

use crate::{
    ast::{self, Ast, AstKind, StringSpan},
    discretise::layout::NonZero,
};

use super::{
    can_broadcast_to, layout::ArcLayout, Layout, LayoutKind, Shape, Tensor, TensorBlock,
    ValidationError, ValidationErrors,
};

pub struct EnvVar {
    layout: ArcLayout,
    is_time_dependent: bool,
    is_state_dependent: bool,
    is_dstatedt_dependent: bool,
    is_input_dependent: bool,
    is_model_dependent: bool,
    is_algebraic: bool,
}

impl EnvVar {
    pub fn is_time_dependent(&self) -> bool {
        self.is_time_dependent
    }

    pub fn is_state_dependent(&self) -> bool {
        self.is_state_dependent
    }

    pub fn is_dstatedt_dependent(&self) -> bool {
        self.is_dstatedt_dependent
    }

    pub fn is_algebraic(&self) -> bool {
        self.is_algebraic
    }

    pub fn is_input_dependent(&self) -> bool {
        self.is_input_dependent
    }

    pub fn is_model_dependent(&self) -> bool {
        self.is_model_dependent
    }

    pub fn layout(&self) -> &Layout {
        self.layout.as_ref()
    }
}

pub struct Env {
    current_span: Option<StringSpan>,
    errs: ValidationErrors,
    vars: HashMap<String, EnvVar>,
    pub(crate) state0_input_deps: Vec<NonZero>,
    pub(crate) dstate0_input_deps: Vec<NonZero>,
}

impl Env {
    fn eval_const_integer_expr(expr: &Ast) -> Option<i64> {
        match &expr.kind {
            AstKind::Integer(v) => Some(*v),
            AstKind::Number(v) => {
                if v.fract() == 0.0 {
                    Some(*v as i64)
                } else {
                    None
                }
            }
            AstKind::Monop(op) => {
                let child = Self::eval_const_integer_expr(op.child.as_ref())?;
                match op.op {
                    '+' => Some(child),
                    '-' => child.checked_neg(),
                    _ => None,
                }
            }
            AstKind::Binop(op) => {
                let left = Self::eval_const_integer_expr(op.left.as_ref())?;
                let right = Self::eval_const_integer_expr(op.right.as_ref())?;
                match op.op {
                    '+' => left.checked_add(right),
                    '-' => left.checked_sub(right),
                    '*' => left.checked_mul(right),
                    '/' => {
                        if right == 0 {
                            None
                        } else {
                            Some(left / right)
                        }
                    }
                    '%' => {
                        if right == 0 {
                            None
                        } else {
                            Some(left % right)
                        }
                    }
                    _ => None,
                }
            }
            _ => None,
        }
    }

    fn eval_integer_expr_with_n(expr: &Ast, n: i64) -> Option<i64> {
        match &expr.kind {
            AstKind::Integer(v) => Some(*v),
            AstKind::Number(v) => {
                if v.fract() == 0.0 {
                    Some(*v as i64)
                } else {
                    None
                }
            }
            AstKind::Name(name) => {
                if name.name == "N" {
                    Some(n)
                } else {
                    None
                }
            }
            AstKind::Monop(op) => {
                let child = Self::eval_integer_expr_with_n(op.child.as_ref(), n)?;
                match op.op {
                    '+' => Some(child),
                    '-' => child.checked_neg(),
                    _ => None,
                }
            }
            AstKind::Binop(op) => {
                let left = Self::eval_integer_expr_with_n(op.left.as_ref(), n)?;
                let right = Self::eval_integer_expr_with_n(op.right.as_ref(), n)?;
                match op.op {
                    '+' => left.checked_add(right),
                    '-' => left.checked_sub(right),
                    '*' => left.checked_mul(right),
                    '/' => {
                        if right == 0 {
                            None
                        } else {
                            Some(left / right)
                        }
                    }
                    '%' => {
                        if right == 0 {
                            None
                        } else {
                            Some(left % right)
                        }
                    }
                    _ => None,
                }
            }
            _ => None,
        }
    }

    fn eval_constant_range_width(start: &Ast, end: &Ast) -> Option<i64> {
        if let (Some(first), Some(last)) = (
            Self::eval_const_integer_expr(start),
            Self::eval_const_integer_expr(end),
        ) {
            return last.checked_sub(first);
        }

        let mut width = None;
        for n in [0_i64, 1, 2, 3, 7, 16] {
            let first_n = Self::eval_integer_expr_with_n(start, n)?;
            let last_n = Self::eval_integer_expr_with_n(end, n)?;
            let width_n = last_n.checked_sub(first_n)?;
            match width {
                Some(prev) if prev != width_n => return None,
                Some(_) => {}
                None => width = Some(width_n),
            }
        }
        width
    }

    pub fn new() -> Self {
        let mut vars = HashMap::new();
        vars.insert(
            "t".to_string(),
            EnvVar {
                layout: ArcLayout::new(Layout::new_scalar()),
                is_time_dependent: true,
                is_state_dependent: false,
                is_dstatedt_dependent: false,
                is_input_dependent: false,
                is_model_dependent: false,
                is_algebraic: true,
            },
        );
        vars.insert(
            "N".to_string(),
            EnvVar {
                layout: ArcLayout::new(Layout::new_scalar()),
                is_time_dependent: false,
                is_state_dependent: false,
                is_dstatedt_dependent: false,
                is_input_dependent: false,
                is_model_dependent: true,
                is_algebraic: true,
            },
        );
        Env {
            errs: ValidationErrors::default(),
            vars,
            current_span: None,
            state0_input_deps: vec![],
            dstate0_input_deps: vec![],
        }
    }

    /// create a new ArcLayout from a Layout, if the layout already exists in the env then return the existing one
    pub fn new_layout_ptr(&mut self, layout: Layout) -> ArcLayout {
        for var in self.vars.values() {
            if var.layout.as_ref().eq_nonzeros_and_deps(&layout) {
                return var.layout.clone();
            }
        }
        ArcLayout::new(layout)
    }

    pub fn is_tensor_time_dependent(&self, tensor: &Tensor) -> bool {
        if tensor.name() == "u" || tensor.name() == "dudt" {
            return true;
        };
        tensor.elmts().iter().any(|block| {
            block
                .expr()
                .get_dependents()
                .iter()
                .any(|&dep| dep == "t" || self.vars[dep].is_time_dependent())
        })
    }
    pub fn is_tensor_state_dependent(&self, tensor: &Tensor) -> bool {
        self.is_tensor_dependent_on(tensor, "u")
    }

    pub fn is_tensor_input_dependent(&self, tensor: &Tensor) -> bool {
        self.is_tensor_dependent_on(tensor, "in")
    }

    pub fn is_tensor_model_dependent(&self, tensor: &Tensor) -> bool {
        if tensor.name() == "N" {
            return true;
        }
        tensor.elmts().iter().any(|block| {
            block
                .expr()
                .get_dependents()
                .iter()
                .any(|&dep| dep == "N" || self.vars[dep].is_model_dependent())
        })
    }

    pub fn is_tensor_dstatedt_dependent(&self, tensor: &Tensor) -> bool {
        self.is_tensor_dependent_on(tensor, "dudt")
    }

    fn is_tensor_dependent_on(&self, tensor: &Tensor, var: &str) -> bool {
        if tensor.name() == var {
            return true;
        };
        tensor.elmts().iter().any(|block| {
            block.expr().get_dependents().iter().any(|&dep| {
                dep == var
                    || match var {
                        "u" => self.vars[dep].is_state_dependent(),
                        "dudt" => self.vars[dep].is_dstatedt_dependent(),
                        // must be an input
                        "in" => self.vars[dep].is_input_dependent(),
                        _ => unreachable!(),
                    }
            })
        })
    }

    pub fn push_var(&mut self, var: &Tensor) {
        self.vars.insert(
            var.name().to_string(),
            EnvVar {
                layout: var.layout_ptr().clone(),
                is_algebraic: true,
                is_time_dependent: self.is_tensor_time_dependent(var),
                is_state_dependent: self.is_tensor_state_dependent(var),
                is_dstatedt_dependent: self.is_tensor_dstatedt_dependent(var),
                is_input_dependent: self.is_tensor_input_dependent(var),
                is_model_dependent: self.is_tensor_model_dependent(var),
            },
        );
    }

    pub fn push_var_blk(&mut self, var: &Tensor, var_blk: &TensorBlock) {
        self.vars.insert(
            var_blk.name().unwrap().to_string(),
            EnvVar {
                layout: var_blk.layout_ptr().clone(),
                is_algebraic: true,
                is_time_dependent: self.is_tensor_time_dependent(var),
                is_state_dependent: self.is_tensor_state_dependent(var),
                is_dstatedt_dependent: self.is_tensor_dstatedt_dependent(var),
                is_input_dependent: self.is_tensor_input_dependent(var),
                is_model_dependent: self.is_tensor_model_dependent(var),
            },
        );
    }

    pub fn get(&self, name: &str) -> Option<&EnvVar> {
        self.vars.get(name)
    }

    fn get_layout_binary_op<'s>(
        &mut self,
        left: &Ast<'s>,
        right: &Ast<'s>,
        op: &ast::Binop,
        indices: &Vec<char>,
    ) -> Option<Layout> {
        let left_layout = self.get_layout(left, indices)?;
        let right_layout = self.get_layout(right, indices)?;
        match Layout::broadcast(vec![left_layout, right_layout], Some(op.op)) {
            Ok(layout) => Some(layout),
            Err(e) => {
                self.errs.push(ValidationError::new(
                    format!("{}. Op is {}, lhs is {}, rhs is {}.", e, op.op, left, right),
                    left.span,
                ));
                None
            }
        }
    }

    fn get_layout_name(
        &mut self,
        name: &str,
        ast: &Ast,
        rhs_indices: &[char],
        lhs_indices: &[char],
        indice: Option<&Ast>,
    ) -> Option<Layout> {
        let var = self.get(name);
        if var.is_none() {
            self.errs.push(ValidationError::new(
                format!("cannot find variable {name}"),
                ast.span,
            ));
            return None;
        }
        let var = var.unwrap();
        let layout = var.layout();

        if rhs_indices.len() < layout.min_rank() {
            self.errs.push(ValidationError::new(
                format!(
                    "cannot index variable {} with {} indices. Expected at least {} indices",
                    name,
                    rhs_indices.len(),
                    layout.rank()
                ),
                ast.span,
            ));
            return None;
        }
        let mut permutation = vec![0; rhs_indices.len()];
        for i in 0..rhs_indices.len() {
            permutation[i] = match lhs_indices.iter().position(|&x| x == rhs_indices[i]) {
                Some(pos) => pos,
                None => {
                    // if we are indexing a single element then we can allow for missing indices
                    let mut allow_missing = false;
                    if let Some(indice) = indice {
                        let indice = indice.kind.as_indice().unwrap();
                        if indice.sep.is_none() || indice.last.is_none() {
                            allow_missing = true;
                        }
                    };
                    if !allow_missing {
                        self.errs.push(ValidationError::new(
                            format!(
                                "cannot find index {} in lhs indices {:?} ",
                                rhs_indices[i], lhs_indices
                            ),
                            ast.span,
                        ));
                        return None;
                    }
                    0
                }
            }
        }
        let layout_permuted = match layout.permute(permutation.as_slice()) {
            Ok(layout) => layout,
            Err(e) => {
                self.errs
                    .push(ValidationError::new(format!("{e}"), ast.span));
                return None;
            }
        };

        if let Some(indice) = indice {
            let indice = indice.kind.as_indice().unwrap();
            // we'll only support indexing dense 1D variables for now
            // a dense 1d variable could be a column vector with shape (n, 1) or a row vector with shape (n)
            // or an nd array with shape (1, 1, ..., n, 1)
            let is_one_d = layout_permuted.shape().iter().filter(|&&d| d != 1).count() == 1;
            if !is_one_d || layout_permuted.kind() != &LayoutKind::Dense {
                self.errs.push(ValidationError::new(
                    format!(
                        "can only index dense 1D variables. Variable {} has layout {}",
                        name, layout_permuted
                    ),
                    ast.span,
                ));
                return None;
            }

            // a separator without a last value is an error
            if indice.sep.is_some() && indice.last.is_none() {
                self.errs.push(ValidationError::new(
                    "range indice must have an end value".to_string(),
                    ast.span,
                ));
                return None;
            }

            // if the indice is a single integer then the resulting layout is a scalar
            if indice.sep.is_none() {
                let mut new_layout = Layout::new_scalar();
                let (first, last) =
                    if let Some(first) = Self::eval_const_integer_expr(indice.first.as_ref()) {
                        (first, first + 1)
                    } else {
                        // Dynamic integer indices (e.g. involving N) cannot be resolved at compile time.
                        // Conservatively keep dependencies for the full 1D extent.
                        let axis = layout_permuted
                            .shape()
                            .iter()
                            .position(|&d| d != 1)
                            .unwrap_or(0);
                        let dim = *layout_permuted.shape().get(axis).unwrap_or(&1);
                        (0, i64::try_from(dim).unwrap())
                    };
                new_layout.filter_deps_from(layout_permuted, first, last);
                return Some(new_layout);
            } else {
                // if the indice is a range then the resulting layout is a dense layout with shape given by the range
                // along the only non-unit dimension of the variable
                let end_expr = indice.last.as_ref().unwrap().as_ref();
                let Some(width) = Self::eval_constant_range_width(indice.first.as_ref(), end_expr)
                else {
                    self.errs.push(ValidationError::new(
                        "range indice width must be an integer constant (independent of N)"
                            .to_string(),
                        ast.span,
                    ));
                    return None;
                };
                // make sure the range is valid
                if width < 0 {
                    self.errs.push(ValidationError::new(
                        format!("invalid range indice: width {} is negative", width),
                        ast.span,
                    ));
                    return None;
                }
                let dim = usize::try_from(width).unwrap();
                let shape = layout_permuted
                    .shape()
                    .map(|&d| if d != 1 { dim } else { 1 });
                let mut new_layout = Layout::new_dense(Shape::from(shape));
                let first_const = Self::eval_const_integer_expr(indice.first.as_ref());
                if let Some(first) = first_const {
                    let last = first + width;
                    new_layout.filter_deps_from(layout_permuted, first, last);
                } else if dim != 0 {
                    // For dynamic starts (e.g. N:N+2), union dependencies over all valid windows.
                    let axis = layout_permuted
                        .shape()
                        .iter()
                        .position(|&d| d != 1)
                        .unwrap_or(0);
                    let source_dim =
                        i64::try_from(*layout_permuted.shape().get(axis).unwrap_or(&1)).unwrap();
                    let max_start = source_dim.saturating_sub(width);
                    let mut merged_layout: Option<Layout> = None;
                    for start in 0..=max_start {
                        let mut window_layout = Layout::new_dense(new_layout.shape().clone());
                        window_layout.filter_deps_from(
                            layout_permuted.clone(),
                            start,
                            start + width,
                        );
                        merged_layout = Some(match merged_layout {
                            Some(accumulated) => accumulated.union(window_layout),
                            None => window_layout,
                        });
                    }
                    if let Some(merged_layout) = merged_layout {
                        new_layout = merged_layout;
                    }
                }
                return Some(new_layout);
            }
        }

        Some(layout_permuted)
    }

    fn get_layout_call(
        &mut self,
        call: &ast::Call,
        ast: &Ast,
        indices: &Vec<char>,
    ) -> Option<Layout> {
        let layouts = call
            .args
            .iter()
            .map(|c| self.get_layout(c, indices))
            .collect::<Option<Vec<Layout>>>()?;
        match Layout::broadcast(layouts, None) {
            Ok(layout) => Some(layout),
            Err(e) => {
                self.errs
                    .push(ValidationError::new(format!("{e}"), ast.span));
                None
            }
        }
    }

    pub fn get_layout(&mut self, ast: &Ast, indices: &Vec<char>) -> Option<Layout> {
        let layout = match &ast.kind {
            AstKind::Assignment(a) => self.get_layout(a.expr.as_ref(), indices),
            AstKind::Binop(binop) => {
                self.get_layout_binary_op(binop.left.as_ref(), binop.right.as_ref(), binop, indices)
            }
            AstKind::Monop(monop) => self.get_layout(monop.child.as_ref(), indices),
            AstKind::Call(call) => self.get_layout_call(call, ast, indices),
            AstKind::CallArg(arg) => self.get_layout(arg.expression.as_ref(), indices),
            AstKind::Number(_) => Some(Layout::new_scalar()),
            AstKind::Integer(_) => Some(Layout::new_scalar()),
            AstKind::Domain(d) => Some(Layout::new_dense(Shape::zeros(1) + d.dim)),
            AstKind::Name(name) => self.get_layout_name(
                name.name,
                ast,
                &name.indices,
                indices,
                name.indice.as_ref().map(|i| i.as_ref()),
            ),
            _ => panic!("unrecognised ast node {:#?}", ast.kind),
        };
        if log_enabled!(Level::Debug) {
            let indices_str = layout.as_ref().map(|l| {
                l.explicit_indices()
                    .iter()
                    .map(|i| {
                        i.into_iter()
                            .map(|x| x.to_string())
                            .collect::<Vec<String>>()
                            .join(", ")
                    })
                    .collect::<Vec<String>>()
            });
            debug!(
                "layout for ast {} with indices {:?} is {} with indices {:?}",
                ast,
                indices,
                layout.as_ref().unwrap_or(&Layout::new_scalar()),
                indices_str.unwrap_or_default()
            );
        }
        layout
    }

    // returns a tuple of (expr_layout, elmt_layout) giving the layouts of the expression and the tensor element.)
    pub fn get_layout_tensor_elmt(
        &mut self,
        elmt: &ast::TensorElmt,
        indices: &[char],
    ) -> Option<(Layout, Layout)> {
        let expr_indices = elmt.expr.get_indices();
        // get any indices from the expression that do not appear in 'indices' and add them to 'indices' to a new vector
        let mut new_indices = indices.to_vec();
        for i in expr_indices {
            if !indices.contains(&i) && !new_indices.contains(&i) {
                new_indices.push(i);
            }
        }

        // TODO: for now we will only support contractions from 2d to 1d
        if new_indices.len() > indices.len() && (new_indices.len() != 2 || indices.len() != 1) {
            self.errs.push(ValidationError::new(
                format!(
                    "contraction only supported from 2D to 1D tensors. Got {}D to {}D",
                    new_indices.len(),
                    indices.len()
                ),
                elmt.expr.span,
            ));
            return None;
        }
        debug!(
            "calculating expr layout for tensor element with expr: {}",
            elmt.expr
        );
        let expr_layout = self.get_layout(elmt.expr.as_ref(), &new_indices)?;

        // broadcast the expression layout to the tensor rank
        // (tensor rank given by the number of indices)
        // if we have an additional index then we contract the last dimension of the expression layout to get the final layout
        let expr_layout_to_rank = if new_indices.len() > indices.len() {
            match expr_layout.contract_last_axis() {
                Ok(layout) => layout,
                Err(e) => {
                    self.errs
                        .push(ValidationError::new(format!("{e}"), elmt.expr.span));
                    return None;
                }
            }
        } else {
            expr_layout.broadcast_to_rank(indices.len())
        };

        // calculate the shape of the tensor element.
        let elmt_layout = if let Some(elmt_indices) = elmt.indices.as_ref() {
            // If there are indicies then the rank is determined by the number of indices, and the
            // shape is determined by the ranges of the indices
            // TODO: this is quite large, perhaps move to another function

            // make sure the number of indices matches the number of dimensions
            let given_indices_ast = &elmt_indices.kind.as_vector().unwrap().data;
            let given_indices: Vec<&ast::Indice> = given_indices_ast
                .iter()
                .map(|i| i.kind.as_indice().unwrap())
                .collect();
            if given_indices.len() != indices.len() {
                self.errs.push(ValidationError::new(
                    format!(
                        "number of dimensions of tensor element ({}) does not match number of dimensions of tensor ({})",
                        given_indices.len(), indices.len()
                    ),
                    elmt_indices.span,
                ));
                return None;
            }

            let mut exp_expr_shape = Shape::ones(indices.len());

            // we will use the expression shape as defaults if the range is not explicitly given
            exp_expr_shape
                .slice_mut(s![..expr_layout_to_rank.rank()])
                .assign(expr_layout_to_rank.shape());

            // calculate the shape of the tensor element from the given indices and expression shape
            let all_range_indices = given_indices.iter().all(|i| i.sep == Some(".."));
            let mut old_dim = None;
            for (i, indice) in given_indices.iter().enumerate() {
                let first = indice.first.kind.as_integer().unwrap();

                // make sure the use of the range separator is valid
                if !all_range_indices && matches!(indice.sep, Some("..")) {
                    self.errs.push(ValidationError::new(
                        "can only use range separator if all indices are ranges".to_string(),
                        given_indices_ast[i].span,
                    ));
                }
                let dim = if indice.sep.is_some() {
                    if let Some(second) = &indice.last {
                        let second = second.kind.as_integer().unwrap();
                        if second < first {
                            self.errs.push(ValidationError::new(
                                "range end must be greater than range start".to_string(),
                                given_indices_ast[i].span,
                            ));
                            return None;
                        }
                        usize::try_from(second - first).unwrap()
                    } else {
                        exp_expr_shape[i]
                    }
                } else {
                    1usize
                };

                // make sure the dimension of the range is consistent
                if all_range_indices && old_dim.is_some() && dim != old_dim.unwrap() {
                    self.errs.push(ValidationError::new(
                        "range indices must have the same dimension".to_string(),
                        given_indices_ast[i].span,
                    ));
                    return None;
                }
                old_dim = Some(dim);
                exp_expr_shape[i] = dim;
            }

            // check that the expression shape can be broadcast to the tensor element shape
            if !can_broadcast_to(&exp_expr_shape, expr_layout_to_rank.shape()) {
                self.errs.push(ValidationError::new(
                    format!(
                        "cannot broadcast expression shape {} to tensor element shape {}",
                        expr_layout_to_rank.shape(),
                        exp_expr_shape
                    ),
                    elmt.expr.span,
                ));
                return None;
            }

            // if we have all range indices, any sparse layouts will error
            if all_range_indices && expr_layout_to_rank.kind() == &LayoutKind::Sparse {
                self.errs.push(ValidationError::new(
                    "cannot use range indices with sparse expression".to_string(),
                    elmt.expr.span,
                ));
                return None;
            }
            // if we have all range indices, any diagonal layouts will error
            if all_range_indices && expr_layout_to_rank.kind() == &LayoutKind::Diagonal {
                self.errs.push(ValidationError::new(
                    "cannot use range indices with diagonal expression".to_string(),
                    elmt.expr.span,
                ));
                return None;
            }

            // if we have all range indices we can make a diagonal layout, otherwise we broadcast
            if all_range_indices {
                match Layout::new_diagonal_from(exp_expr_shape, &expr_layout_to_rank) {
                    Some(layout) => layout,
                    None => {
                        self.errs.push(ValidationError::new(
                            "when using all range indices, the expression layout must be scalar or 1D with dimension matching the range".to_string(),
                            elmt.expr.span,
                        ));
                        return None;
                    }
                }
            } else {
                expr_layout_to_rank.broadcast_to_shape(&exp_expr_shape)
            }
        } else {
            // If there are no indicies then the layout is the same as the expression layout
            expr_layout_to_rank
        };

        Some((expr_layout, elmt_layout))
    }

    pub fn current_span(&self) -> Option<StringSpan> {
        self.current_span
    }

    pub fn set_current_span(&mut self, current_span: Option<StringSpan>) {
        self.current_span = current_span;
    }

    pub fn errs(&self) -> &ValidationErrors {
        &self.errs
    }

    pub fn errs_mut(&mut self) -> &mut ValidationErrors {
        &mut self.errs
    }
}

impl Default for Env {
    fn default() -> Self {
        Self::new()
    }
}