首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型检查RPS示例时出现内部错误

类型检查RPS示例时出现内部错误
EN

Stack Overflow用户
提问于 2015-05-24 04:47:08
回答 1查看 39关注 0票数 0

这是example from core.typed github page:

代码语言:javascript
复制
(ns typedclj.rps-async
  (:require [clojure.core.typed :as t]
            [clojure.core.async :as a]
            [clojure.core.typed.async :as ta]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Types
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(t/defalias Move
            "A legal move in rock-paper-scissors"
            (t/U ':rock ':paper ':scissors))

(t/defalias PlayerName
            "A player's name in rock-paper-scissors"
            t/Str)

(t/defalias PlayerMove
            "A move in rock-paper-scissors. A Tuple of player name and move"
            '[PlayerName Move])

(t/defalias RPSResult
            "The result of a rock-paper-scissors match.
            A 3 place vector of the two player moves, and the winner"
            '[PlayerMove PlayerMove PlayerName])

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Implementation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(t/ann MOVES (t/Vec Move))
(def MOVES [:rock :paper :scissors])

(t/ann BEATS (t/Map Move Move))
(def BEATS {:rock :scissors, :paper :rock, :scissors :paper})

(t/ann rand-player [PlayerName -> (ta/Chan PlayerMove)])
(defn rand-player
  "Create a named player and return a channel to report moves."
  [name]
  (let [out (ta/chan :- PlayerMove)]
    (ta/go (while true (a/>! out [name (rand-nth MOVES)])))
    out))

(t/ann winner [PlayerMove PlayerMove -> PlayerName])
(defn winner
  "Based on two moves, return the name of the winner."
  [[name1 move1] [name2 move2]]
  (cond
    (= move1 move2) "no one"
    (= move2 (BEATS move1)) name1
    :else name2))

(t/ann judge [(ta/Chan PlayerMove) (ta/Chan PlayerMove) -> (ta/Chan RPSResult)])
(defn judge
  "Given two channels on which players report moves, create and return an
  output channel to report the results of each match as [move1 move2 winner]."
  [p1 p2]
  (let [out (ta/chan :- RPSResult)]
    (ta/go
      (while true
        (let [m1 (a/<! p1)
              m2 (a/<! p2)]
          (assert m1)
          (assert m2)
          (a/>! out (t/ann-form [m1 m2 (winner m1 m2)]
                                RPSResult)))))
    out))

(t/ann init (t/IFn [PlayerName PlayerName -> (ta/Chan RPSResult)]
                   [-> (ta/Chan RPSResult)]))
(defn init
  "Create 2 players (by default Alice and Bob) and return an output channel of match results."
  ([] (init "Alice" "Bob"))
  ([n1 n2] (judge (rand-player n1) (rand-player n2))))

(t/ann report [PlayerMove PlayerMove PlayerName -> nil])
(defn report
  "Report results of a match to the console."
  [[name1 move1] [name2 move2] winner]
  (println)
  (println name1 "throws" move1)
  (println name2 "throws" move2)
  (println winner "wins!"))

(t/ann play [(ta/Chan RPSResult) -> nil])
(defn play
  "Play by taking a match reporting channel and reporting the results of the latest match."
  [out-chan]
  (let [[move1 move2 winner] (a/<!! out-chan)]
    (assert move1)
    (assert move2)
    (assert winner)
    (report move1 move2 winner)))

(t/ann play-many [(ta/Chan RPSResult) t/Int -> (t/Map t/Any t/Any)])
(defn play-many
  "Play n matches from out-chan and report a summary of the results."
  [out-chan n]
  (t/loop [remaining :- t/Int, n
           results :- (t/Map PlayerName t/Int), {}]
          (if (zero? remaining)
            results
            (let [[m1 m2 winner] (a/<!! out-chan)]
              (assert m1)
              (assert m2)
              (assert winner)
              (recur (dec remaining)
                     (merge-with + results {winner 1}))))))


(fn []
  (t/ann-form (a/<!! (init))
              (t/U nil RPSResult)))

如果您在repl中选中它:

代码语言:javascript
复制
(clojure.core.typed/check-ns 'typedclj.rps-async)

你会得到一个错误:

代码语言:javascript
复制
Initializing core.typed ...
Building core.typed base environments ...
Finished building base environments
"Elapsed time: 9213.869003 msecs"
core.typed initialized.
Start collecting typedclj.rps-async
Start collecting clojure.core.typed.async
Finished collecting clojure.core.typed.async
Finished collecting typedclj.rps-async
Collected 2 namespaces in 1725.941447 msecs
Not checking clojure.core.typed (does not depend on clojure.core.typed)
Not checking clojure.core.async (does not depend on clojure.core.typed)
Not checking clojure.core.async.impl.channels (does not depend on clojure.core.typed)
Not checking clojure.core.async.impl.ioc-macros (does not depend on clojure.core.typed)
Not checking clojure.core.async.impl.protocols (does not depend on clojure.core.typed)
Not checking clojure.core.async.impl.dispatch (does not depend on clojure.core.typed)
Not checking clojure.core.typed.util-vars (does not depend on clojure.core.typed)
Start checking clojure.core.typed.async
Checked clojure.core.typed.async in 502.126883 msecs
Start checking typedclj.rps-async
Type Error (typedclj/rps_async.clj:91:13) Internal Error (typedclj/rps_async.clj:91:5) Bad call to path-type: nil, ({:idx 0})
ExceptionInfo Type Checker: Found 1 error  clojure.core/ex-info (core.clj:4403)

这里出了什么问题?

EN

回答 1

Stack Overflow用户

发布于 2015-05-24 11:44:21

这是一个core.typed错误。请提交票证here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30417425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档