aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/project.lux
blob: 321e86661176d840c8bb19618c42b988a335cc78 (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
(.module:
  [library
   [lux (#- Name)
    [abstract
     [equivalence (#+ Equivalence)]
     [monoid (#+ Monoid)]
     ["." monad (#+ do)]]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]]
    [data
     ["." text
      ["%" format (#+ format)]]
     [collection
      ["." dictionary (#+ Dictionary)]
      ["." set (#+ Set)]
      ["." list ("#\." fold)]]]]]
  ["." // #_
   ["#" profile (#+ Name Profile)]])

(def: #export file
  "project.lux")

(type: #export Project
  (Dictionary Name Profile))

(def: #export (project name profile)
  (-> Name Profile Project)
  (dictionary.from_list text.hash (list [name profile])))

(def: #export equivalence
  (Equivalence Project)
  (dictionary.equivalence //.equivalence))

(implementation: #export monoid
  (Monoid Project)

  (def: identity
    (dictionary.new text.hash))

  (def: compose
    (dictionary.merge_with (\ //.monoid compose))))

(exception: #export (unknown_profile {name Name})
  (exception.report
   ["Name" (%.text name)]))

(exception: #export (circular_dependency {dependee Name} {dependent Name})
  (exception.report
   ["Dependent" (%.text dependent)]
   ["Dependee" (%.text dependee)]))

(def: (profile' lineage project name)
  (-> (Set Name) Project Name (Try Profile))
  (case (dictionary.get name project)
    (#.Some profile)
    (case (list.find (set.member? lineage)
                     (get@ #//.parents profile))
      (#.Some ouroboros)
      (exception.throw ..circular_dependency [ouroboros name])
      
      #.None
      (do {! try.monad}
        [parents (monad.map ! (profile' (set.add name lineage) project)
                            (get@ #//.parents profile))]
        (wrap (list\fold (function (_ parent child)
                           (\ //.monoid compose child parent))
                         (set@ #//.parents (list) profile)
                         parents))))
    
    #.None
    (exception.throw ..unknown_profile [name])))

(def: #export (profile name project)
  (-> Name Project (Try Profile))
  (..profile' (set.new text.hash) project name))