daily pastebin goal
80%
SHARE
TWEET

SI units in OCaml

a guest Dec 3rd, 2011 164 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module SI : sig
  2.   type 'a t
  3.   type meter
  4.   type kilogram
  5.   type second
  6.  
  7.   type ('a, 'b) mul
  8.   type ('a, 'b) div
  9.  
  10.   val meters : float -> meter t
  11.   val kilograms : float -> kilogram t
  12.   val seconds : float -> second t
  13.  
  14.   val (+!) : 'a t -> 'a t -> 'a t
  15.   val (-!) : 'a t -> 'a t -> 'a t
  16.   val ( *! ) : 'a t -> 'a t -> ('a, 'b) mul t
  17.   val (/!) : 'a t -> 'a t -> ('a, 'b) div t
  18.  
  19.   val to_float : 'a t -> float
  20. end = struct
  21.   type 'a t = SiUnit of float
  22.   type meter
  23.   type kilogram
  24.   type second
  25.  
  26.   type ('a, 'b) mul
  27.   type ('a, 'b) div
  28.  
  29.   let meters x = SiUnit x
  30.   let kilograms x = SiUnit x
  31.   let seconds x = SiUnit x
  32.  
  33.   let (+!) (SiUnit x) (SiUnit y) = SiUnit (x +. y)
  34.   let (-!) (SiUnit x) (SiUnit y) = SiUnit (x -. y)
  35.  
  36.   let ( *! ) (SiUnit x) (SiUnit y) = SiUnit (x *. y)
  37.   let (/!) (SiUnit x) (SiUnit y) = SiUnit (x /. y)
  38.  
  39.   let to_float (SiUnit x) = x
  40. end
  41.  
  42.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top