Polymorphic recursion
In computer science, polymorphic recursion (also referred to as Milner–Mycroft typability or the Milner–Mycroft calculus) refers to a recursiveparametrically polymorphicfunction where the type parameter changes with each recursive invocation made, instead of staying constant. Type inference for polymorphic recursion is equivalent to semi-unification and therefore undecidable and requires the use of a semi-algorithm or programmer-supplied type annotations.[1]
Example
Nested datatypes
Consider the following nested datatype in Haskell:
dataNesteda=a:<:(Nested[a])|Epsiloninfixr5:<:nested=1:<:[2,3,4]:<:[[5,6],[7],[8,9]]:<:EpsilonA length function defined over this datatype will be polymorphically recursive, as the type of the argument changes from Nested a to Nested [a] in the recursive call:
length::Nesteda->IntlengthEpsilon=0length(_:<:xs)=1+lengthxsNote that Haskell normally infers the type signature for a function as simple-looking as this, but here it cannot be omitted without triggering a type error.
Higher-ranked types
Applications
Program analysis
In type-based program analysis polymorphic recursion is often essential in gaining high precision of the analysis. Notable examples of systems employing polymorphic recursion include Dussart, Henglein and Mossin's binding-time analysis[2] and the Tofte–Talpin region-based memory management system.[3] As these systems assume the expressions have already been typed in an underlying type system (not necessary employing polymorphic recursion), inference can be made decidable again.
Data structures, error detection, graph solutions
تستخدم هياكل بيانات البرمجة الوظيفية غالبًا الاستدعاء الذاتي متعدد الأشكال لتبسيط عمليات التحقق من أخطاء النوع وحل المشكلات التي تتطلب حلولًا مؤقتة "وسيطة" معقدة تستهلك الذاكرة بشكل كبير في هياكل البيانات التقليدية مثل الأشجار. في المرجعين التاليين، يقدم أوكازاكي (ص 144-146) مثالًا على CONS في لغة هاسكل ، حيث يقوم نظام النوع متعدد الأشكال تلقائيًا بتحديد أخطاء المبرمج. [ 4 ] يتمثل الجانب الاستدعائي في أن تعريف النوع يضمن أن يكون للمنشئ الخارجي عنصر واحد، والثاني زوج، والثالث زوج من الأزواج، وهكذا بشكل استدعائي، مما يُنشئ نمطًا تلقائيًا لاكتشاف الأخطاء في نوع البيانات. يقدم روبرتس (ص 171) مثالًا مشابهًا في لغة جافا ، باستخدام فئة لتمثيل إطار مكدس. المثال المعطى هو حل لمسألة برج هانوي ، حيث يحاكي المكدس الاستدعاء الذاتي متعدد الأشكال مع بنية استبدال مكدس متداخلة في البداية والمؤقتة والنهاية. [ 5 ]
انظر أيضاً
ملحوظات
- ↑ هينجلين 1993 .
- ↑ دوسارت، ديرك؛ هينجلين، فريتز ؛ موسين، كريستيان. "التكرار متعدد الأشكال ومؤهلات النوع الفرعي: تحليل وقت الربط متعدد الأشكال في وقت متعدد الحدود". وقائع الندوة الدولية الثانية للتحليل الثابت (SAS) . CiteSeerX 10.1.1.646.5884 .
- ↑ توفت، مادز ؛ تالبين، جان بيير (1994). "تنفيذ حساب لامدا المكتوب بالقيمة باستخدام مكدس من المناطق". POPL '94: وقائع الندوة الحادية والعشرين لجمعية ACM SIGPLAN-SIGACT حول مبادئ لغات البرمجة . نيويورك، نيويورك، الولايات المتحدة الأمريكية: ACM. الصفحات 188-201 . doi : 10.1145/174675.177855 . ISBN 0-89791-636-0.
- ↑ كريس أوكازاكي (1999). هياكل البيانات الوظيفية البحتة . نيويورك: كامبريدج. ص 144. ISBN 978-0521663502.
- ↑ إريك روبرتس ( 2006). التفكير التكراري باستخدام جافا . نيويورك: وايلي. ص 171. ISBN 978-0471701460.
للمزيد من القراءة
- ميرتينز، لامبرت (1983). "التحقق التدريجي من النوع متعدد الأشكال في لغة B" (ملف PDF) . ندوة ACM حول مبادئ لغات البرمجة (POPL)، أوستن، تكساس .
- Mycroft, Alan (1984). "Polymorphic type schemes and recursive definitions". International Symposium on Programming, Toulouse, France. Lecture Notes in Computer Science. Vol. 167. pp. 217–228. doi:10.1007/3-540-12925-1_41. ISBN 978-3-540-12925-7.
- Henglein, Fritz (1993). "Type inference with polymorphic recursion". ACM Transactions on Programming Languages and Systems. 15 (2): 253–289. CiteSeerX 10.1.1.42.3091. doi:10.1145/169701.169692. S2CID 17411856.
- Kfoury, A. J.; Tiuryn, J.; Urzyczyn, P. (April 1993). "Type reconstruction in the presence of polymorphic recursion". ACM Transactions on Programming Languages and Systems. 15 (2): 290–311. doi:10.1145/169701.169687. ISSN 0164-0925. S2CID 18059949.
- Michael I. Schwartzbach (June 1995). "Polymorphic type inference". Technical Report BRICS-LS-95-3.
- Emms, Martin; Leiß, Hans (1996). "Extending the type checker for SML by polymorphic recursion—A correctness proof". Technical Report 96-101.
- Richard Bird and Lambert Meertens (1998). "Nested Datatypes".
- C. Vasconcellos, L. Figueiredo, C. Camarao (2003). "Practical Type Inference for Polymorphic Recursion: an Implementation in Haskell". Journal of Universal Computer Science.
- L. Figueiredo, C. Camarao. "Type Inference for Polymorphic Recursive Definitions: a Specification in Haskell".
- Hallett, J. J; Kfoury, A. J. (July 2005). "Programming Examples Needing Polymorphic Recursion". Electronic Notes in Theoretical Computer Science. 136: 57–102. doi:10.1016/j.entcs.2005.06.014. hdl:2144/1532. ISSN 1571-0661.
External links
- Standard ML with polymorphic recursion by Hans Leiß, LMU Munich
- Polymorphism (computer science)
- Recursion
- Object-oriented programming
