let rec all_instantiations l1 l2 =
match l1 with
| [] -> []
| [x1] -> List.map (fun x2 -> [x1, x2]) l2
| x1 :: r1 ->
List.fold_left (fun acc l' ->
List.fold_left (fun acc x2 -> ((x1, x2) :: l') :: acc) acc l2
) [] (all_instantiations r1 l2)