Add tags

Add tags to the following comment
依存型風に書いてみました.法は型で与えます.
法を 3 とするなら,Modulo (S(S(S Z))) Int を
法を10 とするなら,Modulo (S(S(S(S(S(S(S(S(S(S Z)))))))))) Int を
ここでは簡単のために法 0 〜 10 に対応する型エイリアスを宣言してあります.

*Modulo> 1+2 :: Modulo10
3
*Modulo> 7+3 :: Modulo10
0
*Modulo> 11+12 :: Modulo10
3
*Modulo> 3-2 :: Modulo10
1
*Modulo> 2-3 :: Modulo10
9
*Modulo> 2*3 :: Modulo10
6
*Modulo> 11*12 :: Modulo10
2
*Modulo> 18*39 :: Modulo10
2
*Modulo> (1-2::Modulo10) == (1+8::Modulo10)
True
*Modulo> 1+2 :: Modulo7
3
*Modulo> 7+3 :: Modulo7
3
*Modulo> 11+12 :: Modulo7
2
*Modulo> 3-2 :: Modulo7
1
*Modulo> 2-3 :: Modulo7
6
*Modulo> 2*3 :: Modulo7
6
*Modulo> 11*12 :: Modulo7
6
*Modulo> 18*39 :: Modulo7
2
*Modulo> (1-2::Modulo7) == (1+8::Modulo7)
False
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# LANGUAGE EmptyDataDecls #-}

module Modulo where

data Z
zero :: Z
zero = undefined
data S a

predecessor :: S a -> a
predecessor = undefined

class Nat a where
  toInteger :: a -> Integer

instance Nat Z where
  toInteger _ = 0

instance Nat a => Nat (S a) where
  toInteger = (1 +) . Modulo.toInteger . predecessor

newtype Modulo a b = Modulo (a,b)

instance Eq b => Eq (Modulo a b) where
  Modulo (_,x) == Modulo (_,y) = x == y

instance Show b => Show (Modulo a b) where
  show (Modulo (_,x)) = show x

instance (Nat a, Integral b) => Num (Modulo a b) where
  Modulo (m,x) + Modulo (_,y) = Modulo (m,z)
    where z = (x+y) `mod` fromInteger (Modulo.toInteger m)
  Modulo (m,x) - Modulo (_,y) = Modulo (m,z)
    where z = (x-y) `mod` fromInteger (Modulo.toInteger m)
  Modulo (m,x) * Modulo (_,y) = Modulo (m,z)
    where z = (x*y) `mod` fromInteger (Modulo.toInteger m)
  abs = id
  signum (Modulo (m,x)) = Modulo $ (m,signum x)
  fromInteger n = Modulo (m,z)
    where z = fromInteger (n `mod` Modulo.toInteger m)
          m = undefined :: Nat a => a

type Zero  = Z
type One   = S Zero
type Two   = S One      ; type Modulo2 = Modulo Two Int
type Three = S Two      ; type Modulo3 = Modulo Three Int
type Four  = S Three    ; type Modulo4 = Modulo Four Int
type Five  = S Four     ; type Modulo5 = Modulo Five Int
type Six   = S Five     ; type Modulo6 = Modulo Six Int
type Seven = S Six      ; type Modulo7 = Modulo Seven Int
type Eight = S Seven    ; type Modulo8 = Modulo Eight Int
type Nine  = S Eight    ; type Modulo9 = Modulo Nine Int
type Ten   = S Nine     ; type Modulo10= Modulo Ten Int

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...