Skip to content

Instantly share code, notes, and snippets.

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 NunoAlexandre/223f3c2991810a347a2e9cd2bd1fdad9 to your computer and use it in GitHub Desktop.
Save NunoAlexandre/223f3c2991810a347a2e9cd2bd1fdad9 to your computer and use it in GitHub Desktop.
A case for Dependent Types
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
-- Support for https://nunoalexandre.com/2018/01/28/a-case-for-dependent-types
data ValueType = StringType | IntType
data Value :: ValueType -> * where
StringValue :: String -> Value 'StringType
IntValue :: Int -> Value 'IntType
resetWithDefault :: Value a -> Value a
resetWithDefault (StringValue _) = StringValue "Hello, world!"
resetWithDefault (IntValue _) = IntValue 3
serious :: Value 'StringType -> Value 'StringType
serious (StringValue str) = StringValue $ str ++ "!"
foo :: Value 'StringType -> Value 'IntType
foo (StringValue _) = IntValue 3
bar :: Value a -> Value a
-- Compiling error as 'IntValue' does not match 'StringValue'
bar (StringValue str) = IntValue 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment