Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

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

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

Mon Nov 4 16:10:36 2013 UTC (5 years, 10 months ago) by cchiw
Original Path: branches/charisee/src/compiler/high-il/normalize-ein.sml
File size: 22088 byte(s)
`remove generic rep, change Summation index, and add tests`
```structure NormalizeEin = struct

local

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

(*Flattens Add constructor: change, expression *)
|flatten(i,((E.Const c):: l'))=
if (c>0.0 orelse c<0.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.0))
| [e] => (1,e)
(* 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.0 orelse  0.0>c) then (3,[E.Const 0.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.0))
else case a
of [] => (1,E.Const(0.0))
| [e] => (1,e)
| es => (change, E.Prod es)
(* end case *)
end

(* filter function shifts constant/greeks to outside product*)
fun filter([],pre,post)=(pre,post)
| filter(E.Const c::es, pre, post)=filter(es, pre@[E.Const c],post)
| filter(E.Delta d::es,pre,post)=filter(es,pre@[E.Delta d],post)
| filter(E.Value v::es, pre, post)=filter(es, pre@[E.Value v],post)
| filter(E.Epsilon e::es, pre, post)=filter(es, pre@[E.Epsilon e],post)
| filter(E.Tensor(id,[])::es, pre, post)=filter(es, pre@[E.Tensor(id,[])],post)
| filter(E.Prod p::es, pre, post)=filter(p@es,pre,post)
| filter(e::es, pre, post)= filter(es, pre, post@[e])

fun prodPartial ([e1],p1)= E.Prod[E.Partial p1,e1]
| prodPartial((e1::e2),p1)=let
val l= prodPartial(e2,p1)
val (_,e2')= mkProd[e1,l]
val (_,e1')=mkProd(e2@ [E.Partial p1, e1])
in
end

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
end

(*remove eps Index*)
fun rmEpsIndex(i,[],rest)=rest
| rmEpsIndex(i, (E.V c ,lb, ub)::es,rest)=
if (i=c) then rest@es
else rmEpsIndex(i, es, rest@[(E.V c, lb, ub)])

(*remove index variable from list*)
fun rmIndex(_,_,[])=[]
| rmIndex([],[],cs)=cs
| rmIndex([],m ,e1::cs)=[e1]@rmIndex(m,[],cs)
| rmIndex(i::ix,rest ,(c,lb,ub)::cs)=
if(i=c) then rmIndex(rest@ix,[],cs)
else rmIndex(ix,rest@[i],(c,lb,ub)::cs)

(* Transform eps to deltas*)
fun epsToDels(E.Sum(count,E.Prod e))= let
fun doubleEps((E.Epsilon (a,b,c))::(E.Epsilon(d,e,f))::es,eps,e3)=
let

(*Function is called when eps are being changed to deltas*)
fun createDeltas(i,s,t,u,v, e3)= let

(*remove index from original index list*)

val s'= rmEpsIndex(i,count,[])

val deltas= E.Sub(
E.Prod([E.Delta(E.V s,E.V u), E.Delta(E.V t,E.V v)] @e3),
E.Prod([E.Delta(E.V s,E.V v), E.Delta(E.V t,E.V u)]@e3))

in (case (eps,es,s')
of ([],[],[]) =>(1,deltas)
|([],_,[]) =>(1,E.Prod( es@[deltas]))
|([],[],_)=>(1,E.Sum(s',deltas))
|([],_,_)=>(1,E.Sum(s',E.Prod(es@[deltas])))
|(_,_,[])=>(1,E.Prod(eps@es@[deltas]))
|_ =>(1, E.Sum(s', E.Prod(eps@es@[deltas])))
)
end

in if(a=d) then createDeltas(a,b,c,e,f, e3)
else if(a=e) then createDeltas(a,b,c,f,d, e3)
else if(a=f) then createDeltas(a,b,c,d,e, e3)
else if(b=d) then createDeltas(b,c,a,e,f, e3)
else if(b=e) then createDeltas(b,c,a,f,d,e3)
else if(b=f) then createDeltas(b,c,a,d,e,e3)
else if(c=d) then createDeltas(c,a,b,e,f,e3)
else if(c=e) then createDeltas(c,a,b,f,d,e3)
else if(c=f) then createDeltas(c,a,b,d,e,e3)
else (0,E.Const 0.0)
end
fun findeps(e,[])= (e,[])
| findeps(e,(E.Epsilon eps)::es)=  findeps(e@[E.Epsilon eps],es)
| findeps(e,es)= (e, es)

fun dist([],eps,rest)=(0,eps,rest)
| dist([e],eps,rest)=(0,eps@[e],rest)
| dist(c1::current,eps,rest)=let
val(i, exp)= doubleEps(c1::current,eps,rest)
in  (case i of 1=>(i,[exp],[E.Const 2.0])
|_=> dist(current, eps@[c1],rest))
end

val (es,rest)=findeps([],e)
in
dist(es,[],rest)
end

(* Apply deltas to tensors/fields*)
fun reduceDelta(E.Sum(c,E.Prod p))=let

fun findDeltas(dels,rest,E.Delta d::es)= findDeltas(dels@[E.Delta d], rest, es)
| findDeltas(dels,rest,E.Epsilon eps::es)=findDeltas(dels,rest@[E.Epsilon eps],es)
| findDeltas(dels,rest,es)=  (dels,rest,es)

fun distribute(change,d,dels,[],done)=(change,dels@d,done)
| distribute(change,[],[],e,done)=(change,[],done@e)
| distribute(change,E.Delta(i,j)::ds,dels,E.Tensor(id,[tx])::es,done)=
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)
| distribute(change,E.Delta(i,j)::ds,dels,E.Field(id,[tx])::es,done)=
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)
| distribute(change,d,dels,e::es,done)=distribute(change,dels@d,[],es,done@[e])

val (dels,eps,es)=findDeltas([],[],p)
val (change,dels',done)=distribute([],dels,[],es,[])
val index=rmIndex(change,[],c)

in
(length change, E.Sum(index,E.Prod (eps@dels'@done)))
end

(*Apply Sum*)
fun mkApplySum(E.Apply(E.Partial d,E.Sum(c,e)))=(case e
of E.Tensor(a,[])=>(1,E.Const 0.0)
| E.Const _ =>(1,E.Const 0.0)
| E.Delta _ =>(1,E.Const 0.0)
| E.Value _ =>(1,E.Const 0.0)
| E.Epsilon _ =>(1,E.Const 0.0)

| E.Add l => (1,E.Add(List.map (fn e => E.Apply(E.Partial d, E.Sum(c,e))) l))
| E.Sub(e2, e3) =>(1, E.Sub(E.Apply(E.Partial d, E.Sum(c,e2)), E.Apply(E.Partial d, E.Sum(c,e3))))
| E.Conv (fid,alpha,tid, delta)=> let
val e'=E.Conv(fid,alpha, tid, delta@d)
in (1,E.Sum(c,e')) end
| E.Prod [e1]=>(1,E.Apply(E.Partial d,E.Sum(c,e1)))
| E.Prod es=> let
val (pre, post)= filter(es,[],[])
val x1= prodAppPartial(post,d)
in  (case x1
| _ => (1,E.Sum(c, E.Prod(pre@[x1])))
(*end case*))
end
|_=>(0,E.Apply(E.Partial d,E.Sum(c,e)))
(* end case*))

(*Apply*)
fun mkApply(E.Apply(E.Partial d,e))=(case e
of E.Tensor(a,[])=>(1,E.Const 0.0)
| E.Const _ =>(1,E.Const 0.0)
| E.Delta _ =>(1,E.Const 0.0)
| E.Value _ =>(1,E.Const 0.0)
| E.Epsilon _ =>(1,E.Const 0.0)
| E.Conv (fid,alpha,tid, delta)=> let
val e'=E.Conv(fid,alpha, tid, delta@d)
in (1,e') end
| 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))
| 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, post)= filter(es,[],[])
val (_,x)=mkProd(pre@[prodAppPartial(post,d)])
in (1,x) end
|_=>(0,E.Apply(E.Partial d,e))
(* end case*))

(*Sum Apply*)

fun mkSumApply(E.Sum(c,E.Apply(E.Partial d,e)))=(case e

of E.Const _=>(1,E.Const 0.0)
| E.Tensor(_,[])=> (1,E.Const 0.0)
| E.Delta _ =>(1,E.Const 0.0)
| E.Value _ =>(1,E.Const 0.0)
| E.Epsilon _ =>(1,E.Const 0.0)
| E.Conv (fid,alpha,tid, delta)=> let
val e'=E.Conv(fid,alpha, tid, delta@d)
in (1,E.Sum(c,e')) end
| E.Apply(E.Partial d1,e2)=>(1,E.Sum(c,E.Apply(E.Partial(d@d1),e2)))
| E.Add l => (1,E.Add(List.map (fn e => E.Sum(c,E.Apply(E.Partial d, e))) l))
| E.Sub(e1, e2) => (1, E.Sub(E.Sum(c,E.Apply(E.Partial d, e1)), E.Sum(c,E.Apply(E.Partial d, e2))))

| E.Prod [e1]=>(1,E.Sum(c,E.Apply(E.Partial d,e1)))

| E.Prod(E.Tensor(a,[])::e2)=>(1, E.Prod[E.Tensor(a,[]),E.Sum(c,E.Apply(E.Partial d,E.Prod e2))])

| E.Prod es =>(let
fun prod (change,rest, sum,partial,[]) = (change,E.Sum(sum,E.Apply(E.Partial partial,E.Prod rest)))
| prod (change,rest, sum,partial,E.Epsilon(i,j,k)::ps)= let
fun matchprod(2,_,_,_)= 1 (*matched 2*)
| matchprod(num,_,_,[])=0
| matchprod(0,_,_,[eps])=0
| matchprod(num,[],rest,eps::epsx)=
matchprod(num,rest,[],epsx)
| matchprod(num,E.V p::px,rest,eps::epsx)=
if(p=eps) then (matchprod(num+1,rest@px,[],epsx))
else matchprod(num,px,rest@[E.V p], eps::epsx)
| matchprod(num,p::px,rest,eps)=
matchprod(num,px,rest,eps)

val change'= matchprod(0,d,[],[i,j,k])
in (case change'
of 1 => (1,E.Const 0.0)
| _ =>prod(change,rest@[E.Epsilon(i,j,k)],sum,partial,ps)
(*end case*))
end
| prod (change,rest, sum,partial,E.Delta(i,j)::ps)=let
fun applyDelPartial([],_)=(0,[])
| applyDelPartial(p::px,r)=
if(j=p) then (1,r@[i]@px)
else  applyDelPartial(px,r@[p])

val (change',px)=applyDelPartial(d,[])

in (case change'
of 1 => (let val index=rmIndex([j],[],sum)
in prod(1,rest, index,px, ps) end )
| _ => prod(change,rest@[E.Delta(i,j)], sum,partial, ps)
(*end case*)) end

| prod (change,rest,sum, partial,e::es)= prod(change,rest@[e],sum,partial,es)

in  prod(0,[],c, d, es)

end)
| _=>(0,E.Sum(c,E.Apply(E.Partial d,e)))
(* end case*))

(*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 changed = ref false

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 => E.Neg(rewriteBody e)
in if (change=1) then ( changed:=true;body') else body' end
| E.Sub (a,b)=>  E.Sub(rewriteBody a, rewriteBody 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
(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 ))

(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[(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.0))
else body
| E.Prod [E.Partial r1, E.Tensor(_,[])]=> (changed:=true;E.Const(0.0))
| 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))
| E.Prod(E.Sum(c1,E.Prod(E.Epsilon e1::es1))::E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es)=>let
val (change,e,rest)=epsToDels(E.Sum(c1@c2, E.Prod([E.Epsilon e1, E.Epsilon e2]@es1@es2@es)))
in (case (change,e, rest)
of (1,[e1],_)=> (changed:=true;e1)
| _=>let
val e1=rewriteBody(E.Sum(c1,E.Prod(E.Epsilon e1::es1)))
val es'=rewriteBody(E.Prod(E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es))
val (_,e)=(case es' of E.Prod p=>mkProd([e1]@p)
|_=> mkProd([e1]@e)
(*end case*))
in e
end
(*end case*))
end
| E.Prod[e1,e2]=> body
| 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 e)=>let
val s'=rewriteBody(E.Sum  e)
val (c, e')=(case s'
of E.Sum e1=> mkApplySum(E.Apply(E.Partial d,s'))
| _=>(0, E.Apply(E.Partial d, s'))
(*end case*))
in (case c
of 1=>(changed:=true;e')
|_=> e'
(*end case*))
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.Sub(e1,e2))=>(changed:=true; E.Sub(E.Sum(c,e1),E.Sum(c,e2)))
| 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.Epsilon eps1::E.Epsilon eps2::ps))=>
let val (i,e,rest)=epsToDels(body)
in (case (i, e,rest)
of (1,[e1],r) =>(changed:=true;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 E.Sum(c,b) end
|_=>body
(*end case*))
end

| E.Sum(c1,E.Prod(E.Epsilon eps1::E.Sum(c2,E.Prod(E.Epsilon eps2::s2))::ps))=>let
val (i, e, rest)=epsToDels(E.Sum(c1@c2, E.Prod([E.Epsilon eps1, E.Epsilon eps2]@ s2@ps)))
in (case (i,e,rest)
of (1,[e1],_) =>(changed:=true; e1)
|_ => E.Sum(c1,rewriteBody(E.Prod(E.Epsilon eps1::E.Sum(c2,E.Prod(E.Epsilon eps2::s2))::ps)))
(* end case*))
end

| E.Sum(c,E.Prod(E.Delta d::es))=>let
val (change,a)=reduceDelta(body)
in (case (change,a)
of (0, _)=> E.Sum(c,rewriteBody(E.Prod([E.Delta d]@es)))
| (_, E.Prod p)=>let
val (_, p') = mkProd p
in (changed:=true;p') end
| _ => (changed:=true;a )
(*end case*))
end

| E.Sum(c,E.Apply(E.Partial p,e))=>let
val (change,exp)=mkSumApply(body)
val exp'=(case change
of 1=> (changed:=true;exp)
| _ => E.Sum(c,rewriteBody(E.Apply(E.Partial p,e))))
in exp' end

| E.Sum(c,e)=>E.Sum(c,rewriteBody e)

(*Probe*)
| E.Probe(E.Sum(c,s),x)=>(changed:=true;E.Sum(c,E.Probe(s,x)))
| E.Probe(E.Neg e1,x)=>(changed:=true;E.Neg(E.Probe(e1,x)))
| 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([E.Sum s] @es),x)
| E.Probe(E.Prod([E.Neg e] @es),x)
| E.Probe(E.Prod([E.Apply e] @es),x) needs to be rewritten
*)

(*Should be taken care of in next rule.
| E.Probe(E.Prod([E.Sub (e1,e2)] @es),x)=>
| E.Probe(E.Prod([E.Div e] @es),x)=>
*)

| E.Probe(E.Prod p, x)=>let
val (p',x')= (rewriteBody (E.Prod p), rewriteBody x)
fun  probeprod([],rest) =
(print "err-Did not find field/Conv"; body)
| 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;E.Prod(rest@[E.Probe(E.Field f, x')] @es))
| probeprod(E.Conv f::es,rest)=
(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(_,[])=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 = let
val body' = rewriteBody body

in
if !changed
then (changed := false ;loop body')
else body'
end
val z=print "hi"
val u= print(Int.toString( length(params)));
val b = loop body
in
Ein.EIN{params=params, index=index, body=b}
end
end

end (* local *)```