Annotation of researchv10no/cmd/sml/doc/refman/man.dvi, revision 1.1

1.1     ! root        1: �&���;� TeX output 1992.09.17:0837������l�����'���������U��9D��tG�G�cmr17�Standard�7tML�Reference�Man��qual�����}�(PRELIMINAR���VY)��,�&���<��+X�Qcmr12�Septem��rb�S�er��17,�1992�����*�l�����'����UU������"V
        !             2: 
        !             3: cmbx10�Abstract����R�K�`y
        !             4: 
        !             5: cmr10�This�:man���ual�is�a�ma��8jor�revision�b�y�Andrew�W.�App�Gel�and�Da�vid�B.�MacQueen����Rof���the���':
        !             6: 
        !             7: cmti10�Standar��}'d��PML����reference�man���ual�(ECS-LF�CS-86-2).���That�do�Gcumen�t�is����Rdivided�&&in���to�three�parts:��hthe�Core�language�description�b�y�Robin�Milner,�,the����Rstandard��8I/O��library�b���y�Rob�Gert�W.�Harp�er,��rand�the�Mo�dule�system�b���y�Da�vid�B.����RMacQueen.�RThis��Tnew�man���ual�re
ects�a�sligh�tly�ev�olv�ed�language,�      Tand�describ�Ges����Rthe��/library�functions�(initial�en���vironmen�t)��/built�in���to�the�Standard�ML��of�New����RJersey�zsystem,�rdev���elop�Ged�at�A��*�T&T�dBell�Lab�oratories�and�Princeton�Univ���ersit�y��*�.����aA���t���presen�t�the�man�ual�is�in�a�v�ery�rough�form,���and�should�b�Ge�considered�a����Rpreliminary��tdraft.�D�This�v���ersion�is�distributed�primarily�as�do�Gcumen�tation�of�the����Rmid-1989�UUdistribution�of�the��Standar��}'d���ML�of�New�Jersey�UU�compiler.�����&&�l�����'���G��,�H�"V�p
        !             8: cmbx10�HChapter��F1��2��,�I�"V�G
        !             9: cmbx10�IIn��4�tro��vduction��4��,�This�x�is�a�preliminary�draft�of�a�reference�man���ual�for�Standard�ML�x�of�New�Jersey��*�,����,an�*�implemen���tation�of�the�Standard�ML�*�language.�c�Our�implemen�tation�adheres����,closely��to�the�Standard�ML���language�denition�����^��ٓ�Rcmr7�1���
�{�except�that�the�library�of����,built-in�UUfunctions�is�larger�and�b�Getter�organized.����;The�)�do�Gcumen���t�at�presen�t�has�man�y�gaps.�c2It�should�b�Ge�treated�as�a�guide�to����,the�UU1989�pre-release,�rather�than�as�a�complete�reference�man���ual.��,���Éff��v�     J=����"5��-:��Aa�cmr6�1���L��
|{Ycmr8�see�}�#�f�cmti8�The�_ADenition�of�Standar���d�ML,�version�3�,�;b�Îy�Rob�<rert�Harp�er,�;Robin�Milner,�and�Mads��      ��T��J�ofte,��$tec�Îhnical�k�rep�<rort�ECS-LF�CS-89-81,��$Dept.���of�Computer�Science,�Univ.���of�Edin�Îburgh,���Edin�Îburgh��XEH9�3JZ�Scotland.�������1������l�����'���I؍�R�HChapter��F2��4؍�R�ILexical�   ��analysis��<؍��R�7�"Vff
        !            10: cmbx10�2.1��w�@Reserv��=ed��w�ords���͍�R�The�kfollo���wing�are�reserv�ed�w�ords.��DThey�ma�y�not�b�Ge�used�as�iden�tiers.��DIn�this����Rdo�Gcumen���t�UUthe�alphab�etic�reserv���ed�w�ords�are�alw�a�ys�sho�wn�in�b�Goldface.��S���k���<x
        !            11: 
        !            12: cmtt10�abstraction�?�abstype�and�andalso�as�case�datatype�else����kend�?�exception�do�fn�fun�functor�handle�if�in�infix����kinfixr�?�let�local�nonfix�of�op�open�overload�raise�rec����ksharing�?�sig�signature�struct�structure�then�type�val����kwhile�?�with�withtype�orelse��7���k{�
        !            13: �}�[�]�,�;�(�)�->�*�|�:�...�=�=>�#�_��,A򍍑R�2.2��w�@Sp�u�ecial��constan��=ts����R�An��in���teger�constan�t�is�an�y�non-empt�y�sequence�of�digits,��+p�Gossibly�preceded�b�y����Ra�UUnegation�sym���b�Gol�(�~�).��؍�aA���real��constan���t�is�an�in�teger�constan�t,��rp�Gossibly�follo�w�ed�b�y�a�p�Goin�t�(.)���and����Rone�K�or�more�digits,�M�p�Gossibly�follo���w�ed�K�b�y�an�exp�Gonen�t�sym�b�Gol(E)�K�and�an�in�teger����Rconstan���t;��Fat�i�least�one�of�the�optional�parts�m�ust�o�Gccur,���hence�no�in�teger�constan�t����Ris��      a�real�constan���t.�9�Examples:��/�0.7��,����~3.32E5��,��3E~7��.�9�Non-examples:��/�23��,��.3��,����R�4.E5�UU�,��1E2.0��.����aA��)string��<constan���t�is�a�sequence,��vb�Get�w�een�quotes�(�"�),��vof�zero�or�more�prin�t-����Rable��;c���haracters,��uspaces,�or�escap�Ge�sequences.�zEac���h�escap�e�sequence�is�in���tro-����Rduced���b���y�the�escap�Ge�c�haracter��\�,��and�stands�for�a�c�haracter�sequence.�;�The����Rallo���w�ed�yyescap�Ge�sequences�are�as�follo���ws�(all�other�uses�of��\��b�eing�incorrect):������2����  ؠl�����'���6�������d���2�\n�����b���A�UUsingle�c���haracter�in�terpreted�b�y�the�system�as�end-of-line.��������2�\t�����b���T��*�ab.��������2�\^c�����b���The�UUcon���trol�c�haracter�c,�for�an�y�appropriate�c.��������2�\ddd�����b���The�UUsingle�c���haracter�with�ASCI�GI�co�de�ddd�(3�decimal�digits).��������2�\"�����b���The�UUdouble-quote�c���haracter�(�"�).��������2�\\�����b���The�UUbac���kslash�c�haracter�(�\�).��������2�\f___f\�����b���This���sequence�is�ignored,�=wwhere�f�����ff���32�ff����̉ff��
        !            14: ��f�stands�for�a�sequence�of����b��one�1�or�more�formatting�c���haracters�(a�subset�of�the�non-prin�table����b��c���haracters��including�at�least�space,��tab,�newline,�formfeed).�W�This����b��allo���ws���one�to�write�long�strings�on�more�than�one�line,��!b�y�writing����b���\�UU�at�the�end�of�one�line�and�at�the�start�of�the�next.��������Y�����,�2.3��Q�@Iden��=tiers�����,�An�=iden���tier�is�either��alphanumeric�:�e�an�y�sequence�of�letters,�A�digits,�primes�=(�'�),����,and��punderbars�(�_�)�starting�with�a�letter�or�a�prime,���or��symb��}'olic�:���an���y�sequence����,of�UUthe�follo���wing�sym�b�Gols���Ѝ�E�!�?�%�&�$�+�-�/�:�<�=�>�?�@�\�~�\^�|�#�*�`���э�,�In��Neither�case,�fho���w�ev�er,�reserv�ed��Nw�ords�are�excluded.�        f�This�means�that�for����,example�UU�_��and��|��are�not�iden���tiers,�but��also_ran��and��|=|��are�iden�tiers.����;Iden���tiers�are�used�to�stand�for�9�dieren�t�classes�of�ob��8jects,���whic�h�o�Gccup�y����,6�UUdieren���t�name�spaces,�as�follo�ws:�������88�1.����Ev��q�alue�UUv�ariables�(�var�),�v�alue�constructors�(�c��}'on�),����Eexception�UUconstructors�(�exnc��}'on�)���h�����88�2.����Et���yp�Ge�UUv��q�ariables�(�tyvar�)�������88�3.����Et���yp�Ge�UUconstructors�(�tyc��}'on�)�������88�4.����Erecord�UUlab�Gels�(�lab�)�������88�5.����Estructures�UU(�str�),�functors�(�fct�)�������88�6.����Esignatures�UU(�sgn�)���э�,Th���us,�m�an�h�iden�tier�could�not�in�the�same�scop�Ge�stand�for�b�oth�a�v��q�alue�v�ariable����,and��&a�constructor,��but�an�iden���tier�can�b�Ge�b�ound�sim���ultaneously�to�a�t�yp�Ge����,constructor�UUand�a�signature.����;T��*�o��jremo���v�e�some�am�biguit�y��*�,���it�is�recommended�that�constructors�start�with����,an�DBupp�Gercase�letter,��and�v��q�ariables�start�with�a�lo���w�ercase�DBletter;���but�this�is�a����,con���v�en�tion,��Pnot�UQan�enforced�rule�(it�is�confounded,�for�example,�b���y�sym�b�Golic����,iden���tiers).����;A���t���yp�Ge���v��q�ariable�(�tyvar�)�ma�y�b�Ge�an�y�alphan�umeric�iden�tier�starting�with�a����,prime.��The�S�other�eigh���t�classes�(�var,��Ec��}'on,�tyc�on,�...�)��are�S�represen���ted�b�y�iden�tiers������3����&�l�����'������R�not��cstarting�with�a�prime.�OwThe�class�lab�is�also�extended�to�include�the�n���umeric����Rlab�Gels�UU1,�2,�3,�...�q�.����aT���yp�Ge�x�v��q�ariables�are�therefore�disjoin�t�from�the�other�classes.���Otherwise,���the����Rclass�UUof�an�o�Gccurrence�of�an�iden���tier�is�determined�from�con�text.����aSpaces�
kor�paren���theses�are�sometimes�needed�to�separate�sym�b�Golic�iden�tiers����Rand�UUreserv���ed�w�ords.�q�Tw�o�examples�are��qǍ�����d���t��a:=�?�!b�������or����ʄ�a:=(!b)���&�g�but�UUnot���&F���a:=!b������g~�?�:int->int�������or�������(~):int->int���&�g�but�UUnot���&9q��~:int->int��������a�These���punctuation�c���haracters�cannot�b�Ge�constituen�ts�of�iden�tiers�and�there-����Rfore�UUnev���er�need�spaces�around�them:����?��"�?�(�)�,�.�;�[�]�{�}��!č��R�2.4��w�@Commen��=ts�����R�A�Bcommen���t�Bais�a�c�haracter�sequence�(outside�of�a�string)�within�commen�t�brac�k�ets����R(*�UU*)�in�whic���h�commen�t�brac�k�ets�are�prop�Gerly�nested.�����R�2.5��w�@The��bare�syn��=tax�����R�The��^Standard�ML��9bare�language�is�obtained�b���y�stripping�the�full�language�of����Ran���y�[�derive��}'d��forms�(those�that�ma�y�b�Ge�dened�in�terms�of�other�constructs�in����Rthe�֒language),���and�of�an���y�constructs�related�to�the�mo�Gdule�system.��}The�bare����Rlanguage���will�b�Ge�explained�in�Chapters�3�and�4,��and�successiv���e�c�hapters�describ�Ge����Raugmen���tations�UUof�it�that�yield�the�full�language.����aFigure�UU2.5�sho���ws�the�syn�tax�of�the�bare�language.�q�The�notation����z&phrase�UUx������k���&���fe|u��'�x�phrase����Rindicates��the�rep�Getition�of�the��phr��}'ase��at�least���b>
        !            15: 
        !            16: cmmi10�k�_c�times,��separated�b���y�the�punctu-����Ration�UUc���haracter��x�.������4����,�l�����'�����Ǎ����,�Figure�UU2.5:�q�Syn���tax�of�the�Bare�Language�����9�������Ǎ�����lab����!",�
        !            17: 
        !            18: cmsy10�!���E�INT����Eid��!ȍ���*�const����!���E�INT����EREAL����ESTRING��������exp����!���E�id����Econst����E�f�UU�lab�=�exp�,������0���&���fe@����,�lab�=�exp��g����E�let�UU�dec��in��exp��end����E�exp�UUexp����Eexp�UU:�q�t���y����E�fn�UU�matc���h����E�raise�UU�exp����Eexp�UU�handle��matc���h������5matc���h����!���E�pat�UU�=>��exp��j������1���&���fe@����j��pat��=>��exp�������5apat����!���E�id����Econst����E�_����E�(�UUpat�)����E�f�UU�lab�=�pat�,������0���&���fe@����,�lab�=�pat��g����Ef�UU�lab�=�pat�,������0���&���fe@����,�lab�=�pat�,�...�q��g�������6�pat����!���E�apat����Eid�UUapat����Epat�UU:�q�t���y����Eid�UUconstrain���t��as��pat������#pt���y����!���E�tyvar����E�f�UU�lab�:�q�t���y�,������0���&���fe@����,�lab�:�t���y��g����E�(�UUt���y�)����E(�UUt���y�,������2���&���fe@����,�t�y�)�id����Et���y�UUid����Eid����Et���y�UU�->��t�y������!*�vb����!���E�pat�UU�=��exp����Evb�UU�and������1���&���fe@����and��vb������
��constrain���t����!���R���empty����E�:�q�t���y��������Ǎ���ο�rvb����!������id�UUconstrain���t��=�fn��matc�h������rvb�UU�and������1���&���fe@����and��rvb�������7tb����!������t���yv��q�ars�UUid��=��t�y������tb�UU�and������1���&���fe@����and��tb�������t���yv��q�ars����!������empty�������tyvar�������(�UU�tyvar��,������2���&���fe@����,��tyvar��)�������c�db����!������t���yv��q�ars�UUid��=��constr��j������1���&���fe@����j��constr������db�UU�and������1���&���fe@����and��db������¿�constr����!������id������id�UU�of��t���y��������eb����!������id������id�UU�of��t���y������id�UU�=��id������eb�UU�and������1���&���fe@����and��eb�������8dec����!������val�UU�vb�������val�UUrec��rvb�������type�UU�tb�������datatype�UU�db�������exception�UU�eb�������local�UU�dec��in��dec��end�������dec�UU;������dec�UUdec����������5����#��l�����'���PQ��R�HChapter��F3��;Q��R�IEv��ialuation��CQ���R�3.1��w�@En��=vironmen�ts��and�V����alues��&���R�Ev��q�aluation�ѹof�phrases�tak���es�place�in�the�presence�of�an��envir��}'onment��and�a��stor�e�.����RAn�_��value���envir��}'onment��E�_�maps�iden���tiers�to�v��q�alues,�b�v�alue�_�constructors,�and�ex-����Rception�[:constructors.��wA�[9�stor��}'e��S�maps�references�to�v��q�alues;�^-references�are�them-����Rselv���es�T�v��q�alues.�q�(T�yp�Ge�en�vironmen�ts�TE,�are�ignored�here�since�they�are�relev��q�an�t����Ronly���to�t���yp�Ge-c�hec�king���and�compilation,��Cnot�to�ev��q�aluation.��Other�en���vironmen�ts����Rha���ving�UUto�do�with�the�mo�Gdule�system�are�also�ignored�here.)��Q��aA��uv��q�alue����v���is�either�a�constan���t�(a�n�ullary�constructor),�2a�construction�(a����Rconstructor�oapplied�to�a�v��q�alue),�N5a�record,�a�reference,�or�a�function�v��q�alue.��A����Rrecord�O�v��q�alue�is�a�set�of�lab�Gel-v�alue�pairs,�P�written��f��lab�Gel�=�v�alue�,����&�0��Pџ&���fe@�����,�P�lab�Gel�=����Rv��q�alue�ú�g�,��Sin�whic���h�the�lab�Gels�are�distinct;���note�that�the�order�of�comp�onen���ts�is����Rimmaterial.��&Lab�Gels�ema���y�b�e�iden���tiers�or�in�teger�n�umerals;�mb�Goth�kinds�of�lab�els����Rma���y�>�app�Gear�in�the�same�record�v��q�alue.�-�A�>Yfunction�v�alue�is�a�partial�function����Rwhic���h,�*Bgiv�en�}a�v��q�alue,�ma���y�return�a�v��q�alue�or�ma�y�raise�an�exception;�1pit�ma�y�also����Rc���hange�UUthe�store�as�a�side-eect.����aAn���exception��e�,�ʨasso�Gciated�to�an�exception�name��exn��in�a�v��q�alue�en���vironmen�t,����Ris��&a�sp�Gecial�kind�of�constructor.�7&Exception�constructors�ma���y�b�e�n���ullary�or�v��q�alue-����Rcarrying,�}yjust�G�as�ma���y�ordinary�constructors.��Nullary�exception�constructors,�and����Rconstructed�F�exceptions�(exception�constructors�applied�to�v��q�alues),�I�are�ordinary����Rv��q�alues�UUof�t���yp�Ge��exn�.����aEv��q�aluation���of�a�phrase�returns�a��r��}'esult�|a�v�alue��v�[��,���a�v�alue-en���vironmen�t����E����,����Ror�UUa�store��S����as�follo���ws:������6����,P�l�����'����������d���H���Phrase������V��
        !            19: �alue������A�Expression�����v�UUand�S,�or�raise(v)�and�S������AV��*�alue�UUbinding�����E�UUand�S,�or�raise(v)�and�S������AT���yp�Ge�UUbinding������no���ee��}'ct�on�E�or�S������A�Datat���yp�Ge�UUbinding�����E������AException�UUbinding�����E������ADeclaration�����E�UUand�S,�or�raise(v)�and�S������/s��;The�`�remainder�of�this�c���hapter�describ�Ges�the�seman�tics�of�v��q�arious�phrases�in����,terms�UUof�v��q�alues��v��.�and�en���vironmen�ts�UU�E����.��&V��;The��seman���tics�of�stores�(�S����)�are�discussed�in�Chapter�10;���for�the�remainder����,of���this�c���hapter,��mthe�eect�of�v��q�arious�phrases�on�stores�will�b�Ge�ignored.�-5The����,seman���tics�UUof�exceptions�is�discussed�in�Chapter�9.����;F��*�or�8�ev���ery�phrase�except�a��handle��expression,�q�whenev�er�its�ev��q�aluation�de-����,mands�?Wthe�ev��q�aluation�of�an�immediate�subphrase�whic���h�returns�a�raised�excep-����,tion����r��}'aise(v)��as�a�result,��lno�further�ev��q�aluation�of�subphrases�o�Gccurs�and��r�aise(v)����,�is�|2also�the�result�of�the�phrase.��_This�rule�should�b�Ge�remem���b�ered�while�reading����,the�UUev��q�aluation�rules�b�Gelo���w.����;In�r�presen���ting�the�rules,��explicit�t�yp�Ge�constrain�ts�(:t�y)�ha�v�e�b�Geen�ignored�since����,they�UUha���v�e�no�eect�on�ev��q�aluation.��!ٍ��,�3.2��Q�@En��=vironmen�t��manipulation��闍�,�W��*�e�m�ma���y�write��f�i��&��1��l�7!���v��&��1��|s�;�&��:::;�i��&��      0e�rcmmi7�n��a#�7!��v��&��n��q~�g�m��for�a�v��q�alue�en���vironmen�t�m�E�m�(the�iden���tiers����,�i��&��k��t�b�Geing���distinct).�OThen��E����(�i��&��k����)�denotes��v��&��k���,��N�fg��is�the�empt���y�v��q�alue�en�vironmen�t,����,and���E���+��R�E������^��
        !            20: O!�cmsy7�0��2��means�the�en���vironmen�t��in�whic���h�the�asso�Gciations�of��E������^��0���sup�Gersede����,those�UUof��E����.�����,�3.3��Q�@Matc��=hing��patterns��闍�,�P���atterns�PRserv�e�in�ML�PQb�Goth�as�formal�parameters�of�functions,�QSand�as�indices�in����,case�{statemen���ts.�W�When�a�function�or�a�case�statemen�t�is�applied�to�a�particular����,v��q�alue,�UUone�of�its�patterns�ma���y�matc�h�the�v��q�alue.��&V��;A���n���ullary���constructor�(lik�e��nil�)�used�as�a�pattern�will�matc�h�only�the�corre-����,sp�Gonding��Ln���ullary�constructor�v��q�alue.�1�A��;v�alue-carrying��Lconstructor��c�,��Iapplied�to����,a��Ypattern��p��&��1��|s�,��%mak���es�a�pattern��c�(�p��&��1���);��this�constructed�pattern�will�matc���h�a�v��q�alue����,�c�(�v��&��1��|s�)�#built�with�the�same�v��q�alue-carrying�constructor,�-and�only�if�the�pattern��p��&��1�����,�matc���hes�UUthe�v��q�alue��v��&��1��|s�.����;A��Rrecord��ipattern��{���lab����@�&��1����=���p��}'at����ßqƳ1��ҟ�,���n�&���fe      ����p�,���n�lab����E�&��n���,�=���p��}'at����ßqƴn��Ǫ�}��matc���hes�a�record�v��q�alue����,whose�X�lab�Gels�are�the�same,�Y�and�only�if�eac���h�pattern�matc�hes�the�corresp�Gonding����,v��q�alue.�0MIf��,the�ellipsis�(�...�)�is�used�at�the�end�of�a�record�pattern,�bthen�it�will����,matc���h�UUa�record�v��q�alue�with��at���le��}'ast��the�lab�Gels�sp�ecied�in�the�pattern.����;An�{giden���tier�(i.e.���a�v��q�ariable)�used�as�a�pattern�will�matc�h�an�y�v��q�alue;��pwhen����,this���happ�Gens,�R�the�v��q�ariable�will�also�b�e�b�ound�to�that�v��q�alue�throughout�the������7����5�l�����'������R�v��q�ariable's�F�scop�Ge.�E�When�a�v�ariable�is�a�comp�Gonen���t�of�a�record�or�constructor����Rpattern,�T�then��Git�is�b�Gound�to�a�particular�comp�onen���t�of�the�record�v��q�alue�or����Rconstructed�UUv��q�alue.����aAn�UUunderscore�will�matc���h�an�y�v��q�alue.����aSometimes��oit�is�desired�to�bind�a�v��q�ariable�to�a�v�alue�only�if�the�v�alue�matc���hes����Ra�sLparticular�pattern.�ˬThe�pattern��id�?��as��p��}'at�sL�binds�the�v��q�alue�to�the�v�ariable��id�,����Rbut�UUonly�if�the��p��}'at��matc���hes.����aT���yp�Ge��Dconstrain�ts�ma�y�b�Ge�applied�to�patterns.�       i��p��}'at�:�ty��matc�hes�the�same����Rv��q�alues�A6that��p��}'at��do�Ges,�E<but�the�compiler�will�guaran���tee�that�the�pattern�will�only����Rb�Ge�UUapplied�to�v��q�alues�of�t���yp�e��ty�.��e���R�0��N�cmbx12�A��more�formal�description��uT��R�The��fmatc���hing�of�a�pattern�to�a�v��q�alue��v��?�either��fails��or�yields�a�v�alue�en���vironmen�t.����RF��*�ailure�LRis�distinct�from�raising�an�exception,��Sbut�an�exception�will�b�Ge�raised�when����Rall�m�patterns�fail�in�applying�a�matc���h�to�a�v��q�alue�(see�Sections�3.5,�s�3.6,�and�m�9.2).����RIn���the�follo���wing�rules,���if�an�y�comp�Gonen�t�pattern�fails�to�matc�h�then�the�whole����Rpattern�UUfails�to�matc���h.����aThe��
follo���wing�is�the�eect�of�matc�hing�a�pattern�to�a�v��q�alue��v�V��in�the�en�vi-����Rronmen���t�'��E����,�1for�eac�h�of�the�kinds�of�pattern�(with�failure�if�an�y�condition�is�not����Rsatised):��:������hO��ff&f����š��(undersc��}'or�e)���the�empt���y�v��q�alue�en�vironmen�t��fg��is�re-����š�turned���R����g�d�id���š��if�>��E����(�id�)�is�not�a�constructor,�Cpthen�the�v��q�alue�en���viron-����š�men���t�UU�f�id���7!��v�[��g��is�returned������g�d�id���š��if����E����(�id�)�is�a�n���ullary�constructor,��then�if��E��(�id�)���=��v�[��,����š�then�UU�fg��is�returned,�else�failure������g�d�id��Tpat���š��if�b��E����(�id�)�is�a�v��q�alue-carrying�constructor��c�,��Cand��v�"��=���cv��[ٟ�^��0��*�,����š�then�UUpat�is�matc���hed�to��v��[ٟ�^��0��*�,�else�failure.������g�d�id��T�as��pat���š��pat�W~is�matc���hed�to��v��W�returning��E����;�X�then��f�id�ʱ�7!��v�[��g�:Q�+��E����š��is�UUreturned.������g�d�f���T�lab����+�&��1����=���T�p��}'at���Ю�qƳ1��"u�,���T�&���fe���*�,���T�lab����+�&��n��&��=���T�p��}'at���Ю�qƴn����g���&�,�if�J.�v���=�_*�f��lab����ן&��1���t�=��v��&��1��|s�;�&��:::;���lab�����&��n��a'�=�_*�v��&��n��q~�g����š��then��E�p��}'at���&��qƴi��\0�is�Ematc���hed�to��v��&��i��Z��returning��E��&��i��TL�,�2�for�eac�h��i�;����š�then�UU�E��&��1���S�+�8��:::��+��E��&��n�����is�returned.������g�d�f���T�lab����+�&��1����=���T�p��}'at���Ю�qƳ1��"u�,���T�&���fe���*�,���T�lab����+�&��n��&��=���T�p��}'at���Ю�qƴn����,�?�...��g���&7   �if�8��v�"��=���f��lab��������׵0���ō��׳1���)b�=��v��&��1��|s�;�&��:::;���lab���������0���ō���m����2�=����š��v��&��m�����g��0�then�if�the���lab�����&��i����are�a�subset�of�the���lab���������0���ō���j������then���p��}'at���抟qƴi�����š��is�<Umatc���hed�to��v��&��j��s&�returning��E��&��i��TL�,�AUfor�eac�h��i;�&��j����suc�h�that�����š��lab���҇��&��i��أ"�=����lab���������0���ō���j�����;�UUthen��E��&��1���S�+�8��:::��+��E��&��n�����is�UUreturned.��:���RNo�w�pattern�ma���y�con�tain�the�same�v��q�ariable�t�wice.���No�record�pattern,��Arecord����Rexpression,�UUor�record�t���yp�Ge�ma�y�use�the�same�lab�Gel�t�wice.������8����        C(�l�����'�������,�3.4��Q�@Applying��a�matc��=h��e��,�Assume��+en���vironmen�t��E����.�<�Applying�a�matc�h���p��}'at������qƳ1����=�>����exp����D�qƳ1����j���:::��j���p��}'at����r�qƴn����=�>���exp����D�qƴn�����to����,v��q�alue�x)�v���returns�a�v�alue�or�raises�an�exception�as�follo���ws.��DEac�h��x)�p��}'at���s��qƴi��?��is�x)matc�hed����,to�)=�v���in�turn,�^6from�left�to�righ���t,�un�til�)=one�succeeds�returning��E��&��i��TL�;��1then���exp���$i�qƴi�����is����,ev��q�aluated�ʴin��E�&�,�+�&#��E��&��i��TL�.�C�If�none�succeeds,��nthen�an�exception�is�raised,�dep�Gending�on����,the���syn���tactic�con�text�in�whic�h�the�matc�h�o�Gccurs�(see�Sections�3.5,�
�3.6,�and���9.2).����,If�q7a�matc���h�con�tains�a�redundan�t�pattern�(where�an�y�v��q�alue�that�could�satisfy�it����,will�1�b�Ge�matc���hed�b�y�a�previous�pattern�in�the�matc�h),�i
        !            21: the�compiler�will�issue����,a���w���arning�message.��If�the�matc�h�(except�those�that�form�exception-handlers)����,is�q�not�exhaustiv���e�(some�v��q�alue�matc�hes�none�of�the�patterns)�then�the�compiler����,will�UUissue�a�w���arning�message.�����;Th���us,�UUfor�eac�h��E����,�a�matc�h�denotes�a�function�v��q�alue.��!y9���,�3.5��Q�@Ev��{aluation��of�expressions����,�Ev��q�aluating�U�an�expression�in�the�en���vironmen�t�U��E��+�returns�a�v�alue�(or�raises�an����,exception)�UUas�follo���ws,�in�eac�h�of�the�cases�for�exp:��:�����A�d�id�������the��\v��q�alue��E����(�id�)�is�returned;��`id�ma���y�b�Ge�a�v�ariable-id�������or�UUa�constructor-id��N����A�d�const�������the��v��q�alue�denoted�b���y�the�constan�t�(an�in�teger,�1real,�������or�UUstring�literal)�is�returned.�������A�d�exp���Si   �qƳ1���W�|�exp���i�!�qƳ2���������exp����\��qƳ1����is�+ev��q�aluated,�3�returning�function��f�&��;�9,then���exp���&D�qƳ2�����is�������ev��q�aluated,�UUreturning�v�alue��v�[��;�then��f�&��(�v��)�is�returned.������A�d�f���T�lab����+�&��1����=���T�exp���Ѐ�qƳ1��"G�,���T�&���fe���*�,���T�lab����+�&��n��&��=���T�exp���Ѐ�qƴn��R�g������the��8��exp���4�qƴi���>�are�8�ev��q�aluated�in�sequence,�������from�G�left�to�righ���t,�J�returning��v��&��i���*�resp�Gectiv�ely;�L[then�the�������record�UU�f��lab����ן&��1��)b�=���v��&��1��|s�;�&��:::;���lab�����&��n����=��v��&��n��q~�g�UU�is�returned.������A�d�raise��T�exp�������exp�        �is�ev��q�aluated,�wreturning��v�[��;��3then�the�exception-�������v��q�alue�UU�v��.�is�raised.������A�d�exp��T�handle��matc��9h������exp�h�is�ev��q�aluated;��_if�exp�returns�a�v�alue��v�[��,���then��������v�G6�is��]returned.�3�If�exp�raises�an�exception��e��then�the�������matc���h�/6is�applied�to��e�.��jIf�the�matc�h�fails,�e�then��e��is�������raised��(as�the�v��q�alue�of�the��handle��expression).�[�If�the�������matc���h�2succeeds,�9then�the�resulting�v��q�alue�is�returned.������A�d�let��T�dec��in��exp��end����1��dec���is�ev��q�aluated,���returning��E������^��0��a��;��then�exp�is�ev�al-�������uated�UUin�the�en���vironmen�t�UU�E��m�+�8��E������^��0��a��.������A�d�fn��T�matc��9h�������f����is��9returned,��where��f��is�the�function�of��v��gained�������b���y�Kapplying�matc�h�to��v�s$�in�the�en�vironmen�t��E����,�G�and������9����
        !            22: Q��l�����'������š��suc���h�r�that�if�the�matc�h�fails,�zan�exception��Match��will����š�b�Ge��raised.��dBut�matc���hes�that�ma�y�fail�are�to�b�Ge�de-����š�tected��b���y�the�compiler�and�
agged�with�a�w�arning;����š�see�UUSection�3.4.�� �8���R�3.6��w�@Ev��{aluation��of�v�alue�bindings�����R�Ev��q�aluating��a�v�alue�binding�in�en���vironmen�t���E��A�returns�a�v�alue�en���vironmen�t���E������^��0�����R�or�UUraises�an�exception�as�follo���ws:��M|����g�d�pat��T�=��exp���š��exp���is�ev��q�aluated�in��E����,���returning�v�alue��v�[��;��then�pat�is����š�matc���hed�s�to��v�[��;���if�this�returns��E������^��0���s�then��E������^��0���is�returned.����š�If��the�pattern�fails�to�matc���h�then�the�exception��Bind����š��will���b�Ge�raised.��FBut�bindings�that�ma���y�fail�are�to�b�e����š�detected�˿b���y�the�compiler�and�
agged�with�a�w�arning;����š�see�UUSection�3.6.��&������g�d�vb���t#��&��1��|u|�and���T�&���fe���*�and���T�vb���J��&��n����ڊ��The�[yv��q�alue�bindings���vb����ʟ&��1�����through���vb����ʟ&��n�����are�ev�alu-����š�ated��in��E����from�left�to�righ���t,� �returning��E��&��1��|s�;�&��:::E��&��n��q~�;�|then����š��E��&��1���S�+�8��:::��+��E��&��n�����is�UUreturned.������g�d�rec��T�vb���š�vb����is�ev��q�aluated�in��E������^��0��a��,�ireturning��E������^��00�����,�where��E������^��0�����=����š��E���+�`�E������^��00�����.�"8Because��%the�v��q�alues�b�Gound�b���y��rec��vb��m�ust����š�b�Ge�m�function�v��q�alues,��C�E������^��0�����is�w���ell�dened�b�y�\t�ying�knots"����š�(Landin).��M}��RNo�UUbinding�ma���y�bind�the�same�iden�tier�t�wice.����aF��*�or��Neac���h�v��q�alue�binding�\pat�=�exp"�the�compiler�will�issue�a�w�arning�mes-����Rsage��if��either��pat�is�not�exhaustiv���e��or��pat�con�tains�no�v��q�ariable.�L�This�will�(on����Rb�Goth��gcoun���ts)�detect�a�mistak�en�declaration�lik�e��val�?�nil�=�f(x)��g�in�whic�h�the����Ruser���exp�Gects�to�declare�a�new�v��q�ariable�nil�(whereas�the�language�dictates�that����R�nil�       ��here�is�a�constan���t�pattern,�6�so�no�v��q�ariable�gets�declared).��0Ho�w�ev�er,�6�these����Rw���arnings�UUneed�not�b�Ge�giv�en�at�top�lev�el�in�the�in�teractiv�e�system.�� �:���R�3.7��w�@Ev��{aluation��of�t��=yp�u�e�and�datat�yp�u�e�bindings�����R�The���v��q�alue�en���vironmen�t����E���do�Ges�not�aect�the�ev�aluation�of�t���yp�Ge�bindings�or����Rdatat���yp�Ge���bindings�(�T�&c�E� @�aects�their�t�yp�Ge-c�hec�king�and�compilation).�.�Ev��q�aluation����Rof�va�t���yp�Ge�binding�just�returns�the�empt�y�v��q�alue�en�vironmen�t��fg�;���the�purp�Gose�of����Rt���yp�Ge�UUbindings�is�merely�to�pro�vide�an�abbreviation�for�a�comp�Gound�t�yp�Ge.����aEv��q�aluation���of�a�datat���yp�Ge�binding��db��returns�a�v�alue�en���vironmen�t����E������^��0��V�(it����Rcannot�UUraise�an�exception)�as�follo���ws:���ۍ���g�d�t��9yv��\rars��Tid�=��constr���"�ݟ&��1��*h�j����&���fe���,j����constr���!š�&��n����&�P�New�X�constructors���con������&��1��  �;�&��:::;���con���ꢟ&��n�����š��are���created.�&�The�v��q�alue�en���vironmen�t����E������^��0��8�=��T�f�id��&��1��        R��7!��������10����_�l�����'����������v��&��1��|s�;�&��:::;�id��&��n��    ���7!�;�v��&��n��q~�g���is�returned,�R0where��v��&��i��s��is�either�the�������constan���t�1�v��q�alue���con���qٟ&��i����(if���constr���#0h�&��i��*���is�just���id���
�1�&��i��}�),�i&or�else�������the���function�whic���h�maps��x��to���con���п�&��i��%�(�x�)�(if���constr���"�N�&��i��)t_�is���������id����79�&��i������of�UU�t��9y�).��*������A�d�db���Nu|�&��1��V�C�and���T�&���fe���*�and���T�db����l�&��n�����.#�The�8�bindings���db���
�&��1��|��;�&��:::;���db���q��&��n��3�are�8�ev��q�aluated�from�������left�a�to�righ���t,�d�returning��E��&��1��|s�;�&��:::;�E��&��n��q~�;�g�then�a��E��&��1�����+�A�:::��+��E��&��n���������is�UUreturned.��*���,In�=the�left�hand�side�\t���yv��q�ars�id"�of�a�t�yp�Ge�of�datat�yp�Ge�binding,�Bno�t�yp�Ge-v��q�ariable����,ma���y��app�Gear�t�wice�in�\t�yv��q�ars."�=The�righ�t�hand�side�ma�y�con�tain�only�the�t�yp�Ge����,v��q�ariables�"�men���tioned�on�the�left.��\Within�the�scop�Ge�of�the�declaration�of�\id,"����,an���y���o�Gccurrence�of�the�t�yp�Ge�constructor�\id"�m�ust�b�Ge�accompanied�b�y�as�man�y����,t���yp�Ge��margumen�ts�as�indicated�b�y�the�(p�Gossibly�empt�y)�t�yv��q�ar�sequence�\t�yv��q�ars"����,in�UUthe�declaration.��J���;No�UUbinding�ma���y�bind�the�same�iden�tier�t�wice.��"�덍�,�3.8��Q�@Ev��{aluation��of�exception�bindings��r��,�The�ܺev��q�aluation�of�an�exception�binding�in�an�en���vironmen�t�ܺ�E�pG�returns�an�en���vi-����,ronmen���t�UU�E������^��0����as�follo�ws:�������A�d�id�������A��new��exception�constructor��con��is�generated,�=]and�������the�UUen���vironmen�t��f��id��\j�7!����con���g��is�returned.������A�d�id��T�of��t��9y�������A��new��exception�constructor��con��is�generated,�=]and�������the��%en���vironmen�t��f��id��\j�7!���v�[��g��is�returned,���where��v�0��is�the�������function�UUof��x��that�returns���con���O�(�x�).�������A�d�id���KC��&��1��S�}�=���T�id���
j��&��2��������The��en���vironmen�t��f��id���        �R�&��1����7!���E����(��id���     �R�&��2����)�g��is�returned;�zqthat�������is,����id����.�&��1��)�is�~b�Gound�to�the�same�exception�constructor�as���������id����79�&��2������.�������A�d�eb���MWD�&��1��U��and���T�&���fe���*�and���T�eb���~4�&��n�������The��bindings���eb���e_�&��1�����;�&��:::;���eb���
S��&��n�����are��ev��q�aluated�from�left�������to�,      righ���t,�a�returning��E��&��1��|s�;�&��:::;�E��&��n��q~�;��cthen�,      �E��&��1��Du�+���:::��+��E��&��n�� ���is�������returned.���&��,No�UUbinding�ma���y�bind�the�same�iden�tier�t�wice.��"�덍�,�3.9��Q�@Ev��{aluation��of�declarations��r��,�Ev��q�aluating�-a�declaration�in�a�v�alue�en���vironmen�t�-�E����returns�a�v�alue�en���vironmen�t����,�E������^��0�����or�?�raises�an�exception�as�follo���ws�(as�usual�in�this�c�hapter,�D8the�eect�on�t�yp�Ge����,en���vironmen�ts�UUis�ignored):�������11����l�l�����'��������g�d�val��T�vb���š��The���v��q�alue�binding�vb�is�ev�aluated,���returning��E������^��0��a��;��Tthen����š��E������^��0����is�UUreturned.������g�d�type��T�tb���š��The�UUempt���y�en�vironmen�t��fg��is�returned.������g�d�datatype��T�db���š��db�UUis�ev��q�aluated,�returning��E������^��0��a��,�whic���h�is�returned.������g�d�exception��T�eb���š��eb�UUis�ev��q�aluated,�returning��E������^��0��a��,�whic���h�is�returned.������g�d�local���T�dec������&��1���k�in���T�dec������&��2���end����뵀�dec����'H�&��1��&��is�AXev��q�aluated�in��E����,�|Yreturning��E��&��1��|s�;��Zthen�����š�dec������&��2������is�iuev��q�aluated�in��E����+�a#�E��&��1��|s�,���returning��E��&��2���;��then��E����+�a#�E��&��2�����š��is�UUreturned.�������g�d�dec���xs��&��1�����{�dec�����˟&��2�����š��dec������&��1���8:�is��ev��q�aluated�in��E����,���returning��E��&��1��|s�;�Qythen��dec�����&��2��>k�is����š�ev��q�aluated��.in��E���+�|�E��&��1��|s�,��ereturning��E��&��2���;��then��E��&��1�����+�|�E��&��2��6��is����š�returned.�������g�d�dec��|I�;���š��has�UUthe�same�eect�as��dec�.��!č��R�3.10����Ev��{aluation��of�a�program�����R�The��lev��q�aluation�of�a�program���dec���t��&��1�����;���&���fe    ����^�;���dec���t��&��n�����tak���es�place�in�the�initial�presence����Rof��4the�standard�top-lev���el�en�vironmen�t��E��&��0��"��con�taining�all�the�standard�bindings����R(see�$,App�Gendix�B).��MF��*�or��i��>��0�$,the�top-lev���el�en�vironmen�t��E��&��i��TL�,�W�presen�t�after�the����Rev��q�aluation��of���dec���a^�&��i��Q��in�the�program,���is�dened�recursiv���ely�as�follo�ws:���:�dec���Ċ�&��i�����is����Rev��q�aluated�UUin��E��&��i��1����returning�en���vironmen�t��UU�E������^��0�����&��i��m.�,�UUand�then��E��&��i��d�=���E��&��i��1��ɠ�+��8��E������^��0�����m�&��i��P��.�������12����
x1�l�����'���G��,�HChapter��F4��2��,�IThe�     ��t��4�yp��ve�system��4��,�T���yp�Ge��<c�hec�king�is�m�uc�h�the�same�as�in�the�previous�system.�-|This�c�hapter�has����,not�UUy���et�b�Geen�written.�������13������l�����'���G��R�HChapter��F5��2��R�IDirectiv��4�es��4��R�Directiv���es���are�included�in�ML��ias�(syn�tactically)�a�sub�Gclass�of�declarations.�4�They����Rp�Gossess�UUscop�e,�as�do�all�declarations.�q�The�directiv���es����k�infix�UU�d���id���꧟&��1����o�&���fe     �����"��id���+��&��n�����k�infixr�UU�d���id���꧟&��1����o�&���fe        �����"��id���+��&��n�����R�in���tro�Gduce�&]inx�status�for�the�iden�tiers���id������&��1��^�through���id������&��n��--�.�bThe�digit��d��(optional,����Rwith��Va�default�of�0)�determines�the�precedence,��and�an�inxed�iden���tier�as-����Rso�Gciates�9�to�the�left�if�in���tro�duced�b���y��infix�,�sand�to�the�righ�t�if�in�tro�Gduced�b�y����R�infixr�.�G�Dieren���t�׈inxed�op�Gerators�of�equal�precedence�asso�ciate�to�the�left.�G�As����Rindicated��in�App�Gendix�A,�-�the�precedence�of�inxed�application�is�just�w���eak�er����Rthan�UUthat�of�application.����aThe�UUdirectiv���e����k�nonfix��UU�id���꧟&��1����o�&���fe     �����"��id���+��&��n�����R�cancels�UUinx�status�for�the�named�iden���tiers.����aWhile�ߋan�iden���tier�has�inx�status,��eac�h�o�Gccurrence�of�it�(as�a�v��q�alue�v�ariable����Ror�t�as�a�constructor)�m���ust�b�Ge�inxed�or�else�preceded�b�y��op�.��mNote�that�this����Rincludes�J&o�Gccurrences�of�the�iden���tier�within�patterns,�LEev�en�binding�o�Gccurrences����Rof�UUv��q�ariables.����aSev���eral�8�standard�functions�and�constructors�ha�v�e�inx�status�(see�App�Gendix�A)����Rwith�UUprecedence;�these�are�all�left�asso�Gciativ���e�except�\�::�".�������14�����5�l�����'���G�f��,�HChapter��F6��2�f��,�IStandard�    ��bindings��6ލ�,�ML�43pro���vides�4lthe�record�t�yp�Ge�constructor��f��lab���,k�&��1�����:��:��t��9y������qƳ1���;��&���&���fe       ������;��&���lab�����&��n���x�:��:��t��9y������qƴn��)�g��for�an�y����,set���f��lab���,k�&��i�����g��of�lab�Gels�and�corresp�onding�set��f��t��9y���
        !            23: ağqƴi��
��g��of�t���yp�es.�͡The�language�also����,pro���vides���the�inxed�function-t�yp�Ge�constructor��->�.�R�Otherwise,�
        !            24: Zt�yp�Ge�constructors����,are�UUp�Gostxed.�q�The�follo���wing�are�standard:��񗍍��A�d�T��9yp�Q�e��T�c��}'onstants��(n�ullary�constructors):���&V��unit,�/�b�Go�ol,�exn,�in���t,�real,�������string,�UUinstream,�outstream��񖍍��A�d�Unary��Tt��9yp�Q�e�constructors:������list,�UUref����;The���constructors�unit,�"�b�Go�ol,�and���list�are�fully�dened�b���y�the�follo�wing�as-����,sumed�UUdeclaration��񗍑,�infixr�?�5�::����,type�?�unit�=�{}����,datatype���bool�?�=�true�|�false����,datatype�
        !            25: �'a�?�list�=�nil�|�::�of�{1�:�'a,�2�:�'a�list}��51��;�The���w���ord�\unit"�is�c�hosen�since�the�t�yp�Ge�con�tains�just�one�v��q�alue�\�{}�",��4the����,empt���y�UUrecord.�q�This�is�wh�y�it�is�preferred�to�the�w�ord�\v�oid"�of�Algol-68.���f��;The��t���yp�Ge�constan�ts��int�,��real�,�and���string��are�equipp�Ged�with�sp�ecial�con-����,stan���ts�PNas�describ�Ged�in�section�2.3.�pThe�t�yp�Ge�constructor��ref��is�for�constructing����,reference��Et���yp�Ges;�R�see�Chapter�10.�l�The�t�yp�Ge�constan�t��exn��is�the�t�yp�Ge�of�all�ex-����,ceptions,�O�and��is�a�datat���yp�Ge�con�taining�an�un�b�Gounded�n�um�b�Ger�of�constructors����,generated�UUb���y��exception��bindings�(see�Chapter�9).����;The���initial�top-lev���el�en�vironmen�t�is�comprised�of�a�set�of�standard�bind-����,ings.��hThe�~5initial�en���vironmen�t�~5is�m���uc�h�~5more�extensiv���e�than�the�en�vironmen�t����,describ�Ged�ǯin��The��Denition�of�Standar��}'d�ML�,�ǯand�is�almost�a�prop�er�sup�erset.����,The�UUdierences�are:�������15������l�����'���������`�������k�All�&�v��q�alues,�Ft���yp�Ges,�datat�yp�Ges,�etc.�U�are�&�group�ed�in���to�structures;�sthese�struc-����ktures��Aare�op�Gened�in�the�initial�en���vironmen�t��Aso�that�the�names�can�b�e�used����kwithout�UUa�structure-name�qualication.�������`�������k�There�UUare�man���y�additional�initial�bindings,�describ�Ged�in�App�endix�B.�������`�������k�The�sfunctions��input��and��output��are�curried�in�Standard�ML�eof�New�Jer-����ksey��*�,�q�e.g.���input:�?�instream->int->string�8��instead�of��input:�instream*int->string�.�������`�������k�The��   in���teger�functions��+�,�v�-�,��div�,��mod�,��*�,��~�,��abs��       �all�raise��Overflow��if�the����kresult�0is�out-of-range,�>�rather�than��Sum�,��Diff�,��Div�,��Mod�,��Prod�,��Neg�,��Abs�,����kresp�Gectiv���ely��*�.�������`�������k�The��in���teger��div��function�rounds�to�w�ard�zero,��and��x�?�mod�y���is�dened�as����k�x-y*(x�?�div�y)�.����aStandard��ML���of�New�Jersey�is�distributed�with�a�structure��Standard��that����Rma���y���b�Ge�loaded�in�to�the�initial�en�vironmen�t�to�sim�ulate�en�vironmen�t�describ�Ged����Rin�UUthe��Denition�.�������16�����Y�l�����'���GEэ�,�HChapter��F7��2Eэ�,�IDeriv��4�ed� ��forms��4Eэ�,�ML��is��9equipp�Ged�with�a�n���um�b�er��9of��derive��}'d�%)forms�,��whic���h�in�no�w�a�y�add�to�the����,p�Go���w�er�tsof�the�language,��:as�eac���h�is�expressible�in�terms�of�the�more�primitiv�e����,constructs.��Eэ�;The����n�-tuple�t���yp�Ge��(�ty��&��1��|s�*�ty��&��2���*�����&�&��&�&�����*�ty��&��n��q~�)����for��n������2���is�an�abbreviation�for�the����,record�&�t���yp�Ge�with�n�umeric�lab�Gels��{1:�ty��&��1��|s�,2:�ty��&��2���,��&��&�&��&�&���!�,�n�:�ty��&��n��q~�}�.���Similarly�&�the��n�-����,tuple���expression��(�exp��&��1��|s�,�exp��&��2���,�����&�&��&�&��&+�,�exp��&��n��q~�)����is�an�abbreviation�for�the�record�ex-����,pression�8�v���erb"1="�exp��&��1��|s�,2=�exp��&��2���,���&�&��&�&��m�,�n�=�exp��&��n��q~�}�,�q�and�the��n�-tuple�pattern��(�pat��&��1���,�pat��&��2���,���&�&��&�&��m�,�pat��&��n��q~�)����,�is�UUan�abbrev��q�ation�for�the�record�pattern�v���erb"1="�pat��&��1��|s�,2=�pat��&��2���,��UU�&�&��&�&��UQ�,�n�=�pat��&��n��q~�}�.����;The�::\empt���y�record"��{}��can�also�b�Ge�written�as��()�.�h�This�v��q�alue�is�con�v�en�tion-����,ally�I�returned�from�functions�that�ha���v�e�I�side-eects�but�don't�return�an�in���teresting����,v��q�alue.�13The���t���yp�Ge�of�empt�y�records�is�named��unit��(b�Gecause�the�t�yp�Ge�only�con�tains����,one�UUv��q�alue).����;The��expression��#�lab��for�an���y�lab�Gel�(sym�b�Golic�or�n�umeric)�is�a�selector�function����,that�UUextracts�the�named�eld�from�a�record.����;The���case��exp��of��match��expression�is�completely�equiv��q�alen���t�to�a��(fn��match�)����,�expression�UUapplied�to�the��exp�.����;The�U�if�-�then�-�else��expression�has�con���v�en�tional�Useman�tics:���it�ev��q�aluates�a����,b�Go�olean�>�condition,�x�then�ev��q�aluates�either�the��then��expression�or�the��else��ex-����,pression;�t�this�b�Geha���vior�can�b�e�dened�in�terms�of�a��case��with�patterns��true����,�and�UU�false�.����;The�C��orelse��and��andalso��b�Go�olean�C�op�erators�ev��q�aluate�their�righ���t-hand�expres-����,sions��only�if�the�left-hand�expressions�are�insucien���t�to�determine�the�answ�er����,(i.e.����false���for��orelse��and��true��for��andalso�).�Their�syn���tactic�precedence�is����,w���eak�er�UUthan�all�other�inx�op�Gerators,�and��orelse��binds�w���eak�er�UUthan��andalso�.����;Sev���eral��eexpressions�ma�y�b�Ge�separated�b�y�semicolons,��/and�the�whole�enclosed����,in�0�paren���theses;�<�all�the�expressions�will�b�Ge�ev��q�aluated�and�the�result�of�the�whole����,will��is�the�result�of�the�last�expression.��!When�suc���h�an��expr��}'ession�8se�quenc�e���is����,the�UUen���tire�b�Go�dy�UUof�a��let��expression,�the�paren�theses�ma�y�b�Ge�omitted.����;The��expression��while��exp��&��1���E�do��exp��&��2���rep�Geatedly�ev��q�aluates��exp��&��1���follo���w�ed��b�y�������17�����N�l�����'������R�exp��&��2��g�un���til���exp��&��1���ev��q�aluates�to��false��(just�as�in�P���ascal);�9the��while��expression�can����Rb�Ge�UUexpressed�in�terms�of�a�recursiv���e�function.��=q��aThe�8�expression��[�exp��&��1��|s�,�exp��&��2���,��&�&��&�&�����,�exp��&��n��q~�]�8��is�an�abbreviation�for�the�list��exp��&��1��|s�::�exp��&��2���::��&�&��&�&���::�exp��&��n��q~�::nil�,����Rand��'similarly�the�pattern��[�pat��&��1��|s�,�pat��&��2���,��&�&��&�&�����,�pat��&��n��q~�]��'�is�an�abbreviation�for�the�list����Rpattern���pat��&��1��|s�::�pat��&��2���::��&�&��&�&�����::�pat��&��n��q~�::nil�.�5In�b�Goth�patterns�and�expressions,�W�[]��is����Ran�UUabbrev��q�ation�for��nil�.����aIn��]a�record�pattern�(but�not�in�a�record�expression),��(an�elemen���t��id�=�id�,�where����Rthe���pattern�is�a�v��q�ariable�with�the�same�iden���tier�as�the�lab�Gel,���can�b�e�abbreviated����Ras��Djust��id�.�2Similarly��*�,��{�id�=~�id��v���erb"as"��p��}'at��can�b�Ge�abbreviated�as�"�id��v�erb"as"��p��}'at�,����R�id�=~�id�UU�v���erb":"��ty��can�b�Ge�abbreviated�"�id��v�erb":"��ty�.����aRecursiv���e���clausal�function�denitions�can�b�Ge�dened�con�vien�tly�with�the��fun����R�k���eyw�ord.�q�The�UUdeclaration��,�Í�R�fun�?�f�pat1a�pat2a�pat3a�=�exp1����\�|�?�f�pat1b�pat2b�pat3b�=�exp2����\�|�?�f�pat1c�pat2c�pat3c�=�exp2��&�R��R�is�UUan�abbreviation�for�the�curried�function����R�val�?�rec�f�=�fn�pat1�=>�fn�pat2�=>�fn�pat3�=>�����?�case�?�(pat1,pat2,pat3)������of�?�(pat1a,pat2a,pat3a)�=>�exp1�������|�?�(pat1b,pat2b,pat3b)�=>�exp1�������|�?�(pat1c,pat2c,pat3c)�=>�exp1��&�R��R�The��Upatterns�m���ust�all�b�Ge�atomic�patterns�(including,��of�course,�arbitrary�pat-����Rterns�UUenclosed�in�paren���theses)�to�a�v�oid�syn�tactic�am�biguit�y��*�.��=q��aA���sp�Gecial��(form�of��fun��declaration�is�p�ermitted�for�inxed�function�iden���tiers,����Rof�UUwhic���h�an�example�is�sho�wn�here:����R�fun�?�a�+�b�=�b�-�~a��&�R��a�The�UUderiv���ed�forms�are�summarized�in�the�tables�b�Gelo�w.�������18�����Q�l�����'�������,�7.1��Q�@Expressions��and�patterns���UU�����C�卍�,�Deriv��9ed��TF��
        !            26: �orm����f�Equiv��\ralen��9t��TF��
        !            27: �orm���,��ff&V��fd���T��9yp�Q�es:�������t���y���        �qƳ1�����*������2��UU�&���fe@����*��UU�t���y���\r�qƴn�����f��{�UU�1�:��q�t���y���
x�qƳ1��J��,������2���&���fe@����,��n��:��q�t���y���
x�qƴn��?��}����ff&V������Expressions:������()����f�{�?�}�����(��UU�exp����t�qƳ1��n<�,������2��UU�&���fe@����,��UU�exp����t�qƴn��cG�)����f�{�UU�1��=���exp����t�qƳ1��n<�,������2���&���fe@����,��n��=���exp����t�qƴn��cG�}���N8��case�UU�exp��of��matc���h����f�(�UU�fn��matc���h�)�(�exp�)������#�UU�lab����f��fn�?�{�UU�lab��=�x�,�...}�=>�x�����if�UU�exp��then���exp����t�qƳ1��n<�else���exp����t�qƳ2�����f��case�UU�exp��of�?�true�=>��UU�exp����t�qƳ1����������|�?�false�=>��UU�exp����t�qƳ2��������exp���G�qƳ1����orelse��UU�exp����t�qƳ2�����f��if��UU�exp����t�qƳ1��n<�then�?�true�else��UU�exp����t�qƳ2��������exp���G�qƳ1����andalso��UU�exp����t�qƳ2�����f��if��UU�exp����t�qƳ1��n<�then��UU�exp����t�qƳ2���else�?�false������(��UUexp����t�qƳ1��n<�;������1��UU�&���fe@����;��UUexp����t�qƴn��cG�;�UUexp)����f��case��UU�exp����t�qƳ1��n<�of�?�_�=>��UU�&���fe        ������=>�������f�case��UU�exp����t�qƴn��cG�of�?�_�=>�UU�exp������let�UU�dec��in���exp����t�qƳ1��n<�;������1���&���fe@����;��exp����t�qƴn��cG�end����f�let�UU�dec��in��(��exp����t�qƳ1��n<�;������1���&���fe@����;��exp����t�qƴn��
��)��end�����while��UU�exp����t�qƳ1��n<�do��UU�exp����t�qƳ2�������f��let�?�val�rec�f�=�fn�()�=>�����nif��UU�exp����t�qƳ1��n<�then�?�(��UU�exp����t�qƳ2���;�f())�else�()�������in�?�f()�end�����%�[��UU�exp����t�qƳ1��n<�,������0��UU�&���fe@����,��UUexp����t�qƴn��cG�]�����f��exp������qƳ1�����::������0��UU�&���fe@����::��UU�exp����t�qƴn��cG�::�?�nil���N:�ff&V��fd���UU�Deriv��9ed��TF��
        !            28: �orm����f�Equiv��\ralen��9t��TF��
        !            29: �orm����ff&V�����P��9atterns:������()����f�{�?�}�UU�(no���sp��}'ac�e�b�etwe�en�\�&�\�()�")������(��UU�pat�����qƳ1�����,������2��UU�&���fe@����,��UU�pat�����qƴn�����)����f�{�UU�1��=���pat�����qƳ1�����,������2���&���fe@����,��n��=���pat�����qƴn�����}���N8��[��UU�pat�����qƳ1�����,������0��UU�&���fe@����,��UUpat�����qƴn�����]�����f��pat����؎�qƳ1����V�::������0��UU�&���fe@����::��UU�pat�����qƴn�����::�?�nil�����{��UU�&���fe   �������,�UUid�,���&���fe   �����}����f�{��UU�&���fe   �������,�UUid��=��id�,���&���fe    �����}�����{��UU�&���fe     �������,�UUid��as��pat,���&���fe   �����}����f�{��UU�&���fe   �������,�UUid��=��id��as��pat,���&���fe    �����}�����{��UU�&���fe     �������,�UUid�:�q�t���y�,���&���fe        �����}����f�{��UU�&���fe   �������,�UUid��=��id�:�q�t���y�,���&���fe �����}���N:�ff&V������u��;�Eac���h�4"deriv�ed�form�is�iden�tical�seman�tically�to�its�\equiv��q�alen�t�form."�/The����,t���yp�Ge-c�hec�king��of�eac���h�deriv�ed�form�is�also�dened�b�y�that�of�its�equiv��q�alen�t�form.����,The��precedence�among�all�primitiv���e�and�deriv�ed�forms�is�sho�wn�in�App�Gendix�A.��̍�;The�2deriv���ed�t�yp�Ge��t�y���9$�qƳ1����*����\�2���&���fe@����*���t�y���9$�qƴn��ܩ�is�called�an�(n{)tuple�t�yp�Ge,�9and�the�v��q�alues����,of�UUthis�t���yp�Ge�are�called�(n{)tuples.����;The��nal�deriv���ed�pattern�allo�ws�a�lab�Gel�and�its�asso�ciated�v��q�alue�to�b�e�elided����,in�UUa�record�pattern,�when�they�are�the�same�iden���tier.�������19�����ՠl�����'�������R�7.2��w�@Bindings��and�declarations�����R�A�3esyn���tax�3nclass��fb��of�function�bindings�is�used�as�a�con�vien�t�form�of�v��q�alue�bind-����Ring��nfor�(p�Gossibly�recursiv���e)�function�declarations.�wThe�equiv��q�alen�t�form�of�eac�h����Rfunction���binding�is�an�ordinary�v��q�alue�binding.���These�new�function�bindings����Rm���ust���b�Ge�declared�b�y��fun�,���not�b�y��val�;��;ho�w�ev�er,���functions�ma�y�still�b�Ge�declared����Rusing�UU�val��or��val�?�rec��along�with��fn��expressions.��``�����Lˍ������Deriv��9ed��TF��
        !            30: �orm���&93�Equiv��\ralen��9t��TF��
        !            31: �orm���a��ff&`���fd���F��
        !            32: �unction��Tbindings��fb�:�������\�id�UU=��fn��x��&��1�����=>������1���&���fe@����=>�?�fn��x��&��n�����=>��������case�?�(�UU�x��&��1��|s�;���&���fe   ������;�&��x��&��n�����)������UU�id��apat���� �qƳ11����%갸1��"�[�&���fe@���4*��apat���G�~�qƳ1�n��T_��cst�=��exp����t�qƳ1�������of�UU�(��apat���� �qƳ11��"�[�,������1���&���fe@����,��apat���� �qƳ1�n��#�f�=>���exp����t�qƳ1��n<�cst������|��UU�&���fe        ��������|��UU�&���fe      ��������|�UU�id��apat���� �qƴm�1����)ظ1��%���&���fe@���7F��apat���J���qƴmn��Z��cst�=��exp����t�qƴm�����[�|�UU�(��apat���� �qƴm�1��%���,������1���&���fe@����,��apat���� �qƴmn��&���=>���exp����t�qƴm���d�cst�������fb����t�&��1��n<�and������1��UU�&���fe@����and��UUfb����ɟ&��n������\�vb�����&��1����|�and������1��UU�&���fe@����and��UUvb���*��&��n��������\�(wher��}'e�����vb���i?�&��i��Qr�is���the�e�quivalent�of���{�fb���N�&��i���;�)����ff&`���fd����Declarations:������fun�UU�fb����\�val�?�rec�UU�vb�������\�(wher��}'e����vb��is�the�e�quivalent�of��{�fb�)�������exp����\�val�?�it�=�UU�exp��(only���at�top�level)����ff&`��������R�In�X6the�table�ab�Go���v�e,���\cst"�X6stands�for�an�optional�t���yp�e�constrain���t|a�colon�follo�w�ed����Rb���y���a�t�yp�Ge�expression.�B�The�last�deriv�ed�declaration�(using�\it")�is�only�allo�w�ed����Rat��htop-lev���el,��for�treating�top-lev�el�expressions�as�degenerate�declarations;��\it"�is����Rjust�UUa�normal�v��q�alue�v�ariable.�������20�����Ơl�����'���G��,�HChapter��F8��2��,�IEqualit��4�y��4��,�The�(Requalit���y�function��op�?�=�:�''a�*�''a�->�bool�(R�is�a�v��q�ailable�at�all�t�yp�Ges��''a����,�except��function�t���yp�Ges,�%�abstract�t�yp�Ges,�%�and�the�t�yp�Ges�constructed�from�them.�]�In����,fact,�F�t���yp�Ge�B�v��q�ariables�that�b�egin�with�t���w�o�B�primes�are�sp�ecial:�h�they�stand�only�for����,t���yp�Ges�UUthat�admit�equalit�y��*�.����;Tw���o��5v��q�alues�are�tested�for�equalit�y�as�follo�ws,��odep�Gending�on�the�kind�of�v��q�alue:������A�d�Primitiv��9e��Tt�yp�Q�es����0��lik���e���in�tegers,�Areals,�and�strings�ha���v�e�equalit�y�func-�������tions�UUwith�the�con���v�en�tional�UUb�Geha�vior.������A�d�F��
        !            33: �unction��Tt��9yp�Q�es������cannot�UUb�Ge�compared�(\do�not�admit�equalit���y").������A�d�Reference��Tt��9yp�Q�es:�������On��references,���equalit���y�means�iden�tit�y;��a�reference�������is��equal�to�itself�and�to�no�other�references,��'regardless�������of�UUsimilar�con���ten�ts.������A�d�Record�   ��t��9yp�Q�es�������ma���y�;�b�Ge�compared�if�all�their�comp�onen���ts�admit�equal-�������it���y��*�.������A�d�Datat��9yp�Q�es�������ma���y�b%b�Ge�compared�for�equalit�y�if�all�of�their�con-�������structed�UUt���yp�Ges�admit�equalit�y��*�.������A�d�Opaque�/�t��9yp�Q�es�������from�
        !            34: `functor�parameters�and�abstractions�do�not�ad-�������mit���equalit���y�unless�the��eqtype��k�eyw�ord�is�used�(in-�������stead�Y�of�the��type��k���eyw�ord)�Y�in�the�signature�dening�������them.����;The���function��op�?�<>�:�''a�*�''a�->�bool����is�the�inequalit���y�function;��6it�is����,applicable�UUto�an���y�equalit�y�t�yp�Ge.����;The�R�comparison�functions��>�,�STv���erb"<",��<=�,�and�R��>=��do�not�ha�v�e�this�b�Geha�vior;����,they�UUare�o���v�erloaded�UUjust�for�the�t���yp�Ges��int�,��real�,�and��string�.�������21�����Ơl�����'���G�d��R�HChapter��F9��2�d��R�IExceptions��4�d��R�The�Ërough�and�ready�rule�for�understanding�ho���w�exceptions�are�handled�is�as����Rfollo���ws.�q�If�UUan�exception�is�raised�b�y�a��raise��expression��S-��k�raise�UU�E����(��exp��)��S,��R�whic���h�b�lies�in�the�textual�scop�Ge�of�a�declaration�of�the�exception�constructor��E����,����Rthen�UUit�ma���y�b�Ge�handled�b�y�a�handling�rule����k�handle�UU�E����(��p��}'at��)�?�=>��exp'��S,��R�but�UUonly�if�this�handler�is�in�the�textual�scop�Ge�of�the�same�declaration.���d��aAn���y��{exception,�"�regardless�of�scop�Ge,�is�handled�b���y�a�wildcard�or�v��q�ariable����Rpattern,�UUas����k�handle�?�_�=>�UU�exp'��S,��R�This�#Wrule�is�p�Gerfectly�adequate�for�exceptions�declared�at�top�lev���el;�4&some�exam-����Rples�UUin�Section�9.3�b�Gelo���w�illustrate�what�ma�y�o�Gccur�in�other�cases.��%.V���R�9.1��w�@An��example��XT��R�T��*�o��illustrate�the�generalit���y�of�exception�handling,���supp�Gose�w�e�ha�v�e�declared����Rsome�UUexceptions�as�follo���ws:�����R�exception���Oddlist�?�of�int�and���Oddstring�of�string����R�and��xthat�a�certain�expression�exp:in���t�ma�y�raise�either�of�these�exceptions�and����Ralso���runs�the�risk�of�dividing�b���y�zero.��The�handler�in�the�follo�wing��handle����R�expression�UUw���ould�deal�with�these�expressions:�������22�����q�l�����'������,�exp�?�handle�Oddlist�[]�
        !            35: �=>�0����[?�|�?�Oddlist�[x]�=>�2*x����[?�|�?�Oddlist�(x::y::_)�=>�x�div�y����[?�|�?�Oddstring�""�=>�0����[?�|�?�Oddstring�s�=>�size(s)-1����[?�|�?�Div�=>�10000��4'��,�Note�gthat�the�whole�expression�is�w���ell-t�yp�Ged�gb�ecause�in�eac���h�handling�rule�the����,t���yp�Ge�əof�the�matc�h-pattern�is��exn�,��and�b�Gecause�the�result�t�yp�Ge�of�the�matc�h�is����,�int�,�UUthe�same�as�the�t���yp�Ge�of�exp.����;Note��also�that�the�last�handling�rule�will�handle��Div��exceptions�raised�b���y����,exp,���but�Օwill�not�handle�the��Div��exception�that�ma���y�b�Ge�raised�b�y��x�?�div�y�Օ�in����,the�UUthird�handling�rule.�q�Finally��*�,�note�that�a�univ���ersal�handling�rule����[?��|�?�_�=>�50000����,�at�UUthe�end�w���ould�deal�with�all�other�exceptions�raised�b�y�exp.�� w����,�9.2��Q�@Exception��constructors�����,�F��*�or�UUan�exception�constructor�E,�the�expression����E�E����(�UU�exp��)����,�ev��q�aluates�W�the�expression��exp�,��rpro�Gducing�v�alue��v�[��,��rand�then�applies�the�constructor����,�E����to�UUit,�yielding�the�v��q�alue��E����(�v�[��),�whose�t���yp�Ge�is��exn�.����;The��;�raise��k���eyw�ord��;ma�y�b�Ge�applied�to�an�y�expression�of�t�yp�Ge��exn�,�D4and����,has���the�eect�of�\raising"�that�exception�v��q�alue.��The�innermost�(dynamically)����,enclosing�expression��e��?�=��e��&��1���z�handle��m��&��1���is�found;�_�all�further�ev��q�aluation�of�the����,expression��e�e��&��1��2��(and�its�subphrases)�is�ab�Gorted;���and�the�matc���h��m��&��1���is�applied�to����,the�UUexception�v��q�alue,�yielding�the�result�of�the�expression��e�.����;If�B�the�matc���h�in�a�handler�fails,�~Fthen�the�exception�v��q�alue�is�re-raised,�and����,another�UUenclosing�handler�is�found.����;Exception�;1constructors�ma���y�b�Ge�n�ullary�(ha�v�e�no�asso�Gciated�v��q�alue),�@lin�whic�h����,case�UUthe��exp��and��p��}'at��in�the�previous�discussion�are�omitted.����;Exceptions�UUma���y�b�Ge�constructed�indep�enden���tly�of�raising�them:����6��exception�?�A�of�int����6�val�?�e�=�A�6����6�val�?�x�=�raise�e����,�Handlers�UUma���y�b�Ge�abstracted�from�the��handle��k�eyw�ord:����;���val�?�h�=�fn�E�0�=>�"zero"����j��|�?�E�_�=>�"nonzero"����j��|�?�v�=>�raise�v����;��f(x)�
        !            36: �handle�e�?�=>�h�e��������23�����/�l�����'������R�Note�Z6that�it�is�advisable�in�this�case�to�ha���v�e�Z6a�default�clause�in�the�function��h�,����Rsince�the�default�for�a��handle��matc���h�(re-raising�the�exception)�is�dieren�t�from����Rthe�UUdefault�for�a��fn��or��case��matc���h�(raising�the��Match��exception).����aThe��ordinary�wildcard�pattern��_��will�handle�an���y�exception�when�it�is�used����Rin���a�pattern,�  �as�will�an���y�pattern�consisting�solely�of�a�v��q�ariable.�R0These�should�b�Ge����Rused�UUwith�some�care,�b�Gearing�in�mind�that�they�will�ev���en�handle�in�terrupts.����aNullary�c�exception�names,��when�missp�Gelled,�app�ear�c�to�the�compiler�to�b�Ge�v��q�ari-����Rables;�}these�o�will�then�matc���h�an�y�exception.��3F��*�or�this�reason�w�e�recommend�the����Rcon���v�en�tion���that�exception�names�(and�other�constructors)�b�Ge�written�b�eginning����Rwith���an�upp�Gercase�c���haracter,��Tand�v��q�ariables�b�e�written�b�eginning�with�a�lo���w�er-����Rcase��c���haracter.�s�The�compiler�ma�y�remind�the�programmer�of�this�con�v�en�tion����Rwhen�UUit�is�violated.�� N���R�9.3��w�@Some��pathological�examples�����R�W��*�e�5�no���w�consider�some�p�Gossible�misuses�of�exception�handling,�<
        !            37: whic�h�ma�y�arise����Rfrom�q!the�fact�that�exception�declarations�ha���v�e�q!scop�Ge,�xand�that�eac���h�ev��q�aluation����Rof���a�generativ���e�exception�binding�creates�a�distinct�exception.�1QConsider�a�simple����Rexample:��r���R�exception�
        !            38: �E�?�of�bool����Rfun�?�f(x)�=����l?�let�?�exception�E�of�int����q�in�?�if�x�>�100�then�raise�E(x)�else�x+1����l?�end����Rval�?�z�=�f(200)�handle�E(true)�=>�500�|�E(false)�=>�1000����R�The�p_program�is�w���ell-t�yp�Ged,�w"but�p_useless.���The�exception�b�ound�to�the�outer��E�pX�is����Rdistinct��*from�that�b�Gound�to�the�inner��E�;�th���us�the�exception�raised�b�y��f(200)�,����Rwith��{excepted�v��q�alue�200,���could�only�b�Ge�handled�b���y�a�handler�within�the�scop�e����Rof��the�inner�exception�declaration|it�will�not�b�Ge�handled�b���y�the�handler�in�the����Rprogram,���whic���h��9exp�Gects�a�b�o�olean�v��q�alue.�
        !            39: rSo�this�exception�will�b�e�rep�orted�at����Rtop�.�lev���el.�d�This�w�ould�apply�ev�en�if�the�outer�exception�declaration�w�ere�also�of����Rt���yp�Ge�UUin�t;�the�t�w�o�exceptions�b�Gound�to��E��w�ould�b�Ge�distinct.����aOn�UUthe�other�hand,�if�the�last�line�of�the�program�is�c���hanged�to����R�f(200)�?�handle�_�=>�500����R�then��<the�exception�will�b�Ge�caugh���t,��5and�the�v��q�alue�500�returned.�|A���univ�ersal����Rhandling��\rule�(i.e.�H��_��or�a�v��q�ariable-iden���tier)�catc�hes�an�y�exception|ev�en�one����Rexp�Gorted�i\from�the�scop�e�of�the�declaration�of�the�asso�ciated�exception�name|����Rbut�4�cannot�examine�the�excepted�v��q�alue�carried�b���y�the�exception�constructor,����Rsince�UUthe�t���yp�Ge�of�this�v��q�alue�cannot�b�e�statically�determined.����aEv���en��*a�single�textual�exception�binding|if�for�example�it�is�declared�within����Ra�q�recursiv���ely�dened�function|ma�y�bind�distinct�exceptions�to�the�same�iden-����Rtier.�q�Consider�UUanother�useless�program:�������24�����àl�����'������,�fun�?�f(x)�=����F?�let�?�exception�E����K�in�?�if�p(x)�then�a(x)�����?�else�?�if�q(x)�then�f(b(x))�handle�E�=>�c(x)������else�?�raise�E����F?�end����,val�?�z�=�f�v����,�No���w��:if�p(v)�is�false�but�q(v)�is�true,��3the�recursiv�e�call�will�ev��q�aluate�f(b(v)).����,Then�gIif�b�Goth�p(b(v)�and�q(b(v))�are�false,�k�this�ev��q�aluation�will�raise��E�.�But�this����,exception�will�not�b�Ge�handled,��since�the�exception�raised�is�that�whic���h�is�b�ound����,to�UU�E��b���y�the�inner|not�outer|ev��q�aluation�of�the�exception�declaration.����;These��pathological�examples�should�not�lea���v�e��the�impression�that�exceptions����,are�֔hard�to�use�or�to�understand.���The�rough�and�ready�rule�of�Section�9�will����,almost�UUalw���a�ys�giv�e�the�correct�understanding.�������25����呠l�����'���G����R�HChapter��F10��2����R�IReference�  ��v��ialues��4����R�References�#�are�cells�whose�con���ten�ts�#�ma�y�b�Ge�c�hanged�after�creation�b�y�assign-����Rmen���t.�%�The�q�ref��\datat�yp�Ge"�constructor,���and�its�corresp�onding�v��q�alue�constructor,����Rare�UUalmost�as�if�dened�b���y�the�declaration���卑R�datatype�?�'a�ref�=�ref�of�'a���,��R�Th���us,�T�a�!�reference�whose�initial�con�ten�ts�are�the�string��"abc"��ma�y�b�Ge�created����Rb���y����val�?�r�=�ref�"abc"�.�W�Subsequen�tly��*�,�� the���con�ten�ts�of��r��ma�y�b�Ge�altered�b�y����Rassignmen���t:�%2�r�?�:=�"def"�.�~�The��con�ten�ts�of�a�reference�ma�y�b�Ge�examined�b�y����Rusing�UUthe��ref��constructor�in�a�pattern:����R�let�?�val�(ref�s)�=�r����W?�in�?�print�s����Rend���,��R�The�UUfunction��!��is�dened�to�tak���e�the�con�ten�ts�of�a�reference;�that�is,����R�fun�?�!�(ref�x)�=�x����a�References�UUare�not�fully�p�Golymorphic;�see�Chapter�11.������aF��*�ormally�,��ow���e�`�sa�y�that�phrases�in�ML�`Xare�ev��q�aluated�in�the�presence�of�an����R�envir��}'onment����E�eX�and�a��stor�e��S����.��(The�eect�on��E�eX�of�ev��q�aluating�declarations,���ex-����Rpressions,���etc.�&�is���describ�Ged�in�Chapter�3.�Here�w���e�summarize�the�eect�on����R�S����.����aThe��store��S���maps�reference�v��q�alues�to�their�con���ten�ts.�LkEv�aluation��of�an�ex-����Rpression�UUin�the�store��S����yields,�dep�Gending�on�the�form�of�the�expression,���-����g�d�ref��T�exp���š��exp���is�ev��q�aluated�in��S����,�͗pro�Gducing�a�v�alue��v���and�a�store����š��S������^��0��a��;��the�?�reference�v��q�alue��r����is�returned�with�the�store����š��S������^��0�����+�8��f�r�5�7!���v�[��g�.�������26������l�����'���������A�d�exp���P���qƳ1���YGJ�exp���h�i�qƳ2���������exp������qƳ1������is�dev��q�aluated�in��S����yielding�the�function��f��&��1�����and�������store��%�S������^��0��a��;���
exp����,�qƳ2��
��is�ev��q�aluated�in��S������^��0�����yielding��v��&��2����and��S������^��00�����;�������nally�}the�b�Go�dy�}of��f��&��1�����is�ev��q�aluated�with�its�v�ariable�������b�Gound�� to��v��&��2��|s�,��Lin�the�store��S������^��00�����,�yielding�the�result��v����and�������the�UUstore��S������^��000���8�.������A�d�op�?�:=��(�exp���G�qƳ1��Ò�;��&���exp����ǟqƳ2��n:�)������The�8�expression�(�exp���G�qƳ1��Ò�;��&���exp����ǟqƳ2��n:�)�is�ev��q�aluated�in��S����,�q�yield-�������ing�X�the�pair�(�r���;�&��v�[��)�and�the�store��S������^��0��a��;���then�the�unit�v��q�alue��������fg�UU�is�returned�with�the�store��S������^��0�����+�8��f�r�5�7!���v�[��g�.������A�d�f���T�lab���*��&��1��|s�=���T�exp���s�qƳ1��n:�,���T�&���fe���*�,���T�lab���*��&��n��q~�=���T�exp���s�qƴn��cE�g��������exp���&FןqƳ1��&
��is���ev��q�aluated�in��S����,��yielding��������v��&��1�����and�#Kthe�store��S��&��1��|s�;��Fthen�eac���h��exp���jj�qƴi���&�is�ev��q�aluated�in��������S��&��i��1��
���,��yielding�i$�v��&��i���p�and�the�store��S��&��i��TL�;��then�the�record��������f��lab���
UX�&��1��-��=�[��v��&��1��|s�;�&��:::;���lab����&��n���8�=��v��&��n��q~�g����is�returned�with�the�store��������S��&��n��q~�.��"Note�,sthat�the�expressions�are�ev��q�aluated�in�the�������sequence���they�are�written,���not�in�alphab�Getical�order�������of�UUthe�lab�Gels.������A�d�raise��T�exp�������exp��is�ev��q�aluated�in��S����,�-�returning��v�^]�and��S����^��0��a��;�Ythen�the�������exception-pac���k�et�UU(�v�[�;�&��S������^��0��a��)�is�raised.������A�d�exp��T�handle��matc��9h������exp���is�ev��q�aluated;�m�if�exp�returns�a�v�alue��v���with�������state�l��S������^��0��a��,�r�then��v�ȱ�is�returned�with��S������^��0���.��OIf�exp�raises�an�������exception-pac���k�et��(�e;�&��S������^��00�����)�then�the�matc���h�is�applied�to��������e���in�the�state��S������^��00�����.��If�the�matc���h�fails,��Bthen�(�e;�&��S������^��00���)�is�������raised��(as�the�v��q�alue�of�the��handle��expression).�[�If�the�������matc���h�2succeeds,�9then�the�resulting�v��q�alue�is�returned.����;Matc���hing�ta�pattern�to�a�v��q�alue�has�no�eect�on�the�store.�Z|Ev�aluating�a�v�alue����,binding� �has�an�eect�on�the�store�just�from�the�ev��q�aluation�of�the�constituen���t����,expressions.��Ev��q�aluation��of�t���yp�Ge,�ޏdatat�yp�e,�ޏor��exception�bindings�has�no�eect����,on�UUthe�store.�������27�����R�l�����'���G��R�HChapter��F11��2��R�IReference�       ��and�exception����Rt��4�yp��ves��4��R�The���treatmen���t�of�references�and�exceptions�with�\op�Gen"�t�yp�Ges�is�based�on�the����Rfact��-that�the�con���ten�ts��-of�a�reference�cell�cannot�b�Ge�constrained�to�b�e�p�olymor-����Rphic,�N�and��so�m���ust�b�Ge�considered�to�b�e�monomorphic.�ȯThe�follo���wing�example����Rillustrates�UUthe�problem.��@��R�let�?�val�s�=�ref�(fn�x�=>�x)����W?�in�?�s�:=�(fn�x�=>�x+1);�(!s)�true����Rend����R�If��{s�w���ere�giv�en�the�p�Golymorphic�t�yp�Ge��8��     z:�(��В�!�����)��ref���m�,�Чthen��{this�expression�w�ould����Rt���yp�Ge-c�hec�k,�@ap�ermitting�_an�ob���vious�t�yp�Ge�error.���T��*�o�prev�en�t�this,�@aw�e�insist�that����Rthe�>�t���yp�Ge�of�an�applied�o�ccurrence�of�the�ref�constructor�should�alw���a�ys�>�b�e�giv���en����Ra�UU\ground"�t���yp�Ge�(one�with�no�lo�cally-b�ound�t���yp�e�v��q�ariables).����aHo���w�ev�er,�Efunctions��&whose�application�can�create�reference�v��q�ariables�can�still����Rha���v�e�UUp�Golymorphic�t���yp�es�of�a�restricted�kind.�q�Consider�the�declaration����R�val�?�F�=�fn�x�=>�let�val�r�=�ref�x�����?�in�?�!r�������end����R�Here���the�function�F���can�b�Ge�giv���en�p�olymorphic�t���yp�e��8���      z��^��1�����:�(���    z��^��1��]z�!�׍���  z��^��1���)���where����       z��^��1�����R�is� �a�sp�Gecial�kind�of�t���yp�e�v��q�ariable�called�a��we��}'ak��t���yp�e�v��q�ariable�(the�sup�erscript����R\1"��indicates�that�there�is�one�lam���b�Gda�abstraction�susp�ending�the�creation�of����Rthe�׌ref�cell).��lWhen�F��jis�applied�to�an�argumen���t,��a�reference�v��q�alue�of�t�yp�Ge���� z��^��1�����R�is�_�created,�band�hence�this�w���eak�t�yp�Ge�v��q�ariable�m�ust�b�Ge�instan�tiated�to�a�ground����Rt���yp�Ge.�KKThis���means�that�an�expression�lik�e��(F�?�nil)��w�ould�not�b�Ge�prop�erly�t���yp�ed.����RIn�8�con���trast,�>�the�t�yp�Ge����       z��^��1������ref��[4�assigned�to�r�is�p�ermissible�b�ecause����      z��^��1�����is�implicitly����Rb�Gound�
        !            40: in�an�outer�scop�e�and�within�the�scop�e�of�its�binding�is�treated�as�a����Rconstan���t�UUt�yp�Ge.�������28�����s�l�����'������;�In�0ML,�w���eak�t�yp�Ge�v��q�ariables�will�b�e�written��'1a�,�7��'2a�,�etc.,�where�0the�in���teger����,after�UUthe�ap�Gostrophe�denotes�the�lev���el�of�susp�ension.����;Exception��declarations�raise�similar�problems,���whic���h�are�handled�b�y�an�anal-����,ogous�UUuse�of�w���eak�t�yp�Ge�v��q�ariables.�������29����&O�l�����'���G%���R�HChapter��F12��2%���R�IMo��vdules��4%���R�ML�^pro���vides�^Ba�p�Go�w�erful�mo�Gdule�system,���whic�h�can�b�Ge�used�to�partition�programs����Ralong�UUclean�in���terfaces.��!�%���R�12.1����Structures��-���R�In�wits�simplest�form,�'
        !            41: a�mo�Gdule�is�(syn���tactically)�just�a�collection�of�declarations����Rview���ed��Kas�a�unit,���or�(seman�tically)�the�en�vironmen�t�dened�b�y�those�denitions.����RThis���is�one�form�of�a��structur��}'e{expr�ession�:�*u�struct����dec��end�.���F��*�or�example,���the����Rfollo���wing�UUstructure{expression�represen�ts�an�implemen�tation�of�stac�ks:���Ӎ�R�struct����a��datatype�?�'a�stack�=�Empty�|�Push�of�'a�*�'a�stack����a��exception�?�Pop�and�Top����a��fun�?�empty(Empty)�=�true�|�empty�_�=�false����a��val�?�push�=�Push����a��fun�?�pop(Push(v,s))�=�s�|�pop(Empty)�=�raise�Pop����a��fun�?�top(Push(v,s))�=�v�|�top(Empty)�=�raise�Top����Rend��qލ�R�Structure{expressions��.and�ordinary�expressions�are�distinct�classes;��structure{����Rexpressions�
mma���y�b�Ge�b�ound�using�the��structure��k���eyw�ord�
mto�structure{names,����Rwhile���ordinary�expressions�are�b�Gound�using��val��to�v��q�alue{v�ariables.�SDThe���form�of����Ra�UU�structure��binding�is�as�follo���ws:��qߍ�k�structure�UU�name��=��structure{expression����RTh���us,�Lw�e��migh�t�mak�e�a�structure�Stac�k�using�the�structure{expression�sho�wn����Rab�Go���v�e:���Ӎ�R�structure�?�Stack�=�struct��������30����&��l�����'����������datatype�?�.�.�.�������exception�?�.�.�.�����?�.�?�.�.������end��[��;�The��en���vironmen�t��E�p��that�binds�the�iden�tiers��stack�,��#�Pop�,��empty�,�etc.�I�is�no���w����,itself�4�b�Gound�to�the�structure{iden���tier��Stack�.�f�T��*�o�refer�to�names�in��E����,�;�qualie��}'d����,identiers�iC�m���ust�b�Ge�used.���A�h�qualied�iden�tier�consists�of�a�structure{name,����,a�R�dot,��Dand�the�name�of�a�structure�comp�Gonen���t,�e.g.�jl�Stack.empty��(a�v��q�alue),����,�Stack.stack�UU�(a�t���yp�Ge),��Stack.Pop��(an�exception),�etc.��Vɍ�;�Structure���closure:����In�_korder�to�isolate�the�in���terface�b�Get�w�een�a�structure�and����,its�ѩcon���text,���a��struct��phrase�is�not�allo�w�ed�to�con�tain�global�references�to�t�yp�Ges,����,v��q�alues,��~or��exceptions,�except�for�p�Gerv��q�asiv���e�primitiv�es�of�the�language�lik�e��int�,����,�nil�,��etc.��=It��|can,�ho���w�ev�er,��con�tain�global�references�to�other�structures,��signa-����,tures,��!and���functors,�including�qualied�names�referring�to�comp�Genen���ts�(v��q�alues,����,t���yp�Ges,�UUetc.)�q�of�other�structures.����;There�UUare�three�forms�of�structure-expression:��\�����88�1.����EAn�UUen���vironmen�t�enclosed�in��struct��...�q��end��(as�ab�Go�v�e),��[$�����88�2.����EAn�d�iden���tifer�that�has�b�Geen�previously�b�ound�in�a��structure��declaration,����Eand�������88�3.����EA�}functor�}(application��F(�str�)�,��where��F��is�the�name�of�a�functor�and��str��is����Ea�UUstructure�expression.��[��,Th���us,�ήthe��declaration��structure~Pushdown~=~Stack��binds�the�name��Pushdown����,�to�2nthe�same�structure�that��Stack��is�b�Gound�to;�>here,�9i�Stack��is�an�example�of�the����,second�UUkind�of�structure�expression.��ʨ���,�12.1.1��\Accessing��structure�comp�`onen��ts��؍�,�The� bindings�making�up�a�structure�dene�named��c��}'omp�onents� �of�the�structure,����,as��Fin�a�record.���T��*�o�refer�to�suc���h�comp�Gonen�ts�w�e�use�qualied�names,���whic�h�are����,formed�ׂb���y�app�Gending�a�p�erio�d�follo���w�ed�ׂb�y�a�comp�Gonen�t�name�to�the�name�of����,a�`^structure.���F��*�or�instance,�� �Stack.empty��refers�to�the�function��empty��dened����,in�hthe�structure��Stack�.��%If�the�qualied�name�designates�a�substructure�of�a����,structure,��&then��xit�to�Go�has�comp�onen���ts;����e.g.�0�A.B.x��denotes�the�comp�onen���t��x��of����,the�UUsubstructure��B��of�a�structure��A�.��Vɍ�;Qualiers�[can�b�Ge�attac���hed�only�to�names;�^&they�do�not�apply�to�other�forms����,of��structure�expressions.�\4Qualied�names�are�treated�as�single�lexical�units;�*/the����,dot�UUis�not�an�inx�op�Gerator.����;Direct��waccess�to�the�bindings�of�a�structure�is�pro���vided�b�y�the��open��decla-����,ration,��whic���h���is�analagous�to�the�\with"�clause�of�P�ascal.�M�F��*�or�example,��in�the����,scop�Ge�UU(determined�in�the�usual�w���a�y)�UUof�the�declaration�������31���� &�l�����'������k�open�?�Stack��m���R�the�inames��stack�,�L��empty�,��pop�,�etc.��refer�ito�the�corresp�Gonding�comp�onen���ts�of����Rthe����Stack��structure.�g�It�is�as�though�the�b�Go�dy���of�the�structure�denition�had����Rb�Geen��inserted�in�the�program�at�that�p�oin���t,�N�except�that�the�bindings�are�not����Rrecreated,�kbut�f�are�instead�simply�\b�Gorro���w�ed"�f�from�the�op�ened�structure.���open����R�declarations��follo���w�the�usual�rules�for�visibilit�y��*�,�Y�so�that�if��A��)�and��B��are�t���w�o����Rstructures�sYcon���taining�a�binding�for��x��(of�the�same�
a�v�or,�z�of�course),�then�after����Rop�Gening�UUb�oth��A��and��B��with�the�declaration��m���k�open�?�A����kopen�?�B����R�the��Uunqualied�iden���tier��x��will�b�Ge�equiv��q�alen�t�to��B.x�.�TrThe��x��comp�Gonen�t�of��A��?�can����Rstill�UUb�Ge�referred�to�as��A.x�,�unless��B��also�con���tains�a�substructure�named��A�.���.��aQualied��iden���tiers�do�not�ha�v�e�inx�status.�        �If��+��is�declared�inx�in�a����Rstructure���A�,�the�qualied�iden���tier��A.+��is�not�an�inx�iden�tier.�UHo�w�ev�er,�Xwhen����Ran�<9iden���tier�is�made�visible�b�y�op�Gening�a�structure,�A?it�retains�its�inx�status,�if����Ran���y��*�.�q�AMBIGUOUS.����aThe�UUdeclaration��open�?�A�B�C�UU�is�equiv��q�alen���t�to��open�?�A;�open�B;�open�C�.�� Ig���R�12.1.2���Ev��@aluating��structure�expressions���荑R�The��ev��q�aluation�of�a�structure�expression��str��dep�Gends�on�its�form,�&�and�assumes�a����Rcurren���t��/structure�en�vironmen�t��S���E����that�binds�structures�and�functors�to�names.����RInformally��*�,�UUev��q�aluation�pro�Gceeds�as�follo���ws:��m������^8�1.����kIf�+O�str��is�an�encapsulated�declaration�(�i.e.���struct�...�end�),�`�then�the�b�Go�dy����kdeclarations�{�are�ev��q�aluated�relativ���e�to��S���E�t�and�the��p��}'ervasive��v�alue,���excep-����ktion,��and��dt���yp�Ge�en�vironmen�ts�of�ML��L(that�is,��the�en�vironmen�ts�binding�the����kbuilt-in���primitiv���es�of�the�language).�X�The�resulting�en�vironmen�t�is�pac�k-����kaged���as�a�structure�and�returned.�� The�ev��q�aluation�of�v�alue�bindings�ma���y����kha���v�e�o�an�eect�on�the�store�(the�mapping�of�references�to�con���ten�ts);��@the�o�new����kstore�,is�returned�as�w���ell,�4�to�b�Ge�used�in�subsequen�t�expression�ev��q�aluations.��<������^8�2.����kIf�ͷ�str��is�a�simple�name,���then�its�binding�in��S���E�aD�is�returned.�D�If�it�is�qualied����kname,�u{then�=�it�is�used�as�an�access�path�starting�with��S���E���and�the�designated����ksubstructure�UUis�returned.�������^8�3.����kIf���str��is�a�functor�application��F(��str��G��^��0�� ��)�,�9,where�the�functor��F�k�is�declared����kb���y����functor�?�F�(�A�)�=�   ��body�[��,��dthe�parameter�structure��str��G��^��0����is�ev��q�aluated����kin�2��S���E��}�yielding�structure��s��&��1��|s�;�>gthen�the�\b�Go�dy"�2�of�the�denition�of��M�,�whic���h����kis�1�a�structure�expression,�9is�ev��q�aluated�in��S���E����+�&�5�f�A���7!��s��&��1��|s�g�.�fIn�1�other�w���ords,����kfunctor�,lapplications�are�ev��q�aluated�in�a�con���v�en�tional�,lcall-b�y-v�alue�,lfashion.�������32����!&F�l�����'�������,�12.1.3��\Ev��@aluating��structure�declarations��uT��,�T��*�o�'�ev��q�aluate�a�simple�structure�declaration,�0�one�ev�aluates�the�dening�structure����,expression�1�in�the�curren���t�en�vironmen�t��S���E��Q�and�returns�the�binding�of�the�name����,of��the�left�hand�side�to�the�resulting�structure.�B�If�ev��q�aluation�of�a�structure����,expression�UUraises�an�(un���trapp�Ged)�exception,�then�the�declaration�has�no�eect.���ҍ��,�12.1.4��\Structure��equiv��@alence����,�F��*�or��
        !            42: certain�purp�Goses,�ۀsuc���h�as�c�hec�king�sharing�constrain�ts�(Section��??�)�?w�e�m�ust����,b�Ge��able�to�determine�whether�t���w�o��(references�to)�structures�are�equal�or�\the����,same."���Here��structures�are�treated�somewhat�lik���e�datat�yp�Ges;�p6eac�h�ev��q�aluation����,of��5an�encapsulated�declaration�or�functor�application�creates�a�distinct�new����,structure,���and���all�references�to�this�structure�are�considered�equal.�Q�Th���us�after����,the�UUfollo���wing�declarations:��I��,�structure�?�S1�=�struct�...�end����,structure�?�S2�=�S1����,structure�?�S3�=�struct�val�x�=�4�end����,structure�?�S4�=�struct�val�x�=�4�end����,�the�j�names��S1��and��S2��refer�to�the�same�structure�and�are�\equal,"���whereas��S3��and����,�S4�s��are�dieren���t�structures�and�are�not�equal,�{?ev�en�though�the�righ�t-hand-sides����,are�UUiden���tical.�� �`���,�12.2��Y��Signatures�����,�It���is�often�useful�to�explicitly�constrain�a�structure�binding�to�limit�the�visibilit���y����,of�e�its�elds.���This�is�done�with�a��signatur��}'e�,�jwhic���h�is�to�a�structure�binding�as�a����,t���yp�Ge�s�constrain�t�is�to�a�v��q�alue�binding.�̭F��*�or�example,�{5w�e�migh�t�write�a�signature����,for�UUthe��Stack��mo�Gdule�as����;���sig�?�type�'a�stack����P��exception�?�Pop�and�Top����P��val�?�Empty�:�'a�stack����P��val�?�push�:�'a�*�'a�stack�->�'a�stack����P��val�?�empty�:�'a�stack�->�bool����P��val�?�pop�:�'a�stack�->�'a�stack����P��val�?�top�:�'a�stack�->�'a����;��end����,�The�
        !            43: �signature�men���tions�the�structure�comp�Gonen�ts�that�will�b�Ge�visible�outside����,the�UUstructure.����;Signatures�UUma���y�b�Ge�b�ound�to�iden���tiers�b�y�a�signature�declaration,����E�signature�UU�sig-Id��=��sig-expr��������33����"&%]�l�����'������R�where�Ű�sig-Id��is�an�iden���tier�and��sig-expr��is�a�signature�expression|either�a����R�sig�...�end��R�phrase�or�a�previously�b�Gound�signature�iden���tier.�MqTh�us,�� the��Rsignature����Rab�Go���v�e�UUcould�b�e�b�ound�to�the�iden���tier��STACK��b�y�the�declaration���͍�R�signature�?�STACK�=����a��sig�?�type�'a�stack����v��exception�?�Pop�and�Top����R.�?�.�.����a��end����a�A�m}signature�m�can�b�Ge�used�to�constrain�a�structure�b���y�including�it�in�a�structure����Rdeclaration:���̍�k�structure�UU�str-id��:��sig-expr��=��str����R�F��*�or�UUexample,�w���e�could�write����R�structure�?�Stack1�:�STACK�=�Stack����R�No���w��the�constructor��Push��is�not�a�visible�comp�Gonen�t�of�the��Stack1��structure,����Rsince�[�it�do�Gesn't�app�ear�in�the�signature;�^�the�qualied�iden���tier��Stack1.Push��is����Rerroneous.�� F��*�urthermore,�]�since�(��stack��is�men���tioned�in�the�signature�only�as�a����R�type�8��constructor�and�not�as�a��datatype��constructor,�q�the�iden���tier��Stack1.stack����R�is��8usable�as�a�t���yp�Ge�but�not�a�datat�yp�Ge.�1oFinally��*�,��0since�the�constructor��Empty��is����Rmen���tioned�L�as�a��val��in�the�signature,�N�but�not�as�a�constructor�(�i.e.�n��as�part�of�a����Rdatat���yp�Ge���sp�ecication),��^then��Stack1.Empty��ma���y�b�e�applied�as�a�function�but����Rnot�UUmatc���hed�in�a�pattern.����aThere�KIare�man���y�signatures�that�can�matc�h�the�structure��Stack�.�nnOne�of�the����R\broadest"�UUis����R�structure�?�Stack2�:�sig����v��datatype�?�'a�stack�=�Empty�|�Push�of�'a�*�'a�stack����v��exception�?�Pop�and�Top����v��val�?�empty�:�'a�stack�->�bool����v��val�?�push�:�'a�*�'a�stack�->�'a�stack����v��val�?�pop�:�'a�stack�->�'a�stack����v��val�?�top�:�'a�stack�->�'a����a��end����R=�?�Stack����R�and�UUthe�\narro���w�est"�UUis����R�structure�?�Stack3�:�sig�end�=�Stack����R�No���w,�sthe���structure��Stack2��is�equiv��q�alen�t�to��Stack�;�=it�is�the�\same"�structure,����Rand��all�the�same�elds�are�visible.��The�structure��Stack3��has�no�comp�Gonen���ts;����Rthere�=�are�no�qualied�iden���tiers�b�Geginning�with��Stack3.��Ho�w�ev�er,�w��Stack3��is����Rthe���\same"�for�structure-equiv��q�alence�purp�Goses�as��Stack�,���Stack1�,�and����Stack2�;����Rsignature��Oconstrain���ts�do�not�c�hange�the�iden�tit�y�of�a�structure,�
        !            44: just�whic�h�elds����Rare�UUvisible.�������34����#&.Ġl�����'���G�a��,�HApp���endix��FA��2�a��,�ISyn��4�tax�    ��of�the�full�language��5�=�����>���ide����!��dj��ID��Uc�symb��}'olic���or�alphab�etic������dj��*��Ug*����is�le��}'gal�as�a�value-identifer������dj��=���F�=����may�b��}'e�use�d�but�not�r�eb�ound�������8��opid����!��dj��ide������dj��op�UU�ide�}���r��}'emoves���inx�status�������0x��qualid����!��dj��ide������dj�ID�UU.�qualid�������5x�iden���t����!��dj��opid������dj�qualid�������>Ylab����!��dj��ID������dj�INT��@
�numeric���lab��}'els�������4�Y�const����!��dj��INT������dj�REAL������dj�STRING������dj�()������dj�iden���t�����nul���lary���c��}'onstructor�������<#��exp����!��dj��iden���t�����variable������dj��const������dj�#�UUlab��`�eld���sele��}'ctor������dj��f�UU�lab�=�exp�,������0���&���fe@����,�lab�=�exp��g����r��}'e�c�or�d������dj��(�UUexp�,������2���&���fe@����,�exp�)�Jq��tuple������dj��(�UUexp�;������1���&���fe@����;�exp�)�Jq��se��}'quenc�e������dj��[�UU�exp�,������0���&���fe@����,�exp��]�G���list������dj��let�UU�decs��in��expseq��end�1���lo��}'c�al���de�clar�ation������dj��exp�UUexp�v���applic��}'ation;���left{asso�ciative������dj��exp�UUide�exp�f�^�inxe��}'d���applic�ation������dj��exp�UU:�q�t���y�u�|�typ��}'e���c�onstr�aint������dj��exp�UU�andalso��exp�N���c��}'onjunction���������35����$&8��l�����'��������j��exp�UU�orelse��exp�S܎�disjunction�������j��fn�UU�matc���h�o�J�function�������j��case�UU�exp��of��matc���h�D܍�c��}'ase���expr�ession�������j��while�UU�exp��do��exp�KG<�iter��}'ation�������j��if�UU�exp��then��exp��else��exp�%�|�c��}'onditional�������j��exp�UU�handle��matc���h�H1��hand���le���exc��}'eption;�right{asso�ciative�������j��raise�UU�exp�k���r��}'aise���exc�eption�������Vx��matc���h����!���j��pat�UU�=>��exp��j������1���&���fe@����j��pat��=>��exp�������]��apat����!���j��iden���t�����variable���binding�������j��const����c��}'onstant���p�attern�������j��_��Ug�wildc��}'ar�d�������j��(�UUpat�)�������j�(�UUpat�,������2���&���fe@����,�pat)�Oq��tuple�������j��f�UU�pateld�,������0���&���fe@����,�pateld��g�%8��r��}'e�c�or�d�������j��f�UU�pateld�,������0���&���fe@����,�pateld�,�...�q��g�US�
exible���r��}'e�c�or�d�������j��[�UU�pat�,������0���&���fe@����,�pat��]�Ic��list�������b���pat����!���j��apat��#��atomic�������j��iden���t�UUapat�k�z�c��}'onstruction;���left{asso�ciative�������j��pat�UUide�pat�h@�inxe��}'d���c�onstruction�������j��pat�UU:�q�t���y�v���typ��}'e���c�onstr�aint�������j��opid�UUconstrain���t��as��pat�7B�layer��}'e�d�������P���pateld����!���j��lab�UU=�pat�n\z�normal�������j��ID��Uc�abbr��}'eviation�������j��ID�UU�as��pat�m���abbr��}'eviation�������h�!�t���y����!���j��tyvar�~Us�typ��}'e���variable�������j��f�UU�lab�:�q�t���y�,������0���&���fe@����,�lab�:�t���y��g�%8��r��}'e�c�or�d���typ�e�������j��(�UUt���y�)�������j�(�UUt���y�,������2���&���fe@����,�t�y�)�qualid�8���typ��}'e���c�onstruction�������j��t���y�UUqualid�qj��unary���typ��}'e�c�onstruction�������j��qualid�}���nul���lary���typ��}'e�c�onstruction�������j��t���y�UU�*������2���&���fe@����*��t�y�`���typle���typ��}'e�������j��t���y�UU�->��t�y�u���function���typ��}'e;�right{asso�ciative�������f�Y�vb����!���j��pat�UU=�exp�lj��simple�������j��vb�UU�and������1���&���fe@����and��vb�G�o�multiple�������E���constrain���t����!��&#�absent�������j��:�q�t���y��x��typ��}'e���c�onstr�aint�������b���rvb����!���j��opid�UUconstrain���t�=��fn��matc�h�q��simple���r��}'e�cursive�������j��rvb�UU�and������1���&���fe@����and��rvb�@�mutual���ly���r��}'e�cursive�������W@�clause����!���j��opid�UUapat������1���&���fe@����apat�constrain���t�=�exp���1�pr��}'ex�������j��pat�UUide�pat�constrain���t�=�exp���inx�������h�=�fb����!���j��clause�UU�j������1���&���fe@����j��clause�Cy�clausal���function�������j��fb�UU�and������1���&���fe@����and��fb�LG7�mutual���ly���r��}'e�cursive�������g���tb����!���j��t���yv��q�ars�UUID�=�t�y�X1��simple�������j��tb�UU�and������1���&���fe@����and��tb�J���multiple���������36����%&>(�l�����'��������0���t���yv��q�ars����!����absent������dj��tyvar�~Us�single������dj��(�UU�tyvar��,������2���&���fe@����,��tyvar��)�4�)�multiple�������@N=�db����!��dj��t���yv��q�ars�UUID�=�constr��j������1���&���fe@����j��constr�
        !            45: q��simple������dj��db�UU�and������1���&���fe@����and��db�GG7�mutual���ly���r��}'e�cursive�������0���constr����!��dj��opid�����nul���lary���(c��}'onstant)������dj��opid�UU�of��t���y�k���unary�������Aj��eb����!��dj��ide���F�nul���lary���(c��}'onstant)������dj��ide�UU�of��t���y�q��unary������dj��ide�UU=�qualid�bj��r��}'e{naming������dj��eb�UU�and������1���&���fe@����and��eb�I��multiple�������:1��ldec����!��dj��val�UU�vb�z���value���de��}'clar�ation������dj��val�UUrec��rvb�c���r��}'e�cursive���value�de��}'clar�ation������dj��fun�UU�fb�|��function���de��}'clar�ation������dj��type�UU�tb�v�R�typ��}'e���de�clar�ation������dj��datatype�UU�db�`#��datatyp��}'e���de�clar�ation������dj��exception�UU�eb�\)�exc��}'eption���de�clar�ation������dj��local�UU�ldec��in��ldec��end�4Nd�lo��}'c�al���de�clar�ation������dj��open�UU�qualid������1���&���fe@����qualid�5q��structur��}'e���visibility������dj��xit���y�UUide������1���&���fe@����ide�PG!�dir��}'e�ctive������dj��ldec�UUldec�r�E�de��}'clar�ation���se�quenc�e������dj��ldec�UU�;�~�-�optional���semic��}'olon�������4��xit���y����!��dj��infix�UU�INT�h���de��}'clar�e���inx,�left{asso��}'ciative������dj��infix�~Us�de��}'clar�e���inx,�pr��}'e�c�e�denc�e���0������dj��infixr�UU�INT�cj��inx,���right�asso��}'ciative������dj��infixr�yv�inx,���right�asso��}'c.,�pr�e�c.���0������dj��nonfix�yv�c��}'anc�el���inx�status�������<��sgn����!��dj��ID������dj��sig�UU�sp�Gecs��end�������4�>�sp�Gecs����!��dj��sp�Gec������dj�sp�Gecs�UUsp�ec������dj�sp�Gecs�UU;�������8�sp�Gec����!��dj��structure�UU�ID�:�sgn��and������1���&���fe@����and��ID�:�sgn������dj��datatype�UU�db��and������1���&���fe@����and��db������dj��type�UU�t���yv��q�ars�ID��and������1���&���fe@����and��t�yv��q�ars�ID������dj��val�UU�ide�:�q�t���y��and������1���&���fe@����and��ide�:�t���y������dj��exception�UU�exnsp�Gec��and������1���&���fe@����and��exnsp�ec������dj��sharing�UU�sharsp�Gec��and������1���&���fe@����and��sharsp�ec�������)x�exnsp�Gec����!��dj��ide������dj�ide�UU�of��t���y�������&UXsharsp�Gec����!��dj��qualid�UU=������2���&���fe@����=�qualid�����,This�odescription�is�at�presen���t�incomplete,�6as�it�is�missing�the�grammar�rules�for����,structures�UUand�functors.�������37����&&I��l�����'���I ��R�HApp���endix��FB��4 ��R�IThe�      ��standard�library��6 ��R�A��=standard���set�of�v��q�alues,��t���yp�Ges,�exceptions,�etc.�z�are����p��}'ervasive�|they�are�in����Rthe��^initial�en���vironmen�t��^and�a���v��q�ailable�in�ev�ery�structure.���This��standar��}'d��libr�ary����R�is��group�Ged�in���to�structures;��eac�h�structure�deals�with�the�op�Gerations�on�one�or����Rt���w�o�Еabstract�t���yp�Ges.��The�signature�of�eac�h�of�these�mo�Gdules,��dwith�an�informal����Rexplanation�UUof�the�seman���tics,�is�giv�en�in�this�c�hapter.�� ���R�signature�?�GENERAL�=����\�sig����f��infix�?�3�o����f��infix�?�before����f��exception�?�Bind����f��exception�?�Match����f��exception�?�Interrupt����f��exception�?�SystemCall�of�string����f��val�?�callcc�:�('a�cont�->�'a)�->�'a����f��val�?�throw�:�'a�cont�->�'a�->�'b����f��val�?�o�:�('b�->�'c)�*�('a�->�'b)�->�('a�->�'c)����f��val�?�before�:�('a�*�'b)�->�'a����f��datatype�?�'a�option�=�NONE�|�SOME�of�'a����f��type�?�'a�cont����f��type�?�exn����f��type�?�unit����f��infix�?�4�=�<>����f��val�?�=�:�''a�*�''a�->�bool����f��val�?�<>�:�''a�*�''a�->�bool����\�end����Rabstraction�?�General�:�GENERAL��������38����'&T�l�����'������,�The��structure��General��con���tains�v��q�arious�miscellaneous�and�general-purp�Gose�v�al-����,ues,�A�t���yp�Ges,�and�mexceptions.��The�inx��o��is�the�function�comp�osition�op�erator.����,The��inx��before��ev��q�aluates�b�Goth�of�its�argumen���ts�and�returns�the�rst�one.����,The�ɱexceptions��Bind��and��Match��are�automatically�raised�b���y�patterns�that�fail����,to���matc���h,��yas�explained�in�c�hapter�3.���The�exception��Interrupt��is�raised�when����,the��INTERR���UPT���signal�is�receiv�ed�(e.g.���when�the�user�t�yp�Ges�Con�trol-C���or�its����,equiv��q�alen���t).��The��Cfunctions��callcc��and��throw��and�the�t�yp�Ge��'a�?�cont��are�used����,for�!�explicit�manipulation�of�con���tin�uations,�,as�!�explained�no���where.�`�The�standard����,t���yp�Ge�P�exn��is�the�t�yp�Ge�of�all�exceptions�and��unit��is�the�t�yp�Ge�of�empt�y�records.����,The�"��=��and��<>��op�Gerators�are�dened�here��pr��}'o�P�forma�.��And�nally��*�,�Vthe��option����,�datat���yp�Ge�UUis�one�w�e�ha�v�e�often�found�con�v�enien�t.�� �    ���,�B.1��U8?List�����,�signature�?�LIST�=����6�sig����@��infixr�?�5�::�@����@��datatype�?�'a�list�=�::�of�('a�*�'a�list)�|�nil����@��exception�?�Hd����@��exception�?�Tl����@��exception�?�Nth����@��exception�?�NthTail����@��val�?�hd�:�'a�list�->�'a����@��val�?�tl�:�'a�list�->�'a�list����@��val�?�null�:�'a�list�->�bool����@��val�?�length�:�'a�list�->�int����@��val�?�@�:�'a�list�*�'a�list�->�'a�list����@��val�?�rev�:�'a�list�->�'a�list����@��val�?�map�:�
        !            46: �('a�->�'b)�->�'a�list�->�'b�list����@��val�?�fold�:�(('a�*�'b)�->�'b)�->�'a�list�->�'b�->�'b����@��val�?�revfold�:�(('a�*�'b)�->�'b)�->�'a�list�->�'b�->�'b����@��val�?�app�:�('a�->�'b)�->�'a�list�->�unit����@��val�?�revapp�:�('a�->�'b)�->�'a�list�->�unit����@��val�?�nth�:�'a�list�*�int�->�'a����@��val�?�nthtail�:�'a�list�*�int�->�'a�list����@��val�?�exists�:�('a�->�bool)�->�'a�list�->�bool����6�end���Ѝ�,�The�UUseman���tics�of�this�mo�Gdule�are�dened�b�y�the�follo�wing�implemen�tation.���
��,�abstraction�?�List:�LIST�=����6�struct����@��infixr�?�5�::�@����@��infix�?�6�+�-��������39����(&Y!�l�����'������f���datatype�?�'a�list�=�::�of�('a�*�'a�list)�|�nil����f��exception�?�Hd����f��fun�?�hd�(a::r)�=�a�|�hd�nil�=�raise�Hd����f��exception�?�Tl����f��fun�?�tl�(a::r)�=�r�|�tl�nil�=�raise�Tl����f��fun�?�null�nil�=�true�|�null�_�=�false����f��fun�?�length�nil�=�0�|�length�(a::r)�=�1�+�length�r����f��fun�?�op�@�(nil,l)�=�l�|�op�@�(a::r,�l)�=�a�::�(r@l)����f��fun�?�rev�l�=�let�fun�f�(nil,�h)�=�h������|�?�f�(a::r,�h)�=�f(r,�a::h)�������in�
        !            47: �f(l,nil)�������end����f��fun�?�map�f�=�let�fun�m�nil�=�nil�|�m�(a::r)�=�f�a�::�m�r�������in�
        !            48: �m�������end����f��fun�?�fold�f�=�let�fun�f2�nil�=�(fn�b�=>�b)����ʿ�|�?�f2�(e::r)�=�(fn�b�=>�f(e,(f2�r�b)))�����?�in�
        !            49: �f2�����?�end����f��fun�?�revfold�f�l�=�fold�f�(rev�l)����f��fun�?�app�f�l�=�(map�f�l;�())����f��fun�?�revapp�f�l�=�app�f�(rev�l)����f��exception�?�Nth����f��fun�?�nth(e::r,0)�=�e����q�|�?�nth(e::r,n)�=�nth(r,n-1)����q�|�?�nth�_�=�raise�Nth����f��exception�?�NthTail����f��fun�?�nthtail(e,0)�=�e����q�|�?�nth(e::r,n)�=�nthtail(r,n-1)����q�|�?�nth�_�=�raise�NthTail����f��fun�?�exists�f�=������let�?�fun�g�nil�=�false�|�g�(h::t)�=�f�h�orelse�g�t������in�
        !            50: �g������end����\�end��$ʧ���R�B.2��{8?Arra��=y��5\��R�signature�?�ARRAY�=����\�sig����f��infix�?�3�sub����f��type�?�'a�array����f��exception�?�Subscript��������40����)&a�l�����'������@���val�?�array�:�int�*�'1a�->�'1a�array����@��val�?�sub�:�'a�array�*�int�->�'a����@��val�?�update�:�'a�array�*�int�*�'a�->�unit����@��val�?�length�:�'a�array�->�int����@��val�?�arrayoflist�:�'a�list�->�'a�array����6�end��/|��,�Arra���ys�|rma�y�b�Ge�made�whose�elemen�ts�are�an�y�t�yp�Ge.���array(n,x)��returns�a�new����,arra���y�j}of��n��elemen�ts,�o�indexed�from�0�to��n�F����1,�initialized�j}to��x�.��>�a�?�sub�i�j}�returns����,the���i���^��th��Lp�elemen���t�of�the�arra�y��a�.�]��update(a,i,z)��sets�the��i���^��th��Lp�elemen�t�of�the�arra�y����,�a�UU�to�the�v��q�alue��z�p��.��ߍ�;Tw���o��Farra�ys�are�equal�if�and�only�if�they�are�the�same�arra�y�(created�with����,the�@same�call�to��array�);�$�except�that�all�arra���ys�of�length�0�ma�y�b�Ge�equal�to�eac�h����,other,�UUdep�Gending�on�the�implemen���tation.����;The�ճfollo���wing�implemen�tation�denes�the�seman�tics�of�arra�ys,�5�though�in����,practice�UUarra���ys�are�implemen�ted�m�uc�h�more�ecien�tly��*�.��/|��,�abstraction�?�Array�:�ARRAY�=����6�struct����;��type�?�'a�array�=�'a�ref�list����;��exception�?�Subscript����;��fun�?�array(0,x)�=�nil�|�array(n,x)�=�ref�x�::�array(n-1,x)����;��fun�?�a�sub�i�=�!(nth(a,i))�handle�Nth�=>�raise�Subscript����;��fun�?�update(a,i,z)�=�nth(a,i)�:=�z�handle�Nth�=>�raise�Subscript����;��fun�?�length�a�=�List.length�a����;��fun�?�arrayoflist�l�=�l����6�end��!P����,�B.3��U8?Input/Output���3��,�The���input/output�primitiv���es�are�in�tended�as�a�simple�basis�that�ma�y�b�Ge�compat-����,ibly��sup�Gerseded�b���y�a�more�comprehensiv�e�I/O��system�that�pro�vides�for�streams����,of�� arbitrary�t���yp�Ge�or�a�ric�her�rep�Gertoire�of�I/O���op�erations.�FYThe�IO���structure�con-����,tains��ball�I/O��Bprimitiv���es;�
�this�structure�will�in�all�implemen�tation�matc�h�(with����,thinning)�the�BASICIO��signature�pro���vided�b�Gelo�w,��but�ma�y�con�tain�other�prim-����,itiv���es�UUas�w�ell.��;Z��,�signature�?�BASICIO�=����6�sig����@��type�?�instream����@��type�?�outstream����@��exception�?�Io�of�string����@��val�?�std_in�:�instream����@��val�?�std_out�:�outstream��������41����*&gm�l�����'������f���val�?�open_in�:�string�->�instream����f��val�?�open_out�:�string�->�outstream����f��val�?�close_in�:�instream�->�unit����f��val�?�close_out�:�outstream�->�unit����f��val�?�output�:�outstream�->�string�->�unit����f��val�?�input�:�instream�->�int�->�string����f��val�?�lookahead�:�instream�->�string����f��val�?�end_of_stream�:�instream�->�bool����\�end��&��R�The���t���yp�Ge��instream��is�the�t�yp�Ge�of�input�streams�and�the�t�yp�Ge��outstream��is����Rthe�3t���yp�Ge�of�output�streams.�The�exception��Io��is�used�to�represen�t�all�of�the����Rerrors��?that�ma���y�arise�in�the�course�of�p�Gerforming�I/O.�The�v��q�alue�asso�ciated����Rwith���this�exception�is�a�string�represen���ting�the�t�yp�Ge�of�failure.�#iIn�general,��Xan�y����RI/O�a�op�Geration�a�ma���y�fail�if,�d�for�an�y�reason,�d�the�host�system�is�unable�to�p�Gerform����Rthe���requested�task.�EFThe�v��q�alue�asso�Gciated�with�the�exception�should�describ�e�the����Rt���yp�Ge�UUof�failure,�insofar�as�this�is�p�ossible.��
%���aThe�Q�standard�prelude�binds��std_in��to�an�instream�and��std_out��to�an�out-����Rstream.�mTF��*�or�G�in���teractiv�e�ML�G�pro�Gcesses,�J�these�are�exp�ected�to�b�e�asso�ciated�with����Rthe��cuser's�terminal.�m�Ho���w�ev�er,��gan��cimplemen�tation�that�supp�Gorts�the�connec-����Rtion�>of�pro�Gcesses�to�streams�ma���y�asso�ciate�one�pro�cess's��std_in��to�another's����R�std_out�.����aThe�EW�open_in��and��open_out��primitiv���es�are�used�to�asso�Gciate�a�disk�le�with����Ra���stream.�\sThe�expression��open_in(s)��creates�a�new�instream�whose�pro�Gducer����Ris�Oythe�le�named��s��and�returns�that�stream�as�a�v��q�alue.�o�Similarly��*�,�P��open_out(s)����R�creates�UUa�new��outstream��asso�Gciated�with�the�le��s�,�and�returns�that�stream.����aThe����input��primitiv���e�is�used�to�read�c�haracters�from�a�stream.��Ev��q�aluation����Rof�
F�input�?�s�n��causes�the�remo���v��q�al�of��n��c�haracters�from�the�input�stream��s�.���If����Rfew���er��Hthan��n��c�haracters�are�curren�tly�a�v��q�ailable,��then�the�ML��=system�will�blo�Gc�k����Run���til�"�they�b�Gecome�a�v��q�ailable�from�the�pro�Gducer�asso�ciated�with��s����^��1���|s�.�`�If�the�end�of����Rstream��is�reac���hed�while�pro�Gcessing�an��input�,�?few�er�than��n��c�haracters�ma�y�b�Ge����Rreturned.�q�A���ttempting�UU�input��from�a�closed�stream�raises��Io�.����aThe��vfunction��lookahead(s)��returns�the�next�c���haracter�on��instream�?�s��with-����Rout��       remo���ving�it�from�the�stream.�.Input�streams�are�terminated�b�y�the��close_in����R�op�Geration.��-This�        wprimitiv���e�is�pro�vided�primarily�for�symmetry�and�to�supp�Gort����Rthe�i�re-use�of�un���used�streams�on�resource-limited�systems.���The�end�of�an�input����Rstream��is�detected�b���y��end_of_stream�,��a�deriv�ed�form�that�is�dened�as�follo�ws:����R�fun�?�end_of_stream(s)�=�(lookahead(s)="")��R���ff��v�     J=����"5��-:�1���LܸThe���exact�denition�of�\a�Îv��ailable"�is�implemen�tation-dep�<renden�t.��F��J�or�instance,���op�<rerat-��        ��ing�]�systems�t�Îypically�buer�terminal�input�on�a�line-b�y-line�basis�so�that�no�c�haracters�are���a�Îv��ailable��Xun�til�an�en�tire�line�has�b�<reen�t�yp�<red.��������42����+&pO�l�����'������;�Characters�X�are�written�to�an��outstream��with�the��output��primitiv���e.�|wThe����,string���argumen���t�consists�of�the�c�haracters�to�b�Ge�written�to�the�giv�en�outstream.����,The��function��close_out��is�used�to�terminate�an�output�stream.�}BAn���y�further����,attempts�UUto�output�to�a�closed�stream�cause��Io��to�b�Ge�raised.����,�signature�?�IO�=����6�sig����@��type�?�instream����@��type�?�outstream����@��exception�?�Io�of�string����@��val�?�std_in�:�instream����@��val�?�std_out�:�outstream����@��val�?�open_in�:�string�->�instream����@��val�?�open_out�:�string�->�outstream����@��val�?�open_append�:�string�->�outstream����@��val�?�open_string�:�string�->�instream����@��val�?�close_in�:�instream�->�unit����@��val�?�close_out�:�outstream�->�unit����@��val�?�output�:�outstream�->�string�->�unit����@��val�?�input�:�instream�->�int�->�string����@��val�?�input_line�:�instream�->�string����@��val�?�lookahead�:�instream�->�string����@��val�?�end_of_stream�:�instream�->�bool����@��val�?�can_input�:�instream�->�int����@��val�?�flush_out�:�outstream�->�unit����@��val�?�is_term_in�:�instream�->�bool����@��val�?�is_term_out�:�outstream�->�bool����@��val�?�set_term_in�:�instream�*�bool�->�unit����@��val�?�set_term_out�:�outstream�*�bool�->�unit����@��val�?�execute�:�string�->�instream�*�outstream����@��val�?�exportML�:�string�->�bool����@��val�?�exportFn�:�string�*�(string�list�*�string�list�->�unit)�->�unit����@��val�?�use�:�string�->�unit����@��val�?�use_stream�:�instream�->�unit����@��val�?�reduce�:�('a�->�'b)�->�('a�->�'b)����@��val�?�mtime�:�instream�->�int����,(*�?�the�following�are�temporary�components�*)����@��val�?�reduce_r�:�((unit�->�unit)�->�(unit�->�unit))�ref����@��val�?�cleanup�:�unit�->�unit����@��val�?�use_f�:�(string�->�unit)�ref����@��val�?�use_s�:�(instream�->�unit)�ref����6�end����,structure�?�IO�:�IO��������43����,&}��l�����'������a�In�r%addition�to�the�basic�I/O�q�primitiv���es,���pro�vision�r%is�made�for�some�extensions����Rthat��Iare�lik���ely�to�b�Ge�pro�vided�b�y�man�y�implemen�tations.�ͣThe�functions�listed����Rab�Go���v�e�UUare�pro���vided�b�y�Standard�ML�of�New�Jersey��*�.��
���aThe�rRfunction��execute��is�used�to�create�a�pair�of�streams,�y�one�an��instream����R�and���one�an��outstream�,��oand�asso�Gciate�them�with�a�pro�cess.�9�The�string�argumen���t����Rto�UU�execute��is�the�op�Gerating-system�command�that�starts�the�pro�cess.����aThe�8�function��flush_out��ensures�that�the�consumer�asso�Gciated�with�an��out_stream����R�has�"receiv���ed�all�of�the�c�haracters�asso�Gciated�with�that�stream.���It�is�pro�vided����Rprimarily�     to�allo���w�the�ML��user�to�circum�v�en�t�undesirable�buering�c�haracteris-����Rtics���that�ma���y�arise�in�connection�with�terminals�and�other�pro�Gcesses.�<;All�output����Rstreams��are�
ushed�when�they�are�closed,��Fand�in�man���y�implemen�tations�an�out-����Rput��-stream�is�
ushed�whenev���er�a�newline�c�haracter�is�written�if�that�stream�is����Rconnected�UUto�a�terminal.����aThe��function��can_input��returns�the�n���um�b�Ger��of�c���haracters�whic�h�ma�y�b�Ge����Rread���from�its�instream�argumen���t�without�blo�Gc�king.�q�F��*�or�instance,�*�a�command����Rpro�Gcessor���ma���y�wish�to�test�whether�or�not�a�user�has�t�yp�Ged�ahead�in�order�to����Ra���v�oid�ݝan�unnecessary�prompt.�
        !            51: �The�exact�denition�of�\curren���tly�a�v��q�ailable"�is����Rimplemen���tation�]�sp�Gecic,���p�erhaps�dep�ending�on�suc���h�things�as�the�pro�cessing����Rmo�Gde�UUof�a�terminal.����aThe�"�input_line��primitiv���e�returns�a�string�consisting�of�the�c�haracters�from����Ran����instream��up�through,���and�including,�the�next�end�of�line�c���haracter.�b�If�the����Rend���of�stream�is�reac���hed�without�reac�hing�an�end�of�line�c�haracter,���all�remaining����Rc���haracters�UUfrom�the�stream�(�without��an�end�of�line�c�haracter)�are�returned.����aFiles��?ma���y�b�Ge�op�en�for�output�while�preserving�their�con���ten�ts��?b�y�using�the����R�open_append���primitiv���e.�"Subsequen�t��output��to�the�outstream�returned�b�y�this����Rprimitiv���e�UUis�app�Gended�to�the�con�ten�ts�of�the�sp�Gecied�le.����aBasic���supp�Gort�for�the�complexities�of�terminal�I/O���are�pro���vided.�J�The�pair�of����Rfunctions��is_term_in��and��is_term_out��test�whether�or�not�a�stream�is�asso�Gci-����Rated�Jwith�a�terminal;�M�and��set_term_in��and��set_term_out��tell�the�ML�J&system����Rthat�0Xa�stream�is�(or�is�not)�a�terminal.��These�functions�are�esp�Gecially�useful����Rwith���std_in��and��std_out��b�Gecause�they�are�op�ened�as�part�of�the�standard�pre-����Rlude.��WA�l*terminal�l0ma���y�b�Ge�asso�ciated�with�a�stream�using�the�ordinary��open_in����R�and��
        !            52: �open_out��functions;���the�naming�con���v�en�tion��
        !            53: to�do�this�is�implemen���tation-����Rdep�Genden���t.����aGiv���en��#a�name�of�a�le,����use��compiles�and�executes�its�con�ten�ts�as�if�they�w�ere����Rt���yp�Ged�ǜin�to�the�top-lev�el�prompt�of�the�in�teractiv�e�system.�Ȝ�use��ma�y�b�Ge�nested����Rrecursiv���ely��*�.�q�Similarly�,�UU�use_stream��compiles�an�already-op�Gened�instream.����a�exportML����creates���an�executable�le�whose�name�is�giv���en�b�y�the�argumen�t.����RWhen�N{this�le�is�executed,��it�is�an�ML�N9system�in�exactly�the�same�state�as�the�one����Rthat�8�wrote�the�le.��F��*�or�example,�q�the�command��(exportML�?�"foo";�print�"Hello");����R�writes��Fa�le�that,��Iwhen�executed,�prin���ts��Hello��and�then�returns�to�the�top-lev�el����Rprompt.�c�exp�GortML�*|returns�*�true�when�the�executable�le�is�run,�3and�false�when����Rsimply�UUreturning.�������44����-&�_�l�����'������;�exportFn��%�creates�an�executable�le�whose�name�is�giv���en�b�y�the�rst�argu-����,men���t.�$When�1tthis�le�is�executed,�h|it�is�an�ML�1;system�that�calls�the�function����,giv���en��3as�the�second�argumen�t,���then�exits.�RbThe�ML���system�created�will�not����,ha���v�e�(�a�compiler�or�a�top-lev���el,�]]so�it�will�b�Ge�signican�tly�more�compact.��tThe����,command-line�;argumen���ts�and�en�vironmen�t�are�passed�as�the�string�list�argu-����,men���ts�L�to�the�function�that�is�called.�n��exportFn��terminates�execution�of�the�ML����,system�UUthat�called�it.��"j���,�B.4��U8?Bo�u�ol��=���,�signature�?�BOOL�=����6�sig����@��datatype�?�bool�=�true�|�false����@��val�?�not:�bool�->�bool����@��val�?�print:�bool�->�bool����@��val�?�makestring:�bool�->�string����6�end������,�These�UUare�quite�straigh���tforw�ard,�UUand�can�b�Ge�dened�as�follo���ws:������,�abstraction�?�Bool:�BOOL�=����6�struct����@��datatype�?�bool�=�true�|�false����@��fun�?�not�true�=�false�|�not�false�=�true����@��fun�?�makestring�true�=�"true"�|�makestring�false�=�"false"����@��fun�?�print�b�=�(output(std_out,�makestring�b);�b)����6�end��"l���,�B.5��U8?ByteArra��=y����,�signature�?�BYTEARRAY�=����6�sig����@��infix�?�3�sub����@��eqtype�?�bytearray����@��exception�?�Subscript����@��exception�?�Range����@��val�?�array�:�int�*�int�->�bytearray����@��val�?�sub�:�bytearray�*�int�->�int����@��val�?�update�:�bytearray�*�int�*�int�->�unit����@��val�?�length�:�bytearray�->�int����@��val�?�extract�:�bytearray�*�int�*�int�->�string����@��val�?�fold�:�((int�*�'b)�->�'b)�->�bytearray�->�'b�->�'b����@��val�?�revfold�:�((int�*�'b)�->�'b)�->�bytearray�->�'b�->�'b��������45����.&���l�����'������f���val�?�app�:�(int�->�'a)�->�bytearray�->�unit����f��val�?�revapp�:�(int�->�'b)�->�bytearray�->�unit����\�end���Ѝ�R�Byte�r�arra���ys�are�just�lik�e�arra�ys�of�in�tegers,�z7with�the�restriction�that�the�v��q�alues����Rof��othe�comp�Gonen���t�in�tegers�m�ust�b�Ge�b�et���w�een��o0�and�255.��The�in���ten�t��ois�that�the����Rimplemen���tation�UUma�y�store�them�more�ecien�tly�than�the�equiv��q�alen�t�arra�y��*�.����aNote�,�that,�4�b���y�default,�the�ByteArra���y�structure�is�presen�t�but�not�op�Gened�in����Rthe�     %initial�en���vironmen�t.�XbThe�      %declaration��open�?�ByteArray��ma���y�b�Ge�used�to�op�en����Rit.���The�buse�of�ByteArra���y�is�discouraged;�]ifuture�v�ersions�of�the�compiler�ma�y����Rnot�UUsupp�Gort�it,�or�(for�example)�debuggers�migh���t�not�supp�ort�it.����aThe�UUseman���tics�can�b�Ge�dened�b�y�this�implemen�tation:���
��R�abstraction�?�ByteArray�:�BYTEARRAY�=����\�struct����f��infix�?�3�sub����f��type�?�bytearray�=�int�array����f��exception�?�Subscript�=�Array.Subscript����f��exception�?�Range����f��fun�?�check�x�=�if�x<0�orelse�x>255�then�raise�Range�else�()����f��fun�?�array(i,x)�=�(check�x;�Array.array(i,x))����f��val�?�length�=�Array.length����f��fun�?�update(a,i,x)�=�(check�x;�Array.update(a,i,x))����f��val�?�op�sub�=�Array.sub����f��fun�?�extract(b,i,0)�=�if�i<0�orelse�i>length(b)������then�?�raise�Subscript�
        !            54: �else�""����q�|�?�extract(b,i,n)�=�chr(b�sub�i)�^�extract(b,i,n-1)����f��val�?�fold�=�
        !            55: �.�.�.����f��val�?�revfold�=�
        !            56: �.�.�.����f��val�?�app�=�...����f��val�?�revapp�=�...����\�end�� ����R�B.6��{8?In��=teger�����R�signature�?�INTEGER�=����\�sig����f��infix�?�7�*�div�mod����f��infix�?�6�+�-����f��infix�?�4�>�<�>=�<=����f��exception�?�Div����f��exception�?�Overflow����f��type�?�int����f��val�?�~�:�int�->�int��������46����/&�ʠl�����'������@���val�?�*�:�int�*�int�->�int����@��val�?�div�:�int�*�int�->�int����@��val�?�mod�:�int�*�int�->�int����@��val�?�+�:�int�*�int�->�int����@��val�?�-�:�int�*�int�->�int����@��val�?�>�
        !            57: �:�int�*�int�->�bool����@��val�?�>=�:�int�*�int�->�bool����@��val�?�<�
        !            58: �:�int�*�int�->�bool����@��val�?�<=�:�int�*�int�->�bool����@��val�?�min�:�int�*�int�->�int����@��val�?�max�:�int�*�int�->�int����@��val�?�abs�:�int�->�int����@��val�?�print�:�int�->�unit����@��val�?�makestring�:�int�->�string����6�end���x��,�This���should�b�Ge�mostly�self-explanatory��*�.�)�The�function��div��raises��Div��on�divide����,b���y��Yzero,�Ћotherwise��Overflow��if�the�result�do�Gesn't�t;��similarly��mod��ma�y�raise��Div����,�or�j��Overflow�.���Other�op�Gerators�ma���y�raise��Overflow��if�the�result�do�esn't�t�in���to����,their� &represen���tation.���Standard�ML��of�New�Jersey�uses�nite�precision�signed����,31-bit�UUin���tegers,�whic�h�can�represen�t�a�range�from���2���^��30���;�to�2���^��30��
        !            59: ����8��1.��$h����,�B.7��U8?Real�����,�signature�?�REAL�=����6�sig����@��infix�?�7�*�/����@��infix�?�6�+�-����@��infix�?�4�>�<�>=�<=����@��type�?�real����@��exception�?�Floor�and�Sqrt�and�Exp�and�Ln����@��exception�?�Real�of�string����@��val�?�~�:�real�->�real����@��val�?�+�:�(real�*�real)�->�real����@��val�?�-�:�(real�*�real)�->�real����@��val�?�*�:�(real�*�real)�->�real����@��val�?�/�:�(real�*�real)�->�real����@��val�?�>�:�(real�*�real)�->�bool����@��val�?�<�:�(real�*�real)�->�bool����@��val�?�>=�:�(real�*�real)�->�bool����@��val�?�<=�:�(real�*�real)�->�bool����@��val�?�abs�:�real�->�
        !            60: �real����@��val�?�real�:�int�->�real��������47����0&�\�l�����'������f���val�?�floor�:�real�->�int����f��val�?�truncate�:�real�->�int����f��val�?�ceiling�:�real�->�int����f��val�?�sqrt�:�real�->�real����f��val�?�sin�:�real�->�real����f��val�?�cos�:�real�->�real����f��val�?�arctan�:�real�->�real����f��val�?�exp�:�real�->�real����f��val�?�ln�:�real�->�real����f��val�?�print�:�real�->�unit����f��val�?�makestring�:�real�->�string����\�end����Rstructure�?�Real�:�REAL���|��a�This�1�should�b�Ge�mostly�self-explanatory��*�.�Except�for�the�sp�ecial�exceptions����R�Floor�,���Sqrt�,��Exp�,��Ln�,�raised��b���y�the�functions�of�the�corresp�Gonding�names,��all����Rreal-n���um�b�Ger���functions�raise�only�the��Real��exception�with�some�system�dep�en-����Rden���t�UUargumen�t�string.��"J���R�B.8��{8?Ref��T���R�signature�?�REF�=����\�sig����f��infix�?�3�:=����f��val�?�!�:�'a�ref�->�'a����f��val�?�:=�:�'a�ref�*�'a�->�unit����f��val�?�inc�:�int�ref�->�unit����f��val�?�dec�:�int�ref�->�unit����\�end����a�Reference�0�v��q�alues�are�describ�Ged�in�c���hapter�10.��The�functions��inc��and��dec����R�can�UUb�Ge�dened�as���|��\��fun�?�inc�i�=�i�:=�!i+1����\�fun�?�dec�i�=�i�:=�!i-1��"J���R�B.9��{8?String��T���R�signature�?�STRING�=����\�sig����f��infix�?�6�^����f��infix�?�4�>�<�>=�<=����f��type�?�string��������48����1&�>�l�����'������@���exception�?�Substring����@��val�?�length�:�string�->�int����@��val�?�size�:�string�->�int����@��val�?�substring�:�string�*�int�*�int�->�string����@��val�?�explode�:�string�->�string�list����@��val�?�implode�:�string�list�->�string����@��val�?�<=�:�string�*�string�->�bool����@��val�?�<�
        !            61: �:�string�*�string�->�bool����@��val�?�>=�:�string�*�string�->�bool����@��val�?�>�
        !            62: �:�string�*�string�->�bool����@��val�?�^�
        !            63: �:�string�*�string�->�string����@��exception�?�Chr����@��val�?�chr�:�int�->�string����@��exception�?�Ord����@��val�?�ord�:�string�->�int����@��val�?�ordof�:�string�*�int�->�int����@��val�?�print�:�string�->�string����6�end������,�Strings�r�can�b�Ge�explained�b���y�the�implemen�tation�b�Gelo�w;���of�course,�zDin�practice�a����,more�UUecien���t�implemen�tation�is�used.��UU��,�abstraction�?�String�:�STRING�=����6�struct����@��infix�?�6�^����@��infix�?�4�>�<�>=�<=����@��type�?�string�=�int�list����@��exception�?�Substring����@��val�?�length�=�List.length����@��val�?�size�=�length����@��fun�?�substring(s,0,0)�=�nil����K�|�?�substring(a::b,0,len)�=�a::substring(b,0,len-1)����K�|�?�substring(nil,_,_)�=�raise�Substring����K�|�?�substring(a::b,i,len)�=�substring(b,i-1,len)����@��fun�?�explode�nil�=�nil����K�|�?�explode�(i::l)�=�[i]�::�explode�l����@��fun�?�implode�nil�=�nil����K�|�?�implode�(s::l)�=�s�@�implode�l����@��fun�?�(_::_)�>�nil�=�true����K�|�?�nil�>�(_::_)�=�false����K�|�?�(i::r)�>�(j::s)�=�Integer.>(i,j)�orelse�i=j�andalso�r>s����@��fun�?�a�<=�b�=�not�(a>b)����@��fun�?�a�<�b�=�b�>�a����@��fun�?�a�>=�b�=�b�<=�a����@��val�?�op�^�=�op�@��������49����2&���l�����'������f���exception�?�Chr����f��fun�?�chr�i�=�if�i<0�orelse�i>255�then�raise�Chr�else�[i]����f��exception�?�Ord����f��fun�?�ord�nil�=�raise�Ord�|�ord�(i::r)�=�i����f��fun�?�ordof(s,i)�=�nth�s�handle�Nth�=>�raise�Ord����f��fun�?�print�s�=�(output(std_out,s);�s)����\�end��!捍�R�B.10����Bits��荍�R�signature�?�BITS�=����\�sig����f��type�?�int����f��val�?�orb�:�int�*�int�->�int����f��val�?�andb�:�int�*�int�->�int����f��val�?�xorb�:�int�*�int�->�int����f��val�?�lshift�:�int�*�int�->�int����f��val�?�rshift�:�int�*�int�->�int����f��val�?�notb�:�int�*�int�->�int����\�end����Rstructure�?�Bits�:�BITS����R�The�9�structure�Bits�allo���ws�shifting�and�masking�of�in�tegers�(view�ed�as�strings����Rof��[binary�digits).���This�structure�is�presen���t�but��not��op�Gened�in�the�standard����Ren���vironmen�t;���its�vuse�is�discouraged.��)The�righ���t�shift�(rshift)�op�Gerator�ma�y�shift����R0's,�UU1's,�or�sign�bits�in���to�the�left�end�of�an�in�teger�at�its�discretion.��!䍍�R�B.11����System����R�signature�?�SYSTEM�=����\�sig����f��structure�?�Control�:�CONTROL����f��structure�?�Tags�:�TAGS����f��structure�?�Timer�:�TIMER����f��structure�?�Stats�:�STATS����f��structure�?�Unsafe�:�UNSAFE����f��val�?�exn_name�:�exn�->�string����f��val�?�version�:�string����f��val�?�interactive�:�bool�ref����f��val�?�cleanup�:�unit�->�unit����f��val�?�system�:�string�->�unit����f��val�?�cd�:�string�->�unit����f��val�?�argv�:�unit�->�string�list��������50����3&�o�l�����'������@���val�?�environ�:�unit�->�string�list����6�end����,structure�?�System�:�SYSTEM����,�F��*�eatures�Y�of�Standard�ML�Ytof�New�Jersey�that�should�not�b�Ge�exp�ected�in�an���y����,other��implemen���tation�of�ML��are�group�Ged�in�to�the�System�structure,�Mowhic�h�is����,presen���t�UUbut�not�op�Gened�in�the�standard�en�vironmen�t.����;The�UUsubstructures��Control��of��System��are�not�do�Gcumen���ted.����;The��function��exn_name��returns�the�name�of�the�exception�constructor�that����,w���as�Ydused�to�build�a�giv�en�exception�v��q�alue.�}�The��version��string�indicates�whic�h����,v���ersion�ͯof�Standard�ML�͐of�New�Jersey�is�running.���The�v��q�ariable��interactive����,�ma���y�ĺb�Ge�set�to�indicate�whether�the�compiler's�input�stream�should�b�e�treated����,as�:\in���teractiv�e�(i.e.�h�issue�primary�and�secondary�prompts,�?�read�a�line�at�a�time)����,or�non-in���teractiv�e�(i.e.�[
        !            64: no�prompts,��read�a�large�blo�Gc�k�at�a�time).�[
        !            65: The��cleanup����,�function��7closes�all�les.�|mThe��system��function�runs�an�op�Gerating-system�(shell)����,command�gsp�Gecied�b���y�its�argumen�t.���cd��c�hanges�the�curren�t�w�orking�directory��*�.����,�argv�|��and��environment��return�the�command-line�argumen���t-list�and�(Unix)�en-����,vironmen���t�UUwith�whic�h�the�Standard�ML�pro�Gcess�w�as�created.�������51����4&���l�����'���G��R�HApp���endix��FC��2��R�ICompatibilit��4�y��5Vx��R�The�,�language�denition�has�c���hanged�a�bit�o�v�er�the�past�y�ear�(1986{87),�5partic-����Rularly���in�the�area�of�exception�syn���tax.��Some�attempt�has�b�Geen�made�to�allo�w����Rold�UUprograms�to�con���tin�ue�UUrunning.��!č��R�C.1��{fTExceptions�����R�Three�72k���eyw�ords:�5��exceptionx�,�o��raisex�,�and��handlex��are�pro���vided.�^They�im-����Rplemen���t��the�old-st�yle�exception�mec�hanism,�        �and�are�treated�as�deriv�ed�forms.��5�-���������v|m�Deriv��9ed��TF��
        !            66: �orm�����Equiv��\ralen��9t��TF��
        !            67: �orm���R��ff&���fd���exceptionx�UU�iden���tier�����5�exception�UU�Iden���tier������exceptionx�UU�iden���tier��:��t�y�����5�exception�UU�Iden���tier��of��t�y������raisex�UU�iden���tier�����5�raise�UU�Iden���tier������raisex�UU�iden���tier��with��exp�����5�raise�UU�Iden���tier��(��exp��)�����handlex�UU�iden���t��=>��exp�����5�handle�UU�Iden���t��=>��exp������handlex�UU�iden���t��=>��exp�����5�handle�UU�Iden���t�()��=>��exp������handlex�UU�iden���t��with��pat��=>��exp�����5�handle�UU�Iden���t�pat��=>��exp����ff&��������aThe���deriv��q�ations�for��handlex��are�a�bit�more�in���tricate�than�sho�wn�ab�Go�v�e.�I�The����Rin���ten�t�a&is�that�an���y�program�that�w�orks�under�the�old�sc�heme�will�con�tin�ue�to�w�ork����Rif���all�instances�of��exception�,�ܡ�handle�,�and����raise��are�c���hanged�to��exceptionx�,����R�handlex�,�UUand��raisex��resp�Gectiv���ely��*�.����aNote�*�that�the�deriv���ed�forms�c�hange�the�exception�iden�tier�in�con�v�erting�to����Rthe��'standard�forms;��they�capitalize�the�rst�letter.�a<This�is�to�sim���ulate�the�old����Rsc���heme,���in��_whic�h�exception�names�liv�ed�in�a�dieren�t�space�from�v��q�alue�names.����RThis���will�cause�problems�in�an���y�programs�with�a�capitalized�v��q�alue-name�that����Rhapp�Gens�UUto�con
ict�with�a�(capitalized�or�uncapitalized)�exception�name.�������52����&�V&���;�l&��
        !            68: 6�I�"V�G
        !            69: cmbx10�H�"V�p
        !            70: cmbx10�9D��tG�G�cmr17�7�"Vff
        !            71: cmbx10�0��N�cmbx12�+X�Qcmr12���<x
        !            72: 
        !            73: cmtt10��"V
        !            74: 
        !            75: cmbx10��':
        !            76: 
        !            77: cmti10�!",�
        !            78: 
        !            79: cmsy10��b>
        !            80: 
        !            81: cmmi10�K�`y
        !            82: 
        !            83: cmr10�#�f�cmti8�
|{Ycmr8�
        !            84: O!�cmsy7�   0e�rcmmi7�ٓ�Rcmr7��Aa�cmr6�&�u����

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.