Repository navigation
Expand file tree
/
Copy pathcandidateTargetCheck.ts
More file actions
54 lines (52 loc) · 2.61 KB
/
Copy pathcandidateTargetCheck.ts
File metadata and controls
54 lines (52 loc) · 2.61 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
import { leanDeclarationContextCommands } from '../../src/math/leanSource';
/** Runs in a separate host-written module after the candidate compiles. Other original goals may remain open;
* the selected declaration must have a value, the requested type and no admitted or additional axioms. */
export function candidateTargetQuery(
sourceModule: string,
source: string,
target: { name: string; expectedType?: string },
marker: string,
): string {
const context = leanDeclarationContextCommands(source, target.name);
return [
'import Lean',
`import ${sourceModule}`,
...(target.expectedType
? [
...context.before,
'run_cmd Lean.Elab.Command.liftTermElabM do',
` let parsedType ← match Lean.Parser.runParserCategory (← Lean.getEnv) (Lean.Name.mkSimple "term") ${JSON.stringify(target.expectedType)} with`,
' | .ok stx => pure stx',
' | .error message => throwError "{message}"',
' let expected ← Lean.Elab.Term.elabType parsedType',
' Lean.Elab.Term.synthesizeSyntheticMVarsNoPostponing',
' let expected ← Lean.instantiateMVars expected',
' if expected.hasMVar || expected.hasFVar then throwError "Candidate expected type must be closed"',
` let declaration ← Lean.Meta.mkConstWithFreshMVarLevels (${JSON.stringify(target.name)}.toName)`,
' let actual ← Lean.Meta.inferType declaration',
' unless (← Lean.Meta.isDefEq actual expected) do throwError "Candidate declaration has a different type or additional assumptions"',
...context.after,
]
: []),
'run_cmd do',
` let target := ${JSON.stringify(target.name)}.toName`,
' let some info := (← Lean.getEnv).find? target | throwError "Candidate target is missing"',
' unless info.value? (allowOpaque := true) |>.isSome do throwError "Candidate target has no proof value"',
' let axioms ← Lean.Elab.Command.liftCoreM <| Lean.collectAxioms target',
' let permitted : List String := ["propext", "Classical.choice", "Quot.sound"]',
' for dependency in axioms do',
' unless permitted.contains dependency.toString do throwError "Candidate target depends on an unproved declaration or unsupported axiom: {dependency}"',
` Lean.logInfo ${JSON.stringify(marker)}`,
'',
].join('\n');
}
export function candidateTargetWasChecked(output: string, marker: string): boolean {
return output.split('\n').some(line => {
try {
const item = JSON.parse(line);
return item.severity === 'information' && item.data === marker;
} catch {
return false;
}
});
}