6. Theorem
This function never returns because if it did, it would never have compiled
(ann irrelevant (All [a b] [a -> b]))
(defn irrelevant [a]
(irrelevant a))
8. Theorem
The even function returns either true or false
(ann even? [Number -> Boolean])
(defn even? [p]
(= (mod p 2) 0))
9. Escape hatches
ā£ null (actually not an issue in core.typed)
ā£ exceptions
ā£ Type-casing (isInstanceOf)
ā£ Type-casting (asInstanceOf)
ā£ Side-effects
ā£ equals/toString/hashCode
ā£ notify/wait
ā£ classOf/.getClass
ā£ General recursion
10. Escape hatches
ā£ null (actually not an issue in core.typed)
ā£ exceptions
ā£ Type-casing (isInstanceOf)
ā£ Type-casting (asInstanceOf)
ā£ Side-effects
ā£ equals/toString/hashCode
ā£ notify/wait
ā£ classOf/.getClass
ā£ General recursion
11. Theorem
Every element A in the result sequence appears in the input.
(ann irrelevant (All [x] [(Seq x) -> (Seq x)]))
(defn irrelevant [xs]
nil)
12. Theorem
Every element A in the result sequence appears in the input.
(ann irrelevant (All [x] [(Seq x) -> (Seq x)]))
(defn irrelevant [xs]
nil)
Doesnāt compile with core.typed!
13. Theorem
This function ignores its argument and consistently returns either true or false
(ann irrelevant (All [a] [a -> Boolean]))
(defn irrelevant [a]
(or (instance? Long 1)
(and (instance? String a)
(< (count a) 10))))
14. Theorem
This function ignores its argument and consistently returns either true or false
(ann irrelevant (All [a] [a -> Boolean]))
(defn irrelevant [a]
(or (instance? Long 1)
(and (instance? String a)
(< (count a) 10))))
15. Theorem
Every A element in the result list appears in the input list
(ann irrelevant (All [x] [(Seq x) -> (Seq x)]))
(defn irrelevant [xs]
(cons (cast "abc" 'x) xs))
16. Theorem
Every A element in the result list appears in the input list
(ann irrelevant (All [x] [(Seq x) -> (Seq x)]))
(defn irrelevant [xs]
(cons (cast "abc" 'x) xs))
Doesnāt compile with core.typed!
17. Theorem
This function only ever does one thing āreturn its argument
(ann irrelevant (All [a] [a -> a]))
(defn irrelevant [x]
(prn "hi")
x)
18. Theorem
This function only ever does one thing āreturn its argument
(ann irrelevant (All [a] [a -> a]))
(defn irrelevant [x]
(prn "hi")
x)
19. Theorem
This function ignores its argument to return one of 2^32 values.
(ann irrelevant (All [a] [a -> Int]))
(defn irrelevant [x]
(-> x
str
(.length)))
20. Theorem
This function ignores its argument to return one of 2^32 values.
(ann irrelevant (All [a] [a -> Int]))
(defn irrelevant [x]
(-> x
str
(.length)))
21. Theorem
This function always returns Nil and so cannot possibly reverse the list
(ann reverse (All [a b] [(Seq a) -> (Seq b)]))
(defn reverse [as]
(reduce (fn [acc a]
(conj acc (cast b a))) nil as))
22. Theorem
This function always returns Nil and so cannot possibly reverse the list
(ann reverse (All [a b] [(Seq a) -> (Seq b)]))
(defn reverse [as]
(reduce (fn [acc a]
(conj acc (cast b a))) nil as))
Doesnāt compile with core.typed!
23. Escape hatches
ā£ null (actually not an issue in core.typed)
ā£ exceptions
ā£ Type-casing (isInstanceOf)
ā£ Type-casting (asInstanceOf)
ā£ Side-effects
ā£ equals/toString/hashCode
ā£ notify/wait
ā£ classOf/.getClass
ā£ General recursion
27. Example: repeat
(defn repeat
"Returns a lazy (infinite!, or length n if supplied) sequence of xs."
([x] (clojure.lang.Repeat/create x))
([n x] (clojure.lang.Repeat/create n x)))
28. Example: repeat
(defn repeat
"Returns a lazy (infinite!, or length n if supplied) sequence of xs."
([x] (clojure.lang.Repeat/create x))
([n x] (clojure.lang.Repeat/create n x)))
29. Example: repeat
(ann repeat (All [x]
(IFn [x -> (ASeq x)]
[AnyInteger x -> (ASeq x)])))
(defn repeat
"Returns a lazy (infinite!, or length n if supplied) sequence of xs."
([x] (clojure.lang.Repeat/create x))
([n x] (clojure.lang.Repeat/create n x)))
30. Example: iterate
(ann iterate (All [x]
[[x -> x] x -> (ASeq x)]))
(defn iterate
"Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of
side-effects"
[f x] (clojure.lang.Iterate/create f x) )
31. Example: iterate
(ann iterate (All [x]
[[x -> x] x -> (ASeq x)]))
(defn iterate
"Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of
side-effects"
[f x] (clojure.lang.Iterate/create f x) )
32. Example: iterate
(ann iterate (All [x]
[[x -> x] x -> (ASeq x)]))
(defn iterate
"Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of
side-effects"
[f x] (clojure.lang.Iterate/create f x) )
41. Takeaway points
ā£ Donāt dismiss types too quickly. They can improve code comprehension
and quality
ā£ Even comments expressed in terms of types can be a big help when
reading foreign code
ā£ Write property-based tests ļ¬rst, unit tests second