Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. module util where
  2.  
  3. // Determines if a given list is sorted
  4. isSorted : {a, b} (Cmp a, fin b) => [b]a -> Bit
  5. isSorted l = ~0 == [ e1 <= e2
  6. | e1 <- ([zero] # l) >> 1
  7. | e2 <- ([zero] # l) ]
  8.  
  9. // Determines if a given list is sorted and all sublists are sorted
  10. isSorted2d : {a, b, c} (Cmp b, fin a, fin c) => [a][c]b -> Bit
  11. isSorted2d l = isSorted l && ~0 == [isSorted e | e <- l]
  12.  
  13. // Trims or pads a list (usually a word) to fit the type
  14. cast : {a, b, c} (fin a, fin b) => [a]c -> [b]c
  15. cast list = drop ((zero:[b]c) # list)
  16.  
  17. // Fold from the left
  18. foldl : {a, b, c} (fin c) => (a -> b -> a) -> a -> [c]b -> a
  19. foldl fun base list = intermediates ! 0
  20. where intermediates = [fun pr el
  21. | el <- list
  22. | pr <- [base]#intermediates]
  23.  
  24. // Returns the sum of the elements of a list.
  25. sum : {a, b} (fin a, fin b) => [a][b] -> [b]
  26. sum list = foldl (\x y -> x+y) 0 ([0]#list)
  27.  
  28. // Converts from ascii single-character decimal digits to
  29. // integers. Note that This has no error checking.
  30. atoi : {a} (fin a) => Char -> [a]
  31. atoi c = cast (c - '0')
  32.  
  33. // Adds two numbers, adding a bit to ensure no overflow.
  34. safePlus : {a, b} (fin a, fin b) => [a] -> [b] -> [(max a b)+1]
  35. safePlus word1 word2 = (cast word1) + (cast word2)
  36.  
  37. // Multiplies two numbers, resizing to ensure no overflow.
  38. safeTimes : {a, b} (fin a, fin b) => [a] -> [b] -> [a+b]
  39. safeTimes word1 word2 = (cast word1) * (cast word2)
  40.  
  41. // Increment a number, adding a bit to ensure no overflow.
  42. inc : {a} (fin a) => [a] -> [a+1]
  43. inc word = (cast word) + 1
  44.  
  45. // Returns true if x is an element of set, false otherwise.
  46. isElem : {a, b} (Cmp a, fin b) => a -> [b]a -> Bit
  47. isElem x set = 0 != [x == y | y <- set]
  48.  
  49. // Counts the number of times a given element occurs in list.
  50. countElem : {a, b, c} (Cmp b, fin a, fin c) => b -> [a]b -> [c]
  51. countElem elem list =
  52. sum [if elem == item then (cast (1:[1])) else 0 | item <- list]
  53.  
  54. // Counts the number of different elements from set that appear in
  55. // list. This assumes that set is in fact a set (contains no
  56. // duplicates.)
  57. countElemsFromSet : {a, b, c, d} (Cmp c, fin a, fin b, fin d) =>
  58. [a]c -> [b]c -> [d]
  59. countElemsFromSet list set =
  60. sum [if isElem e list then (cast (1:[1])) else 0 | e <- set]
  61.  
  62. // Returns true if two lists of elements contain the same elements,
  63. // some of which may be duplicates, regardless of order.
  64. isPermutation : {a, b} (Cmp b, fin (width a), a >= 1) =>
  65. [a]b -> [a]b -> Bool
  66. isPermutation r1 r2 =
  67. ~0 == [(countElem`{c=width a} e r1) == (countElem e r2) | e <- r1]
  68.  
  69. // Returns the sum of the elements of a list
  70. safeSum : {a, b} (fin a, fin b) => [a][b] -> [b+a]
  71. safeSum list = foldl (\x y -> (cast x) + (cast y)) 0 ([0]#list)
  72.  
  73. // Given a list, removes the element indicated by offset, returning
  74. // the remaining list.
  75. removeElem : {a, b, c} (fin b, fin c) => [b] -> [1 + c]a -> [c]a
  76. removeElem offset list = take new_list
  77. where
  78. new_list = [if i < offset then normal else shift
  79. | normal <- list
  80. | shift <- list << 1
  81. | i <- [0...]]
  82.  
  83. // Returns all pairs of elements in a list. Order matters, so each
  84. // pair of elements is included twice in both possible orders.
  85. pairs : {a, b} (fin (1 + a)) => [1 + a]b -> [(1 + a) * a][2]b
  86. pairs list = join ps
  87. where
  88. shifts = [list] # [shift <<< 1 | shift <- shifts]
  89. pairWithEach a alist = [[a, b] | b <- alist]
  90. ps = [pairWithEach a (tail shift)
  91. | shift <- shifts
  92. | a <- list]
  93.  
  94. // Checks each pair of items from a list with the function comp and
  95. // returns the resulting list of booleans.
  96. pairCompare : {a, b} (fin (1 + a)) =>
  97. [a + 1]b -> (b -> b -> Bit) -> [(a + 1) * a]
  98. pairCompare list comp =
  99. [comp elem1 elem2 | [elem1, elem2] <- pairs list]
  100.  
  101. // returns true if list contains at least n instances of elem
  102. hasNInstances : {a, b, c} (Cmp c, fin b, fin a, a >= 1) =>
  103. [a] -> c -> [b]c -> Bit
  104. hasNInstances n elem list =
  105. n == foldl fun 0 list
  106. where fun c item = if c == n then n
  107. else if item == elem then c+1
  108. else c
  109.  
  110. // returns true if list contains at least n different elements of set
  111. hasNElemsFromSet : {a, b, c, d} (Cmp d, fin b, fin c, fin a,
  112. a >= 1) => [a] -> [b]d -> [c]d -> Bit
  113. hasNElemsFromSet n list set =
  114. n == foldl fun 0 set
  115. where fun c item = if c == n then n
  116. else if isElem item list then c+1
  117. else c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement