3 Description: dimension estimation and delay estimation of faust expressions.
5 Created: 03/06/2013 Modified: 04/06/2013
13 (** Exception raised in beam matching of faust expressions.*)
14 exception Beam_Matching_Error of string;;
16 (** Exception raised in case that the branch under call hasn't yet been programed.*)
17 exception NotYetDone;;
20 (* PROCESS DELAY ESTIMATION *)
22 (** val delay : faust_exp -> int, returns the number of delays estimated staticly.
23 Attention: delays of "@" is estimated as 10 constant,
24 delays of "vectorize" and "serialize" haven't been implemented,
25 delays of "rdtable" hasn't been implemented.*)
26 let rec delay exp_faust = match exp_faust with
38 |Delay -> 100000 (* danger! *)
46 |Rdtable -> 100000 (* danger! *)
50 |Vectorize -> 100 (* danger! *)
58 |Par (e1, e2) -> max (delay e1) (delay e2)
59 |Seq (e1, e2) -> (delay e1) + (delay e2)
60 |Split (e1, e2) -> (delay e1) + (delay e2)
61 |Merge (e1, e2) -> (delay e1) + (delay e2)
62 |Rec (e1, e2) -> delay e1;;
67 (** val exp_of_string : string -> faust_exp, faust expression parser. *)
68 let exp_of_string s = (Parser.main Lexer.token (Lexing.from_string s));;
72 (* PROCESS DIMENSION ESTIMATION *)
73 (* process dimension := (size of input beam, size of output beam).*)
76 (** val get_root : dimension -> int * int, returns the root of dimension tree. *)
77 let get_root = fun d_tree -> match d_tree with
79 | Tree (d, branches) -> d;;
82 (** val subtree : dimention -> int -> dimension, returns a subtree of dimension tree.*)
83 let subtree = fun d_tree -> fun i ->
85 | End d -> raise (Beam_Matching_Error "Subtree left absent.")
86 | Tree (d, branches) -> (
88 (left, right) -> if i = 0 then left else right);;
90 (** val subtree_left : dimension -> dimension, returns the left subtree of dimension tree.*)
91 let subtree_left = fun d_tree -> subtree d_tree 0;;
94 (** val subtree_right : dimension -> dimension, returns the right subtree of dimension tree.*)
95 let subtree_right = fun d_tree -> subtree d_tree 1;;
98 (** val d_par : int * int -> int * int -> int * int, process dimension for constructor "par(,)",
99 which is the addition of two dimensions.*)
100 let d_par a b = (((fst a) + (fst b)), ((snd a) + (snd b)));;
103 (** val d_seq : int * int -> int * int -> int * int, process dimension for constructor "seq(:)",
104 which is (size of input beam of first exp, size of output beam of second exp)
105 along with beam matching.*)
106 let d_seq a b = if (snd a) = (fst b) then (fst a, snd b) else raise (Beam_Matching_Error "seq");;
109 (** val d_split : int * int -> int * int -> int * int, process dimension for constructor "split(<:)",
110 which is (size of input beam of first exp, size of output beam of second exp)
111 along with beam matching.*)
113 if ((fst b) mod (snd a)) = 0 then
115 else raise (Beam_Matching_Error "split");;
118 (** val d_merge : int * int -> int * int -> int * int, process dimension for constructor "merge(:>)",
119 which is (size of input beam of first exp, size of output beam of second exp)
120 along with beam matching. *)
122 if ((snd a) mod (fst b)) = 0 then
124 else raise (Beam_Matching_Error "merge");;
127 (** val d_rec : int * int -> int * int -> int * int, process dimension for constructor "rec(~)",
128 which is (size of input beam of first exp - size of output beam of second exp,
129 size of output beam of first exp)
130 along with beam matching.*)
132 if (fst a) >= (snd b) && (snd a) >= (fst b) then
133 ((fst a) - (snd b), snd a)
134 else raise (Beam_Matching_Error "rec");;
137 (** val dim : faust_exp -> int * int, returns dimension for faust expression,
138 along with beam matching.*)
139 let rec dim exp_faust =
141 (** val dimension_constructor : ((int * int) -> (int * int) -> (int * int)) -> faust_exp
142 -> faust_exp -> dimension,
143 returns the dimension tree of constructor(e1, e2).*)
144 let dimension_constructor = fun constructor -> fun e1 -> fun e2 ->
145 let subtree1 = dim e1 in
146 let subtree2 = dim e2 in
147 let root = constructor (get_root subtree1) (get_root subtree2) in
148 Tree (root, (subtree1, subtree2)) in
151 |Const v -> End (0, 1)
168 |Atantwo -> End (2, 1)
170 |Rdtable -> End (3, 1)
172 |Vectorize -> End (2, 1)
173 |Concat -> End (2, 1)
175 |Serialize -> End (1, 1)
176 |Larger -> End (2, 1)
177 |Smaller -> End (2, 1)
178 |Prefix -> End (2, 1)
179 |Selecttwo -> End (3, 1)
180 |Selectthree -> End (4, 1)
183 |Par (e1, e2) -> dimension_constructor d_par e1 e2
184 |Seq (e1, e2) -> dimension_constructor d_seq e1 e2
185 |Split (e1, e2) -> dimension_constructor d_split e1 e2
186 |Merge (e1, e2) -> dimension_constructor d_merge e1 e2
187 |Rec (e1, e2) -> dimension_constructor d_rec e1 e2;;
191 (* AUXILIARY 'CONVERT_TO_STRING' FUNCTIONS *)
193 (** val print_exp : faust_exp -> unit, print to console the input faust expression.*)
195 let rec string_of_exp exp = match exp with
196 |Const v -> "Const" ^ " (" ^ (string_of_value v) ^ ")"
197 |Ident s -> "Ident" ^ " \"" ^ "s" ^ "\""
198 |Par (e1, e2) -> "Par" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
199 |Seq (e1, e2) -> "Seq" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
200 |Split (e1, e2) -> "Split" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
201 |Merge (e1, e2) -> "Merge" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
202 |Rec (e1, e2) -> "Rec" ^ " (" ^ (string_of_exp e1) ^ ", " ^ (string_of_exp e2) ^ ")"
204 print_string("Parer : Types.faust_exp = "^ (string_of_exp exp));;