Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/charisee_dev/src/compiler/high-il/normalize-ein.sml
ViewVC logotype

View of /branches/charisee_dev/src/compiler/high-il/normalize-ein.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2586 - (download) (annotate)
Thu Apr 17 04:04:36 2014 UTC (5 years, 5 months ago) by cchiw
Original Path: branches/charisee/src/compiler/high-il/normalize-ein.sml
File size: 24516 byte(s)
Outer product
structure NormalizeEin = struct

    local

    structure E = Ein
    structure P=Printer(*
    structure O =OrderEin*)
    in

      
(*Flattens Add constructor: change, expression *)
fun mkAdd [e]=(1,e)
    | mkAdd(e)=let
    fun flatten((i, (E.Add l)::l'))= flatten(1,l@l')
        |flatten(i,((E.Const c):: l'))=
            if (c>0 orelse c<0) then let
                    val(b,a)=flatten(i,l') in (b,[E.Const c]@a) end
            else flatten(1,l')
        | flatten(i,[])=(i,[])
        | flatten (i,e::l') =  let
                    val(b,a)=flatten(i,l') in (b,[e]@a) end
    
     val (b,a)=flatten(0,e)
    in case a
     of [] => (1,E.Const(1))
                | [e] => (1,e)
                | es => (b,E.Add es)
                (* end case *)
     end
        

fun mkProd [e]=(1,e)
    | mkProd(e)=let
    fun flatten(i,((E.Prod l)::l'))= flatten(1,l@l')
        |flatten(i,((E.Const c)::l'))=
                if(c>0 orelse  0>c) then (3,[E.Const 0])
                else flatten(i,l')
         | flatten(i,[])=(i,[])
         | flatten (i,e::l') =  let val(a,b)=flatten(i,l') in (a,[e]@b) end
     val (change,a)=flatten(0,e)
     in if(change=3) then (1,E.Const(0))
        else case a
        of [] => (1,E.Const(0))
        | [e] => (1,e)
        | es => (change, E.Prod es)
        (* end case *)
         end
                
                
                
(* filter function shifts constant/greeks to outside product*)
fun filter2([],pre,eps,dels,post)=(pre,eps,dels,post)
    | filter2(E.Const c::es,pre, eps,dels,post)=filter2(es, pre@[E.Const c],eps,dels,post)
    | filter2(E.Delta d::es,pre,eps,dels,post)= filter2(es,pre,eps,dels@[E.Delta d],post)
    | filter2(E.Value v::es, pre, eps,dels,post)=filter2(es, pre@[E.Value v],eps,dels,post)
    | filter2(E.Epsilon e::es, pre,eps,dels, post)=filter2(es, pre,eps@[E.Epsilon e],dels,post)
    | filter2(E.Tensor(id,[])::es, pre,eps, dels,post)=filter2(es, pre@[E.Tensor(id,[])],eps,dels,post)
    | filter2(E.Prod p::es, pre,eps,dels, post)=filter2(p@es,pre,eps,dels,post)
    | filter2(e::es, pre,eps,dels, post)= filter2(es, pre, eps,dels,post@[e])


(*Only used to find eps, and embedded sums*)
fun findeps(e,(E.Epsilon eps)::es,rest)=  findeps(e@[E.Epsilon eps],es,rest)
    | findeps(e, E.Sum(sx,E.Prod(E.Epsilon eps::ps))::es,rest)= (e@[E.Epsilon eps], rest@ps@es,sx)
    | findeps(e, E.Prod p::es,rest)=findeps(e, p@es,rest)
    | findeps(e, E.Field f::es,rest)=findeps(e, es,rest@[E.Field f])
    | findeps(e, E.Tensor t::es,rest)=findeps(e, es,rest@[E.Tensor t])
    | findeps(e,es,rest)= (e, rest@es,[])
                
                
        
fun prodAppPartial ([e1],p1)= E.Apply(E.Partial p1,e1)
  | prodAppPartial((e1::e2),p1)=let
    val l= prodAppPartial(e2,p1)
    val (_,e2')= mkProd[e1,l]
    val (_,e1')=mkProd(e2@ [E.Apply(E.Partial p1, e1)])
    in
        E.Add[e1',e2']
    end
                

                
        
(*remove eps Index*)

fun rmEpsIndex(i,[],[],[])= []
    | rmEpsIndex(i,[],x,[])= [x]
    | rmEpsIndex(i,[],x,e::es)= rmEpsIndex(i,e,[],es)@[x]
    | rmEpsIndex(i,  ( c ,lb, ub)::b,x, es)=
        if (i=c) then let
                val z=[(x@b)]
                in (case z of [] => es |_=>z@es) end
        else rmEpsIndex(i,b,x@[(c ,lb, ub)],es)
  
                
  
fun doubleEps(count,E.Epsilon (a,b,c),E.Epsilon(d,e,f))=let
    (*Function is called when eps are being changed to deltas*)
    fun createDeltas(i,s,t,u,v)= let
        val c'= rmEpsIndex(E.V i,[],[],count)
        val d1=[E.Delta(E.V s,E.V u), E.Delta(E.V t,E.V v)]
        val d2= [E.Delta(E.V s,E.V v), E.Delta(E.V t,E.V u)]
                
        in (1,c',d1,d2)
        end
                
    in if(a=d) then createDeltas(a,b,c,e,f)
        else if(a=e) then createDeltas(a,b,c,f,d)
        else if(a=f) then createDeltas(a,b,c,d,e)
        else if(b=d) then createDeltas(b,c,a,e,f)
        else if(b=e) then createDeltas(b,c,a,f,d)
        else if(b=f) then createDeltas(b,c,a,d,e)
        else if(c=d) then createDeltas(c,a,b,e,f)
        else if(c=e) then createDeltas(c,a,b,f,d)
        else if(c=f) then createDeltas(c,a,b,d,e)
        else (0,[],[],[])
    end
                
                
                
fun distEps([],eps,_,_)=(0,[],[],[],[])
    | distEps([e],eps,_,_)=(0,[],[],[],[])
    | distEps(e1::e2::[],eps,c1::count,sx)=let
        val(change,c',d1,d2)= doubleEps([c1@sx]@count,e1,e2)
        in (case change
        of 1=>(1, c', eps, d1,d2)
        |_=> (0,[],[],[],[])
        (*end case*))
        end
           
    | distEps(e1::e2::current,eps,count,sx)=let
        val(change,c',d1,d2)= doubleEps(count,e1,e2)
        in (case change
            of 1=>(1, c', eps@current, d1,d2)
            |_=> distEps(e2::current, eps@[e1],count,sx)
            (*end case*))
        end
                

                   
(* Transform eps to deltas*)
fun epsToDels(count,E.Prod e)= let
    val (epsA,es,sx)=findeps([],e,[])
    val (change, s', eps,d1,d2)= distEps(epsA,[],count,sx)
    val deltas=E.Sub(E.Prod d1,E.Prod d2)
    
    in (case (change,eps,es)
        of (0,_,_)=>(print "nooo";(0,[],epsA,es))
        |(_,[],[]) =>(1,s',[deltas],[])
        | _ =>(1,s',[E.Sub( E.Prod(eps@d1@es), E.Prod(eps@d2@es))],[])
        (*end case *))
    end

(*Another strategy. Go through entire expression inside summation and jsut examine index to apply deltas*)
                
(* Apply deltas to tensors/fields*)
fun reduceDelta(c, eps, dels, es)=let
    fun distribute(change,d,dels,[],done)=(change,dels@d,done)
        | distribute(change,[],[],e,done)=(change,[],done@e)
        | distribute(change,[],dels,e::es,done)=distribute(change,dels,[],es,done@[e])
        | distribute(change,E.Delta(i,j)::ds,dels,e::es,done)=(case e
                of  E.Tensor(id,[tx])=>
                    if(j=tx) then distribute(change@[j],dels@ds,[] ,es ,done@[E.Tensor(id,[i])])
                    else distribute(change,ds,dels@[E.Delta(i,j)],E.Tensor(id,[tx])::es,done)
            |  E.Field(id,[tx])=>
                    if(j=tx) then distribute(change@[j],dels@ds,[] ,es ,done@[E.Field(id,[i])])
                    else distribute(change,ds,dels@[E.Delta(i,j)],E.Field(id,[tx])::es,done)
            | E.Apply(E.Partial d,e)=>let
                fun distPart([],rest) =(0 ,rest)
                    | distPart(p::pd,rest)=
                        if(p=j) then (1,rest@[i]@pd)
                        else (distPart(pd,rest@[p]))
                        val (change'',p')=distPart(d,[])
                in (case change''
                    of 0=>distribute(change, ds,dels@[E.Delta(i,j)], [E.Apply(E.Partial d, e)]@es,done)
                    |_=> distribute(change@[j], dels@ds,[], es,done@[E.Apply(E.Partial p', e)])
                    (*end case*))
                end
            | _=>distribute(change,dels@[E.Delta(i,j)]@ds,[],es,done@[e])
            (*end case*))

    val (change,dels',done)=distribute([],dels,[],es,[])
     fun m([],c')=c'
        | m(e::es,c')= let val s=rmEpsIndex(e,[],[],c')
          in m(es, s) end
    val index= m(change, c)
    in
       (length change, index,E.Prod (eps@dels'@done))
    end



                
(*Apply*)
fun mkApply(E.Apply(E.Partial d,e))=(case e
    of E.Tensor(a,[])=>(1,E.Const 0)
    | E.Const _ =>(1,E.Const 0)
    | E.Delta _ =>(1,E.Const 0)
    | E.Value _ =>(1,E.Const 0)
    | E.Epsilon _ =>(1,E.Const 0)
    | E.Conv (fid,alpha,tid, delta)=> (1, E.Conv(fid,alpha, tid, delta@d))
    | E.Add l => (1,E.Add(List.map (fn e => E.Apply(E.Partial d, e)) l))
    | E.Sub(e2, e3) =>(1, E.Sub(E.Apply(E.Partial d, e2), E.Apply(E.Partial d, e3)))
    | E.Div(e2, e3) =>(1, E.Div(E.Apply(E.Partial d, e2),  e3))(********FIXX******)
    | E.Apply(E.Partial d2,e2)=>(1,E.Apply(E.Partial(d@d2), e2))
    | E.Prod [e1]=>(1,E.Apply(E.Partial d,e1))
    | E.Prod es=> let 
        val (pre,eps,dels, post)= filter2(es,[],[],[],[])
        val (_,x)=mkProd(pre@eps@dels@[prodAppPartial(post,d)])
        in (1,x) end
    |_=>(0,E.Apply(E.Partial d,e))
    (* end case*))
 
(*Sum Apply*)
            
                
fun matchEps(2,_,_,_)= 1 (*matched 2*)
    | matchEps(num,_,_,[])=0
    | matchEps(0,_,_,[eps])=0
    | matchEps(num,[],rest,eps::epsx)=
        matchEps(num,rest,[],epsx)
    | matchEps(num,E.V p::px,rest,eps::epsx)=
        if(p=eps) then (matchEps(num+1,rest@px,[],epsx))
        else matchEps(num,px,rest@[E.V p], eps::epsx)
    | matchEps(num,p::px,rest,eps)=
        matchEps(num,px,rest,eps)
                

               
           (*
                     
fun epsapply(c,eps,dels,post)=let
    fun  applyEps(change,count,[],[],rest,done)= (print "kkk";(0,E.Const 0))
        | applyEps(change,count,eps,epsrest,[],done)=(print "yyyy";(0,E.Const 0))
        | applyEps(change,count,[],epsrest,r::rest,done)=(print "bb";applyEps(change,count,epsrest,[],rest,done@[r]))
        | applyEps(change,count,eps1::eps,epsrest,r::rest,done)=( print "lrr";(case (r,eps1)
            of (E.Sum(c2,E.Prod(E.Epsilon eps2::s2)),_)=> let
                val (change', s',_,d1,d2)= distEps([eps1,E.Epsilon eps2],[],count@c2)
                in (case change'
                        of 1=> let
                            val (_,p1)=mkProd(epsrest@eps@d1@dels@done@s2@rest)
                            val (_,p2)=mkProd(epsrest@eps@d2@dels@done@s2@rest)
                            in
                            (1,E.Sum(s',E.Sub(p1,p2))) end
                    | _=>applyEps(change,count,eps, epsrest@[eps1],r::rest,done)
                    (*end case*))
                end
            | (E.Conv(v,vx, h ,d),E.Epsilon(i,j,k))=> let
                val change'= matchEps(0,d,[],[i,j,k])
                in (case change' of 1=>  (1,E.Const 0)
                    |_=>  applyEps(0,count,epsrest@[eps1],[],rest,done@[r]))
                end
            |_=>applyEps(0,count,epsrest@[eps1],[],rest,done@[r])
            (*end case*)))
                
    val (change, s', eps',d1,d2)= distEps(eps,[],c)
    in (case change
        of 1 =>  let
            val (_,p1)=mkProd(eps'@d1@dels@post)
            val (_,p2)=mkProd(eps'@d2@dels@post)
                in  (print "in changed";(1,E.Sub(E.Sum(s',p1), E.Sum(s',p2)))) end
        |_=> (print "in apply eps";applyEps(0,c,eps,[],post,[]))
        (*end case*))
    end 
        
                
                *)
                
 (*print summation range*)
fun handleIndex e= (case e
    of E.C(cx)=> String.concat["'",Int.toString(cx),"'"]
    |  E.V(ix)=> Int.toString(ix)
)
fun handleSumRange (mu,lb,ub)= print(String.concat[(handleIndex mu),"[",Int.toString(lb),"-",Int.toString(ub),"]"])
fun printSx e=(print "\n $";List.map handleSumRange e; print "$")

fun K gg=String.concatWith "," (List.map (fn (E.V e1,_,_)=> (Int.toString(e1))) gg)
fun Kt gg=List.map (fn e1=> print(String.concat["[", (K e1),"]"])) gg

(*Apply normalize to each term in product list
or Apply normalize to tail of each list*)
fun normalize (Ein.EIN{params, index, body}) = let
                (* val _ = print(String.concat["\n IN NORMALIZE@", P.printbody(body),"@\n"])*)
      val changed = ref false
      val sumIndex=ref []
    
      fun rewriteBody body =
            (case body
             of E.Const _=> body
              | E.Tensor _ =>body
              | E.Field _=> body
              | E.Delta _ => body
              | E.Value _ =>body
              | E.Epsilon _=>body
              | E.Conv _=>body                
           
              | E.Neg(E.Neg e)=> rewriteBody e
              | E.Neg e => E.Neg(rewriteBody e)
              | E.Add es => let val (change,body')= mkAdd(List.map rewriteBody es)
                   in if (change=1) then ( changed:=true;body') else body' end
              | E.Sub(a, E.Field f)=> (changed:=true;E.Add[a, E.Neg(E.Field(f))])
              | E.Sub(E.Sub(a,b),E.Sub(c,d))=> rewriteBody(E.Sub(E.Add[a,d],E.Add[b,c]))
              | E.Sub(E.Sub(a,b),e2)=>rewriteBody (E.Sub(a,E.Add[b,e2]))
              | E.Sub(e1,E.Sub(c,d))=>rewriteBody(E.Add([E.Sub(e1,c),d]))
              | E.Sub (a,b)=>  E.Sub(rewriteBody a, rewriteBody b)
              | E.Div(E.Div(a,b),E.Div(c,d))=> rewriteBody (E.Div(E.Prod[a,d],E.Prod[b,c]))
              | E.Div(E.Div(a,b),c)=> rewriteBody (E.Div(a, E.Prod[b,c]))
              | E.Div(a,E.Div(b,c))=>  rewriteBody (E.Div(E.Prod[a,c],b))
                | E.Div (a, b) => E.Div(rewriteBody a, rewriteBody b)
              | E.Partial _=>body
              | E.Krn(tid,deltas,pos)=> E.Krn(tid,deltas, (rewriteBody pos))
              | E.Img(fid,alpha,pos)=> E.Img(fid,alpha, (List.map rewriteBody pos))
             
            
                (*************Product**************)
              | E.Prod [e1] => rewriteBody e1
              | E.Prod((E.Div(e2,e3))::e4)=>
                (changed :=true; E.Div(E.Prod([e2]@e4), e3 ))
              | E.Prod((E.Add(e2))::e3)=>
                   (changed := true; E.Add(List.map (fn e=> E.Prod([e]@e3)) e2))
              | E.Prod(e1::E.Add(e2)::e3)=>
                (changed := true; E.Add(List.map (fn e=> E.Prod([e1,e]@e3)) e2))
              | E.Prod((E.Sub(e2,e3))::e4)=>
                (changed :=true; E.Sub(E.Prod([e2]@e4), E.Prod([e3]@e4 )))
              | E.Prod(e1::E.Sub(e2,e3)::e4)=>
                (changed :=true; E.Sub(E.Prod([e1,e2]@e4), E.Prod([e1,e3]@e4 )))
                
              | E.Prod [E.Partial r1,E.Partial r2]=>
                (changed:=true;E.Partial(r1@r2))
              | E.Prod(E.Partial r1::E.Partial r2::p)=>
                (changed:=true;E.Prod([E.Partial(r1@r2)]@p))
                
                
                (*************Product EPS **************)
                
                (* Apply (d, e) shoudl be convereted to Conv operator *)
              | E.Prod(E.Epsilon(i,j,k)::E.Apply(E.Partial d,e)::es)=>let
                 val change= matchEps(0,d,[],[i,j,k])
                 in case (change,es)
                of (1,_) =>(changed:=true; E.Const 0)
                    | (_,[]) =>E.Prod[E.Epsilon(i,j,k),rewriteBody (E.Apply(E.Partial d,e))]
                    |(_,_)=> let
                        val a=rewriteBody(E.Prod([E.Apply(E.Partial d,e)]@ es))
                        val (_,b)=mkProd [E.Epsilon(i,j,k),a]
                        in b end 
                end
              | E.Prod(E.Epsilon(i,j,k)::E.Conv(V,alpha, h, d)::es)=>let
                
                val change= matchEps(0,d,[],[i,j,k])
                in case (change,es)
                    of (1,_) =>(changed:=true; E.Const 0)
                    | (_,[]) =>E.Prod[E.Epsilon(i,j,k),E.Conv(V,alpha, h, d)]
                    | (_,_) =>let
                        val a=rewriteBody(E.Prod([E.Conv(V,alpha, h, d)]@ es))
                        val (_,b) = mkProd [E.Epsilon(i,j,k),a]
                        in b end
                end
                
                
              | E.Prod[(E.Epsilon(e1,e2,e3)), E.Tensor(_,[E.V i1,E.V i2])]=>
                    if(e2=i1 andalso e3=i2) then (changed :=true;E.Const(0))
                    else body


            | E.Prod(E.Epsilon eps1::ps)=>
                let
        
                val ref x=sumIndex
                val (i,s',e,rest)=epsToDels(x,body)
                in (case (i, e,rest)
                of (1,[e1],_) =>(changed:=true;sumIndex:=s';e1)
                    |(0,eps,[])=>body
                    |(0,eps,rest)=> let
                        val p'=rewriteBody(E.Prod rest)
                        val p''= (case p' of E.Prod p=>p |e=>[e])
                        val(_,b)= mkProd (eps@p'')
                        in b end
                        (*end case*))
                 end
            | E.Prod(E.Sum(c1,E.Prod(E.Epsilon e1::es1))::E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es)=>let
                val ref x=sumIndex
          
                
                val m= Kt x
                
                val c'= [c1@c2]@x
                val (i,s',e,rest)=epsToDels(c', E.Prod([E.Epsilon e1, E.Epsilon e2]@es1@es2@es))
                 val gsg=Kt s'
                
          
                in (case (i, e,rest)
                    of (1,[e1],_)=> (changed:=true;sumIndex:=s';let
                        val ss=List.nth(s',((length s')-2))
                        in 
                            E.Sum(ss,e1) end )
                    | _=>let
                        val eA=rewriteBody(E.Sum(c1,E.Prod(E.Epsilon e1::es1)))
                        val eB=rewriteBody(E.Prod(E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es))
                        val (_,e)=mkProd([eA,eB])
                        in e
                        end
                    (*end case*))
                end
                
            | E.Prod(E.Delta d::es)=>let
                val (pre',eps, dels,post)= filter2(E.Delta d::es,[],[],[],[])
                val ref x=sumIndex
                val (change,i',a)=reduceDelta(x, eps, dels, post)
                
                in (case (change,a)
                    of (0, _)=> E.Prod [E.Delta d,rewriteBody(E.Prod es)]
                    | (_, E.Prod p)=>let
                        val (_, p') = mkProd p
                        in (changed:=true;sumIndex:=i';p') end
                    | _ => (changed:=true;sumIndex:=i';a )
                    (*end case*))
                    end
                
              | E.Prod[e1,e2]=> let val (_,b)=mkProd[rewriteBody e1, rewriteBody e2] in b end
              | E.Prod(e::es)=>let
                    val e'=rewriteBody e
                    val e2=rewriteBody(E.Prod es)
                    val(_,b)=(case e2
                        of E.Prod p'=> mkProd([e']@p')
                        |_=>mkProd [e',e2])
                in b
                   end                
              (**************Apply**************)
       
              (* Apply, Sum*)
              | E.Apply(E.Partial d,E.Sum (c,e))=> let
                  val ref x=sumIndex
                  val x'=[c]@x
                  val e' = (sumIndex:=x';rewriteBody(E.Apply(E.Partial d, e)))
                  val ref s=sumIndex
                  in 
                    (sumIndex:=tl(s);E.Sum(hd(s), e'))
                  end
            
              | E.Apply(E.Partial [],e)=> e
              | E.Apply(E.Partial p,E.Probe(E.Conv(fid,alpha,tid,d),x))=>
                (changed:=true;E.Probe(E.Conv(fid,alpha,tid,d@p),x))
              | E.Apply(E.Partial p,E.Conv(fid,alpha,tid,d))=>
                (changed:=true;E.Conv(fid,alpha,tid,d@p))
              | E.Apply(E.Partial p, e)=>let
                    val body'=E.Apply(E.Partial p, rewriteBody e)
                    val (c, e')=mkApply(body')
                    in (case c
                        of 1=>(changed:=true;e')
                        | _ =>e') end
              | E.Apply(e1,e2)=>E.Apply(rewriteBody e1, rewriteBody e2)
                
              (************** Sum *****************)
              | E.Sum([],e)=> (changed:=true;rewriteBody e)
              | E.Sum(_,E.Const c)=>(changed:=true;E.Const c)
              | E.Sum(c, E.Sum(c', e))=> (changed:=true; E.Sum(c@c', e))
              | E.Sum(c, E.Sub(e1,e2))=>(changed:=true; E.Sub(E.Sum(c,e1), E.Sum(c, e2)))
              | E.Sum(c,(E.Add l))=> (changed:=true;E.Add(List.map (fn e => E.Sum(c,e)) l))
              | E.Sum(c,E.Div(e1,e2))=>(changed:=true; E.Div(E.Sum(c,e1),E.Sum(c,e2)))
              | E.Sum(c, E.Prod(E.Const e::es))=>(changed:=true;E.Prod[E.Const e,E.Sum(c, E.Prod es)])
              | E.Sum(c, E.Prod(E.Value v::es))=>(changed:=true; E.Prod [E.Value v, E.Sum(c, E.Prod es)])
                | E.Sum(c, E.Prod(E.Tensor(id,[])::es))=> (changed:=true;E.Prod [E.Tensor(id,[]), E.Sum(c, E.Prod es)])
             
             | E.Sum(c,E.Prod e)=> let
                    val e' =rewriteBody(E.Prod e)
                    (*val _=print (String.concat["\n change \n",P.printbody(body ),"\n ==>\n",P.printbody(E.Sum(c,e')),"\n"])*)
                    val b'= (case e'
                        of E.Prod p=>let
                            val (_,b)=mkProd p
                            in b end
                        |_=>e'
                        (* end case*))
                    val _ =print(P.printbody( b'))
                    in E.Sum(c,b')
                    end 
              | E.Sum(c,e)=>let
                    
                    val ref x=sumIndex
                    val c'=[c]@x
                
                    val A= Kt c'
                    val e'=(sumIndex:=c';rewriteBody e)
                    val ref s=sumIndex

                    val C= Kt s
                    val z=hd(s)
             
                    val B= Kt [z]
                    val D=Kt (tl(s))
                in (sumIndex:=tl(s);E.Sum(z, e')) end
            
            (*******************Probe*****************)
            | E.Probe(E.Sum(c,s),x)=>(changed:=true;E.Sum(c,E.Probe(s,x)))
            | E.Probe(E.Tensor t,_)=> E.Tensor t            
            | E.Probe(E.Neg e1,x)=>(changed:=true;E.Neg(E.Probe(e1,x)))
            | E.Probe(E.Add es,x) =>
                (changed:=true;E.Add(List.map (fn(e1)=>E.Probe(e1,x)) es))
            | E.Probe(E.Sub (a,b),x)=>
                (changed:=true;E.Sub(rewriteBody(E.Probe(a,x)), rewriteBody(E.Probe(b,x))))
            | E.Probe(E.Div (a,b),x) =>
                (changed:=true;E.Div(rewriteBody(E.Probe(a, x)),b))
            | E.Probe(E.Prod p, x)=>let
                val _=print ("\n probe of prod \n ")
                val (p',x')= (rewriteBody (E.Prod p), rewriteBody x)
                fun  probeprod([],[e1]) =e1
                  | probeprod([],rest) = E.Prod rest                
                  | probeprod(E.Const c::es,rest)=
                        (changed:=true;probeprod(es,rest@[E.Const c]))
                  | probeprod(E.Tensor t::es,rest)=
                        (changed:=true;probeprod(es,rest@[E.Tensor t]))
                  | probeprod(E.Krn e::es, rest)=
                        (changed:=true;probeprod(es, rest@[E.Krn e]))
                  | probeprod(E.Delta e::es, rest)=
                        (changed:=true;probeprod(es, rest@[E.Delta e]))
                  | probeprod(E.Value e::es, rest)=
                        (changed:=true;probeprod(es, rest@[E.Value e]))
                  | probeprod(E.Epsilon e::es, rest)=
                        (changed:=true;probeprod(es, rest@[E.Epsilon e]))
                  | probeprod(E.Partial e::es, rest)=
                        (changed:=true;probeprod(es, rest@[E.Partial e]))
                  | probeprod(E.Field f::es,rest)=
                        (changed:=true;print "\n $#$ Found Field";probeprod(es,rest@[E.Probe(E.Field f, x')]))

                      (*  (changed:=true;E.Prod(rest@[E.Probe(E.Field f, x')] @es))*)
                  | probeprod(E.Conv f::es,rest)=
                            (changed:=true;print "\n $#$ Found Field";probeprod(es,rest@[E.Probe(E.Conv f, x')]))

                        (*(changed:=true;E.Prod(rest@[E.Probe(E.Conv f, x')] @es))*)
                  | probeprod(E.Prod p::es , rest)=
                        (changed:=true;probeprod(p@es,rest))
                    | probeprod(E.Sum(c,e)::es, rest)=
                         (changed:=true;;print "\n $#$ Found Sum in probe product \n ";probeprod(es,rest@[E.Sum(c,E.Probe(e, x'))]))
                  | probeprod(_,[])=body
                  | probeprod(e1::es, rest)=let
                        val e'= rewriteBody(E.Prod(e1::es))
                        val e''= rewriteBody(E.Probe(e',x'))
                    in  (changed:=true;E.Prod(rest@[e'']))
                    end
                in (case p'
                    of E.Prod pro=>probeprod(p,[])
                    |_=> E.Probe(p',x')
                    (*end case*))
                end
                | E.Probe(u,v)=>  (E.Probe(rewriteBody u, rewriteBody v))
            (*end case*))
  
            fun loop(body ,count) = let
                val body' = rewriteBody body
               
                           in 
              if !changed
                then (print(String.concat["\n=>",P.printbody(body')]);
                changed := false ;sumIndex:=[];loop(body',count+1))
                else (body',count)
            end

    val (b,count) = loop(body,0)
    val _ = print(String.concat["\n out of normalize \n",P.printbody(b),"\n Final CounterXX:",Int.toString(count),"\n\n"])
    in
                (Ein.EIN{params=params, index=index, body=b},count)
    end
end
                


end (* local *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0