(module term (data Term (K) (S) (I) (Iota) (App Term Term)) (data Pattern (PAtom Term) (PVar Text) (PApp Pattern Pattern)) (data Rule (Rule Text Pattern Pattern)) (data Binding (Binding Text Term)) (data NormalVerdict (NfNormal) (NfFuel)) (data Step (Step Term Term)) (data Normalization (Normalization Term (List Step) NormalVerdict)) (data Relation (Eq) (Neq)) (data Verdict (Equal) (Distinct) (Fuel)) (data Cone (Cone Term Term (List Step))) (data Certificate (Certificate Relation Cone Cone Verdict)) (data Replay (Replayed Term) (TraceFailed Int)) (data ConeResult (ConeNormal Term) (ConeRejected Check)) (data Check (CheckAccepted Verdict) (CheckTrace Text Int) (CheckVerdict Verdict Verdict) (CheckRelation Relation Verdict)) (defn lookup-binding ((name Text) (bindings (List Binding))) (Option Term) (match bindings ((nil) (none)) ((cons (Binding bound value) rest) (if (= name bound) (some value) (lookup-binding name rest))))) (defn extend-bindings ((left (List Binding)) (right (List Binding))) (Option (List Binding)) (match left ((nil) (some right)) ((cons (Binding name value) rest) (match (lookup-binding name right) ((none) (match (extend-bindings rest right) ((none) (none)) ((some merged) (some (cons (Binding name value) merged))))) ((some other) (if (= value other) (extend-bindings rest right) (none))))))) (defn match-pattern ((pattern Pattern) (term Term)) (Option (List Binding)) (match pattern ((PAtom atom) (if (= atom term) (some (nil)) (none))) ((PVar name) (some (cons (Binding name term) (nil)))) ((PApp function argument) (match term ((App term-function term-argument) (match (match-pattern function term-function) ((none) (none)) ((some left-bindings) (match (match-pattern argument term-argument) ((none) (none)) ((some right-bindings) (extend-bindings left-bindings right-bindings)))))) (_ (none)))))) (defn instantiate ((pattern Pattern) (bindings (List Binding))) (Option Term) (match pattern ((PAtom atom) (some atom)) ((PVar name) (lookup-binding name bindings)) ((PApp function argument) (match (instantiate function bindings) ((none) (none)) ((some term-function) (match (instantiate argument bindings) ((none) (none)) ((some term-argument) (some (App term-function term-argument))))))))) (defn match-rule ((rule Rule) (term Term)) (Option Term) (match rule ((Rule _ pattern replacement) (match (match-pattern pattern term) ((none) (none)) ((some bindings) (instantiate replacement bindings)))))) (defn rewrite-rules ((rules (List Rule)) (term Term)) (Option Term) (match rules ((nil) (none)) ((cons rule rest) (match (match-rule rule term) ((none) (rewrite-rules rest term)) ((some next) (some next)))))) (defn rewrite-once ((rules (List Rule)) (term Term)) (Option Term) (match (rewrite-rules rules term) ((some next) (some next)) ((none) (match term ((App function argument) (match (rewrite-once rules function) ((some next-function) (some (App next-function argument))) ((none) (match (rewrite-once rules argument) ((some next-argument) (some (App function next-argument))) ((none) (none)))))) (_ (none)))))) (defn normalize-loop ((rules (List Rule)) (term Term) (fuel Int) (steps (List Step))) Normalization (if (<= fuel 0) (match (rewrite-once rules term) ((none) (Normalization term (reverse steps) (NfNormal))) ((some _) (Normalization term (reverse steps) (NfFuel)))) (match (rewrite-once rules term) ((none) (Normalization term (reverse steps) (NfNormal))) ((some next) (normalize-loop rules next (- fuel 1) (cons (Step term next) steps)))))) (defn normalize ((rules (List Rule)) (term Term) (fuel Int)) Normalization (normalize-loop rules term fuel (nil))) (defn normalization-term ((normalization Normalization)) Term (match normalization ((Normalization term _ _) term))) (defn normalization-steps ((normalization Normalization)) (List Step) (match normalization ((Normalization _ steps _) steps))) (defn normalization-verdict ((normalization Normalization)) NormalVerdict (match normalization ((Normalization _ _ verdict) verdict))) (defn cone-of ((term Term) (normalization Normalization)) Cone (Cone term (normalization-term normalization) (normalization-steps normalization))) (defn replay-steps ((rules (List Rule)) (current Term) (steps (List Step)) (index Int)) Replay (match steps ((nil) (Replayed current)) ((cons (Step before after) rest) (if (= before current) (match (rewrite-once rules current) ((none) (TraceFailed index)) ((some next) (if (= next after) (replay-steps rules after rest (+ index 1)) (TraceFailed index)))) (TraceFailed index))))) (defn check-cone ((rules (List Rule)) (cone Cone) (side Text)) ConeResult (match cone ((Cone term normal steps) (match (replay-steps rules term steps 0) ((TraceFailed index) (ConeRejected (CheckTrace side index))) ((Replayed end) (if (= end normal) (match (rewrite-once rules end) ((none) (ConeNormal end)) ((some _) (ConeRejected (CheckTrace side (length steps))))) (ConeRejected (CheckTrace side (length steps))))))))) (defn term-verdict ((left Term) (right Term)) Verdict (if (= left right) (Equal) (Distinct))) (defn relation-accepts ((relation Relation) (verdict Verdict)) Bool (match relation ((Eq) (match verdict ((Equal) true) (_ false))) ((Neq) (match verdict ((Distinct) true) (_ false))))) (defn check-certificate ((rules (List Rule)) (certificate Certificate)) Check (match certificate ((Certificate relation left right recorded) (match (check-cone rules left "left") ((ConeRejected check) check) ((ConeNormal left-normal) (match (check-cone rules right "right") ((ConeRejected check) check) ((ConeNormal right-normal) (let ((computed (term-verdict left-normal right-normal))) (if (= recorded computed) (if (relation-accepts relation computed) (CheckAccepted computed) (CheckRelation relation computed)) (CheckVerdict recorded computed)))))))))) (defn prove ((rules (List Rule)) (relation Relation) (left Term) (right Term) (fuel Int)) Certificate (let ((left-normalization (normalize rules left fuel)) (right-normalization (normalize rules right fuel))) (let ((left-verdict (normalization-verdict left-normalization)) (right-verdict (normalization-verdict right-normalization))) (Certificate relation (cone-of left left-normalization) (cone-of right right-normalization) (if (= left-verdict (NfFuel)) (Fuel) (if (= right-verdict (NfFuel)) (Fuel) (term-verdict (normalization-term left-normalization) (normalization-term right-normalization)))))))))