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

SCM Repository

[diderot] View of /trunk/src/compiler/fields/shape.sml
ViewVC logotype

View of /trunk/src/compiler/fields/shape.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 435 - (download) (annotate)
Tue Oct 19 13:14:20 2010 UTC (8 years, 9 months ago) by jhr
File size: 1810 byte(s)
  Upated URL in header to diderot-language.cs.uchicago.edu
(* shape.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * A tree representation of the shape of a tensor (or loop nest).  The height
 * of the tree corresponds to the order of the tensor (or nesting depth) plus
 * one.  I.e., a 0-order tensor is represented by a leaf, a 1-order tensor
 * will be ND(_, [LF _, ..., LF _]), etc.
 *)

structure Shape : sig

    datatype ('nd, 'lf) shape
      = LF of 'lf
      | ND of ('nd * ('nd, 'lf) shape list)

  (* create (depth, wid, labelNd, f, labelLf, root)
   *)
    val create : int * int * ('a -> 'nd) * (int * 'a -> 'a) * ('a -> 'lf) * 'a -> ('nd, 'lf) shape

    val map : ('a -> 'b) * ('c -> 'd) -> ('a,'c) shape -> ('b,'d) shape

  (* right-to-left traversal of the tree *)
    val foldr : ('a * 'b -> 'b) -> 'b -> ('c,'a) shape -> 'b

    val appPreOrder : ('nd -> unit) * ('lf -> unit) -> ('nd, 'lf) shape -> unit

  end = struct

    datatype ('nd, 'lf) shape
      = LF of 'lf
      | ND of ('nd * ('nd, 'lf) shape list)

  (* creates a shape with the given depth and width at each level *)
    fun create (depth, width, ndAttr, f, lfAttr, init) = let
	  fun mk (d, i, arg) = if (d < depth)
		then ND(ndAttr arg, List.tabulate(width, fn j => mk(d+1, j, f(j, arg))))
		else LF(lfAttr arg)
	  in
	    mk (0, 0, init)
	  end

    fun map (nd, lf) t = let
	  fun mapf (LF x) = LF(lf x)
	    | mapf (ND(i, kids)) = ND(nd i, List.map mapf kids)
	  in
	    mapf t
	  end

    fun foldr f init t = let
	  fun fold (LF x, acc) = f(x, acc)
	    | fold (ND(_, kids), acc) = List.foldr fold acc kids
	  in
	    fold (t, init)
	  end

    fun appPreOrder (ndFn, lfFn) = let
	  fun app (ND(attr, kids)) = (ndFn attr; List.app app kids)
	    | app (LF attr) = lfFn attr
	  in
	    app
	  end

  end

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