-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
268 lines (257 loc) · 6.6 KB
/
query.go
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"container/heap"
"flag"
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"log"
"os"
"runtime/pprof"
"strings"
)
const DEBUG = false
// Parses a ghi and emits the label of each stmt on out, followed by an empty
// sentinel.
func parseInterface(fn string, out chan *Entry) {
file, err := os.Open(fn)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't open interface %s: %v\n", fn, err)
os.Exit(-1)
}
scanner := NewScanner(file)
for scanner.Scan() {
_ = scanner.Text()
e := scanner.Entry()
if DEBUG {
fmt.Fprintf(os.Stderr, "Axiom: %s\n%s\n%s\n",
e.Fact.Skin.Name, e.Key, e.Fact)
}
out <- e
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Scanner error: %v", err)
panic(err)
}
out <- nil
}
func parseInterfaces(interfaces []string) map[string]*Entry {
out := make(map[string]*Entry)
ch := make(chan *Entry)
jobs := 0
for _, interfaceFn := range interfaces {
if len(interfaceFn) > 0 {
go parseInterface(interfaceFn, ch)
jobs++
}
}
for jobs > 0 {
axiom := <-ch
if axiom != nil {
out[axiom.MarkStr()] = axiom
} else {
jobs--
}
}
return out
}
// Given a depmap of entries, return a compact prooflist with duplicates removed
// (each duplicated entry only keeps the last copy); also returns the number of
// ungrounded stmts in the output. The given entry st will be prepended.
func compactify(st *Entry, groundSet map[string]*Entry,
depMap map[string][]*Entry) (out []*Entry, stmts int) {
stmts = 0
out = make([]*Entry, 0)
seen := make(map[string]bool)
for _, es := range depMap {
for i := range es {
e := es[len(es)-i-1]
if !seen[e.Key] {
seen[e.Key] = true
out = append(out, e)
if (len(e.Fact.Deps()) == 0) &&
(groundSet[e.Fact.Skin.Name] != nil) {
stmts++
}
}
}
}
out = append(out, st)
// Reverse out
for i, j := 0, len(out)-1; i < j; i, j = i+1, j-1 {
out[i], out[j] = out[j], out[i]
}
return out, stmts
}
func churn(db *leveldb.DB, groundMeat map[string][]*Entry,
drafts *DraftHeap, maxLaps int) *Draft {
//draftsSeen := make(map[string]bool)
laps := 0
for len(*drafts) > 0 {
draft := heap.Pop(drafts).(*Draft)
mark, ok := draft.TopNeed()
if !ok {
panic("No need!?")
}
bone := mark.BoneKey()
needers := make([]*Draft, 1) // TODO: scan for other drafts needing this
needers[0] = draft
if DEBUG {
fmt.Fprintf(os.Stderr, "%s (%f) needs %v\n", draft, draft.Score,
mark.String())
}
// First check axioms in the groundSet. Use only ones which match fully,
// to avoid the need to rewrite our imports.
for _, v := range groundMeat[mark.String()] {
//TODO: need to disallow conflating ground terms
//TODO: also need to disallow re-defthm-ing them
if DEBUG {
fmt.Fprintf(os.Stderr, "Ground Using %s = %s ",
v.MarkStr()[len(bone):], v.Fact.Skin.Name)
}
newDraft := draft.AddEntry(mark, v)
if newDraft != nil {
heap.Push(drafts, newDraft)
if DEBUG {
fmt.Fprintf(os.Stderr, "==> %f\n", newDraft.Score)
}
if _, ok := newDraft.TopNeed(); !ok {
return newDraft
}
} else {
if DEBUG {
fmt.Fprintf(os.Stderr, "==> Nil!\n")
}
}
}
// Then check proved theorems
resolved := make(chan *Entry, 100)
GetFactsByPrefix(db, bone, resolved)
for e := range resolved {
if len(e.Fact.Deps()) > 0 {
if DEBUG {
fmt.Fprintf(os.Stderr, "Using %s = %s ",
e.MarkStr()[len(bone):], e.Fact.Skin.Name)
}
for _, d := range needers {
newDraft := d.AddEntry(mark, e)
if newDraft != nil {
if _, ok := newDraft.TopNeed(); !ok {
return newDraft
}
//hash := newDraft.Hash()
if true { //!draftsSeen[hash] {
//draftsSeen[hash] = true
if DEBUG {
fmt.Fprintf(os.Stderr, "==> %f\n",
newDraft.Score)
}
heap.Push(drafts, newDraft)
} else {
// TODO: this never seems to happen in practice...
fmt.Fprintf(os.Stderr, "==> Already Seen!\n")
}
} else {
if DEBUG {
fmt.Fprintf(os.Stderr, "==> Nil!\n")
}
}
}
}
}
laps += 1
if laps > maxLaps {
fmt.Fprintf(os.Stderr, "Too many laps, giving up!")
return nil
}
if laps%100 == 0 {
fmt.Fprintf(os.Stderr, "Laps: %d, Drafts: %d\n", laps, drafts.Len())
}
}
fmt.Fprintf(os.Stderr, "Out of drafts!")
return nil
}
func main() {
dbPath := flag.String("d", "facts.leveldb", "path to facts.leveldb")
markQuery := flag.String("m", "", "for raw mark-to-json query")
imports := flag.String("i", "", "comma-separated list of .ghi imports")
exports := flag.String("e", "", "comma-separated list of .ghi exports")
prof := flag.Bool("prof", true, "do profiling?")
laps := flag.Int("l", 8000, "Maximum iterations")
flag.Parse()
opt := opt.Options{ErrorIfMissing: true}
db, err := leveldb.OpenFile(*dbPath, &opt)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(-1)
}
defer db.Close()
if len(*markQuery) > 0 {
results := make(chan *Entry)
go GetFactsByPrefix(db, *markQuery, results)
fmt.Print("{\n")
for e := range results {
fmt.Printf(" %s: %s\n", jsonize(e.Key), e.Json)
}
fmt.Print("}\n")
return
}
importList := strings.Split(*imports, ",")
exportList := strings.Split(*exports, ",")
groundSet := parseInterfaces(importList)
targets := parseInterfaces(exportList)
groundMeat := make(map[string][]*Entry)
for _, e := range groundSet {
markStr := e.MarkStr()
arr := groundMeat[markStr]
arr = append(arr, e)
groundMeat[markStr] = arr
}
draft := new(Draft)
for _, e := range targets {
targetMark := e.Mark()
if DEBUG {
fmt.Fprintf(os.Stderr, "Target: %s\n%s\n%s\n%v\n",
e.Fact.Skin.Name, e.Key, e.Fact, e.Mark())
}
draft = draft.AddTarget(targetMark)
}
drafts := new(DraftHeap)
heap.Init(drafts)
heap.Push(drafts, draft)
var cpu, heap *os.File
if *prof {
cpu, err = os.Create("pprof-cpu.txt")
if err != nil {
log.Fatal("Couldn't create pprof.txt! ", err)
}
heap, err = os.Create("pprof-heap.txt")
if err != nil {
log.Fatal("Couldn't create pprof.txt! ", err)
}
pprof.StartCPUProfile(cpu)
}
winner := churn(db, groundMeat, drafts, *laps)
if *prof {
pprof.StopCPUProfile()
pprof.WriteHeapProfile(heap)
cpu.Close()
heap.Close()
}
fmt.Fprintf(os.Stderr, "\nResult: %s\n", winner)
if winner == nil {
os.Exit(-1)
}
for _, imp := range importList {
fmt.Printf("import (%s %s () \"\")\n", imp, imp)
}
_, err = WriteProofs(os.Stdout, winner.Flatten(), targets, winner.Bind)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(-1)
}
for _, exp := range exportList {
fmt.Printf("export (%s %s () \"\")\n", exp, exp)
}
os.Exit(0)
}