ravenlang 0.5.0

Language core for ravencheck.
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
use crate::Comp;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;

pub fn substruct_name() -> String {
    "substruct".to_string()
}

pub fn substruct_code(t: BType) -> OpCode {
    OpCode {
        ident: substruct_name(),
        types: vec![VType::Base(t)],
        path: None,
    }
}

pub fn substruct_op() -> (String, Vec<String>, Op) {
    let tname = "T".to_string();
    let t = VType::ui(tname.clone());
    let op = Op::Symbol(PredSymbol{ inputs: vec![t.clone(), t] });
    (substruct_name(), vec![tname], op)
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OpCode {
    pub ident: String,
    pub types: Vec<VType>,
    pub path: Option<String>,
}

impl fmt::Display for OpCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = match &self.path {
            Some(p) => format!("{}::{}", p, &self.ident),
            None => format!("{}", &self.ident),
        };
        // write!(f, "{}::<", &self.ident)?;
        if self.types.len() == 1 {
            s.push_str(&format!("::<{}>", &self.types[0].render()));
            // write!(f, "{}>", &self.types[0].render())
        } else if self.types.len() > 1 {
            s.push_str(&format!("::<"));
            let mut first = true;
            for t in &self.types {
                if first {
                    s.push_str(&format!("{}", t.render()));
                    // write!(f, "{}", t.render())?;
                    first = false;
                } else {
                    s.push_str(&format!(",{}", t.render()));
                    // write!(f, ",{}", t.render())?;
                }
            }
        }
        write!(f, "{}", s)
    }
}

impl OpCode {
    pub fn special_recursive() -> Self {
        Self{
            ident: format!("special_recursive"),
            types: Vec::new(),
            path: None,
        }
    }
    pub fn fun_types<T: ToString>(
        fun_name: T,
        types: Vec<VType>,
    ) -> Self {
        Self{
            ident: fun_name.to_string(),
            types,
            path: None,
        }
    }
    pub fn enum_con<T1: ToString, T2: ToString>(
        enum_name: T1,
        types: Vec<VType>,
        constructor: T2,
    ) -> Self {
        Self{
            ident: constructor.to_string(),
            path: Some(enum_name.to_string()),
            types,
        }
    }
    pub fn get_enum_type(&self) -> Option<BType> {
        match &self.path {
            Some(path) => Some(BType::ui_args(path, self.types.clone())),
            None => None
        }
    }
}

/// A BType is a base type, which can be represented directly by a
/// sort.
///
/// Although BTypes can contain VTypes, they should never contain
/// Thunks.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BType {
    Prop,
    UI(String, Vec<VType>),
}

impl fmt::Display for BType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.render())
    }
}

impl BType {
    pub fn prop() -> Self { Self::Prop }
    pub fn ui<S: ToString>(s: S) -> Self {
        assert!(
            &s.to_string() != "bool",
            "\"bool\" should not be used as an uninterpreted type name."
        );
        Self::UI(s.to_string(), Vec::new())
    }
    pub fn ui_args<S: ToString>(s: S, args: Vec<VType>) -> Self {
        assert!(
            &s.to_string() != "bool",
            "\"bool\" should not be used as an uninterpreted type name."
        );
        assert!(
            args.iter().all(|t| !t.contains_thunk()),
            "type arguments to \"{}\" should not contain thunks",
            s.to_string(),
        );
        Self::UI(s.to_string(), args)
    }
    pub fn render(&self) -> String {
        match self {
            BType::Prop => format!("bool"),
            BType::UI(name, args) if args.len() == 0 => {
                format!("{}", name)
            }
            BType::UI(name, args) => {
                let mut out = format!("{}<", name);
                let mut first = true;
                for t in args {
                    if first {
                        first = false;
                        out.push_str(&t.render());
                    } else {
                        out.push_str(&format!(", {}", t.render()));
                    }
                }
                out.push_str(">");
                out
            }
        }
    }
    pub fn contains_prop(&self) -> bool {
        match self {
            BType::Prop => true,
            // We don't care if the type args contain prop
            BType::UI(_, _args) => false,
        }
    }
    pub fn contains_ui(&self, name: &str) -> bool {
        match self {
            BType::Prop => false,
            BType::UI(n, args) => {
                n == name || args.iter().any(|t| t.contains_ui(name))
            }
        }
    }
    pub fn contains_thunk(&self) -> bool {
        match self {
            BType::Prop => false,
            BType::UI(_, args) => args.iter().any(|t| t.contains_thunk()),
        }
    }
    pub fn expand_aliases(self, aliases: &HashMap<String,VType>) -> VType {
        match self {
            BType::Prop => VType::Base(BType::Prop),
            BType::UI(s, args) => match aliases.get(&s) {
                Some(t) => {
                    assert!(args.len() == 0, "Aliases must have zero arity, but {} with arity {} was aliased.", s, args.len());
                    t.clone()
                }
                None => VType::Base(BType::UI(
                    s,
                    args.into_iter().map(|t| t.expand_aliases(aliases)).collect(),
                )),
            }
        }
    }
    pub fn get_ta(self) -> Option<String> {
        match self {
            BType::UI(name,args) if args.len() == 0 => {
                Some(name)
            }
            _ => None,
        }
    }
}

/// A VType is a base type or a tuple
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum VType {
    Base(BType),
    Tuple(Vec<VType>),
    Thunk(Box<CType>),
}

impl VType {
    pub fn contains_prop(&self) -> bool {
        match self {
            VType::Base(t) => t.contains_prop(),
            VType::Tuple(vs) => vs.iter().any(|t| t.contains_prop()),
            vt => panic!("no contains_prop for {:?}", vt),
        }
    }
    pub fn contains_ui(&self, s: &str) -> bool {
        match self {
            VType::Base(t) => t.contains_ui(s),
            VType::Tuple(ts) => {
                ts.iter().map(|t| t.contains_ui(s)).any(|r| r)
            }
            VType::Thunk(ct) => ct.contains_ui(s),
        }
    }
    pub fn expand_aliases(self, aliases: &HashMap<String,Self>) -> Self {
        match self {
            VType::Base(t) => t.expand_aliases(aliases),
            VType::Tuple(ts) => VType::Tuple(
                ts.into_iter().map(|t| t.expand_aliases(aliases)).collect()
            ),
            VType::Thunk(ct) =>
                VType::Thunk(Box::new(ct.expand_aliases(aliases))),
        }
    }
    pub fn render(&self) -> String {
        match self.clone() {
            VType::Base(t) => t.render(),
            VType::Tuple(ts) => {
                let mut out = String::from("(");
                let mut first = true;
                for t in ts {
                    if first {
                        first = false;
                        out.push_str(&t.render());
                    } else {
                        out.push_str(&format!(", {}", t.render()));
                    }
                }
                out.push_str(")");
                out
            }
            VType::Thunk(c) => format!("Thunk({})", c.render()),
        }
    }
    pub fn contains_thunk(&self) -> bool {
        match self {
            VType::Base(t) => t.contains_thunk(),
            VType::Tuple(ts) => {
                for t in ts {
                    if t.contains_thunk() {
                        return true
                    }
                }
                false
            }
            VType::Thunk(_ct) => true,
        }
    }
    pub fn unwrap_base(self) -> Result<BType,Self> {
        match self {
            VType::Base(t) => Ok(t),
            t => Err(t),
        }
    }
    pub fn flatten(self) -> Vec<Self> {
        let mut out = Vec::new();
        match self {
            Self::Base(t) => {
                out.push(Self::Base(t));
            }
            Self::Tuple(ts) => {
                for t in ts {
                    let mut v = t.flatten();
                    out.append(&mut v);
                }
            }
            vt => panic!("Can't flatten {:?}", vt),
        }
        out
    }
    pub fn flatten_many(ts: Vec<Self>) -> Vec<Self> {
        let mut out = Vec::new();
        for t in ts {
            out.append(&mut t.flatten());
        }
        out
    }

    pub fn tuple<V: Into<Vec<VType>>>(v: V) -> Self {
        Self::Tuple(v.into())
    }

    pub fn ui<T: ToString>(s: T) -> Self {
        Self::Base(BType::ui(s))
    }

    pub fn ui_args<T: ToString>(s: T, args: Vec<VType>) -> Self {
        Self::Base(BType::ui_args(s, args))
    }

    pub fn unit() -> Self {
        Self::Tuple(Vec::new())
    }

    pub fn fun_v<V: Into<Vec<VType>>>(inputs: V, output: VType) -> Self {
        Self::Thunk(Box::new(CType::Fun(
            inputs.into(),
            Box::new(CType::Return(output)),
        )))
    }
    pub fn unwrap_fun_v(self) -> Option<(Vec<VType>, VType)> {
        match self {
            Self::Thunk(ct) => match *ct {
                CType::Fun(vts, ct) => match *ct {
                    CType::Return(vt) => Some((vts, vt)),
                    _ => None,
                }
                _ => None,
            }
            _ => None,
        }
    }
    pub fn prop() -> Self {
        Self::Base(BType::prop())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CType {
    Fun(Vec<VType>, Box<CType>),
    Return(VType),
}

impl CType {
    pub fn render(&self) -> String {
        match self {
            Self::Fun(vs, c) => {
                let mut s: String = format!("Fn(");
                let mut first = true;
                for t in vs {
                    if first {
                        s.push_str(&format!("{}", t.render()));
                        first = false;
                    } else {
                        s.push_str(&format!(",{}", t.render()));
                    }
                }
                s.push_str(&format!(") -> {}", c.render()));
                s
            }
            Self::Return(vt) => format!("Return({})", vt.render()),
        }
    }
    pub fn return_prop() -> Self {
        Self::Return(VType::prop())
    }
    pub fn fun(ts: Vec<VType>, m: CType) -> Self {
        CType::Fun(ts, Box::new(m))
    }
    pub fn unwrap_fun_v(self) -> Option<(Vec<VType>, VType)> {
        match self {
            CType::Return(v) => v.unwrap_fun_v(),
            _ => None,
        }
    }
    pub fn expand_aliases(self, aliases: &HashMap<String,VType>) -> Self {
        match self {
            Self::Fun(vts, ct) => Self::fun(
                vts.into_iter().map(|t| t.expand_aliases(aliases)).collect(),
                ct.expand_aliases(aliases),
            ),
            Self::Return(vt) => Self::Return(vt.expand_aliases(aliases)),
        }
    }
    pub fn contains_ui(&self, s: &str) -> bool {
        match self {
            Self::Fun(vts, ct) => {
                let in_args = vts.iter().map(|t| t.contains_ui(s)).any(|r| r);
                let in_body = ct.contains_ui(s);
                in_args || in_body
            }
            Self::Return(vt) => vt.contains_ui(s),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstOp {
    pub vtype: VType,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecOp {
    pub inputs: Vec<VType>,
    pub output: VType,
    pub axioms: Vec<Comp>,
    pub def: Comp,
}

impl RecOp {
    pub fn as_fun_op(self) -> FunOp {
        FunOp {
            inputs: self.inputs,
            output: self.output,
            axioms: self.axioms,
        }
    }

    pub fn annotation_type(&self) -> CType {
        CType::Return(VType::fun_v(
            self.inputs.clone(),
            VType::fun_v(
                [self.output.clone()],
                VType::prop(),
            )
        ))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunOp {
    pub inputs: Vec<VType>,
    pub output: VType,
    pub axioms: Vec<Comp>,
}

impl FunOp {
    pub fn annotation_type(&self) -> CType {
        CType::Return(VType::fun_v(
            self.inputs.clone(),
            VType::fun_v(
                [self.output.clone()],
                VType::prop(),
            )
        ))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PredOp {
    pub inputs: Vec<VType>,
    pub axioms: Vec<Comp>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PredSymbol {
    pub inputs: Vec<VType>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Op {
    Const(ConstOp),
    Direct(Comp),
    Fun(FunOp),
    Pred(PredOp),
    Rec(RecOp),
    Symbol(PredSymbol),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InstRule {
    pub left: BType,
    pub right: Vec<VType>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InstMode {
    Code(String),
    Rules(Vec<InstRule>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Axiom {
    pub tas: Vec<String>,
    pub inst_mode: InstMode,
    pub body: Comp,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeDef {
    Alias(VType),
    Enum(HashMap<String, Vec<VType>>),
    Uninterpreted,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Sig {
    pub type_defs: HashMap<String,(Vec<String>, TypeDef)>,
    // The Vec<String> is the list of type parameters, which act as
    // aliases on the types in the Op.
    pub ops: Vec<(String, Vec<String>, Op)>,
    // Note that axioms here should already be in normal form.
    pub axioms: Vec<Axiom>,
    // OpCodes that should trigger the recursive flag and force
    // assumption of the Inductive Hypothesis, in a recursive
    // annotation problem.
    pub recs: Option<HashSet<OpCode>>,
    // Base types that are inducted upon in a recursive annotation
    // problem, and thus should have their substruct relation defined.
    pub inductive_bases: Option<HashSet<BType>>,
}

impl Sig {
    pub fn empty() -> Sig {
        Sig {
            type_defs: HashMap::new(),
            ops: vec![substruct_op()],
            axioms: Vec::new(),
            recs: Some(HashSet::new()),
            inductive_bases: None,
        }
    }
    pub fn sorts(&self) -> HashMap<String,usize> {
        let mut sorts = HashMap::new();
        for (name, (tas, def)) in &self.type_defs {
            match def {
                TypeDef::Uninterpreted => {
                    sorts.insert(name.clone(), tas.len());
                }
                _ => {},
            }
        }
        sorts
    }
    pub fn type_aliases(&self) -> HashMap<String,VType> {
        let mut aliases = HashMap::new();
        // We ignore the type abstractions for now, since aliases are
        // so-far not allowed to have any.
        for (name, (_tas, def)) in &self.type_defs {
            match def {
                TypeDef::Alias(t) => {
                    aliases.insert(name.clone(), t.clone());
                }
                _ => {},
            }
        }
        aliases
    }
    pub fn sorts_insert(&mut self, s: String, arity: usize) {
        assert!(
            !self.type_defs.contains_key(&s),
            "You tried to define type {}, but it was already defined",
            s,
        );
        let mut tas = Vec::new();
        for n in 0..arity {
            tas.push(format!("T{}", n));
        }
        self.type_defs.insert(s, (tas, TypeDef::Uninterpreted));
    }
    pub fn sorts_get(&mut self, s: &str) -> Option<usize> {
        match self.type_defs.get(s) {
            Some((tas, TypeDef::Uninterpreted)) => Some(tas.len()),
            _ => None,
        }
    }
    pub fn type_aliases_insert(&mut self, s: String, t: VType) {
        assert!(
            !self.type_defs.contains_key(&s),
            "You tried to define type {}, but it was already defined",
            s,
        );
        self.type_defs.insert(s, (Vec::new(), TypeDef::Alias(t)));
    }
    pub fn type_aliases_get(&mut self, s: &str) -> Option<&VType> {
        match self.type_defs.get(s) {
            Some((_tas, TypeDef::Alias(t))) => Some(t),
            _ => None,
        }
    }
    pub fn type_sums_insert(
        &mut self,
        s: String,
        tas: Vec<String>,
        variants: HashMap<String, Vec<VType>>,
    ) {
        assert!(
            !self.type_defs.contains_key(&s),
            "You tried to define type {}, but it was already defined",
            s,
        );
        let def = TypeDef::Enum(variants);
        // println!("Defined sum {} with tas {:?} as {:?}", &s, tas, def);
        self.type_defs.insert(s, (tas, def));
    }
    pub fn get_op(&self, s: &str) -> Option<(&Vec<String>, &Op)> {
        for (name, tas, op) in self.ops.iter() {
            if name == s {
                return Some((tas, op))
            }
        }
        None
    }
    pub fn get_tas(&self, s: &str) -> Option<&Vec<String>> {
        for (name, tas, _) in self.ops.iter() {
            if name == s {
                return Some(tas);
            }
        }
        None
    }
    pub fn get_op_input_types(&self, s: &str) -> Option<&Vec<VType>> {
        match self.get_op(s)? {
            (_, Op::Fun(op)) => Some(&op.inputs),
            (_, op) => todo!("get_op_input_types for {:?}", op),
        }
    }
    pub fn type_arity(&self, s: &str) -> Option<usize> {
        match self.type_defs.get(s) {
            // For now, type aliases are not considered definitions
            // like the others.
            Some((_tas, TypeDef::Alias(_))) => None,
            Some((tas, _)) => Some(tas.len()),
            None => None,
        }
    }
    pub fn all_op_names(&self) -> Vec<String> {
        self.ops_map().clone().into_iter().map(|(k,_)| k).collect()
    }
    pub fn ops_map(&self) -> HashMap<String, (Vec<String>,Op)> {
        let mut m = HashMap::new();
        for (n,args,o) in self.ops.clone() {
            m.insert(n,(args,o));
        }
        m
    }
    pub fn ops_vec(&self) -> Vec<(String, Vec<String>, Op)> {
        self.ops.clone()
    }
    pub fn add_sort<S: ToString>(&mut self, s: S) {
        self.sorts_insert(s.to_string(), 0);
    }
    pub fn add_type_con<S: ToString>(&mut self, s: S, arity: usize) {
        self.sorts_insert(s.to_string(), arity);
    }
    pub fn add_alias<S1: ToString>(&mut self, s: S1, t: VType) {
        let s = s.to_string();
        assert!(
            !t.contains_ui(&s),
            "Recursive type alias \"{}\" is not allowed",
            s,
        );
        if let Err(e) = t.validate(self, &Vec::new()) {
            panic!(
                "right side {} of type alias '{}' is not valid: {}",
                t.render(),
                s,
                e,
            )
        }
        self.type_aliases_insert(s, t);
    }
    pub fn add_constant<S1: ToString, S2: ToString>(
        &mut self,
        name: S1,
        sort: S2,
    ) {
        assert!(self.type_arity(&sort.to_string()).is_some());
        self.ops.push((
            name.to_string(),
            Vec::new(),
            Op::Const(ConstOp{
                vtype: VType::ui(sort.to_string()),
            }))
        );
    }
    pub fn add_relation<S1: ToString, S2: ToString, const N: usize>(
        &mut self,
        name: S1,
        inputs: [S2; N],
    ) {
        for i in inputs.iter() {
            // In this one case, since we perform alias exapansion
            // afterwards, we check whether i is a defined sort or
            // alias.
            assert!(
                self.type_defs.contains_key(&i.to_string()),
                "{} is not a declared sort",
                i.to_string(),
            );
        }
        let op = Op::Symbol(PredSymbol{
            inputs: inputs
                .into_iter()
                .map(|s| VType::ui(s).expand_aliases(&self.type_aliases()))
                .collect(),
        });
        self.ops.push((name.to_string(), Vec::new(), op));
    }
    pub fn add_relation_t<S1: ToString, const N: usize>(
        &mut self,
        name: S1,
        inputs: [VType; N],
    ) {
        for i in inputs.iter() {
            assert!(i.validate(self, &Vec::new()) == Ok(()));
        }
        let op = Op::Symbol(PredSymbol{
            inputs: inputs
                .into_iter()
                .map(|t| t.expand_aliases(&self.type_aliases()))
                .collect(),
        });
        self.ops.push((name.to_string(), Vec::new(), op));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn vtype_render1() {
        assert_eq!(
            &VType::unit().render(),
            "()",
        );
        assert_eq!(
            &VType::tuple([VType::ui("u32"), VType::ui("u32"), VType::prop()]).render(),
            "(u32, u32, bool)",
        );
        assert_eq!(
            &VType::tuple([
                VType::ui("u32"),
                VType::tuple([
                    VType::ui("Set_u32"),
                    VType::prop(),
                ]),
                VType::prop(),
            ]).render(),
            "(u32, (Set_u32, bool), bool)",
        );
    }
}