File: cs_util_coercion.ml

package info (click to toggle)
galax 1.1-10
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 12,832 kB
  • sloc: ml: 96,603; xml: 26,602; ansic: 4,875; sh: 3,977; makefile: 1,667; java: 1,146
file content (278 lines) | stat: -rw-r--r-- 10,725 bytes parent folder | download | duplicates (5)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(***********************************************************************)
(*                                                                     *)
(*                                 GALAX                               *)
(*                              XQuery Engine                          *)
(*                                                                     *)
(*  Copyright 2001-2007.                                               *)
(*  Distributed only by permission.                                    *)
(*                                                                     *)
(***********************************************************************)

(* $Id: cs_util_coercion.ml,v 1.21 2007/10/16 01:25:34 mff Exp $ *)

(* Module: Cs_util_coercion
   Description:
     This module implements coercion operation for algebraic operators
     input and output.
*)


open Xquery_algebra_ast
open Xquery_physical_type_ast
open Algebra_type
open Execution_context 

open Physical_value_util
open Physical_value
open Error

(* Module: Cs_util_coercion
   Description:
    Implementation of code to perform coercisions between differing datamodels
    and their helper functions 
*)


(* Signatures to clarify the types of some of the things
   below. *)

type 'a input_coercion_function = physical_value -> 'a 
type 'a return_coercion_function = 'a -> physical_value


(* val coerce_unit: 
  unit -> 'a return_coercion_function ->
  (Execution_context.algebra_context -> unit -> 'a)
  -> Algebra_type.alg_eval_code

val coerce_unary :
  'a input_coercion_function -> 'b return_coercion_function ->
  (Execution_context.algebra_context -> 'a -> 'b)
  -> Algebra_type.alg_eval_code

val coerce_binary : 
  'a input_coercion_function -> 'b return_coercion_function ->
  (Execution_context.algebra_context -> 'a -> 'a -> 'b)
  -> Algebra_type.alg_eval_code

val coerce_many :
  'a input_coercion_function -> 'b return_coercion_function ->
  (Execution_context.algebra_context -> 'a list -> 'b)
  -> Algebra_type.alg_eval_code
*)

(* These are helper functions (signatures above)
   their purpose is to apply the physical coercision
   functions 
   coerce: coercion (or fc1,fc2)
   rc: return coercion
   f: the function to be wrapped

   They return the constructed type (alg_eval_code) 
*)

let coerce_unit () rc f = 
  AOECUnit(fun alg_ctxt () -> 
    let f' = f alg_ctxt () in
    rc f')

let coerce_unary coerce rc f =
  AOECUnary(fun alg_ctxt to_coerce ->
    rc (f alg_ctxt (coerce to_coerce)))

let coerce_binary coerce rc f = 
  AOECBinary (fun alg_ctxt c1 c2 ->
    let x1 = coerce c1 in  (* Forces evaluation order *)
    let x2 = coerce c2 in
    rc (f alg_ctxt x1 x2))

let coerce_binary_hetero fc1 fc2 rc f = 
  AOECBinary (fun alg_ctxt c1 c2 ->
    let x1 = fc1 c1 in
    let x2 = fc2 c2 in
    rc (f alg_ctxt x1 x2))

let coerce_many coerce rc f = 
  AOECMany (fun alg_ctxt c_args ->
    rc (f alg_ctxt (Array.map coerce c_args)))


(* Same for the prolog operations *)

let coerce_unit_prolog () rc f = 
  PAOECUnit(fun alg_ctxt () -> 
    let (f':'a) = f alg_ctxt () in
    rc f')

let coerce_unary_prolog coerce rc f =
  PAOECUnary(fun alg_ctxt to_coerce ->
    rc (f alg_ctxt (coerce to_coerce)))

let coerce_binary_prolog coerce rc f = 
  PAOECBinary (fun alg_ctxt c1 c2 ->
    let x1 = coerce c1 in
    let x2 = coerce c2 in
    rc (f alg_ctxt x1 x2))

let coerce_binary_hetero_prolog fc1 fc2 rc f = 
  PAOECBinary (fun alg_ctxt c1 c2 ->
    let x1 = fc1 c1 in
    let x2 = fc2 c2 in
    rc (f alg_ctxt x1 x2))

let coerce_many_prolog coerce rc f = 
  PAOECMany (fun alg_ctxt c_args ->
		rc (f alg_ctxt (Array.map coerce c_args)))


(* for physical_model -> physical_model coercisions *)
let coerce_id x = x (* odd - explain this *)


(* Below here is exposed in the module *)
(* Parameter Coercions - for dependant parameters 
   see algebra_coercion.mli *)

let coerce_nodep input_code coercion_fun =
  NoDep ((fun ef -> coercion_fun input_code), None)

let coerce_unitdep input_code () coercion_fun =
  SomeDep ((fun ef ->
    let f' = input_code () ef in
    coercion_fun f'), None)
      
let coerce_onedep input_code dep_op coercion_fun =
  SomeDep ((fun  ef -> 
    let f' = input_code dep_op ef in		  
    coercion_fun f'), None)

let coerce_twodep input_code (ae1,ae2) coercion_fun =
  SomeDep ((fun ef -> 
    let f' = input_code (ae1,ae2) ef in
    coercion_fun f'), None)

let coerce_manydep input_code ae_array coercion_fun =
  SomeDep ((fun ef -> 
    let f' = input_code ae_array ef in
    coercion_fun f'), None)

let coerce_nodep_prolog input_code coercion_fun =
  PNoDep (coercion_fun input_code)

let coerce_onedep_prolog input_code dep_op coercion_fun =
  PSomeDep (fun ef ->
    let f' = input_code dep_op ef in
    coercion_fun f')


(* Signature helpers 
let make_signature_unit in_sig out_sig              = NoInput, out_sig
let make_signature_unary in_sig out_sig             = (OneInput in_sig), out_sig
let make_signature_binary (in_sig1,in_sig2) out_sig = (TwoInput (in_sig1, in_sig2)), out_sig 
let make_signature_many in_sig out_sig              = (ManyInput in_sig), out_sig
*)

(* Actual Coercision functions as explained in algebra_coercion.mli *)
let coerce_unit_to_xml f   = coerce_unit () physical_value_of_xml_value f

let coerce_unit_to_sax f   = coerce_unit () physical_value_of_sax_value f

let coerce_unit_to_item_cursor f   = coerce_unit () physical_value_of_item_cursor f

let coerce_unit_to_item_list f   = coerce_unit () physical_value_of_item_list f

let coerce_unit_to_physical_value f   = coerce_unit () coerce_id f

let coerce_unit_to_tuple f   = coerce_unit () physical_value_of_tuple f

let coerce_unit_to_item f   = coerce_unit () physical_value_of_item f

let coerce_unary_sax_to_sax f   = coerce_unary sax_value_of_physical_value physical_value_of_sax_value f

let coerce_binary_sax_to_sax f   = coerce_binary sax_value_of_physical_value physical_value_of_sax_value f

let coerce_unary_sax_to_item_list f   = coerce_unary sax_value_of_physical_value physical_value_of_item_list f

let coerce_unary_sax_to_item_cursor f   = coerce_unary sax_value_of_physical_value physical_value_of_item_cursor f

let coerce_unary_sax_to_tuple_cursor f   = coerce_unary sax_value_of_physical_value physical_value_of_tuple_cursor f

let coerce_unary_sax_to_physical_value f   = coerce_unary sax_value_of_physical_value coerce_id f

let coerce_unary_xml_to_xml f   = coerce_unary xml_value_of_physical_value physical_value_of_xml_value f

let coerce_unary_xml_to_item_list f   = coerce_unary xml_value_of_physical_value physical_value_of_item_list f

let coerce_unary_xml_to_tuple_cursor f   = coerce_unary xml_value_of_physical_value physical_value_of_tuple_cursor f

let coerce_unary_item_cursor_to_item_cursor f = 
 coerce_unary item_cursor_of_physical_value physical_value_of_item_cursor f

let coerce_unary_item_list_to_xml f = coerce_unary item_list_of_physical_value physical_value_of_xml_value f

let coerce_unary_item_cursor_to_xml f = coerce_unary item_cursor_of_physical_value physical_value_of_xml_value f

let coerce_unary_item_list_to_physical_value f = coerce_unary item_list_of_physical_value coerce_id f

let coerce_unary_item_list_to_item_list f   = coerce_unary item_list_of_physical_value physical_value_of_item_list f

let coerce_unary_item_list_to_tuple_cursor f   = coerce_unary item_list_of_physical_value physical_value_of_tuple_cursor f

let coerce_unary_item_cursor_to_item_list f   = coerce_unary item_cursor_of_physical_value physical_value_of_item_list f

let coerce_unary_tuple_cursor_to_tuple_cursor f = coerce_unary tuple_cursor_of_physical_value physical_value_of_tuple_cursor f

let coerce_unary_item_cursor_to_tuple_cursor f = coerce_unary item_cursor_of_physical_value physical_value_of_tuple_cursor f

let coerce_unary_tuple_cursor_to_item_cursor f = coerce_unary tuple_cursor_of_physical_value physical_value_of_item_cursor f

let coerce_unary_tuple_cursor_to_xml f = coerce_unary tuple_cursor_of_physical_value physical_value_of_xml_value f

let coerce_binary_tuple_cursor_to_tuple_cursor f = coerce_binary tuple_cursor_of_physical_value physical_value_of_tuple_cursor f

let coerce_binary_tuple_to_tuple f = coerce_binary (fun x -> Cursor.cursor_get_singleton (tuple_cursor_of_physical_value x)) (fun x -> physical_value_of_tuple_cursor (Cursor.cursor_of_singleton x)) f

let coerce_binary_item_cursor_to_item_cursor  f    = coerce_binary item_cursor_of_physical_value physical_value_of_item_cursor f

let coerce_binary_item_cursor_to_item_list  f   = coerce_binary item_cursor_of_physical_value physical_value_of_item_list f
let coerce_binary_item_cursor_to_tuple_cursor f = coerce_binary item_cursor_of_physical_value  physical_value_of_tuple_cursor f

let coerce_unary_tuple_cursor_to_physical_value f   = coerce_unary tuple_cursor_of_physical_value coerce_id f

let coerce_unary_item_cursor_to_physical_value f   = coerce_unary item_cursor_of_physical_value coerce_id f

let coerce_unary_xml_to_physical_value f = coerce_unary xml_value_of_physical_value coerce_id f

let coerce_binary_item_list_to_item_list f   = coerce_binary item_list_of_physical_value physical_value_of_item_list f

let coerce_binary_xml_and_sax_to_sax f   = 
  coerce_binary_hetero xml_value_of_physical_value sax_value_of_physical_value physical_value_of_sax_value f

let coerce_binary_item_cursor_and_sax_to_sax f   = 
  coerce_binary_hetero item_cursor_of_physical_value sax_value_of_physical_value physical_value_of_sax_value f

let coerce_binary_xml_and_xml_to_sax f   = 
  coerce_binary_hetero xml_value_of_physical_value xml_value_of_physical_value physical_value_of_sax_value f

let coerce_many_sax_to_sax f   = coerce_many sax_value_of_physical_value physical_value_of_sax_value f

let coerce_many_sax_to_item_list f   = coerce_many sax_value_of_physical_value physical_value_of_item_list f

let coerce_many_xml_to_tuple f = coerce_many xml_value_of_physical_value physical_value_of_tuple f

let coerce_many_item_cursor_to_item_cursor f = coerce_many item_cursor_of_physical_value physical_value_of_item_cursor f

let coerce_many_item_list_to_item_list f = coerce_many item_list_of_physical_value physical_value_of_item_list f

let coerce_many_item_cursor_to_physical_value f   = coerce_many item_cursor_of_physical_value coerce_id f

let coerce_binary_item_cursor_tuple_cursor_to_tuple_cursor f =
  coerce_binary_hetero item_cursor_of_physical_value tuple_cursor_of_physical_value physical_value_of_tuple_cursor f

(* Prolog coercions *)

let coerce_unary_item_cursor_to_algebra_context f =
    coerce_unary_prolog item_cursor_of_physical_value coerce_id f
let coerce_unit_to_algebra_context f =
    coerce_unit_prolog () coerce_id f