如何在Clojure';s核心类型?
我想对代码应用core.type注释,但在如何/何时实例化从函数体内部调用的多态核心函数方面遇到了障碍 解决这个问题后,我了解到我必须特别对待filter和count,因为它们分别是多态的和静态的,匿名函数应该被拉出并在let绑定中注释。如果有人能解释如何根据下面错误消息的输出对其进行注释,我将不胜感激 以下是我的别名:如何在Clojure';s核心类型?,clojure,type-systems,clojure-core.typed,Clojure,Type Systems,Clojure Core.typed,我想对代码应用core.type注释,但在如何/何时实例化从函数体内部调用的多态核心函数方面遇到了障碍 解决这个问题后,我了解到我必须特别对待filter和count,因为它们分别是多态的和静态的,匿名函数应该被拉出并在let绑定中注释。如果有人能解释如何根据下面错误消息的输出对其进行注释,我将不胜感激 以下是我的别名: (defalias Key-set (Set (Option Kw))) (defalias Player (Ref1 Key-set)) (defalias Set-vec
(defalias Key-set (Set (Option Kw)))
(defalias Player (Ref1 Key-set))
(defalias Set-vec (Vec (Set Kw)))
(defalias Move (U ':x ':o))
函数所处的当前形式:
(ann first-two [Player -> (Option Seq)])
(defn first-two [player]
(let [;; best guesses here
filter> (inst filter [[(U (Seqable Any) clojure.lang.Counted nil) -> Bool] -> Any] (Option (clojure.lang.Seqable [(U (Seqable Any) clojure.lang.Counted nil) -> Bool])))
count> (ann-form count [(U nil (Seqable Any) clojure.lang.Counted) -> Number])
two-count? :- [(U nil (Seqable Any) clojure.lang.Counted)-> Bool], #(= (count> %) 2)
couples :- (Option (Seqable Key-set)), (concat adjacents opposite-corners opposite-sides)]
(first (filter> two-count?
(for [pair :- Key-set, couples]
(clojure.set/intersection pair @player))))))
类型检查器错误消息:
Type Error (tic_tac_toe/check.clj:52:5) Function filter> could not be applied to arguments:
Domains:
[[[(U (Seqable Any) Counted nil) -> Bool] -> Any] -> Any :filters {:then (is (Option (clojure.lang.Seqable [(U nil (Seqable Any) clojure.lang.Counted) -> Bool])) 0), :else tt}] (Option (clojure.lang.Seqable [[(U (Seqable Any) Counted nil) -> Bool] -> Any]))
[[[(U (Seqable Any) Counted nil) -> Bool] -> Any] -> Any :filters {:then (! (Option (clojure.lang.Seqable [(U nil (Seqable Any) clojure.lang.Counted) -> Bool])) 0), :else tt}] (Option (clojure.lang.Seqable [[(U (Seqable Any) Counted nil) -> Bool] -> Any]))
[[[(U (Seqable Any) Counted nil) -> Bool] -> Any] -> Any] (Option (clojure.lang.Seqable [[(U (Seqable Any) Counted nil) -> Bool] -> Any]))
Arguments:
[(U nil (Seqable Any) clojure.lang.Counted) -> Bool] (clojure.lang.LazySeq Any)
Ranges:
(ASeq (Option (clojure.lang.Seqable [(U nil (Seqable Any) clojure.lang.Counted) -> Bool])))
(ASeq (I [[(U (Seqable Any) Counted nil) -> Bool] -> Any] (Not (Option (clojure.lang.Seqable [(U nil (Seqable Any) clojure.lang.Counted) -> Bool])))))
(ASeq [[(U (Seqable Any) Counted nil) -> Bool] -> Any])
ExceptionInfo Type Checker: Found 1 error clojure.core/ex-info (core.clj:4566)
原始功能:
(defn first-two [player]
(let [couples (concat adjacents opposite-corners opposite-sides)]
(first (filter #(= (count %) 2)
(for [pair couples]
(clojure.set/intersection pair @player))))))
我现在没时间帮你查一查,所以我有个简单的想法 但是类型化clojure中的多态类型参数通常不会直接映射到参数。快速看一眼,我想这就是你问题的根源 例如,假设我们定义了一个多态函数,并希望将其实例化
(ann f (All [x] [[x -> Bool] x -> Integer]))
(defn f [predicate? value] (if (f value) 1 0))
要正确地实例化它,您必须这样做
((inst f String) (typed/fn [x :- String] :- Bool true) "lol")
而不是
((inst f [String -> Bool]) (typed/fn [x :- String] :- Bool true) "lol2") ;; baaaad baaaad
现在我必须回去工作了,因为我今天花了太多时间定制emacs啊哈…你可以在别处提供类型,以避免实例化
过滤器
例如,此类型检查
(ns typed-test.core
(:refer-clojure :exclude [fn for])
(:require [clojure.core.typed :as t
:refer [fn for Vec Coll Any Num]]))
(filter (fn [a :- (Coll Any)] (= (count a) 2))
(for [pair :- (Vec Num), {1 2 3 4}]
:- (Vec Num)
pair))
可能缺少调用for
的返回类型会丢失太多信息
inst
只是用一些类型替换All
活页夹中的类型变量
typed-test.core=> (cf identity)
(t/All [x] [x -> x :filters {:then (! (t/U nil false) 0), :else (is (t/U nil false) 0)} :object {:id 0}])
typed-test.core=> (cf (t/inst identity Num))
[Num -> Num :filters {:then (! (t/U nil false) 0), :else (is (t/U nil false) 0)} :object {:id 0}]
我现在正在搞砸它。你的解释确实为我澄清了很多关于多态类型的问题,但是我仍然不确定我需要做什么来修复这个例子,我所做的一切似乎都不起作用。