# How to safely use withDict?

**URL:** <https://discourse.haskell.org/t/how-to-safely-use-withdict/13571>\
**Category:** Learn\
**Created:** [January 19, 2026, 11:04pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571 "2026-01-19T23:04:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashokkimmel](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/ashokkimmel/32/5111_2.png) [@ashokkimmel](https://discourse.haskell.org/u/ashokkimmel)\
**Post date:** [January 19, 2026, 11:04pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/1 "2026-01-19T23:04:16Z")

</div>

I was wondering if `ImplicitParams` could be emulated via `withDict` from `GHC.Base`

```haskell
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
import GHC.Base (withDict)
class LocalVar str val | str -> val where
    local :: val
data LocalDict str val where 
    LocalDict :: LocalVar str val => LocalDict str val

mkLocalDict :: forall str val. val -> LocalDict str val
mkLocalDict val = withDict @(LocalVar str val) val LocalDict

pattern LocalBind :: forall str val. LocalDict str val -> val
pattern LocalBind dict <- (mkLocalDict @str @val -> dict)
  where
    LocalBind (LocalDict :: LocalDict str val) = local @str @val
{-# COMPLETE LocalBind #-}
main = do 
    LocalBind @"inpt" LocalDict <- getLine
    putStrLn (local @"inpt")

```

This gives me:  
`bus error runhaskell`  
and  
`segfault runhaskell`  
or just doesn’t output anything. Is withDict inherently unsafe or am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![Leary](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/leary/32/4348_2.png) [@Leary](https://discourse.haskell.org/u/Leary)\
**Post date:** [January 20, 2026, 5:56am UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/2 "2026-01-20T05:56:37Z")

</div>

`withDict` should only threaten coherence, not cause segfaults—this is a GHC bug. The code also compiles just fine, so I guess it’s a bytecode issue. I suggest trying again with latest GHC to see if it’s already been fixed, otherwise report the bug on the GHC gitlab.

---

<div class="post-metadata">

**Author:** ![arybczak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/arybczak/32/2527_2.png) [@arybczak](https://discourse.haskell.org/u/arybczak)\
**Post date:** [January 20, 2026, 3:15pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/3 "2026-01-20T15:15:25Z")

</div>

This seems to work fine with GHC 9.14.1, segfaults with earlier versions.

---

<div class="post-metadata">

**Author:** ![ashokkimmel](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/ashokkimmel/32/5111_2.png) [@ashokkimmel](https://discourse.haskell.org/u/ashokkimmel)\
**Post date:** [January 20, 2026, 6:24pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/4 "2026-01-20T18:24:08Z")

</div>

Interestingly, while running it on my computer gave a bus error, it seems to work fine with PlayHaskell. Is that due to os types? (Mine was windows) V 9.12.2

---

<div class="post-metadata">

**Author:** ![arybczak](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/arybczak/32/2527_2.png) [@arybczak](https://discourse.haskell.org/u/arybczak)\
**Post date:** [January 20, 2026, 6:26pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/5 "2026-01-20T18:26:05Z")

</div>

If it segfaults for you on Windows even on 9.14.1, please report it as a GHC bug.

---

<div class="post-metadata">

**Author:** ![ashokkimmel](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/ashokkimmel/32/5111_2.png) [@ashokkimmel](https://discourse.haskell.org/u/ashokkimmel)\
**Post date:** [January 20, 2026, 6:26pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/6 "2026-01-20T18:26:32Z")

</div>

Sorry, that was 9.12.2

---

<div class="post-metadata">

**Author:** ![Leary](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/leary/32/4348_2.png) [@Leary](https://discourse.haskell.org/u/Leary)\
**Post date:** [January 20, 2026, 10:11pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/7 "2026-01-20T22:11:45Z")

</div>

play.haskell compiles.

---

<div class="post-metadata">

**Author:** ![ashokkimmel](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/ashokkimmel/32/5111_2.png) [@ashokkimmel](https://discourse.haskell.org/u/ashokkimmel)\
**Post date:** [January 20, 2026, 11:13pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/8 "2026-01-20T23:13:18Z")

</div>

Not only compiles, it runs fine. However on my computer, it gave a bus error on GHC 9.12.2. I was simply wondering why it worked there and not on my computer given that it was the same compiler. I’m guessing machine types?

---

<div class="post-metadata">

**Author:** ![Leary](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/leary/32/4348_2.png) [@Leary](https://discourse.haskell.org/u/Leary)\
**Post date:** [January 20, 2026, 11:24pm UTC](https://discourse.haskell.org/t/how-to-safely-use-withdict/13571/9 "2026-01-20T23:24:13Z")

</div>

> [@Leary](#):
>
> The code also compiles just fine, so I guess it’s a bytecode issue.

Perhaps I wasn’t clear. What I meant by this is that—from what I’ve seen—ghc produces good binaries for the code; the issue only emerges when _interpreting_ it, such as with ghci, runghc or runhaskell. Since play.haskell compiles to binary, it’s not affected.
