Index: dyn_modules/syzextra/mod_main.cc
===================================================================
--- dyn_modules/syzextra/mod_main.cc	(revision cd5fefcfcb3762114c087ec176782c753f2a14d8)
+++ dyn_modules/syzextra/mod_main.cc	(revision 20409220dfe91ce9647c6b8d12e43f086961aa14)
@@ -34,4 +34,5 @@
 
 #include "singularxx_defs.h"
+
 #include "DebugPrint.h"
 #include "myNF.h"
@@ -51,10 +52,4 @@
 #include <string.h>
 
-
-#ifdef _GNU_SOURCE
-#define qsort_my(m, s, ss, r, cmp) qsort_r(m, s, ss, cmp, r)
-#else
-#define qsort_my(m, s, ss, r, cmp) qsort_r(m, s, ss, cmp)
-#endif
 
 
@@ -64,4 +59,6 @@
 extern ring rAssure_InducedSchreyerOrdering(const ring r, BOOLEAN complete, int sign);
 extern int rGetISPos(const int p, const ring r);
+
+// USING_NAMESPACE_SINGULARXX;
 
 USING_NAMESPACE( SINGULARXXNAME :: DEBUG )
@@ -509,204 +506,5 @@
 
 
-#ifdef _GNU_SOURCE
-static int cmp_c_ds(const void *p1, const void *p2, void *R)
-{
-#else
-static int cmp_c_ds(const void *p1, const void *p2)
-{
-  void *R = currRing; 
-#endif
-
-  const int YES = 1;
-  const int NO = -1;
-
-  const ring r =  (const ring) R; // TODO/NOTE: the structure is known: C, lp!!!
-
-  assume( r == currRing );
-
-  const poly a = *(const poly*)p1;
-  const poly b = *(const poly*)p2;
-
-  assume( a != NULL );
-  assume( b != NULL );
-  
-  assume( p_LmTest(a, r) );
-  assume( p_LmTest(b, r) );
-
-
-  const signed long iCompDiff = p_GetComp(a, r) - p_GetComp(b, r);
-
-  // TODO: test this!!!!!!!!!!!!!!!!
-
-  //return -( compare (c, qsorts) )
-
-  const int __DEBUG__ = 0;
-
-#ifndef NDEBUG
-  if( __DEBUG__ )
-  {
-    PrintS("cmp_c_ds: a, b: \np1: "); dPrint(a, r, r, 2);
-    PrintS("b: "); dPrint(b, r, r, 2);
-    PrintLn();
-  }
-#endif
-
-
-  if( iCompDiff > 0 )
-    return YES;
-
-  if( iCompDiff < 0 )
-    return  NO;
-
-  assume( iCompDiff == 0 );
-
-  const signed long iDegDiff = p_Totaldegree(a, r) - p_Totaldegree(b, r);
-
-  if( iDegDiff > 0 )
-    return YES;
-
-  if( iDegDiff < 0 )
-    return  NO;
-
-  assume( iDegDiff == 0 );
-
-#ifndef NDEBUG
-  if( __DEBUG__ )
-  {
-    PrintS("cmp_c_ds: a & b have the same comp & deg! "); PrintLn();
-  }
-#endif
-
-  for (int v = rVar(r); v > 0; v--)
-  {
-    assume( v > 0 );
-    assume( v <= rVar(r) );
-
-    const signed int d = p_GetExp(a, v, r) - p_GetExp(b, v, r);
-
-    if( d > 0 )
-      return YES;
-
-    if( d < 0 )
-      return NO;
-
-    assume( d == 0 );
-  }
-
-  return 0;  
-}
-
-
-
-static ideal ComputeLeadingSyzygyTerms(const ideal& id, const ring r)
-{
-  // 1. set of components S?
-  // 2. for each component c from S: set of indices of leading terms
-  // with this component?
-  // 3. short exp. vectors for each leading term?
-
-  const int size = IDELEMS(id);
-
-  if( size < 2 )
-  {
-    const ideal newid = idInit(1, 0); newid->m[0] = NULL; // zero ideal...        
-    return newid;
-  }
-
-
-  // TODO/NOTE: input is supposed to be (reverse-) sorted wrt "(c,ds)"!??
-
-  // components should come in groups: count elements in each group
-  // && estimate the real size!!!
-
-
-  // use just a vector instead???
-  const ideal newid = idInit( (size * (size-1))/2, size); // maximal size: ideal case!
-
-  int k = 0;
-
-  for (int j = 0; j < size; j++)
-  {
-    const poly p = id->m[j];
-    assume( p != NULL );
-    const int  c = p_GetComp(p, r);
-
-    for (int i = j - 1; i >= 0; i--)
-    {
-      const poly pp = id->m[i];
-      assume( pp != NULL );
-      const int  cc = p_GetComp(pp, r);
-
-      if( c != cc )
-        continue;
-
-      const poly m = p_Init(r); // p_New???
-
-      // m = LCM(p, pp) / p! // TODO: optimize: knowing the ring structure: (C/lp)!
-      for (int v = rVar(r); v > 0; v--)
-      {
-        assume( v > 0 );
-        assume( v <= rVar(r) );
-
-        const short e1 = p_GetExp(p , v, r);
-        const short e2 = p_GetExp(pp, v, r);
-
-        if( e1 >= e2 )
-          p_SetExp(m, v, 0, r);
-        else
-          p_SetExp(m, v, e2 - e1, r);
-
-      }
-
-      assume( (j > i) && (i >= 0) );
-
-      p_SetComp(m, j + 1, r);
-      pNext(m) = NULL;
-      p_SetCoeff0(m, n_Init(1, r->cf), r); // for later...
-
-      p_Setm(m, r); // should not do anything!!!
-
-      newid->m[k++] = m;
-    }
-  }
-
-//   if( __DEBUG__ && FALSE )
-//   {
-//     PrintS("ComputeLeadingSyzygyTerms::Temp0: \n");
-//     dPrint(newid, r, r, 1);
-//   }
-
-  // the rest of newid is assumed to be zeroes...
-
-  // simplify(newid, 2 + 32)??
-  // sort(newid, "C,ds")[1]???
-  id_DelDiv(newid, r); // #define SIMPL_LMDIV 32
-
-//   if( __DEBUG__ && FALSE )
-//   {
-//     PrintS("ComputeLeadingSyzygyTerms::Temp1: \n");
-//     dPrint(newid, r, r, 1);
-//   }
-
-  idSkipZeroes(newid); // #define SIMPL_NULL 2
-
-//   if( __DEBUG__ )
-//   {
-//     PrintS("ComputeLeadingSyzygyTerms::Output: \n");
-//     dPrint(newid, r, r, 1);
-//   }
-
-  const int sizeNew = IDELEMS(newid);
-
-  if( sizeNew >= 2 )
-    qsort_my(newid->m, sizeNew, sizeof(poly), r, cmp_c_ds);
-
-  newid->rank = id_RankFreeModule(newid, r);
-
-  return newid;
-}
-
-
-static BOOLEAN ComputeLeadingSyzygyTerms(leftv res, leftv h)
+static BOOLEAN _ComputeLeadingSyzygyTerms(leftv res, leftv h)
 {
   const ring r = currRing;
@@ -767,5 +565,5 @@
 /// change the input inplace!!!
 // TODO: use a ring with >_{c, ds}!???
-static BOOLEAN Sort_c_ds(leftv res, leftv h)
+static BOOLEAN _Sort_c_ds(leftv res, leftv h)
 {
   NoReturn(res);
@@ -817,12 +615,7 @@
     id_Test(id, r);
 
-    const int size = IDELEMS(id);
-
-    const ideal newid = id; // id_Copy(id, r); // copy???
-
-    if( size >= 2 )
-      qsort_my(newid->m, size, sizeof(poly), r, cmp_c_ds);
-    
-//    res->data = newid;
+    Sort_c_ds(id, r); // NOT A COPY! inplace sorting!!!
+
+//    res->data = id;
 //    res->rtyp = h->Typ();
     
@@ -830,7 +623,8 @@
     {
       PrintS("Sort_c_ds::Output: \n");
-      dPrint(newid, r, r, 1);
+      dPrint(id, r, r, 1);
     }
 
+    // NOTE: nothing is to be returned!!!
     return FALSE;
   }
@@ -841,7 +635,17 @@
 
 
-
-static ideal Compute2LeadingSyzygyTerms(const ideal& id, const ring r)
-{
+static BOOLEAN _Compute2LeadingSyzygyTerms(leftv res, leftv h)
+{
+  const ring r = currRing;
+  NoReturn(res);
+
+  if( h == NULL )
+  {
+    WarnS("Compute2LeadingSyzygyTerms needs an argument...");
+    return TRUE;
+  }
+
+  assume( h != NULL );  
+
 #ifndef NDEBUG
   const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
@@ -850,172 +654,4 @@
 #endif
 
-  const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)0)));
-  const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)0)));   
-
-
-
-  // 1. set of components S?
-  // 2. for each component c from S: set of indices of leading terms
-  // with this component?
-  // 3. short exp. vectors for each leading term?
-
-  const int size = IDELEMS(id);
-
-  if( size < 2 )
-  {
-    const ideal newid = idInit(1, 1); newid->m[0] = NULL; // zero module...   
-    return newid;
-  }
-
-
-    // TODO/NOTE: input is supposed to be sorted wrt "C,ds"!??
-
-    // components should come in groups: count elements in each group
-    // && estimate the real size!!!
-
-
-    // use just a vector instead???
-  ideal newid = idInit( (size * (size-1))/2, size); // maximal size: ideal case!
-
-  int k = 0;
-
-  for (int j = 0; j < size; j++)
-  {
-    const poly p = id->m[j];
-    assume( p != NULL );
-    const int  c = p_GetComp(p, r);
-
-    for (int i = j - 1; i >= 0; i--)
-    {
-      const poly pp = id->m[i];
-      assume( pp != NULL );
-      const int  cc = p_GetComp(pp, r);
-
-      if( c != cc )
-        continue;
-
-        // allocate memory & zero it out!
-      const poly m = p_Init(r); const poly mm = p_Init(r);
-
-
-        // m = LCM(p, pp) / p! mm = LCM(p, pp) / pp!
-        // TODO: optimize: knowing the ring structure: (C/lp)!
-
-      for (int v = rVar(r); v > 0; v--)
-      {
-        assume( v > 0 );
-        assume( v <= rVar(r) );
-
-        const short e1 = p_GetExp(p , v, r);
-        const short e2 = p_GetExp(pp, v, r);
-
-        if( e1 >= e2 )
-          p_SetExp(mm, v, e1 - e2, r); //            p_SetExp(m, v, 0, r);
-        else
-          p_SetExp(m, v, e2 - e1, r); //            p_SetExp(mm, v, 0, r);
-
-      }
-
-      assume( (j > i) && (i >= 0) );
-
-      p_SetComp(m, j + 1, r);
-      p_SetComp(mm, i + 1, r);
-
-      const number& lc1 = p_GetCoeff(p , r);
-      const number& lc2 = p_GetCoeff(pp, r);
-
-      number g = n_Lcm( lc1, lc2, r );
-
-      p_SetCoeff0(m ,       n_Div(g, lc1, r), r);
-      p_SetCoeff0(mm, n_Neg(n_Div(g, lc2, r), r), r);
-
-      n_Delete(&g, r);
-
-      p_Setm(m, r); // should not do anything!!!
-      p_Setm(mm, r); // should not do anything!!!
-
-      pNext(m) = mm; //        pNext(mm) = NULL;
-
-      newid->m[k++] = m;
-    }
-  }
-
-//   if( __DEBUG__ && FALSE )
-//   {
-//     PrintS("Compute2LeadingSyzygyTerms::Temp0: \n");
-//     dPrint(newid, r, r, 1);
-//   }
-
-  if( !__TAILREDSYZ__ )
-  {
-      // simplify(newid, 2 + 32)??
-      // sort(newid, "C,ds")[1]???
-    id_DelDiv(newid, r); // #define SIMPL_LMDIV 32
-
-//     if( __DEBUG__ && FALSE )
-//     {
-//       PrintS("Compute2LeadingSyzygyTerms::Temp1 (deldiv): \n");
-//       dPrint(newid, r, r, 1);
-//     }
-  }
-  else
-  {
-      //      option(redSB); option(redTail);
-      //      TEST_OPT_REDSB
-      //      TEST_OPT_REDTAIL
-    assume( r == currRing );
-    BITSET _save_test = test;
-    test |= (Sy_bit(OPT_REDTAIL) | Sy_bit(OPT_REDSB));
-
-    intvec* w=new intvec(IDELEMS(newid));
-    ideal tmp = kStd(newid, currQuotient, isHomog, &w);
-    delete w;
-
-    test = _save_test;
-
-    id_Delete(&newid, r);
-    newid = tmp;
-
-//     if( __DEBUG__ && FALSE )
-//     {
-//       PrintS("Compute2LeadingSyzygyTerms::Temp1 (std): \n");
-//       dPrint(newid, r, r, 1);
-//     }
-
-  }
-
-  idSkipZeroes(newid);
-
-  const int sizeNew = IDELEMS(newid);
-
-  if( sizeNew >= 2 )
-    qsort_my(newid->m, sizeNew, sizeof(poly), r, cmp_c_ds);
-
-  newid->rank = id_RankFreeModule(newid, r);
-  
-  return newid;
-}
-
-
-
-static BOOLEAN Compute2LeadingSyzygyTerms(leftv res, leftv h)
-{
-  const ring r = currRing;
-  NoReturn(res);
-
-  if( h == NULL )
-  {
-    WarnS("Compute2LeadingSyzygyTerms needs an argument...");
-    return TRUE;
-  }
-
-  assume( h != NULL );  
-
-#ifndef NDEBUG
-  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
-#else
-  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
-#endif
-
   const BOOLEAN __LEAD2SYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"LEAD2SYZ",INT_CMD, (void*)0)));
   
@@ -1047,143 +683,8 @@
 
 
-/// return a new term: leading coeff * leading monomial of p
-/// with 0 leading component!
-static inline poly leadmonom(const poly p, const ring r)
-{
-  poly m = NULL;
-
-  if( p != NULL )
-  {
-    assume( p != NULL );
-    assume( p_LmTest(p, r) );
-
-    m = p_LmInit(p, r);
-    p_SetCoeff0(m, n_Copy(p_GetCoeff(p, r), r), r);
-
-    p_SetComp(m, 0, r);
-    p_Setm(m, r);
-
-    assume( p_GetComp(m, r) == 0 );
-    assume( m != NULL );
-    assume( pNext(m) == NULL );
-    assume( p_LmTest(m, r) );
-  }
-
-  return m;
-}
-
-
-
-static poly FindReducer(poly product, poly syzterm,
-                        ideal L, ideal LS,
-                        const ring r)
-{
-  assume( product != NULL );
-  assume( L != NULL );
-
-  int c = 0;
-
-  if (syzterm != NULL)
-    c = p_GetComp(syzterm, r) - 1;
-
-  assume( c >= 0 && c < IDELEMS(L) );
-  
-#ifndef NDEBUG
-  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
-#else
-  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
-#endif
-
-  long debug = __DEBUG__;
-  const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)debug)));   
-  
-  if (__SYZCHECK__ && syzterm != NULL)
-  {
-    const poly m = L->m[c];
-
-    assume( m != NULL ); assume( pNext(m) == NULL );
-
-    poly lm = p_Mult_mm(leadmonom(syzterm, r), m, r);
-    assume( p_EqualPolys(lm, product, r) );
-
-    //  def @@c = leadcomp(syzterm); int @@r = int(@@c);
-    //  def @@product = leadmonomial(syzterm) * L[@@r];
-
-    p_Delete(&lm, r);    
-  }
-  
-  // looking for an appropriate diviser q = L[k]...
-  for( int k = IDELEMS(L)-1; k>= 0; k-- )
-  {
-    const poly p = L->m[k];    
-
-    // ... which divides the product, looking for the _1st_ appropriate one!
-    if( !p_LmDivisibleBy(p, product, r) )
-      continue;
-
-
-    const poly q = p_New(r);
-    pNext(q) = NULL;
-    p_ExpVectorDiff(q, product, p, r); // (LM(product) / LM(L[k]))
-    p_SetComp(q, k + 1, r);
-    p_Setm(q, r);
-
-    // cannot allow something like: a*gen(i) - a*gen(i)
-    if (syzterm != NULL && (k == c))
-      if (p_ExpVectorEqual(syzterm, q, r))
-      {
-        if( __DEBUG__ )
-        {
-          Print("FindReducer::Test SYZTERM: q == syzterm !:((, syzterm is: ");
-          dPrint(syzterm, r, r, 1);
-        }    
-
-        p_LmFree(q, r);
-        continue;
-      }
-
-    // while the complement (the fraction) is not reducible by leading syzygies 
-    if( LS != NULL )
-    {
-      BOOLEAN ok = TRUE;
-
-      for(int kk = IDELEMS(LS)-1; kk>= 0; kk-- )
-      {
-        const poly pp = LS->m[kk];
-
-        if( p_LmDivisibleBy(pp, q, r) )
-        {
-          
-          if( __DEBUG__ )
-          {
-            Print("FindReducer::Test LS: q is divisible by LS[%d] !:((, diviser is: ", kk+1);
-            dPrint(pp, r, r, 1);
-          }    
-
-          ok = FALSE; // q in <LS> :((
-          break;                 
-        }
-      }
-
-      if(!ok)
-      {
-        p_LmFree(q, r);
-        continue;
-      }
-    }
-
-    p_SetCoeff0(q, n_Neg( n_Div( p_GetCoeff(product, r), p_GetCoeff(p, r), r), r), r);
-    return q;
-
-  }
-
-  
-  return NULL;
-}
-
 
 /// TODO: save shortcut (syz: |-.->) LM(LM(m) * "t") -> syz?
 /// proc SSFindReducer(def product, def syzterm, def L, def T, list #)
-static BOOLEAN FindReducer(leftv res, leftv h)
+static BOOLEAN _FindReducer(leftv res, leftv h)
 {
   const char* usage = "`FindReducer(<poly/vector>, <vector/0>, <ideal/module>[,<module>])` expected";
@@ -1278,120 +779,6 @@
 }
 
-static poly ReduceTerm(poly multiplier, poly term4reduction, poly syztermCheck,
-                       ideal L, ideal T, ideal LS, const ring r);
-
-static poly TraverseTail(poly multiplier, poly tail, 
-                         ideal L, ideal T, ideal LS,
-                         const ring r)
-{
-  assume( multiplier != NULL );
-
-  assume( L != NULL );
-  assume( T != NULL );
-
-  poly s = NULL;
-
-  // iterate over the tail
-  for(poly p = tail; p != NULL; p = pNext(p))
-    s = p_Add_q(s, ReduceTerm(multiplier, p, NULL, L, T, LS, r), r);  
-    
-  return s;
-}
-
-
-static poly ReduceTerm(poly multiplier, poly term4reduction, poly syztermCheck,
-                       ideal L, ideal T, ideal LS, const ring r)
-{
-  assume( multiplier != NULL );
-  assume( term4reduction != NULL );
-
-
-  assume( L != NULL );
-  assume( T != NULL );
-  
-  // assume(r == currRing); // ?
-
-  // simple implementation with FindReducer:
-  poly s = NULL;
-  
-  if(1)
-  {
-    // NOTE: only LT(term4reduction) should be used in the following:
-    poly product = pp_Mult_mm(multiplier, term4reduction, r);
-    s = FindReducer(product, syztermCheck, L, LS, r);
-    p_Delete(&product, r);
-  }
-
-  if( s == NULL ) // No Reducer?
-    return s;
-
-  poly b = leadmonom(s, r);
-
-  const int c = p_GetComp(s, r) - 1;
-  assume( c >= 0 && c < IDELEMS(T) );
-
-  const poly tail = T->m[c];
-
-  if( tail != NULL )
-    s = p_Add_q(s, TraverseTail(b, tail, L, T, LS, r), r);  
-  
-  return s;
-}
-
-
-static poly SchreyerSyzygyNF(poly syz_lead, poly syz_2, ideal L, ideal T, ideal LS, const ring r)
-{
-  assume( syz_lead != NULL );
-  assume( syz_2 != NULL );
-
-  assume( L != NULL );
-  assume( T != NULL );
-
-  assume( IDELEMS(L) == IDELEMS(T) );
-
-  int  c = p_GetComp(syz_lead, r) - 1;
-
-  assume( c >= 0 && c < IDELEMS(T) );
-
-  poly p = leadmonom(syz_lead, r); // :(  
-  poly spoly = pp_Mult_qq(p, T->m[c], r);
-  p_Delete(&p, r);
-
-  
-  c = p_GetComp(syz_2, r) - 1;
-  assume( c >= 0 && c < IDELEMS(T) );
-
-  p = leadmonom(syz_2, r); // :(
-  spoly = p_Add_q(spoly, pp_Mult_qq(p, T->m[c], r), r);
-  p_Delete(&p, r);
-
-  poly tail = p_Copy(syz_2, r); // TODO: use bucket!?
-
-  while (spoly != NULL)
-  {
-    poly t = FindReducer(spoly, NULL, L, LS, r);
-
-    p_LmDelete(&spoly, r);
-    
-    if( t != NULL )
-    {
-      p = leadmonom(t, r); // :(
-      c = p_GetComp(t, r) - 1;
-
-      assume( c >= 0 && c < IDELEMS(T) );
-      
-      spoly = p_Add_q(spoly, pp_Mult_qq(p, T->m[c], r), r);
-
-      p_Delete(&p, r);
-      
-      tail = p_Add_q(tail, t, r);
-    }    
-  }
-  
-  return tail;
-}
-
 // proc SchreyerSyzygyNF(vector syz_lead, vector syz_2, def L, def T, list #)
-static BOOLEAN SchreyerSyzygyNF(leftv res, leftv h)
+static BOOLEAN _SchreyerSyzygyNF(leftv res, leftv h)
 {
   const char* usage = "`SchreyerSyzygyNF(<vector>, <vector>, <ideal/module>, <ideal/module>[,<module>])` expected";
@@ -1503,5 +890,5 @@
 /// TODO: save shortcut (syz: |-.->) LM(m) * "t" -> ?
 /// proc SSReduceTerm(poly m, def t, def syzterm, def L, def T, list #)
-static BOOLEAN ReduceTerm(leftv res, leftv h)
+static BOOLEAN _ReduceTerm(leftv res, leftv h)
 {
   const char* usage = "`ReduceTerm(<poly>, <poly/vector>, <vector/0>, <ideal/module>, <ideal/module>[,<module>])` expected";
@@ -1648,5 +1035,5 @@
 // TODO: store m * @tail -.-^-.-^-.--> ?
 // proc SSTraverseTail(poly m, def @tail, def L, def T, list #)
-static BOOLEAN TraverseTail(leftv res, leftv h)
+static BOOLEAN _TraverseTail(leftv res, leftv h)
 {
   const char* usage = "`TraverseTail(<poly>, <poly/vector>, <ideal/module>, <ideal/module>[,<module>])` expected";
@@ -1750,11 +1137,14 @@
 
 
-
-static void ComputeSyzygy(const ideal L, const ideal T, ideal& LL, ideal& TT, const ring R)
-{
-  assume( R == currRing ); // For attributes :-/
-
-  assume( IDELEMS(L) == IDELEMS(T) );
-  
+// module (N, LL, TT) = SSComputeSyzygy(L, T);
+// Compute Syz(L ++ T) = N = LL ++ TT
+// proc SSComputeSyzygy(def L, def T)
+static BOOLEAN _ComputeSyzygy(leftv res, leftv h)
+{
+  const char* usage = "`ComputeSyzygy(<ideal/module>, <ideal/module>])` expected";
+  const ring r = currRing;
+
+  NoReturn(res);
+
 #ifndef NDEBUG
   const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
@@ -1763,102 +1153,4 @@
 #endif
 
-  const BOOLEAN __LEAD2SYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"LEAD2SYZ",INT_CMD, (void*)1)));
-  const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)1)));
-  const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)__DEBUG__)));
-
-  const BOOLEAN __HYBRIDNF__ = (BOOLEAN)((long)(atGet(currRingHdl,"HYBRIDNF",INT_CMD, (void*)0)));
-
-
-  if( __LEAD2SYZ__ )
-    LL = Compute2LeadingSyzygyTerms(L, R); // 2 terms!
-  else
-    LL = ComputeLeadingSyzygyTerms(L, R); // 1 term!
-
-  const int size = IDELEMS(LL);
-
-  TT = idInit(size, 0);
-
-  if( size == 1 && LL->m[0] == NULL )
-    return;
-  
-
-  ideal LS = NULL;
-  
-  if( __TAILREDSYZ__ )
-    LS = LL;
-
-  for( int k = size - 1; k >= 0; k-- )
-  {
-    const poly a = LL->m[k]; assume( a != NULL );
-    
-    const int r = p_GetComp(a, R) - 1; 
-    
-    assume( r >= 0 && r < IDELEMS(T) );
-    assume( r >= 0 && r < IDELEMS(L) );
-
-    poly aa = leadmonom(a, R); assume( aa != NULL); // :(    
-    
-    poly a2 = pNext(a);    
-
-    if( a2 != NULL )
-    {
-      TT->m[k] = a2; pNext(a) = NULL;
-    }
-
-    if( ! __HYBRIDNF__ )
-    {
-      poly t = TraverseTail(aa, T->m[r], L, T, LS, R);
-
-      if( a2 != NULL )
-      {
-        assume( __LEAD2SYZ__ );
-
-        const int r2 = p_GetComp(a2, R) - 1; poly aa2 = leadmonom(a2, R); // :(
-
-        assume( r2 >= 0 && r2 < IDELEMS(T) );
-        
-        TT->m[k] = p_Add_q(a2, p_Add_q(t, TraverseTail(aa2, T->m[r2], L, T, LS, R), R), R);
-
-        p_Delete(&aa2, R);
-      } else
-        TT->m[k] = p_Add_q(t, ReduceTerm(aa, L->m[r], a, L, T, LS, R), R);
-
-    } else
-    {
-      if( a2 == NULL )
-      {
-        aa = p_Mult_mm(aa, L->m[r], R);
-        a2 = FindReducer(aa, a, L, LS, R); 
-      }
-      assume( a2 != NULL );
-        
-      TT->m[k] = SchreyerSyzygyNF(a, a2, L, T, LS, R); // will copy a2 :(
-      
-      p_Delete(&a2, R);
-    }
-    p_Delete(&aa, R);    
-  }
-
-  TT->rank = id_RankFreeModule(TT, R);
-}
-
-
-
-// module (N, LL, TT) = SSComputeSyzygy(L, T);
-// Compute Syz(L ++ T) = N = LL ++ TT
-// proc SSComputeSyzygy(def L, def T)
-static BOOLEAN ComputeSyzygy(leftv res, leftv h)
-{
-  const char* usage = "`ComputeSyzygy(<ideal/module>, <ideal/module>])` expected";
-  const ring r = currRing;
-
-  NoReturn(res);
-
-#ifndef NDEBUG
-  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
-#else
-  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
-#endif
-
   if ((h==NULL) || (h->Typ()!=IDEAL_CMD && h->Typ() !=MODUL_CMD) || (h->Data() == NULL))
   {
@@ -1915,12 +1207,6 @@
 }
 
-
-
-
-
-
-
 /// Get leading term without a module component
-static BOOLEAN leadmonom(leftv res, leftv h)
+static BOOLEAN _leadmonom(leftv res, leftv h)
 {
   NoReturn(res);
@@ -1931,8 +1217,6 @@
     const poly p = (poly)(h->Data());
 
-    const poly m = leadmonom(p, r);
-
+    res->data = reinterpret_cast<void *>(  leadmonom(p, r) );
     res->rtyp = POLY_CMD;
-    res->data = reinterpret_cast<void *>(m);
 
     return FALSE;
@@ -2530,5 +1814,5 @@
 
   ADD(psModulFunctions, currPack->libname, "DetailedPrint", FALSE, DetailedPrint);
-  ADD(psModulFunctions, currPack->libname, "leadmonomial", FALSE, leadmonom);
+  ADD(psModulFunctions, currPack->libname, "leadmonomial", FALSE, _leadmonom);
   ADD(psModulFunctions, currPack->libname, "leadcomp", FALSE, leadcomp);
   ADD(psModulFunctions, currPack->libname, "leadrawexp", FALSE, leadrawexp);
@@ -2551,17 +1835,17 @@
   ADD(psModulFunctions, currPack->libname, "Tail", FALSE, Tail);
   
-  ADD(psModulFunctions, currPack->libname, "ComputeLeadingSyzygyTerms", FALSE, ComputeLeadingSyzygyTerms);
-  ADD(psModulFunctions, currPack->libname, "Compute2LeadingSyzygyTerms", FALSE, Compute2LeadingSyzygyTerms);
-  
-  ADD(psModulFunctions, currPack->libname, "Sort_c_ds", FALSE, Sort_c_ds);
-  ADD(psModulFunctions, currPack->libname, "FindReducer", FALSE, FindReducer);
-
-
-  ADD(psModulFunctions, currPack->libname, "ReduceTerm", FALSE, ReduceTerm);
-  ADD(psModulFunctions, currPack->libname, "TraverseTail", FALSE, TraverseTail);
+  ADD(psModulFunctions, currPack->libname, "ComputeLeadingSyzygyTerms", FALSE, _ComputeLeadingSyzygyTerms);
+  ADD(psModulFunctions, currPack->libname, "Compute2LeadingSyzygyTerms", FALSE, _Compute2LeadingSyzygyTerms);
+  
+  ADD(psModulFunctions, currPack->libname, "Sort_c_ds", FALSE, _Sort_c_ds);
+  ADD(psModulFunctions, currPack->libname, "FindReducer", FALSE, _FindReducer);
+
+
+  ADD(psModulFunctions, currPack->libname, "ReduceTerm", FALSE, _ReduceTerm);
+  ADD(psModulFunctions, currPack->libname, "TraverseTail", FALSE, _TraverseTail);
 
     
-  ADD(psModulFunctions, currPack->libname, "SchreyerSyzygyNF", FALSE, SchreyerSyzygyNF);
-  ADD(psModulFunctions, currPack->libname, "ComputeSyzygy", FALSE, ComputeSyzygy);
+  ADD(psModulFunctions, currPack->libname, "SchreyerSyzygyNF", FALSE, _SchreyerSyzygyNF);
+  ADD(psModulFunctions, currPack->libname, "ComputeSyzygy", FALSE, _ComputeSyzygy);
   
   //  ADD(psModulFunctions, currPack->libname, "GetAMData", FALSE, GetAMData);
Index: dyn_modules/syzextra/syzextra.cc
===================================================================
--- dyn_modules/syzextra/syzextra.cc	(revision cd5fefcfcb3762114c087ec176782c753f2a14d8)
+++ dyn_modules/syzextra/syzextra.cc	(revision 20409220dfe91ce9647c6b8d12e43f086961aa14)
@@ -19,13 +19,163 @@
 #include "syzextra.h"
 
+#include "DebugPrint.h"
+
 #include <omalloc/omalloc.h>
+
+#include <misc/intvec.h>
+#include <misc/options.h>
+
+#include <coeffs/coeffs.h>
+
 #include <polys/monomials/p_polys.h>
-
+#include <polys/monomials/ring.h>
+
+#include <kernel/kstd1.h>
+#include <kernel/polys.h>
+#include <kernel/syz.h>
 #include <kernel/ideals.h>
 
 
+
+#include <Singular/tok.h>
+#include <Singular/ipid.h>
+#include <Singular/lists.h>
+#include <Singular/attrib.h>
+
+#include <Singular/ipid.h> 
+#include <Singular/ipshell.h> // For iiAddCproc
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+// USING_NAMESPACE_SINGULARXX;
+USING_NAMESPACE( SINGULARXXNAME :: DEBUG )
+
+
 BEGIN_NAMESPACE_SINGULARXX     BEGIN_NAMESPACE(SYZEXTRA)
 
-
+BEGIN_NAMESPACE(SORT_c_ds)
+
+
+#ifdef _GNU_SOURCE
+static int cmp_c_ds(const void *p1, const void *p2, void *R)
+{
+#else
+static int cmp_c_ds(const void *p1, const void *p2)
+{
+  void *R = currRing; 
+#endif
+
+  const int YES = 1;
+  const int NO = -1;
+
+  const ring r =  (const ring) R; // TODO/NOTE: the structure is known: C, lp!!!
+
+  assume( r == currRing );
+
+  const poly a = *(const poly*)p1;
+  const poly b = *(const poly*)p2;
+
+  assume( a != NULL );
+  assume( b != NULL );
+
+  assume( p_LmTest(a, r) );
+  assume( p_LmTest(b, r) );
+
+
+  const signed long iCompDiff = p_GetComp(a, r) - p_GetComp(b, r);
+
+  // TODO: test this!!!!!!!!!!!!!!!!
+
+  //return -( compare (c, qsorts) )
+
+  const int __DEBUG__ = 0;
+
+#ifndef NDEBUG
+  if( __DEBUG__ )
+  {
+    PrintS("cmp_c_ds: a, b: \np1: "); dPrint(a, r, r, 2);
+    PrintS("b: "); dPrint(b, r, r, 2);
+    PrintLn();
+  }
+#endif
+
+
+  if( iCompDiff > 0 )
+    return YES;
+
+  if( iCompDiff < 0 )
+    return  NO;
+
+  assume( iCompDiff == 0 );
+
+  const signed long iDegDiff = p_Totaldegree(a, r) - p_Totaldegree(b, r);
+
+  if( iDegDiff > 0 )
+    return YES;
+
+  if( iDegDiff < 0 )
+    return  NO;
+
+  assume( iDegDiff == 0 );
+
+#ifndef NDEBUG
+  if( __DEBUG__ )
+  {
+    PrintS("cmp_c_ds: a & b have the same comp & deg! "); PrintLn();
+  }
+#endif
+
+  for (int v = rVar(r); v > 0; v--)
+  {
+    assume( v > 0 );
+    assume( v <= rVar(r) );
+
+    const signed int d = p_GetExp(a, v, r) - p_GetExp(b, v, r);
+
+    if( d > 0 )
+      return YES;
+
+    if( d < 0 )
+      return NO;
+
+    assume( d == 0 );
+  }
+
+  return 0;  
+}
+
+END_NAMESPACE
+/* namespace SORT_c_ds */
+
+/// return a new term: leading coeff * leading monomial of p
+/// with 0 leading component!
+poly leadmonom(const poly p, const ring r)
+{
+  poly m = NULL;
+
+  if( p != NULL )
+  {
+    assume( p != NULL );
+    assume( p_LmTest(p, r) );
+
+    m = p_LmInit(p, r);
+    p_SetCoeff0(m, n_Copy(p_GetCoeff(p, r), r), r);
+
+    p_SetComp(m, 0, r);
+    p_Setm(m, r);
+
+    assume( p_GetComp(m, r) == 0 );
+    assume( m != NULL );
+    assume( pNext(m) == NULL );
+    assume( p_LmTest(m, r) );
+  }
+
+  return m;
+}
+
+
+   
 poly p_Tail(const poly p, const ring r)
 {
@@ -54,4 +204,593 @@
 
 
+void Sort_c_ds(const ideal id, const ring r)
+{
+  const int sizeNew = IDELEMS(id);
+
+#ifdef _GNU_SOURCE
+#define qsort_my(m, s, ss, r, cmp) qsort_r(m, s, ss, cmp, r)
+#else
+#define qsort_my(m, s, ss, r, cmp) qsort_r(m, s, ss, cmp)
+#endif
+  
+  if( sizeNew >= 2 )
+    qsort_my(id->m, sizeNew, sizeof(poly), r, SORT_c_ds::cmp_c_ds);
+
+#undef qsort_my
+
+  id->rank = id_RankFreeModule(id, r);
+}
+
+
+
+ideal ComputeLeadingSyzygyTerms(const ideal& id, const ring r)
+{
+  // 1. set of components S?
+  // 2. for each component c from S: set of indices of leading terms
+  // with this component?
+  // 3. short exp. vectors for each leading term?
+
+  const int size = IDELEMS(id);
+
+  if( size < 2 )
+  {
+    const ideal newid = idInit(1, 0); newid->m[0] = NULL; // zero ideal...        
+    return newid;
+  }
+
+
+  // TODO/NOTE: input is supposed to be (reverse-) sorted wrt "(c,ds)"!??
+
+  // components should come in groups: count elements in each group
+  // && estimate the real size!!!
+
+
+  // use just a vector instead???
+  const ideal newid = idInit( (size * (size-1))/2, size); // maximal size: ideal case!
+
+  int k = 0;
+
+  for (int j = 0; j < size; j++)
+  {
+    const poly p = id->m[j];
+    assume( p != NULL );
+    const int  c = p_GetComp(p, r);
+
+    for (int i = j - 1; i >= 0; i--)
+    {
+      const poly pp = id->m[i];
+      assume( pp != NULL );
+      const int  cc = p_GetComp(pp, r);
+
+      if( c != cc )
+        continue;
+
+      const poly m = p_Init(r); // p_New???
+
+      // m = LCM(p, pp) / p! // TODO: optimize: knowing the ring structure: (C/lp)!
+      for (int v = rVar(r); v > 0; v--)
+      {
+        assume( v > 0 );
+        assume( v <= rVar(r) );
+
+        const short e1 = p_GetExp(p , v, r);
+        const short e2 = p_GetExp(pp, v, r);
+
+        if( e1 >= e2 )
+          p_SetExp(m, v, 0, r);
+        else
+          p_SetExp(m, v, e2 - e1, r);
+
+      }
+
+      assume( (j > i) && (i >= 0) );
+
+      p_SetComp(m, j + 1, r);
+      pNext(m) = NULL;
+      p_SetCoeff0(m, n_Init(1, r->cf), r); // for later...
+
+      p_Setm(m, r); // should not do anything!!!
+
+      newid->m[k++] = m;
+    }
+  }
+
+//   if( __DEBUG__ && FALSE )
+//   {
+//     PrintS("ComputeLeadingSyzygyTerms::Temp0: \n");
+//     dPrint(newid, r, r, 1);
+//   }
+
+  // the rest of newid is assumed to be zeroes...
+
+  // simplify(newid, 2 + 32)??
+  // sort(newid, "C,ds")[1]???
+  id_DelDiv(newid, r); // #define SIMPL_LMDIV 32
+
+//   if( __DEBUG__ && FALSE )
+//   {
+//     PrintS("ComputeLeadingSyzygyTerms::Temp1: \n");
+//     dPrint(newid, r, r, 1);
+//   }
+
+  idSkipZeroes(newid); // #define SIMPL_NULL 2
+
+//   if( __DEBUG__ )
+//   {
+//     PrintS("ComputeLeadingSyzygyTerms::Output: \n");
+//     dPrint(newid, r, r, 1);
+//   }
+
+  Sort_c_ds(newid, r);
+
+  return newid;
+}
+
+
+
+
+ideal Compute2LeadingSyzygyTerms(const ideal& id, const ring r)
+{
+#ifndef NDEBUG
+  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
+#else
+  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
+#endif
+
+  const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)0)));
+  const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)0)));   
+
+
+
+  // 1. set of components S?
+  // 2. for each component c from S: set of indices of leading terms
+  // with this component?
+  // 3. short exp. vectors for each leading term?
+
+  const int size = IDELEMS(id);
+
+  if( size < 2 )
+  {
+    const ideal newid = idInit(1, 1); newid->m[0] = NULL; // zero module...   
+    return newid;
+  }
+
+
+    // TODO/NOTE: input is supposed to be sorted wrt "C,ds"!??
+
+    // components should come in groups: count elements in each group
+    // && estimate the real size!!!
+
+
+    // use just a vector instead???
+  ideal newid = idInit( (size * (size-1))/2, size); // maximal size: ideal case!
+
+  int k = 0;
+
+  for (int j = 0; j < size; j++)
+  {
+    const poly p = id->m[j];
+    assume( p != NULL );
+    const int  c = p_GetComp(p, r);
+
+    for (int i = j - 1; i >= 0; i--)
+    {
+      const poly pp = id->m[i];
+      assume( pp != NULL );
+      const int  cc = p_GetComp(pp, r);
+
+      if( c != cc )
+        continue;
+
+        // allocate memory & zero it out!
+      const poly m = p_Init(r); const poly mm = p_Init(r);
+
+
+        // m = LCM(p, pp) / p! mm = LCM(p, pp) / pp!
+        // TODO: optimize: knowing the ring structure: (C/lp)!
+
+      for (int v = rVar(r); v > 0; v--)
+      {
+        assume( v > 0 );
+        assume( v <= rVar(r) );
+
+        const short e1 = p_GetExp(p , v, r);
+        const short e2 = p_GetExp(pp, v, r);
+
+        if( e1 >= e2 )
+          p_SetExp(mm, v, e1 - e2, r); //            p_SetExp(m, v, 0, r);
+        else
+          p_SetExp(m, v, e2 - e1, r); //            p_SetExp(mm, v, 0, r);
+
+      }
+
+      assume( (j > i) && (i >= 0) );
+
+      p_SetComp(m, j + 1, r);
+      p_SetComp(mm, i + 1, r);
+
+      const number& lc1 = p_GetCoeff(p , r);
+      const number& lc2 = p_GetCoeff(pp, r);
+
+      number g = n_Lcm( lc1, lc2, r );
+
+      p_SetCoeff0(m ,       n_Div(g, lc1, r), r);
+      p_SetCoeff0(mm, n_Neg(n_Div(g, lc2, r), r), r);
+
+      n_Delete(&g, r);
+
+      p_Setm(m, r); // should not do anything!!!
+      p_Setm(mm, r); // should not do anything!!!
+
+      pNext(m) = mm; //        pNext(mm) = NULL;
+
+      newid->m[k++] = m;
+    }
+  }
+
+//   if( __DEBUG__ && FALSE )
+//   {
+//     PrintS("Compute2LeadingSyzygyTerms::Temp0: \n");
+//     dPrint(newid, r, r, 1);
+//   }
+
+  if( !__TAILREDSYZ__ )
+  {
+      // simplify(newid, 2 + 32)??
+      // sort(newid, "C,ds")[1]???
+    id_DelDiv(newid, r); // #define SIMPL_LMDIV 32
+
+//     if( __DEBUG__ && FALSE )
+//     {
+//       PrintS("Compute2LeadingSyzygyTerms::Temp1 (deldiv): \n");
+//       dPrint(newid, r, r, 1);
+//     }
+  }
+  else
+  {
+      //      option(redSB); option(redTail);
+      //      TEST_OPT_REDSB
+      //      TEST_OPT_REDTAIL
+    assume( r == currRing );
+    BITSET _save_test = test;
+    test |= (Sy_bit(OPT_REDTAIL) | Sy_bit(OPT_REDSB));
+
+    intvec* w=new intvec(IDELEMS(newid));
+    ideal tmp = kStd(newid, currQuotient, isHomog, &w);
+    delete w;
+
+    test = _save_test;
+
+    id_Delete(&newid, r);
+    newid = tmp;
+
+//     if( __DEBUG__ && FALSE )
+//     {
+//       PrintS("Compute2LeadingSyzygyTerms::Temp1 (std): \n");
+//       dPrint(newid, r, r, 1);
+//     }
+
+  }
+
+  idSkipZeroes(newid);
+
+  Sort_c_ds(newid, r);
+  
+  return newid;
+}
+
+poly FindReducer(poly product, poly syzterm,
+                        ideal L, ideal LS,
+                        const ring r)
+{
+  assume( product != NULL );
+  assume( L != NULL );
+
+  int c = 0;
+
+  if (syzterm != NULL)
+    c = p_GetComp(syzterm, r) - 1;
+
+  assume( c >= 0 && c < IDELEMS(L) );
+
+#ifndef NDEBUG
+  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
+#else
+  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
+#endif
+
+  long debug = __DEBUG__;
+  const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)debug)));   
+
+  if (__SYZCHECK__ && syzterm != NULL)
+  {
+    const poly m = L->m[c];
+
+    assume( m != NULL ); assume( pNext(m) == NULL );
+
+    poly lm = p_Mult_mm(leadmonom(syzterm, r), m, r);
+    assume( p_EqualPolys(lm, product, r) );
+
+    //  def @@c = leadcomp(syzterm); int @@r = int(@@c);
+    //  def @@product = leadmonomial(syzterm) * L[@@r];
+
+    p_Delete(&lm, r);    
+  }
+
+  // looking for an appropriate diviser q = L[k]...
+  for( int k = IDELEMS(L)-1; k>= 0; k-- )
+  {
+    const poly p = L->m[k];    
+
+    // ... which divides the product, looking for the _1st_ appropriate one!
+    if( !p_LmDivisibleBy(p, product, r) )
+      continue;
+
+
+    const poly q = p_New(r);
+    pNext(q) = NULL;
+    p_ExpVectorDiff(q, product, p, r); // (LM(product) / LM(L[k]))
+    p_SetComp(q, k + 1, r);
+    p_Setm(q, r);
+
+    // cannot allow something like: a*gen(i) - a*gen(i)
+    if (syzterm != NULL && (k == c))
+      if (p_ExpVectorEqual(syzterm, q, r))
+      {
+        if( __DEBUG__ )
+        {
+          Print("FindReducer::Test SYZTERM: q == syzterm !:((, syzterm is: ");
+          dPrint(syzterm, r, r, 1);
+        }    
+
+        p_LmFree(q, r);
+        continue;
+      }
+
+    // while the complement (the fraction) is not reducible by leading syzygies 
+    if( LS != NULL )
+    {
+      BOOLEAN ok = TRUE;
+
+      for(int kk = IDELEMS(LS)-1; kk>= 0; kk-- )
+      {
+        const poly pp = LS->m[kk];
+
+        if( p_LmDivisibleBy(pp, q, r) )
+        {
+
+          if( __DEBUG__ )
+          {
+            Print("FindReducer::Test LS: q is divisible by LS[%d] !:((, diviser is: ", kk+1);
+            dPrint(pp, r, r, 1);
+          }    
+
+          ok = FALSE; // q in <LS> :((
+          break;                 
+        }
+      }
+
+      if(!ok)
+      {
+        p_LmFree(q, r);
+        continue;
+      }
+    }
+
+    p_SetCoeff0(q, n_Neg( n_Div( p_GetCoeff(product, r), p_GetCoeff(p, r), r), r), r);
+    return q;
+
+  }
+
+
+  return NULL;
+}
+
+poly TraverseTail(poly multiplier, poly tail, 
+                         ideal L, ideal T, ideal LS,
+                         const ring r)
+{
+  assume( multiplier != NULL );
+
+  assume( L != NULL );
+  assume( T != NULL );
+
+  poly s = NULL;
+
+  // iterate over the tail
+  for(poly p = tail; p != NULL; p = pNext(p))
+    s = p_Add_q(s, ReduceTerm(multiplier, p, NULL, L, T, LS, r), r);  
+
+  return s;
+}
+
+
+poly ReduceTerm(poly multiplier, poly term4reduction, poly syztermCheck,
+                       ideal L, ideal T, ideal LS, const ring r)
+{
+  assume( multiplier != NULL );
+  assume( term4reduction != NULL );
+
+
+  assume( L != NULL );
+  assume( T != NULL );
+
+  // assume(r == currRing); // ?
+
+  // simple implementation with FindReducer:
+  poly s = NULL;
+
+  if(1)
+  {
+    // NOTE: only LT(term4reduction) should be used in the following:
+    poly product = pp_Mult_mm(multiplier, term4reduction, r);
+    s = FindReducer(product, syztermCheck, L, LS, r);
+    p_Delete(&product, r);
+  }
+
+  if( s == NULL ) // No Reducer?
+    return s;
+
+  poly b = leadmonom(s, r);
+
+  const int c = p_GetComp(s, r) - 1;
+  assume( c >= 0 && c < IDELEMS(T) );
+
+  const poly tail = T->m[c];
+
+  if( tail != NULL )
+    s = p_Add_q(s, TraverseTail(b, tail, L, T, LS, r), r);  
+
+  return s;
+}
+
+
+poly SchreyerSyzygyNF(poly syz_lead, poly syz_2, ideal L, ideal T, ideal LS, const ring r)
+{
+  assume( syz_lead != NULL );
+  assume( syz_2 != NULL );
+
+  assume( L != NULL );
+  assume( T != NULL );
+
+  assume( IDELEMS(L) == IDELEMS(T) );
+
+  int  c = p_GetComp(syz_lead, r) - 1;
+
+  assume( c >= 0 && c < IDELEMS(T) );
+
+  poly p = leadmonom(syz_lead, r); // :(  
+  poly spoly = pp_Mult_qq(p, T->m[c], r);
+  p_Delete(&p, r);
+
+
+  c = p_GetComp(syz_2, r) - 1;
+  assume( c >= 0 && c < IDELEMS(T) );
+
+  p = leadmonom(syz_2, r); // :(
+  spoly = p_Add_q(spoly, pp_Mult_qq(p, T->m[c], r), r);
+  p_Delete(&p, r);
+
+  poly tail = p_Copy(syz_2, r); // TODO: use bucket!?
+
+  while (spoly != NULL)
+  {
+    poly t = FindReducer(spoly, NULL, L, LS, r);
+
+    p_LmDelete(&spoly, r);
+
+    if( t != NULL )
+    {
+      p = leadmonom(t, r); // :(
+      c = p_GetComp(t, r) - 1;
+
+      assume( c >= 0 && c < IDELEMS(T) );
+
+      spoly = p_Add_q(spoly, pp_Mult_qq(p, T->m[c], r), r);
+
+      p_Delete(&p, r);
+
+      tail = p_Add_q(tail, t, r);
+    }    
+  }
+
+  return tail;
+}
+
+
+void ComputeSyzygy(const ideal L, const ideal T, ideal& LL, ideal& TT, const ring R)
+{
+  assume( R == currRing ); // For attributes :-/
+
+  assume( IDELEMS(L) == IDELEMS(T) );
+
+#ifndef NDEBUG
+  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)TRUE)));
+#else
+  const BOOLEAN __DEBUG__ = (BOOLEAN)((long)(atGet(currRingHdl,"DEBUG",INT_CMD, (void*)FALSE)));
+#endif
+
+  const BOOLEAN __LEAD2SYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"LEAD2SYZ",INT_CMD, (void*)1)));
+  const BOOLEAN __TAILREDSYZ__ = (BOOLEAN)((long)(atGet(currRingHdl,"TAILREDSYZ",INT_CMD, (void*)1)));
+  const BOOLEAN __SYZCHECK__ = (BOOLEAN)((long)(atGet(currRingHdl,"SYZCHECK",INT_CMD, (void*)__DEBUG__)));
+
+  const BOOLEAN __HYBRIDNF__ = (BOOLEAN)((long)(atGet(currRingHdl,"HYBRIDNF",INT_CMD, (void*)0)));
+
+
+  if( __LEAD2SYZ__ )
+    LL = Compute2LeadingSyzygyTerms(L, R); // 2 terms!
+  else
+    LL = ComputeLeadingSyzygyTerms(L, R); // 1 term!
+
+  const int size = IDELEMS(LL);
+
+  TT = idInit(size, 0);
+
+  if( size == 1 && LL->m[0] == NULL )
+    return;
+
+
+  ideal LS = NULL;
+
+  if( __TAILREDSYZ__ )
+    LS = LL;
+
+  for( int k = size - 1; k >= 0; k-- )
+  {
+    const poly a = LL->m[k]; assume( a != NULL );
+
+    const int r = p_GetComp(a, R) - 1; 
+
+    assume( r >= 0 && r < IDELEMS(T) );
+    assume( r >= 0 && r < IDELEMS(L) );
+
+    poly aa = leadmonom(a, R); assume( aa != NULL); // :(    
+
+    poly a2 = pNext(a);    
+
+    if( a2 != NULL )
+    {
+      TT->m[k] = a2; pNext(a) = NULL;
+    }
+
+    if( ! __HYBRIDNF__ )
+    {
+      poly t = TraverseTail(aa, T->m[r], L, T, LS, R);
+
+      if( a2 != NULL )
+      {
+        assume( __LEAD2SYZ__ );
+
+        const int r2 = p_GetComp(a2, R) - 1; poly aa2 = leadmonom(a2, R); // :(
+
+        assume( r2 >= 0 && r2 < IDELEMS(T) );
+
+        TT->m[k] = p_Add_q(a2, p_Add_q(t, TraverseTail(aa2, T->m[r2], L, T, LS, R), R), R);
+
+        p_Delete(&aa2, R);
+      } else
+        TT->m[k] = p_Add_q(t, ReduceTerm(aa, L->m[r], a, L, T, LS, R), R);
+
+    } else
+    {
+      if( a2 == NULL )
+      {
+        aa = p_Mult_mm(aa, L->m[r], R);
+        a2 = FindReducer(aa, a, L, LS, R); 
+      }
+      assume( a2 != NULL );
+
+      TT->m[k] = SchreyerSyzygyNF(a, a2, L, T, LS, R); // will copy a2 :(
+
+      p_Delete(&a2, R);
+    }
+    p_Delete(&aa, R);    
+  }
+
+  TT->rank = id_RankFreeModule(TT, R);
+}
+
+
+
 
 
Index: dyn_modules/syzextra/syzextra.h
===================================================================
--- dyn_modules/syzextra/syzextra.h	(revision cd5fefcfcb3762114c087ec176782c753f2a14d8)
+++ dyn_modules/syzextra/syzextra.h	(revision 20409220dfe91ce9647c6b8d12e43f086961aa14)
@@ -33,4 +33,5 @@
 BEGIN_NAMESPACE_SINGULARXX    BEGIN_NAMESPACE(SYZEXTRA)
 
+poly leadmonom(const poly p, const ring r);
 
 /// return the tail of a given polynomial or vector
@@ -47,4 +48,31 @@
 
 
+/// inplace sorting of the module (ideal) id wrt >_(c,ds)
+void Sort_c_ds(const ideal id, const ring r);
+
+ideal ComputeLeadingSyzygyTerms(const ideal& id, const ring r);
+
+ideal Compute2LeadingSyzygyTerms(const ideal& id, const ring r);
+
+
+poly FindReducer(poly product, poly syzterm,
+                 ideal L, ideal LS,
+                 const ring r);
+
+
+poly TraverseTail(poly multiplier, poly tail, 
+                  ideal L, ideal T, ideal LS,
+                  const ring r);
+
+poly ReduceTerm(poly multiplier, poly term4reduction, poly syztermCheck,
+                ideal L, ideal T, ideal LS, const ring r);
+
+
+
+poly SchreyerSyzygyNF(poly syz_lead, poly syz_2, ideal L, ideal T, ideal LS, const ring r);
+
+
+void ComputeSyzygy(const ideal L, const ideal T, ideal& LL, ideal& TT, const ring R);
+
 
 END_NAMESPACE               END_NAMESPACE_SINGULARXX
