


## latex-print-type.pkg
## Copyright 2003 by The SML/NJ Fellowship
# Compiled by:
# src/lib/compiler/front/typer/typer.sublib# modified to use Lib7 Lib pp. [dbm, 7/30/03])
api Latex_Print_Type {
type_formals
:
Int
-> List( String );
typevar_ref_printname
:
types::Typevar_Ref
-> String;
latex_print_type
:
symbolmapstack::Symbolmapstack
-> prettyprint::Stream
-> types::Typ
-> Void;
latex_print_type_scheme
:
symbolmapstack::Symbolmapstack
-> prettyprint::Stream
-> types::Type_Scheme
-> Void;
latex_print_some_type
:
symbolmapstack::Symbolmapstack
-> prettyprint::Stream
-> types::Type
-> Void;
latex_print_enum_constructor_domain
:
((Vector( types::Datatype_Member ), List( types::Typ )) )
-> symbolmapstack::Symbolmapstack
-> prettyprint::Stream
-> types::Type
-> Void;
latex_print_enum_constructor_types
:
symbolmapstack::Symbolmapstack
-> prettyprint::Stream
-> types::Typ
-> Void;
reset_latex_print_type
:
Void -> Void;
latex_print_formals
:
prettyprint::Stream
-> Int
-> Void;
debugging: Ref( Bool );
unalias: Ref( Bool );
};
stipulate
package sp = symbol_path; # symbol_path is from src/lib/compiler/front/typer-stuff/basics/symbol-path.pkg package ip = inverse_path; # inverse_path is from src/lib/compiler/front/typer-stuff/basics/symbol-path.pkg package bt = type_types; # type_types is from src/lib/compiler/front/typer/types/type-types.pkg package ty = types; # types is from src/lib/compiler/front/typer-stuff/types/types.pkg package tu = type_junk; # type_junk is from src/lib/compiler/front/typer-stuff/types/type-junk.pkg package pp = prettyprint; # prettyprint is from src/lib/prettyprint/big/src/prettyprint.pkg #
# include types;
include unparse_junk;
herein
package latex_print_type
: (weak) Latex_Print_Type
{
debugging = REF FALSE;
unalias = REF TRUE;
#
fun bug s
=
error_message::impossible ("latex_print_type: " + s);
pps = pp::string;
#
fun by f x y
=
f y x;
internals
=
typer_control::internals;
unit_path
=
ip::extend
(
ip::empty,
symbol::make_type_symbol "Void"
);
#
fun bound_type_variable_name k
=
{ a = char::to_int 'a';
if (k < 26)
string::from_char (char::from_int (k+a));
else
implode [ char::from_int (int::(/) (k, 26) + a),
char::from_int (int::(%) (k, 26) + a)
];
fi;
};
#
fun meta_tyvar_name' k
=
{ a = char::to_int 'Z'; # use reverse order for meta vars
if (k < 26)
string::from_char (char::from_int (a - k));
else
implode [ char::from_int (a - (int::(/) (k, 26))),
char::from_int (a - (int::(%) (k, 26)))
];
fi;
};
#
fun type_formals n
=
{ fun loop i
=
if (i>=n)
[];
else
(bound_type_variable_name i) ! loop (i + 1);fi;
loop 0;
};
#
fun literal_kind_printname (lk: ty::Literal_Kind)
=
case lk
#
ty::INT => "Int"; # or "INT"
ty::UNT => "Unt"; # or "UNT"
ty::FLOAT => "Float"; # or "FLOAT"
ty::CHAR => "Char"; # or "CHAR"
ty::STRING => "String"; # or "STRING"
esac;
stipulate # WARNING -- compiler global variables
count = REF (-1);
meta_tyvars = REF([]: List( ty::Typevar_Ref ));
herein
fun meta_tyvar_name ((tv as { id, ref_typevar }): ty::Typevar_Ref)
=
meta_tyvar_name' (find (*meta_tyvars, 0))
where
fun find ([], _)
=>
{ meta_tyvars := tv ! *meta_tyvars;
count := *count+1;
*count;
};
find ((tv' as { id, ref_typevar => ref_typevar' }) ! rest, k)
=>
ref_typevar == ref_typevar' ?? *count - k
:: find (rest, k+1);
end;
end;
#
fun reset_latex_print_type ()
=
{ count := -1;
meta_tyvars := [];
};
end;
# This function used to add a "'" prefix to vanilla type variables
# and a "''" prefix to equality type variables, producing the forms
# 'a 'b ... and ''a ''b ... respectively.
# Our current convention is to use X Y Z ... for vanilla type variables
# (and to try to ignore equality type variables...):
#
fun decorated_type_variable_name (eq, base)
=
# if eq "''";
# else "'";
# fi
# +
# base;
if eq "''" + base;
else
case base
"a" => "X";
"b" => "Y";
"c" => "Z";
"d" => "A";
"e" => "B";
"f" => "C";
"g" => "D";
"h" => "E";
"i" => "F";
"j" => "G";
"k" => "H";
"l" => "I";
"m" => "J";
"n" => "K";
"o" => "L";
"p" => "M";
"q" => "N";
"r" => "O";
"s" => "P";
"t" => "Q";
"u" => "R";
"v" => "S";
"w" => "T";
"x" => "U";
"y" => "V";
"z" => "W";
x => "A_" + x;
esac;
fi;
#
fun annotate (name, annotation, depth_op)
=
if *internals
cat ( name
! "."
! annotation
! case depth_op
THE depth => ["[", (int::to_string depth), "]"];
NULL => NIL;
esac
);
else
name;
fi;
#
fun typevar_ref_printname (tv as { id, ref_typevar })
=
pr_kind *ref_typevar
where
fun pr_kind info
=
case info
#
ty::RESOLVED_TYPE_VARIABLE (ty::TYPE_VARIABLE_REF (tv as { id, ref_typevar => _ }) )
=>
typevar_ref_printname tv
# +
# (sprintf "[id%d]" id) # We probably don't want this for latex printing...?
;
ty::RESOLVED_TYPE_VARIABLE _
=>
"<RESOLVED_TYPE_VARIABLE ?>";
ty::META_TYPE_VARIABLE { fn_nesting, eq }
=>
decorated_type_variable_name (eq, annotate ( meta_tyvar_name tv,
"META",
THE fn_nesting
)
);
ty::INCOMPLETE_RECORD_TYPE_VARIABLE { fn_nesting, eq, known_fields }
=>
decorated_type_variable_name (eq, annotate ( meta_tyvar_name tv,
"INCOMPLETE_RECORD",
THE fn_nesting
)
);
ty::USER_TYPE_VARIABLE { name, fn_nesting, eq }
=>
decorated_type_variable_name (eq, annotate (symbol::name name, "USER", THE fn_nesting));
ty::LITERAL_TYPE_VARIABLE { kind, ... }
=>
annotate (literal_kind_printname kind, "L", NULL);
ty::OVERLOADED_TYPE_VARIABLE eq
=>
decorated_type_variable_name (eq, annotate (meta_tyvar_name tv, "OVERLOADED", NULL));
ty::TYPE_VARIABLE_MARK _ => "<TYPE_VARIABLE_MARK ?>";
esac;
end;
/*
fun ppkind stream kind
=
pps stream
(case kind
of BASE _ => "BASE"
| FORMAL => "FORMAL"
| ty::FLEXIBLE_TYP _ => "FLEXIBLE_TYP"
| ty::ABSTRACT _ => "ABSTYC"
| ty::DATATYPE _ => "DATATYPE"
| ty::TEMP => "TEMP"
)
*/
#
fun ppkind stream kind
=
pps stream
#
case kind
#
ty::BASE _ => "P";
ty::FORMAL => "F";
ty::FLEXIBLE_TYP _ => "X";
ty::ABSTRACT _ => "A";
ty::DATATYPE _ => "D";
ty::TEMP => "T";
esac;
#
fun effective_path (path, typ, symbolmapstack) : String
=
{ fun typ_path ( ty::PLAIN_TYP { path, ... }
| ty::DEFINED_TYP { path, ... }
| ty::TYP_BY_STAMPPATH { path, ... }
)
=>
THE path;
typ_path _
=>
NULL;
end;
#
fun find (path, typ)
=
(find_path (path,
(fn typ' = tu::typ_equality (typ', typ)),
(fn x = find_in_symbolmapstack::find_typ_via_symbol_path (symbolmapstack, x,
(fn _ = raise exception symbolmapstack::UNBOUND))))
);
#
fun search (path, typ)
=
{ my (suffix, found)
=
find (path, typ);
if found
(suffix, TRUE);
else
if (not *unalias)
#
(suffix, FALSE);
else
case (tu::unwrap_definition_1 typ)
#
THE typ'
=>
case (typ_path typ')
#
THE path'
=>
{ my x as (suffix', found')
=
search (path', typ');
if found' x;
else (suffix, FALSE);
fi;
};
NULL => (suffix, FALSE);
esac;
NULL => (suffix, FALSE);
esac;
fi;
fi;
};
my (suffix, found)
=
search (path, typ);
name = sp::to_string (sp::SYMBOL_PATH suffix);
if found name;
else "?." + name;
fi;
};
arrow_stamp = bt::arrow_stamp;
#
fun strength (type)
=
case type
#
ty::TYPE_VARIABLE_REF { id, ref_typevar => REF (ty::RESOLVED_TYPE_VARIABLE type') }
=>
strength (type');
ty::TYPCON_TYPE (typ, args)
=>
case typ
#
ty::PLAIN_TYP { stamp, kind => ty::BASE _, ... }
=>
if (stamp::same_stamp (stamp, arrow_stamp)) 0;
else 2;
fi;
ty::RECORD_TYP (_ ! _) # excepting type Void
=>
if (tuples::is_tuple_typ (typ)) 1;
else 2;
fi;
_ => 2;
esac;
_ => 2;
esac;
#
fun prettyprint_eq_prop stream p
=
{ a = case p
#
ty::eq_type::NO => "NO";
ty::eq_type::YES => "YES";
ty::eq_type::INDETERMINATE => "INDETERMINATE";
ty::eq_type::CHUNK => "CHUNK";
ty::eq_type::DATA => "DATA";
ty::eq_type::EQ_ABSTRACT => "EQ_ABSTRACT";
ty::eq_type::UNDEF => "UNDEF";
esac;
pps stream a;
};
#
fun prettyprint_inverse_path ppstream (inverse_path::INVERSE_PATH inverse_path: inverse_path::Inverse_Path)
=
pp::string ppstream (symbol_path::to_string (symbol_path::SYMBOL_PATH (reverse inverse_path)));
#
fun latex_print_type' symbolmapstack stream members_op
=
latex_print_type''
where
my { begin_horizontal_else_vertical_box, begin_wrap_box, end_box, pps, break, ... }
=
en_pp stream;
#
fun latex_print_type'' (typ as ty::PLAIN_TYP { path, stamp, eqtype_info, kind, ... } )
=>
if *internals
#
begin_wrap_box 1;
prettyprint_inverse_path stream path;
pps "[";
pps "G"; ppkind stream kind; pps ";";
pps (stamp::to_short_string stamp);
pps ";";
prettyprint_eq_prop stream *eqtype_info;
pps "]";
end_box ();
else
pps (effective_path (path, typ, symbolmapstack));
fi;
latex_print_type'' (typ as ty::DEFINED_TYP { path, type_scheme => ty::TYPE_SCHEME { body, ... }, ... } )
=>
if *internals
#
begin_wrap_box 1;
prettyprint_inverse_path stream path;
pps "["; pps "D;";
latex_print_some_type symbolmapstack stream body;
pps "]";
end_box();
else
pps (effective_path (path, typ, symbolmapstack));
fi;
latex_print_type'' (ty::RECORD_TYP labels)
=>
{
unparse_closed_sequence
stream
{ front=> by pp::string "{",
sep => fn stream = { pp::string stream ", ";
pp::break stream { spaces=>0, indent_on_wrap=>0 };
},
back => by pp::string "}",
style=> INCONSISTENT,
pr => unparse_symbol
}
labels;
};
latex_print_type'' (ty::RECURSIVE_TYPE n)
=>
{
case members_op
THE (members, _)
=>
{ my { typ_name, constructor_list, ... }
=
vector::get (members, n);
unparse_symbol stream typ_name;
};
NULL
=>
pps (string::cat ["<RECURSIVE_TYPE ", int::to_string n, ">"]);
esac;
};
latex_print_type'' (ty::FREE_TYPE n)
=>
case members_op
THE (_, free_typs)
=>
{ typ
=
( list::nth (free_typs, n)
except _
=
bug "unexpected free_typs in prettyprintTypeConstructor"
);
latex_print_type'' typ;
};
NULL
=>
pps (string::cat ["<FREE_TYPE ", int::to_string n, ">"]);
esac;
latex_print_type'' (typ as ty::TYP_BY_STAMPPATH { arity, stamppath, path } )
=>
if *internals
begin_wrap_box 1;
prettyprint_inverse_path stream path; pps "[P;";
pps (stamppath::stamppath_to_string stamppath);
pps "]";
end_box ();
else
prettyprint_inverse_path stream path;
fi;
latex_print_type'' ty::ERRONEOUS_TYP
=>
pps "[E]";
end;
end
also
fun latex_print_some_type1
symbolmapstack
stream
( type: ty::Type,
an_api: ty::Type_Scheme_Arg_Eq_Properties,
members_op: Null_Or( (Vector( ty::Datatype_Member ), List( ty::Typ )) )
)
: Void
=
{ my { begin_horizontal_else_vertical_box, begin_wrap_box, end_box, pps, break, newline }
=
en_pp stream;
#
fun prty type
=
case type
#
ty::TYPE_VARIABLE_REF { id, ref_typevar => REF (ty::RESOLVED_TYPE_VARIABLE type') }
=>
prty type';
ty::TYPE_VARIABLE_REF tv
=>
latex_print_type_variable tv;
ty::TYPE_SCHEME_ARG_I n
=>
{ eq = list::nth (an_api, n)
except
(SUBSCRIPT|INDEX_OUT_OF_BOUNDS) = FALSE;
pps (decorated_type_variable_name (eq, (bound_type_variable_name n)));
};
ty::TYPCON_TYPE (typ, args)
=>
{ fun otherwise ()
=
{ begin_wrap_box 2;
latex_print_type' symbolmapstack stream members_op typ;
case args
#
[] => ();
_ => { pps "(";
break { spaces=>0, indent_on_wrap=>0 };
latex_print_type_args args;
pps ")";
};
esac;
end_box();
};
case typ
#
ty::PLAIN_TYP { stamp, kind, ... }
=>
case kind
#
ty::BASE _
=>
if (stamp::same_stamp (stamp, arrow_stamp))
#
case args
#
[domain, range]
=>
{ begin_horizontal_else_vertical_box 0;
if (strength domain == 0)
#
begin_horizontal_else_vertical_box 1;
pps "(";
prty domain;
pps ")";
end_box();
else
prty domain;
fi;
break { spaces=>1, indent_on_wrap=>0 };
pps "-> ";
prty range;
end_box();
};
_ => bug "TYPCON_TYPE: arity";
esac;
else
begin_wrap_box 2;
latex_print_type' symbolmapstack stream members_op typ;
case args
#
[] => ();
_ => { pps "(";
break { spaces=>0, indent_on_wrap=>0 };
latex_print_type_args args;
pps ")";
};
esac;
end_box();
fi;
_ => otherwise ();
esac;
ty::RECORD_TYP labels
=>
if (tuples::is_tuple_typ typ) prettyprint_tuplety args;
else prettyprint_recordty (labels, args);
fi;
_ => otherwise ();
esac;
};
ty::TYPE_SCHEME_TYPE { type_scheme_arg_eq_properties => an_api,
#
type_scheme => ty::TYPE_SCHEME { arity, body }
}
=>
latex_print_some_type1 symbolmapstack stream (body, an_api, members_op);
ty::WILDCARD_TYPE
=>
pps "_";
ty::UNDEFINED_TYPE
=>
pps "undef";
esac
also
fun latex_print_type_args []
=>
();
latex_print_type_args [type]
=>
{ if (strength type <= 1)
begin_wrap_box 1;
pps "(";
prty type;
pps ")";
end_box();
else
prty type;
fi;
break { spaces=>1, indent_on_wrap=>0 };
};
latex_print_type_args tys
=>
unparse_closed_sequence
stream
{ front => by pp::string "(",
sep => fn stream = { pp::string stream ", ";
pp::break stream { spaces=>0, indent_on_wrap=>0 };
},
back => by pp::string ") ",
style => INCONSISTENT,
pr => fn _ = fn type = prty type
}
tys;
end
also
fun prettyprint_tuplety []
=>
pps (effective_path (unit_path, ty::RECORD_TYP [], symbolmapstack));
prettyprint_tuplety tys
=>
{ pps "(";
unparse_sequence
stream
{ sep => fn stream = { pp::break stream { spaces=>1, indent_on_wrap=>0 };
pp::string stream ", "; # Was "* "
},
style => INCONSISTENT,
pr => (fn _ = fn type = if (strength type <= 1)
begin_wrap_box 1;
pps "(";
prty type;
pps ")";
end_box ();
else
prty type;
fi
)
}
tys;
pps ")";
};
end
also
fun prettyprint_field (lab, type)
=
{ begin_horizontal_else_vertical_box 0;
unparse_symbol stream lab;
pps ":";
prty type;
end_box ();
}
also
fun prettyprint_recordty ([],[])
=>
pps (effective_path (unit_path, ty::RECORD_TYP [], symbolmapstack));
# this case should not occur
prettyprint_recordty (lab ! labels, arg ! args)
=>
{ begin_wrap_box 1;
pps "{";
prettyprint_field (lab, arg);
paired_lists::apply
(fn field'
=
{ pps ", ";
break { spaces=>1, indent_on_wrap=>0 };
prettyprint_field field';
}
)
(labels, args);
pps "}";
end_box ();
};
prettyprint_recordty _
=>
bug "latex_print_type::prettyprintRECORDty";
end
also
fun latex_print_type_variable (tv as { id, ref_typevar => (ref_info as REF info) }: ty::Typevar_Ref): Void
=
{ printname = typevar_ref_printname tv;
#
case info
#
ty::INCOMPLETE_RECORD_TYPE_VARIABLE { fn_nesting, eq, known_fields }
=>
case known_fields
#
[] =>
{ pps "{";
pps printname;
pps "}";
};
field' ! fields
=>
{ begin_wrap_box 1;
pps "{";
prettyprint_field field';
apply (fn x = { pps ", ";
break { spaces=>1, indent_on_wrap=>0 };
prettyprint_field x;
}
)
fields;
pps ";";
break { spaces=>1, indent_on_wrap=>0 };
pps printname;
pps "}";
end_box ();
};
esac;
_ => pps printname;
esac;
};
prty type;
} # prettyprintType1
also
fun latex_print_some_type (symbolmapstack: symbolmapstack::Symbolmapstack) stream (type: ty::Type) : Void
=
{ pp::begin_indented_wrap_box stream (pp::CURSOR_RELATIVE 1);
latex_print_some_type1 symbolmapstack stream (type,[], NULL);
pp::end_box stream;
};
#
fun latex_print_enum_constructor_domain members (symbolmapstack: symbolmapstack::Symbolmapstack) stream (type: ty::Type)
: Void
=
{ pp::begin_indented_wrap_box stream (pp::CURSOR_RELATIVE 1);
latex_print_some_type1 symbolmapstack stream (type,[], THE members);
pp::end_box stream;
};
#
fun latex_print_type symbolmapstack stream typ
=
latex_print_type' symbolmapstack stream NULL typ;
#
fun latex_print_type_scheme symbolmapstack stream (ty::TYPE_SCHEME { arity, body } )
=
{ (en_pp stream)
->
{ begin_horizontal_else_vertical_box, begin_wrap_box, end_box, pps, break, ... };
begin_wrap_box 2;
pps "TYPE_SCHEME( { arity=";
unparse_int stream arity; unparse_comma stream;
break { spaces=>0, indent_on_wrap=>0 };
pps "body=";
latex_print_some_type symbolmapstack stream body;
pps "} )";
end_box();
};
#
fun latex_print_formals stream
=
{ fun prettyprint_f 0 => ();
prettyprint_f 1 => pps stream "X";
prettyprint_f n
=>
{ unparse_tuple
stream
(fn stream = fn s = pps stream (tweak s))
(type_formals n)
where
fun tweak "a" => "X";
tweak "b" => "Y";
tweak "c" => "Z";
tweak "d" => "A";
tweak "e" => "B";
tweak "f" => "C";
tweak "g" => "D";
tweak "h" => "E";
tweak "i" => "F";
tweak "j" => "G";
tweak "k" => "H";
tweak "l" => "I";
tweak "m" => "J";
tweak "n" => "K";
tweak "o" => "L";
tweak "p" => "M";
tweak "q" => "N";
tweak "r" => "O";
tweak "s" => "P";
tweak "t" => "Q";
tweak "u" => "R";
tweak "v" => "S";
tweak "w" => "T";
tweak "x" => "U";
tweak "y" => "V";
tweak "z" => "W";
tweak x => x;
end;
end;
};
end;
prettyprint_f;
};
#
fun latex_print_enum_constructor_types symbolmapstack stream (ty::PLAIN_TYP { kind => ty::DATATYPE dt, ... } )
=>
{ my { index, free_typs, family=> { members, ... }, ... }
=
dt;
my { begin_horizontal_else_vertical_box, begin_wrap_box, end_box, pps, break, ... }
=
en_pp stream;
my { constructor_list, ... }
=
vector::get (members, index);
begin_horizontal_else_vertical_box 0;
apply
(fn { name, domain, ... }
=
{ pps (symbol::name name);
pps ":";
case domain
THE type
=>
latex_print_some_type1 symbolmapstack stream (type,[], THE (members, free_typs));
NULL
=>
pps "CONST";
esac;
break { spaces=>1, indent_on_wrap=>0 };
}
)
constructor_list;
end_box ();
};
latex_print_enum_constructor_types symbolmapstack stream _
=>
bug "latex_print_enum_constructor_types";
end;
}; # package latex_print_type
end; # toplevel "stipulate"


