aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/math/modular.lux
blob: acfdbab2d83327f08c683b5b3fe406afdd38f6d9 (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
(.module:
  [library
   [lux #*
    [abstract
     [equivalence (#+ Equivalence)]
     [order (#+ Order)]
     [monoid (#+ Monoid)]
     [codec (#+ Codec)]
     [monad (#+ do)]]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]
     ["<>" parser
      ["<.>" text (#+ Parser)]
      ["<.>" code]]]
    [data
     ["." product]
     ["." text ("#\." monoid)]]
    [macro
     [syntax (#+ syntax:)]
     ["." code]]
    [math
     [number
      ["i" int ("#\." decimal)]]]
    [type
     abstract]]]
  ["." // #_
   ["#" modulus (#+ Modulus)]])

(abstract: .public (Mod m)
  {}

  (Record
   {#modulus (Modulus m)
    #value Int})

  (def: .public (modular modulus value)
    (All [%] (-> (Modulus %) Int (Mod %)))
    (:abstraction {#modulus modulus
                   #value (i.mod (//.divisor modulus) value)}))

  (template [<name> <type> <side>]
    [(def: .public <name>
       (All [%] (-> (Mod %) <type>))
       (|>> :representation <side>))]

    [modulus (Modulus %) product.left]
    [value Int product.right]
    )
  
  (exception: .public [%] (incorrect_modulus {modulus (Modulus %)}
                                             {parsed Int})
    (exception.report
     ["Expected" (i\encoded (//.divisor modulus))]
     ["Actual" (i\encoded parsed)]))

  (def: separator
    " mod ")

  (def: intL
    (Parser Int)
    (<>.codec i.decimal
              (<text>.and (<text>.one_of "-+") (<text>.many <text>.decimal))))

  (implementation: .public (codec expected)
    (All [%] (-> (Modulus %) (Codec Text (Mod %))))

    (def: (encoded modular)
      (let [[_ value] (:representation modular)]
        ($_ text\composite
            (i\encoded value)
            ..separator
            (i\encoded (//.divisor expected)))))

    (def: decoded
      (<text>.result
       (do <>.monad
         [[value _ actual] ($_ <>.and intL (<text>.this ..separator) intL)
          _ (<>.assertion (exception.error ..incorrect_modulus [expected actual])
                          (i.= (//.divisor expected) actual))]
         (in (..modular expected value))))))

  (template [<name> <op>]
    [(def: .public (<name> reference subject)
       (All [%] (-> (Mod %) (Mod %) Bit))
       (let [[_ reference] (:representation reference)
             [_ subject] (:representation subject)]
         (<op> reference subject)))]

    [= i.=]
    [< i.<]
    [<= i.<=]
    [> i.>]
    [>= i.>=]
    )

  (implementation: .public equivalence
    (All [%] (Equivalence (Mod %)))

    (def: = ..=))

  (implementation: .public order
    (All [%] (Order (Mod %)))

    (def: &equivalence ..equivalence)
    (def: < ..<))

  (template [<name> <op>]
    [(def: .public (<name> param subject)
       (All [%] (-> (Mod %) (Mod %) (Mod %)))
       (let [[modulus param] (:representation param)
             [_ subject] (:representation subject)]
         (:abstraction {#modulus modulus
                        #value (|> subject
                                   (<op> param)
                                   (i.mod (//.divisor modulus)))})))]

    [+ i.+]
    [- i.-]
    [* i.*]
    )

  (template [<composition> <identity> <monoid>]
    [(implementation: .public (<monoid> modulus)
       (All [%] (-> (Modulus %) (Monoid (Mod %))))

       (def: identity
         (..modular modulus <identity>))
       (def: composite
         <composition>))]
    
    [..+ +0 addition]
    [..* +1 multiplication]
    )
  
  (def: .public (inverse modular)
    (All [%] (-> (Mod %) (Maybe (Mod %))))
    (let [[modulus value] (:representation modular)
          [[vk mk] gcd] (i.extended_gcd value (//.divisor modulus))]
      (case gcd
        +1 (#.Some (..modular modulus vk))
        _ #.None)))
  )

(exception: .public [r% s%] (moduli_are_not_equal {reference (Modulus r%)}
                                                  {subject (Modulus s%)})
  (exception.report
   ["Reference" (i\encoded (//.divisor reference))]
   ["Subject" (i\encoded (//.divisor subject))]))

(def: .public (adapter reference subject)
  (All [r% s%]
    (-> (Modulus r%) (Modulus s%)
        (Try (-> (Mod s%) (Mod r%)))))
  (if (//.= reference subject)
    (#try.Success (|>> ..value
                       (..modular reference)))
    (exception.except ..moduli_are_not_equal [reference subject])))