-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathdisplay.rs
More file actions
456 lines (419 loc) · 15.1 KB
/
display.rs
File metadata and controls
456 lines (419 loc) · 15.1 KB
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
use std::fmt;
use std::sync::OnceLock;
use crate::dag::{
Dag, DagLike, InternalSharing, MaxSharing, NoSharing, PostOrderIterItem, SharingTracker,
};
use crate::encode;
use crate::node::{Inner, Marker, Node};
use crate::BitWriter;
/// Convenience structure for displaying a Simplictiy expression with its
/// witness.
pub struct Display<'n, M: Marker> {
node: &'n Node<M>,
#[cfg_attr(not(feature = "base64"), allow(dead_code))]
prog_bytes: OnceLock<Vec<u8>>,
wit_bytes: OnceLock<Vec<u8>>,
}
impl<'n, M: Marker> From<&'n Node<M>> for Display<'n, M> {
fn from(node: &'n Node<M>) -> Self {
// Because of Rust's lack of specialization we cannot cache the witness data
// until we're in a function which is gated on `M: Marker<Witness = Value>`.
// So we use `OnceLock` for that.
//
// While we're at it, use `OnceLock` for the program bytes, since maybe the
// user doesn't want the program data (or can't use it due to lack of base64).
Self {
node,
prog_bytes: OnceLock::new(),
wit_bytes: OnceLock::new(),
}
}
}
impl<'n, M: Marker> Display<'n, M> {
/// Display the program in base64.
#[cfg(feature = "base64")]
pub fn program(&self) -> impl fmt::Display + '_ {
use crate::base64::display::Base64Display;
use crate::base64::engine::general_purpose;
let prog_bytes = self
.prog_bytes
.get_or_init(|| self.node.to_vec_without_witness());
Base64Display::new(prog_bytes, &general_purpose::STANDARD)
}
}
impl<'n, M: Marker<Witness = crate::Value>> Display<'n, M> {
/// Display the witness data in hex.
pub fn witness(&self) -> impl fmt::Display + '_ {
use crate::hex::DisplayHex;
let wit_bytes = self.wit_bytes.get_or_init(|| {
let mut wit_v = vec![];
let mut witness = BitWriter::new(&mut wit_v);
let sharing_iter = self.node.post_order_iter::<MaxSharing<M>>();
encode::encode_witness(sharing_iter.into_witnesses(), &mut witness)
.expect("Vec::write is infallible");
witness.flush_all().expect("Vec::write is infallible");
wit_v
});
wit_bytes.as_hex()
}
}
/// Display a Simplicity expression as a linear string.
///
/// The linear string has no sharing and may be **exponentially larger**
/// than the underlying shared expression.
///
/// There are some basic transformations to increase readability:
///
/// ## Infix notation
///
/// `pair s t` → `s & t`
///
/// `comp s t` → `s; t`
///
/// ## Booleans
///
/// `injl unit` → `false`
///
/// `injr unit` → `true`
///
/// ## Selectors
///
/// `take drop iden` → `OIH`
///
/// Sequences of `take` and `drop` that end in `iden` are transformed as follows:
///
/// `take` → `O` (looks like zero)
///
/// `drop` → `I` (looks like one)
///
/// `iden` → `H`
pub struct DisplayExpr<'a, M: Marker>(&'a Node<M>);
impl<'a, M: Marker> From<&'a Node<M>> for DisplayExpr<'a, M> {
fn from(node: &'a Node<M>) -> Self {
Self(node)
}
}
impl<'a, M: Marker> fmt::Display for DisplayExpr<'a, M>
where
&'a Node<M>: DagLike,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for data in self.0.verbose_pre_order_iter::<NoSharing>(None) {
match data.n_children_yielded {
1 => match data.node.inner() {
Inner::Comp(..) => f.write_str("; ")?,
Inner::Pair(..) => f.write_str(" & ")?,
Inner::Case(..) => f.write_str(") (")?,
x => debug_assert!(matches!(x.as_dag(), Dag::Unary(..))),
},
2 => {
debug_assert!(matches!(data.node.inner().as_dag(), Dag::Binary(..)));
if data.parent.is_some() {
f.write_str(")")?;
} else {
// Omit parentheses for root node
}
}
n => {
debug_assert!(n == 0, "Combinators are nullary, unary or binary");
match data.node.inner() {
Inner::Iden
if matches!(
data.parent.map(Node::inner),
Some(Inner::Take(..) | Inner::Drop(..))
) =>
{
f.write_str("H")?
}
Inner::Take(child) if is_take_drop_iden(child) => f.write_str("O")?,
Inner::Drop(child) if is_take_drop_iden(child) => f.write_str("I")?,
Inner::Iden => f.write_str("iden")?,
Inner::Take(..) => f.write_str("take ")?,
Inner::Drop(..) => f.write_str("drop ")?,
Inner::Unit
if matches!(
data.parent.map(Node::inner),
Some(Inner::InjL(..) | Inner::InjR(..))
) => {} // skip unit inside false | true
Inner::InjL(child) if matches!(child.inner(), Inner::Unit) => {
f.write_str("false")?
}
Inner::InjR(child) if matches!(child.inner(), Inner::Unit) => {
f.write_str("true")?
}
Inner::Unit => f.write_str("unit")?,
Inner::InjL(..) => f.write_str("injl ")?,
Inner::InjR(..) => f.write_str("injr ")?,
Inner::Comp(..) | Inner::Pair(..) => {} // display comp and pair as infix
Inner::Case(..) => f.write_str("case ")?,
Inner::AssertL(..) => f.write_str("assertl ")?,
Inner::AssertR(..) => f.write_str("assertr ")?,
Inner::Disconnect(..) => f.write_str("disconnect ")?,
Inner::Witness(..) => f.write_str("witness ")?,
Inner::Fail(..) => f.write_str("fail")?,
Inner::Jet(jet) => write!(f, "jet_{jet} ")?,
Inner::Word(value) => write!(f, "const {value} ")?,
}
match data.node.inner().as_dag() {
Dag::Binary(..) if data.parent.is_some() => f.write_str("(")?,
_ => {} // Omit parentheses for root node
}
}
};
}
Ok(())
}
}
fn is_take_drop_iden<M: Marker>(node: &Node<M>) -> bool {
for node in node.pre_order_iter::<InternalSharing>() {
match node.inner() {
Inner::Take(..) | Inner::Drop(..) | Inner::Iden => {}
_ => return false,
}
}
true
}
impl<'a, M: Marker> fmt::Debug for DisplayExpr<'a, M>
where
&'a Node<M>: DagLike,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
/// The output format for [`DisplayAsGraph`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GraphFormat {
/// Graphviz DOT format, renderable with `dot -Tsvg` or similar tools.
Dot,
/// Mermaid diagram format, renderable in Markdown or the Mermaid live editor.
Mermaid,
}
/// The node-sharing level for [`DisplayAsGraph`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SharingLevel {
/// No sharing: every use of a node is visited separately (may be exponentially large).
None,
/// Internal sharing: nodes shared within the expression are visited once.
Internal,
/// Maximum sharing: maximize sharing across the entire expression.
Max,
}
/// Display a Simplicity expression as a graph in a chosen format.
///
/// Construct via [`Node::display_as_dot`], [`Node::display_as_mermaid`], or
/// [`DisplayAsGraph::new`]. The [`fmt::Display`] impl renders using the stored
/// `format` and `sharing` fields; [`to_dot_string`](DisplayAsGraph::to_dot_string)
/// and [`to_mermaid_string`](DisplayAsGraph::to_mermaid_string) always render in
/// the named format using the stored sharing level.
pub struct DisplayAsGraph<'a, M: Marker> {
node: &'a Node<M>,
/// Output format (DOT or Mermaid).
pub format: GraphFormat,
/// Node-sharing level used when rendering.
pub sharing: SharingLevel,
}
impl<'a, M: Marker> DisplayAsGraph<'a, M> {
/// Create a new `DisplayAsGraph` with the given format and sharing level.
pub fn new(node: &'a Node<M>, format: GraphFormat, sharing: SharingLevel) -> Self {
Self {
node,
format,
sharing,
}
}
/// Render as a Graphviz DOT string using the stored sharing level.
pub fn to_dot_string(&self) -> String
where
&'a Node<M>: DagLike,
{
let mut result = String::new();
match self.render(GraphFormat::Dot, &mut result) {
Ok(_) => result,
Err(e) => format!("Could not display as string: {}", e),
}
}
/// Render as a Mermaid string using the stored sharing level.
pub fn to_mermaid_string(&self) -> String
where
&'a Node<M>: DagLike,
{
let mut result = String::new();
match self.render(GraphFormat::Mermaid, &mut result) {
Ok(_) => result,
Err(e) => format!("Could not display as string: {}", e),
}
}
fn render<W: fmt::Write>(&self, graph_format: GraphFormat, w: &mut W) -> fmt::Result
where
&'a Node<M>: DagLike,
{
match self.sharing {
SharingLevel::None => self.render_with::<NoSharing, _>(graph_format, w),
SharingLevel::Internal => self.render_with::<InternalSharing, _>(graph_format, w),
SharingLevel::Max => self.render_with::<MaxSharing<M>, _>(graph_format, w),
}
}
fn render_with<S, W>(&self, graph_format: GraphFormat, w: &mut W) -> fmt::Result
where
S: SharingTracker<&'a Node<M>> + Default,
W: fmt::Write,
{
let node_label = |data: &PostOrderIterItem<&Node<M>>| -> String {
match data.node.inner() {
Inner::Witness(_) => format!("witness({})", data.index),
Inner::Word(word) => format!("word({})", shorten(word.to_string(), 12)),
_ => data.node.inner().to_string(),
}
};
match graph_format {
GraphFormat::Dot => {
writeln!(w, "digraph G {{")?;
writeln!(w, "ordering=\"out\";")?;
for data in self.node.post_order_iter::<S>() {
writeln!(w, " node{}[label=\"{}\"];", data.index, node_label(&data))?;
if let Some(left) = data.left_index {
writeln!(w, " node{}->node{};", data.index, left)?;
}
if let Some(right) = data.right_index {
writeln!(w, " node{}->node{};", data.index, right)?;
}
}
writeln!(w, "}}")?;
}
GraphFormat::Mermaid => {
writeln!(w, "flowchart TD")?;
for data in self.node.post_order_iter::<S>() {
match data.node.inner() {
Inner::Case(..) => {
writeln!(w, " node{}{{\"{}\"}}", data.index, node_label(&data))?;
}
_ => {
writeln!(w, " node{}[\"{}\"]", data.index, node_label(&data))?;
}
}
if let Some(left) = data.left_index {
writeln!(w, " node{} --> node{}", data.index, left)?;
}
if let Some(right) = data.right_index {
writeln!(w, " node{} --> node{}", data.index, right)?;
}
}
}
}
Ok(())
}
}
impl<'a, M: Marker> fmt::Display for DisplayAsGraph<'a, M>
where
&'a Node<M>: DagLike,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.render(self.format, f)
}
}
fn shorten<S: AsRef<str>>(s: S, max_len: usize) -> String {
let s = s.as_ref();
let chars: Vec<char> = s.chars().collect();
if chars.len() <= max_len {
s.to_string()
} else {
let dots = "...";
let available = max_len.saturating_sub(dots.len());
let start_len = available.div_ceil(2); // Slightly favor the start
let end_len = available / 2;
let start: String = chars[..start_len].iter().collect();
let end: String = chars[chars.len() - end_len..].iter().collect();
format!("{}{}{}", start, dots, end)
}
}
#[cfg(test)]
mod tests {
use crate::human_encoding::Forest;
use crate::jet::Core;
use crate::types;
use crate::RedeemNode;
use std::collections::HashMap;
use std::sync::Arc;
fn parse_program(s: &str) -> Arc<RedeemNode<Core>> {
types::Context::with_context(|ctx| {
let empty_witness = HashMap::new();
Forest::<Core>::parse(s)
.unwrap()
.to_witness_node(&ctx, &empty_witness)
.unwrap()
.finalize_unpruned()
.unwrap()
})
}
#[test]
fn display_boolean() {
let s = "
false := injl unit
true := injr unit
main := comp pair false true unit";
let program = parse_program(s);
assert_eq!("(false & true); unit", program.display_expr().to_string())
}
#[test]
fn display_oih() {
let s = "
oih := take drop iden
input := pair (pair unit unit) unit
output := unit
main := comp input (comp (pair oih (take unit)) output)";
let program = parse_program(s);
assert_eq!(
"((unit & unit) & unit); ((OIH & take unit); unit)",
program.display_expr().to_string()
)
}
#[test]
fn display_as_dot() {
let s = "
oih := take drop iden
input := pair (pair unit unit) unit
output := unit
main := comp input (comp (pair oih (take unit)) output)";
let program = parse_program(s);
let str = program
.display_as_dot()
.to_string()
.replace(" ", "")
.replace("\n", "");
let expected = "
digraph G {
ordering=\"out\";
node0[label=\"unit\"];
node1[label=\"unit\"];
node2[label=\"pair\"];
node2->node0;
node2->node1;
node3[label=\"unit\"];
node4[label=\"pair\"];
node4->node2;
node4->node3;
node5[label=\"iden\"];
node6[label=\"drop\"];
node6->node5;
node7[label=\"take\"];
node7->node6;
node8[label=\"unit\"];
node9[label=\"take\"];
node9->node8;
node10[label=\"pair\"];
node10->node7;
node10->node9;
node11[label=\"unit\"];
node12[label=\"comp\"];
node12->node10;
node12->node11;
node13[label=\"comp\"];
node13->node4;
node13->node12;
}"
.replace(" ", "")
.replace("\n", "");
assert_eq!(str, expected);
}
}