grabapl 0.0.4

A library for graph-based programming languages, with pluggable type systems and a focus on visible intermediate states.
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
use grabapl::operation::query::{BuiltinQuery, ConcreteQueryOutput};
use grabapl::operation::signature::parameter::{
    AbstractOperationOutput, GraphWithSubstitution, OperationOutput, OperationParameter,
};
use grabapl::operation::signature::parameterbuilder::OperationParameterBuilder;
use grabapl::operation::{BuiltinOperation, ConcreteData};
use grabapl::semantics::{
    AbstractGraph, AbstractJoin, AbstractMatcher, ConcreteGraph, ConcreteToAbstract,
};
use grabapl::{Semantics, SubstMarker};
use std::collections::HashMap;

pub mod interval {
    /// Inclusive i32 interval. Empty interval is represented by start > end.
    #[derive(Clone, Copy, derive_more::Debug, PartialEq, Eq)]
    #[debug("[{start}, {end}]")]
    pub struct Interval {
        pub start: i32,
        pub end: i32,
    }

    impl Interval {
        pub fn any() -> Self {
            Interval {
                start: i32::MIN,
                end: i32::MAX,
            }
        }

        pub fn new_singleton(value: i32) -> Self {
            Interval {
                start: value,
                end: value,
            }
        }

        pub fn new(start: i32, end: i32) -> Self {
            Interval { start, end }
        }

        pub fn union(&self, other: &Self) -> Self {
            Interval {
                start: self.start.min(other.start),
                end: self.end.max(other.end),
            }
        }

        pub fn intersection(&self, other: &Self) -> Self {
            Interval {
                start: self.start.max(other.start),
                end: self.end.min(other.end),
            }
        }

        pub fn contains(&self, value: i32) -> bool {
            self.start <= value && value <= self.end
        }

        pub fn contains_interval(&self, other: &Self) -> bool {
            self.start <= other.start && self.end >= other.end
        }

        pub fn is_empty(&self) -> bool {
            self.start > self.end
        }
    }
}

pub struct IntervalSemantics;

pub struct NodeMatcher;
impl AbstractMatcher for NodeMatcher {
    type Abstract = NodeType;

    fn matches(argument: &Self::Abstract, parameter: &Self::Abstract) -> bool {
        parameter.0.contains_interval(&argument.0)
    }
}

pub struct EdgeMatcher;
impl AbstractMatcher for EdgeMatcher {
    type Abstract = EdgeType;

    fn matches(argument: &Self::Abstract, parameter: &Self::Abstract) -> bool {
        true
    }
}

pub struct NodeJoiner;
impl AbstractJoin for NodeJoiner {
    type Abstract = NodeType;

    fn join(a: &Self::Abstract, b: &Self::Abstract) -> Option<Self::Abstract> {
        Some(NodeType(a.0.union(&b.0)))
    }
}

pub struct EdgeJoiner;
impl AbstractJoin for EdgeJoiner {
    type Abstract = EdgeType;

    fn join(a: &Self::Abstract, b: &Self::Abstract) -> Option<Self::Abstract> {
        Some(EdgeType)
    }
}

pub struct NodeConcreteToAbstract;
impl ConcreteToAbstract for NodeConcreteToAbstract {
    type Concrete = NodeValue;
    type Abstract = NodeType;

    fn concrete_to_abstract(c: &Self::Concrete) -> Self::Abstract {
        NodeType(interval::Interval::new_singleton(c.0))
    }
}

pub struct EdgeConcreteToAbstract;
impl ConcreteToAbstract for EdgeConcreteToAbstract {
    type Concrete = EdgeValue;
    type Abstract = EdgeType;

    fn concrete_to_abstract(c: &Self::Concrete) -> Self::Abstract {
        EdgeType
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NodeType(pub interval::Interval);

impl NodeType {
    pub fn any() -> Self {
        NodeType(interval::Interval::any())
    }

    pub fn new_singleton(value: i32) -> Self {
        NodeType(interval::Interval::new_singleton(value))
    }

    pub fn new(start: i32, end: i32) -> Self {
        NodeType(interval::Interval::new(start, end))
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NodeValue(pub i32);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct EdgeType;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct EdgeValue;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TestOperation {
    NoOp,
    SetTo {
        op_typ: NodeType,
        target_typ: NodeType,
        value: NodeValue,
    },
    AddEdge {
        node_typ: NodeType,
    },
    AddNode {
        node_type: NodeType,
        value: NodeValue,
    },
    CopyValueFromTo,
    SwapValues,
    DeleteNode,
    DeleteEdge,
    AddInteger(i32),
}

impl BuiltinOperation for TestOperation {
    type S = IntervalSemantics;

    fn parameter(&self) -> OperationParameter<Self::S> {
        let mut param_builder = OperationParameterBuilder::new();
        match self {
            TestOperation::NoOp => {
                param_builder
                    .expect_explicit_input_node("input", NodeType::any())
                    .unwrap();
            }
            TestOperation::SetTo {
                op_typ,
                target_typ,
                value,
            } => {
                param_builder
                    .expect_explicit_input_node("target", *op_typ)
                    .unwrap();
            }
            TestOperation::AddEdge { node_typ } => {
                param_builder
                    .expect_explicit_input_node("src", *node_typ)
                    .unwrap();
                param_builder
                    .expect_explicit_input_node("dst", *node_typ)
                    .unwrap();
            }
            TestOperation::AddNode { node_type, value } => {}
            TestOperation::CopyValueFromTo => {
                param_builder
                    .expect_explicit_input_node("source", NodeType::any())
                    .unwrap();
                param_builder
                    .expect_explicit_input_node("destination", NodeType::any())
                    .unwrap();
            }
            TestOperation::SwapValues => {
                param_builder
                    .expect_explicit_input_node("source", NodeType::any())
                    .unwrap();
                param_builder
                    .expect_explicit_input_node("destination", NodeType::any())
                    .unwrap();
            }
            TestOperation::DeleteNode => {
                param_builder
                    .expect_explicit_input_node("target", NodeType::any())
                    .unwrap();
            }
            TestOperation::DeleteEdge => {
                param_builder
                    .expect_explicit_input_node("src", NodeType::any())
                    .unwrap();
                param_builder
                    .expect_explicit_input_node("dst", NodeType::any())
                    .unwrap();
                param_builder
                    .expect_edge(SubstMarker::from("src"), SubstMarker::from("dst"), EdgeType)
                    .unwrap();
            }
            TestOperation::AddInteger(i) => {
                // TODO: expect a max of i32::MAX - 1 here? due to overflow
                param_builder
                    .expect_explicit_input_node("target", NodeType::any())
                    .unwrap();
            }
        }
        param_builder.build().unwrap()
    }

    fn apply_abstract(
        &self,
        g: &mut GraphWithSubstitution<AbstractGraph<Self::S>>,
    ) -> AbstractOperationOutput<Self::S> {
        let mut new_node_names = HashMap::new();
        match self {
            TestOperation::NoOp => {
                // No operation, so no changes to the abstract graph.
            }
            TestOperation::SetTo {
                op_typ,
                target_typ,
                value,
            } => {
                // Set the abstract value of the node to the specified type.
                g.set_node_value(SubstMarker::from("target"), *target_typ)
                    .unwrap();
            }
            TestOperation::AddEdge { node_typ } => {
                // Add an edge from source to destination with the specified type.
                g.add_edge(SubstMarker::from("src"), SubstMarker::from("dst"), EdgeType);
            }
            TestOperation::AddNode { node_type, value } => {
                // Add a new node with the specified type and value.
                g.add_node("new", node_type.clone());
                new_node_names.insert("new".into(), "new".into());
            }
            TestOperation::CopyValueFromTo => {
                // Copy the value from one node to another.
                let value = g.get_node_value(SubstMarker::from("source")).unwrap();
                g.set_node_value(SubstMarker::from("destination"), value.clone())
                    .unwrap();
            }
            TestOperation::SwapValues => {
                // Swap the values of two nodes.
                let value1 = g.get_node_value(SubstMarker::from("source")).unwrap();
                let value2 = g.get_node_value(SubstMarker::from("destination")).unwrap();
                let v1 = value1.clone();
                let v2 = value2.clone();
                g.set_node_value(SubstMarker::from("source"), v2).unwrap();
                g.set_node_value(SubstMarker::from("destination"), v1)
                    .unwrap();
                // TODO: talk about above problem in user defined op.
                //  Specifically: We take two objects as input, and swap them. Nothing guarantees that the most precise
                //  types of the two nodes are the same, hence the valid inferred signature (if this were an user defined op) would be
                //  that both nodes end up as type Object. However, because this is builtin, it can actually in a sense
                //  "look at" the real, most precise values of the nodes, and just say that it swaps those.
                //  If we didn't have this, we'd need monomorphized swapvaluesInt etc operations, or support for generics.
            }
            TestOperation::DeleteNode => {
                // Delete the node.
                g.delete_node(SubstMarker::from("target")).unwrap();
            }
            TestOperation::DeleteEdge => {
                // Delete the edge from source to destination.
                g.delete_edge(SubstMarker::from("src"), SubstMarker::from("dst"))
                    .unwrap();
            }
            TestOperation::AddInteger(i) => {
                // Add an integer to the node.
                let &NodeType(old_interval) =
                    g.get_node_value(SubstMarker::from("target")).unwrap();
                let new_type = NodeType(interval::Interval::new(
                    old_interval.start + *i,
                    old_interval.end + *i,
                ));
                g.set_node_value(SubstMarker::from("target"), new_type)
                    .unwrap();
            }
        }
        g.get_abstract_output(new_node_names)
    }

    fn apply(
        &self,
        g: &mut GraphWithSubstitution<ConcreteGraph<Self::S>>,
        _: &mut ConcreteData,
    ) -> OperationOutput {
        let mut new_node_names = HashMap::new();
        match self {
            TestOperation::NoOp => {
                // No operation, so no changes to the concrete graph.
            }
            TestOperation::SetTo {
                op_typ,
                target_typ,
                value,
            } => {
                // Set the concrete value of the node to the specified value.
                g.set_node_value(SubstMarker::from("target"), value.clone())
                    .unwrap();
            }
            TestOperation::AddEdge { node_typ } => {
                // Add an edge from source to destination with the specified value.
                g.add_edge(
                    SubstMarker::from("src"),
                    SubstMarker::from("dst"),
                    EdgeValue,
                );
            }
            TestOperation::AddNode { node_type, value } => {
                // Add a new node with the specified type and value.
                g.add_node("new", value.clone());
                new_node_names.insert("new".into(), "new".into());
            }
            TestOperation::CopyValueFromTo => {
                // Copy the value from one node to another.
                let value = g.get_node_value(SubstMarker::from("source")).unwrap();
                g.set_node_value(SubstMarker::from("destination"), value.clone())
                    .unwrap();
            }
            TestOperation::SwapValues => {
                // Swap the values of two nodes.
                let value1 = g.get_node_value(SubstMarker::from("source")).unwrap();
                let value2 = g.get_node_value(SubstMarker::from("destination")).unwrap();
                let v1 = value1.clone();
                let v2 = value2.clone();
                g.set_node_value(SubstMarker::from("source"), v2).unwrap();
                g.set_node_value(SubstMarker::from("destination"), v1)
                    .unwrap();
            }
            TestOperation::DeleteNode => {
                // Delete the node.
                g.delete_node(SubstMarker::from("target")).unwrap();
            }
            TestOperation::DeleteEdge => {
                // Delete the edge from source to destination.
                g.delete_edge(SubstMarker::from("src"), SubstMarker::from("dst"))
                    .unwrap();
            }
            TestOperation::AddInteger(i) => {
                // Add an integer to the node.
                let &NodeValue(old_value) = g.get_node_value(SubstMarker::from("target")).unwrap();
                let new_value = NodeValue(*i + old_value);
                g.set_node_value(SubstMarker::from("target"), new_value)
                    .unwrap();
            }
        }
        g.get_concrete_output(new_node_names)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TestQuery {
    ValuesEqual,
    ValueEqualTo(NodeValue),
    CmpFstSnd(std::cmp::Ordering),
}

impl BuiltinQuery for TestQuery {
    type S = IntervalSemantics;

    fn parameter(&self) -> OperationParameter<Self::S> {
        let mut param_builder = OperationParameterBuilder::new();
        match self {
            TestQuery::ValuesEqual => {
                param_builder
                    .expect_explicit_input_node("a", NodeType::any())
                    .unwrap();
                param_builder
                    .expect_explicit_input_node("b", NodeType::any())
                    .unwrap();
            }
            TestQuery::ValueEqualTo(_) => {
                param_builder
                    .expect_explicit_input_node("a", NodeType::any())
                    .unwrap();
            }
            TestQuery::CmpFstSnd(_) => {
                param_builder
                    .expect_explicit_input_node("a", NodeType::any())
                    .unwrap();
                param_builder
                    .expect_explicit_input_node("b", NodeType::any())
                    .unwrap();
            }
        }
        param_builder.build().unwrap()
    }

    fn apply_abstract(&self, g: &mut GraphWithSubstitution<AbstractGraph<Self::S>>) {
        // does nothing, not testing side-effect-ful queries here
    }

    fn query(&self, g: &mut GraphWithSubstitution<ConcreteGraph<Self::S>>) -> ConcreteQueryOutput {
        match self {
            TestQuery::ValuesEqual => {
                let value1 = g.get_node_value(SubstMarker::from("a")).unwrap();
                let value2 = g.get_node_value(SubstMarker::from("b")).unwrap();
                ConcreteQueryOutput {
                    taken: value1 == value2,
                }
            }
            TestQuery::ValueEqualTo(value) => {
                let node_value = g.get_node_value(SubstMarker::from("a")).unwrap();
                ConcreteQueryOutput {
                    taken: node_value == value,
                }
            }
            TestQuery::CmpFstSnd(ordering) => {
                let value1 = g.get_node_value(SubstMarker::from("a")).unwrap();
                let value2 = g.get_node_value(SubstMarker::from("b")).unwrap();
                let cmp_result = match (value1, value2) {
                    (NodeValue(a), NodeValue(b)) => a.cmp(&b),
                };
                ConcreteQueryOutput {
                    taken: cmp_result == *ordering,
                }
            }
        }
    }
}

impl Semantics for IntervalSemantics {
    type NodeConcrete = NodeValue;
    type NodeAbstract = NodeType;
    type EdgeConcrete = EdgeValue;
    type EdgeAbstract = EdgeType;
    type NodeMatcher = NodeMatcher;
    type EdgeMatcher = EdgeMatcher;
    type NodeJoin = NodeJoiner;
    type EdgeJoin = EdgeJoiner;
    type NodeConcreteToAbstract = NodeConcreteToAbstract;
    type EdgeConcreteToAbstract = EdgeConcreteToAbstract;
    type BuiltinOperation = TestOperation;
    type BuiltinQuery = TestQuery;
}