Created
February 13, 2015 16:14
-
-
Save aaronlevin/81086303892e0e6fbb4d to your computer and use it in GitHub Desktop.
Is this fundamentally any different than normal phantom 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 DataKinds #-} | |
| {-# LANGUAGE GADTs #-} | |
| {-# LANGUAGE KindSignatures #-} | |
| {-# LANGUAGE OverloadedStrings #-} | |
| {-# LANGUAGE RankNTypes #-} | |
| {-# LANGUAGE TypeFamilies #-} | |
| {- Is this fundamentally any different than regular phantom types ala https://wiki.haskell.org/Phantom_type ? | |
| -} | |
| module Phantom.Alternative where | |
| -- | universe of validation types | |
| data ValidationUniverse = Validated | Unvalidated | |
| -- | alternative to phantom: `data FormData a = FormData String` | |
| -- note `UnvalidatedData` contains more information (a user & msg) | |
| data FormData (v :: ValidationUniverse) :: * where | |
| ValidatedData :: String -> FormData Validated | |
| UnvalidatedData :: String -> String -> FormData Unvalidated | |
| -- | validate data particulare to a user | |
| validate :: FormData Unvalidated -> Maybe (FormData Validated) | |
| validate (UnvalidatedData user msg) = | |
| if length msg == 10 && user == "aaron" | |
| then Just (ValidatedData msg) | |
| else Nothing | |
| -- | do something with valid form data | |
| useData :: FormData Validated -> IO () | |
| useData (ValidatedData s) = print s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment