--* From BMT%WATSON.vnet.ibm.com@yktvmh.watson.ibm.com  Wed Jun 23 22:23:42 1993
--* Received: from yktvmh.watson.ibm.com by radical.watson.ibm.com (AIX 3.2/UCB 5.64/900524)
--*           id AA19985; Wed, 23 Jun 1993 22:23:42 -0400
--* Received: from watson.vnet.ibm.com by yktvmh.watson.ibm.com (IBM VM SMTP V2R3)
--*    with BSMTP id 3736; Wed, 23 Jun 93 22:24:29 EDT
--* Received: from YKTVMH by watson.vnet.ibm.com with "VAGENT.V1.0"
--*           id <A.BMT.NOTE.VAGENT2.8985.Jun.23.22:24:29.-0400>
--*           for asbugs@watson; Wed, 23 Jun 93 22:24:29 -0400
--* Received: from YKTVMH by watson.vnet.ibm.com with "VAGENT.V1.0"
--*           id 8983; Wed, 23 Jun 1993 22:24:28 EDT
--* Received: from cyst.watson.ibm.com by yktvmh.watson.ibm.com (IBM VM SMTP V2R3)
--*    with TCP; Wed, 23 Jun 93 22:24:28 EDT
--* Received: from spadserv.watson.ibm.com by cyst.watson.ibm.com (AIX 3.2/UCB 5.64/900528)
--*   id AA38538; Wed, 23 Jun 1993 22:24:44 -0400
--* Received: by spadserv.watson.ibm.com (AIX 3.2/UCB 5.64/900524)
--*           id AA19627; Wed, 23 Jun 1993 22:27:12 -0400
--* Date: Wed, 23 Jun 1993 22:27:12 -0400
--* From: bmt@spadserv.watson.ibm.com
--* X-External-Networks: yes
--* Message-Id: <9306240227.AA19627@spadserv.watson.ibm.com>
--* To: asbugs@watson.ibm.com
--* Subject: errors point to macro def for Exports instead of true source of error [bugibits.as][28.I (current)]

--@ Fixed  by: SSD Wed Jul 28 18:25:43 1993
--@ Tested by: none
--@ Summary:   v29.3 error message pointing to true source of error


#include "aslib.as"
Object ==>
        =:        (%, %) -> Bit
        ~=:       (%, %) -> Bit
        apply:    (Outport, %) -> Outport

Logic ==>
        ~:        % -> %
        /\:       (%, %) -> %
        \/:       (%, %) -> %

Aggregate S ==>
        generator: % -> Generator S
        bracket:  Generator S -> %
        bracket:  Tuple S -> %
	#:        % -> SingleInteger
        map:      (S->S, %) -> %
	map:	  ((S,S)->S, %, %) -> %
        empty?:   % -> Bit

macro
  SI == SingleInteger
  Boolean == Bit
  BArr  == BuiltinArr
  BBit  == BuiltinBit
  BSInt == BuiltinSInt
  max(aa,bb) == if aa < bb then bb else aa

import String:with (string : Literal -> String)

(IndexedBits(mn:SI):Exports == Implementation) where
  Exports ==>
    with (Object; Aggregate(Bit); Logic; string:Literal -> % )
  Implementation ==>
    add
      Rep ==> Record(size: SI, nvalues:SI, values: BArr)

      import Builtin: with
	   ArrNew: (BBit, BSInt) -> BArr
	   ArrElt: (BArr, BSInt) -> BBit
	   ArrSet: (BArr, BSInt, BBit) -> BBit

      import Rep
      import Bit

      apply(v:%, i:SI): Bit == ArrElt(rep(v).values, (i-mn)::BSInt)::Bit
      set!(v:%, i:SI, val:Bit): Bit == ArrSet(rep(v).values, (i-mn)::BSInt, val::BBit)::Bit
      new(n:SI, val:Bit):% ==
          v := ArrNew(false::BBit, n::BSInt)
          for i in 0..n-1 repeat
               ArrSet(v, i::BSInt, val::BBit)
          per [n, n, v]
      extend!(u: %, val: Bit): None  ==
                r := rep u
                if r.nvalues = r.size then
                        nsize     := max(4, r.size + r.size quo 2)
                        v := ArrNew(false::BBit, nsize::BSInt)
                        oldv := r.values
                        for i in 0..r.nvalues-1 repeat
                             ArrSet(v, i::BSInt, ArrElt(oldv, i::BSInt))
                        r.values  := v
                        r.size    := nsize
                r.nvalues := r.nvalues+1
                ArrSet(r.values, (r.nvalues-1)::BSInt, val::BBit)

      empty?(v:%):Bit == #v=0
      empty(n:SI):% == per [n, 0, ArrNew(false::BBit, n::BSInt)]
      minIndex (u:%):SI == mn
      range(v:%,i:SI):SI ==
        import Boolean
        not (i < 0) and i < #v => i
        error "Index out of range"
--      coerce (v:%):OutputForm ==
--        import Character
--        import String
--        import StringCategory
--        t:Character := char "1"
--        f:Character := char "0"
--        s := new(#v,space()$Character)$String
--        for i in minIndex s..maxIndex s for j in mn.. repeat
--          (s i := (v j => t; f))
--        s::OutputForm
      empty():% == new(0,false)
      copy (v:%):% ==
          [vv for vv in v]
      #(v:%):SI == rep(v).nvalues
      (v:% = u:%):Boolean ==
          #v ~= #u => false
          for vv in v for uu in u repeat
              if vv ~= uu then return false
          true
      (v:% < u:%):Boolean ==
          for vv in v for uu in u repeat
             uu ~= vv =>
                uu = false => return true
                return false
          #v < #u
      (u:% /\ v:%):% ==
        [uu and vv for uu in u for vv in v]
      (u:% \/ v:%):% ==
        [uu or vv for uu in u for vv in v]
      xor(v:%,u:%):% ==
        [uu ~= vv for uu in u for vv in v]
      ~(v:%):% == [not vv for vv in v]

      generator(v: %): Generator Bit == generate
           for i in mn..#v+mn-1 repeat yield v.i

      [it: Generator Bit]: % ==
                w := empty()
                for t in it repeat extend!(w, t)
                w

      [t: Tuple Bit]: % ==
                w := empty #t
                for i in 1..#t repeat extend!(w, t.i)
                w

      string(l:Literal):% ==
         import SingleInteger
         s:String := string l
         z:Character := "0".1
         [ss ~= z for ss in s]

      apply(p: Outport, v: %): Outport ==
                q:String := "_""
                p(q)
                for i in 1..#v repeat p(v.i)
                p(q)


Bits:with (Object; Aggregate(Bit); Logic; string:Literal -> % ) ==
   IndexedBits(1$SI)



f ():None ==
  import Bit
  import Outport
  vvv:Bits:=[true,false,false]
  print(vvv)()
  print("010101" /\ "101111")()


f()
 
