#  Copyright (c) 1997-2018
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------

object Ideal {

   rule initial : GENERATORS {
      my $gens = $this->GENERATORS;
      my $nvars = $gens->[0]->n_vars();
      foreach my $p (@$gens){
         if($p->n_vars() != $nvars){
            croak("\ninvalid input: Number of variables needs to be the same for all GENERATORS");
            return;
         }
      }
   }
   precondition : !exists(N_VARIABLES);
	precondition : GENERATORS {
		$this->GENERATORS->size() > 0;
	}
   
   rule initial : GENERATORS, N_VARIABLES {
      my $gens = $this->GENERATORS;
      my $nvars = $this->N_VARIABLES;
      foreach my $p (@$gens){
         if($p->n_vars() != $nvars){
            croak("\ninvalid input: N_VARIABLES does not agree with the number of variables of the GENERATORS");
            return;
         }
      }
   }
   precondition : exists(N_VARIABLES);

   rule PRIMARY : {
      $this->PRIMARY = 1;
   }
   precondition : PRIME;
   weight 0.10;

   rule PRIMARY : RADICAL.PRIME {
      $this->PRIMARY = $this->RADICAL->PRIME;
   }
   weight 0.10;

   rule ZERO : GENERATORS {
      foreach my $p (@{$this->GENERATORS}){
         if(!$p->trivial){
            $this->ZERO = 0;
            return;
         }
      }
      $this->ZERO = 1;
   }
   weight 1.10;

	rule N_VARIABLES : GENERATORS {
		$this->N_VARIABLES = $this->GENERATORS->[0]->n_vars();	
	}
	precondition : GENERATORS {
		$this->GENERATORS->size() > 0;
	}
	weight 0.10;

}

object Groebner {
      
   rule initial :  {
      if (my $order = $this->lookup("ORDER_NAME")) {
         $order =~ /^(dp|Dp|lp|Lp|rp|ls|rs|ds|Ds)$/ or croak ("unknown term order");
      } elsif ( ! defined($this->lookup("ORDER_VECTOR | ORDER_MATRIX"))) {
         croak ("no term order specified");
      }
   }

   rule INITIAL_FORMS : BASIS, ORDER_VECTOR {
      $this->INITIAL_FORMS = [ map { $_->initial_form($this->ORDER_VECTOR) } @{$this->BASIS} ];
   }
   weight 1.10;

   rule INITIAL_FORMS : BASIS, ORDER_MATRIX {
      $this->INITIAL_FORMS = [ map { $_->initial_form($this->ORDER_MATRIX->row(0)) } @{$this->BASIS} ];
   }
   weight 1.10;

}

# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
