Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/RewriteGen/poly-gen.sml

raw
1 functor PolyGen
2 (structure AstPP : MDL_AST_PRETTY_PRINTER
3 structure AstTrans : MDL_AST_TRANSLATION
4 sharing AstPP.Ast = AstTrans.Ast
5 ) : POLY_GEN =
6 struct
7 structure Ast = AstPP.Ast
8 structure A = Ast
9 structure T = AstTrans
10 structure H = HashTable
11
12 fun bug msg = MLRiscErrorMsg.error("PolyGen",msg)
13 exception PolyGen
14 fun error msg = (MDLError.error msg; raise PolyGen)
15
16 datatype hook =
17 HOOK of
18 { name : string, (* name of function *)
19 factor : bool, (* factor rules by constructor? *)
20 args : string list, (* function arguments *)
21 ret : string, (* return argument name *)
22 unit : Ast.exp -> Ast.exp,
23 gen : (Ast.ty * Ast.exp -> Ast.exp) * Ast.consbind -> Ast.exp
24 }
25
26 fun ID id = A.IDexp(A.IDENT([],id))
27 val argExp = ID "arg"
28 val argPat = A.IDpat "arg"
29
30 fun gen(HOOK{name, args, ret, unit, factor, gen, ...}) isNonTerm ruleSet =
31 let val redex = ID(hd args) (* the redex must be the first argument *)
32
33 (*
34 * Given a type, returns the appropriate function that performs the
35 * transformation
36 *)
37 val nullTrans = A.LAMBDAexp[A.CLAUSE([argPat],NONE,argExp)]
38 fun ty2Exp(A.IDty(A.IDENT(_,id))) =
39 if isNonTerm id then SOME(ID(name^"'"^id)) else NONE
40 | ty2Exp(A.APPty(A.IDENT(_,id),args)) =
41 if isNonTerm id then
42 let val args = map ty2Exp args
43 in if List.exists Option.isSome args then
44 SOME(A.APPexp(ID(name^"'"^id),
45 A.TUPLEexp(map (fn SOME f => f | NONE => nullTrans)
46 args)))
47 else NONE
48 end
49 else NONE
50 | ty2Exp(A.TUPLEty tys) = (* create a functional *)
51 let val args = map ty2Exp tys
52 fun bind([], i, pats, exps, some) = (rev pats, rev exps, some)
53 | bind(arg::args, i, pats, exps, some) =
54 let val v = "v_"^Int.toString i
55 val pat = A.IDpat v
56 val exp = ID v
57 val (exp, some) =
58 case arg of NONE => (exp, some)
59 | SOME f => (A.APPexp(f,exp), true)
60 in bind(args, i+1, pat::pats, exp::exps, some) end
61 val (pats, exps, some) = bind(args, 0, [], [], false)
62 in if some then
63 SOME(A.LAMBDAexp[A.CLAUSE([A.TUPLEpat pats],NONE,
64 A.TUPLEexp exps)])
65 else NONE
66 end
67 | ty2Exp(A.RECORDty ltys) =
68 let val args = map (fn (l,t) => (l, ty2Exp t)) ltys
69 fun bind([], i, pats, exps, some) = (rev pats, rev exps, some)
70 | bind((l,arg)::args, i, pats, exps, some) =
71 let val pat = (l, A.IDpat l)
72 val exp = ID l
73 val (exp, some) =
74 case arg of NONE => (exp, some)
75 | SOME f => (A.APPexp(f,exp), true)
76 in bind(args, i+1, pat::pats, (l,exp)::exps, some)
77 end
78 val (pats, exps, some) = bind(args, 0, [], [], false)
79 in if some then
80 SOME(A.LAMBDAexp[A.CLAUSE([A.RECORDpat(pats,false)],NONE,
81 A.RECORDexp exps)])
82 else NONE
83 end
84 | ty2Exp(A.TYVARty(A.VARtv id)) = SOME(ID("param"^id))
85 | ty2Exp t = error("Can't handle type "^PP.text(AstPP.ty t))
86
87 fun genOneRule(A.DATATYPEbind{id,tyvars,cbs, ...}, rules) =
88 let val prefix = []
89 val subTerm = ref false
90 fun appTrans(ty,e) =
91 case ty2Exp ty of
92 NONE => unit e
93 | SOME f => (subTerm := true; A.APPexp(f,e))
94
95 (* arguments for this function *)
96 (* How to generate the traversal for one constructor *)
97 fun genNonFactoredTraversal(cons) =
98 let val _ = subTerm := false
99 val exp = gen(appTrans,cons)
100 val exp = if !subTerm then exp else unit redex
101 fun mapPat{origName,newName,ty} = A.IDpat newName
102 in T.mapConsToClause{prefix=prefix,pat=fn p => p,exp=exp} cons
103 end
104
105 exception Can'tFactor
106
107 (* How to generate the traversal for one constructor *)
108 fun genFactoredTraversal(cons as A.CONSbind{id, ty, ...},rules) =
109 let val _ = subTerm := false
110 val resultExp as A.CONSexp(_,caseExp) = gen(appTrans,cons)
111 val caseExp = case caseExp of SOME e => e
112 | NONE => A.TUPLEexp []
113 val body =
114 case (rules, !subTerm) of
115 ([], false) => redex
116 | ([], true) => resultExp
117 | (_, _) =>
118 A.CASEexp
119 (caseExp,
120 rules @
121 [A.CLAUSE([argPat],NONE,
122 case ty of
123 SOME _ => A.CONSexp(A.IDENT([],id), SOME argExp)
124 | NONE => redex
125 )
126 ]
127 )
128 fun mapPat{origName,newName,ty} = A.IDpat newName
129 in T.mapConsToClause{prefix=prefix,pat=fn p => p, exp=body} cons
130 end
131
132 (* first factor all rules by their top level constructors *)
133 fun factorRules(rules) =
134 let exception Bad
135 val tbl = H.mkTable(HashString.hashString,op=)(32,Can'tFactor)
136 val _ = app (fn A.CONSbind{id,...} => H.insert tbl (id,[]))
137 cbs
138 fun factor(r,A.CONSpat(A.IDENT([],id),arg),g,e) =
139 enterRule(r, id, arg, g, e)
140 | factor(r,A.IDpat id, g, e) =
141 enterRule(r, id, NONE, g, e)
142 | factor(r,A.ASpat(_,p), g, e) = factor(r, p, g, e)
143 | factor _ = raise Can'tFactor
144 and factorRule(r as A.CLAUSE([p],g,e)) = factor(r,p,g,e)
145 | factorRule _ = raise Can'tFactor
146 and enterRule(r, consName, arg, g, e) =
147 let val rs = H.lookup tbl consName
148 val r = A.CLAUSE([case arg of NONE => A.WILDpat
149 | SOME p => p],g,e)
150 in H.insert tbl (consName,r::rs)
151 end
152 val _ = app factorRule rules
153
154 in map (fn c as A.CONSbind{id,...} => (c,rev(H.lookup tbl id))) cbs
155 end
156
157 fun factoredBody rules =
158 A.CASEexp(redex,map genFactoredTraversal (factorRules rules))
159 fun nonfactoredBody rules =
160 A.LETexp([A.VALdecl[A.VALbind(A.IDpat ret,
161 A.CASEexp(redex,map genNonFactoredTraversal cbs))]],
162 [A.CASEexp(A.TUPLEexp(map ID args), rules)]
163 )
164
165 val body =
166 if factor then (factoredBody rules handle Can'tFactor =>
167 nonfactoredBody rules)
168 else nonfactoredBody rules
169 fun curriedArg(A.VARtv id) = A.IDpat("param"^id)
170 | curriedArg _ = bug "curriedArg"
171 val args = [A.TUPLEpat(map A.IDpat args)]
172 val args = case tyvars of
173 [] => args
174 | vs => A.TUPLEpat(map curriedArg vs)::args
175 in A.FUNbind(name^"'"^id,[A.CLAUSE(args,NONE,body)])
176 end
177 | genOneRule _ = bug "genOneRule"
178 in A.FUNdecl(map genOneRule ruleSet)
179 end
180 end