Created
March 23, 2026 21:25
-
-
Save instinctive/7e9eb8522c794ea130184507605a8a11 to your computer and use it in GitHub Desktop.
Creating custom Unbox types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE MultiParamTypeClasses #-} | |
| {-# LANGUAGE TypeFamilies #-} | |
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
| {-# LANGUAGE StandaloneDeriving #-} | |
| import qualified Data.Vector.Generic as G | |
| import qualified Data.Vector.Generic.Mutable as M | |
| import qualified Data.Vector.Unboxed as U | |
| import Data.Word (Word64) | |
| newtype Foo = Foo Word64 deriving (Eq, Show) | |
| -- 1. Tell GHC that Foo's Mutable Vector is just a Word64 Mutable Vector | |
| newtype instance U.MVector s Foo = MV_Foo (U.MVector s Word64) | |
| -- 2. Tell GHC that Foo's Immutable Vector is just a Word64 Vector | |
| newtype instance U.Vector Foo = V_Foo (U.Vector Word64) | |
| -- 3. Use GeneralizedNewtypeDeriving to steal the implementations | |
| deriving instance M.MVector U.MVector Foo | |
| deriving instance G.Vector U.Vector Foo | |
| -- 4. Mark it as Unboxable | |
| instance U.Unbox Foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment