Skip to content

Instantly share code, notes, and snippets.

@mpickering
Created May 10, 2020 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpickering/144e42b4429673d8449f59413cc10ca4 to your computer and use it in GitHub Desktop.
Save mpickering/144e42b4429673d8449f59413cc10ca4 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TemplateHaskell #-}
module Bad where
import Language.Haskell.TH.Lib
import GHC.Exts
-- Example from the user guide but with the function wrapped in quotes
bad :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
(a :: TYPE r1) (b :: TYPE r2).
TExpQ ((a -> b) -> a -> b)
bad = [|| \f x -> f x ||]
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TemplateHaskell #-}
module Lev where
import Language.Haskell.TH.Lib
import Bad
import GHC.Exts
-- Code generator type checked but splicing fails to produce well-typed
-- program -- unsound
unsound :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
(a :: TYPE r1) (b :: TYPE r2).
((a -> b) -> a -> b)
unsound = $$(bad)
-- Generating a monomorphic program is sound, and hence a workaround for
-- normal levity polymorphic restrictions.
workaround :: Int
workaround = $$bad id 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment