<< Back to previous view

[ALGOM-3] Bug in writer-monad-protocol for lists Created: 18/Apr/12  Updated: 05/Feb/14  Resolved: 05/Feb/14

Status: Closed
Project: algo.monads
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Minor
Reporter: Greg Chapman Assignee: Konrad Hinsen
Resolution: Completed Votes: 0
Labels: None
Environment:

Clojure 1.4, Java 1.7, Windows 7


Attachments: File monads.clj.diff    

 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")]
}}



 Comments   
Comment by Konrad Hinsen [ 05/Feb/14 5:45 AM ]

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.

Generated at Wed Apr 16 23:00:43 CDT 2014 using JIRA 4.4#649-r158309.