Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.05 KB | None | 0 0
  1. (* A modification of binary-set-fn.sml to be able to handle set elements
  2. * that contain sets. *)
  3.  
  4. (* binary-set-fn.sml
  5. *
  6. * COPYRIGHT (c) 1993 by AT&T Bell Laboratories. See COPYRIGHT file for details.
  7. *
  8. * This code was adapted from Stephen Adams' binary tree implementation
  9. * of applicative integer sets.
  10. *
  11. * Copyright 1992 Stephen Adams.
  12. *
  13. * This software may be used freely provided that:
  14. * 1. This copyright notice is attached to any copy, derived work,
  15. * or work including all or part of this software.
  16. * 2. Any derived work must contain a prominent notice stating that
  17. * it has been altered from the original.
  18. *
  19. * Name(s): Stephen Adams.
  20. * Department, Institution: Electronics & Computer Science,
  21. * University of Southampton
  22. * Address: Electronics & Computer Science
  23. * University of Southampton
  24. * Southampton SO9 5NH
  25. * Great Britian
  26. * E-mail: sra@ecs.soton.ac.uk
  27. *
  28. * Comments:
  29. *
  30. * 1. The implementation is based on Binary search trees of Bounded
  31. * Balance, similar to Nievergelt & Reingold, SIAM J. Computing
  32. * 2(1), March 1973. The main advantage of these trees is that
  33. * they keep the size of the tree in the node, giving a constant
  34. * time size operation.
  35. *
  36. * 2. The bounded balance criterion is simpler than N&R's alpha.
  37. * Simply, one subtree must not have more than `weight' times as
  38. * many elements as the opposite subtree. Rebalancing is
  39. * guaranteed to reinstate the criterion for weight>2.23, but
  40. * the occasional incorrect behaviour for weight=2 is not
  41. * detrimental to performance.
  42. *
  43. * 3. There are two implementations of union. The default,
  44. * hedge_union, is much more complex and usually 20% faster. I
  45. * am not sure that the performance increase warrants the
  46. * complexity (and time it took to write), but I am leaving it
  47. * in for the competition. It is derived from the original
  48. * union by replacing the split_lt(gt) operations with a lazy
  49. * version. The `obvious' version is called old_union.
  50. *
  51. * 4. Most time is spent in T', the rebalancing constructor. If my
  52. * understanding of the output of *<file> in the sml batch
  53. * compiler is correct then the code produced by NJSML 0.75
  54. * (sparc) for the final case is very disappointing. Most
  55. * invocations fall through to this case and most of these cases
  56. * fall to the else part, i.e. the plain contructor,
  57. * T(v,ln+rn+1,l,r). The poor code allocates a 16 word vector
  58. * and saves lots of registers into it. In the common case it
  59. * then retrieves a few of the registers and allocates the 5
  60. * word T node. The values that it retrieves were live in
  61. * registers before the massive save.
  62. *
  63. * Modified to functor to support general ordered values
  64. *)
  65. signature BINARY_TREE =
  66. sig
  67. type 'a set
  68. val compare : ('a * 'b -> order) -> 'a set * 'b set -> order
  69. end
  70.  
  71. local
  72.  
  73. structure BinaryTreeInternal =
  74. struct
  75. datatype 'a set
  76. = E
  77. | T of {
  78. elt : 'a,
  79. cnt : int,
  80. left : 'a set,
  81. right : 'a set
  82. }
  83.  
  84. local
  85. fun next ((t as T{right, ...})::rest) = (t, left(right, rest))
  86. | next _ = (E, [])
  87. and left (E, rest) = rest
  88. | left (t as T{left=l, ...}, rest) = left(l, t::rest)
  89. in
  90. (********** Moved from BinarySetFn and made to take key_cmp **********)
  91. fun compare key_cmp (s1, s2) = let
  92. fun cmp (t1, t2) = (case (next t1, next t2)
  93. of ((E, _), (E, _)) => EQUAL
  94. | ((E, _), _) => LESS
  95. | (_, (E, _)) => GREATER
  96. | ((T{elt=e1, ...}, r1), (T{elt=e2, ...}, r2)) => (
  97. case key_cmp(e1, e2)
  98. of EQUAL => cmp (r1, r2)
  99. | order => order
  100. (* end case *))
  101. (* end case *))
  102. in
  103. cmp (left(s1, []), left(s2, []))
  104. end
  105. end
  106. end
  107. in
  108.  
  109. structure BinaryTree : BINARY_TREE = BinaryTreeInternal
  110.  
  111. functor BinarySetFn (K : ORD_KEY) : ORD_SET =
  112. struct
  113.  
  114. structure Key = K
  115.  
  116. type item = K.ord_key
  117. (********** Modified type and compare implementation **********)
  118. open BinaryTreeInternal
  119. type set = item set
  120. fun compare sets = BinaryTreeInternal.compare K.compare sets
  121.  
  122. fun numItems E = 0
  123. | numItems (T{cnt,...}) = cnt
  124.  
  125. fun isEmpty E = true
  126. | isEmpty _ = false
  127.  
  128. fun mkT(v,n,l,r) = T{elt=v,cnt=n,left=l,right=r}
  129.  
  130. (* N(v,l,r) = T(v,1+numItems(l)+numItems(r),l,r) *)
  131. fun N(v,E,E) = mkT(v,1,E,E)
  132. | N(v,E,r as T{cnt=n,...}) = mkT(v,n+1,E,r)
  133. | N(v,l as T{cnt=n,...}, E) = mkT(v,n+1,l,E)
  134. | N(v,l as T{cnt=n,...}, r as T{cnt=m,...}) = mkT(v,n+m+1,l,r)
  135.  
  136. fun single_L (a,x,T{elt=b,left=y,right=z,...}) = N(b,N(a,x,y),z)
  137. | single_L _ = raise Match
  138. fun single_R (b,T{elt=a,left=x,right=y,...},z) = N(a,x,N(b,y,z))
  139. | single_R _ = raise Match
  140. fun double_L (a,w,T{elt=c,left=T{elt=b,left=x,right=y,...},right=z,...}) =
  141. N(b,N(a,w,x),N(c,y,z))
  142. | double_L _ = raise Match
  143. fun double_R (c,T{elt=a,left=w,right=T{elt=b,left=x,right=y,...},...},z) =
  144. N(b,N(a,w,x),N(c,y,z))
  145. | double_R _ = raise Match
  146.  
  147. (*
  148. ** val weight = 3
  149. ** fun wt i = weight * i
  150. *)
  151. fun wt (i : int) = i + i + i
  152.  
  153. fun T' (v,E,E) = mkT(v,1,E,E)
  154. | T' (v,E,r as T{left=E,right=E,...}) = mkT(v,2,E,r)
  155. | T' (v,l as T{left=E,right=E,...},E) = mkT(v,2,l,E)
  156.  
  157. | T' (p as (_,E,T{left=T _,right=E,...})) = double_L p
  158. | T' (p as (_,T{left=E,right=T _,...},E)) = double_R p
  159.  
  160. (* these cases almost never happen with small weight*)
  161. | T' (p as (_,E,T{left=T{cnt=ln,...},right=T{cnt=rn,...},...})) =
  162. if ln<rn then single_L p else double_L p
  163. | T' (p as (_,T{left=T{cnt=ln,...},right=T{cnt=rn,...},...},E)) =
  164. if ln>rn then single_R p else double_R p
  165.  
  166. | T' (p as (_,E,T{left=E,...})) = single_L p
  167. | T' (p as (_,T{right=E,...},E)) = single_R p
  168.  
  169. | T' (p as (v,l as T{elt=lv,cnt=ln,left=ll,right=lr},
  170. r as T{elt=rv,cnt=rn,left=rl,right=rr})) =
  171. if rn >= wt ln (*right is too big*)
  172. then
  173. let val rln = numItems rl
  174. val rrn = numItems rr
  175. in
  176. if rln < rrn then single_L p else double_L p
  177. end
  178. else if ln >= wt rn (*left is too big*)
  179. then
  180. let val lln = numItems ll
  181. val lrn = numItems lr
  182. in
  183. if lrn < lln then single_R p else double_R p
  184. end
  185. else mkT(v,ln+rn+1,l,r)
  186.  
  187. fun add (E,x) = mkT(x,1,E,E)
  188. | add (set as T{elt=v,left=l,right=r,cnt},x) =
  189. case K.compare(x,v) of
  190. LESS => T'(v,add(l,x),r)
  191. | GREATER => T'(v,l,add(r,x))
  192. | EQUAL => mkT(x,cnt,l,r)
  193. fun add' (s, x) = add(x, s)
  194.  
  195. fun concat3 (E,v,r) = add(r,v)
  196. | concat3 (l,v,E) = add(l,v)
  197. | concat3 (l as T{elt=v1,cnt=n1,left=l1,right=r1}, v,
  198. r as T{elt=v2,cnt=n2,left=l2,right=r2}) =
  199. if wt n1 < n2 then T'(v2,concat3(l,v,l2),r2)
  200. else if wt n2 < n1 then T'(v1,l1,concat3(r1,v,r))
  201. else N(v,l,r)
  202.  
  203. fun split_lt (E,x) = E
  204. | split_lt (T{elt=v,left=l,right=r,...},x) =
  205. case K.compare(v,x) of
  206. GREATER => split_lt(l,x)
  207. | LESS => concat3(l,v,split_lt(r,x))
  208. | _ => l
  209.  
  210. fun split_gt (E,x) = E
  211. | split_gt (T{elt=v,left=l,right=r,...},x) =
  212. case K.compare(v,x) of
  213. LESS => split_gt(r,x)
  214. | GREATER => concat3(split_gt(l,x),v,r)
  215. | _ => r
  216.  
  217. fun min (T{elt=v,left=E,...}) = v
  218. | min (T{left=l,...}) = min l
  219. | min _ = raise Match
  220.  
  221. fun delmin (T{left=E,right=r,...}) = r
  222. | delmin (T{elt=v,left=l,right=r,...}) = T'(v,delmin l,r)
  223. | delmin _ = raise Match
  224.  
  225. fun delete' (E,r) = r
  226. | delete' (l,E) = l
  227. | delete' (l,r) = T'(min r,l,delmin r)
  228.  
  229. fun concat (E, s) = s
  230. | concat (s, E) = s
  231. | concat (t1 as T{elt=v1,cnt=n1,left=l1,right=r1},
  232. t2 as T{elt=v2,cnt=n2,left=l2,right=r2}) =
  233. if wt n1 < n2 then T'(v2,concat(t1,l2),r2)
  234. else if wt n2 < n1 then T'(v1,l1,concat(r1,t2))
  235. else T'(min t2,t1, delmin t2)
  236.  
  237.  
  238. local
  239. fun trim (lo,hi,E) = E
  240. | trim (lo,hi,s as T{elt=v,left=l,right=r,...}) =
  241. if K.compare(v,lo) = GREATER
  242. then if K.compare(v,hi) = LESS then s else trim(lo,hi,l)
  243. else trim(lo,hi,r)
  244.  
  245. fun uni_bd (s,E,_,_) = s
  246. | uni_bd (E,T{elt=v,left=l,right=r,...},lo,hi) =
  247. concat3(split_gt(l,lo),v,split_lt(r,hi))
  248. | uni_bd (T{elt=v,left=l1,right=r1,...},
  249. s2 as T{elt=v2,left=l2,right=r2,...},lo,hi) =
  250. concat3(uni_bd(l1,trim(lo,v,s2),lo,v),
  251. v,
  252. uni_bd(r1,trim(v,hi,s2),v,hi))
  253. (* inv: lo < v < hi *)
  254.  
  255. (* all the other versions of uni and trim are
  256. * specializations of the above two functions with
  257. * lo=-infinity and/or hi=+infinity
  258. *)
  259.  
  260. fun trim_lo (_, E) = E
  261. | trim_lo (lo,s as T{elt=v,right=r,...}) =
  262. case K.compare(v,lo) of
  263. GREATER => s
  264. | _ => trim_lo(lo,r)
  265.  
  266. fun trim_hi (_, E) = E
  267. | trim_hi (hi,s as T{elt=v,left=l,...}) =
  268. case K.compare(v,hi) of
  269. LESS => s
  270. | _ => trim_hi(hi,l)
  271.  
  272. fun uni_hi (s,E,_) = s
  273. | uni_hi (E,T{elt=v,left=l,right=r,...},hi) =
  274. concat3(l,v,split_lt(r,hi))
  275. | uni_hi (T{elt=v,left=l1,right=r1,...},
  276. s2 as T{elt=v2,left=l2,right=r2,...},hi) =
  277. concat3(uni_hi(l1,trim_hi(v,s2),v),v,uni_bd(r1,trim(v,hi,s2),v,hi))
  278.  
  279. fun uni_lo (s,E,_) = s
  280. | uni_lo (E,T{elt=v,left=l,right=r,...},lo) =
  281. concat3(split_gt(l,lo),v,r)
  282. | uni_lo (T{elt=v,left=l1,right=r1,...},
  283. s2 as T{elt=v2,left=l2,right=r2,...},lo) =
  284. concat3(uni_bd(l1,trim(lo,v,s2),lo,v),v,uni_lo(r1,trim_lo(v,s2),v))
  285.  
  286. fun uni (s,E) = s
  287. | uni (E,s) = s
  288. | uni (T{elt=v,left=l1,right=r1,...},
  289. s2 as T{elt=v2,left=l2,right=r2,...}) =
  290. concat3(uni_hi(l1,trim_hi(v,s2),v), v, uni_lo(r1,trim_lo(v,s2),v))
  291.  
  292. in
  293. val hedge_union = uni
  294. end
  295.  
  296. (* The old_union version is about 20% slower than
  297. * hedge_union in most cases
  298. *)
  299. fun old_union (E,s2) = s2
  300. | old_union (s1,E) = s1
  301. | old_union (T{elt=v,left=l,right=r,...},s2) =
  302. let val l2 = split_lt(s2,v)
  303. val r2 = split_gt(s2,v)
  304. in
  305. concat3(old_union(l,l2),v,old_union(r,r2))
  306. end
  307.  
  308. val empty = E
  309. fun singleton x = T{elt=x,cnt=1,left=E,right=E}
  310.  
  311. fun addList (s,l) = List.foldl (fn (i,s) => add(s,i)) s l
  312.  
  313. fun fromList l = addList (E, l)
  314.  
  315. val add = add
  316.  
  317. fun member (set, x) = let
  318. fun pk E = false
  319. | pk (T{elt=v, left=l, right=r, ...}) = (
  320. case K.compare(x,v)
  321. of LESS => pk l
  322. | EQUAL => true
  323. | GREATER => pk r
  324. (* end case *))
  325. in
  326. pk set
  327. end
  328.  
  329. local
  330. (* true if every item in t is in t' *)
  331. fun treeIn (t,t') = let
  332. fun isIn E = true
  333. | isIn (T{elt,left=E,right=E,...}) = member(t',elt)
  334. | isIn (T{elt,left,right=E,...}) =
  335. member(t',elt) andalso isIn left
  336. | isIn (T{elt,left=E,right,...}) =
  337. member(t',elt) andalso isIn right
  338. | isIn (T{elt,left,right,...}) =
  339. member(t',elt) andalso isIn left andalso isIn right
  340. in
  341. isIn t
  342. end
  343. in
  344. fun isSubset (E,_) = true
  345. | isSubset (_,E) = false
  346. | isSubset (t as T{cnt=n,...},t' as T{cnt=n',...}) =
  347. (n<=n') andalso treeIn (t,t')
  348.  
  349. fun equal (E,E) = true
  350. | equal (t as T{cnt=n,...},t' as T{cnt=n',...}) =
  351. (n=n') andalso treeIn (t,t')
  352. | equal _ = false
  353. end
  354.  
  355. fun delete (E,x) = raise LibBase.NotFound
  356. | delete (set as T{elt=v,left=l,right=r,...},x) =
  357. case K.compare(x,v) of
  358. LESS => T'(v,delete(l,x),r)
  359. | GREATER => T'(v,l,delete(r,x))
  360. | _ => delete'(l,r)
  361.  
  362. val union = hedge_union
  363.  
  364. fun intersection (E, _) = E
  365. | intersection (_, E) = E
  366. | intersection (s, T{elt=v,left=l,right=r,...}) = let
  367. val l2 = split_lt(s,v)
  368. val r2 = split_gt(s,v)
  369. in
  370. if member(s,v)
  371. then concat3(intersection(l2,l),v,intersection(r2,r))
  372. else concat(intersection(l2,l),intersection(r2,r))
  373. end
  374.  
  375. fun difference (E,s) = E
  376. | difference (s,E) = s
  377. | difference (s, T{elt=v,left=l,right=r,...}) =
  378. let val l2 = split_lt(s,v)
  379. val r2 = split_gt(s,v)
  380. in
  381. concat(difference(l2,l),difference(r2,r))
  382. end
  383.  
  384. fun map f set = let
  385. fun map'(acc, E) = acc
  386. | map'(acc, T{elt,left,right,...}) =
  387. map' (add (map' (acc, left), f elt), right)
  388. in
  389. map' (E, set)
  390. end
  391.  
  392. fun app apf =
  393. let fun apply E = ()
  394. | apply (T{elt,left,right,...}) =
  395. (apply left;apf elt; apply right)
  396. in
  397. apply
  398. end
  399.  
  400. fun foldl f b set = let
  401. fun foldf (E, b) = b
  402. | foldf (T{elt,left,right,...}, b) =
  403. foldf (right, f(elt, foldf (left, b)))
  404. in
  405. foldf (set, b)
  406. end
  407.  
  408. fun foldr f b set = let
  409. fun foldf (E, b) = b
  410. | foldf (T{elt,left,right,...}, b) =
  411. foldf (left, f(elt, foldf (right, b)))
  412. in
  413. foldf (set, b)
  414. end
  415.  
  416. fun listItems set = foldr (op::) [] set
  417.  
  418. fun filter pred set =
  419. foldl (fn (item, s) => if (pred item) then add(s, item) else s)
  420. empty set
  421.  
  422. fun partition pred set =
  423. foldl
  424. (fn (item, (s1, s2)) =>
  425. if (pred item) then (add(s1, item), s2) else (s1, add(s2, item))
  426. )
  427. (empty, empty) set
  428.  
  429. fun find p E = NONE
  430. | find p (T{elt,left,right,...}) = (case find p left
  431. of NONE => if (p elt)
  432. then SOME elt
  433. else find p right
  434. | a => a
  435. (* end case *))
  436.  
  437. fun exists p E = false
  438. | exists p (T{elt, left, right,...}) =
  439. (exists p left) orelse (p elt) orelse (exists p right)
  440.  
  441. end (* BinarySetFn *)
  442.  
  443. end
  444.  
  445. structure Prover =
  446. struct
  447. type name = string
  448. (* Propositions are either atomic or an implication from a set
  449. * props to a prop *)
  450. datatype Prop = Atom of name
  451. | Imp of Prop BinaryTree.set * Prop
  452.  
  453. fun name_compare (n1, n2) = String.compare (n1, n2)
  454.  
  455. fun prop_compare (Atom n1, Atom n2) = name_compare (n1, n2)
  456. | prop_compare (Imp (ps, p), Imp (qs, q)) =
  457. (case BinaryTree.compare prop_compare (ps, qs) of
  458. EQUAL => prop_compare (p, q)
  459. | x => x)
  460. | prop_compare (Atom _, Imp _) = LESS
  461. | prop_compare (Imp _, Atom _) = GREATER
  462.  
  463. structure PropSet = BinarySetFn(struct
  464. type ord_key = Prop
  465. val compare = prop_compare
  466. end)
  467. val a = Atom "a"
  468. val b = Atom "b"
  469. val c = Atom "c"
  470.  
  471. val a_imp_b = Imp (PropSet.fromList [a], b)
  472. val c' = Imp (PropSet.fromList [a, a_imp_b], c)
  473. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement