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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/ein/ein-pp.sml
ViewVC logotype

View of /branches/vis15/src/compiler/ein/ein-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4191 - (download) (annotate)
Tue Jul 12 22:09:36 2016 UTC (3 years, 3 months ago) by cchiw
File size: 4221 byte(s)
added bug notes
(* ein-pp.sml
 *
 * NOTE: this code probably should be merged into the EinUtil module.
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *)

structure EinPP : sig

    val toString : Ein.ein -> string

    val expToString : Ein.ein_exp -> string

  end = struct

    structure E = Ein

    val i2s = Int.toString
    val shp2s = String.concatWithMap " " i2s

    fun index2s (E.C cx) = concat["'", i2s cx, "'"]
      | index2s (E.V ix) = "i" ^ i2s ix

    fun multiIndex2s [] = ""
      | multiIndex2s alpha = concat ["_{", String.concatWithMap "," index2s alpha, "}"]

    fun delta (a, b) = concat["δ_{", i2s a, ",", i2s b,"}"]
    fun deltaKrn (a, b) = concat["δ_{", index2s a, ",", index2s b,"}"]
    fun border E.Default = "Default"
      | border E.Clamp = "Clamp"
      | border E.Mirror =" Mirror"
      | border E.Wrap = "Wrap"
      | border E.None = raise Fail "unexpected"

    fun expToString e = (case e
	   of E.Const r => i2s r
	    | E.ConstR r => Rational.toString r
	    | E.Tensor(id, []) => "T" ^ i2s id
	    | E.Tensor(id, alpha) => concat["T", i2s id, multiIndex2s alpha]
	    | E.Delta ix => delta ix
	    | E.Epsilon(ix, jx, kx) => concat["ϵ_{i", i2s ix, ",i", i2s jx, ",i", i2s kx, "}"]
	    | E.Eps2(ix, jx) => concat["ϵ_{i", i2s ix, ",i", i2s jx, "}"]
	    | E.Field(id, []) => "F" ^ i2s id
	    | E.Field(id, alpha) => concat["F", i2s id, multiIndex2s alpha]
	    | E.Lift e1 => concat["«", expToString e1, "»"]
	    | E.Conv(img, alpha, kern, beta) => let
		val alpha = if null alpha then "" else multiIndex2s alpha
		val beta = if null beta then "" else "dx" ^ multiIndex2s beta
		in
		  concat ["V", i2s img, alpha, "⊛", beta, "H", i2s kern]
		end
	    | E.Partial alpha => "∂/∂x" ^ multiIndex2s alpha
	    | E.Apply(e1, e2) => concat [ expToString e1, "@(", expToString e2, ")"]
	    | E.Probe(e1, e2) => concat ["Probe(", expToString e1, ",", expToString e2, ")"]
	    | E.Value ix => "i" ^ i2s ix
	    | E.Img(fid, alpha, pos, s, E.None) => concat [
		  "V", i2s fid, multiIndex2s alpha, "(", i2s s, ")[",
		  String.concatWithMap "," expToString pos, "]"
		]
	    | E.Img(fid, alpha, pos, s, b) => concat [
		  "V", i2s fid, multiIndex2s alpha, "(", i2s s, ")[",
		  String.concatWithMap "," expToString pos, "]#", border b
		]
	    | E.Krn(tid, [], dim) => concat["H", i2s tid, "(", Int.toString dim, ")"]
	    | E.Krn(tid, betas, dim) => concat[
		  "H", i2s tid, "^{", String.concatWithMap "" deltaKrn betas, "}(", Int.toString dim, ")"
		]
	     | E.Sum(sx, e) => let
		val sx = List.map
		      (fn (v, lb, ub) => concat ["(i", i2s v, "=", i2s lb, "..", i2s ub, ")"])
			sx
		in
 concat ("Σ⋐" :: sx @ ["(", expToString e, ")⋑"]@sx)
		end
	    | E.Op1(E.PowInt n, e) => concat["(", expToString e , ")^", i2s n]
	    | E.Op1(f, e) => let
		val f = (case f
		       of E.Neg => "Neg"
			| E.Exp => "Exp"
			| E.Sqrt => "Sqrt"
			| E.Cosine => "Cosine"
			| E.ArcCosine => "ArcCosine"
			| E.Sine => "Sine"
			| E.ArcSine => "ArcSine"
			| E.Tangent => "Tangent"
			| E.ArcTangent => "ArcTangent"
			| _ => raise Fail "impossible"
		      (* end case *))
		in
		  concat[f, "(", expToString e, ")"]
		end
	   | E.Op2(E.Sub, e1, e2) => concat ["(", expToString e1, ") - (", expToString e2, ")"]
	   | E.Op2(E.Div, e1, e2) => concat ["(", expToString e1, ") / ( ", expToString e2, ")"]
	   | E.Opn(E.Add, el) => concat["(", String.concatWithMap " + " expToString el,")"]
	   | E.Opn(E.Prod, el) => concat["(", String.concatWithMap " * " expToString el, ")"]
	  (* end case *))

    fun toString (Ein.EIN{params, index, body}) = let
	  fun paramToString (i, E.TEN(t, shp)) = concat["T", i2s i, "[", shp2s shp, "]"]
	    | paramToString (i, E.FLD d) = concat["F", i2s i, "[", i2s d, "]"]
	    | paramToString (i, E.KRN) = "H" ^ i2s i
	    | paramToString (i, E.IMG(d, shp)) = concat["V", i2s i, "(", i2s d, ")[", shp2s shp, "]"]
	  val params = String.concatWith "," (List.mapi paramToString params)
	  val index = if null index then "" else concat["_{", shp2s index, "}"]
	  in
	    concat["λ(", params, ")<", expToString body, ">", index]
	  end

  end

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