algo.monads

Bug in writer-monad-protocol for lists

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    Clojure 1.4, Java 1.7, Windows 7

Description

The writer-monad-protocol for lists uses concat in writer-m-combine, the result of which has type LazySeq. However, LazySeq does not have an implementation in writer-monad-protocol (the first example, using vectors, shows expected output):

{{
user=> (use 'clojure.algo.monads)
nil
user=> (domonad (writer-m []) [_ (domonad [_ (write "foo")] nil) _ (write "bar")] 1)
[1 ["foo" "bar"]]
user=> (domonad (writer-m ()) [_ (domonad [_ (write "foo")] nil) _ (write "bar")] 1)
IllegalArgumentException No implementation of method: :writer-m-combine of protocol: #'clojure.algo.monads/writer-monad-
protocol found for class: clojure.lang.LazySeq clojure.core/-cache-protocol-fn (core_deftype.clj:527)
}}

I suggest changing the protocol extension to ISeq as in the attached diff. With that change:

{{
user=> (use 'clojure.algo.monads)
nil
user=> (domonad (writer-m []) [_ (domonad [_ (write "foo")] nil) _ (write "bar")] 1)
[1 ["foo" "bar"]]
user=> (domonad (writer-m ()) [_ (domonad [_ (write "foo")] nil) _ (write "bar")] 1)
[1 ("foo" "bar")]
}}

Activity

Hide
Konrad Hinsen added a comment -

I fixed this using a somewhat different approach: ensure that the return type of writer-m-combine is a list if it is called with list arguments:
https://github.com/clojure/algo.monads/commit/7679776bbddde7a716b9e077a4f65d1a568fe9c7

I prefer this solution because ISeq is implemented by so many types that a writer-m-combine implementation for this protocol may well cause trouble elsewhere.

Show
Konrad Hinsen added a comment - I fixed this using a somewhat different approach: ensure that the return type of writer-m-combine is a list if it is called with list arguments: https://github.com/clojure/algo.monads/commit/7679776bbddde7a716b9e077a4f65d1a568fe9c7 I prefer this solution because ISeq is implemented by so many types that a writer-m-combine implementation for this protocol may well cause trouble elsewhere.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: