!!!! SIMPLIFY : compute conds with lower complexity nodes 
!!!! CLEAN UP defconds (sort out TRUE cond, put them end of list)
!!!! EXO2 OUTPUT necconds (non trivially true definition conditions)
!!!!  no logically implied conds. 
!!!!  two rounds for trueconds : 1) defconds, 2)dummies
!!!! EXO3 OUTPUT simpcond : recursively simplify, reduce
!!!! compute  non tautological consequences of defconds

!!!! EXO2 : no renaming
!!! CHECK regles avec cube in exo3 prod x^3y >0 iff xy >0


!!!! EXO3 : ELIMINATE DOUBLES due to VAR. RENAMING
!!!! first step (step=1) in defconds, next step (step=2) in dummies

!if $mode=3
  step=1
  xpos=!item 1 of $varpos
  list=$defconds
  simpconds=
  !while $list!=$empty
     cond=!item 1 of $list
     otralist=
     row=!word 1 of $cond
     col=!word 2 of $cond
     rel=!word 3 of $cond
     nb=!word 4 of $cond
     exp=$(exps[$row;$col])
     list=!item 2 to -1 of $list
     !for cond2 in $list
         row2=!word 1 of $cond2
         col2=!word 2 of $cond2
         rel2=!word 3 of $cond2
         nb2=!word 4 of $cond2
         exp2=$(exps[$row2;$col2])
        !if $rel!=$rel2 or $[$nb]!=$[$nb2] or $exp2 notsametext $exp  
              otralist=$otralist,$cond2
        !endif
     !next cond2 
!!!! change position row col for cond about the variable $x 
     pos=$row $col
     !if $pos isitemof $varpos
         row=!word 1 of $xpos
         col=!word 2 of $xpos
         cond=$row $col $rel $nb
     !endif
     simpconds=$simpconds,$cond
     list=!item 2 to -1 of $otralist
     !if  $list=$empty
         !if $step=1
                step=2
               list=$dummies
               defconds=!item 2 to -1 of $simpconds
               simpconds=
         !else
              dummies=!item 2 to -1 of $simpconds
         !endif
     !endif
  !endwhile
!endif
!!!! END EXO3 ELIMINATE DOUBLES

!!!! EXO2 + EXO3 : SIMPLIFY 
!!! for exo 2, one round with defconds, snd round (level 2) with dummies
!!! for modif., see at the end 

!if $mode=2 
   round=3 
!else 
   round=$depth 
!endif
!!!round=$depth

curconds=$defconds 
level=1
necconds=
!!!! trueconds initialized in tree.init with possible dummies
!while $level<$round and $curconds!=$empty
 !advance level
  simpconds=
 !for cond in $curconds
    row=!word 1 of $cond
    col=!word 2 of $cond
    rel=!word 3 of $cond
    nb=!word 4 of $cond
    node=$(nodematrix[$row;$col])
    spos=$(succmatrix[$row;$col])
    pos1=0
    pos2=0
    strpos1=0
    strpos2=0
!!! by default no change
    new=$cond
    true=no
    s1=!word 1 of $spos
!!!! possibly empty
    s2=!word 2 of $spos
!!!! cases where node is binary
    !if $node isitemof sum,diff,prod,div
        succ1=$(nodematrix[$[$row+1];$s1])
        succ2=$(nodematrix[$[$row+1];$s2])
        !if $succ1 isitemof sqrt,carre,abs,exp or $[$succ1]>0
               pos1=1
        !endif
        !if $succ1=exp or $[$succ1]>0
               strpos1=1
        !endif
        !if $succ2 isitemof sqrt,carre,abs,exp or $[$succ2]>0
               pos2=1
        !endif
        !if $succ2=exp or $[$succ2]>0
               strpos2=1
        !endif
         !if  $strpos1=1 or $succ1=inv
                nonul1=1
         !else
                nonul1=0
         !endif 
         !if  $strpos2=1 or $succ2=inv
                nonul2=1
         !else
                nonul2=0
         !endif 
        !if $node=sum  
            !if $pos1=1 and $pos2=1
                !if $rel=geq and $nb<=0
                    true=YES        
                !endif
                !if $rel isitemof geq,neq and $nb<0  
                    true=YES        
                !endif
                !if ($rel=leq and $nb<0) or ($rel=lt and $nb<=0)
                     true=FALSE
                !endif  
                !if $rel isitemof gt,neq  and $nb=0 
                    !if $strpos1=1 or $strpos2=1
                        true=YES
                    !else
                        new=$[$row+1] $s1 neq 0, $[$row+1] $s2 neq 0  
                    !endif 
                !endif    
            !endif                     
        !else
!!!! product non null or positive
           !if $node=prod and $nb=0
               !if $rel=neq           
                    !if $nonul1=1 and $nonul2=1
                       true=YES
                    !endif
                    !if $nonul1=1 and $nonul2=0
                            new=$[$row+1] $s2 neq 0                    
                    !endif
                    !if $nonul1=0 and $nonul2=1
                            new=$[$row+1] $s1 neq 0                    
                    !endif
                    !if $nonul1=0 and $nonul2=0
                    new=$[$row+1] $s1 neq 0, $[$row+1] $s2 neq 0  
                     !endif
               !else
                   !if $pos1=1
                        !if $pos2=1
                            !if $rel=geq
                                true=YES
                            !endif
                        !else
                             new=$[$row+1] $s2 $rel 0 
!!!! strictness condition for the positive expression
                            !if $rel isitemof gt,lt and $strpos1=0 
                                 new=$new, $[$row+1] $s1 neq 0 
                            !endif
                         !endif
                   !else
                       !if $pos2=1
                                new= $[$row+1] $s1 $rel 0 
!!!! add strictness condition for the positive expression
                               !if $rel isin gt,lt and $strpos2=0
                                   new=$new, $[$row+1] $s2 neq 0 
                              !endif
                       !endif
                   !endif 
               !endif
           !else
!!!!!! quotient non null or positive
               !if $node=div and $nb=0
                    !if $rel=neq 
                        !if $nonul1=1
                             true=YES
                        !else
                            new=$[$row+1] $s1 neq 0
                        !endif
                    !else
                      !if $pos1=1
                         !if $pos2=1
                             !if $rel=geq
                                 true=YES
                             !endif
                         !else
                             new= $[$row+1] $s2 $rel 0
!!!! add strictness condition for the positive expression
                             !if $rel isitemof gt,lt and $strpos1=0
                                 new=$new, $[$row+1] $s1 neq 0 
                             !endif 
                          !endif
                       !else
                           !if $pos2=1
                                new= $[$row+1] $s1 $rel 0 
                           !endif
                      !endif
                   !endif 
               !endif
            !endif
        !endif 
    !endif 
!!!! end binary node  
!!! begin unary node 
    !if $node isitemof $lesfct 
       !if $rel!=$empty
           !if ($node=carre or $node=abs) 
               !if ($rel=geq and $nb<=0) or ($rel=gt and $nb<0) or ($rel=neq and $nb<0)     
                     true=YES
               !endif
               !if ($rel=leq and $nb<0) or ($rel=lt and $nb<=0)
                     true=FALSE
               !endif  
               !if ($rel isitemof gt,neq and $nb=0)
                     new=$[$row+1] $s1 neq 0
               !endif 
            !endif
            !if $node=cube and ($nb=0 or $nb=1)
                 new=$[$row+1] $s1 $rel $nb
            !endif
            !if $node=inv 
              !if $nb=0
                 !if $rel!=neq
                      new=$[$row+1] $s1 $rel $nb
                 !else
                       true=YES
                 !endif
              !endif
            !endif
            !if $node=sqrt 
                    !if $rel isitemof gt,neq and $nb<0
                           true=YES
                    !endif
                    !if $rel=geq and $nb<=0
                           true=YES
                    !endif
                    !if $rel isitemof gt,geq,neq  and ($nb=0 or $nb=1)
                          new=$[$row+1] $s1 $rel $nb
                    !endif
             !endif
             !if $node=exp 
                  !if $rel isitemof geq,gt,neq and $nb<=0
                       true=YES
                  !endif
                  !if $rel isitemof leq,lt,eq and $nb<=0
                        true=FALSE
                  !endif 
                   !if  $nb=1
                          new=$[$row+1] $s1 $rel 0
                  !endif 
             !endif
             !if $node=ln 
                !if $nb=0
                    new=$[$row+1] $s1 $rel 1
                 !endif
             !endif
        !endif
      !endif
!!!!keep condition affected to terminal nodes, but this (for numbers)
      !if $[$node]!=NaN
          !if $[$node]>0 and $rel isitemof geq,gt,neq
              true=YES
          !endif
          !if $[$node]<0 and $rel isitemof leq,lt,neq
              true=YES
          !endif
      !endif 
      !if $true=YES
           trueconds=!append item $cond to $trueconds
           seconds=!append item $cond to $seconds
      !else 
!!!! first pass : remove true conds from defconds 
          !if $mode=2 and $level=2 
                 necconds=!append item $cond to $necconds
          !endif
          !if $new notitemof $simpconds
                simpconds=!append item $new to $simpconds 
          !endif             
          !if $new notsametext $cond
                seconds=!append item $cond to $seconds
          !endif
      !endif
   !next cond
   !if $mode=2 and $level=2 
         curconds=$dummies
   !else
         curconds=$simpconds
   !endif
!endwhile


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!! EXO3 : REDUCE  sisters conditions (conditions about same subexp)
!!!!! discard redundant conditions into seconds
!!!! output lesconds (minimal set of simpler and sufficient conds)

!if $mode=3

list=$simpconds
lesconds=
!!!while there are at least two conditions
!while $list!=$empty
    cond=!item 1 of $list
    new=0
    otralist=
    sisters=
    row=!word 1 of $cond
    col=!word 2 of $cond
    rel=!word 3 of $cond
    nb=!word 4 of $cond
    exp=$(exps[$row;$col])
    list=!item 2 to -1 of $list
    !for cond2 in $list
        row2=!word 1 of $cond2
        col2=!word 2 of $cond2
        rel2=!word 3 of $cond2
        nb2=!word 4 of $cond2
        exp2=$(exps[$row2;$col2])
       !if $exp2 notsametext $exp
              otralist=$otralist,$cond2
       !else
           rels=$rel,$rel2
           !if $[$nb]=$[$nb2]  
              !if $rel=$rel2 
                     new=1
              !else
                  !if $rel2=neq and $rel isin gt,lt 
                        second=$cond2
                        new=1
                  !else
                      !if $rel=neq and $rel2 isin gt,lt 
                           second=$cond
                           new=1
                           rel=$rel2
                       !else
!!!!! case where both relations are discarded (cond in the end)
                     second=$cond,$cond2
                    !if neq isin $rels and geq isin $rels 
                        new=1
                        rel=gt
                    !endif
                    !if neq isin $rels and leq isin $rels 
                         new=1
                         rel=lt
                     !endif
                     !if geq isin $rels and leq isin $rels 
                         new=1
                         rel=eq
                     !endif
                     !if gt isin $rels and lt isin $rels
                         new=1
                         rel=eq
                     !endif
                     !if gt isin $rels and geq isin $rels
                          new=1
                          rel=gt
                     !endif
                     !if lt isin $rels and leq isin $rels
                          new=1
                          rel=lt
                     !endif
                     !if geq isin $rels and lt isin $rels 
                         new=FALSE
                        !break
                     !endif
                     !if leq isin $rels and gt isin $rels 
                         new=FALSE
                        !break
                     !endif
!!! end second=cond,cond2
                     !endif
                  !endif
              !endif 
            !endif
!!! endif nb egaux ... prciser qui est mis dans seconds
            !if $[$nb]>$[$nb2]
                 !if $rel isin geq,gt and $rel2 isin geq,gt 
                     new=1
                      second=$cond2
                 !endif
                 !if $rel isin leq,lt and $rel2 isin leq,lt
                      new=1
                      rel=$rel2
                      nb=$nb2
                      second=$cond
                 !endif
                 !if $rel isin geq,gt and $rel2 isin leq,lt 
                     new=FALSE
                     !break
                 !endif
            !endif
!!! endif nb > nb2
            !if $[$nb]<$[$nb2]
                 !if $rel isin geq,gt and $rel2 isin geq,gt 
                      new=1
                      rel=$rel2
                      nb=$nb2
                      second=$cond
                 !endif
                 !if $rel isin leq,lt and $rel2 isin leq,lt
                      new=1
                      second=$cond2
                 !endif
                 !if $rel isin leq,lt and $rel2 isin geq,gt 
                     new=FALSE
                     !break
                 !endif
            !endif
            !if $new=1
                cond=$row $col $rel $nb
            !else
                sisters=$sisters,$cond2
            !endif
      !endif
!!!! end if exp!=exp2
   !next cond2
   !if $new=FALSE
       lesconds=FALSE
        !break
   !else
       list=$otralist
       sisters=$cond,$sisters
       lesconds=$lesconds,$sisters
!!!! CAREFUL sisters may be empty
       lesconds=!nonempty item $lesconds
       list=!nonempty item $list
!!!!! careful : second may have been already put in seconds 
       !if $new=1
           !if $second notitemof $seconds
                seconds=$seconds,$second
           !endif
       !endif
    !endif
!endwhile
seconds=!nonempty item $seconds
!endif
