Repository navigation
Expand file tree
/
Copy pathlogic.js
More file actions
189 lines (169 loc) · 4.13 KB
/
Copy pathlogic.js
File metadata and controls
189 lines (169 loc) · 4.13 KB
1
2
3
4
5
6
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
131
132
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var std_vars = {S:true};
function free_variables(ast, bound, free)
{
if (ast.type == 'name')
{
if (!bound[ast.name])
free[ast.name] = true;
}
else if (ast.type == 'number' || ast.type == 'quoted')
{
/* nothing to do */
}
else if (ast.type == 'tree')
{
free_variables(ast.lhs, bound, free);
free_variables(ast.rhs, bound, free);
}
else
throw "Unrecognized AST node: " + ast.type;
}
function subst(ast, name, value)
{
if (ast.type == 'name')
{
if (ast.name == name)
return value;
else
return ast;
}
else if (ast.type == 'number' || ast.type == 'quoted')
{
return ast;
}
else if (ast.type == 'tree')
{
return Tree(
subst(ast.lhs, name, value),
ast.opindex,
subst(ast.rhs, name, value)
);
}
else
throw "Unrecognized AST node: " + ast.type;
}
function list_free_variables(ast, fixed_vars)
{
var free = {};
var bound = Object.create(std_vars);
for (var v in fixed_vars)
{
bound[v] = true;
}
free_variables(ast, bound, free);
var result = Object.keys(free);
result.sort();
return result;
}
function all_variables(ast, vars)
{
if (ast.type == 'name')
vars[ast.name] = true;
else if (ast.type == 'number' || ast.type == 'quoted')
{
/* nothing to do */
}
else if (ast.type == 'tree')
{
all_variables(ast.lhs, vars);
all_variables(ast.rhs, vars);
}
else
throw "Unrecognized AST node: " + ast.type;
}
function fresh_variable(ast, bound)
{
var vars = Object.create(bound);
all_variables(ast, vars);
var suggestions = ['x','y','z','a','b','c'];
for (var i = 0; i < suggestions.length; i++)
{
var x = suggestions[i];
if (!vars[x]) return x;
}
throw "Can't think of a variable name";
}
function Container(vars, premises, goal)
{
this.vars = vars;
this.premises = premises;
this.goal = goal;
this.toString = function() {
var result = goal;
for (var i = 0; i < premises.length; i++)
{
result = premises[i] + ' -> ' + result;
}
return result;
}
}
function isTransitive(op)
{
return op == '=' || op == '<' || op == '<=' || op == '>' || op == '>=';
}
function isReflexive(op)
{
return op == '=' || op == '<=' || op == '>=';
}
function suggestRules(goal, fixed_vars)
{
var result = [];
try
{
var ast = parser(goal);
} catch (e) {
return [];
}
var free_vars = list_free_variables(ast, fixed_vars);
if (free_vars.length == 1)
result.push('induction');
if (ast.type == 'tree' && isTransitive(ast.op))
result.push('transitive');
if (ast.type == 'tree' && isReflexive(ast.op))
result.push('reflexive');
if (ast.type == 'tree' && ast.lhs.type == 'tree' && ast.lhs.op == '+' && ast.lhs.lhs.type == 'number' && ast.lhs.lhs.number == 0)
result.push('0+');
return result;
}
function deepEqual(lhs, rhs)
{
if (lhs.type != rhs.type) return false;
if (lhs.type == 'name')
return lhs.name == rhs.name;
else if (lhs.type == 'number')
return lhs.number == rhs.number;
else if (lhs.type == 'quoted')
return lhs.quoted == rhs.quoted;
else if (lhs.type == 'tree')
return lhs.op == rhs.op && deepEqual(lhs.lhs, rhs.lhs) && deepEqual(lhs.rhs, rhs.rhs);
else
throw "Unrecognized AST node: " + lhs.type;
}
function applyRuleBackwards(rule, goal, fixed_vars)
{
var ast = parser(goal);
if (rule == 'induction')
{
var free_vars = list_free_variables(ast, fixed_vars);
if (free_vars.length != 1) throw "Need exactly one free variable to apply induction. Got: " + free_vars;
var ind_var = free_vars[0];
var n = Name(fresh_variable(ast, fixed_vars));
var Sn = binop('@', new Name('S'), n);
var ast_0 = subst(ast, ind_var, Num(0));
var ast_n = subst(ast, ind_var, n);
var ast_S = subst(ast, ind_var, Sn);
var ast_iH = binop('->', ast_n, ast_S);
return [ast_str(ast_0), new Container([n.name], [ast_str(ast_n)], ast_str(ast_S))];
}
else if (rule == 'reflexive')
{
if (ast.type == 'tree' && isReflexive(ast.op) && deepEqual(ast.lhs, ast.rhs))
return [];
else
throw 'LHS and RHS not identical';
}
else
{
throw "Don't know how to apply rule: " + rule;
}
}