Sta$c Typing of Complex Presence Constraints in Interfaces Nathalie Oostvogels , Joeri De Koster, Wolfgang De Meuter
Interfaces ○ send a private message in Twitter interface PrivateMessage { user_id?: number; screen_name?: string; text: string; } � 2
IPCs in programming languages ○ exactly one of user_id and screen_name is required interface PrivateMessage { user_id?: number; screen_name?: string; text: string; } � 3
Inter-property constraints
Inter-property constraints are everywhere XOR exactly one of user_id and screen_name is required the property infoWindow is ignored, ➔ when suppressInfoWindows is “true” la$tude will be ignored when there is no ⬌ corresponding longitude parameter NAND it is an error to specify values for both times and ns the InterfaceId or InterfaceIndex […] must be set XOR to the interface for which to retrieve informa:on � 5
Complex inter-property constraints XOR “You can iden:fy a list by its slug instead of its list_id . If you decide to do so, note that you will also have to specify the list owner using the owner_id or owner_screen_name parameters.” XOR � 6
IPCs in programming languages exactly one of user_id and screen_name is required interface PrivateMessage { user_id: number; screen_name: string; text: string; } constraining { present(text); present(user_id) XOR present(screen_name); } � 7
Programming with inter-property constraints
Initialisation ○ object must satisfy constraints present(text) present(user_id) XOR present(screen_name) � var msg1: PrivateMessage = {text: "Hello", user_id: 42}; 02 � var msg2: PrivateMessage = {text: "Hello"}; � var msg3: PrivateMessage = {text: "Hello", user_id: 42, screen_name: "Alice"} � 9
Accessing properties ○ only allowed when property is known to be present or absent function getInfo(msg: PrivateMessage) { 02 msg.text; //:: string � msg.user_id; � msg.screen_name; } � 10
Accessing properties ○ if-tests provide more information about presence of properties function getInfo(msg: PrivateMessage) { msg.text; //:: string if (msg.user_id !== undefined) { msg.user_id; //:: number } else { msg.user_id; //:: undefined } } � 11
Accessing properties ○ if-tests provide more information about presence of properties function getInfo(msg: PrivateMessage) { msg.text; //:: string if (msg.user_id !== undefined) { msg.user_id; //:: number msg.screen_name; //:: undefined } else { msg.user_id; //:: undefined msg.screen_name; //:: string } } � 12
Updating properties function setInfo(msg: PrivateMessage) { � msg.text = "Hello again"; � msg.user_id = 43; if (msg.user_id !== undefined) { � msg.user_id = 43; � msg.screen_name = undefined; } � TypeScript strict } null checking mode � 13
Updating multiple properties var msg: PrivateMessage = {text: “Hello", user_id: 42}; if (msg.user_id !== undefined) { msg.user_id = undefined; � msg.screen_name = "Alice"; } � 14
Updating multiple properties var msg1: PrivateMessage = {text: “Hello", user_id: 42}; � var msg2 = assign(msg1, {userid: undefined, screenname: "Alice"}); � var msg3 = assign(msg1, {userid: undefined}); � all related properties need to be updated � 15
Updating multiple properties ○ constraints form clusters interface PrivateMessage’ { text: string; var msg: PrivateMessage’ = r_user_id: number; {text: "Hello", r_screen_name: string; r_user_id: 42, s_user_id: 43}; 02 s_user_id: number; s_screen_name: string; var msg2 = assign(msg, } constraining { {r_user_id: 44, present(text); r_screen_name: undefined}); present(r_user_id) XOR present(r_screen_name); present(s_user_id) XOR present(s_screen_name); } � 16
Updating multiple properties ○ constraints form clusters interface PrivateMessage’ { text: string; var msg: PrivateMessage’ = r_user_id: number; {text: "Hello", r_screen_name: string; r_user_id: 42, s_user_id: 43}; 02 s_user_id: number; s_screen_name: string; var msg2 = assign(msg, } constraining { {r_user_id: 44, present(text); r_screen_name: undefined}); present(r_user_id) XOR present(r_screen_name); present(s_user_id) XOR present(s_screen_name); � assign is functional } � 17
Verifying inter-property constraints with propositional logic
Object literals have to satisfy constraints var msg: PrivateMessage = {text: "Hello", user_id: 42}; valuation v : assigns truth values to proposition letters 02 presence or absence property names � right-hand side needs to be an object literal � 19
Property Access { } ⊨ present(property) interface constraints � 20
Constraints dictate property presence ○ verify presence/absence with logical entailment function getInfo(msg: PrivateMessage) { � msg.text; //:: string msg.user_id; } { } present(text) ⊨ present(text) present(screen_name) XOR present(user_id) � 21
Constraints dictate property presence ○ verify presence/absence with logical entailment function getInfo(msg: PrivateMessage) { msg.text; //:: string � msg.user_id; } { } present(screen_name) XOR present(user_id) ⊭ present(text) present(user_id) � 22
Constraints dictate property presence ○ verify presence/absence with logical entailment function getInfo(msg: PrivateMessage) { msg.text; //:: string � msg.user_id; } { } present(user_id) present(screen_name) XOR present(user_id) ⊭ present(text) ¬ present(user_id) � 23
Constraints dictate property presence ○ verify presence/absence with logical entailment function getInfo(msg: PrivateMessage) { msg.text; //:: string if (msg.user_id !== undefined) { msg.user_id; } } { } present(text) ⊨ present(screen_name) XOR present(user_id) present(user_id) � 24
Constraints dictate property presence ○ verify presence/absence with logical entailment function getInfo(msg: PrivateMessage) { msg.text; //:: string if (msg.user_id !== undefined) { � msg.user_id; //:: number } } { } present(text) ⊨ present(user_id) present(screen_name) XOR present(user_id) present(user_id) � 25
Constraints dictate property presence ○ verify presence/absence with logical entailment function getInfo(msg: PrivateMessage) { msg.text; //:: string if (msg.user_id !== undefined) { msg.user_id; //:: number � msg.screen_name; //:: undefined } } { } present(text) ⊨ ¬ present(screen_name) present(screen_name) XOR present(user_id) present(user_id) � 26
Interface Compatibility var target = source { } { } source constraints target constraints ⊨ + structural delta + structural delta � 27
Assignment interface PrivateMessage { interface PrivateMessageId { text: string; text: string; user_id: number; user_id: number; ○ screen_name: string; } constraining { } constraining { present(text); present(text); ○ present(user_id) XOR present(user_id); present(screen_name); } } var msg: PrivateMessage = ...; var msgId: PrivateMessageId = ...; msgId = msg; msg = msgId; � 28
Assignment interface PrivateMessage { interface PrivateMessageId { text: string; text: string; user_id: number; user_id: number; screen_name: string; ○ } constraining { } constraining { 02 present(text); present(text); present(user_id) XOR present(user_id); present(screen_name); } } var source: PrivateMessageId = {text: "Hello", user_id: 42}; var target: PrivateMessage = source; { } present(text) present(text) ⊭ present(user_id) XOR present(user_id) present(screen_name) source: PrivateMessageId target: PrivateMessage � 29
Assignment interface PrivateMessage { interface PrivateMessageId { text: string; text: string; user_id: number; user_id: number; screen_name: string; ○ } constraining { } constraining { 02 present(text); present(text); present(user_id) XOR present(user_id); present(screen_name); } } var source: PrivateMessageId = {text: "Hello", user_id: 42}; var target: PrivateMessage = source; { ¬ present(screen_name) } present(text) present(text) ⊨ present(user_id) present(user_id) XOR present(screen_name) source: PrivateMessageId target: PrivateMessage � 30
Assignment interface PrivateMessageId { interface PrivateMessageAll { text: string; text: string; user_id: number; user_id: number; screen_name: string; ○ } constraining { } constraining { present(text); present(text); 02 present(user_id); present(user_id); present(screen_name); } } { { present(screen_name) } present(text) present(text) ⊨ present(user_id) present(user_id) source: PrivateMessageAll target: PrivateMessageId � 31
Recommend
More recommend