【问题标题】:How do I annotate protocols and their methods in Clojure's core.typed?如何在 Clojure 的 core.typed 中注释协议及其方法?
【发布时间】:2014-12-08 08:50:46
【问题描述】:

我正在制作井字游戏,并为我的策略制定了协议。游戏运行良好,所以我想借此机会磨练我的 core.typed 技能。我已经对协议进行了注释(如下所示),但是当我在repl中运行(cf method-name)(cf protocol-name)时,我得到了这个错误:

例如:

=> `(cf win)`
clojure.lang.ExceptionInfo: Type Checker: Found 1 error :: {:type-error :top-level-error, :errors (#<ExceptionInfo clojure.lang.ExceptionInfo: Unannotated var tic-tac-toe.protocol/win
Hint: Add the annotation for tic-tac-toe.protocol/win via check-ns or cf {:env {:file "/Users/jessediaz/Documents/workspace/tic-tac-toe/src/tic_tac_toe/protocol.clj", :column 5, :line 47}, :form win, :type-error :clojure.core.typed.errors/tc-error-parent}>)}

我已检查以确保协议的版本实际上是 core.typed/protocol。当我使用 protocol> 说语法已被弃用时,控制台对我咆哮。我还查看了 github 页面和 clojure-doc.org 上的文档。这就是我了解到有一个可选的 :methods 关键字,我可以使用它来将方法映射到类型。我觉得这可能会从我的代码中删除很多重复,但我无法找到任何使用示例。协议中的主要策略方法都有副作用,返回nil。验证方法返回 nil 或它们验证的原始 Strategy 方法。我不确定我在那里有语法,但是代码在 LightTable 中的评估结果很好,有或没有Fn 签名,他们 core.typed wiki 暗示它并不总是必要的。

我觉得我正处于学习这个令人惊叹的图书馆的风口浪尖,如果有任何有见地的建议可以帮助我理解,我将不胜感激。

protocol.clj 文件:

(ns tic-tac-toe.protocol
  (:require [clojure.core.typed :refer :all]))

(ann-protocol Strategy
                win                        (Fn [Strategy -> nil])
                block                      (Fn [Strategy -> nil])
                fork                       (Fn [Strategy -> nil])
                block-fork                 (Fn [Strategy -> nil])
                take-center                (Fn [Strategy -> nil])
                take-opposite-corner       (Fn [Strategy -> nil])
                take-corner                (Fn [Strategy -> nil])
                take-side                  (Fn  [Strategy -> nil])

                can-win?                   (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
                can-block?                 (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
                can-fork?                  (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
                can-block-fork?            (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
                can-take-center?           (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
                can-take-corner?           (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
                can-take-side?             (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))]))
(defprotocol Strategy
  "Strategy methods update the Tic-tac-toe board when they are called"

  ;; strategies
  (win [_] "wins the game by filling an open space to get 3 in a row")
  (block [_] "blocks an opponents win by filling an open space")
  (fork [_] "creates a two way win scenario guaranteeing victory")
  (block-fork [_] "prevents an opponent from forking")
  (take-center [_] "takes center")
  (take-opposite-corner [_] "takes a corner opposite to one the computer already has")
  (take-corner [_] "takes an avaiable corner")
  (take-side [_] "takes an available side")

  ;; tests
  (can-win? [_])
  (can-block? [_])
  (can-fork? [_])
  (can-block-fork? [_])
  (can-take-center? [_])
  (can-take-opposite-corner? [_])
  (can-take-corner? [_])
  (can-take-side? [_]))

【问题讨论】:

    标签: clojure type-systems lighttable clojure-core.typed


    【解决方案1】:

    我怀疑在使用cf 查询当前命名空间之前,您需要先check-ns。类型化代码的评估不会触发类型检查或注释收集。

    这里不需要ann-protocolclojure.core.typed/defprotocol支持内联注解。

    尝试类似:

    (defprotocol Strategy
      "Strategy methods update the Tic-tac-toe board when they are called"
    
      ;; strategies
      (win [_] :- nil "wins the game by filling an open space to get 3 in a row")
      (block [_] :- nil "blocks an opponents win by filling an open space")
      ...)
    

    【讨论】:

    • 谢谢安布罗斯!我会试一试。
    • 是否推断出这些方法的输入类型?
    • 'self' 类型是推断出来的,不能在 defprotocol 中直接覆盖(还没有!)。对于非多态协议,'self' 是策略,否则它是(策略 a b c)假设协议由 a、b 和 c 参数化。
    • 第一个之后的所有参数都没有被推断出来。
    • 这是有道理的。但是,如果我返回 nil 或 Strategy 方法,我会对其进行注释:- (U nil [nil]) 还是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2011-05-29
    相关资源
    最近更新 更多