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

SCM Repository

[diderot] View of /branches/ein16/src/compiler/ein/cleanIndex.sml
ViewVC logotype

View of /branches/ein16/src/compiler/ein/cleanIndex.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3687 - (download) (annotate)
Sun Feb 28 03:46:43 2016 UTC (4 years, 5 months ago) by cchiw
File size: 4139 byte(s)
added flags
(*
cleanIndex.sml cleans the indices in the EIN expression. This process is a bit more complicated because the vast number of possibilities. We need to both know all indices used in the subexpression in order to create a mapp, and the shape of the subexpression. 

For each subexpression we look for the\\
ashape(mu list): all the indices mentioned in body
tshape(mu list):shape of tensor replacement
size(int list): TensorType of tensor replacement\\
 
                 
*)
structure cleanIndex= struct

    local
   
    structure E = Ein
    structure P = Printer
    structure S = GetShape
    structure R = RewriteIndices

    in
    val testing=0
 

    fun getShapes e= S.getShapes e
    fun rewriteIx e= R.rewrite e
 
    fun iTos i =Int.toString i
    fun insert (key, value) d =(fn s =>
        if s = key then SOME value
        else d s)
    fun lookup k d = d k
    val empty =fn key =>NONE
    fun err str=raise Fail str
    fun testp n=(case testing
        of 0=> 1
        | _ =>(print(String.concat n);1)
        (*end case*))
    (*dictionary to lookup mapp*)
    fun lkupIX(e1,mapp,str)=(case (lookup e1 mapp)
        of SOME l=>l
        | _=> raise Fail(str^iTos(e1))
        (*endcase*))

    (*sizeMapp:->int
    * create sizeMapp for bound on mu
    *)
    fun mkSizeMapp(index,sx)=let
        fun m([],mapp)=mapp
          | m((E.V v, _,ub)::es,mapp)= m(es,insert(v,ub+1) mapp)
          | m( _,_)=err"Non-V-index in sx"
        fun f(_,[],mapp)= mapp
          |f(counter,ix::es,mapp)=f(counter+1,es,insert(counter,ix) mapp)
        val mapp=f(0,index,empty)
        in
            m(sx,mapp)
        end

     (* mkIndexMapp:int list, sum_id list*mu list*mu list=>dict*int list *mu list
     * A map is created to match index-ids in e1 to their new ids.
     * First we iterate over indices in $tshape\beta$ and set a counter to 0.
     * i.e. [$\beta_0 \longrightarrow 0,\beta_1 \longrightarrow 1,..,\beta_n \longrightarrow n $]$\in $indexMapp
     Then from i=0 to i=maxium possible index-id.
     if (i $\not \in $indexMapp and i $\in \gamma$) then $i \longrightarrow counter \in indexMapp$
     *)
    fun mkIndexMapp(index,sx,ashape,tshape)= let
        fun f(mapp,[],tocounter)=(mapp,tocounter)
          | f(mapp,(E.V e1)::es,tocounter)=let
            val dict=insert(e1, tocounter) mapp
            (*val _ =testp["\nInserting into dictionary",iTos e1,"=>",iTos tocounter]*)
            in
                f(dict,es,tocounter+1)
            end
        fun m(mapp,[],tocounter)= mapp
          | m(mapp,e1::es, tocounter)=(case (lookup e1 mapp)
                of SOME _=>m(mapp,es,tocounter)
                | _ =>(case (List.find (fn x =>  x = E.V e1) ashape)
                        of NONE=>m(mapp,es,tocounter)
                        | SOME _ =>let
                            val dict=insert(e1, tocounter) mapp
                           (* val _ =testp["\nInserting into dictionary",iTos e1,"=>",iTos tocounter]*)
                            in
                                m(dict,es,tocounter+1)
                            end
                        (*end case*))
                (*end case*))
         
        val (mapp,tocounter)=f(empty,tshape,0)
         
        val pp=List.map (fn E.V v=>v | _ => 0) ashape
        val max= List.foldl(fn(a,b)=> Int.max(a,b)) (length index-1) pp
        (*finds max element in ashape and creates [0,1,2,....,max]*)
        val maxmu=List.tabulate(max+1,(fn e=> e))
        val indexMapp=m(mapp,maxmu,tocounter)
    in
        indexMapp
    end

    (* cleanIndex:ein_exp*int list *sum_id ->mu list *int list*ein_exp
    * cleans index in body
    * returns shape of replacement in index variable list, size, and rewritten body
    *)
    fun cleanIndex(e,index,sx)=let
        val (ashape,tshape)=getShapes(e,index,sx)
        val sizeMapp= mkSizeMapp(index,sx)
        val sizes= List.map(fn E.V e1=> lkupIX(e1,sizeMapp,"Could not find Size of")) tshape
        val indexMapp=mkIndexMapp(index,sx, ashape,tshape)
        val body=rewriteIx(indexMapp,e)
        in
            (tshape,sizes,body)
        end

  end; (* local *)

end (* local *)

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