aboutsummaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: 76b6c9f26217f25de044bfaebc373fb491010d78 (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
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
use derive_more::Display;

// Generic

pub trait TypingSignifier {
    const SIGNIFIER: &'static str;
}

#[derive(Clone, PartialEq, Display, Debug)]
#[display(".{tag} {bound} => {body}")]
pub struct CaseArm<T: std::fmt::Display> {
    pub tag: String,
    pub bound: String,
    pub body: T,
}

#[derive(Clone, PartialEq, Display, Debug)]
#[display("{name} {} {carries}", T::SIGNIFIER)]
pub struct Field<T: std::fmt::Display + TypingSignifier> {
    pub name: String,
    pub carries: T,
}

// Set layer

#[derive(Clone, PartialEq, Display, Debug)]
pub enum BuiltIn {
    Nat,
    Int,
    Float,
    Str,
    Bool,
}

#[derive(Clone, PartialEq, Display, Debug)]
pub enum Set {
    #[display("{_0}")]
    BuiltIn(BuiltIn),

    #[display("record {{ {} }}", _0.iter().map(|f| f.to_string()).collect::<Vec<_>>().join(" , "))]
    Record(Vec<Field<Set>>),

    #[display("variant [ {} ]", _0.iter().map(|f| f.to_string()).collect::<Vec<_>>().join(" | "))]
    Variant(Vec<Field<Set>>),

    #[display("set-of({_0})")]
    ClaimedSet(Instance),

    #[display("{_0}")]
    Var(String),
}

impl TypingSignifier for Set {
    const SIGNIFIER: &'static str = ":";
}

// Signature layer

#[derive(Clone, PartialEq, Display, Debug)]
#[display("({name} : {set})")]
pub struct Param {
    pub name: String,
    pub set: Set,
}

#[derive(Clone, PartialEq, Display, Debug)]
pub enum Signature {
    #[display("Set")]
    Set,

    #[display("theory {{ {} }}", _0.iter().map(|f| f.to_string()).collect::<Vec<_>>().join(" , "))]
    Theory(Vec<Field<Signature>>),

    #[display("{} -> {}", params.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(", "), codomain)]
    Ext {
        params: Vec<Param>,
        codomain: Box<Signature>,
    },

    #[display("{_0}")]
    Var(String),
}

impl TypingSignifier for Signature {
    const SIGNIFIER: &'static str = "::";
}

// Element layer

#[derive(Clone, PartialEq, Display, Debug)]
pub enum Literal {
    Nat(u64),
    Int(i64),
    Float(f64),
    Str(String),
    Bool(bool),
}

#[derive(Clone, PartialEq, Display, Debug)]
#[display(".{name} = {element}")]
pub struct ElemAssign {
    pub name: String,
    pub element: Element,
}

#[derive(Clone, PartialEq, Display, Debug)]
pub enum Element {
    #[display("{_0}")]
    Literal(Literal),

    #[display("{_0}")]
    Var(String),

    #[display("{{ {} }}", _0.iter().map(|f| f.to_string()).collect::<Vec<_>>().join(" , "))]
    Record(Vec<ElemAssign>),

    #[display("{element} .{field}")]
    Project {
        element: Box<Element>,
        field: String,
    },

    #[display("{field}. {element}")]
    Inject {
        field: String,
        element: Box<Element>,
    },

    #[display("case {} of {{ {} }}", scrutinee, arms.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(" | "))]
    Case {
        scrutinee: Box<Element>,
        arms: Vec<CaseArm<Element>>,
    },
}

// Instance layer

#[derive(Clone, PartialEq, Display, Debug)]
#[display(".{name} = {instance}")]
pub struct InstAssign {
    pub name: String,
    pub instance: Instance,
}

#[derive(Clone, PartialEq, Display, Debug)]
pub enum Instance {
    #[display("({_0} :: Set)")]
    SetCoerce(Box<Set>),

    #[display("{_0}")]
    Var(String),

    #[display("{{ {} }}", _0.iter().map(|f| f.to_string()).collect::<Vec<_>>().join(", "))]
    Record(Vec<InstAssign>),

    #[display("for {}, {body}", params.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(""))]
    For {
        params: Vec<Param>,
        body: Box<Instance>,
    },

    #[display("{instance} {}", args.iter().map(|e| e.to_string()).collect::<Vec<_>>().join(" "))]
    App {
        instance: Box<Instance>,
        args: Vec<Element>,
    },

    #[display("{instance} .{field}")]
    Project {
        instance: Box<Instance>,
        field: String,
    },

    #[display("case {} of [ {} ]", scrutinee, arms.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(" | "))]
    Case {
        scrutinee: Box<Element>,
        arms: Vec<CaseArm<Instance>>,
    },
}

// Declarations

#[derive(Clone, PartialEq, Display, Debug)]
pub enum Decl {
    #[display("let set {name} = {set}")]
    Set { name: String, set: Set },

    #[display("let element {name} : {set} = {element}")]
    Element {
        name: String,
        set: Set,
        element: Element,
    },

    #[display("let signature {name} = {signature}")]
    Signature { name: String, signature: Signature },

    #[display("let instance {name} :: {signature} = {instance}")]
    Instance {
        name: String,
        signature: Signature,
        instance: Instance,
    },
}

#[derive(Display, Debug)]
#[display("{}", _0.iter().map(|d| d.to_string()).collect::<Vec<_>>().join("\n"))]
pub struct Programme(pub Vec<Decl>);