Inheritance and Typing Inference in Compiler Design
220 likes | 322 Vues
Explore the principles of interpreter vs. compiler in typing inference, focusing on signature compilation, subtype relationships, feature structures, and error handling for optimal code execution.
Inheritance and Typing Inference in Compiler Design
E N D
Presentation Transcript
Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf
Interpretieren vs. Kompilieren • Eine Typensignatur enthält implizit viele Relationen. • Ein Interpreter macht diese Relationen explizit. • Laufzeit-, Runtime-, bzw. Online-Berechnung • Ein Compiler berechnet (alle) Relationen vollständig und liefert das Ergebnis der Berechnung zum direkten Zugriff. • Kompilezeit-, Compiletime-, bzw. Offline-Berechnung Typinferenz
Kompilierung der Signatur compile_signature:- complete_signature_syntax, % insert empty subtype/feature lists define_undefined_types, % with empty subtype/feature lists connect_homeless_types, % they become subtypes of top no_multiple_type_definitions, % instead of merging together no_cycles, % with respect to subtype relation cp_ist, % immediate subtypes cp_subtypes, % subtypes cp_lbs, % lower bounds cp_glbs, % greatest lower bounds cp_ubs, % upper bounds cp_lubs, % least upper bounds no_bcpo, % bounded complete partial order check cp_flubs, % least upper bounds for features feature_inheritance, % with respect to subtype relation cp_fss. % construct feature structures Typinferenz
Syntax-Komplettierung complete_signature_syntax:- complete_subtype_lists, complete_feature_lists, !. complete_feature_lists:- (S=[] ; S=[_|_]), retract((T >> S)), assert((T >> S :: [])), fail. complete_feature_lists. complete_subtype_lists:- retract((T :: F)), assert((T >> [] :: F)), fail. complete_subtype_lists. Typinferenz
Undefinierte Typen % define_undefined_types/0 % Every subtype of a defined type and every feature % value that is itself undefined gets a default definition % with empty sets of immediate subtypes and features. define_undefined_types:- immediate_subtypes(SubTypes), all_feature_values(Values), append(SubTypes,Values,Candidates), define_undefined_types(Candidates), !. define_undefined_types([]):- !. define_undefined_types([H|T]):- type(H), !, define_undefined_types(T). define_undefined_types([H|T]):- assert((H >> [] :: [])), define_undefined_types(T). Typinferenz
Heimatlose Typen: Folge 1 % connect_homeless_types/0 % Every defined type that is not defined as a subtyp of some other type % becomes an immediate subtype of 'top'. If top is not defined, it gets % introduced as the common supertype of all other types in the lattice, % that will be bounded in that course. connect_homeless_types:- types(Types), immediate_subtypes(SubTypes), connect_homeless_types(Types,SubTypes), !. connect_homeless_types(Types,SubTypes):- connect_homeless_types(Types,SubTypes,HomelessTypes), connect_homeless_types(HomelessTypes), !. Typinferenz
Heimatlose Typen: Folge 2 connect_homeless_types([],_,[]):- !. connect_homeless_types([H|T],ST,Homeless):- (H = top ; member(H,ST)), !, connect_homeless_types(T,ST,Homeless). connect_homeless_types([H|T],ST,[H|Homeless]):- connect_homeless_types(T,ST,Homeless). connect_homeless_types(Homeless):- retract((top >> SubTypes :: Features)), !, append(SubTypes,Homeless,CompletedSubTypes), assert((top >> CompletedSubTypes :: Features)). connect_homeless_types(Homeless):- asserta((top >> Homeless :: [])). Typinferenz
Multiple Typdefinitionen no_multiple_type_definitions:- multiple_type_definitions(Multiples), m_t_d(Multiples), !. m_t_d([]):- !. m_t_d(Multiples):- nl, write('- ERROR - multiple definitions for type(s): '), nl, write(Multiples), nl, !. multiple_type_definitions(Multiples):- bagof(Type, type(Type), Types), list_duplicates(Types, Multiples). list_duplicates([],[]):- !. list_duplicates([H|T1],[H|T2]):- member(H,T1), !, list_duplicates(T1,T2). list_duplicates([H|T],Multiples):- list_duplicates(T,Multiples). Typinferenz
Zyklen no_cycles:- cycles(Cycles), report_cycles(Cycles). cycles(Cycles):- types(Types), cycles(Types,Cycles). cycles([],[]):- !. cycles([Type|Types],[Cycle|Cycles]):- cycle(Type,Cycle), !, Cycle = [_|CTypes], difference(Types,CTypes,DTypes), cycles(DTypes,Cycles). cycles([_|Types],Cycles):- cycles(Types,Cycles). cycle(Type,Cycle):- type(Type), connected(Type,Type,Cycle). Typinferenz
Pfade connected(SuperType,SubType,Path):- type(SuperType), type(SubType), connected(SuperType,SubType,[],RPath), reverse([SubType|RPath],Path). connected(Type1,Type2,Path,[Type1|Path]):- immediate_supertype(Type1,Type2), !. connected(Type,_,Visited,_):- member(Type,Visited), !, fail. connected(Type1,Type2,Visited,Path):- immediate_supertype(Type1,Type3), connected(Type3,Type2,[Type1|Visited],Path). Typinferenz
Meldung: Zyklen report_cycles([]):- !. report_cycles([Cycle|Cycles]):- report_cycle(Cycle), report_cycles(Cycles). report_cycle(Types):- write('- ERROR - type inheritance cycle: '), write(Types), nl. Typinferenz
Subtypen cp_subtypes:- retractall(db_subtype(_,_)), subtype(A,B), enter(db_subtype(A,B)), fail. cp_subtypes. enter(Clause):- call(Clause), !. enter(Clause):- assert(Clause). Typinferenz
BCPO-Check no_bcpo:- setof1((A,B), C^(db_glb(A,B,C),A @< B), ABs), member((A,B),ABs), nuglb(A,B,GLBs), write('- ERROR - multiple greatest lower bounds: '), nl, tab(5), write(T1 + T2 = GLBs), nl, fail. no_bcpo. nuglb(A,B,GLBs):- setof(C, db_glb(A,B,C), GLBs), GLBs = [_,_|_], !. Typinferenz
Feature-LUBs I cp_flubs:- retractall(db_feature_lub(_,_)), feature_lubs(FLUBs), cp_flubs(FLUBs), !. cp_flubs([]):- !. cp_flubs([FLUB|FLUBs]):- FLUBPred =.. [db_feature_lub|FLUB], assert(FLUBPred), cp_flubs(FLUBs). Typinferenz
Feature-LUBs II feature_lub(Feature:Val,Type):- feature(_,Feature:_), setof(T, V^feature(T,Feature:V),Types), lubs(Types,LUBs), member(Type,LUBs), feature(Type,Feature:Val). feature_lubs(FLUBs):- setof1([F:V,T], feature_lub(F:V,T),FLUBs). Typinferenz
Vererbung % feature_inheritance/0 % All types inherit the features of their supertypes through % manipulation of the type signature in the dynamic database. % Features are inherited top down, depth first, left to right. feature_inheritance:- call(top >> SubTypes :: Feats), expand_types(SubTypes,Feats), !. Typinferenz
Typ-Expansion durch Vererbung % expand_types(+Types,+Feats) % Every type in the list Types inherits the feature value pairs in Feats. expand_types([],_). expand_types([H|T],Feats):- expand_type(H,Feats), expand_types(T,Feats). % expand_type(+Type,+Feats) % Type and all it's subtypes inherit the feature value pairs in Feats. expand_type(T,IFeats):- retract((T >> Subtypes :: TFeats)), inherit_features(TFeats,IFeats,UFeats,T), assert((T >> Subtypes :: UFeats)), !, expand_types(Subtypes,UFeats). Typinferenz
Merkmalsvererbung % inherit_features(+LocalFeats,+InheritedFeats,-UnifiedFeats,+Type). % LocalFeats of Type and InheritedFeats are merged to UnifiedFeats. If an % attribute occurs in both LocalFeats and InheritedFeats, the consistency of % their values is checked. If the values are inconsistent, UnifiedFeats gets % the local feature value and an error message is printed onto the screen. % If their values are consistent, UnifiedFeats gets the greatest lower bound % as the value for that attribute. inherit_features([],F,F,_). inherit_features([F:V1|F1],F2,F3,T):- delete(F:V2,F2,F21), !, check_value_consistency(V1,V2,V3,F,T), inherit_features(F1,[F:V3|F21],F3,T). inherit_features([F:V|F1],F2,F3,T):- inherit_features(F1,[F:V|F2],F3,T). Typinferenz
Konsistenz-Check check_value_consistency(V1,V2,V3,F,T):- glb(V1,V2,V3), !. check_value_consistency(V1,V2,V1,F,T):- nl, write('- ERROR - inconsistent feature inheritance'), nl, tab(5), write('for type: '), write(T), nl, tab(5), write('at feature: '), write(F), nl, tab(5), write('local value: '), write(V1), nl, tab(5), write('inherited value: '), write(V2), nl. Typinferenz
Merkmalsstrukturen I cp_fss:- abolish(db_fs/2), type(T), cp_fs(T,FS,[]), assert(db_fs(T,FS)), fail. cp_fss. cp_fs(Type, _-Type=FS, Types):- call(Type >> _ :: Features), cp_construct_feature_structure(Features,FS,[Type|Types]), !. Typinferenz
Merkmalsstrukturen II cp_construct_feature_structure([],[],_). cp_construct_feature_structure([F:V|FVs],[F:(_-V=[])|FVFSs],Ts):- member(V,Ts), !, write('ERROR: cyclic feature structure for type '), write(V), nl, cp_construct_feature_structure(FVs,FVFSs,Ts). cp_construct_feature_structure([F:V|FVs],[F:VFS|FVFSs],T):- get_fs(V,VFS,T), cp_construct_feature_structure(FVs,FVFSs,T). get_fs(T,FS,_):- call(db_fs(T,_)), !, db_fs(T,FS). get_fs(T,FS,Ts):- cp_fs(T,FS,Ts). Typinferenz
Literatur • Carpenter, Bob (199?): The Logic of Typed Feature Structures. • O‘Keefe, Richard (199?): The Craft of Prolog. Typinferenz