Created
January 28, 2018 17:12
-
-
Save NunoAlexandre/223f3c2991810a347a2e9cd2bd1fdad9 to your computer and use it in GitHub Desktop.
A case for Dependent Types
This file contains 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 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