Guest User

Untitled

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. structure Syntax =
  2. struct
  3. open Set
  4.  
  5. type control = {name : string, active : bool, index : int option}
  6.  
  7. val fresh = ref 0
  8. fun freshIndex {name=name, active=active, index=_} =
  9. {name=name, active=active, index=SOME (fresh := !fresh + 1; !fresh)}
  10.  
  11. fun indexOf (k : control) =
  12. case #index k of
  13. NONE => []
  14. | SOME i => [i]
  15.  
  16. (* Definition 1, 4 and 5 *)
  17. (* Definition 5 is covered because controls can optionally be
  18. indexed. *)
  19. datatype graph = Par of graph * graph (* P | P *)
  20. | Prefix of control * graph (* k.P *)
  21. | Hole of int (* [ ]_int *)
  22. | Index of graph * int (* <graph>_int *)
  23. | Nil (* nil *)
  24.  
  25. (* Definition 2 *)
  26. type inst_map = (int * int) set
  27. type reaction_rule = graph * graph * inst_map
  28.  
  29. (* The identity instantiation map *)
  30. fun inst_id [] = [] : inst_map | inst_id (h::t) = (h,h) :: inst_id t
  31.  
  32. (* Definitions of notational helpers *)
  33. infix 6 ||
  34. infix 5 |-
  35. infix 4 |->
  36. infixr 7 $
  37. fun a || b = (a,b)
  38. fun a |- b = (a,b)
  39. fun (v,i) |-> e = Vector.update(v,i,e)
  40.  
  41. (* Retrieve the set J of hole indices from a given process term *)
  42. fun holes (Hole j) = [j]
  43. | holes Stop = []
  44. | holes (Prefix(_,p)) = holes p
  45. | holes (Par(a,b)) = holes a @ holes b
  46. | holes (Index (p,_)) = holes p
  47.  
  48. (* Pretty printer for place graphs *)
  49.  
  50. fun pp_control {name=name, active=active, index=SOME i} =
  51. name ^ "_" ^ Int.toString i
  52. | pp_control {name=name, ...} = name
  53.  
  54. fun pp_pg (Par(a,b)) = pp_pg a ^ " | " ^ pp_pg b
  55. | pp_pg (Prefix(c,p)) = pp_control c ^ "." ^ pp_pg p
  56. | pp_pg (Hole i) = "[" ^ Int.toString i ^ "]"
  57. | pp_pg (Index (p,i)) = "<" ^ pp_pg p ^ ">_" ^ Int.toString i
  58. | pp_pg Stop = "nil"
  59.  
  60. (* lhs_match_stack * graph * context_vec * inst_map * int list *)
  61.  
  62. fun pp_ctx_vec v = "[" ^
  63. (Vector.foldli
  64. (fn (i,a,b) => b ^ ", " ^ (Int.toString i) ^ ": " ^ pp_pg a)
  65. ""
  66. v) ^ "]"
  67.  
  68. fun pp_lhsms [] = "&bull;"
  69. | pp_lhsms (h::t) = pp_pg h ^ " &#x22B3; " ^ pp_lhsms t
  70.  
  71. fun pp_pm (P_L,L,P_R,D,Xi,I) =
  72. "(" ^ pp_pg P_L ^ ", "^ pp_lhsms L ^", " ^ pp_pg P_R ^ ", " ^ pp_ctx_vec D ^ ", &xi;, {"^
  73. (String.concatWith "," (map Int.toString I)) ^ "})"
  74.  
  75. fun pp_pm_list l = String.concatWith "," (map pp_pm l)
  76.  
  77. (* Pretty printer for local states *)
  78. fun pp_ls (PM,(p,q)) =
  79. (pp_pm_list PM) ^ " &#x22A2; " ^ pp_pg p ^ " || " ^
  80. pp_pg q
  81. (* (indexed_control * local_state) list *)
  82.  
  83. (* Pretty printer for indexed control stacks *)
  84. fun pp_ic [] = "&bull;"
  85. | pp_ic ((k,ls)::t) = pp_control k ^ " &#x22b3; " ^ pp_ls ls ^ " &#x22B3; " ^ pp_ic t
  86.  
  87. (* Pretty printer for global states *)
  88. fun pp (ls,ic) = "(" ^ pp_ls ls ^ ", " ^
  89. pp_ic ic ^ ") "
  90. end
  91.  
  92. signature SEMANTICS =
  93. sig
  94. type control
  95.  
  96. datatype graph = datatype Syntax.graph
  97.  
  98. val reactionRules : (graph * graph * (int * int) Set.set) Set.set
  99. end
  100.  
  101. signature PAMSIG =
  102. sig
  103. eqtype pam_state
  104. datatype graph = datatype Syntax.graph
  105.  
  106. val PAR : pam_state -> pam_state
  107. val UNIT_strip : pam_state -> pam_state
  108. val UNIT_add : pam_state -> pam_state
  109. val MATCH : pam_state -> pam_state
  110. val POP : pam_state -> pam_state
  111. val REACT : pam_state -> pam_state
  112. val TOP : pam_state -> pam_state
  113.  
  114. val initial_state : graph -> pam_state
  115. val pp : pam_state -> string
  116. end
  117.  
  118. functor PAM (Semantics : SEMANTICS) : PAMSIG =
  119. struct
  120. open Set
  121. open Syntax
  122.  
  123. infix 4 |->
  124.  
  125. exception RuleNotApplicable of string
  126.  
  127. (* Definition 6 (Partial match) *)
  128. type context_vec = graph vector
  129. type lhs_match_stack = graph list
  130. type partial_match =
  131. graph * lhs_match_stack * graph * context_vec * inst_map * int set
  132.  
  133. (* Definition 7 (Initial partial matches) *)
  134. fun make_vec hs = Vector.fromList (map (fn i => Hole i) hs)
  135.  
  136. fun init [] = [] : partial_match list
  137. | init ((P_L,P_R,xi)::t) = (P_L,
  138. [],
  139. P_R,
  140. make_vec (union ((holes P_L),(holes P_R))),
  141. xi,
  142. Set.empty) :: init t
  143.  
  144. (* Definition 8 (PAM state) *)
  145. type local_state = partial_match list * (graph * graph)
  146. type control_stack = (control * local_state) list
  147. type pam_state = local_state * control_stack
  148.  
  149. (* Definition 9 (Initial PAM state) *)
  150. fun initial_state p =
  151. ((init (Semantics.reactionRules), (Nil,p)), [])
  152.  
  153. (* Corollary definitions *)
  154.  
  155. (* The first element of the tuple is the "alpha" parameter,
  156. indicating whether we are in an active context *)
  157. fun matchpre (true, k_i, PM) =
  158. Set.union (matchnode (k_i, PM), init (Semantics.reactionRules))
  159. | matchpre (false, k_i, PM) =
  160. matchnode (k_i, PM)
  161.  
  162. and matchnode (k_i, PM) =
  163. let
  164. fun m ((Par (Prefix (k,P_L), P'_L), L, P_R, D, Xi, I) :: rest) =
  165. (P_L, P'_L :: L, P_R, D, Xi, Set.union (I,indexOf k_i)) :: m rest
  166. | m (h::t) = m t
  167. | m [] = []
  168. in
  169. m PM
  170. end
  171.  
  172. and matchparcon(Prefix(k,p),PM) =
  173. union (matchpar (Prefix(k,p),PM), matchcontext PM)
  174.  
  175. and matchpar(ki_p as Prefix(k,p),PM) =
  176. let
  177. fun m [] = []
  178. | m ((Par(Hole j,P_L), L, P_R, D, xi, I) :: rest) =
  179. (Par(Hole j,P_L),
  180. L,
  181. P_R,
  182. (D,j) |-> (Par(ki_p, Vector.sub (D,j))),
  183. xi,
  184. union (I, indexOf k)) :: m rest
  185. | m (_::rest) = m rest
  186. in
  187. m PM
  188. end
  189.  
  190. and matchcontext [] = []
  191. | matchcontext ((m as (_,[],_,_,_,_))::rest) = m :: matchcontext rest
  192. | matchcontext (_::rest) = matchcontext rest
  193.  
  194. fun act _ = false
  195.  
  196. fun pop [] = []
  197. | pop ((H, P'_L :: L, P_R, D, xi, I) :: rest) =
  198. let
  199. fun c (Par (l,r)) = c l andalso c r
  200. | c (Hole j) = true
  201. | c _ = false
  202. in
  203. if not (c H) then pop rest
  204. else ((P'_L, L, P_R, D, xi, I) :: pop rest)
  205. end
  206.  
  207. (* Rule PAR *)
  208.  
  209. fun PAR ((PM, (q, Par(Par(p,p'),p''))), S) =
  210. ((PM, (q, Par(p,Par(p',p'')))), S)
  211. | PAR _ = raise RuleNotApplicable "PAR"
  212.  
  213. (* Rule UNIT_strip *)
  214.  
  215. fun UNIT_strip ((PM, (q, Par(Nil,p))), S) =
  216. ((PM, (q, p)), S)
  217. | UNIT_strip _ = raise RuleNotApplicable "UNIT_strip"
  218.  
  219. (* Rule UNIT_add *)
  220.  
  221. fun UNIT_add ((PM, (q, Prefix (k_i,p))), S) =
  222. ((PM, (q, Par (Prefix (k_i,p),Nil))), S)
  223. | UNIT_add _ = raise RuleNotApplicable "UNIT_add"
  224.  
  225. (* Rule MATCH *)
  226.  
  227. fun MATCH ((PM, (q, Par (Prefix (k_i,p), p'))), S) =
  228. ((matchpre(act S, k_i, PM), (Nil, p)),
  229. (k_i, (matchparcon (Prefix (k_i,p),PM), (q,p'))) :: S)
  230. | MATCH _ = raise RuleNotApplicable "MATCH"
  231.  
  232. (* Rule POP *)
  233.  
  234. fun POP ((PM, (q,Nil)), (k_j, (PM', (q',p))) :: S) =
  235. ((Set.union (PM',pop PM), (Par(q',Prefix(k_j,q)),p)), S)
  236. | POP _ = raise RuleNotApplicable "POP"
  237.  
  238. (* Rule REACT *)
  239.  
  240. fun pIndMemb (k : control,I) =
  241. case #index k of NONE => false
  242. | SOME i => Set.member (i,I)
  243.  
  244. fun excl (Prefix (k,g),I) =
  245. if pIndMemb (k,I) then Nil else Prefix (k, excl (g,I))
  246. | excl (Par (g1,g2),I) = Par (excl (g1,I), excl (g2,I))
  247. | excl (x,I) = x
  248.  
  249. fun exclPM ([],I) = []
  250. | exclPM (PM,I) =
  251. Set.filter (fn (_, _, _, _, _, I') => Set.inter (I,I') = []) PM
  252.  
  253. fun exclS ([],I) = []
  254. | exclS ((k,(PM,(p,q)))::S_t,I) =
  255. if pIndMemb (k,I) then exclS (S_t,I)
  256. else (k,(exclPM (PM, I), (excl (p,I),excl (q,I)))) :: exclS (S_t,I)
  257.  
  258. fun index (Par (g1,g2)) = Par (index g1, index g2)
  259. | index (Prefix (k,g)) = Prefix (freshIndex k, index g)
  260. | index x = x
  261.  
  262. fun inst(P_R,D,xi) =
  263. let
  264. fun s (Hole j) =
  265. let
  266. fun inst_lk [] = Hole j
  267. | inst_lk ((a,b)::t) =
  268. if a = j then Vector.sub(D,b) else inst_lk t
  269. in
  270. inst_lk xi
  271. end
  272. | s (Par(a,b)) = Par(s a, s b)
  273. | s (Prefix(a,b)) = Prefix(a,s b)
  274. | s (Index(a,i)) = Index(s a,i)
  275. | s Nil = Nil
  276. in
  277. s P_R
  278. end
  279.  
  280. fun REACT ((PM_all, (q,p)), S) =
  281. let
  282. val s = ref NONE : partial_match option ref
  283.  
  284. fun allHoles (Hole j) = true
  285. | allHoles (Par (a,b)) = allHoles a andalso allHoles b
  286. | allHoles _ = false
  287.  
  288. fun pmmatch ((P_L,[],P_R,D,Xi,I)::rest) =
  289. if allHoles P_L then (s := SOME (P_L,[],P_R,D,Xi,I); rest)
  290. else (P_L,[],P_R,D,Xi,I) :: pmmatch rest
  291. | pmmatch (h::t) = h :: pmmatch t
  292. | pmmatch [] = []
  293. in
  294. case (pmmatch PM_all,!s) of
  295. (_,NONE) => raise RuleNotApplicable "REACT"
  296. | (PM,SOME (P_L,_,P_R,D,Xi,I)) =>
  297. ((exclPM (PM,I),
  298. (Par (excl (q,I), index (inst (P_R,D,Xi))), p)),
  299. exclS (S,I))
  300. end
  301.  
  302. (* Rule TOP *)
  303. fun TOP ((PM, (q,Nil)), []) =
  304. ((init (Semantics.reactionRules), (Nil,q)), [])
  305. | TOP _ = raise RuleNotApplicable "TOP"
  306.  
  307. end
Add Comment
Please, Sign In to add comment