package Logic::Guesses;

use Logic;

use strict;

sub new {
	my ($package) = @_;
	return bless {
		guessing_rels => [undef],
		bindings => [[]],
	}, $package;
}

sub let {
	my ($self, $var) = @_;
	push @{$self->{bindings}[-1]}, $var;
}

sub guess {
	my ($self, $guessing_rel) = @_;
	die "must specify a relation when guessing" unless $guessing_rel;
	push @{$self->{bindings}}, [];
	push @{$self->{guessing_rels}}, $guessing_rel;
}

sub fail {
	my ($self) = @_;
	for my $var (@{$self->{bindings}[-1]}) {
		undef $$var;
	}
	pop @{$self->{bindings}};
	if (my $guessing_rel = pop @{$self->{guessing_rels}}) {
		Logic::activate($guessing_rel); # stupid exporter
	} else {
		die "contradiction";
	}
}

1
