+ module M = MathQL
+
+ let analyze x =
+ let rec join l1 l2 = match l1, l2 with
+ | [], _ -> l2
+ | _, [] -> l1
+ | s1 :: tl1, s2 :: _ when s1 < s2 -> s1 :: join tl1 l2
+ | s1 :: _, s2 :: tl2 when s2 < s1 -> s2 :: join l1 tl2
+ | s1 :: tl1, s2 :: tl2 -> s1 :: join tl1 tl2
+ in
+ let rec iter f = function
+ | [] -> []
+ | head :: tail -> join (f head) (iter f tail)
+ in
+ let rec an_val = function
+ | M.True -> []
+ | M.False -> []
+ | M.Const _ -> []
+ | M.VVar _ -> []
+ | M.Ex _ -> []
+ | M.Dot rv _ -> [rv]
+ | M.Not x -> an_val x
+ | M.StatVal x -> an_val x
+ | M.Count x -> an_val x
+ | M.Align _ x -> an_val x
+ | M.Proj _ x -> an_set x
+ | M.Test _ x y -> iter an_val [x; y]
+ | M.Set l -> iter an_val l
+ and an_set = function
+ | M.Empty -> []
+ | M.SVar _ -> []
+ | M.AVar _ -> []
+ | M.Subj x -> an_val x
+ | M.Keep _ _ x -> an_set x
+ | M.Log _ _ x -> an_set x
+ | M.StatQuery x -> an_set x
+ | M.Bin _ x y -> iter an_set [x; y]
+ | M.LetSVar _ x y -> iter an_set [x; y]
+ | M.For _ _ x y -> iter an_set [x; y]
+ | M.Add _ g x -> join (an_grp g) (an_set x)
+ | M.LetVVar _ x y -> join (an_val x) (an_set y)
+ | M.Select _ x y -> join (an_set x) (an_val y)
+ | M.Property _ _ _ _ c d _ _ x ->
+ join (an_val x) (iter an_con [c; List.concat d])
+ | M.If x y z -> join (an_val x) (iter an_set [y; z])
+ and fc (_, _, v) = an_val v
+ and an_con c = iter fc c
+ and fg (_, v) = an_val v
+ and an_grp = function
+ | M.Attr g -> iter (iter fg) g
+ | M.From _ -> []
+ in
+ an_val x
+
+ let f (x, y, z) = x
+ let s (x, y, z) = y
+ let t (x, y, z) = z