pcp.ml 9.27 KB
Newer Older
1
2
3
4
5
6
(** {1 PCP in lambda calculus}
This terms can be used to test and experiment. *)

open Alpha

let tree_of_string s = Grammar.input Lexer.token (Lexing.from_string (s^"\n"))
Frontull Samuel's avatar
Frontull Samuel committed
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

let s_ite = "(/c a b.c a b)"
let t_ite = tree_of_string s_ite

let s_true = "(/x y.x)"
let t_true = tree_of_string s_true

let s_false = "(/x y.y)"
let t_false = tree_of_string s_false
let s_pair = "(/a b c.c a b)"
let t_pair = tree_of_string s_pair
let s_first = "(/p.p (/x y.x))"
let t_first = tree_of_string s_first
let s_second = "(/p.p (/x y.y))"
let t_second = tree_of_string s_second

let s_empty = "(/x a b.x)"
let t_empty = tree_of_string s_empty

let t_at = tree_of_string "(/x./x y.x)"
let t_af = tree_of_string "(/x./x y.y)"
let s_concat = "(/y z x a b.y (z x a b) a b)"
let t_concat = tree_of_string s_concat
let t_isempty = tree_of_string "(/s.(s (/x y.x)) (/x./x y.y) (/x./x y.y))"
let t_prepa = tree_of_string "(/s x a b.a (s x a b))"
let t_prepb = tree_of_string "(/s x a b.b (s x a b))"
let t_nexta = tree_of_string "(/x.(/a b c.c a b) ((/s x a b.a (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x))"
let t_nextb = tree_of_string "(/x.(/a b c.c a b) ((/s x a b.b (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x))"
let t_tl = tree_of_string "(/s.(/p.p (/x y.y)) (s ((/a b c.c a b) (/x a b.x) (/x a b.x)) (/x.(/a b c.c a b) ((/s x a b.a (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)) (/x.(/a b c.c a b) ((/s x a b.b (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x))))"
let t_hd = tree_of_string "(/s x a b.s x (/y.a x) (/y.b x))"
let t_hd_a = tree_of_string "(/s.s (/x y.y) (/x./x y.x) (/x./x y.y))"
let t_hd_b = tree_of_string "(/s.s (/x y.y) (/x./x y.x) (/x./x y.x))"
let t_is_e = tree_of_string "(/s.s (/x y.x) (/x./x y.y) (/x./x y.y))"
let t_and = tree_of_string "(/x y.x y x)"

let s_or = "(/x y.x x y)"
let t_or = tree_of_string s_or

let t_hd_eq = tree_of_string "(/x y.(/x y.x x y) ((/x y.x y x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.y)) x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.y)) y)) ((/x y.x y x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.x)) x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.x)) y)))"
let s_ycomb = "(/f.(/x.f (x x)) (/x.f (x x)))"
let t_ycomb = tree_of_string s_ycomb

let s_zcomb = "(/f.(/x.f (/y.x x y)) (/x.f (/y.x x y)))"
let t_zcomb = tree_of_string s_zcomb

let _eq_str = "(/rec x y.(/c a b.c a b) ((/x y.x x y) ((/s.(s (/x y.x)) (/x./x y.y) (/x./x y.y)) x) ((/s.(s (/x y.x)) (/x./x y.y) (/x./x y.y)) y)) ((/x y.x y x) ((/s.(s (/x y.x)) (/x./x y.y) (/x./x y.y)) x) ((/s.(s (/x y.x)) (/x./x y.y) (/x./x y.y)) y)) ((/x y.x y x) ((/x y.(/x y.x x y) ((/x y.x y x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.y)) x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.y)) y)) ((/x y.x y x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.x)) x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.x)) y))) x y) (rec ((/s.(/p.p (/x y.y)) (s ((/a b c.c a b) (/x a b.x) (/x a b.x)) (/x.(/a b c.c a b) ((/s x a b.a (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)) (/x.(/a b c.c a b) ((/s x a b.b (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)))) x) ((/s.(/p.p (/x y.y)) (s ((/a b c.c a b) (/x a b.x) (/x a b.x)) (/x.(/a b c.c a b) ((/s x a b.a (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)) (/x.(/a b c.c a b) ((/s x a b.b (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)))) y))))";;
let s_eq_str = ("(/a b.(/f.(/x.f (x x)) (/x.f (x x))) " ^_eq_str ^ " a b)");;
let eq_str = tree_of_string s_eq_str;;

(* LISTS *)
let s_cons = "(/x y.(/a b c.c a b) (/x y.y) ((/a b c.c a b) x y))"
let t_cons = tree_of_string s_cons
let s_lhd = "(/z.(/p.p (/x y.x)) ((/p.p (/x y.y)) z))"
let t_lhd = tree_of_string s_lhd
let s_ltl = "(/z.(/p.p (/x y.y)) ((/p.p (/x y.y)) z))"
let t_ltl = tree_of_string s_ltl
let s_nil = "(/x.x)"
let t_nil = tree_of_string s_nil
let s_isnil = "(/p.p (/x y.x))"
let t_isnil = tree_of_string s_isnil

(* LIST with TUPLE (e,e) *)
let s_mktuple = "(/x y." ^ s_pair ^ " x y)"
let s_tuple_ee = "(" ^ s_mktuple ^ " " ^ s_empty ^ " " ^ s_empty ^ ")"
let s_listtuple_ee = "(" ^ s_cons ^ " " ^ s_tuple_ee ^ " " ^ s_nil ^ ")"
let t_listtuple_ee = tree_of_string s_listtuple_ee

(* LIST OF TILES *)
let s_t1_2 = "(/x a b.a x)"
let s_b1_2 = "(/x a b.a (b (b x)))"
let s_tile1 = "(" ^ s_mktuple ^ " " ^ s_t1_2 ^ " " ^ s_b1_2 ^ ")"

let s_t2_2 = "(/x a b.b x)"
let s_b2_2 = "(/x a b.x)"
let s_tile2 = "(" ^ s_mktuple ^ " " ^ s_t2_2 ^ " " ^ s_b2_2 ^ ")"

let s_tiles = "(" ^ s_cons ^ " " ^ s_tile2 ^ " (" ^ s_cons ^ " " ^ s_tile1 ^ " " ^ s_nil ^ "))"
let t_tiles = tree_of_string s_tiles

(* find if for (t1,t2), t1 = t2 in list of tuples *)
(* "/r x.ite (isnil x) (false) (or (eq_str (fst (lhd x)) (snd (lhd x))) (r (tl x)))" *)
let s__find_eq = "(/r x."^s_ite^" ("^s_isnil^" x) "^s_false^" ("^s_or^" ("^s_eq_str^" ("^s_first^" ("^s_lhd^" x)) ("^s_second^" ("^s_lhd^" x))) (r ("^s_ltl^" x))))"
let s_find_eq = "(/x."^s_ycomb^" "^s__find_eq^" x)"
let t_find_eq = tree_of_string s_find_eq

(* let tmp = Core.AApp(t_find_eq, t_tiles, UnT) *)

let s_concat_pairs = "(/p s."^s_mktuple^" ("^s_concat^" ("^s_first^" p) ("^s_first^" s)) ("^s_concat^" ("^s_second^" p) ("^s_second^" s)))"

let s__combine = "(/r x ys."^s_ite^" ("^s_isnil^" ys) ("^s_nil^") ("^s_cons^" ("^s_concat_pairs^" x ("^s_lhd^" ys)) (r x ("^s_ltl^" ys))))"
let s_combine = "(/x y."^s_ycomb^" "^s__combine^" x y)"

let s__cross_append = "(/r xs ys."^s_ite^" ("^s_isnil^" xs) ("^s_nil^") ("^s_cons^" ("^s_combine^" ("^s_lhd^" xs) ys) (r ("^s_ltl^" xs) ys)))"
let s_cross_append = "(/xs ys."^s_ycomb^" "^s__cross_append^" xs ys)"

let t_cross_append = tree_of_string s_cross_append
(* let tmp = Core.AApp(Core.AApp(t_cross_append, t_listtuple_ee, UnT), t_tiles, UnT) *)
(* let tmp = t_tiles *)

let s__pcp = "(/r xs tiles."^s_ite^" ("^s_find_eq^" ("^s__cross_append^" xs tiles)) "^s_true^" (r ("^s__cross_append^" xs tiles) tiles))"
let s_pcp = "(/tiles."^s_ycomb^" "^s__pcp^" "^s_listtuple_ee^" tiles)"
let t_pcp = tree_of_string s_pcp

let tmp = Core.AApp(t_pcp, t_tiles, UnT)



(* STRINGS *)
let s_e = tree_of_string "(/x a b.x)"
let s_a = tree_of_string "(/x a b.a x)"
let s_b = tree_of_string "(/x a b.b x)"
let s_ab = tree_of_string "(/x a b.a (b x))"
let s_aa = tree_of_string "(/x a b.a (a x))"
let s_ba = tree_of_string "(/x a b.b (a x))"
let s_baba = tree_of_string "(/x a b.b (a (b (a x))))"

(* let _all_a = tree_of_string "(/f x.(/c a b.c a b) ((/s.(s (/x y.x)) (/x./x y.y) (/x./x y.y)) x) (/x y.x) ((/x y.x y x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.y)) x) (f ((/s.(/p.p (/x y.y)) (s ((/a b c.c a b) (/x a b.x) (/x a b.x)) (/x.(/a b c.c a b) ((/s x a b.a (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)) (/x.(/a b c.c a b) ((/s x a b.b (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)))) x))))"
let all_a = tree_of_string "(/a.(/f.(/x.f (x x)) (/x.f (x x))) (/f x.(/c a b.c a b) ((/s.(s (/x y.x)) (/x./x y.y) (/x./x y.y)) x) (/x y.x) ((/x y.x y x) ((/s.s (/x y.y) (/x./x y.x) (/x./x y.y)) x) (f ((/s.(/p.p (/x y.y)) (s ((/a b c.c a b) (/x a b.x) (/x a b.x)) (/x.(/a b c.c a b) ((/s x a b.a (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)) (/x.(/a b c.c a b) ((/s x a b.b (s x a b)) ((/p.p (/x y.x)) x)) ((/p.p (/x y.x)) x)))) x)))) a)"

let tmp = Core.AApp(all_a, s_baba, UnT)
let res_all_a = Core.nf (Core.casubst) tmp



131
132

let tmp = Core.AApp(Core.AApp(eq_str, s_baba, UnT), s_baba, UnT)
Frontull Samuel's avatar
Frontull Samuel committed
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
let res_eq_str = Core.nf (Core.casubst) tmp *)


(* PCP2 *)
(* let s_t1_2 = "(/x a b.a x)"
let s_b1_2 = "(/x a b.a (b (b x)))"

let s_t2_2 = "(/x a b.b x)"
let s_b2_2 = "(/x a b.x)" *)

(* let s_t1_2 = "(/x a b.a x)"
let s_b1_2 = "(/x a b.a (b x))"

let s_t2_2 = "(/x a b.b x)"
let s_b2_2 = "(/x a b.x)"

let _pcp2 = "(/f a b." ^ s_ite ^ " (" ^ s_or ^ " (" ^ s_eq_str ^ " (" ^ s_concat ^ " a " ^ s_t1_2 ^ ") (" ^ s_concat ^ " b " ^ s_b1_2 ^ ")) (" ^ s_eq_str ^ " (" ^ s_concat ^ " a " ^ s_t2_2 ^ ") (" ^ s_concat ^ " b " ^ s_b2_2 ^ "))) " ^ s_true ^ " (" ^ s_or ^ " (f (" ^ s_concat ^ " a " ^ s_t1_2 ^ ") (" ^ s_concat ^ " b " ^ s_b1_2 ^ ")) (f (" ^ s_concat ^ " a " ^ s_t2_2 ^ ") (" ^ s_concat ^ " b " ^ s_b2_2 ^ "))))"

let s_pcp2 = s_ycomb ^ " " ^ _pcp2 ^ " " ^ s_empty ^ " " ^ s_empty
let pcp2 = tree_of_string s_pcp2
let res_pcp2 = Core.nf (Core.casubst) pcp2 *)

(* PCP3 *)
(* let s_t1 = "(/x a b.b x)"
let s_b1 = "(/x a b.b (b (b x)))"

let s_t2 = "(/x a b.b (a (b (b (b x)))))"
let s_b2 = "(/x a b.b (a x))"

let s_t3 = "(/x a b.b (a x))"
let s_b3 = "(/x a b.a x)"

let _pcp3 = "/f a b." ^ s_ite ^ " (" ^ s_or ^ " (" ^ s_or ^ " (" ^ s_eq_str ^ " (" ^ s_concat ^ " a " ^ s_t1 ^ ") (" ^ s_concat ^ " b " ^ s_b1 ^ ")) (" ^ s_eq_str ^ " (" ^ s_concat ^ " a " ^ s_t2 ^ ") (" ^ s_concat ^ " b " ^ s_b2 ^ ")))) (" ^ s_eq_str ^ " (" ^ s_concat ^ " a " ^ s_t3 ^ ") (" ^ s_concat ^ " b " ^ s_b3 ^ ")) " ^ s_true ^ " (" ^ s_or ^ " (" ^ s_or ^ " (f (" ^ s_concat ^ " a " ^ s_t1 ^ ") (" ^ s_concat ^ " b " ^ s_b1 ^ ")) (f (" ^ s_concat ^ " a " ^ s_t2 ^ ") (" ^ s_concat ^ " b " ^ s_b2 ^ "))) (f (" ^ s_concat ^ " a " ^ s_t3 ^ ") (" ^ s_concat ^ " b " ^ s_b3 ^ ")))"
166

Frontull Samuel's avatar
Frontull Samuel committed
167
168
let s_pcp3 = s_ycomb ^ " " ^ _pcp3 ^ " " ^ s_empty ^ " " ^ s_empty
let pcp3 = tree_of_string s_pcp3
169

Frontull Samuel's avatar
Frontull Samuel committed
170
let res_pcp3 = Core.nf (Core.casubst) pcp3 *)
171

Frontull Samuel's avatar
Frontull Samuel committed
172
173
174
175
176
177
178
179
let () =
  Printf.printf "PCP file.\n";
  (* Printf.printf "%s\n\n" s_pcp; *)
  Printf.printf "res_tmp: %s\n" (Core.a_term_to_string (Core.nf Core.casubst tmp));
  (* Printf.printf "res_all_a: %s\n" (Core.a_term_to_string res_all_a);
  Printf.printf "res_eq_str: %s\n" (Core.a_term_to_string res_eq_str);
  (* Printf.printf "pcp2: %s\n" (s_pcp2); *)
  Printf.printf "res_pcp2: %s\n" (Core.a_term_to_string res_pcp2); *)