use transfinite::Ordinal;
struct Hydra {
children: Vec<Hydra>,
}
impl Hydra {
fn leaf() -> Self {
Hydra { children: vec![] }
}
fn to_ordinal(&self) -> Ordinal {
if self.children.is_empty() {
return Ordinal::zero();
}
let mut child_ordinals: Vec<Ordinal> = self.children.iter().map(Self::to_ordinal).collect();
child_ordinals.sort_by(|a, b| b.cmp(a));
let mut builder = Ordinal::builder();
let mut last: Option<Ordinal> = None;
let mut multiplicity: u32 = 0;
for beta in child_ordinals {
if last.as_ref() == Some(&beta) {
multiplicity += 1;
} else {
if let Some(prev) = last {
builder = builder.term(prev, multiplicity);
}
last = Some(beta);
multiplicity = 1;
}
}
if let Some(prev) = last {
builder = builder.term(prev, multiplicity);
}
builder.build().expect("CNF terms in decreasing order")
}
}
fn main() {
println!("Kirby-Paris Hydra: every cut strictly decreases an ordinal label,");
println!("and ordinals below ε₀ are well-ordered, so Hercules always wins.\n");
let h1 = Hydra {
children: vec![Hydra::leaf()],
};
println!("Hydra 1: root with a single leaf child");
println!(" ordinal = {}\n", h1.to_ordinal());
let h2 = Hydra {
children: (0..3).map(|_| Hydra::leaf()).collect(),
};
println!("Hydra 2: root with three leaf children");
println!(" ordinal = {}\n", h2.to_ordinal());
let h3 = Hydra {
children: vec![Hydra {
children: vec![Hydra::leaf()],
}],
};
println!("Hydra 3: a chain - root - middle - leaf");
println!(" ordinal = {}\n", h3.to_ordinal());
let h4 = Hydra {
children: vec![
Hydra {
children: vec![Hydra::leaf()],
},
Hydra {
children: vec![Hydra::leaf()],
},
],
};
println!("Hydra 4: root with two chain-of-length-2 branches");
println!(" ordinal = {}\n", h4.to_ordinal());
let h5 = Hydra {
children: vec![Hydra {
children: vec![Hydra {
children: vec![Hydra::leaf()],
}],
}],
};
println!("Hydra 5: root with a depth-3 chain to a leaf");
println!(" ordinal = {}\n", h5.to_ordinal());
println!("Each hydra's ordinal label is its certificate of termination.");
println!("When Hercules cuts a leaf, the parent grows N copies of the");
println!("rest of the subtree above it (N = round number). Even though");
println!("the integer node count explodes, the ordinal label STRICTLY");
println!("DECREASES. Because there is no infinite descending chain in");
println!("the ordinals, Hercules wins in finitely many rounds - even");
println!("though that number can be astronomically larger than anything");
println!("Peano Arithmetic can express.");
}