I've been working on a similar idea for replacing types. If you can specify how to take apart a type, name the parts and put them back together you can then get validators, pattern matching, recursive-descent parsing with backtracking, generic traversals, lenses and generators more or less for free. By using simple data-structures to describe the types and compiling them to clojure at the call site you can have first-class types without being penalised in performance.
It's still work in progress but there are working examples as of https://github.com/jamii/strucjure/commit/e0e56a25c1b880c382...
(using strucjure.sugar
;; define a pattern
(def peano-pattern
(graph num ~(or ~succ ~zero)
succ (succ ~num)
zero zero))
(comment ;; desugars to
{'num (->Or (->Node 'succ) (->Node 'zero))
'succ (list 'succ (->Node 'num))
'zero 'zero})
;; define a view over that pattern
(def peano->int
(view peano-graph {'succ (fnk [num] (inc num))
'zero (fnk [] 0)}))
(peano->int 'zero) ;; => 0
(peano->int '(succ (succ zero))) ;; => 2
(peano->int '(succ (succ succ))) ;; => throws MatchFailure
)
It's similar to the old ideas at http://scattered-thoughts.net/blog/2012/12/04/strucjure-moti... but significantly simpler. I'm hoping to be able to release at least the core functionality in a few weeks.