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

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/compiler/fields/shape.sml
ViewVC logotype

Annotation of /branches/pure-cfg/src/compiler/fields/shape.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 342 (* shape.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 342 * All rights reserved.
7 :     *
8 : jhr 349 * A tree representation of the shape of a tensor (or loop nest). The height
9 :     * of the tree corresponds to the order of the tensor (or nesting depth) plus
10 :     * one. I.e., a 0-order tensor is represented by a leaf, a 1-order tensor
11 :     * will be ND(_, [LF _, ..., LF _]), etc.
12 : jhr 586 *
13 :     *
14 : jhr 342 *)
15 :    
16 : jhr 349 structure Shape : sig
17 : jhr 342
18 :     datatype ('nd, 'lf) shape
19 :     = LF of 'lf
20 :     | ND of ('nd * ('nd, 'lf) shape list)
21 :    
22 : jhr 586 (* create (h, w, labelNd, f, labelLf, root)
23 :     * creates a tree of height h (>= 1), with each interior node having
24 :     * w (>= 1) children.
25 : jhr 349 *)
26 :     val create : int * int * ('a -> 'nd) * (int * 'a -> 'a) * ('a -> 'lf) * 'a -> ('nd, 'lf) shape
27 :    
28 : jhr 586 (* map functions over the nodes and leaves of a shape *)
29 : jhr 349 val map : ('a -> 'b) * ('c -> 'd) -> ('a,'c) shape -> ('b,'d) shape
30 :    
31 : jhr 353 (* right-to-left traversal of the tree *)
32 : jhr 349 val foldr : ('a * 'b -> 'b) -> 'b -> ('c,'a) shape -> 'b
33 :    
34 : jhr 353 val appPreOrder : ('nd -> unit) * ('lf -> unit) -> ('nd, 'lf) shape -> unit
35 :    
36 : jhr 349 end = struct
37 :    
38 :     datatype ('nd, 'lf) shape
39 :     = LF of 'lf
40 :     | ND of ('nd * ('nd, 'lf) shape list)
41 :    
42 : jhr 586 (* creates a shape with the given height and width at each level *)
43 :     fun create (height, width, ndAttr, f, lfAttr, init) = let
44 :     fun mk (0, arg) = LF(lfAttr arg)
45 :     | mk (d, arg) = ND(ndAttr arg, List.tabulate(width, fn j => mk(d-1, f(j, arg))))
46 : jhr 342 in
47 : jhr 586 if (height < 0) orelse (width < 1)
48 :     then raise Size
49 :     else mk (height, init)
50 : jhr 342 end
51 :    
52 :     fun map (nd, lf) t = let
53 :     fun mapf (LF x) = LF(lf x)
54 :     | mapf (ND(i, kids)) = ND(nd i, List.map mapf kids)
55 :     in
56 :     mapf t
57 :     end
58 :    
59 :     fun foldr f init t = let
60 :     fun fold (LF x, acc) = f(x, acc)
61 :     | fold (ND(_, kids), acc) = List.foldr fold acc kids
62 :     in
63 : jhr 349 fold (t, init)
64 : jhr 342 end
65 :    
66 : jhr 353 fun appPreOrder (ndFn, lfFn) = let
67 :     fun app (ND(attr, kids)) = (ndFn attr; List.app app kids)
68 :     | app (LF attr) = lfFn attr
69 :     in
70 :     app
71 :     end
72 :    
73 : jhr 342 end

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