blr-lang 0.1.0

A language implementation that provides type safe dataframes
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
use expect_test::{Expect, expect};
use test_log::test;

use super::{builder::ExprBuilder, items::ItemSource, *};

use crate::{compiler::parse, debug_alt, runtime::binary};

fn test_convert(src: &str, expect: Expect) {
    let module = parse(src).expect("source should parse");
    debug_alt!(module, "ast");
    let mut item_source = ItemSource::default();
    binary::register_binary_operator_functions(&mut item_source);
    let semantic = super::lower(module, &mut item_source, "main");
    expect.assert_debug_eq(&semantic);
}

fn test_infer(src: &str, expect: Expect) {
    let module = parse_module(src).expect("source should parse");
    let mut item_source = ItemSource::default();
    binary::register_binary_operator_functions(&mut item_source);
    let typed = super::type_infer_with_items(item_source, module);
    expect.assert_debug_eq(&typed);
}

#[test]
fn precedence_01() {
    test_convert(
        "1 + 2 * 3",
        expect![[r#"
            (mod
              (pub
                main
                (abs
                  0
                  (app
                    (itm (id 0) (symbol std::ops +))
                    (i 1)
                    (app (itm (id 2) (symbol std::ops *)) (i 2) (i 3))))
                (tsh (fn (unit) (tvar 0)) row typ (tvar 0))))
        "#]],
    );
}

#[test]
fn precedence_30() {
    test_convert(
        "let a = 1;
        let b = 2;
        let c = 3;
        a .. b >> c()",
        expect![[r#"
            (mod
              (pub
                main
                (abs
                  0
                  (app
                    (abs
                      1
                      (app
                        (abs 2 (app (abs 3 (app (var 3) (cat (var 1) (var 2)))) (i 3)))
                        (i 2)))
                    (i 1)))
                (tsh (fn (unit) (tvar 0)) row typ (tvar 0))))
        "#]],
    );
}

#[test]
fn label_unlabel() {
    test_convert(
        "{a:1,b:3}.a",
        expect![[r#"
            (mod
              (pub
                main
                (abs 0 (ulb a (prj (cat (lbl a (i 1)) (lbl b (i 3))))))
                (tsh (fn (unit) (tvar 0)) row typ (tvar 0))))
        "#]],
    );
}

#[test]
fn abstraction_0() {
    test_convert(
        "fn (x) => x",
        expect![[r#"
            (mod (pub main (abs 0 1 (var 1)) (tsh (fn (unit) (tvar 0)) row typ (tvar 0))))
        "#]],
    );
}
#[test]
fn abstraction_1() {
    test_convert(
        "fn (x, y, z) => x",
        expect![[r#"
            (mod
              (pub main (abs 0 1 2 3 (var 1)) (tsh (fn (unit) (tvar 0)) row typ (tvar 0))))
        "#]],
    );
}

#[test]
fn products_0() {
    test_convert(
        "let r = {a:1, b:1}; r.b",
        expect![[r#"
            (mod
              (pub
                main
                (abs
                  0
                  (app (abs 1 (ulb b (prj (var 1)))) (cat (lbl a (i 1)) (lbl b (i 1)))))
                (tsh (fn (unit) (tvar 0)) row typ (tvar 0))))
        "#]],
    );
}
#[test]
#[ignore]
fn polymorphic_add() {
    test_infer(
        "(mod (pub main (app (abs 0
            (app (itm 0 std_ops add) (f 1.0) (f 1.0)))
            (app (itm 0 std_ops add) (i 1) (i 1)))))",
        expect![[r#"
            Ok(
                └─┐tmd
                  └─┐tni 0  main
                    ├─┐sch
                    │ ├─ tvs
                    │ ├─ rows
                    │ ├─ evs
                    │ └─ typ flt
                    └─┐app n11
                      ├─┐abs n5
                      │ ├─┐tdv
                      │ │ ├─ var n5 v0
                      │ │ └─ typ int
                      │ └─┐app n4
                      │   ├─┐app n3
                      │   │ ├─ itm n0 0 std_ops add
                      │   │ └─ 1 n1
                      │   └─ 1 n2
                      └─┐app n10
                        ├─┐app n9
                        │ ├─ itm n6 0 std_ops add
                        │ └─ 1 n7
                        └─ 1 n8
    
                ,
            )
        "#]],
    );
}

#[test]
#[ignore]
fn infer_products_0() {
    test_infer(
        "(mod (pub main (app (abs 0 (ulb b (prj (var 0)))) (cat (lbl a (i 1)) (lbl b (i 1))))))",
        expect![[r#"
            Ok(
                └─┐tmd
                  └─┐tni 0  main
                    ├─┐sch
                    │ ├─ tvs
                    │ ├─ rows
                    │ ├─ evs
                    │ └─ typ int
                    └─┐app n9
                      ├─┐abs n3
                      │ ├─┐tdv
                      │ │ ├─ var n3 v0
                      │ │ └─┐typ
                      │ │   └─┐prd
                      │ │     └─┐row
                      │ │       └─┐cld
                      │ │         ├─┐a
                      │ │         │ └─ typ int
                      │ │         └─┐b
                      │ │           └─ typ int
                      │ └─┐ubl n2 b
                      │   └─┐prj n1
                      │     └─┐tdv
                      │       ├─ var n0 v0
                      │       └─┐typ
                      │         └─┐prd
                      │           └─┐row
                      │             └─┐cld
                      │               ├─┐a
                      │               │ └─ typ int
                      │               └─┐b
                      │                 └─ typ int
                      └─┐cat n8
                        ├─┐lbl n5 a
                        │ └─ 1 n4
                        └─┐lbl n7 b
                          └─ 1 n6
    
                ,
            )
        "#]],
    );
}

macro_rules! set {
        () => {{ BTreeSet::new() }};
        ($($ele:expr),*) => {{
            let mut tmp = BTreeSet::new();
            $(tmp.insert($ele);)*
            tmp
        }};
    }

//#[test]
//fn infers_k_combinator() {
//    let x = Var(0);
//    let y = Var(1);
//    let b = ExprBuilder::default();
//    let expr = b.funs([x, y], b.var(x));
//
//    let ty_chk = type_infer(expr).expect("Type inference to succeed");
//
//    let a = TypeVar(0);
//    let b = TypeVar(1);
//    assert_eq!(
//        ty_chk.scheme,
//        TypeScheme {
//            unbound_tys: set![a, b],
//            unbound_rows: set![],
//            evidence: vec![],
//            ty: Type::fun(Type::Var(a), Type::fun(Type::Var(b), Type::Var(a))),
//        }
//    );
//}
//
//#[test]
//fn infers_s_combinator() {
//    let x = Var(0);
//    let y = Var(1);
//    let z = Var(2);
//    let b = ExprBuilder::default();
//    let expr = b.funs(
//        [x, y, z],
//        b.app(b.app(b.var(x), b.var(z)), b.app(b.var(y), b.var(z))),
//    );
//
//    let ty_chk = type_infer(expr).expect("Type inference to succeed");
//
//    let a = TypeVar(0);
//    let b = TypeVar(1);
//    let c = TypeVar(2);
//    let x_ty = Type::fun(Type::Var(a), Type::fun(Type::Var(b), Type::Var(c)));
//    let y_ty = Type::fun(Type::Var(a), Type::Var(b));
//    assert_eq!(
//        ty_chk.scheme,
//        TypeScheme {
//            unbound_tys: set![a, b, c],
//            unbound_rows: set![],
//            evidence: vec![],
//            ty: Type::fun(x_ty, Type::fun(y_ty, Type::fun(Type::Var(a), Type::Var(c)))),
//        }
//    )
//}
//
//#[test]
//fn type_infer_fails() {
//    let x = Var(0);
//    let b = ExprBuilder::default();
//    let expr = b.locals([(x, b.int(1))], b.app(b.var(x), b.int(3)));
//
//    let ty_chk_res = type_infer(expr);
//
//    assert_eq!(
//        ty_chk_res,
//        Err(TypeError {
//            kind: TypeErrorKind::TypeNotEqual((
//                Type::fun(Type::Int, Type::Unifier(TypeUniVar { id: 1 })),
//                Type::Int
//            )),
//            node_id: NodeId(1)
//        })
//    );
//}

//#[test]
//#[ignore]
//fn test_wand_combinator() {
//    let b = ExprBuilder::default();
//    let expr = b.make_funs(|[m, n]| b.unlabel(b.project(b.concatenate(b.var(m), b.var(n))), "x"));
//
//    let ty_chk = type_infer(expr).expect("Type inference to succeed");
//
//    let m = RowVar(0);
//    let n = RowVar(1);
//    let goal = RowVar(2);
//    let a = TypeVar(0);
//    assert_eq!(
//        ty_chk.scheme,
//        TypeScheme {
//            unbound_rows: set![n, RowVar(3), m, goal],
//            unbound_tys: set![a],
//            evidence: vec![
//                Evidence::RowEquation {
//                    left: Row::Open(m),
//                    right: Row::Open(n),
//                    goal: Row::Open(goal)
//                },
//                Evidence::RowEquation {
//                    left: Row::Closed(single_row("x", Type::Var(a))),
//                    right: Row::Open(RowVar(3)),
//                    goal: Row::Open(goal)
//                }
//            ],
//            ty: Type::fun(
//                Type::Prod(Row::Open(m)),
//                Type::fun(Type::Prod(Row::Open(n)), Type::Var(a))
//            )
//        }
//    );
//}

#[test]
fn type_check_items() {
    let b = ExprBuilder::default();
    let expr = b.make_funs(|[m, n]| b.unlabel(b.project(b.concatenate(b.var(m), b.var(n))), "x"));

    let r = RowVar(0);
    let s = RowVar(1);
    let w = RowVar(2);
    let a = TypeVar(0);
    let scheme = TypeScheme {
        unbound_rows: set![r, s, w, RowVar(3)],
        unbound_tys: set![a],
        evidence: vec![
            Evidence::RowEquation {
                left: Row::Open(r),
                right: Row::Open(s),
                goal: Row::Open(w),
            },
            Evidence::RowEquation {
                left: Row::single("x", Type::Var(a)),
                right: Row::Open(RowVar(3)),
                goal: Row::Open(w),
            },
        ],
        typ: Type::abstraction(
            Type::Prod(Row::Open(r)),
            Type::abstraction(Type::Prod(Row::Open(s)), Type::Var(a)),
        ),
    };

    let out = type_check(expr, scheme);

    assert!(out.is_ok());
}

#[test]
fn type_check_item_fails() {
    let a = TypeVar(0);

    let b = ExprBuilder::default();
    let expr = b.make_funs(|[x, y]| b.app(b.var(y), b.var(x)));
    let signature = TypeScheme {
        unbound_tys: set![a],
        unbound_rows: set![],
        evidence: vec![],
        typ: Type::abstraction(
            Type::Var(a),
            Type::abstraction(Type::abstraction(Type::Int, Type::Int), Type::Int),
        ),
    };

    let out = type_check(expr, signature);

    assert_eq!(
        out,
        Err(TypeError {
            kind: TypeErrorKind::TypeNotEqual((Type::Var(a), Type::Int)),
            node_id: NodeId(0),
        })
    );
}

#[test]
fn product_as_parameter() {
    let module = parse_module(
        "(mod (pub main (app (abs 0 (cat (lbl a (ulb x (prj (var 0)))) (lbl b (ulb y (prj (var 0)))))) (cat (lbl x (i 2)) (lbl y (i 1)))) (tsh (prd (cld a (int) b (int))) row typ)))",
    )
    .unwrap();

    let out = type_infer_with_items(ItemSource::default(), module);
    expect![[r#"
        Ok(
            (typed_mod
              ((id 4)
                (pub
                  main
                  (app
                    (abs
                      (var 0)
                      (cat (lbl a (ulb x (prj (var 0)))) (lbl b (ulb y (prj (var 0))))))
                    (cat (lbl x (i 2)) (lbl y (i 1))))
                  (tsh (prd (cld a (int) b (int))) row typ)
                  row_to_ev
                  ((nid 1) (equation (cld x (int)) (cld y (int)) (cld x (int) y (int))))
                  ((nid 5) (equation (cld y (int)) (cld x (int)) (cld x (int) y (int))))
                  ((nid 8) (equation (cld a (int)) (cld b (int)) (cld a (int) b (int))))
                  ((nid 14) (equation (cld x (int)) (cld y (int)) (cld x (int) y (int))))
                  branch_to_ret
                  wrappers))),
        )
    "#]]
    .assert_debug_eq(&out);
}

#[test]
fn product_parameter_func() {
    let module = parse_module(
        "(mod (pub main (app (abs 0 1 (app (var 0) (var 1))) (abs 0 (cat (lbl a (ulb x (prj (var 0)))) (lbl b (ulb y (prj (var 0)))))) (cat (lbl x (i 2)) (lbl y (i 1)))) (tsh (prd (cld a (int) b (int))) row typ)))",
    )
    .unwrap();

    let out = type_infer_with_items(ItemSource::default(), module);
    expect![[r#"
        Ok(
            (typed_mod
              ((id 4)
                (pub
                  main
                  (app
                    (abs (var 0) (var 1) (app (var 0) (var 1)))
                    (abs
                      (var 0)
                      (cat (lbl a (ulb x (prj (var 0)))) (lbl b (ulb y (prj (var 0))))))
                    (cat (lbl x (i 2)) (lbl y (i 1))))
                  (tsh (prd (cld a (int) b (int))) row typ)
                  row_to_ev
                  ((nid 6) (equation (cld x (int)) (cld y (int)) (cld x (int) y (int))))
                  ((nid 10) (equation (cld y (int)) (cld x (int)) (cld x (int) y (int))))
                  ((nid 13) (equation (cld a (int)) (cld b (int)) (cld a (int) b (int))))
                  ((nid 19) (equation (cld x (int)) (cld y (int)) (cld x (int) y (int))))
                  branch_to_ret
                  wrappers))),
        )
    "#]]
    .assert_debug_eq(&out);
}