@@ -10,6 +10,11 @@ syntax subst =
1010 }
1111
1212
13+ def $composesubsts(subst*) : subst hint(show (++)%)
14+ def $composesubsts(eps) = {}
15+ def $composesubsts(s_1 s*) = s_1 ++ $composesubsts(s*)
16+
17+
1318;; Domain
1419
1520syntax dom =
@@ -54,6 +59,7 @@ def $subst_typ(s, VAR x a*) = VAR x $subst_arg(s, a)* -- otherwise
5459def $subst_typ(s, optyp) = optyp
5560def $subst_typ(s, TUP (x `: t)*) = TUP (x `: $subst_typ(s, t))* ;; TODO: avoid capture
5661def $subst_typ(s, ITER t it) = ITER $subst_typ(s, t) $subst_iter(s, it)
62+ def $subst_typ(s, MATCH x a* WITH inst*) = MATCH x $subst_arg(s, a)* WITH $subst_inst(s, inst)*
5763
5864def $subst_deftyp(subst, deftyp) : deftyp
5965def $subst_deftyp(s, ALIAS t) = ALIAS $subst_typ(s, t)
@@ -67,7 +73,7 @@ def $subst_iter(subst, iter) : iter
6773def $subst_iter(s, QUEST) = QUEST
6874def $subst_iter(s, STAR) = STAR
6975def $subst_iter(s, PLUS) = PLUS
70- def $subst_iter(s, SUP x? e) = SUP x? $subst_exp(s, e) ;; TODO: avoid capture
76+ def $subst_iter(s, SUP x e) = SUP x $subst_exp(s, e) ;; TODO: avoid capture
7177
7278
7379;; Expressions
@@ -95,9 +101,10 @@ def $subst_exp(s, ACC e_1 p) = ACC $subst_exp(s, e_1) $subst_path(s, p)
95101def $subst_exp(s, UPD e_1 p e_2) = UPD $subst_exp(s, e_1) $subst_path(s, p) $subst_exp(s, e_2)
96102def $subst_exp(s, EXT e_1 p e_2) = EXT $subst_exp(s, e_1) $subst_path(s, p) $subst_exp(s, e_2)
97103def $subst_exp(s, CALL x a*) = CALL $subst_fun(s, x) $subst_arg(s, a)*
98- def $subst_exp(s, ITER e it (x `<- e')*) = ITER $subst_exp(s, e) $subst_iter(s, it) (x `<- $subst_exp(s, e'))* ;; TODO: avoid capture
104+ def $subst_exp(s, ITER e it (x `: t ` <- e')*) = ITER $subst_exp(s, e) $subst_iter(s, it) (x `: $subst_typ(s, t) `<- $subst_exp(s, e'))* ;; TODO: avoid capture
99105def $subst_exp(s, CVT e nt_1 nt_2) = CVT $subst_exp(s, e) nt_1 nt_2
100106def $subst_exp(s, SUB e t_1 t_2) = SUB $subst_exp(s, e) $subst_typ(s, t_1) $subst_typ(s, t_2)
107+ def $subst_exp(s, MATCH a* WITH clause*) = MATCH $subst_arg(s, a)* WITH $subst_clause(s, clause)*
101108
102109def $subst_path(subst, path) : path
103110def $subst_path(s, ROOT) = ROOT
@@ -120,39 +127,66 @@ def $subst_sym(s, SEQ g*) = SEQ $subst_sym(s, g)*
120127def $subst_sym(s, ALT g*) = ALT $subst_sym(s, g)*
121128def $subst_sym(s, RANGE g_1 g_2) = RANGE $subst_sym(s, g_1) $subst_sym(s, g_2)
122129def $subst_sym(s, ATTR e g) = ATTR $subst_exp(s, e) $subst_sym(s, g)
123- def $subst_sym(s, ITER g it (x `<- e)*) = ITER $subst_sym(s, g) $subst_iter(s, it) (x `<- $subst_exp(s, e))* ;; TODO: avoid capture
130+ def $subst_sym(s, ITER g it (x `: t ` <- e)*) = ITER $subst_sym(s, g) $subst_iter(s, it) (x `: $subst_typ(s, t) `<- $subst_exp(s, e))* ;; TODO: avoid capture
124131
125132
126133;; Definitions
127134
128135def $subst_arg(subst, arg) : arg
129- def $subst_arg(s, EXP e) = EXP $subst_exp(s, e)
130136def $subst_arg(s, TYP t) = TYP $subst_typ(s, t)
137+ def $subst_arg(s, EXP e) = EXP $subst_exp(s, e)
131138def $subst_arg(s, FUN x) = FUN $subst_fun(s, x)
132139def $subst_arg(s, GRAM g) = GRAM $subst_sym(s, g)
133140
134141def $subst_param(subst, param) : param
135- def $subst_param(s, EXP x `: t) = EXP x `: $subst_typ(s, t)
136142def $subst_param(s, TYP x) = TYP x
143+ def $subst_param(s, EXP x `: t) = EXP x `: $subst_typ(s, t)
137144def $subst_param(s, FUN x `: p* `-> t) = FUN x `: $subst_param(s, p)* `-> $subst_typ(s, t) ;; TODO: avoid capture
138145def $subst_param(s, GRAM x `: p* `-> t) = GRAM x `: $subst_param(s, p)* `-> $subst_typ(s, t) ;; TODO: avoid capture
139146
140147def $subst_quant(subst, quant) : quant
141148def $subst_quant(s, q) = $subst_param(s, q)
142149
143150def $subst_prem(subst, prem) : prem
144- def $subst_prem(s, RULE x a* `: e) = RULE x $subst_arg(s, a)* `: $subst_exp(s, e)
151+ def $subst_prem(s, REL x a* `: e) = REL x $subst_arg(s, a)* `: $subst_exp(s, e)
145152def $subst_prem(s, IF e) = IF $subst_exp(s, e)
146153def $subst_prem(s, ELSE) = ELSE
147154def $subst_prem(s, LET e_1 `= e_2) = LET $subst_exp(s, e_1) `= $subst_exp(s, e_2) ;; TODO: avoid capture
148- def $subst_prem(s, ITER pr it (x `<- e)*) = ITER $subst_prem(s, pr) $subst_iter(s, it) (x `<- $subst_exp(s, e))* ;; TODO: avoid capture
155+ def $subst_prem(s, ITER pr it (x `: t `<- e)*) = ITER $subst_prem(s, pr) $subst_iter(s, it) (x `: $subst_typ(s, t) `<- $subst_exp(s, e))* ;; TODO: avoid capture
156+
157+
158+ def $subst_inst(subst, inst) : inst
159+ def $subst_inst(s, INST `{q*} a* `= dt) = INST `{$subst_param(s, q)*} $subst_arg(s, a)* `= $subst_deftyp(s, dt) ;; TODO: avoid capture
160+
161+ def $subst_rule(subst, rul) : rul
162+ def $subst_rule(s, RULE `{q*} e `- pr*) = RULE `{$subst_param(s, q)*} $subst_exp(s, e) `- $subst_prem(s, pr)* ;; TODO: avoid capture
163+
164+ def $subst_clause(subst, clause) : clause
165+ def $subst_clause(s, CLAUSE `{q*} a* `= e `- pr*) = CLAUSE `{$subst_param(s, q)*} $subst_arg(s, a)* `= $subst_exp(s, e) `- $subst_prem(s, pr)* ;; TODO: avoid capture
166+
167+ def $subst_prod(subst, prod) : prod
168+ def $subst_prod(s, PROD `{q*} g `=> e `- pr*) = PROD `{$subst_param(s, q)*} $subst_sym(s, g) `=> $subst_exp(s, e) `- $subst_prem(s, pr)* ;; TODO: avoid capture
169+
170+
171+ ;; Constructing substitutions for parameters
172+
173+ def $arg_for_param(arg, param) : subst
174+ def $arg_for_param(TYP t, TYP x) = {TYP (x, t)}
175+ def $arg_for_param(EXP e, EXP x `: t) = {EXP (x, e)}
176+ def $arg_for_param(FUN y, FUN x `: p* `-> t) = {FUN (x, y)}
177+ def $arg_for_param(GRAM g, GRAM x `: p* `-> t) = {GRAM (x, g)}
178+
179+ def $args_for_params(arg*, param*) : subst
180+ def $args_for_params(eps, eps) = {}
181+ def $args_for_params(a_1 a*, p_1 p*) = s ++ $args_for_params(a*, $subst_param(s, p)*)
182+ -- if s = $arg_for_param(a_1, p_1)
149183
150184
151185;; Well-formedness
152186
153187def $paramarg(param) : arg
154- def $paramarg(EXP x `: t) = EXP (VAR x)
155188def $paramarg(TYP x) = TYP (VAR x)
189+ def $paramarg(EXP x `: t) = EXP (VAR x)
156190def $paramarg(FUN x `: p* `-> t) = FUN x
157191def $paramarg(GRAM x `: p* `-> t) = GRAM (VAR x)
158192
0 commit comments