# Decoupling from dependencies

**URL:** <https://discourse.haskell.org/t/decoupling-from-dependencies/12429>\
**Category:** Links\
**Created:** [July 9, 2025, 7:38am UTC](https://discourse.haskell.org/t/decoupling-from-dependencies/12429 "2025-07-09T07:38:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcosh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/marcosh/32/436_2.png) [@marcosh](https://discourse.haskell.org/u/marcosh)\
**Post date:** [July 9, 2025, 7:38am UTC](https://discourse.haskell.org/t/decoupling-from-dependencies/12429/1 "2025-07-09T07:38:52Z")

</div>

I wrote a new blog post.

Just a simple idea but hopefully worth sharing

> **[Decoupling from dependencies](https://marcosh.github.io/post/2025/07/09/decoupling-from-dependencies.html)**
>
> Decoupling from dependencies

---

<div class="post-metadata">

**Author:** ![f-a](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/f-a/32/2740_2.png) [@f-a](https://discourse.haskell.org/u/f-a)\
**Post date:** [July 9, 2025, 11:11am UTC](https://discourse.haskell.org/t/decoupling-from-dependencies/12429/2 "2025-07-09T11:11:33Z")

</div>

Interesting article!

I wonder: why did you choose `data` over typeclasses?

This

> [@](#):
>
> Abstract the context `m` in which the operations are actually happening, delaying the choice to a later moment and allowing the possibility to reuse the same abstraction with multiple concrete implementations. This could be useful if we want to write unit tests for effectful code using a mocked version of the dependency.

seems exactly what a typeclass is suited for, am I wrong?

---

<div class="post-metadata">

**Author:** ![marcosh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/marcosh/32/436_2.png) [@marcosh](https://discourse.haskell.org/u/marcosh)\
**Post date:** [July 9, 2025, 11:28am UTC](https://discourse.haskell.org/t/decoupling-from-dependencies/12429/3 "2025-07-09T11:28:57Z")

</div>

You’re not wrong at all. Typeclasses are another (probably more standard) way to declare an interface in Haskell.

I actually plan to write a follow-up post on several way to declare an interface in Haskell.

[This post](https://www.haskellforall.com/2012/05/scrap-your-type-classes.html) by Gabriella Gonzales details some reasons why sometime a typeclass could not be the best tool for declaring an interface

---

<div class="post-metadata">

**Author:** ![f-a](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/f-a/32/2740_2.png) [@f-a](https://discourse.haskell.org/u/f-a)\
**Post date:** [July 9, 2025, 2:06pm UTC](https://discourse.haskell.org/t/decoupling-from-dependencies/12429/4 "2025-07-09T14:06:17Z")

</div>

I have problems checking how `applicationBar` works.

```haskell
applicationBar :: OurOwnFoo -> String
applicationBar = ourOwnBarImplementation.ourOwnBar

```

`ourOwnBar` has type `OurOwnFoo -> String`, which is fine. But then `ourOwnBarImplementation` has type `OurOwnBar`. What am I missing?

---

<div class="post-metadata">

**Author:** ![marcosh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/marcosh/32/436_2.png) [@marcosh](https://discourse.haskell.org/u/marcosh)\
**Post date:** [July 9, 2025, 2:35pm UTC](https://discourse.haskell.org/t/decoupling-from-dependencies/12429/5 "2025-07-09T14:35:25Z")

</div>

I think I used too many similar types…

`ourOwnBarImplementation` has type `OurOwnBar`. `OurOwnBar` has a field `ourOwnBar` of type `OurOwnFoo -> String`.

Hence `ourOwnBarImplementation.ourOwnBar` should have type `OurOwnFoo -> String`, unless I’m missing something

---

<div class="post-metadata">

**Author:** ![f-a](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/f-a/32/2740_2.png) [@f-a](https://discourse.haskell.org/u/f-a)\
**Post date:** [July 9, 2025, 2:58pm UTC](https://discourse.haskell.org/t/decoupling-from-dependencies/12429/6 "2025-07-09T14:58:49Z")

</div>

```Haskell
data Foo = Foo
data OurOwnFoo = OurOwnFoo
data OurOwnBar = OurOwnBar {ourOwnBar :: OurOwnFoo -> String}

bar :: Foo -> String
bar = undefined

encodeFoo :: OurOwnFoo -> Foo
encodeFoo = undefined

ourOwnBarImplementation :: OurOwnBar
ourOwnBarImplementation = OurOwnBar $ bar . encodeFoo

applicationBar :: OurOwnFoo -> String
applicationBar = ourOwnBarImplementation.ourOwnBar

```

errors with:

```haskell
/tmp/prova.hs:15:18-40: error: [GHC-83865]
    • Couldn't match expected type ‘(OurOwnFoo -> String) -> String’
                  with actual type ‘OurOwnBar’
    • In the first argument of ‘(.)’, namely ‘ourOwnBarImplementation’
      In the expression: ourOwnBarImplementation . ourOwnBar
      In an equation for ‘applicationBar’:
          applicationBar = ourOwnBarImplementation . ourOwnBar
   |
15 | applicationBar = ourOwnBarImplementation.ourOwnBar
   | ^^^^^^^^^^^^^^^^^^^^^^^
/tmp/prova.hs:15:42-50: error: [GHC-83865]
    • Couldn't match type ‘OurOwnBar’ with ‘OurOwnFoo’
      Expected: OurOwnFoo -> OurOwnFoo -> String
        Actual: OurOwnBar -> OurOwnFoo -> String
    • In the second argument of ‘(.)’, namely ‘ourOwnBar’
      In the expression: ourOwnBarImplementation . ourOwnBar
      In an equation for ‘applicationBar’:
          applicationBar = ourOwnBarImplementation . ourOwnBar
   |
15 | applicationBar = ourOwnBarImplementation.ourOwnBar

```

---

<div class="post-metadata">

**Author:** ![marcosh](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/marcosh/32/436_2.png) [@marcosh](https://discourse.haskell.org/u/marcosh)\
**Post date:** [July 9, 2025, 3:12pm UTC](https://discourse.haskell.org/t/decoupling-from-dependencies/12429/7 "2025-07-09T15:12:21Z")

</div>

Ah, I forgot `OverloadedRecordDot`!

> **[Haskell Playground](https://play.haskell.org/saved/rKce4eey)**
