1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Data.Reflection

Last updated at Posted at 2013-04-10

reifyで型に情報を閉じ込めて、reflectで取り出すというもの。情報の持ち運びのために、最後にダミーの型を持つようなファントム型を定義する(ここではM)。

% runghc
{-# LANGUAGE NoMonomorphismRestriction, ScopedTypeVariables #-}
module Main (main) where
import Data.Proxy
import Data.Reflection

main :: IO ()
main = do
  print $ reify 7 (\(_ :: Proxy s) -> unM (culc :: (Integral a, Reifies s a) => M a s))
  print $ reify 9 (\(_ :: Proxy s) -> unM (culc :: (Integral a, Reifies s a) => M a s))
  return ()

newtype M a s = M { unM :: a } deriving (Eq, Show)
add, mul :: (Integral a, Reifies s a) => M a s -> M a s -> M a s
add a b = M $ (unM a + unM b) `mod` (reflect a)
mul a b = M $ (unM a * unM b) `mod` (reflect a)

culc :: (Integral a, Reifies s a) => M a s
culc = (M 3 `add` M 2) `mul` M 5
4
7

型のあわせ方は http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf を参照した。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?