Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Very cool. Also check out the implementation of encapsulation in John Shutt's Kernel Lisp: http://web.cs.wpi.edu/~jshutt/kernel.html


Interesting. I've seen his fexpr work before on LTU but never really looked at it closely. I suppose the only way to go is to read the whole thesis?


Nice! Really looking forward to the release!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: