Skip to content

Instantly share code, notes, and snippets.

@erp12
Created November 16, 2021 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erp12/5f7a46ff4a960feeed155da6cf89b0db to your computer and use it in GitHub Desktop.
Save erp12/5f7a46ff4a960feeed155da6cf89b0db to your computer and use it in GitHub Desktop.
Failing Recursive Malli Schema
(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