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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3174 - (download) (annotate)
Mon Mar 30 11:46:58 2015 UTC (3 years, 11 months ago) by cchiw
File size: 12158 byte(s)
hack
structure NormalizeEin = struct

    local

    structure E = Ein
    structure P=Printer
    structure F=Filter
    structure G=EpsHelpers
    structure Eq=EqualEin
    structure R=RationalEin

    in

    val testing=0
    fun err str=raise Fail (String.concat["Ill-formed EIN Operator",str])
    fun mkProd e= F.mkProd e
    fun filterSca e=F.filterSca e
    fun mkAdd e=F.mkAdd e
    fun filterGreek e=F.filterGreek e
    fun mkapply e= derivativeEin.mkapply e
    fun testp n=(case testing
        of 0=> 1
        | _ =>(print(String.concat n);1)
        (*end case*))


    (*mkSum:sum_indexid list * ein_exp->int *ein_exp
    *distribute summation expression 
    *)
    fun mkSum(c1,e1)=(case e1
        of E.Lift e   => (1,E.Lift(E.Sum(c1,e)))
        | E.Tensor(_,[]) => (1,e1)
        | E.Const _   => (1,e1)
        | E.ConstR _  => (1,e1)
        | E.Prod p    => filterSca(c1,p)
        | _           => (0,E.Sum(c1,e1))
        (*end case*))


    (*mkprobe:ein_exp* ein_exp-> int ein_exp
    *rewritten probe
    *)
    fun mkprobe(e1,x)=let
        val (c,rtn)=(case e1
            of E.Lift e   => (1,e)
            | E.Sqrt a    => (1,E.Sqrt(E.Probe(a,x)))
            | E.Cosine a    => (1,E.Cosine(E.Probe(a,x)))
            | E.ArcCosine a    => (1,E.ArcCosine(E.Probe(a,x)))
            | E.Sine a    => (1,E.Sine(E.Probe(a,x)))
            | E.ArcSine a    => (1,E.ArcSine(E.Probe(a,x)))
            | E.PowReal(a,n1)    => (1,E.PowReal(E.Probe(a,x),n1))
            | E.Prod []   => err("Probe of empty product")
            | E.Prod p    => (1,E.Prod (List.map (fn(a)=>E.Probe(a,x)) p))
            | E.Apply _   => (0,E.Probe(e1,x))
            | E.Conv _    => (0,E.Probe(e1,x))
            | E.Field _   => (0,E.Probe(e1,x))
            | E.Sum(c,e') =>  (1,E.Sum(c,E.Probe(e',x)))
            | E.Add e     => (1,E.Add (List.map (fn(a)=>E.Probe(a,x)) e))
            | E.Sub (a,b) => (1,E.Sub(E.Probe(a,x),E.Probe(b,x)))
            | E.Neg a    => (1,E.Neg(E.Probe(a,x)))
            | E.Div (a,b) => (1,E.Div(E.Probe(a,x),E.Probe(b,x)))
            | E.Const _   => (1,e1)
            | Ein.ConstR _          =>(1,e1)
            | E.Tensor _  => err("Tensor without Lift")
            | E.Delta _   => (0,e1)
            | E.Epsilon _ => (0,e1)
            | E.Eps2 _    => (0,e1)
            | E.Partial _ => err("Probe Partial")
            | E.Probe _   => err("Probe of a Probe")
            | E.Krn _     => err("Krn used before expand")
            | E.Value _   => err("Value used before expand")
            | E.Img _     => err("Probe used before expand")
            (*end case*))
        in
            (c,rtn)
        end

    (* normalize: EIN->EIN
    * rewrite body of EIN
    * note "c" keeps track if ein_exp is changed 
    *)
    fun normalize (ee as Ein.EIN{params, index, body},args) = let
      val changed = ref false
      fun rewriteBody body =(case body
        of E.Const _    => body
        | Ein.ConstR _  => body
        | E.Tensor _    => body
        | E.Field _     => body
        | E.Delta _     => body
        | E.Epsilon _   => body
        | E.Eps2 _      => body
        | E.Conv _      => body
        | E.Partial _   => body
        | E.Krn _       => raise Fail"Krn before Expand"
        | E.Img _       => raise Fail"Img before Expand"
        | E.Value _     => raise Fail"Value before Expand"
        | E.Neg(E.Neg e)    => rewriteBody e
        | E.Neg e           => E.Neg(rewriteBody e)
        | E.Lift e          => E.Lift(rewriteBody e)
        | E.Sqrt e          => E.Sqrt(rewriteBody e)
        | E.Cosine e        => E.Cosine(rewriteBody e)
        | E.ArcCosine e     => E.ArcCosine(rewriteBody e)
        | E.Sine e          => E.Sine(rewriteBody e)
        | E.ArcSine e       => E.ArcSine(rewriteBody e)
        | E.PowInt(e,n1)    => E.PowInt(rewriteBody e,n1)
        | E.PowReal(e,n1)   => E.PowReal(rewriteBody e,n1)
            (*************Algebraic Rewrites **************)
        | 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(e1 as E.Tensor(_,[_]),e2 as E.Tensor(_,[]))=>
                rewriteBody (E.Prod[E.Div(E.Const 1, e2),e1])

        | 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))
            (**************Apply, Sum, Probe**************)
        | E.Apply(E.Partial [],e)   => e
        | E.Apply(E.Partial d1, e1) =>
            let
            val e2 = rewriteBody e1
            val (c,e3)=mkapply(E.Partial d1,e2)
            in
                (case c of 1=>(changed:=true;e3)| _ =>e3 (*end case*))
            end
        | E.Apply _                 => raise Fail" Not well-formed Apply expression"
        | E.Sum([],e)               => (changed:=true;rewriteBody e)
        | E.Sum(c,e)                => let
            val (c,e')=mkSum(c,rewriteBody e)
            in
                (case c of 0 => e'|_ => (changed:=true;e'))
            end
        | E.Probe(u,v)              =>
            let
            val (c',b')=mkprobe(rewriteBody u,rewriteBody v)
            in (case c'
                of 1=> (changed:=true;b')
                |_=> b'
                (*end case*))
            end
        (*************Product**************)
        | E.Prod [] => raise Fail"missing elements in product"
        | E.Prod [e1] => rewriteBody e1
(*
        | E.Prod((E.Add(e2))::e3)=>
           (changed := true; E.Add(List.map (fn e=> E.Prod([e]@e3)) e2))
        | E.Prod((E.Sub(e2,e3))::e4)=>
            (changed :=true; E.Sub(E.Prod([e2]@e4), E.Prod([e3]@e4 )))
*)

        | E.Prod((E.Div(e2,e3))::e4)=> (changed :=true; E.Div(E.Prod([e2]@e4), e3))

(*
        | E.Prod(e1::E.Add(e2)::e3)=>
            (changed := true; E.Add(List.map (fn e=> E.Prod([e1,e]@e3)) e2))
        | E.Prod(e1::E.Sub(e2,e3)::e4)=>
            (changed :=true; E.Sub(E.Prod([e1,e2]@e4), E.Prod([e1,e3]@e4 )))
*)

        | E.Prod((e1 as E.Sqrt(s1))::(e2 as E.Sqrt(s2))::es)=>
            if(Eq.isBodyEq(s1,s2)) then (changed :=true;s1)
            else let
                val a=rewriteBody e1
                val b=rewriteBody (E.Prod([e2]@es))
                val  (_,d)=mkProd ([a,b])
                in d 
                end
   
        (*************Product EPS **************)

        | E.Prod(E.Epsilon(i,j,k)::E.Apply(E.Partial d,e)::es)=>let
            val change= G.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(V1,[a1], h1, d1)::E.Conv(V,alpha, h, d)::es)=>let
            val change= G.matchEps(0,alpha@d,[],[i,j,k])
            in case (change,es)
                of (1,_) =>(changed:=true; E.Lift(E.Const 0))
                | (_,[]) =>E.Prod[E.Epsilon(i,j,k),E.Conv(V1,[a1], h1, d1),E.Conv(V,alpha, h, d)]
                | (_,_) =>let
                    val a=rewriteBody(E.Prod([E.Conv(V1,[a1], h1, d1),E.Conv(V,alpha, h, d)]@ 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= G.matchEps(0,d,[],[i,j,k])
            in case (change,es)
                of (1,_) =>(changed:=true; E.Lift(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 e1::E.Sum(c1,E.Prod(E.Sum(c2,E.Prod(E.Epsilon e2::es3))::es2))::es1) =>
            (case G.epsToDels([E.Epsilon e1, E.Epsilon e2]@es3@es2@es1)
            of (1,e,sx,_,_)=> (changed:=true; E.Sum(c1@c2@sx,e))
            | (_,_,_,_,_)=>let
                    val eA=rewriteBody(E.Epsilon e1)
                    val eB=rewriteBody(E.Prod(E.Sum(c1,E.Prod(E.Sum(c2,E.Prod(E.Epsilon e2::es3))::es2))::es1))
                    val (_,e)=mkProd([eA,eB])
                in
                    e
                end
            (*end case*))
*)
        | E.Prod(E.Epsilon eps1::ps)=> (case (G.epsToDels(E.Epsilon eps1::ps))
            of (1,e,[],_,_)      =>(changed:=true;e)(* Changed to Deltas*)
            | (1,e,sx,_,_)      =>(changed:=true;E.Sum(sx,e))
                    (* Changed to Deltas *)
            | (_,_,_,_,[])   =>  body
            | (_,_,_,epsAll,rest) => let
                val p'=rewriteBody(E.Prod rest)
                val(_,b)= mkProd(epsAll@[p'])
                in b end
            (*end case*))
        | E.Prod(E.Sum(c1,E.Prod(E.Epsilon e1::es1))::E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es) =>
            (case G.epsToDels([E.Epsilon e1, E.Epsilon e2]@es1@es2@es)
                of (1,e,sx,_,_)=> (changed:=true; E.Sum(c1@c2@sx,e))
                | (_,_,_,_,_)=>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*))
        | E.Prod[E.Delta d, E.Neg e]=> (changed:=true;E.Neg(E.Prod[E.Delta d, e]))
        | E.Prod(E.Delta d::es)=>let
            val (pre',eps, dels,post)= filterGreek(E.Delta d::es)
             val _= testp["\n\n Reduce delta--",P.printbody(body)]
            val (change,a)=G.reduceDelta(eps, dels, post)
              val _= testp["\n\n ---delta moved--",P.printbody(a)]
            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;p') end
                | _ => (changed:=true;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                

    (*end case*))

    val _=testp["\n********Normalize",P.printerE ee,"\n*****\n"]
    fun loop(body ,count) = let
        val _= testp["\n\n N =>",Int.toString(count),"--",P.printbody(body)]
        val body' = rewriteBody body

        in
            if !changed
            then  (changed := false ;loop(body',count+1))
            else (body',count)
        end
    val _ =testp["\n ******************* \n Start Normalize \n\n "]
    val (b,count) = loop(body,0)
    val _ =testp["\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