Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. open Lib
  2. open Printf
  3. open Str
  4.  
  5.  
  6. let rec ocamlcoq l =
  7. match l with
  8. | [] -> Nil
  9. | h :: t -> Cons (h, (ocamlcoq t))
  10.  
  11. let rec ocamlnat n =
  12. match n with
  13. | 0 -> O
  14. | _ -> S (ocamlnat (n -1))
  15.  
  16. let balfun l =
  17. match l with
  18. | [(A, b1); (B, b2); (C, b3); (D, b4)] ->
  19. let
  20. f c = match c with
  21. | A -> b1
  22. | B -> b2
  23. | C -> b3
  24. | D -> b4
  25. in f
  26. | _ -> failwith "failed to match pattern"
  27.  
  28.  
  29. let input_line_opt ic =
  30. try Some (input_line ic)
  31. with End_of_file -> None
  32.  
  33. let read_lines ic =
  34. let rec aux acc =
  35. match input_line_opt ic with
  36. | Some line -> aux (line::acc)
  37. | None -> (List.rev acc)
  38. in
  39. aux []
  40.  
  41. let lines_of_file filename =
  42. let ic = open_in filename in
  43. let lines = read_lines ic in
  44. close_in ic;
  45. lines
  46.  
  47.  
  48. let process_input filename =
  49. let lines = lines_of_file filename in
  50. List.map (fun x -> Str.split (Str.regexp ";") x) lines
  51.  
  52. let lst =
  53. [[(A,3);(B,1);(C,2);(D,4)];
  54. [(A,1);(B,0);(C,4);(D,3)];
  55. [(A,3);(B,1);(C,2);(D,4)];
  56. [(A,2);(B,3);(C,3);(D,4)];
  57. [(A,3);(B,1);(C,2);(D,4)];
  58. [(A,1);(B,2);(C,3);(D,4)];
  59. [(A,1);(B,2);(C,3);(D,4)];
  60. [(A,1);(B,2);(C,2);(D,4)];
  61. [(A,1);(B,2);(C,2);(D,4)];
  62. [(A,1);(B,3);(C,2);(D,4)]]
  63.  
  64. let l =
  65. let w = List.map (fun x -> List.map (fun (a, b) -> (a, ocamlnat b)) x) lst in
  66. let v = List.map (fun x -> balfun x) w in
  67. ocamlcoq v
  68.  
  69. let rec print_list = function
  70. [] -> ()
  71. | e::l -> match e with
  72. | True -> printf "%B" true ; print_string " "; print_list l
  73. | False -> printf "%B" false ; print_string " " ; print_list l
  74.  
  75. let _ =
  76. match schulze_winners_pf l with
  77. | ExistT (f, ExistT (y, _)) -> print_list (List.map f [A; B; C; D])
  78.  
  79.  
  80.  
  81.  
  82. process_input "example-votes.txt";;
  83. - : string BatList.t BatList.t =
  84. [["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,0)"; "(C,4)"; "(D,3)"];
  85. ["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,2)"; "(B,3)"; "(C,3)"; "(D,4)"];
  86. ["(A,3)"; "(B,1)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,2)"; "(C,3)"; "(D,4)"];
  87. ["(A,1)"; "(B,2)"; "(C,3)"; "(D,4)"]; ["(A,1)"; "(B,2)"; "(C,2)"; "(D,4)"];
  88. ["(A,1)"; "(B,2)"; "(C,2)"; "(D,4)"]; ["(A,1)"; "(B,3)"; "(C,2)"; "(D,4)"]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement