LISP: Declarations In Lisp


Published on

LISP: Declarations In Lisp

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

LISP: Declarations In Lisp

  1. 1. Declarations in Lisp<br />
  2. 2. Declarations<br />Declarations allow the user to specify extra information about the program to Lisp system.<br />Declarations are truly optional but special declarations do effect the interpretation of the variable bindings and references and so must be specified wherever necessary.<br />Declarations are one of the good ways to add documentation to the program.<br />
  3. 3. overview<br />Declaration syntax<br />Declaration specifiers<br />Type declaration for forms<br />
  4. 4. Declaration syntax<br />declare construct is used to embed declarations within the executable code.<br />A declare form is known as the declaration.<br />A declaration may occur only at the beginning of the bodies of the certain special forms.<br />declare {decl-spec}*<br />
  5. 5. Declarations may occur in the lambda expressions and in the forms listed below:<br />
  6. 6. Variable bindings appear only to the bindings made by the form at the head of whose body they appear.<br />(defun foo (x)<br /> (declare (type float x))..<br />(let (( x’a)) …)<br />….) <br />The type declaration applies only to the outer binding of the x and not to the binding made in the let.<br />The scope of the declaration always includes the body forms, as well as any “stepper” or “result” forms, of the special forms or lambda-expressions.<br />If the declaration applies to a name binding, then the scope of declaration also includes the scope of the name binding.<br />
  7. 7. locally {declaration}* {form}*<br />This macro is used to make local pervasive declarations where desired.<br />This construct does not bind any variables.<br />Special declaration may be used with lacally to pervasively affect references to, rather than bindings of, variables.<br />The function proclaim takes a decl-spec as its argument and puts it into effect globally.<br />
  8. 8. proclaim decl-spec<br />The proclaim takes decl-spec as the argument and puts it into effect globally.<br />Ex: (proclaim ‘(type float tolerance)) specifies that the dynamic value of the tolerance should always be a floating-point number.<br />A proclaim takes special decl-spec as applying to all bindings as well as to all references of the mentioned variables.<br />This macro is syntactically like declare and semantically like proclaim.<br />It is an executable form and may be used anywhere proclaim may be called.<br />declaim {decl-spec}*<br />
  9. 9. Declaration specifiers<br />List of valid declaration specifiers for use in declare.<br />(special var1 var2….) specifies that all of the variables named are to be considered special.<br />(type var1, var2,…) affects only variable bindings and specifies that the variables mentioned will take on values only of the specified type.<br />
  10. 10. (ftype type function-name-1, function-name-2,…) specifies that the named functions will be of the function type type.<br />Ex: (declare (ftype(function (integer list) t) nth)<br /> (ftype (function (number) float) sin cos))<br />To extend ftype declaration specifiers to accept any function-name, one may write<br />(declaim (ftype (function (list) t) (setf cadr)))<br />
  11. 11. (inline function1, function2,…)specifies that it is desirable for the compiler to open code calls to the specified functions.<br />The compiler is free to ignore this declaration.<br />(notinline function1, function2,…)specifies that it is undesirable for the compiler to compile the specified functions inline.<br />This declaration is pervasive and the compiler is not free to ignore this declaration.<br />(ignore var1,var2,….) affects only variable bindings and specifies that the bindings of the specified variables are never used.<br />(optimize (quality1 value1) (quality2 vale2)…) advises compiler that each quality must be given attention according to the specified corresponding value.<br />
  12. 12. (declaration name1, name2,…) advises the compiler that each namej is a valid but non-standard declaration name.<br />The is kind of declaration may be used only as a proclamation.<br />Ex: (proclaim ‘(declaration author<br /> target-language<br /> target-machine))<br />  (proclaim ‘(target-language ada))<br /> (proclaim ‘(target-machine IBM-650))<br />(defun strangep(x)<br /> (declare (author “Harry Tweeker”))<br /> (member x’(strange weird odd peculiar)))<br />
  13. 13. Type declarations for forms<br />The special form is defined to declare the type of value held by a bound variable.<br />(the type form)the type of value means the type of value of is declared to be of type type.<br />the value-type formthe form is evaluated and whatever it produces is returned by the form, it is an error if what is returned by the form does not confirm to the data-type specified by value-type.<br />
  14. 14. Examples:<br />(the string (copy-seq x)) the result will be a string.<br />(the integer (+ x 3)) the result of + will be an integer.<br />(+ (the integer x) 3)the value of x will be an integer.<br />
  15. 15. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at<br />