summaryrefslogtreecommitdiff
path: root/src/kompsos.ads
blob: f3f847044ad1dce9b6aa4b4bbbc97d64fa604bd7 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318


--  Programmed by Jedidiah Barber
--  Licensed under the Sunset License v1.0

--  See license.txt for further details


with

    Ada.Strings.Unbounded;

private with

    Ada.Containers.Ordered_Maps,
    Ada.Containers.Vectors,
    Ada.Finalization;


generic
    type Element_Type is private;
package Kompsos is


    type Term is tagged private;
    type Term_Array is array (Positive range <>) of Term;

    --  Can also be constructed by supplying an empty array to T
    --  or just declaring a Term without initial value.
    Null_Term : constant Term;

    function "="
           (Left, Right : in Term)
        return Boolean;

    function T
           (Item : in Element_Type)
        return Term;

    function T
           (Item1, Item2 : in Term'Class)
        return Term;

    function T
           (Items : in Term_Array)
        return Term;

    --  Might include subprograms to retrieve Term contents later?


    type Mu_World is tagged private;
    type Mu_World_Array is array (Positive range <>) of Mu_World;

    Empty_Mu_World : constant Mu_World;




    function Fresh
           (This : in out Mu_World'Class)
        return Term;

    function Fresh
           (This : in out Mu_World'Class;
            Name : in     String)
        return Term;

    function Fresh
           (This : in out Mu_World'Class;
            Name : in     Ada.Strings.Unbounded.Unbounded_String)
        return Term;




    function Unify
           (This  : in Mu_World;
            Left  : in Term'Class;
            Right : in Element_Type)
        return Mu_World;

    procedure Unify
           (This  : in out Mu_World;
            Left  : in     Term'Class;
            Right : in     Element_Type);

    function Unify
           (This        : in Mu_World;
            Left, Right : in Term'Class)
        return Mu_World;

    procedure Unify
           (This        : in out Mu_World;
            Left, Right : in     Term'Class);




    function Disjunct
           (Left, Right : in Mu_World)
        return Mu_World;

    procedure Disjunct
           (This  : in out Mu_World;
            Right : in     Mu_World);

    function Disjunct
           (Inputs : in Mu_World_Array)
        return Mu_World;

    procedure Disjunct
           (This   : in out Mu_World;
            Inputs : in     Mu_World_Array);




    function Recurse
           (This : in Mu_World)
        return Mu_World;

    procedure Recurse
           (This : in out Mu_World);




    function Take
           (This  : in Mu_World;
            Count : in Natural)
        return Mu_World;

    procedure Take
           (This  : in out Mu_World;
            Count : in     Natural);

    procedure Force
           (This  : in out Mu_World;
            Count : in     Positive);

    procedure Force_All
           (This : in out Mu_World);

    function Failed
           (This : in out Mu_World)
        return Boolean;


private


    package SU renames Ada.Strings.Unbounded;


    function "+"
           (Item : in String)
        return SU.Unbounded_String
    renames SU.To_Unbounded_String;

    function "-"
           (Item : in SU.Unbounded_String)
        return String
    renames SU.To_String;




    --  2^32 possible Variables per World is enough for anybody, right?
    type ID_Number is mod 2 ** 32;

    type Variable is record
        Ident : ID_Number;
        Name  : SU.Unbounded_String;
    end record;




    type Term_Kind is (Atom_Term, Var_Term, Pair_Term);

    type Term_Component (Kind : Term_Kind) is record
        Count : Long_Integer;
        case Kind is
        when Atom_Term =>
            Value : Element_Type;
        when Var_Term =>
            Refer : Variable;
        when Pair_Term =>
            Left, Right : Term;
        end case;
    end record;

    type Term_Component_Access is access Term_Component;

    type Term is new Ada.Finalization.Controlled with record
        Actual : Term_Component_Access;
    end record;

    overriding procedure Initialize
           (This : in out Term);

    overriding procedure Adjust
           (This : in out Term);

    overriding procedure Finalize
           (This : in out Term);

    Null_Term : constant Term := (Ada.Finalization.Controlled with Actual => null);




    package Name_Maps is new Ada.Containers.Ordered_Maps
       (Key_Type     => ID_Number,
        Element_Type => SU.Unbounded_String,
        "="          => SU."=");

    package Binding_Maps is new Ada.Containers.Ordered_Maps
       (Key_Type     => ID_Number,
        Element_Type => Term);

    type State is record
        LVars : Name_Maps.Map;
        Subst : Binding_Maps.Map;
    end record;

    Empty_State : constant State :=
       (LVars => Name_Maps.Empty_Map,
        Subst => Binding_Maps.Empty_Map);




    type World_Access is access Mu_World'Class;

    type World_Holder is new Ada.Finalization.Controlled with record
        Ptr : World_Access;
    end record;

    overriding procedure Initialize
           (This : in out World_Holder);

    overriding procedure Adjust
           (This : in out World_Holder);

    overriding procedure Finalize
           (This : in out World_Holder);

    function Hold
           (This : in Mu_World'Class)
        return World_Holder;

    procedure Swap
           (Left, Right : in out World_Holder);

    type Generator_Kind is
       (No_Gen,
        Fresh_Gen,
        Unify_Gen,
        Buffer_Gen,
        Disjunct_Gen,
        Recurse_Gen);

    type Generator (Kind : Generator_Kind := No_Gen) is record
        case Kind is
        when No_Gen =>
            null;
        when Fresh_Gen =>
            Frs_World  : World_Holder;
            Frs_Name   : SU.Unbounded_String;
        when Unify_Gen =>
            Uni_World  : World_Holder;
            Uni_Term1  : Term;
            Uni_Term2  : Term;
        when Buffer_Gen =>
            Buff_World : World_Holder;
        when Disjunct_Gen =>
            Dis_World1 : World_Holder;
            Dis_World2 : World_Holder;
        when Recurse_Gen =>
            Rec_World  : World_Holder;
            Rec_Index  : Positive;
        end case;
    end record;

    package State_Vectors is new Ada.Containers.Vectors
       (Index_Type   => Positive,
        Element_Type => State);

    type Mu_World is tagged record
        Possibles  : State_Vectors.Vector;
        Next_Ident : ID_Number;
        Engine     : Generator;
    end record;

    function Has_State
           (This  : in out Mu_World;
            Index : in     Positive)
        return Boolean;

    procedure Rollover
           (This : in out Mu_World);

    procedure Roll_Until
           (This  : in out Mu_World;
            Index : in     Positive);

    use type State_Vectors.Vector;

    Empty_Mu_World : constant Mu_World :=
       (Possibles  => State_Vectors.Empty_Vector & Empty_State,
        Next_Ident => 0,
        Engine     => (Kind => No_Gen));


end Kompsos;