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
use std::iter;
use std::iter::FromIterator;
use std::borrow::Borrow;
use std::fmt;
use bad_idea::BrokenF32;
use std::ops::Deref;
pub use print::*;
pub trait ATermFactory<'a, B: 'a> {
type Rec: Rec + FromIterator<Self::ATermRef>;
type ATerm: Deref<Target = _ATerm<Self::Rec, B>>;
type ATermRef: Borrow<Self::ATerm>;
fn int(&'a self, value: i32) -> Self::ATermRef {
self.no_annos(self.t_int(value))
}
fn string(&'a self, value: String) -> Self::ATermRef {
self.application(format!("{:?}", value), iter::empty())
}
fn real(&'a self, value: f32) -> Self::ATermRef {
self.no_annos(self.t_real(value))
}
fn application<I>(&'a self, constructor: String, children: I) -> Self::ATermRef
where I: IntoIterator<Item = Self::ATermRef>
{
self.no_annos(self.t_application(constructor, children))
}
fn list<I>(&'a self, value: I) -> Self::ATermRef
where I: IntoIterator<Item = Self::ATermRef>
{
self.no_annos(self.t_list(value))
}
fn placeholder(&'a self, value: TermPlaceholder<Self::Rec>) -> Self::ATermRef {
self.no_annos(self.t_placeholder(value))
}
fn blob(&'a self, value: B) -> Self::ATermRef {
self.no_annos(self.t_blob(value))
}
fn long(&'a self, value: i64) -> Self::ATermRef {
self.no_annos(self.t_long(value))
}
fn t_int(&'a self, value: i32) -> Term<Self::Rec, B> {
Term::Int(value)
}
fn t_string(&'a self, value: String) -> Term<Self::Rec, B> {
self.t_application(format!("{:?}", value), iter::empty())
}
fn t_real(&'a self, value: f32) -> Term<Self::Rec, B> {
Term::Real(BrokenF32(value))
}
fn t_application<I>(&'a self, constructor: String, children: I) -> Term<Self::Rec, B>
where I: IntoIterator<Item = Self::ATermRef>
{
Term::Application(constructor, Self::Rec::from_iter(children))
}
fn t_list<I>(&'a self, value: I) -> Term<Self::Rec, B>
where I: IntoIterator<Item = Self::ATermRef>
{
Term::List(Self::Rec::from_iter(value))
}
fn t_placeholder(&'a self, value: TermPlaceholder<Self::Rec>) -> Term<Self::Rec, B> {
Term::Placeholder(value)
}
fn t_blob(&'a self, value: B) -> Term<Self::Rec, B> {
Term::Blob(value)
}
fn t_long(&'a self, value: i64) -> Term<Self::Rec, B> {
Term::Long(value)
}
fn no_annos(&'a self, term: Term<Self::Rec, B>) -> Self::ATermRef;
fn with_annos<A>(&'a self, term: Term<Self::Rec, B>, annos: A) -> Self::ATermRef
where A: IntoIterator<Item = Self::ATermRef>;
}
pub trait SharedATermFactory<'a, B: 'a>: ATermFactory<'a, B> {
fn get_shared(&'a self, value: Self::ATermRef) -> Self::ATermRef
where Self::Rec: Clone,
B: Clone;
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum Term<Rec, Blob> {
Int(i32),
Long(i64),
Real(BrokenF32),
Application(String, Rec),
List(Rec),
Placeholder(TermPlaceholder<Rec>),
Blob(Blob),
}
impl<Rec, B> fmt::Display for Term<Rec, B>
where Rec: self::Rec + ATermWrite,
B: ATermWrite
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_ascii(f)
}
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum TermPlaceholder<Rec> {
Int,
String,
Real,
Term,
Application(Rec),
List,
Placeholder,
Blob,
Long,
}
impl<Rec> fmt::Display for TermPlaceholder<Rec>
where Rec: self::Rec + ATermWrite
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_ascii(f)
}
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct _ATerm<Rec, B> {
pub term: Term<Rec, B>,
pub annotations: Rec,
}
impl<Rec, B> _ATerm<Rec, B>
where Rec: Default
{
pub fn no_annos(term: Term<Rec, B>) -> Self {
_ATerm {
term: term,
annotations: Rec::default(),
}
}
pub fn with_annos(term: Term<Rec, B>, annos: Rec) -> Self {
_ATerm {
term: term,
annotations: annos,
}
}
}
impl<Rec, B> fmt::Display for _ATerm<Rec, B>
where Rec: self::Rec + ATermWrite,
B: ATermWrite
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_ascii(f)
}
}
pub trait Rec: Default {
fn is_empty(&self) -> bool;
}