/failing_mutual_recursion.clj Secret
Created
November 16, 2021 17:31
Failing Recursive Malli Schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns failing-mutual-recursion | |
(:require [malli.core :as m] | |
[malli.util :as mu])) | |
;; Define some AST node schemas | |
;; Includes references to the generic AST schema. | |
(def Lit | |
[:cat [:= :lit] :any]) | |
(def Var | |
:symbol) | |
(def App | |
[:cat | |
[:= :apply] | |
[:ref ::AST] | |
[:* [:ref ::AST]]]) | |
;; Define the generic AST schema as a union of all possible node types. | |
(def AST | |
[:or | |
[:ref ::Lit] | |
[:ref ::Var] | |
[:ref ::App]]) | |
;; Create a custom registery with the new AST schemas | |
(def registry | |
(merge (m/default-schemas) | |
(mu/schemas) | |
{::Lit Lit | |
::Var Var | |
::Abs App | |
::AST AST})) | |
;; Try it out! | |
(m/validate ::Lit [:lit 1] {:registry registry}) | |
;; => true | |
;; So far, so good. | |
(m/validate ::App | |
[:apply 'inc [:lit 1]] | |
{:registry registry}) | |
;; clojure.lang.ExceptionInfo: :malli.core/potentially-recursive-seqex | |
;; [:ref :erp12.schema-inference.meta-schemas/AST] | |
;; {:type :malli.core/potentially-recursive-seqex, | |
;; :message :malli.core/potentially-recursive-seqex, | |
;; :data [:ref :erp12.schema-inference.meta-schemas/AST]} | |
;; The `App` schema _is_ mutually recursive with `AST` but all recursive references have | |
;; been wrapped in a `[:ref _]`. Why is the `App` schemas not usable? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment