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
|
namespace Eva {
public interface List : Term {
private static Empty _empty;
public static List empty {
get {
if(_empty == null)
_empty = new Empty();
return _empty;
}
}
public abstract bool is_empty { get; }
public static List from_list(Gee.List<Term> list) {
if(list.is_empty)
return empty;
else
return new Cons(list.first(), List.from_list(list[1:list.size]));
}
private class Empty : Object, Term, List {
internal Empty() {}
public bool is_empty {
get {
return true;
}
}
public string to_string() {
return "[]";
}
protected bool do_match(Term o, Gee.Map<string, Term> vars, Gee.Map<string, string> aliases) {
if(o is Var) {
return o.do_match(this, vars, aliases);
}
else {
return (o == this);
}
}
internal void encode(Buffer buffer) {
buffer.buffer.encode_empty_list();
}
}
}
public class Cons : Object, Term, List {
public Term head { get; construct;}
public Term tail { get; construct;}
public bool is_empty {
get {
return false;
}
}
public Cons(Term head0, Term tail0 = empty) {
Object(head: head0, tail: tail0);
}
protected bool do_match(Term o, Gee.Map<string, Term> vars, Gee.Map<string, string> aliases) {
if(o is Var) {
return o.do_match(this, vars, aliases);
}
if(o is Cons) {
Cons c = o as Cons;
return (head.do_match(c.head, vars, aliases) && tail.do_match(c.tail, vars, aliases));
}
else if(o is String) {
return do_match(string_to_list((o as String).value), vars, aliases);
}
else {
return false;
}
}
public string to_string() {
string ret = "[" + head.to_string();
unowned Term rest;
for(rest = tail; rest is Cons; rest = (rest as Cons).tail) {
ret += "," + (rest as Cons).head.to_string();
}
if(rest != List.empty) {
ret += "|" + rest.to_string();
}
return ret + "]";
}
internal void encode(Buffer buffer) {
buffer.buffer.encode_list_header(1);
head.encode(buffer);
tail.encode(buffer);
}
}
}
|