This file is indexed.

/usr/lib/ats2-postiats-0.2.6/libats/DATS/linheap_binomial.dats is in ats2-lang 0.2.6-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
(***********************************************************************)
(*                                                                     *)
(*                         Applied Type System                         *)
(*                                                                     *)
(***********************************************************************)

(*
** ATS/Postiats - Unleashing the Potential of Types!
** Copyright (C) 2011-2013 Hongwei Xi, ATS Trustful Software, Inc.
** All rights reserved
**
** ATS is free software;  you can  redistribute it and/or modify it under
** the terms of  the GNU GENERAL PUBLIC LICENSE (GPL) as published by the
** Free Software Foundation; either version 3, or (at  your  option)  any
** later version.
** 
** ATS is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without  even  the  implied  warranty  of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
** for more details.
** 
** You  should  have  received  a  copy of the GNU General Public License
** along  with  ATS;  see the  file COPYING.  If not, please write to the
** Free Software Foundation,  51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*)

(* ****** ****** *)

(* Author: Hongwei Xi *)
(* Authoremail: hwxi AT cs DOT bu DOT edu *)
(* Start time: November, 2011 *)

(* ****** ****** *)
//
// HX: Note that this implementation is largely of
// functional-style and only supports mergeable-heap
// operations; it particular it does not support the
// decrease-key operation.
//
(* ****** ****** *)
//
// HX-2012-12: ported to ATS/Postiats
//
(* ****** ****** *)

#define ATS_PACKNAME "ATSLIB.libats.linheap_binomial"
#define ATS_DYNLOADFLAG 0 // no need for dynloading at run-time

(* ****** ****** *)

staload UN = "prelude/SATS/unsafe.sats"

(* ****** ****** *)

staload "libats/SATS/linheap_binomial.sats"

(* ****** ****** *)

#define nullp the_null_ptr

(* ****** ****** *)
//
// HX-2012-12-21:
// the file should be included here
// before [heap_vtype] is assumed
//
#include "./SHARE/linheap.hats" // code reuse
//
(* ****** ****** *)
//
// binomial trees:
// btree(a, n) is for a binomial tree of rank(n)
//
datavtype
btree (
a:vt@ype+, int(*rank*)
) = // btree
  | {n:nat}
    btnode (a, n) of (int (n), a, btreelst (a, n))
// end of [btree]

and btreelst
(
  a:vt@ype+, int(*rank*)
) =
  | {n:nat}
    btlst_cons (a, n+1) of (btree (a, n), btreelst (a, n))
  | btlst_nil (a, 0)
// end of [btreelst]

(* ****** ****** *)

datavtype
bheap (
  a:vt@ype+, int(*rank*), int(*size*)
) = // bheap
  | {n:nat}
    bheap_nil (a, n, 0) of ()
  | {n:nat}{p:int}{sz:nat}{n1:int | n1 > n}
    bheap_cons (a, n, p+sz) of (EXP2 (n, p) | btree (a, n), bheap (a, n1, sz))
// end of [bheap]

(* ****** ****** *)

fun{
a:vt0p
} btree_rank
  {n:int} .<>. (
  bt: !btree (a, n)
) :<> int (n) = let
  val btnode (n, _, _) = bt in n
end // end of [btree_rank]

(* ****** ****** *)

extern
fun{
} pow2 {n:nat}
  (n: int n):<> [p:int] (EXP2 (n, p) | size_t p)
// end of [pow2]

implement{}
pow2 {n} (n) = let
  val res =
    g0uint_lsl_size ((i2sz)1, n)
  val [p:int] res = g1ofg0_uint (res)
  prval pf = __assert () where
  {
    extern praxi __assert (): EXP2 (n, p)
  } // end of [where] // end of [prval]
in
  (pf | res)
end // end of [pow2]

(* ****** ****** *)

fun{
a:vt0p
} btree_size
  {n:int} .<>.
(
  bt: !btree (a, n)
) :<> [p:int]
(
  EXP2 (n, p) | size_t (p)
) = let
  val btnode (n, _, _) = bt in pow2 (n)
end // end of [btree_size]

(* ****** ****** *)

fun{
a:vt0p
} bheap_size
  {n:int}{sz:int}
(
  hp: !bheap (a, n, sz)
) : size_t (sz) = let
in
//
case+ hp of
| bheap_cons
    (pf | bt, hp) => let
    val (pf2 | p) = btree_size<a> (bt)
    prval () = exp2_isfun (pf, pf2)
  in
    p + bheap_size<a> (hp)
  end // end of [bheap_cons]
| bheap_nil ((*void*)) => g1int2uint (0)
//
end // end of [bheap_size]

(* ****** ****** *)

fun{
a:t0p
} btree_free
  {n:nat} .<n,1>.
  (bt: btree (a, n)) :<!wrt> void = let 
  val ~btnode (_, _, bts) = bt in btreelst_free<a> (bts)
end // end of [btree_free]

and
btreelst_free
  {n:nat} .<n,0>.
(
  bts: btreelst (a, n)
) :<!wrt> void = let
in
//
case+ bts of
| ~btlst_cons
    (bt, bts) => let
    val () = btree_free<a> (bt) in btreelst_free<a> (bts)
  end // end of [btlst_cons]
|  ~btlst_nil () => ()
end // end of [btreelst_free]

(* ****** ****** *)

fun{a:vt0p}
btree_btree_merge
  {n:nat} .<>. (
  bt1: btree (a, n)
, bt2: btree (a, n)
) :<!wrt> btree (a, n+1) = let
  val @btnode (n1, x1, bts1) = bt1
  val @btnode (n2, x2, bts2) = bt2
  val sgn = compare_elt_elt<a> (x1, x2)
in
  if sgn <= 0 then let
    prval () = fold@ (bt2)
    val () = n1 := n1 + 1
    val () = bts1 := btlst_cons{a}(bt2, bts1)
  in
    fold@ (bt1); bt1
  end else let
    prval () = fold@ (bt1)
    val () = n2 := n2 + 1
    val () = bts2 := btlst_cons{a}(bt1, bts2)
  in
    fold@ (bt2); bt2
  end // end of [if]
end // end of [btree_btree_merge]

(* ****** ****** *)

fun{a:vt0p}
btree_bheap_merge
  {n:nat}
  {n1:int | n <= n1}
  {sz:nat}{p:int} .<sz>.
(
  pf: EXP2 (n, p)
| bt: btree (a, n), n: int (n), hp: bheap (a, n1, sz)
) :<!wrt> [n2:int | n2 >= min(n, n1)] bheap (a, n2, sz+p) =
  case+ hp of
  | ~bheap_nil () =>
      bheap_cons{a}(pf | bt, bheap_nil {a} {n+1} ())
    // end of [bheap_nil]
  | @bheap_cons (pf1 | bt1, hp1) => let
      val n1 = btree_rank<a> (bt1)
    in
      if n < n1 then let
        prval () = fold@ (hp) in bheap_cons{a}(pf | bt, hp)
      end else if n > n1 then let
        val () = hp1 := btree_bheap_merge<a> (pf | bt, n, hp1)
        prval () = fold@ (hp) 
      in
        hp
      end else let
        prval () = exp2_ispos (pf1)
        prval () = exp2_isfun (pf, pf1)
        val bt = btree_btree_merge<a> (bt, bt1)
        val hp1 = hp1
        val () = free@{a}{0}{0}{0}{1}(hp)
      in
        btree_bheap_merge<a> (EXP2ind (pf) | bt, n+1, hp1)
      end // end of [if]
    end (* end of [bheap_cons] *)
// end of [btree_bheap_merge]

(* ****** ****** *)

fun{a:vt0p}
bheap_bheap_merge
  {n1,n2:nat}
  {sz1,sz2:nat} .<sz1+sz2>. (
  hp1: bheap (a, n1, sz1), hp2: bheap (a, n2, sz2)
) :<!wrt>
  [n:int | n >= min(n1, n2)] bheap (a, n, sz1+sz2) = let
in
//
case+ hp1 of
| ~bheap_nil () => hp2
| @bheap_cons (pf1 | bt1, hp11) => (
  case+ hp2 of
  | ~bheap_nil () => (fold@ (hp1); hp1)
  | @bheap_cons (pf2 | bt2, hp21) => let
//
      prval pf1 = pf1 and pf2 = pf2
      prval () = exp2_ispos (pf1) and () = exp2_ispos (pf2)
//
      val n1 = btree_rank<a> (bt1)
      and n2 = btree_rank<a> (bt2)
    in
      if n1 < n2 then let
        prval () = fold@ (hp2)
        val () = hp11 := bheap_bheap_merge<a> (hp11, hp2)
        prval () = fold@ (hp1)
      in
        hp1
      end else if n1 > n2 then let
        prval () = fold@ (hp1)
        val () = hp21 := bheap_bheap_merge<a> (hp1, hp21)
        prval () = fold@ (hp2)
      in
        hp2
      end else let
        prval () = exp2_isfun (pf1, pf2)
        val bt12 = btree_btree_merge<a> (bt1, bt2)
        val hp11 = hp11 and hp21 = hp21
        val () = free@{a}{0}{0}{0}{1}(hp1)
        val () = free@{a}{0}{0}{0}{1}(hp2)
      in
        btree_bheap_merge<a> (EXP2ind (pf1) | bt12, n1+1, bheap_bheap_merge<a> (hp11, hp21))
      end // end of [if]
    end (* end of [bheap_cons] *)
  ) // end of [bheap_cons]
end // end of [bheap_bheap_merge]

(* ****** ****** *)

fun{a:vt0p}
bheap_search_ref
  {n:nat}{sz:pos} .<>.
(
  hp0: !bheap (a, n, sz)
) :<> cPtr1(a) = let
//
fun search
  {n:nat}{sz:nat} .<sz>.
(
  hp0: !bheap (a, n, sz), p_x0: Ptr1
) :<> Ptr1 = let
in
//
case+ hp0 of
| @bheap_cons
    (pf | bt, hp) => let
    prval () = exp2_ispos (pf)
    val @btnode (_, x, _) = bt
    val (
      pf, fpf | p_x0
    ) = $UN.ptr_vtake{a}(p_x0)
    val sgn = compare_elt_elt<a> (!p_x0, x)
    prval () = fpf (pf)
    val res =
    (
      if sgn > 0 then search (hp, addr@(x)) else search (hp, p_x0)
    ) : Ptr1 // end of [val]
    prval () = fold@ (bt)
    prval () = fold@ (hp0)
  in
    res
  end // end of [bheap_cons]
| bheap_nil () => p_x0
//
end (* end of [search] *)
//
val+ @bheap_cons
  (pf0 | bt0, hp1) = hp0
val+ @btnode (_, x0, _) = bt0
prval () = fold@ (bt0)
val res = search (hp1, addr@(x0))
prval () = fold@ (hp0)
//
in
  $UN.ptr2cptr{a}(res)
end // end of [bheap_search_ref]

(* ****** ****** *)

fun{a:vt0p}
bheap_remove
  {n:nat}{sz:pos} .<>.
(
  hp0: &bheap (a, n, sz) >> bheap (a, n1, sz-p)
) :<!wrt> #[
  n1,n2,p:int | n1 >= n; n2 >= n; sz >= p
] (
  EXP2 (n2, p) | btree (a, n2)
) = let
//
// HX: [search] and [remove] can be merged into one
//
fun search
  {n:nat}{sz:nat} .<sz>. (
  hp0: !bheap (a, n, sz), x0: &a, pos: &Nat >> _
) :<!wrt> void = let
in
//
case+ hp0 of
| @bheap_cons
    (pf | bt, hp) => let
    prval () = exp2_ispos (pf)
    val+ @btnode (_, x, _) = bt
    val sgn = compare_elt_elt<a> (x0, x)
    val () =
      if sgn > 0 then let
      val p_x0 = addr@ (x0) and p_x = addr@ (x)
      val () = $UN.ptr0_set<a> (p_x0, $UN.ptr0_get<a>(p_x))
      val () = pos := pos + 1
    in
      // nothing
    end // end of [val]
    prval () = fold@ (bt)
    val () = search (hp, x0, pos)
    prval () = fold@ (hp0)
  in
    // nothing
  end // [bheap_cons]
| bheap_nil () => ()
//
end // end of [search]
//
val+ @bheap_cons
  (pf0 | bt0, hp1) = hp0
val+ @btnode (_, x, _) = bt0
val p_x = addr@ (x); prval () = fold@ {a} (bt0)
var x0: a = $UN.ptr0_get<a> (p_x) and pos: Nat = 0
val () = search (hp1, x0, pos)
prval () =
  __clear (x0) where {
  extern praxi __clear (x: &a >> a?): void
} (* end of [prval] *)
prval () = fold@ {a} (hp0)
//
fun remove
  {n:nat}{sz:nat}
  {pos:nat} .<pos>. (
  hp0: &bheap (a, n, sz) >> bheap (a, n1, sz-p)
, pos: int (pos)
, btmin: &btree(a, 0)? >> btree (a, n2)
) :<!wrt> #[
  n1,n2,p:int | n1 >= n; n2 >= n; sz >= p
] (
  EXP2 (n2, p) | void
) = let
//
  prval (
  ) = __assert () where
  {
    extern praxi __assert (): [sz > 0] void
  } // end of [prval]
//
  val+ @bheap_cons (pf | bt, hp) = hp0
//
  prval pf = pf
  prval () = exp2_ispos (pf)
in
//
if pos > 0 then let
  val (pfmin | ()) = remove (hp, pos-1, btmin)
  prval () = fold@ (hp0)
in
  (pfmin | ())
end else let
  val () = btmin := bt
  val hp = hp
  val () = free@{a}{0}{0}{0}{1}(hp0)
  val () = hp0 := hp
in
  (pf | ())
end // end of [if]
//
end (* end of [remove] *)
//
var btmin: btree (a, 0)?
val (pf | ()) = remove (hp0, pos, btmin)
//
in
  (pf | btmin)
end // end of [bheap_remove]

(* ****** ****** *)

assume
heap_vtype
  (a:vt0p) = [n,sz:nat] bheap (a, n, sz)
// end of [heap_vtype]

(* ****** ****** *)

implement{}
linheap_nil {a} () = bheap_nil{a}{0}()
implement{}
linheap_make_nil {a} () = bheap_nil{a}{0}()

(* ****** ****** *)

implement{}
linheap_is_nil (hp) = let
in
//
case+ hp of
| bheap_cons (_ | _, _) => false | bheap_nil () => true
//
end // end of [linheap_is_nil]

implement{}
linheap_isnot_nil (hp) = let
in
//
case+ hp of
| bheap_cons (_ | _, _) => true | bheap_nil () => false
//
end // end of [linheap_is_cons]

(* ****** ****** *)

implement{a}
linheap_size (hp) = $effmask_all (bheap_size<a> (hp))

(* ****** ****** *)

implement{a}
linheap_insert
  (hp0, x0) = let
  val bt = btnode{a}(0, x0, btlst_nil()) in
  hp0 := btree_bheap_merge<a> (EXP2bas () | bt, 0, hp0)
end // end of [linheap_insert]

(* ****** ****** *)

implement{a}
linheap_getmin_ref (hp0) = let
(*
val () = (
  print ("linheap_getmin_ref: enter"); print_newline ()
) // end of [val]
*)
in
//
case+ hp0 of
| bheap_cons
    (pf | _, _) => let
    prval (
    ) = exp2_ispos (pf)
  in
    bheap_search_ref<a> (hp0)
  end // end of [bheap_cons]
| bheap_nil ((*void*)) => cptr_null{a}((*void*))
//
end // end of [linheap_getmin_ref]

(* ****** ****** *)

implement{a}
linheap_delmin
  (hp0, res) = let
(*
val () = (
  print ("linheap_delmin: enter"); print_newline ()
) // end of [val]
*)
in
//
case+ hp0 of
| bheap_cons
    (pf0 | _, _) => let
    prval () = exp2_ispos (pf0)
    val (_(*pf*) | btmin) = bheap_remove<a> (hp0)
    val ~btnode (_, x, bts) = btmin
    val () = res := x
    prval () = opt_some{a}(res)
    val hp1 = hp1 where {
      fun loop
        {n:nat}{sz:nat} .<n>.
      (
        bts: btreelst (a, n), hp: bheap (a, n, sz)
      ) :<> [sz:nat] bheap (a, 0, sz) =
        case+ bts of
        | ~btlst_cons (bt, bts) => let
            prval pf = exp2_istot () in loop (bts, bheap_cons{a}(pf | bt, hp))
          end // end of [btlst_cons]
        | ~btlst_nil () => hp
      // end of [loop]
      val hp1 = loop (bts, bheap_nil)
    } // end of [val]
    val () = hp0 := bheap_bheap_merge<a> (hp0, hp1)
  in
    true
  end // end of [bheap_cons]
| bheap_nil () => let
    prval () = opt_none{a}(res) in false
  end // end of [bheap_nil]
// end of [case]
//
end // end of [linheap_delmin]

(* ****** ****** *)

implement{a}
linheap_merge
  (hp1, hp2) = bheap_bheap_merge<a> (hp1, hp2)
// end of [linheap_merge]

(* ****** ****** *)

implement{a}
linheap_free (hp0) = let
in
//
case+ hp0 of
| ~bheap_cons
    (_ | bt, hp) => let
    val () = btree_free<a> (bt) in linheap_free (hp)
  end // end of [bheap]
| ~bheap_nil () => ()
//
end // end of [linheap_free]

(* ****** ****** *)

implement{a}
linheap_free_ifnil
  (hp0) = let
  vtypedef hp = heap (a)
in
//
case+ hp0 of
| bheap_cons
    (_ | _, _) => let
    prval () = opt_some{hp}(hp0) in true
  end // end of [bheap_cons]
| bheap_nil () => let
    prval () = __assert (hp0) where
    {
      extern
      praxi __assert {n:int} (hp: !bheap (a, n, 0) >> ptr): void
    } // end of [prval]
    prval () = opt_none{hp}(hp0) in false
  end // end of [bheap_nil]
//
end // end of [linheap_free_vt]

(* ****** ****** *)

(* linheap_binomial.dats *)