## latex-print-type.pkg
# Compiled by:
#
src/lib/compiler/front/typer/typer.sublib# modified to use Lib7 Lib pp. [dbm, 7/30/03])
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 mtt = more_type_types; # more_type_types is from
src/lib/compiler/front/typer/types/more-type-types.pkg package tdt = type_declaration_types; # type_declaration_types is from
src/lib/compiler/front/typer-stuff/types/type-declaration-types.pkg package pp = standard_prettyprinter; # standard_prettyprinter is from
src/lib/prettyprint/big/src/standard-prettyprinter.pkg package syx = symbolmapstack; # symbolmapstack is from
src/lib/compiler/front/typer-stuff/symbolmapstack/symbolmapstack.pkgherein
api Latex_Print_Type {
type_formals
:
Int
-> List( String );
typevar_ref_printname
:
tdt::Typevar_Ref
-> String;
latex_print_type
:
syx::Symbolmapstack
-> pp::Prettyprinter
-> tdt::Type
-> Void;
latex_print_typescheme
:
syx::Symbolmapstack
-> pp::Prettyprinter
-> tdt::Typescheme
-> Void;
latex_print_some_type
:
syx::Symbolmapstack
-> pp::Prettyprinter
-> tdt::Typoid
-> Void;
latex_print_sumtype_constructor_domain
:
((Vector( tdt::Sumtype_Member ), List( tdt::Type )) )
-> syx::Symbolmapstack
-> pp::Prettyprinter
-> tdt::Typoid
-> Void;
latex_print_sumtype_constructor_types
:
syx::Symbolmapstack
-> pp::Prettyprinter
-> tdt::Type
-> Void;
reset_latex_print_type
:
Void -> Void;
latex_print_formals
:
pp::Prettyprinter
-> Int
-> Void;
debugging: Ref( Bool );
unalias: Ref( Bool );
};
end;
stipulate
package fis = find_in_symbolmapstack; # find_in_symbolmapstack is from
src/lib/compiler/front/typer-stuff/symbolmapstack/find-in-symbolmapstack.pkg package ip = inverse_path; # inverse_path is from
src/lib/compiler/front/typer-stuff/basics/symbol-path.pkg package mtt = more_type_types; # more_type_types is from
src/lib/compiler/front/typer/types/more-type-types.pkg package pp = standard_prettyprinter; # standard_prettyprinter is from
src/lib/prettyprint/big/src/standard-prettyprinter.pkg package sp = symbol_path; # symbol_path is from
src/lib/compiler/front/typer-stuff/basics/symbol-path.pkg package syx = symbolmapstack; # symbolmapstack is from
src/lib/compiler/front/typer-stuff/symbolmapstack/symbolmapstack.pkg package tdt = type_declaration_types; # type_declaration_types is from
src/lib/compiler/front/typer-stuff/types/type-declaration-types.pkg package tu = type_junk; # type_junk is from
src/lib/compiler/front/typer-stuff/types/type-junk.pkg package uj = unparse_junk; # unparse_junk is from
src/lib/compiler/front/typer/print/unparse-junk.pkg Pp = pp::Pp;
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);
fun by f x y
=
f y x;
# internals = typer_control::internals;
internals = log::internals;
unit_path
=
ip::extend
(
ip::empty,
symbol::make_type_symbol "Void"
);
#
fun bound_typevar_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
=
{ z = char::to_int 'Z'; # use reverse order for meta vars
#
if (k < 26)
#
string::from_char (char::from_int (z - k));
else
implode [ char::from_int (z - (int::(/) (k, 26))),
char::from_int (z - (int::(%) (k, 26)))
];
fi;
};
#
fun type_formals n
=
{ fun loop i
=
if (i>=n)
[];
else
(bound_typevar_name i) ! loop (i + 1);fi;
loop 0;
};
#
fun literal_kind_printname (lk: tdt::Literal_Kind)
=
case lk
#
tdt::INT => "Int"; # or "INT"
tdt::UNT => "Unt"; # or "UNT"
tdt::FLOAT => "Float"; # or "FLOAT"
tdt::CHAR => "Char"; # or "CHAR"
tdt::STRING => "String"; # or "STRING"
esac;
stipulate # WARNING -- compiler global variables
count = REF (-1);
meta_tyvars = REF([]: List( tdt::Typevar_Ref ));
herein
fun meta_tyvar_name ((tv as { id, ref_typevar }): tdt::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_typevar_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
#
tdt::RESOLVED_TYPEVAR (tdt::TYPEVAR_REF (tv as { id, ref_typevar => _ }) )
=>
typevar_ref_printname tv
# +
# (sprintf "[id%d]" id) # We probably don't want this for latex printing...?
;
tdt::RESOLVED_TYPEVAR _
=>
"<RESOLVED_TYPEVAR ?>";
tdt::META_TYPEVAR { fn_nesting, eq }
=>
decorated_typevar_name (eq, annotate ( meta_tyvar_name tv,
"META",
THE fn_nesting
)
);
tdt::INCOMPLETE_RECORD_TYPEVAR { fn_nesting, eq, known_fields }
=>
decorated_typevar_name (eq, annotate ( meta_tyvar_name tv,
"INCOMPLETE_RECORD",
THE fn_nesting
)
);
tdt::USER_TYPEVAR { name, fn_nesting, eq }
=>
decorated_typevar_name (eq, annotate (symbol::name name, "USER", THE fn_nesting));
tdt::LITERAL_TYPEVAR { kind, ... }
=>
annotate (literal_kind_printname kind, "L", NULL);
tdt::OVERLOADED_TYPEVAR eq
=>
decorated_typevar_name (eq, annotate (meta_tyvar_name tv, "OVERLOADED", NULL));
tdt::TYPEVAR_MARK _ => "<TYPEVAR_MARK ?>";
esac;
end;
/*
fun ppkind (pp:Pp) kind
=
pp.lit
(case kind
of BASE _ => "BASE"
| FORMAL => "FORMAL"
| tdt::FLEXIBLE_TYPE _ => "FLEXIBLE_TYPE"
| tdt::ABSTRACT _ => "ABSTYC"
| tdt::SUMTYPE _ => "SUMTYPE"
| tdt::TEMP => "TEMP"
)
*/
#
fun ppkind (pp:Pp) kind
=
pp.lit
#
case kind
#
tdt::BASE _ => "P";
tdt::FORMAL => "F";
tdt::FLEXIBLE_TYPE _ => "X";
tdt::ABSTRACT _ => "A";
tdt::SUMTYPE _ => "D";
tdt::TEMP => "T";
esac;
#
fun effective_path (path, type, symbolmapstack) : String
=
{ fun namepath_of_type ( tdt::SUM_TYPE { namepath, ... }
| tdt::NAMED_TYPE { namepath, ... }
| tdt::TYPE_BY_STAMPPATH { namepath, ... }
)
=>
THE namepath;
namepath_of_type _
=>
NULL;
end;
#
fun find (path, type)
=
(uj::find_path (path,
(\\ type' = tu::type_equality (type', type)),
(\\ x = fis::find_type_via_symbol_path (symbolmapstack, x,
(\\ _ = raise exception syx::UNBOUND))))
);
#
fun search (path, type)
=
{ (find (path, type))
->
(suffix, found);
if found
(suffix, TRUE);
else
if (not *unalias)
#
(suffix, FALSE);
else
case (tu::unwrap_definition_1 type)
#
THE type'
=>
case (namepath_of_type type')
#
THE path'
=>
{ (search (path', type'))
->
x as (suffix', found');
if found' x;
else (suffix, FALSE);
fi;
};
NULL => (suffix, FALSE);
esac;
NULL => (suffix, FALSE);
esac;
fi;
fi;
};
(search (path, type))
->
(suffix, found);
name = sp::to_string (sp::SYMBOL_PATH suffix);
if found name;
else "?." + name;
fi;
};
arrow_stamp = mtt::arrow_stamp;
#
fun strength (type)
=
case type
#
tdt::TYPEVAR_REF { id, ref_typevar => REF (tdt::RESOLVED_TYPEVAR type') }
=>
strength (type');
tdt::TYPCON_TYPOID (type, args)
=>
case type
#
tdt::SUM_TYPE { stamp, kind => tdt::BASE _, ... }
=>
if (stamp::same_stamp (stamp, arrow_stamp)) 0;
else 2;
fi;
tdt::RECORD_TYPE (_ ! _) # excepting type Void
=>
if (tuples::is_tuple_type type) 1;
else 2;
fi;
_ => 2;
esac;
_ => 2;
esac;
#
fun prettyprint_eq_prop (pp:Pp) p
=
{ a = case p
#
tdt::e::NO => "NO";
tdt::e::YES => "YES";
tdt::e::INDETERMINATE => "INDETERMINATE";
tdt::e::CHUNK => "CHUNK";
tdt::e::DATA => "DATA";
tdt::e::UNDEF => "UNDEF";
esac;
pp.lit a;
};
#
fun prettyprint_inverse_path (pp:Pp) (inverse_path::INVERSE_PATH inverse_path: inverse_path::Inverse_Path)
=
pp.lit (symbol_path::to_string (symbol_path::SYMBOL_PATH (reverse inverse_path)));
#
fun latex_print_type' symbolmapstack (pp:Pp) members_op
=
latex_print_type''
where
#
fun latex_print_type'' (type as tdt::SUM_TYPE { namepath, stamp, is_eqtype, kind, ... } )
=>
if *internals
#
pp.wrap {. pp.rulename "ppplw3";
prettyprint_inverse_path pp namepath;
pp.lit "[";
pp.lit "G"; ppkind pp kind; pp.lit ";";
pp.lit (stamp::to_short_string stamp);
pp.lit ";";
prettyprint_eq_prop pp *is_eqtype;
pp.lit "]";
};
else
pp.lit (effective_path (namepath, type, symbolmapstack));
fi;
latex_print_type'' (type as tdt::NAMED_TYPE { namepath, typescheme => tdt::TYPESCHEME { body, ... }, ... } )
=>
if *internals
#
pp.wrap {. pp.rulename "ppplw4";
prettyprint_inverse_path pp namepath;
pp.lit "["; pp.lit "D;";
latex_print_some_type symbolmapstack pp body;
pp.lit "]";
};
else
pp.lit (effective_path (namepath, type, symbolmapstack));
fi;
latex_print_type'' (tdt::RECORD_TYPE labels)
=>
{
uj::unparse_closed_sequence pp
{ front => \\ (pp:Pp) = { pp.lit "{"; },
separator => \\ (pp:Pp) = { pp.txt ", "; },
back => \\ (pp:Pp) = { pp.lit "}"; },
breakstyle => uj::WRAP,
print_one => uj::unparse_symbol
}
labels;
};
latex_print_type'' (tdt::RECURSIVE_TYPE n)
=>
case members_op
#
THE (members, _)
=>
{ (vector::get (members, n))
->
{ name_symbol, valcons, ... };
uj::unparse_symbol pp name_symbol;
};
NULL => pp.lit (string::cat ["<RECURSIVE_TYPE ", int::to_string n, ">"]);
esac;
latex_print_type'' (tdt::FREE_TYPE n)
=>
case members_op
#
THE (_, free_types)
=>
{ type = ( list::nth (free_types, n)
except _
=
bug "unexpected free_types in prettyprintTypeConstructor"
);
latex_print_type'' type;
};
NULL => pp.lit (string::cat ["<FREE_TYPE ", int::to_string n, ">"]);
esac;
latex_print_type'' (type as tdt::TYPE_BY_STAMPPATH { arity, stamppath, namepath } )
=>
if *internals
#
pp.wrap {. pp.rulename "ppplw5";
prettyprint_inverse_path pp namepath;
pp.lit "[P;";
pp.lit (stamppath::stamppath_to_string stamppath);
pp.lit "]";
};
else
prettyprint_inverse_path pp namepath;
fi;
latex_print_type'' tdt::ERRONEOUS_TYPE
=>
pp.lit "[E]";
end;
end
also
fun latex_print_some_type1
symbolmapstack
pp
( typoid: tdt::Typoid,
an_api: tdt::Typescheme_Eqflags,
members_op: Null_Or( (Vector( tdt::Sumtype_Member ), List( tdt::Type )) )
)
: Void
=
{
#
fun prty typoid
=
case typoid
#
tdt::TYPEVAR_REF { id, ref_typevar => REF (tdt::RESOLVED_TYPEVAR type') }
=>
prty type';
tdt::TYPEVAR_REF tv
=>
latex_print_typevar tv;
tdt::TYPESCHEME_ARG n
=>
{ eq = list::nth (an_api, n)
except
INDEX_OUT_OF_BOUNDS = FALSE;
pp.lit (decorated_typevar_name (eq, (bound_typevar_name n)));
};
tdt::TYPCON_TYPOID (type, args)
=>
{ fun otherwise ()
=
{ pp.wrap' 0 2 {. pp.rulename "ppplw6";
latex_print_type' symbolmapstack pp members_op type;
case args
#
[] => ();
_ => { pp.lit "(";
pp.cut ();
latex_print_type_args args;
pp.lit ")";
};
esac;
};
};
case type
#
tdt::SUM_TYPE { stamp, kind, ... }
=>
case kind
#
tdt::BASE _
=>
if (stamp::same_stamp (stamp, arrow_stamp))
#
case args
#
[domain, range]
=>
{ pp.box' 0 -1 {. pp.rulename "lppl56";
if (strength domain == 0)
#
pp.wrap {. pp.rulename "lppl57";
pp.lit "(";
prty domain;
pp.lit ")";
};
else
prty domain;
fi;
pp.txt' 0 -1 " ";
pp.txt "-> ";
prty range;
};
};
_ => bug "TYPCON_TYPE: arity";
esac;
else
pp.wrap' 0 2 {. pp.rulename "lptw1";
latex_print_type' symbolmapstack pp members_op type;
case args
#
[] => ();
_ => { pp.lit "(";
pp.cut ();
latex_print_type_args args;
pp.lit ")";
};
esac;
};
fi;
_ => otherwise ();
esac;
tdt::RECORD_TYPE labels
=>
if (tuples::is_tuple_type type) prettyprint_tuplety args;
else prettyprint_recordty (labels, args);
fi;
_ => otherwise ();
esac;
};
tdt::TYPESCHEME_TYPOID { typescheme_eqflags => an_api,
#
typescheme => tdt::TYPESCHEME { arity, body }
}
=>
latex_print_some_type1 symbolmapstack pp (body, an_api, members_op);
tdt::WILDCARD_TYPOID
=>
pp.lit "_";
tdt::UNDEFINED_TYPOID
=>
pp.lit "undef";
esac
also
fun latex_print_type_args []
=>
();
latex_print_type_args [type]
=>
{ if (strength type <= 1)
#
pp.wrap' 0 2 {. pp.rulename "lptw2";
pp.lit "(";
prty type;
pp.lit ")";
};
else
prty type;
fi;
pp.txt " ";
};
latex_print_type_args tys
=>
uj::unparse_closed_sequence
pp
{ front => \\ (pp:Pp) = pp.lit "(",
separator => \\ (pp:Pp) = pp.txt ", ",
back => \\ (pp:Pp) = pp.lit ")",
breakstyle => uj::WRAP,
print_one => \\ _ = \\ type = prty type
}
tys;
end
also
fun prettyprint_tuplety []
=>
pp.txt (effective_path (unit_path, tdt::RECORD_TYPE [], symbolmapstack));
prettyprint_tuplety tys
=>
{ pp.lit "(";
#
uj::unparse_sequence
pp
{ separator => \\ (pp:Pp) = { pp.txt " ";
pp.txt ", "; # Was "* "
},
breakstyle => uj::WRAP,
print_one => (\\ _ = \\ type = if (strength type <= 1)
#
pp.wrap {. pp.rulename "lptw3";
pp.lit "(";
prty type;
pp.lit ")";
};
else
prty type;
fi
)
}
tys;
pp.txt ")";
};
end
also
fun prettyprint_field (lab, type)
=
{ pp.box' 0 -1 {. pp.rulename "lpt1";
uj::unparse_symbol pp lab;
pp.lit ":";
prty type;
};
}
also
fun prettyprint_recordty ([],[])
=>
pp.txt (effective_path (unit_path, tdt::RECORD_TYPE [], symbolmapstack));
# this case should not occur
prettyprint_recordty (lab ! labels, arg ! args)
=>
{ pp.wrap {. pp.rulename "lptw4";
pp.lit "{";
prettyprint_field (lab, arg);
paired_lists::apply
(\\ field'
=
{ pp.txt ", ";
prettyprint_field field';
}
)
(labels, args);
pp.lit "}";
};
};
prettyprint_recordty _
=>
bug "latex_print_type::prettyprintRECORDty";
end
also
fun latex_print_typevar (tv as { id, ref_typevar => (ref_info as REF info) }: tdt::Typevar_Ref): Void
=
{ printname = typevar_ref_printname tv;
#
case info
#
tdt::INCOMPLETE_RECORD_TYPEVAR { fn_nesting, eq, known_fields }
=>
case known_fields
#
[] =>
{ pp.lit "{";
pp.lit printname;
pp.lit "}";
};
field' ! fields
=>
{ pp.wrap {. pp.rulename "lptw5";
pp.lit "{";
prettyprint_field field';
apply (\\ x = { pp.txt ", ";
prettyprint_field x;
}
)
fields;
pp.endlit ";";
pp.txt " ";
pp.lit printname;
pp.lit "}";
};
};
esac;
_ => pp.lit printname;
esac;
};
prty typoid;
} # prettyprintType1
also
fun latex_print_some_type (symbolmapstack: syx::Symbolmapstack) (pp:Pp) (type: tdt::Typoid) : Void
=
{ pp.cwrap {. pp.rulename "lptcw1";
latex_print_some_type1 symbolmapstack pp (type,[], NULL);
};
};
#
fun latex_print_sumtype_constructor_domain members (symbolmapstack: syx::Symbolmapstack) pp (type: tdt::Typoid)
: Void
=
{ pp.cwrap {. pp.rulename "lpplcw2";
latex_print_some_type1 symbolmapstack pp (type,[], THE members);
};
};
#
fun latex_print_type symbolmapstack pp type
= latex_print_type' symbolmapstack pp NULL type;
#
fun latex_print_typescheme symbolmapstack pp (tdt::TYPESCHEME { arity, body } )
=
pp.wrap' 0 2 {. pp.rulename "lptw6";
pp.txt "TYPESCHEME( { arity=";
uj::unparse_int pp arity; pp.txt ", ";
pp.cut ();
pp.lit "body=";
latex_print_some_type symbolmapstack pp body;
pp.lit "} )";
};
#
fun latex_print_formals pp
=
{ fun prettyprint_f 0 => ();
prettyprint_f 1 => pp.lit "X";
prettyprint_f n
=>
{ uj::unparse_tuple
pp
(\\ pp = \\ s = pp.lit (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_sumtype_constructor_types symbolmapstack pp (tdt::SUM_TYPE { kind => tdt::SUMTYPE dt, ... } )
=>
{ dt -> { index, free_types, family=> { members, ... }, ... };
#
(vector::get (members, index)) -> { valcons, ... };
pp.box' 0 -1 {. pp.rulename "lpt2";
#
apply
(\\ { name, domain, ... }
=
{ pp.lit (symbol::name name);
pp.lit ":";
case domain
#
THE type => latex_print_some_type1 symbolmapstack pp (type,[], THE (members, free_types));
NULL => pp.lit "CONST";
esac;
pp.txt " ";
}
)
valcons;
};
};
latex_print_sumtype_constructor_types symbolmapstack pp _
=>
bug "latex_print_sumtype_constructor_types";
end;
}; # package latex_print_type
end; # toplevel "stipulate"