Guest User

Untitled

a guest
Nov 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. order = enum {greater, equal, lesser}
  2. comparator<a> = (a,a)order
  3.  
  4. @assert pure, terminates
  5. fn sorted<a>(pair (List<a>,comparator<a>))bool {
  6. (l, cmp) = pair
  7. if l.length() <= 1 {
  8. return true
  9. }
  10. return cmp(l[0],l[1]) != order.greater & sorted(l.tail())
  11. }
  12.  
  13.  
  14. SortedList<a> = (List<a>,(a,a)order) & sorted
  15.  
  16. @assert pure, terminates
  17. fn empty(l SortedList<a>) bool {
  18. l[0].empty()
  19. }
  20.  
  21. @assert pure, terminates
  22. fn join<a>(start SortedList<a>,end SortedList<a>)SortedList<a>{
  23. assert: start[1] == end[1] // Comparators must be the same for this to make sense
  24. if start.empty() {
  25. return end
  26. }
  27. if end.empty(){
  28. return start
  29. }
  30. assert: start[1](start[0].last(),end[0].head()) != order.greater // can't make a sorted list by joining them
  31. return (start[0].join(end[0]), start[1])
  32. }
  33.  
  34. @assert pure, terminates
  35. fn insert<a>(list SortedList<a>, element a) SortedList<a>{
  36. // You get the idea
  37. }
  38.  
  39. @assert pure, terminates
  40. fn insertionSort<a>(l List<a>, cmp (a,a)order) SortedList<a> {
  41. if l.empty() {
  42. return ([],cmp)
  43. }
  44. out SortedList<a> = (l,cmp)
  45. for element in l {
  46. out = out.insert(element)
  47. }
  48. return out
  49. }
  50.  
  51. fn commutative<a,b>(f (a,a)b){
  52. assert x,y: f(x,y) == f(y,x)
  53. }
  54.  
  55. XY = (int, int)
  56.  
  57. @assert commutative
  58. fn add(a XY, b XY) XY {
  59. return (a[0]+b[0], a[1] + b[1])
  60. }
  61.  
  62. assert x: add(x,(0,0)) == x
Add Comment
Please, Sign In to add comment