malli

https://github.com/metosin/malli :malli:
ikitommi 2021-02-21T15:26:28.001200Z

added more info on function schema explain:

(def MyFunctionSchema
  (m/schema
    [:function
     [:=> [:cat :int] :int]
     [:=> [:cat :int :int [:* :int]] :int]]
    {::m/function-checker mg/function-checker}))

(m/explain
  MyFunctionSchema
  (fn
    ([x] x)
    ([x y & zs] (apply max x y zs))))
; => nil

(m/explain
  MyFunctionSchema
  (fn
    ([x] x)
    ([x y & zs] (str (apply max x y zs)))))
;{:schema [:function 
;          [:=> [:cat :int] :int] 
;          [:=> [:cat :int :int [:* :int]] :int]],
; :value #object[],
; :errors (#Error{:path [],
;                 :in [],
;                 :schema [:function 
;                          [:=> [:cat :int] :int] 
;                          [:=> [:cat :int :int [:* :int]] :int]],
;                 :value #object[],
;                 :check ({:total-nodes-visited 2,
;                          :depth 1,
;                          :pass? false,
;                          :result false,
;                          :result-data nil,
;                          :time-shrinking-ms 0,
;                          :smallest [(0 0)],
;                          :malli.generator/explain-output {:schema :int,
;                                                           :value "0",
;                                                           :errors (#Error{:path []
;                                                                           :in []
;                                                                           :schema :int
;                                                                           :value "0"})}})})}

đź‘Ť 1
ikitommi 2021-02-21T15:29:16.003100Z

really happy how good the function schemas are now, but still - it’s just elegant (and slow) way to verify things at runtime, e.g. not a type system.

ikitommi 2021-02-21T15:32:52.003600Z

in master

borkdude 2021-02-21T15:33:38.003800Z

compared to clojure.spec, how slow?

ikitommi 2021-02-21T15:44:13.006800Z

the sequence schemas are at least an order of magnitude faster than spec in initial tests. Full function verification uses test.check, I think that dominates, and quess they are about the same.

ikitommi 2021-02-21T15:47:09.010300Z

but, having arity info available for fns at runtime (in clojure.core) would help to see that fns are correct on arity, without testing it and getting arityexceptions - costs a lot.

borkdude 2021-02-21T16:13:04.011Z

Maybe you can combine that with static analysis (e.g. from clj-kondo)?

ikitommi 2021-02-21T18:09:50.013900Z

I guess it could be done using a clj-kondo hook? with defaults, the test.check runs 19ms for a function on my machine. that’s 500 funcs per sec (on 1 core?). errors are faster, the given example fails fast on invalid return, under 1ms.

ikitommi 2021-02-21T18:10:42.014700Z

i believe it could be made faster, less iterations on arguments etc.

ikitommi 2021-02-21T18:11:27.015200Z

[metosin/malli "0.3.0-SNAPSHOT"] has the stuff in.

🎉 2
ikitommi 2021-02-21T19:59:03.018800Z

with 10 iterations, it drops to 0.3ms per fn.'

borkdude 2021-02-21T20:03:27.018900Z

I mean: > but, having arity info available for fns at runtime (in clojure.core) would help to see that fns are correct on arity, without testing it and getting arityexceptions - costs a lot. You could use clj-kondo to find the correct arities perhaps

emccue 2021-02-21T20:42:32.019300Z

> e.g. not a type system.

emccue 2021-02-21T20:42:45.019700Z

i mean

emccue 2021-02-21T20:43:02.020200Z

for me, it would serve much the same purpose i'd think

emccue 2021-02-21T20:43:39.021100Z

or rather - the same workflow

emccue 2021-02-21T20:43:49.021400Z

maybe i'm missing a worldview though

borkdude 2021-02-21T21:07:20.021700Z

Btw, this sounds interesting, but I haven't heard anything about it yet: https://www.fulcrologic.com/copilot