fmod PL-PRINT is
  inc MY-META-ALL .
  inc STRING .
  inc CONVERSION .
  inc META-PETRI .

****************************     opdecls     **************************** 
**** printing rule ids
*** print  up to first dot in ddd, or all if no dot
  op printRid : Qid -> String .

**** ridT has form ''ddd.Qid  maybe ddd has no dot
*** print  up to first dot in ddd, or all if no dot
*** if PNRidSuffix occurs, then what follows is rule instance index n
*** add -n to printed id
  op printRidT : Term -> String .

**** print the full id (just drop the single quote)
  op printRidTAll : Term -> String .

**** applies printRidTall to each term in the list (with blank separator)
  op printRidTL : TermList -> String .

**** printing an occ termlist, space separated, full occ name (occ2label)
  op printOccTL : Module TermList -> String .

**** prints occ as  thing - loc
  op occ2label : Module Term -> String .
**** prints occ without loc
  op occ2labelx : Module Term -> String .

**** prints location name -- assume constants, ow print as "?"
  op printLoc : Module Term -> String .

******** printing things
  op printThing : Module Term -> String .

**** separator for rule instance count, currently #
  op PNRidSuffix : -> String .

****************************   end opdecls    **************************** 
  vars rid : Qid .
  vars rn prn sfx : String .
  vars res res1 : FindResult .
  vars ridT locnameT strT thC : Constant .
  var thV : Variable .
  var mod : Module .
  vars occT thT lT pT msT t thT0 thT1 thT2 tsoupT : Term .
  var occTL tl : TermList .
  vars res?? res0?? res1?? : [ResultPair?] .

**** printing rule ids
**** print  up to first dot in ddd, or all if no dot
****  op printRid : Qid -> String .
  eq printRid(rid) =
       (if (find(string(rid) , ".",0) :: Nat) 
        then substr(string(rid) ,0,find(string(rid) , ".",0))  
        else string(rid) fi) .

****  op PNRidSuffix : -> String .
  eq PNRidSuffix = "#" .

**** ridT has form ''ddd.Qid  maybe ddd has no dot
**** print  up to first dot in ddd, or all if no dot
**** if PNRidSuffix occurs, then what follows is rule instance index n
**** add -n to printed id
****  op printRidT : Term -> String .
  ceq printRidT(ridT) =
       prn + sfx     
  if rn := string(getName(ridT)) 
  /\ res := find(rn, ".", 0) 
  /\ prn := substr(rn, 1, 
                   ( if res :: NzNat then sd(res,1) else length(rn) fi))
  /\ res1 := rfind(rn, PNRidSuffix, length(rn)) 
  /\ sfx := (if res1 :: Nat
             then "-" + substr(rn,s res1, length(rn)) 
             else "" fi) .

**** print the full id (just drop the single quote)
****  op printRidTAll : Term -> String .
  eq printRidTAll(ridT) = 
       substr(string(getName(ridT)), 1,length(string(getName(ridT)))) .

****  op printRidTL : TermList -> String .
  eq printRidTL(empty) = "" .
  eq printRidTL((ridT, tl)) = printRidTAll(ridT) + " " + printRidTL(tl) .

*** turning occs into strings
*** module must include DISH-OPS  (define Occ, Loc)

****  op printOccTL : Module TermList -> String .
  eq printOccTL(mod,empty) = "" .
  eq printOccTL(mod,(occT,occTL)) = 
       occ2label(mod,occT) + " " + printOccTL(mod,occTL) .

**** prints occ as  thing - loc
****  op occ2label : Module Term -> String .
  eq occ2label(mod, occT) =
      (if isOccT(occT)
       then printThing(mod,occT2thT(occT))  
                       + "-" +  printLoc(mod, occT2locT(occT)) 
       else "" fi) .

**** prints occ without loc
****  op occ2labelx : Module Term -> String .
  eq occ2labelx(mod, occT) =
        (if isOccT(occT) then printThing(mod,occT2thT(occT)) else "" fi) .

**** prints location name -- assume constants, ow print as "?"
****  op printLoc : Module Term -> String .

  eq printLoc(mod,'newL[strT]) =  stringT2String(strT) .
  eq printLoc(mod,locnameT) =  string(getName(locnameT)) .
  eq printLoc(mod,lT) = "?" [owise]  .

**** printing things
**** ops printThing  : Module Term -> String .
  ops printComplex printModified printModSet : Module Term -> String .
  op printModList : Module TermList -> String .

 eq printThing(mod,thC) = string(getName(thC)) .
 eq printThing(mod,thV) = "?" + string(getType(thV)) .
**** eq printThing(mod,'_:_[thT1,thT2]) = 
****      "(" + printThing(mod,thT1) + ":" + printThing(mod,thT2) + ")" . 
**** allow for assoc 
**** 07apr08 drop parens -- makes sending chattylabels to maude weird
**** eq printThing(mod,'_:_[tl]) = "(" + printThingSoupL(mod,tl) + ")" . 
 eq printThing(mod,'_:_[tl]) =  printThingSoupL(mod,tl)  . 

 eq printThing(mod,'<_>[tsoupT]) =  "<" + printTSoup(mod,tsoupT) + ">" .
 eq printThing(mod,'`[_-_`][pT,msT]) = 
      printThing(mod,pT) + "-" + printModSet(mod,msT) .
 eq printThing(mod,'newC[strT]) = stringT2String(strT) .

eq printThing(mod,thT) = "???" +
   (if metaPrettyPrint(mod,thT) :: QidList
   then qidl2strx(metaPrettyPrint(mod,thT))
   else "*" fi)    [owise] .

  op qidl2strx : QidList -> String .
  eq qidl2strx(nil) = "" .
  eq qidl2strx(q:Qid) = string(q:Qid) .
  eq qidl2strx(q:Qid q':Qid ql:QidList) = 
         string(q:Qid) + "%" + qidl2strx(q':Qid ql:QidList) .


***!!! NEWCOMPLEX

  op printTSoup : Module Term -> String .
  op printThingL : Module TermList -> String .
  op printThingSoupL : Module TermList -> String .

****  eq printComplex(mod,'<_>[tsoupT]) = "<" + printTSoup(mod,tsoupT) + ">" .
****  assume tsoupT is soup of things possibly not normalized
  eq printTSoup(mod,tsoupT) =
      (if tsoupT == 'empty.Soup
       then " "
       else (if getOp(tsoupT) =/= '__ **** hasSort(mod,tsoupT,'Thing)
             then printThing(mod,tsoupT)
             else printThingSoupL(mod,getArgs(tsoupT))
             fi)
       fi) .

****  eq printTSoup(mod,tsoupT) = "" [owise] .  **** just in case

**** terms are things or thing soups
  eq printThingSoupL(mod, empty) = "" .
  eq printThingSoupL(mod, (t, tl)) = 
        (if getOp(t) == '__    **** a soup
         then printThingSoupL(mod,getArgs(t)) 
         else printThing(mod,t)
         fi) 
        + (if tl == empty then "" else ":" + printThingSoupL(mod,tl) fi) .


  eq printThingL(mod, empty) = "" .
  eq printThingL(mod, (thT, tl)) = 
        (if  hasSort(mod,thT,'Thing)
         then printThing(mod,thT)
         else printThingL(mod,getArgs(thT)) fi) 
        + (if tl == empty then "" else ":" + printThingL(mod,tl) fi) .

****!!! END NEWCOMPLEX

****  eq printModified(mod,'`[_-_`][pT,msT]) = 
****       printThing(mod,pT) + "-" + printModSet(mod,msT) + "" .

***(
Modification is
  constant
  op[site]
site is '__[aa,n]
)

  eq  printModSet(mod,m:Constant) = string(getName(m:Constant)) .
  eq  printModSet(mod,op:Qid[tl]) =
        (if (op:Qid == '__)
         then printModList(mod,tl) 
***        else (if (tl :: Constant)
***              then string(op:Qid) + "(" + string(getName(tl)) + ")"
***              else "" fi)
***      else printOpTerm(op:Qid, tl)
         else (if (op:Qid == 'num)
               then "num" + "(" + printNat(first(tl)) + ")" 
               else printSiteMod(op:Qid, tl)
          fi) fi) .

  op printSiteMod : Qid TermList -> String .
  eq printSiteMod(op:Qid, t) =
      string(op:Qid) + "-" + printSite(t) .

  op printSite : Term -> String .
  eq printSite(c:Constant) = string(getName(c:Constant)) .
  eq printSite('__[c:Constant,nT:Term]) =
        string(getName(c:Constant)) + printNat(nT:Term).

  op printNat : Term -> String .
  eq printNat(t) = string(downTerm(t,0),10) .

**** Not used ??
  op printOpTerm : Qid TermList -> String .
  op printTerm : Term -> String .
  op printTermList : TermList -> String .

  eq printOpTerm(op:Qid, tl) = 
       string(op:Qid) + "(" + printTermList(tl) + ")" .

  eq printTerm(t) =
        (if (t :: Variable) 
         then "?" + string(getName(t))
         else (if (t :: Constant)
               then  string(getName(t))
               else printOpTerm(getOp(t),getArgs(t))
               fi ) fi ) .

  eq printTermList(empty) =  "" .
                
  eq printTermList((t,tl)) =
      printTerm(t) + (if tl == empty then "" else "," + printTermList(tl) fi) . 

  eq  printModList(mod,empty) = "" .
  eq  printModList(mod,(t, tl)) =
        printModSet(mod,t) +  
          (if tl == empty then "" else " " + printModList(mod,tl) fi) .

  op stringT2String : Term -> String .
  eq stringT2String(t) = 
     if (t :: Constant) and getType(t) == 'String
     then substr(string(getName(t)),1,sd(length(string(getName(t))),2))
     else "" fi .

endfm


