Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

22
Vererbung Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf

Transcript of Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

Page 1: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

VererbungVererbung

Prolog Aufbaukurs SS 2000

Heinrich-Heine-Universität Düsseldorf

Christof Rumpf

Page 2: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 2

Interpretieren vs. KompilierenInterpretieren 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

Page 3: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 3

Kompilierung der SignaturKompilierung der Signatur

compile_signature:-complete_signature_syntax, % insert empty subtype/feature listsdefine_undefined_types, % with empty subtype/feature listsconnect_homeless_types, % they become subtypes of topno_multiple_type_definitions, % instead of merging togetherno_cycles, % with respect to subtype relationcp_ist, % immediate subtypescp_subtypes, % subtypescp_lbs, % lower boundscp_glbs, % greatest lower boundscp_ubs, % upper boundscp_lubs, % least upper boundsno_bcpo, % bounded complete partial order checkcp_flubs, % least upper bounds for featuresfeature_inheritance, % with respect to subtype relationcp_fss. % construct feature structures

Page 4: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 4

Syntax-KomplettierungSyntax-Komplettierung

complete_signature_syntax:-

complete_subtype_lists,

complete_feature_lists, !.

complete_subtype_lists:-

retract((T :: F)),

assert((T >> [] :: F)),

fail.

complete_subtype_lists.

complete_feature_lists:-

(S=[] ; S=[_|_]),

retract((T >> S)),

assert((T >> S :: [])),

fail.

complete_feature_lists.

Page 5: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 5

Undefinierte TypenUndefinierte 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).

Page 6: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 6

Heimatlose Typen: Folge 1Heimatlose 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), !.

Page 7: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 7

Heimatlose Typen: Folge 2Heimatlose 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 :: [])).

Page 8: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 8

Multiple TypdefinitionenMultiple 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).

Page 9: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 9

ZyklenZyklenno_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).

Page 10: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 10

PfadePfade

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).

Page 11: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 11

Meldung: ZyklenMeldung: Zyklen

report_cycles([]):- !.

report_cycles([Cycle|Cycles]):-

report_cycle(Cycle),

report_cycles(Cycles).

report_cycle(Types):-

write('- ERROR - type inheritance cycle: '),

write(Types), nl.

Page 12: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 12

SubtypenSubtypen

cp_subtypes:-

retractall(db_subtype(_,_)),

subtype(A,B),

enter(db_subtype(A,B)),

fail.

cp_subtypes.

enter(Clause):- call(Clause), !.

enter(Clause):- assert(Clause).

Page 13: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 13

BCPO-CheckBCPO-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 = [_,_|_], !.

Page 14: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 14

Feature-LUBs IFeature-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).

Page 15: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 15

Feature-LUBs IIFeature-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).

Page 16: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 16

VererbungVererbung

% 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), !.

Page 17: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 17

Typ-Expansion durch VererbungTyp-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).

Page 18: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 18

MerkmalsvererbungMerkmalsvererbung

% 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).

Page 19: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 19

Konsistenz-CheckKonsistenz-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.

Page 20: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 20

Merkmalsstrukturen IMerkmalsstrukturen Icp_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]),

!.

Page 21: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 21

Merkmalsstrukturen IIMerkmalsstrukturen IIcp_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).

Page 22: Vererbung Prolog Aufbaukurs SS 2000 Heinrich-Heine-Universität Düsseldorf Christof Rumpf.

05.06.2000 Typinferenz 22

LiteraturLiteratur

• Carpenter, Bob (199?): The Logic of Typed Feature Structures.

• O‘Keefe, Richard (199?): The Craft of Prolog.