blob: 24c7b0e722c352ed5f6ca4c8fbe59a63d7fbfb5c (
plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
(.module:
[lux #*
[abstract
[codec (#+ Codec)]
[monad (#+ do)]]
[control
["ex" exception (#+ exception:)]
["p" parser]]
[data
["." error (#+ Error)]
[number
["." int ("#@." decimal)]]
["." text ("#@." monoid)
["l" lexer (#+ Lexer)]]]
[type
abstract]
[macro
["." code]
["s" syntax (#+ syntax:)]]])
(exception: #export zero-cannot-be-a-modulus)
(abstract: #export (Modulus m)
{#.doc (doc "A number used as a modulus in modular arithmetic."
"It cannot be 0.")}
Int
(def: #export (from-int value)
(Ex [m] (-> Int (Error (Modulus m))))
(if (i/= +0 value)
(ex.throw zero-cannot-be-a-modulus [])
(#error.Success (:abstraction value))))
(def: #export (to-int modulus)
(All [m] (-> (Modulus m) Int))
(|> modulus :representation))
)
(exception: #export [m] (incorrect-modulus {modulus (Modulus m)}
{parsed Int})
(ex.report ["Expected" (int@encode (to-int modulus))]
["Actual" (int@encode parsed)]))
(exception: #export [rm sm] (cannot-equalize-moduli {reference (Modulus rm)}
{sample (Modulus sm)})
(ex.report ["Reference" (int@encode (to-int reference))]
["Sample" (int@encode (to-int sample))]))
(def: #export (congruent? modulus reference sample)
(All [m] (-> (Modulus m) Int Int Bit))
(|> sample
(i/- reference)
(i/% (to-int modulus))
(i/= +0)))
(syntax: #export (modulus {modulus s.int})
(case (from-int modulus)
(#error.Success _)
(wrap (list (` (error.assume (..from-int (~ (code.int modulus)))))))
(#error.Failure error)
(p.fail error)))
(def: intL
(Lexer Int)
(p.codec int.decimal
(l.and (l.one-of "-+") (l.many l.decimal))))
(abstract: #export (Mod m)
{#.doc "A number under a modulus."}
{#remainder Int
#modulus (Modulus m)}
(def: #export (mod modulus)
(All [m] (-> (Modulus m) (-> Int (Mod m))))
(function (_ value)
(:abstraction {#remainder (i/mod (to-int modulus) value)
#modulus modulus})))
(def: #export (un-mod modular)
(All [m] (-> (Mod m) [Int (Modulus m)]))
(:representation modular))
(def: separator " mod ")
(structure: #export (codec modulus)
(All [m] (-> (Modulus m) (Codec Text (Mod m))))
(def: (encode modular)
(let [[remainder modulus] (:representation modular)]
($_ text@compose
(int@encode remainder)
separator
(int@encode (to-int modulus)))))
(def: (decode text)
(<| (l.run text)
(do p.monad
[[remainder _ _modulus] ($_ p.and intL (l.this separator) intL)
_ (p.assert (ex.construct incorrect-modulus [modulus _modulus])
(i/= (to-int modulus) _modulus))]
(wrap (mod modulus remainder))))))
(def: #export (equalize reference sample)
(All [r s] (-> (Mod r) (Mod s) (Error (Mod r))))
(let [[reference reference-modulus] (:representation reference)
[sample sample-modulus] (:representation sample)]
(if (i/= (to-int reference-modulus)
(to-int sample-modulus))
(#error.Success (:abstraction {#remainder sample
#modulus reference-modulus}))
(ex.throw cannot-equalize-moduli [reference-modulus sample-modulus]))))
(template [<name> <op>]
[(def: #export (<name> reference sample)
(All [m] (-> (Mod m) (Mod m) Bit))
(let [[reference _] (:representation reference)
[sample _] (:representation sample)]
(<op> reference sample)))]
[m/= i/=]
[m/< i/<]
[m/<= i/<=]
[m/> i/>]
[m/>= i/>=]
)
(template [<name> <op>]
[(def: #export (<name> param subject)
(All [m] (-> (Mod m) (Mod m) (Mod m)))
(let [[param modulus] (:representation param)
[subject _] (:representation subject)]
(:abstraction {#remainder (|> subject
(<op> param)
(i/mod (to-int modulus)))
#modulus modulus})))]
[m/+ i/+]
[m/- i/-]
[m/* i/*])
(def: (i/gcd+ a b)
(-> Int Int [Int Int Int])
(if (i/= +0 a)
[+0 +1 b]
(let [[ak bk gcd] (i/gcd+ (i/% a b) a)]
[(i/- (i/* ak
(i// a b))
bk)
ak
gcd])))
(def: #export (inverse modular)
(All [m] (-> (Mod m) (Maybe (Mod m))))
(let [[value modulus] (:representation modular)
_modulus (to-int modulus)
[vk mk gcd] (i/gcd+ value _modulus)
co-prime? (i/= +1 gcd)]
(if co-prime?
(#.Some (mod modulus vk))
#.None)))
)
|