Revision 650a0ec8
src/backends/OCaml/example.ml  

27  27 
*) 
28  28 

29  29  
30 
class arrow = 

30 
class ['expr_t] arrow =


31  31 
object 
32 
(* attention pb de polymorphisme dans les classes. Fautil introduire une arrow par type? *) 

33  32 
val mutable init = true; 
34  33 
method reset () = init < true 
35 
method step (a:bool) b =


34 
method step a b :'expr_t =


36  35 
let res = if init then a else b in 
37  36 
init < false; 
38  37 
res 
...  ...  
64  63 
val sqrt : t > t 
65  64 
val log : t > t 
66  65 
val ite : Bool.t > t > t > t 
66 
val pp: Format.formatter > t > unit 

67 
end 

68  
69 
module DFloat = 

70 
struct 

71 
type t = float 

72 
let of_float x = x 

73 
let of_float_float a b = a +. (Random.float (b . a)) 

74 
let comp op x y = if op x y then Bool.True else Bool.False 

75 
let eq = comp (=) 

76 
let lt = comp (<) 

77 
let le = comp (<=) 

78 
let add = (+.) 

79 
let sub = (.) 

80 
let mul = ( *.) 

81 
let div = (/.) 

82 
let neg = (~.) 

83 
let abs = abs_float 

84 
let sin = sin 

85 
let cos = cos 

86 
let exp = exp 

87 
let pow = ( **) 

88 
let npow x i = x ** (float_of_int i) 

89 
let sqrt = sqrt 

90 
let log = log 

91 
let ite g a b = match g with  Bool.True > a  Bool.False > b  _ > assert false 

92 
let pp = Format.pp_print_float 

93  
67  94 
end 
68  95  
69  96 
module Env = Map.Make(struct type t =string let compare = compare end) 
70  97 
module Name = 
71 
(* functor (D: DOMAIN) >*)


98 
functor (D: DOMAIN) >


72  99 
struct 
73  100 

74 
(* type mem = D.t Env.t * D.t Env.t *) 

75 


101 
(* type mem = D.t Env.t * D.t Env.t *) 

76  102  
77  103 
class top = 
78  104 
object 
79  105 
(* Default value for local memories *) 
80 
val mutable __top_2 = 0.; 

81 
val mutable __top_3 = 0.; 

106 
val mutable __top_2 = D.of_float 0.;


107 
val mutable __top_3 = D.of_float 0.;


82  108 
(* Node instances *) 
83  109 
val ni_0 = new arrow; 
84  110 

85  111 
method reset () = 
86 
__top_2 < 0.; 

87 
__top_3 < 0.; 

112 
__top_2 < D.of_float 0.;


113 
__top_3 < D.of_float 0.;


88  114 
ni_0#reset () 
89  115  
90  116 
method step in0 = 
...  ...  
92  118 
let y = 
93  119 
match __top_1 with 
94  120 
 false > __top_3 
95 
 true > 0. 

121 
 true > D.of_float 0.


96  122 
in 
97  123 
let x = 
98  124 
match __top_1 with 
99 
 false > (1.5 *. __top_3) . (0.7 *.__top_2) +. (1.6 *. in0)


100 
 true > 0. 

125 
 false > D.add (D.sub (D.mul (D.of_float 1.5) __top_3) (D.mul (D.of_float 0.7) __top_2)) (D.mul (D.of_float 1.6) in0)


126 
 true > D.of_float 0.


101  127 
in 
102  128 
__top_3 < x; 
103  129 
__top_2 < y; 
...  ...  
109  135 
let top_obj = new top in 
110  136 
top_obj#reset (); 
111  137 
while true do 
112 
let in0 = read_float () in


138 
let in0 = D.of_float (read_float ()) in


113  139 
let x, y = top_obj#step in0 in 
114 
Format.printf "in0: %f > x: %f, y: %f@." in0 x y


140 
Format.printf "in0: %a > x: %a, y: %a@." D.pp in0 D.pp x D.pp y


115  141 
done 
116  142 
end 
117  143  
118 
let _ = Name.main () 

144 


145  
146 
module NameTop = Name (DFloat) 

147 
let _ = NameTop.main () 
Also available in: Unified diff