Skip to content

Instantly share code, notes, and snippets.

View frenchy64's full-sized avatar

Ambrose Bonnaire-Sergeant frenchy64

  • Madison, Wisconsin
View GitHub Profile
@frenchy64
frenchy64 / hy.clj
Created September 19, 2013 23:30
hy.clj
(-> (ast (letfn [(a [])
(b [a] a)
(c [c] a)
(a [b c] b)]
(a b c)))
ast-hy emit-hy)
;=>
((fn* ([] (letfn* [a (fn* a4844 ([] nil))
b (fn* b4846 ([a4853] a4853))
c (fn* c4848 ([c4854] a4843))
clojure.core.typed=> (->> (check-ns-info) :delayed-errors first ex-data)
{:env {:source "clojure.core.typed", :column 1, :line 53, :ns {:name clojure.core.typed}, :locals {}}, :form (def -base-aliases (quote #{EmptySeqable Vec Int NonEmptyCount Seqable NonEmptyColl Option Set NonEmptySeqable SortedSet Coll NonEmptyLazySeq Atom1 EmptyCount Map Num Seq NonEmptyVec Id AnyInteger NonEmptySeq})), :type-error :clojure.core.typed.utils/tc-error-parent}
(defmacro if-every-let [bindings then else]
(let [[lhs rhs] bindings]
`(let [temp# ~rhs]
(if (not-any? nil? temp#)
(let [~lhs temp#]
~then)
~else))))
; (if-every-let [foo bar]
; then
(cf (defprotocol Foo (bar [this])))
=>
Type Error (clojure.core.typed.test.core:1:34) Found untyped var definition: clojure.core.typed.test.core/Foo
Hint: Add the annotation for clojure.core.typed.test.core/Foo via check-ns or cf
in: (def Foo {})
Type Error (clojure.core.typed.test.core:1:34) Unannotated var clojure.core/alter-meta!
Hint: Add the annotation for clojure.core/alter-meta! via check-ns or cf
in: clojure.core/alter-meta!
hugod
is there a description somewhere of why vars need explicit annotation?
ambrosebs_
hugod: well there's a few papers ;)
ambrosebs_
core.typed implements local type inference, which requires lots of help at the top level.
ambrosebs_
turns out it's powerful enough to infer locals, but not quite enough to infer local fn parameters. Definitely not powerful enough for top-levels.
ambrosebs_
also loop variables often need annotation.
@frenchy64
frenchy64 / fnil.clj
Created August 17, 2013 01:32
fnil
clojure.core/fnil (All [x y z a b ...]
(Fn [[x b ... b -> a] x -> [(U nil x) b ... b -> a]]
[[x y b ... b -> a] x y -> [(U nil x) (U nil y) b ... b -> a]]
[[x y z b ... b -> a] x y z -> [(U nil x) (U nil y) (U nil z) b ... b -> a]]))
(cljs.core.typed/cf
(defn ^{:ann '[(U nil (cljs.core/ISeqable Any)) Any -> int]}
index-of [xs x]
(let [len (count xs)]
(cljs.core.typed/loop> [i :- int, 0]
(if (< i len)
(if (= (nth xs i) x)
i
(recur (inc i)))
-1))))))
(ann ^:no-check add-datatype-ancestors [Symbol (t/Set r/TCType) -> nil])
(defn add-datatype-ancestors
"Add a set of ancestor overrides for the datatype named sym."
[sym tset]
(assert-dt-ancestors)
(when-let-fail [a *current-dt-ancestors*]
(swap! a update-in [sym] #(set/union (or % #{}) tset)))
nil)
(defn ^:no-check ^{:ann '[Symbol (t/Set r/TCType) -> nil]}
@frenchy64
frenchy64 / extenders.clj
Created August 12, 2013 09:38
extenders ignores datatypes
(defprotocol PMaths
(mult-by-two [this]))
(defrecord SpecialNumber [x]
PMaths
(mult-by-two [this] (assoc this :x (* (:x this) 2))))
(extend-protocol PMaths
nil
(mult-by-two [this])
(ns typed-example.core
(:import (clojure.lang Symbol Seqable))
(:require [clojure.core.typed :refer [ann check-ns defprotocol> ann-datatype ann-protocol inst]]))
(ann-protocol Similar
is-similar? [Similar Any -> Boolean])
(defprotocol> Similar
(is-similar? [this a]))
(ann-datatype MyInt [x :- Number])