bool isFinished() { bool isFinished(List <Shrub <Sum <Combinator, Variable> > > term, int arr, int i) { if (term.Count == 0) { return(arr == i); } var tail = term.Skip(1).ToList(); //TODO test me return(term[0].Match(l => isFinished(tail, arr, i), v => v.Match(c => isFinished(tail, arr, i), x => { if ((int)x == i) { return isFinished(tail, arr, i - 1); } if ((int)x == -1) { return isFinished(tail, arr, i - 1) || isFinished(tail, arr, i); } return false; }) )); } goal.Match(l => isFinished(l, arrity, 0), v => { if (arrity == 1) { return(v.Match(_ => false, x => (int)x <= 0)); } return(false); }); return(goal.Match <bool>(l => { var ll = l.SkipWhile(s => s.Preorder().TrueForAll(v => v.Match(_ => true, x => (int)x == -1))); //ignore combinators at the start return ll.Select((s, i) => (s, i)).ToList() .TrueForAll(pi => pi.s.Match(_ => false, v => v.Match(_ => false, x => (int)x == pi.i))); //the rest is just the variables in order //TODO verify off by ones }, x => x.Match(_ => false, i => (int)i == 0))); }
public static BinaryTree <T> ToBinary <T>(Shrub <T> shrub) { return(shrub.Match <BinaryTree <T> >(l => { int n = l.Count; if (n == 0) { throw new Exception("Invalid shrub, empty parenthesis"); } if (n == 1) { return ToBinary <T>(l[0]); } return BinaryTree <T> .Node(ToBinary <T>(Shrub <T> .Node(l.Take(n - 1).ToList())), ToBinary <T>(l[n - 1])); }, x => BinaryTree <T> .Leaf(x))); }