aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/symbol.lux
blob: 4e0a67ec20b1d80cbc73c3229c5966526965464b (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
(.using
 [library
  [lux (.full)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]
    [order (.only Order)]
    [codec (.only Codec)]]
   [data
    ["[0]" text ("[1]#[0]" equivalence monoid)]
    ["[0]" product]]]])

... (type: Symbol
...   [Text Text])

(template [<name>]
  [(def: .public (<name> [module short])
     (-> Symbol Text)
     <name>)]

  [module]
  [short]
  )

(def: .public hash
  (Hash Symbol)
  (product.hash text.hash text.hash))

(def: .public equivalence
  (Equivalence Symbol)
  (# ..hash equivalence))

(implementation: .public order
  (Order Symbol)
  
  (def: equivalence ..equivalence)
  (def: (< [moduleP shortP] [moduleS shortS])
    (if (text#= moduleP moduleS)
      (# text.order < shortP shortS)
      (# text.order < moduleP moduleS))))

(def: .public separator
  ".")

(implementation: .public codec
  (Codec Text Symbol)
  
  (def: (encoded [module short])
    (case module
      "" short
      _ (all text#composite module ..separator short)))
  
  (def: (decoded input)
    (case (text.all_split_by ..separator input)
      (pattern (list short))
      {.#Right ["" short]}

      (pattern (list module short))
      {.#Right [module short]}

      _
      {.#Left (text#composite "Invalid format for Symbol: " input)})))