-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rational.hs
64 lines (50 loc) · 1.8 KB
/
Rational.hs
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
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE PolyKinds #-}
module Rational where
import Data.Kind ( Type )
import Bool
import Nat
import Int
data Q =
Fraction {
numerator :: Z,
denominator :: Nat
}
data IsQ (i :: Q) :: Type where
IsFrac :: IsZ m -> IsNat n -> IsQ ('Fraction m n)
type family ZeroQ :: Q where
ZeroQ = 'Fraction ZeroZ OneN
type family IsNullQ (m :: Q) :: Bool where
IsNullQ ('Fraction ('Int _ 'ZN) _) = 'True
IsNullQ _ = 'False
type family Simplify (m ::Q) :: Q where
Simplify ('Fraction ('Int _ 'ZN) ('SN d)) = ZeroQ
Simplify ('Fraction ('Int s num) ('SN d)) = AuxFrac s num ('SN d) (PGCD num ('SN d))
type family AuxFrac (s :: Sign) (num :: Nat) (d :: Nat) (pgcd :: Nat) :: Q where
AuxFrac s num d pgcd = 'Fraction ('Int s (Quo num pgcd)) (Quo d pgcd)
type family EqQ (a :: Q) (b :: Q) :: Bool where
EqQ ('Fraction ('Int _ 'ZN) _) ('Fraction ('Int _ 'ZN) _) = 'True
EqQ ('Fraction ('Int sA numA) denA) ('Fraction ('Int sB numB) denB) =
And (EqSign sA sB) (EqN (MultN numA denB) (MultN numB denA))
type family AddQ (a :: Q) (b :: Q) :: Q where
AddQ ('Fraction numA denA) ('Fraction numB denB) =
'Fraction
(AddZ (MultZ numA (CastNZ denB)) (MultZ numB (CastNZ denA)))
(MultN denA denB)
type family MultQ (a :: Q) (b :: Q) :: Q where
MultQ ('Fraction ('Int _ 'ZN) _) _ = ZeroQ
MultQ _ ('Fraction ('Int _ 'ZN) _) = ZeroQ
MultQ ('Fraction numA denA) ('Fraction numB denB) =
'Fraction
(MultZ numA numB)
(MultN denA denB)
type family InvZ (n :: Z) :: Q where
InvZ ('Int s n) = 'Fraction ('Int s ('SN 'ZN)) n
type family CastZQ (z :: Z) :: Q where
CastZQ z = 'Fraction z ('SN 'ZN)
type family OneQ :: Q where
OneQ = CastZQ OneZ