1 module Vtslib( Option(..) , Sum(..) , forall , exists , assoc , 2 haskey , uncurry , curry , for , map' , module Core_datatype ) 3 where 4 5 import Core_datatype 6 7 data Option a = NONE | SOME a 8 9 data Sum a b = Inl a | Inr b 10 11 {- 12 (* Apply the predicate p to all the elements of *) 13 (* a list, and then AND the results together. *) 14 N.B. forall & exists rewritten from ML 15 -} 16 17 forall :: ( a -> Bool ) -> [a] -> Bool 18 19 forall p = and . ( map p ) 20 21 22 23 24 25 {- 26 (* Apply the predicate p to all the elements of *) 27 (* a list, and then OR the results together. *) 28 -} 29 30 exists :: ( a -> Bool ) -> [a] -> Bool 31 32 exists p = or . ( map p ) 33 34 35 36 37 38 {- 39 (* Return the value stored in a association list *) 40 (* store under key. An association list is a *) 41 (* list of pairs (key, value) *) 42 -} 43 44 assoc :: ( Eq a ) => a -> [(a,b)] -> Option b 45 46 assoc key [] = NONE 47 48 assoc key ((key',entry):l') 49 | key == key' = SOME entry 50 | otherwise = assoc key l' 51 52 53 54 55 56 57 {- 58 (* Return whether an association list has a particular key. *) 59 -} 60 61 62 haskey key al 63 = case assoc key al of 64 SOME _ -> True 65 NONE -> False 66 67 68 69 70 {- 71 val op !! = nth 72 73 fun foldl f = 74 let fun fold e [] = e 75 | fold e (a::l) = fold (f a e) l 76 in fold end 77 78 fun foldr f e = 79 let fun fold [] = e 80 | fold (a::l) = f a (fold l) 81 in fold end 82 83 exception Reduce 84 85 fun reducel f [a] = a 86 | reducel f (a::b::l) = reducel f (f a b :: l) 87 | reducel f [] = raise Reduce 88 89 fun reducer f [a] = a 90 | reducer f (a::l) = f a (reducer f l) 91 | reducer f [] = raise Reduce 92 -} 93 94 95 96 {- 97 (* Return an uncurried version of a curried function *) 98 -} 99 100 --in 1.3: uncurry f (a,b) = f a b 101 102 103 104 105 {- 106 (* Return a curried version of an uncurried function *) 107 -} 108 109 --in 1.3: curry f a b = f (a,b) 110 111 112 113 114 115 116 for :: Int -> (b -> b) -> b -> b 117 118 for 0 f y = y 119 120 for i f y = for (i-1) f ( f y ) 121 122 123 {- 124 fun str_to_int s = 125 let val s_len = size s 126 val zero_ord = fromEnum "0" 127 val nine_ord = fromEnum "9" 128 fun is_digit i = i >= zero_ord andalso i <= nine_ord 129 fun convert si i = 130 if si >= s_len 131 then i 132 else if is_digit (ordof (s,si)) then 133 convert (si + 1) (i * 10 - zero_ord + ordof (s,si)) 134 else raise Str_to_int 135 in if s_len > 1 then 136 if ordof (s, 0) = fromEnum "~" then 137 ~ (convert 1 0) 138 else 139 convert 0 0 140 else if s_len > 0 then 141 convert 0 0 142 else 143 raise Str_to_int 144 end 145 -} 146 147 map' :: (Int -> b -> c) -> [b] -> [c] 148 149 map' f = mapf 0 150 where 151 mapf i [] = [] 152 153 mapf i (x:l) = f i x : mapf (i+1) l 154