summaryrefslogtreecommitdiff
path: root/src/kompsos.ads
blob: 0463f6fdd526bf7888b54ffcd80c6f3a2946bfc4 (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550


--  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.Indefinite_Holders,
    Ada.Containers.Ordered_Maps,
    Ada.Containers.Vectors,
    Ada.Finalization;


generic
    type Element_Type is private;
package Kompsos is


    -----------------
    --  Datatypes  --
    -----------------

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

    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 World is tagged private;
    type World_Array is array (Positive range <>) of World;

    Empty_World : constant World;


    type Conjunct_Zero_Func is access function
           (This : in World)
        return World;

    type Conjunct_One_Func is access function
           (This  : in World;
            Input : in Term'Class)
        return World;

    type Conjunct_Many_Func is access function
           (This   : in World;
            Inputs : in Term_Array)
        return World;




    -------------------
    --  microKanren  --
    -------------------

    --  Variable Introduction  --

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

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

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


    --  Unification  --

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

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

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

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


    --  Combining / Disjunction  --

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

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

    function Disjunct
           (Inputs : in World_Array)
        return World;

    procedure Disjunct
           (This   : in out World;
            Inputs : in     World_Array);


    --  Lazy Sequencing / Conjunction  --

    function Conjunct
           (This : in World;
            Func : in Conjunct_Zero_Func)
        return World;

    procedure Conjunct
           (This : in out World;
            Func : in     Conjunct_Zero_Func);

    function Conjunct
           (This  : in World;
            Func  : in Conjunct_One_Func;
            Input : in Term'Class)
        return World;

    procedure Conjunct
           (This  : in out World;
            Func  : in     Conjunct_One_Func;
            Input : in     Term'Class);

    function Conjunct
           (This   : in World;
            Func   : in Conjunct_Many_Func;
            Inputs : in Term_Array)
        return World;

    procedure Conjunct
           (This   : in out World;
            Func   : in     Conjunct_Many_Func;
            Inputs : in     Term_Array);




    -------------------------
    --  Auxiliary Helpers  --
    -------------------------

    --  Infinite State Loops  --

    function Recurse
           (This : in World)
        return World;

    procedure Recurse
           (This : in out World);


    --  Forced Evaluation  --

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

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

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

    procedure Force_All
           (This : in out World);

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




    --------------------------
    --  miniKanren Prelude  --
    --------------------------

    --  caro  --
    --  Inputs = List_Term & Head_Term
    function Head
           (This   : in World;
            Inputs : in Term_Array)
        return World
    with Pre => Inputs'Length = 2;

    --  Inputs = List_Term & Head_Term
    procedure Head
           (This   : in out World;
            Inputs : in     Term_Array)
    with Pre => Inputs'Length = 2;


    --  cdro  --
    --  Inputs = List_Term & Tail_Term
    function Tail
           (This   : in World;
            Inputs : in Term_Array)
        return World
    with Pre => Inputs'Length = 2;

    --  Inputs = List_Term & Tail_Term
    procedure Tail
           (This   : in out World;
            Inputs : in     Term_Array)
    with Pre => Inputs'Length = 2;


    --  conso  --
    --  Inputs = Head_Term & Tail_Term & List_Term
    function Cons
           (This   : in World;
            Inputs : in Term_Array)
        return World
    with Pre => Inputs'Length = 3;

    --  Inputs = Head_Term & Tail_Term & List_Term
    procedure Cons
           (This   : in out World;
            Inputs : in     Term_Array)
    with Pre => Inputs'Length = 3;


    --  nullo  --

    function Nil
           (This     : in World;
            Nil_Term : in Term'Class)
        return World;

    procedure Nil
           (This     : in out World;
            Nil_Term : in     Term'Class);


    --  eqo  --
    --  Skipped due to being a synonym for Unify


    --  eq-caro  --
    --  Skipped due to being a synonym for Head


    --  pairo  --

    function Pair
           (This      : in World;
            Pair_Term : in Term'Class)
        return World;

    procedure Pair
           (This      : in out World;
            Pair_Term : in     Term'Class);


    --  listo  --

    function Linked_List
           (This      : in World;
            List_Term : in Term'Class)
        return World;

    procedure Linked_List
           (This      : in out World;
            List_Term : in     Term'Class);


    --  membero  --
    --  Inputs = Item_Term & List_Term
    function Member
           (This   : in World;
            Inputs : in Term_Array)
        return World
    with Pre => Inputs'Length = 2;

    --  Inputs = Item_Term & List_Term
    procedure Member
           (This   : in out World;
            Inputs : in     Term_Array)
    with Pre => Inputs'Length = 2;


    --  rembero  --
    --  Inputs = Item_Term & List_Term & Out_Term
    function Remove
           (This   : in World;
            Inputs : in Term_Array)
        return World
    with Pre => Inputs'Length = 3;

    --  Inputs = Item_Term & List_Term & Out_Term
    procedure Remove
           (This   : in out World;
            Inputs : in     Term_Array)
    with Pre => Inputs'Length = 3;


    --  appendo  --
    --  Inputs = List_Term & Item_Term & Out_Term
    function Append
           (This   : in World;
            Inputs : in Term_Array)
        return World
    with Pre => Inputs'Length = 3;

    --  Inputs = List_Term & Item_Term & Out_Term
    procedure Append
           (This   : in out World;
            Inputs : in     Term_Array)
    with Pre => Inputs'Length = 3;


    --  anyo  --
    --  Skipped due to Recurse doing the same thing


    --  nevero  --
    --  Skipped since it just creates a failed World


    --  alwayso  --
    --  Skipped due to Recurse doing the same thing


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 Term_Array_Holders is new Ada.Containers.Indefinite_Holders (Term_Array);




    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 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 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,
        Conjunct_Zero_Gen,
        Conjunct_One_Gen,
        Conjunct_Many_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 Conjunct_Zero_Gen =>
            ConZ_World  : World_Holder;
            ConZ_Func   : Conjunct_Zero_Func;
        when Conjunct_One_Gen =>
            ConO_World  : World_Holder;
            ConO_Func   : Conjunct_One_Func;
            ConO_Input  : Term;
        when Conjunct_Many_Gen =>
            ConM_World  : World_Holder;
            ConM_Func   : Conjunct_Many_Func;
            ConM_Inputs : Term_Array_Holders.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 World is tagged record
        Possibles  : State_Vectors.Vector;
        Next_Ident : ID_Number;
        Engine     : Generator;
    end record;

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

    procedure Rollover
           (This : in out World);

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

    use type State_Vectors.Vector;

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


end Kompsos;