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 342 - (download) (annotate)
Wed Sep 15 21:52:14 2010 UTC (11 years, 9 months ago) by jhr
File size: 1081 byte(s)
  New files
(* shape.sml
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
 * All rights reserved.
 * A tree representation of the shape of a tensor.  The height of the tree
 * corresponds to the order of the tensor 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 =

    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)
	    mk (0, 0, init)

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

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


ViewVC Help
Powered by ViewVC 1.0.0