aboutsummaryrefslogtreecommitdiff
path: root/src/combinators.zig
blob: d78e40ce4b1486a5da7674eee71482fc8f356c05 (plain)
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
const std = @import("std");
const typeVerify = @import("util.zig").typeVerify;

pub fn I(x: anytype) @TypeOf(x) {
    return x;
}

pub fn K(x: anytype) fn(anytype) @TypeOf(x) {
    return struct {
        fn Kinner(y: anytype) @TypeOf(x) {
            _=y;
            return x;
        }
    }.Kinner;
}

fn SHelper(comptime x: anytype) type {
    return struct {
        pub fn call(comptime y: anytype) type {
            return struct {
                pub fn call(z: anytype) @TypeOf(x(z)(y(z))) {
                    return x(z)(y(z));
                }
            };
        }
    };
}

pub fn S(comptime x: anytype) fn(anytype) fn(anytype) @TypeOf(blk: {
    const dummy = struct {
        fn d(a: anytype) @TypeOf(a) { return a; }
    }.d;
    break :blk x(dummy)(dummy);
}) {
    const Helper = SHelper(x);
    return struct {
        fn inner1(y: anytype) fn(anytype) @TypeOf(blk: {
            const dummy = struct {
                fn d(a: anytype) @TypeOf(a) { return a; }
            }.d;
            break :blk x(dummy)(y(dummy));
        }) {
            return struct {
                fn inner2(z: anytype) @TypeOf(x(z)(y(z))) {
                    return Helper.call(y).call(z);
                }
            }.inner2;
        }
    }.inner1;
}