-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_num.js
More file actions
150 lines (120 loc) · 3.44 KB
/
core_num.js
File metadata and controls
150 lines (120 loc) · 3.44 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
"use strict"
// using numbers instead of arrays as the "heart" of structures may make them faster, let's see how well this works
var generate = (type, propString, start, stop) =>
{
start = start || 0
stop = stop || Math.pow(type,Math.pow(type,2)) // defaults to highest possible value for type
let count = start
const result = []
const typeClass = [null, Magma1, Magma2, Magma3, Magma4] [type]
const props =
propString.split (" ").map (x => Property.fromString (x, type))
do
{
const magma = new typeClass (count)
let keep = true; let propCount = 0
do keep = props [propCount]
while (keep && propCount ++ < props.length)
keep && result.push(count)
}
while (++ count < stop)
return result
}
class Magma1
{
constructor (opNum) { this.num = opNum; this.width = 1 }
lookup () { return 0 }
}
class Magma2
{
constructor (opNum) { this.num = opNum; this.width = 2 }
lookup (x, y)
{ return (this.num / (1 << (x + y * y))) & 1 }
}
class Magma3
{
constructor (opNum) { this.num = opNum; this.width = 3 }
lookup (x, y)
{ return (this.num / Math.pow(3, x + y * y * y)) | 0 % 3 }
}
class Magma4
{
constructor (opNum) { this.num = opNum; this.width = 4 }
lookup (x, y)
{ return (this.num / (1 << ((x + (y << 2)) << 1))) & 3 }
}
class Term // in postfix notation: operators after operand. this makes parentheses and precedence rules unnessecary.
{
constructor (termArray = [])
{ this.a = termArray }
step (struc, stack = [], progress = 0)
{
if (this.a[progress] < 0) // negative numbers are operators
stack.push (struc.lookup (stack.pop, stack.pop))
else stack.push (this.a[progress])
return stack
}
evaluate (struc)
{
let progress = 0; let stack = []
while (progress < this.a.length)
stack = this.step (struc, stack, progress ++)
return stack
}
isEqual (struc)
{
let stack = this.evaluate(struc)
let result = true
for (let element of stack)
result = result && stack[0] == element
return result
}
}
const lower = "abcdefghijklmnopqrstuvwxyz"
const upper = lower.toUpperCase()
const opSymbols = "*+/-&|%$"
class Property // algebraic property, defined by quantified equalities
{
constructor (propertyArray, quantors)
{ this.a = propertyArray; this.quantors = quantors }
static fromString (propString)
{
// lower case for existance claims, upper case for universal claims. variables are evaluated in alphabeitical order
let quantCount = 0; let quantors = []
do
{
if (propString.includes (lower[quantCount]))
quantors.push (false)
else if (propString.includes (upper[quantCount]))
quantors.push (true)
else break
}
while (quantCount ++ <= lower.length)
let propArray = propString.toLowerCase().split("")
for (let opSymbol of opSymbols)
while (propArray.includes (opSymbol))
propArray [propArray.indexOf(opSymbol)] = - 1 - opSymbols.indexOf(opSymbol)
return new Property (propArray, quantors)
}
evaluate (struc, width)
{
const evaluateRecursive = (symbolList) =>
{
let possibility = 0; let keep = true
do
{
const newSymbolList = [...symbolList, ...possibility]
if (newSymbolList.length >= this.a.length)
{
const currentTerm = this.a.map
(symbol => newSymbolList[lower.indexOf (symbol)])
keep = new Term (currentTerm).isEqual(struc)
}
else keep = evaluateRecursive (newSymbolList)
}
while (keep == this.quantors [symbolList.length] && possibility ++ < struc.width)
return keep
}
return evaluateRecursive([])
}
}