Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

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

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

Thu Dec 24 15:45:26 2015 UTC (5 years, 5 months ago) by jhr
File size: 3866 byte(s)
`debugging merge: ein normalization`
```(* 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
*)

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) = i2s ix

fun multiIndex2s [] = ""
| multiIndex2s [E.V ix] = "_" ^ i2s ix
| multiIndex2s alpha = concat ["_{", String.concatWithMap "," index2s alpha, "}"]

fun delta (a, b) = concat["δ_{", index2s a, ",", index2s b,"}"]

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["ϵ_{", i2s ix, i2s jx, i2s kx, "}"]
| E.Eps2(ix, jx) => concat["ϵ_{", i2s ix, 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 => concat["Value(", i2s ix, ")"]
| E.Img(fid, alpha, pos) => concat [
"V", i2s fid, multiIndex2s alpha, "[",
String.concatWithMap "," expToString pos, "]"
]
| E.Krn(tid, [], pos) => concat["H ", i2s tid, "[", expToString pos, "]"]
| E.Krn(tid, betas, pos) => concat[
"H", i2s tid, "^{", String.concatWithMap "" delta betas, "}[",
expToString pos, "]"
]
| E.Sum(_, e) => concat["Σ(", expToString e, ")"]
| E.Op1(E.PowInt n, e) => concat["(", expToString e , ")^", i2s n]
| E.Op1(E.PowReal r, e) => concat["(", expToString e , ")^", Rational.toString r]
| E.Op1(E.PowEmb(sx, n), e) => concat["Pow(", 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
```