Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.83 KB | None | 0 0
  1. open Hw2_unify;;
  2. open Printf
  3.  
  4. (* Write in console to run:                                          *)
  5. (* ocamlc -o <EXECUTOR-NAME> -I ../HW1  ../HW1/hw1.mli ../HW1/hw1.ml  hw2_unify.mli hw2_unify.ml t_unify.ml *)
  6. (* > ./<EXECUTOR-NAME>                                               *)
  7. type substitution = (string * algebraic_term) list
  8. type equation = algebraic_term * algebraic_term
  9. type system_of_equations = equation list
  10.  
  11. let eqt11 = Fun("x", [Var "p1"]), Fun("x", [Var "p2"])
  12. let eqt12 = Fun("y", [Var "p2"]), Fun("y", [Var "p4"])
  13. let eqt13 = Fun("z", [Var "p5"]), Fun("z", [Var "p6"])
  14.  
  15. let eqt21 = Fun("x", [Var "p1"]), Fun("x", [Var "p2"])
  16. let eqt22 = Fun("m", [Var "p1"]), Fun("y", [Var "p4"])
  17. let eqt23 = Fun("z", [Var "p5"]), Fun("z", [Var "p6"])
  18.  
  19. let eqt31 = Fun("x", [Var "p1"]), Fun("x", [Var "p2"])
  20. let eqt32 = Fun("y", [Var "p1"]), Fun("y", [Var "p4"])
  21. let eqt33 = Fun("z", [Var "p1"]), Fun("z", [Var "p6"])
  22.  
  23. let sym1 = [eqt11; eqt12; eqt13];;
  24. let sym2 = [eqt21; eqt22; eqt23];;
  25. let sym3 = [eqt31; eqt32; eqt33];;
  26.  
  27.  
  28. let vx = Hw1.Var "x"
  29. let vy = Hw1.Var "y"
  30. let vz = Hw1.Var "z"
  31. let s = Hw1.Abs ("x", Hw1.Abs("y", Hw1.Abs ("z", Hw1.App (Hw1.App (vx, vz), Hw1.App (vy, vz)))))
  32. let k = Hw1.Abs ("a", Hw1.Abs ("b", Hw1.Var "a"))
  33.  
  34. let lambda_to_system term =
  35.     let cnt = ref 0 in
  36.     let arrow l r = Fun ("A", [l; r]) in
  37.     let rec helper t =
  38.         match t with
  39.         | Hw1.Var x ->
  40.             ([], Hw2_unify.Var ("t" ^ x))
  41.         | Hw1.App (p, q) -> cnt := !cnt + 1;
  42.                         let new_type = Hw2_unify.Var ("t" ^ (string_of_int !cnt)) in
  43.                         let (sys_p, type_p) = helper p in
  44.                         let (sys_q, type_q) = helper q in
  45.                         ((type_p, arrow type_q new_type)
  46.                             :: (List.rev_append  sys_p sys_q), new_type)
  47.         | Hw1.Abs (x, body) ->
  48.                         let res = helper body in
  49.                         (fst res, arrow (Hw2_unify.Var ("t" ^ x)) (snd res))
  50.     in let (sys, tp) = helper term in
  51.     (sys, tp) ;;
  52.  
  53.  
  54. let subt = [("p1", Var("p3"));("p3", Var("p4"));("p5", Var("p6"))]
  55.  
  56. let rec algebraic_term_to_string (at : algebraic_term) =
  57.     let rec impl a =
  58.         match a with
  59.         | Var x -> x
  60.         | Fun(f, l) -> f ^ "(" ^ impl_for_list l ^ ")"
  61.    
  62.     and impl_for_list lt =
  63.         match lt with
  64.         | [] -> ""
  65.         | (h::[]) -> impl h
  66.         | (h::t) -> (impl h) ^ " " ^ (impl_for_list t)
  67.     in
  68.     impl at;;
  69.  
  70. let eqt_to_string (eqt : equation) =
  71.     match eqt with
  72.     | (l, r) -> algebraic_term_to_string l ^ " = " ^ algebraic_term_to_string r;;
  73.  
  74. let rec print_substitution sub =
  75.     match sub with
  76.     | [] -> print_string "\n"
  77.     | (h::t) -> print_string ((fst h) ^ " = " ^ (algebraic_term_to_string (snd h)) ^ "\n"); print_substitution t;;  
  78.  
  79. let print_ans ans =
  80.     match ans with
  81.     | Some s -> print_substitution s
  82.     | None -> print_string "Not substitution!\n";;
  83.  
  84. let (sys, _) = lambda_to_system (Hw1.App (s, k)) in
  85. let ans = solve_system sys in
  86. let correct = match solve_system sys with
  87.     | Some sub -> check_solution sub sys
  88.     | None -> printf "No solution\n"; true in
  89. printf "System\n";
  90. List.iter (fun eq -> printf "%s\n" (eqt_to_string eq)) sys;
  91. printf "Answer\n";
  92. print_ans ans;
  93. printf "S K : %B\n" correct;;
  94.  
  95.  
  96. let maj_eqt = system_to_equation(sym1);;
  97.  
  98. let eql = apply_substitution subt (fst maj_eqt);;
  99. let eqr = apply_substitution subt (snd maj_eqt);;
  100.  
  101. print_string ((algebraic_term_to_string eql) ^ "\n");;
  102. print_string ((algebraic_term_to_string eqr) ^ "\n");;
  103.  
  104. let check_subt = check_solution subt sym1;;
  105. print_string ((string_of_bool check_subt) ^ "\n");;
  106.  
  107. let ans_sym1 = solve_system sym1;;
  108. let ans_sym2 = solve_system sym2;;
  109. let ans_sym3 = solve_system sym3;;
  110.  
  111. print_ans ans_sym1;;
  112. print_ans ans_sym2;;
  113. print_ans ans_sym3;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement