3 Description: dimension estimation and delay estimation of faust expressions.
5 Created: 03/06/2013 Modified: 04/06/2013
12 exception NotYetDone;;
16 class virtual expression : expression_type =
17 fun (exp_init : faust_exp) ->
20 val dim = new dimension
24 method get_delay = delay
25 method to_string = "NotYetDone"
26 method virtual evaluate : beam_type -> beam_type
33 method evaluate = fun b1 ->
82 (* PROCESS DELAY ESTIMATION *)
84 (** val delay : faust_exp -> int, returns the number of delays estimated staticly.
85 Attention: delays of "@" is estimated as 10 constant,
86 delays of "vectorize" and "serialize" haven't been implemented,
87 delays of "rdtable" hasn't been implemented.*)
88 let rec delay exp_faust = match exp_faust with
100 |Delay -> 100000 (* danger! *)
104 |Rdtable -> 100000 (* danger! *)
108 |Vectorize -> 100 (* danger! *)
116 |Par (e1, e2) -> max (delay e1) (delay e2)
117 |Seq (e1, e2) -> (delay e1) + (delay e2)
118 |Split (e1, e2) -> (delay e1) + (delay e2)
119 |Merge (e1, e2) -> (delay e1) + (delay e2)
120 |Rec (e1, e2) -> delay e1;;
125 (** val exp_of_string : string -> faust_exp, faust expression parser. *)
126 let exp_of_string s = (Parser.main Lexer.token (Lexing.from_string s));;
130 (* PROCESS DIMENSION ESTIMATION *)
131 (* process dimension := (size of input beam, size of output beam).*)
134 (** val get_root : dimension -> int * int, returns the root of dimension tree. *)
135 let get_root = fun d_tree -> match d_tree with
137 | Tree (d, branches) -> d;;
140 (** val subtree : dimention -> int -> dimension, returns a subtree of dimension tree.*)
141 let subtree = fun d_tree -> fun i ->
143 | End d -> raise (Beam_Matching_Error "Subtree left absent.")
144 | Tree (d, branches) -> (
146 (left, right) -> if i = 0 then left else right);;
148 (** val subtree_left : dimension -> dimension, returns the left subtree of dimension tree.*)
149 let subtree_left = fun d_tree -> subtree d_tree 0;;
152 (** val subtree_right : dimension -> dimension, returns the right subtree of dimension tree.*)
153 let subtree_right = fun d_tree -> subtree d_tree 1;;
156 (** val d_par : int * int -> int * int -> int * int, process dimension for constructor "par(,)",
157 which is the addition of two dimensions.*)
158 let d_par a b = (((fst a) + (fst b)), ((snd a) + (snd b)));;
161 (** val d_seq : int * int -> int * int -> int * int, process dimension for constructor "seq(:)",
162 which is (size of input beam of first exp, size of output beam of second exp)
163 along with beam matching.*)
164 let d_seq a b = if (snd a) = (fst b) then (fst a, snd b) else raise (Beam_Matching_Error "seq");;
167 (** val d_split : int * int -> int * int -> int * int, process dimension for constructor "split(<:)",
168 which is (size of input beam of first exp, size of output beam of second exp)
169 along with beam matching.*)
171 if ((fst b) mod (snd a)) = 0 then
173 else raise (Beam_Matching_Error "split");;
176 (** val d_merge : int * int -> int * int -> int * int, process dimension for constructor "merge(:>)",
177 which is (size of input beam of first exp, size of output beam of second exp)
178 along with beam matching. *)
180 if ((snd a) mod (fst b)) = 0 then
182 else raise (Beam_Matching_Error "merge");;
185 (** val d_rec : int * int -> int * int -> int * int, process dimension for constructor "rec(~)",
186 which is (size of input beam of first exp - size of output beam of second exp,
187 size of output beam of first exp)
188 along with beam matching.*)
190 if (fst a) >= (snd b) && (snd a) >= (fst b) then
191 ((fst a) - (snd b), snd a)
192 else raise (Beam_Matching_Error "rec");;
195 (** val dim : faust_exp -> int * int, returns dimension for faust expression,
196 along with beam matching.*)
197 let rec dim exp_faust =
199 (** val dimension_constructor : ((int * int) -> (int * int) -> (int * int)) -> faust_exp
200 -> faust_exp -> dimension,
201 returns the dimension tree of constructor(e1, e2).*)
202 let dimension_constructor = fun constructor -> fun e1 -> fun e2 ->
203 let subtree1 = dim e1 in
204 let subtree2 = dim e2 in
205 let root = constructor (get_root subtree1) (get_root subtree2) in
206 Tree (root, (subtree1, subtree2)) in
209 |Const v -> End (0, 1)
224 |Rdtable -> End (3, 1)
226 |Vectorize -> End (2, 1)
227 |Concat -> End (2, 1)
229 |Serialize -> End (1, 1)
230 |Larger -> End (2, 1)
231 |Smaller -> End (2, 1)
232 |Prefix -> End (2, 1)
233 |Selecttwo -> End (3, 1)
234 |Selectthree -> End (4, 1)
237 |Par (e1, e2) -> dimension_constructor d_par e1 e2
238 |Seq (e1, e2) -> dimension_constructor d_seq e1 e2
239 |Split (e1, e2) -> dimension_constructor d_split e1 e2
240 |Merge (e1, e2) -> dimension_constructor d_merge e1 e2
241 |Rec (e1, e2) -> dimension_constructor d_rec e1 e2;;
245 (* AUXILIARY 'CONVERT_TO_STRING' FUNCTIONS *)
247 (** val print_exp : faust_exp -> unit, print to console the input faust expression.*)
249 let rec string_of_exp exp = match exp with
250 |Const v -> "Const" ^ " (" ^ (string_of_value v) ^ ")"
251 |Ident s -> "Ident" ^ " \"" ^ "s" ^ "\""
252 |Par (e1, e2) -> "Par" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
253 |Seq (e1, e2) -> "Seq" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
254 |Split (e1, e2) -> "Split" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
255 |Merge (e1, e2) -> "Merge" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
256 |Rec (e1, e2) -> "Rec" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
258 print_string("Parer : Types.faust_exp = "^ (string_of_exp exp));;