Previous Up Next

Module Pp

The implementation of the pretty-printer is rather straightforward: each syntactic constituent of the transducer has its output function. The recursive functions are in most cases self-explanatory. For efficiency’s sake, the pretty-printer does not directly produce strings but an abstract output of type Layout.aseq, provided by the module Layout.3


module S = Symbol

module AS = Absynt 

type abstract_layout = Layout.aseq

let layout_qindex (i) = Layout.aStr(string_of_int i)

let layout_qstarindex (alpha) = 
   Layout.aConcat 
     [Layout.aStr "[";
       Layout.aInterleave (Layout.aStr(" ")) (List.map (layout_qindexalpha);
       Layout.aStr "]"]

let layout_pname (ppname) = 
   match p with (s,i) → Layout.aStr("y_" ^ (string_of_int i))
let layout_vname (vvname) = 
   match v with (s,i) → Layout.aStr("x_" ^ (string_of_int i))

let layout_state (layout_index : (α → abstract_layout)) (q: α AS.state) =
   match q with
     Q i → 
       Layout.aConcat 
         [Layout.aStr "q_";
           (layout_index i)]

let rec layout_term (layout_index: α → abstract_layoutt =
       match t with
         AS.V (x,i) → Layout.aStr("x_" ^ string_of_int(i))
       ∣ AS.P (y,i) → Layout.aStr("y_"^string_of_int(i))
       ∣ AS.T (nargs) →
           let sep = Layout.aStr(", ")
           in
           Layout.aConcat [ Layout.aStr(S.string_of_symbol(n));
                             (Layout.aInterleave_with_par
                               sep 
                               (Layout.aStr("("))
                               (Layout.aStr(")"))
                               (List.map (layout_term layout_indexargs))
                           ]
       ∣ AS.QT(q,arg,args) → 
           Layout.aConcat [ (layout_state layout_index)(q);
                       Layout.aStr("(");
                       layout_term layout_index arg;
                       Layout.aStr(")");
                       (Layout.aInterleave_with_par
                             (Layout.aStr(","))
                             (Layout.aStr("<"))
                             (Layout.aStr(">"))
                             (List.map (layout_term layout_indexargs)) ]

let layout_qarity (layout_index: α → abstract_layout) (a: α × int) =
   Layout.aConcat [ Layout.aStr("(");
                     ((layout_state layout_index) (AS.Q(fst a)));
                     Layout.aStr(",");
                     Layout.aStr(string_of_int (snd a));
                     Layout.aStr(")")
                   ] 

let layout_sarity ((s,i) : (symbol × int)) =
   Layout.aConcat [ Layout.aStr("(");
                     Layout.aStr(S.string_of_symbol s);
                     Layout.aStr(",");
                     Layout.aStr(string_of_int i);
                     Layout.aStr(")")
                   ]

let layout_rule layout_index (r_nondet: α rule) = 
   match r_nondet with
     (lhs,rhs) →
       Layout.aConcat [ layout_term layout_index lhs;
                         Layout.aStr(" -> ");
                         layout_term layout_index rhs
                       ]

let layout (layout_index: α → abstract_layout) (t: α mtt) =
   match t with 
     (namearslislrl) → 
       Layout.aConcat [ Layout.aStr("TT ");
                         Layout.aStr(S.string_of_symbol name);
                         Layout.aStr(" = ");
                         Layout.aStr("[");
                         Layout.aIndent 
                           (Layout.aConcat [
                           Layout.aNewline;
                           Layout.aStr("state arities    = ");
                           Layout.aStr("{");
                           Layout.aInterleave 
                             (Layout.aStr(","))
                             (List.map (layout_qarity layout_index) (fst ar));
                           Layout.aStr"}";
                           Layout.aNewline;
                           Layout.aStr("symbols’ arities = ");
                           Layout.aStr("{");
                           Layout.aInterleave 
                             (Layout.aStr(","))
                             (List.map layout_sarity (snd ar));
                           Layout.aStr"}";
                           Layout.aNewline;
                           Layout.aStr("states           = ");
                           Layout.aStr("{");
                           Layout.aInterleave 
                             (Layout.aStr(","))
                             (List.map (layout_state layout_indexsl);
                           Layout.aStr"}";
                           Layout.aNewline;
                           Layout.aStr("initial states   = ");
                           Layout.aStr("{");
                           Layout.aInterleave 
                             (Layout.aStr(","))
                             (List.map (layout_state layout_indexisl);
                           Layout.aStr"}";
                           Layout.aNewline;
                           Layout.aStr("------------------------");
                           Layout.aNewline;
                           Layout.aStr("rules  =  ");
                           (Layout.aIndent 
                               (Layout.aInterleave 
                                 Layout.aNewline
                                 (List.map (layout_rule layout_indexrl)));
                           Layout.aNewline]);
                         Layout.aStr ("]");
                         Layout.aNewline
                       ]

let string_of_layout a = Layout.aDisplay(a)


Previous Up Next