import Counter from "../Counter"; import ExecutionContext from "../ExecutionContext"; import { ProofType } from "../ProofType"; import StackTrace from "../StackTrace"; import Expr0 from "./Expr0"; import Fun from "./Fun"; import Node from "./Node"; import { Type } from "./types"; import Variable from "./Variable"; /** * 숫자가 큰 것이 우선순위가 높다. */ export enum EqualsPriority { /** Variable (primitive) */ ZERO, /** Fun */ ONE, /** Tee */ TWO, /** Funcall */ THREE, /** Variable (macro) */ FOUR, /** $Variable, Reduction */ FIVE } export default abstract class Metaexpr extends Node { public readonly type: Type; private expandMetaCache: Metaexpr; constructor (doc: string, tex: string, type: Type, trace: StackTrace) { super(doc, tex, trace); if (!type) throw Node.error('Assertion failed', trace); this.type = type; } public abstract substitute(map: Map): Metaexpr; /** * * @param andFuncalls 이름 없는 Funcall도 푼다. */ public expandMeta(andFuncalls: boolean): Metaexpr { if (this.expandMetaCache) return this.expandMetaCache; return this.expandMetaCache = this.expandMetaInternal(andFuncalls); } protected abstract expandMetaInternal(andFuncalls: boolean): Metaexpr; /** * * @return 같지 않으면 `false`. 같으면 같음을 보이는 데 사용한 매크로들의 목록. */ public equals(obj: Metaexpr, context: ExecutionContext): (Fun | Variable)[] | false { // console.log(`${this}\n\n${obj}`); // var ret = (() => { if (this === obj) return []; if (!this.type.equals(obj.type)) return false; if (obj.getEqualsPriority(context) > this.getEqualsPriority(context)) return obj.equalsInternal(this, context); return this.equalsInternal(obj, context); // })(); // console.log(`${this}\n\n${obj}\n\nresult:`, ret); // return ret; } /** * * @return 같지 않으면 `false`. 같으면 같음을 보이는 데 사용한 매크로들의 목록. */ protected abstract equalsInternal(obj: Metaexpr, context: ExecutionContext): (Fun | Variable)[] | false; protected abstract getEqualsPriority(context: ExecutionContext): EqualsPriority; public isProved(hypotheses?: Metaexpr[]): boolean { hypotheses = hypotheses || []; for (var i = 0; i < hypotheses.length; i++) { if (hypotheses[i] == this) return true; } return this.isProvedInternal(hypotheses); } protected abstract isProvedInternal(hypotheses: Metaexpr[]): boolean; public getProof( hypnumMap: Map, $Map: Map, ctr: Counter, root: boolean=false): ProofType[] { if (hypnumMap.has(this)) { return [{ _type: 'R', ctr: ctr.next(), num: hypnumMap.get(this), expr: this }]; } if ($Map.has(this)) { return [{ _type: 'R', ctr: ctr.next(), num: $Map.get(this), expr: this }]; } return this.getProofInternal(hypnumMap, $Map, ctr, root); } protected abstract getProofInternal( hypnumMap: Map, $Map: Map, ctr: Counter, root?: boolean): ProofType[]; }