Changeset 62 for Deliverables/D4.1/Nibble.ml
 Timestamp:
 Sep 10, 2010, 5:37:19 PM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Deliverables/D4.1/Nibble.ml
r57 r62 1 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) 2 (* FILENAME: Nibble.ml *) 3 (* DESCRIPTION: An ADT implementing nibbles, and common operations on them. *) 4 (* CREATED: 10/09/2010, Dominic Mulligan *) 5 (* BUGS: *) 6 (*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*) 7 1 8 open Bit;; 2 9 3 module type NIBBLE = functor(Bit: BIT) >10 module type NIBBLE = 4 11 sig 5 type bit = Bit.bit12 type bit 6 13 type nibble 7 14 8 15 val from_bits: bit > bit > nibble 16 val from_int: int > nibble option 9 17 10 val upper_bit: nibble > bit11 val lower_bit: nibble > bit18 val get_bit_at: int > nibble > bit option 19 val set_bit_at: int > bit > nibble > nibble option 12 20 13 21 val (&): nibble > nibble > nibble … … 16 24 val not: nibble > nibble 17 25 18 val swap : nibble > nibble26 val swap_bits: nibble > nibble 19 27 20 val mask: (bit > bit) > nibble > nibble 21 val zip: (bit > bit > bit) > nibble > nibble > nibble 28 val map_bit: (bit > bit) > nibble > nibble 29 val iter_bit: (bit > string) > nibble > string 30 val zip_bit: (bit > bit > bit) > nibble > nibble > nibble 31 val pretty: nibble > string 22 32 end 23 33 24 module NibbleFunctor : NIBBLE = functor(Bit: BIT) >34 module NibbleFunctor(Bit: BIT) : NIBBLE with type bit = Bit.bit = 25 35 struct 26 36 type bit = Bit.bit … … 28 38 29 39 let from_bits l h = (l, h) 40 let from_int int_val = 41 match int_val with 42 0 > Some (Bit.from_int 0, Bit.from_int 0) 43  1 > Some (Bit.from_int 0, Bit.from_int 1) 44  2 > Some (Bit.from_int 1, Bit.from_int 0) 45  3 > Some (Bit.from_int 1, Bit.from_int 1) 46  _ > None 30 47 31 let upper_bit (l, r) = r 32 let lower_bit (l, r) = l 48 let get_bit_at index (l, h) = 49 if index = 0 then 50 Some l 51 else if index = 1 then 52 Some h 53 else 54 None 55 56 let set_bit_at index new_bit (l, h) = 57 if index = 0 then 58 Some (new_bit, h) 59 else if index = 1 then 60 Some (l, new_bit) 61 else 62 None 33 63 34 64 let (&) (l1,h1) (l2, h2) = … … 41 71 let not (l, h) = (Bit.not l, Bit.not h) 42 72 43 let swap (l, h) = (h, l)73 let swap_bits (l, h) = (h, l) 44 74 45 let mask f (l, h) = (f l, f h) 46 let zip f (l1, h1) (l2, h2) = (f l1 l2, f l2 h2) 75 let map_bit f (l, h) = (f l, f h) 76 let iter_bit f (l, h) = 77 let str_l = f l in 78 let str_h = f h in 79 str_l ^ str_h 80 let zip_bit f (l1, h1) (l2, h2) = (f l1 l2, f l2 h2) 81 let pretty = iter_bit Bit.pretty 47 82 end 83 84 module Nibble = NibbleFunctor(Bit)
Note: See TracChangeset
for help on using the changeset viewer.