Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / backends / OCaml / example.ml @ dbca8647

History | View | Annotate | Download (2.54 KB)

1
(* Example for the linear controller
2

    
3
   x = 1.5 pre x + 0.7 pre y + 1.6 u
4
   y = pre x
5

    
6
We obtain the machine 
7
machine top
8
    mem      : __top_2: real, __top_3: real
9
    instances: (ni_0, _arrow<>)
10
    init     : reset ni_0
11
    step     :
12
      inputs : in0: real
13
      outputs: x: real, y: real
14
      locals : __top_1: bool
15
      checks : 
16
      instrs : __top_1 = ni_0 (true, false)
17
               case(__top_1) {
18
                 false:
19
                   y<-l- __top_3
20
                   x<-l- + (- ( * (1.5, __top_3), * (0.7, __top_2)), * (1.6, in0))
21
                 true:
22
                   y<-l- 0.
23
                   x<-l- 0.
24
                 }
25
               __top_3<-s- x __top_2<-s- y
26

    
27
*)
28
        
29

    
30
class arrow = 
31
object
32
      (* attention pb de polymorphisme dans les classes. Faut-il introduire une arrow par type? *)
33
      val mutable init = true;
34
      method reset () = init <- true
35
      method step (a:bool) b = 
36
	let res = if init then a else b in
37
	init <- false;
38
	res
39
    end
40

    
41
module Bool = 
42
struct
43
  type t = True | False | Maybe
44
end
45

    
46
module type DOMAIN = sig
47
  type t
48
  val of_float : float -> t
49
  val of_float_float : float -> float -> t
50
  val eq : t -> t -> Bool.t
51
  val lt : t -> t -> Bool.t
52
  val le : t -> t -> Bool.t
53
  val add : t -> t -> t
54
  val sub : t -> t -> t
55
  val mul : t -> t -> t
56
  val div : t -> t -> t
57
  val neg : t -> t
58
  val abs : t -> t
59
  val sin : t -> t
60
  val cos : t -> t
61
  val exp : t -> t
62
  val pow : t -> t -> t
63
  val npow : t -> int -> t
64
  val sqrt : t -> t
65
  val log : t -> t
66
  val ite : Bool.t -> t -> t -> t
67
end
68

    
69
module Env = Map.Make(struct type t =string let compare = compare end)
70
module Name = 
71
(*  functor (D: DOMAIN) ->*)
72
struct
73
  
74
(*  type mem = D.t Env.t * D.t Env.t *)
75
    
76

    
77
  class top =
78
  object
79
    (* Default value for local memories *)
80
    val mutable __top_2 = 0.;
81
    val mutable __top_3 = 0.;
82
    (* Node instances *)
83
    val ni_0 = new arrow;
84
     
85
    method reset () = 
86
      __top_2 <- 0.;
87
      __top_3 <- 0.;
88
      ni_0#reset ()
89

    
90
    method step in0 =
91
      let __top_1 = ni_0#step true false in
92
      let y = 
93
	match __top_1 with
94
	| false -> __top_3
95
	| true -> 0.
96
      in
97
      let x = 
98
	match __top_1 with
99
	| false -> (1.5 *. __top_3) -. (0.7 *.__top_2) +. (1.6 *. in0)
100
	| true -> 0.
101
      in
102
      __top_3 <- x;
103
      __top_2 <- y;
104
      x, y (* list outputs *)
105

    
106
  end
107

    
108
  let main () = 
109
    let top_obj = new top in
110
    top_obj#reset ();
111
    while true do
112
      let in0 = read_float () in
113
      let x, y = top_obj#step in0 in
114
      Format.printf "in0: %f -> x: %f, y: %f@." in0 x y
115
    done
116
end
117

    
118
let _ = Name.main ()