package lutin

  1. Overview
  2. Docs

Source file prog.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(*-----------------------------------------------------------------------
** Copyright (C) - Verimag.
** This file may only be copied under the terms of the CeCill
** Public License
**-----------------------------------------------------------------------
**
** File: prog.ml
** Author: erwan.jahier@univ-grenoble-alpes.fr
*)


(****************************************************************************)
(* exported *)
type atomic_ctrl_state = string
type ctrl_state = atomic_ctrl_state list



(****************************************************************************)
type dyn_weight = V of int | Infin

(** A wt (weigthed tree) is a n-ary tree *)
type wt = children Util.StringMap.t * string  (* the top-level node of the wt *) 
    
and children =
  | Children of (dyn_weight * string) list
  | Leave of Exp.formula * atomic_ctrl_state
  | Stop of string


(****************************************************************************)
(* exported *)
type  dynamic_state_fields = {
  (* memory  : Var.subst list; *)
  memory : Var.env;
  ctrl_state : ctrl_state list;
  (* input : Var.subst list; *)
  last_input : Var.env;
  last_output : Var.env;
  last_local : Var.env;
  snt: Solver.t;
  verbose : int
}

(* exported *)
type t = {
  initial_ctrl_state : ctrl_state list;
  in_vars  : Exp.var list;
  out_vars : Exp.var list;
  loc_vars : Exp.var list;
  ext_func_tbl : Exp.ext_func_tbl;
  memories_names   : (string * Exp.var) list ;
  bool_vars_to_gen : Exp.var list list ;
  num_vars_to_gen  : Exp.var list list ;
  output_var_names : Var.name list list ; (* redundant, but useful *)
  reactive : bool ; (* A scorie to remove. At least, it should be a list of bool *)

  get_wtl :  Var.env_in -> state -> ctrl_state -> wt list;
  is_final : ctrl_state list -> bool;
  gen_dot : ctrl_state list -> ctrl_state list -> string -> int
}

(* exported *)
and state = {
  d : dynamic_state_fields ;
  s : t
}

let (memory_of_state : state -> Var.env) = fun s -> s.d.memory 
let last_values_of_state st = (st.d.last_input, st.d.last_output, st.d.last_local)

(****************************************************************************)
(* exported *)
let (ctrl_state_to_string : ctrl_state -> string) =
  fun nl -> 
    String.concat "," nl

(* exported *)
let (string_to_atomic_ctrl_state : string -> atomic_ctrl_state) =
  fun str -> str

(* exported *)
let (ctrl_state_list_to_string_list : ctrl_state -> string list) =
  fun str -> str


(****************************************************************************)
(* exported *)
let (ctrl_state_to_string_long : ctrl_state list -> string) =
  fun ctrl_states -> 
    let cnl = List.map ctrl_state_to_string ctrl_states in
    let nodes_str = String.concat " " cnl in
    let node_msg =
      if
	     (List.length cnl) = 1
      then
	     ("current node: " ^ nodes_str)
      else
	     ("current nodes: " ^ nodes_str)
    in
      (node_msg ^ "\n")

(****************************************************************************)
let (compute_weight : Exp.weight -> Var.env_in -> state -> dyn_weight) =
  fun w input state ->
    match w with
	   Exp.Wint(i) -> V i
    | Exp.Infinity -> Infin
    | Exp.Wexpr(e) ->
      let ctx_msg = ctrl_state_to_string_long state.d.ctrl_state in 
      match Solver.eval_int_expr state.d.snt e ctx_msg input
              state.d.memory state.d.verbose
      with
      | Some v -> V v
      | None -> 
        print_string (
          "*** The weight expression " ^ (Exp.weight_to_string w) ^
          " ougth to depend only on pre and input vars\n");
        exit 2



(****************************************************************************)
(* exported *)
let dyn_weight_to_string =
  function
      V i -> string_of_int i
    | Infin -> "infinity"

(****************************************************************************)
(* exported *)
let rec (print_wt : wt -> unit) =
  fun (tbl, n) -> 
    print_string ("toplevel node:" ^ n ^ "\n");
    Util.StringMap.iter 
      (fun n children -> 
	      print_string ("\n\t" ^ n ^ " -> ");
	      print_children children
      )
      tbl;
    print_string "\n---------------------------------------------\n";
    flush stdout
and 
    print_children = function
	     Children l -> 
	       List.iter 
	         (fun (dw,n) -> 
	            print_string (dyn_weight_to_string dw);
	            print_string (":" ^ n ^ ", ");
	         )
	         l
      | Leave (f,next_state) -> 
	       print_string ((Exp.formula_to_string f) ^ "->"^next_state)
      | Stop str ->  print_string ("Stop " ^ str)
   



(****************************************************************************)
(* exported *)
let (print_mem : state -> unit) = 
  fun s -> 
    print_string "*** memories:\n";
    (* Var.print_subst_list s.d.memory stdout; *)
    output_string stdout (Value.OfIdent.to_string "" s.d.memory);
    flush stdout
  
(****************************************************************************)