Hi there,
I have been playing a little bit with clojure.spec and I find it a very
nice addition to the core language.
One thing I am particularly interested in is the spec of recursive
structures, but I noticed there
is no dedicated support for these (especially mutually recursive
structures).
As a toy example, I have encoded a spec for constructive even/odd numbers.
;; ==============
(:require [clojure.spec :as s]))
(s/def ::zero #{:zero})
(s/def ::one #{:one})
(s/def ::succ #{:succ})
(s/def ::odd nil)
(s/def ::even (s/or :zero ::zero ; 0 is even
:even (s/tuple ::succ ::odd))) ; n+1 is even if
n is odd
(s/def ::odd (s/tuple ::succ ::even)) ; n+1 is odd if n is even
;; ==================
First question : is there a better way to define a set of mutually
recursive specs ?
This seems to work anyways :
;; ===================
(s/conform ::even :zero) ; zero is even
;; => [:zero :zero]
(s/conform ::even [:succ [:succ :zero]]) ; 2 is even
;; => [:even [:succ [:succ [:zero :zero]]]]
(s/exercise ::even)
;; => generates 10 even numbers (nice !)
(s/conform ::odd [:succ :zero]) ; 1 is odd
;; => [:succ [:zero :zero]]
(s/conform ::odd [:succ [:succ :zero]]) ; 2 is not odd
;; => :clojure.spec/invalid
;; ======================
My main concern is when asking for some explanations for invalid data.
;; ========================================
(s/explain ::odd [:succ [:succ :zero]])
;; In: [1] val: [:succ :zero] fails spec: :typetheory.syntax/zero at: [1
:zero] predicate: #{:zero}
;; In: [1 1] val: :zero fails spec: :typetheory.syntax/odd at: [1 :even 1]
predicate: vector?
;; => nil
;; ========================================
I think the explanation will be of a size as long as the length of the
input data,
and moreover it is not easy to understand what is the problem...
so, my second question : is there a way to get a better explanation ?
Cheers,
Frederic.
--
Frederic Peschanski
UPMC - LIP6 - Complex. - APR
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.