Knowledge

Structural type system

Source 📝

25: 368:, do not substitute structurally in the case where an expected type is declared (i.e., not inferred), e.g., only substitute for functions that are signature-based polymorphic via type inference. Then it is not possible to accidentally subtype a non-inferred type, although it may still be possible to provide an explicit conversion to a non-inferred type, which is invoked implicitly. 298:
in which type compatibility and equivalence are determined by the type's actual structure or definition and not by other characteristics such as its name or place of declaration. Structural systems are used to determine if types are equivalent and whether a type is a subtype of another. It contrasts
386:
A pitfall of structural typing versus nominative typing is that two separately defined types intended for different purposes, but accidentally holding the same properties (e.g. both composed of a pair of integers), could be considered the same type by the type system, simply because they happen to
1223:# (z :> simpler_obj);; This expression cannot be coerced to type simpler_obj = < get_x : int >; it has type < blahblah : float; set_x : int -> unit > but is here used with type < get_x : int; .. > The first object type has no method get_x 323:, an element is considered to be compatible with another if, for each feature within the second element's type, a corresponding and identical feature exists in the first element's type. Some languages may differ on the details, such as whether the 383:; in particular, it permits creation of a type which is a supertype of an existing type, without modifying the definition of the latter. However, this may not be desirable where the programmer wishes to create closed abstractions. 552:) is defined only by its methods. In other words, the type of x is defined by the method types "get_x : int" and "set_x : int -> unit" rather than by any name. 327:
must match in name. This definition is not symmetric, and includes subtype compatibility. Two types are considered to be identical if each is compatible with the other.
401:
Checking that two types are compatible, based on structural typing, is a non-trivial operation, e.g., requires maintaining a stack of previous checked types.
361:
of the base type, or subtypes thereof. The subtype may contain added features, such as members not present in the base type, or stronger invariants.
1300: 357:
can be formed based on how the subtype relationship is defined. One type is a subtype of another if and only if it contains all the
89: 61: 42: 380: 273: 68: 1352: 163: 1314: 723:
So they must be the same type, or else this wouldn't even type-check. This shows that equivalence of types is structural.
684:
OCaml considers them the same type. For example, the equality operator is typed to only take two values of the same type:
364:
A distinction exists between structural substitution for inferred and non-inferred polymorphism. Some languages, such as
75: 1276: 108: 57: 1342: 1368: 876:
Another object can be made that happens to have that method and method type; the other methods are irrelevant:
365: 46: 833:
means that the first argument can be any object which has a "set_x" method, which takes an int as argument.
548:
Here the OCaml interactive runtime prints out the inferred type of the object for convenience. Its type (
266: 1259:
Proceedings of the 17th ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '90
335: 237: 82: 416:) without going through a nominative class. Classes only serve as functions for creating objects. 35: 372: 300: 143: 159: 1387: 259: 212: 1048:
This shows that compatibility for things like method invocation is determined by structure.
311:, in which only the part of the structure accessed at runtime is checked for compatibility. 1051:
Let us define a type synonym for objects with only a "get_x" method and no other methods:
338:
uses structural typing on methods to determine compatibility of a type with an interface.
8: 1243: 388: 177: 1328: 1294: 1282: 1257:
Cook, W.R.; Hill, W.L.; Canning, P.S. (January 1990). "Inheritance is not subtyping".
305:, where comparisons are based on the names of the types or explicit declarations, and 1348: 1272: 222: 1286: 1262: 409:
Objects in OCaml are structurally typed by the names and types of their methods.
242: 232: 168: 217: 207: 172: 1381: 227: 202: 555:
To define another object, which has the same methods and types of methods:
295: 247: 1372: 387:
have identical structure. One way this can be avoided is by creating one
307: 186: 138: 124: 1267: 339: 1226:
This shows that compatibility for widening coercions are structural.
354: 350: 334:
uses structural typing on methods for compatibility of object types.
24: 346:
uses structural typing, but classes are not structurally subtyped.
331: 343: 550:< get_x : int; set_x : int -> unit > 342:
functions exhibit structural typing on type arguments.
1315:"Type compatibility: name vs structural equivalence" 371:
Structural subtyping is arguably more flexible than
49:. Unsourced material may be challenged and removed. 726:One can define a function that invokes a method: 1379: 1261:. San Francisco, California. pp. 125–135. 1256: 267: 1011:The "set_to_10" function also works on it: 1299:: CS1 maint: location missing publisher ( 1220:, because it is not a structural subtype: 825:The inferred type for the first argument ( 274: 260: 1266: 109:Learn how and when to remove this message 1123:contains a superset of its methods. So 1115:is not of this type; but structurally, 827:< set_x : int -> 'a; .. > 1380: 1340: 1341:Pierce, Benjamin C. (2002). "19.3". 1119:is of a subtype of this type, since 398:in structurally-typed OO languages. 47:adding citations to reliable sources 18: 394:In 1990, Cook, et al., proved that 13: 14: 1399: 1362: 412:Objects can be created directly ( 375:, as it permits the creation of 23: 1344:Types and Programming Languages 34:needs additional citations for 1321: 1307: 1250: 1244:"Signature-based polymorphism" 1236: 314: 1: 1369:NominativeAndStructuralTyping 1229: 1127:can be coerced to this type: 836:So it can be used on object 396:inheritance is not subtyping 7: 349:In languages which support 10: 1404: 404: 292:property-based type system 1129: 1053: 1013: 878: 842: 728: 686: 557: 418: 58:"Structural type system" 829:) is interesting. The 294:) is a major class of 288:structural type system 144:Strong vs. weak typing 16:Class of type systems 373:nominative subtyping 351:subtype polymorphism 43:improve this article 1268:10.1145/96709.96721 389:algebraic data type 302:nominative systems 1354:978-0-262-16209-8 414:immediate objects 321:structural typing 284: 283: 119: 118: 111: 93: 1395: 1358: 1333: 1332: 1325: 1319: 1318: 1311: 1305: 1304: 1298: 1290: 1270: 1254: 1248: 1247: 1240: 1219: 1212: 1211: 1208: 1205: 1202: 1199: 1195: 1192: 1189: 1186: 1183: 1180: 1177: 1174: 1171: 1170: 1167: 1164: 1161: 1158: 1155: 1152: 1148: 1145: 1142: 1139: 1136: 1133: 1126: 1122: 1118: 1114: 1107: 1106: 1103: 1100: 1097: 1094: 1091: 1088: 1085: 1081: 1078: 1075: 1072: 1069: 1066: 1063: 1060: 1057: 1044: 1041: 1038: 1035: 1032: 1029: 1026: 1023: 1020: 1017: 1007: 1006: 1003: 1000: 997: 994: 991: 988: 985: 982: 979: 976: 973: 970: 967: 964: 961: 958: 955: 951: 948: 945: 942: 939: 936: 933: 930: 927: 924: 921: 918: 915: 912: 909: 906: 903: 900: 897: 894: 891: 888: 885: 882: 872: 871: 868: 865: 862: 859: 855: 852: 849: 846: 839: 832: 828: 821: 820: 817: 814: 811: 808: 805: 802: 799: 796: 793: 790: 787: 784: 781: 778: 775: 772: 769: 766: 763: 759: 756: 753: 750: 747: 744: 741: 738: 735: 732: 719: 718: 715: 712: 709: 706: 702: 699: 696: 693: 690: 680: 679: 676: 673: 670: 667: 664: 661: 658: 655: 652: 649: 646: 643: 640: 637: 634: 631: 628: 624: 621: 618: 615: 612: 609: 606: 603: 600: 597: 594: 591: 588: 585: 582: 579: 576: 573: 570: 567: 564: 561: 551: 544: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 507: 504: 501: 498: 495: 492: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 428: 425: 422: 276: 269: 262: 195:Minor categories 152:Major categories 131:General concepts 121: 120: 114: 107: 103: 100: 94: 92: 51: 27: 19: 1403: 1402: 1398: 1397: 1396: 1394: 1393: 1392: 1378: 1377: 1365: 1355: 1337: 1336: 1327: 1326: 1322: 1313: 1312: 1308: 1292: 1291: 1279: 1255: 1251: 1242: 1241: 1237: 1232: 1224: 1217: 1216:But not object 1214: 1213: 1209: 1206: 1203: 1200: 1197: 1196: 1193: 1190: 1187: 1184: 1181: 1178: 1175: 1172: 1168: 1165: 1162: 1159: 1156: 1153: 1150: 1149: 1146: 1143: 1140: 1137: 1134: 1131: 1124: 1120: 1116: 1112: 1109: 1108: 1104: 1101: 1098: 1095: 1092: 1089: 1086: 1083: 1082: 1079: 1076: 1073: 1070: 1067: 1064: 1061: 1058: 1055: 1046: 1045: 1042: 1039: 1036: 1033: 1030: 1027: 1024: 1021: 1018: 1015: 1009: 1008: 1004: 1001: 998: 995: 992: 989: 986: 983: 980: 977: 974: 971: 968: 965: 962: 959: 956: 953: 952: 949: 946: 943: 940: 937: 934: 931: 928: 925: 922: 919: 916: 913: 910: 907: 904: 901: 898: 895: 892: 889: 886: 883: 880: 874: 873: 869: 866: 863: 860: 857: 856: 853: 850: 847: 844: 837: 830: 826: 823: 822: 818: 815: 812: 809: 806: 803: 800: 797: 794: 791: 788: 785: 782: 779: 776: 773: 770: 767: 764: 761: 760: 757: 754: 751: 748: 745: 742: 739: 736: 733: 730: 721: 720: 716: 713: 710: 707: 704: 703: 700: 697: 694: 691: 688: 682: 681: 677: 674: 671: 668: 665: 662: 659: 656: 653: 650: 647: 644: 641: 638: 635: 632: 629: 626: 625: 622: 619: 616: 613: 610: 607: 604: 601: 598: 595: 592: 589: 586: 583: 580: 577: 574: 571: 568: 565: 562: 559: 549: 546: 545: 541: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 407: 317: 280: 115: 104: 98: 95: 52: 50: 40: 28: 17: 12: 11: 5: 1401: 1391: 1390: 1376: 1375: 1364: 1363:External links 1361: 1360: 1359: 1353: 1335: 1334: 1329:"Object types" 1320: 1306: 1278:978-0897913430 1277: 1249: 1234: 1233: 1231: 1228: 1222: 1130: 1054: 1014: 879: 843: 729: 687: 558: 419: 406: 403: 391:for each use. 316: 313: 282: 281: 279: 278: 271: 264: 256: 253: 252: 251: 250: 245: 240: 235: 230: 225: 220: 215: 213:Flow-sensitive 210: 205: 197: 196: 192: 191: 190: 189: 184: 175: 166: 154: 153: 149: 148: 147: 146: 141: 133: 132: 128: 127: 117: 116: 31: 29: 22: 15: 9: 6: 4: 3: 2: 1400: 1389: 1386: 1385: 1383: 1374: 1370: 1367: 1366: 1356: 1350: 1347:. MIT Press. 1346: 1345: 1339: 1338: 1330: 1324: 1316: 1310: 1302: 1296: 1288: 1284: 1280: 1274: 1269: 1264: 1260: 1253: 1245: 1239: 1235: 1227: 1221: 1128: 1052: 1049: 1012: 877: 841: 834: 727: 724: 685: 556: 553: 417: 415: 410: 402: 399: 397: 392: 390: 384: 382: 378: 374: 369: 367: 362: 360: 356: 352: 347: 345: 341: 337: 333: 330:For example, 328: 326: 322: 312: 310: 309: 304: 303: 297: 293: 289: 277: 272: 270: 265: 263: 258: 257: 255: 254: 249: 246: 244: 241: 239: 238:Substructural 236: 234: 231: 229: 226: 224: 221: 219: 216: 214: 211: 209: 206: 204: 201: 200: 199: 198: 194: 193: 188: 185: 183: 179: 176: 174: 170: 167: 165: 161: 158: 157: 156: 155: 151: 150: 145: 142: 140: 137: 136: 135: 134: 130: 129: 126: 123: 122: 113: 110: 102: 91: 88: 84: 81: 77: 74: 70: 67: 63: 60: –  59: 55: 54:Find sources: 48: 44: 38: 37: 32:This article 30: 26: 21: 20: 1388:Type systems 1343: 1323: 1309: 1258: 1252: 1238: 1225: 1215: 1110: 1050: 1047: 1010: 875: 835: 824: 725: 722: 683: 554: 547: 413: 411: 408: 400: 395: 393: 385: 376: 370: 363: 358: 353:, a similar 348: 340:C++ template 329: 324: 320: 318: 306: 301: 296:type systems 291: 287: 285: 223:Intersection 181: 125:Type systems 105: 96: 86: 79: 72: 65: 53: 41:Please help 36:verification 33: 1373:WikiWikiWeb 1185:simpler_obj 1157:simpler_obj 1144:simpler_obj 1111:The object 1087:simpler_obj 1062:simpler_obj 315:Description 308:duck typing 187:Duck typing 139:Type safety 1230:References 379:types and 233:Refinement 182:structural 69:newspapers 1295:cite book 1019:set_to_10 848:set_to_10 765:set_to_10 737:set_to_10 381:protocols 355:dichotomy 208:Dependent 99:July 2011 1382:Category 966:blahblah 935:"%d 899:blahblah 608:"%d 359:features 325:features 203:Abstract 173:inferred 169:Manifest 1287:8225906 439:mutable 405:Example 366:Haskell 248:Session 218:Gradual 178:Nominal 164:dynamic 83:scholar 1351:  1285:  1275:  1080:>;; 941:" 932:printf 926:Printf 914:method 896:method 893:object 614:" 605:printf 599:Printf 587:method 575:method 572:object 463:method 451:method 433:object 377:ad hoc 243:Unique 228:Latent 160:Static 85:  78:  71:  64:  56:  1283:S2CID 1191:get_x 1182::> 1141::> 1096:get_x 1071:get_x 987:-> 978:set_x 972:float 917:set_x 804:' 801:-> 786:' 783:-> 774:set_x 752:set_x 717:false 660:-> 651:set_x 639:get_x 590:set_x 578:get_x 524:-> 515:set_x 503:get_x 478:<- 466:set_x 454:get_x 332:OCaml 299:with 90:JSTOR 76:books 1349:ISBN 1301:link 1273:ISBN 1169:> 1163:< 1105:> 1093:< 1084:type 1068:< 1059:type 1037:unit 1005:> 999:< 993:> 990:unit 963:< 864:unit 819:> 813:< 798:> 771:< 711:bool 678:> 672:< 666:> 663:unit 636:< 542:> 536:< 530:> 527:unit 500:< 344:Haxe 290:(or 180:vs. 171:vs. 162:vs. 62:news 1371:at 1263:doi 1204:int 1166:obj 1147:);; 1102:int 1077:int 1002:obj 984:int 954:val 947:end 884:let 816:fun 780:int 762:val 734:let 675:obj 657:int 645:int 627:val 620:end 563:let 539:obj 521:int 509:int 491:val 484:end 436:val 424:let 319:In 45:by 1384:: 1297:}} 1293:{{ 1281:. 1271:. 1210:10 1194:;; 1188:)# 1043:() 1028:10 1025:;; 950:;; 938:\n 870:() 854:;; 840:: 831:.. 795:.. 758:;; 755:10 701:;; 623:;; 611:\n 487:;; 336:Go 286:A 1357:. 1331:. 1317:. 1303:) 1289:. 1265:: 1246:. 1218:z 1207:= 1201:: 1198:- 1179:x 1176:( 1173:# 1160:= 1154:: 1151:- 1138:x 1135:( 1132:# 1125:x 1121:x 1117:x 1113:x 1099:: 1090:= 1074:: 1065:= 1056:# 1040:= 1034:: 1031:- 1022:z 1016:# 996:= 981:: 975:; 969:: 960:: 957:z 944:y 929:. 923:= 920:y 911:5 908:. 905:2 902:= 890:= 887:z 881:# 867:= 861:: 858:- 851:x 845:# 838:x 810:= 807:a 792:; 789:a 777:: 768:: 749:# 746:a 743:= 740:a 731:# 714:= 708:: 705:- 698:y 695:= 692:x 689:# 669:= 654:: 648:; 642:: 633:: 630:y 617:y 602:. 596:= 593:y 584:2 581:= 569:= 566:y 560:# 533:= 518:: 512:; 506:: 497:: 494:x 481:y 475:x 472:= 469:y 460:x 457:= 448:5 445:= 442:x 430:= 427:x 421:# 275:e 268:t 261:v 112:) 106:( 101:) 97:( 87:· 80:· 73:· 66:· 39:.

Index


verification
improve this article
adding citations to reliable sources
"Structural type system"
news
newspapers
books
scholar
JSTOR
Learn how and when to remove this message
Type systems
Type safety
Strong vs. weak typing
Static
dynamic
Manifest
inferred
Nominal
structural
Duck typing
Abstract
Dependent
Flow-sensitive
Gradual
Intersection
Latent
Refinement
Substructural
Unique

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.