Theme index -- Keyboard shortcut: 'u'  Previous theme in this lecture -- Keyboard shortcut: 'p'  Next slide in this lecture -- Keyboard shortcut: 'n'Linguistic abstraction
22.  Introduction to linguistic abstraction

This section if about linguistic abstraction in Scheme, with and without LAML. Linguistic abstraction is the act of making new languages. Thus, with linguistic abstraction we form new ways to express ourselves far beyond functional abstraction, as it has been studied until now.

22.1 Linguistic abstraction22.4 An example of fine grained abstraction
22.2 Linguistic abstraction in Lisp22.5 Coarse grained linguistic abstraction in Lisp
22.3 Fine grained linguistic abstraction in Lisp22.6 An example of coarse grained abstraction
 

22.1.  Linguistic abstraction
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

Linguistic abstraction can be defined very briefly, as follows.

Linguistic abstraction is the act of establishing a new language

We introduce linguistic abstraction by comparing it with the more well-known discipline of making abstractions with functions (or for that sake, procedural abstraction).

  • Abstraction by means of functions

    • Encapsulation, naming and parametrization of a potentially complex expression

    • Use of good abstractions makes a program easier to understand

    • Use of abstractions makes a program shorter, because the functions can be called from more than one context

  • Abstraction by means of languages

    • Involves primitive means of expressions as well as the way the primitives can be meaningfully composed

    • A more global kind of abstraction than functional abstraction

    • Raises lexical, syntactical and - most important - semantical concerns

    • Specialized or general purpose language

Problem solving by means of linguistic abstraction is a very powerful approach

The idea of defining and implementing a new language as part of a problem solving process is a very strong idea. Expert programmers tend to work in that way.

 

22.2.  Linguistic abstraction in Lisp
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

The primary language of interest in this material is Scheme, and with it the family of Lisp languages. Therefore we will at this point study linguistic abstraction in Lisp languages.

The are several possible approaches to linguistic abstractions in Lisp

Below we discuss an incremental approach to linguistic abstraction in Lisp. This approach is based on the point of view that definition of functions, procedures, and macros in Lisp contribute with new aspects of the Lisp language. This is discussed in additional detail in Section 22.3 and Section 22.4.

The contrast to an incremental approach will be called a total approach. As it appears from the items below, compilation and interpretation are considered as 'a total linguistic abstraction implementation technique'. Total linguistic abstraction is discussed in more details in Section 22.5 and Section 22.6.

  • Incremental approaches

    • Each new construct is defined by a function or a macro

      • Macros are used for new surface syntax, and in cases where evaluation order issues prevent use of functions

    • Fine grained linguistic abstraction

  • Total approaches

    • Writing an interpreter for the new language    or

    • Translating the new language to an existing language (compilation)

    • Coarse grained linguistic abstraction

In some cases we embed the new language in an existing language, hereby combining the use of two or more languages in a single program or document

Language embedding is the issue of Chapter 23.

 

22.3.  Fine grained linguistic abstraction in Lisp
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

In this section, and in Section 22.4, we will discuss and give examples of fine grained linguistic abstraction in Lisp.

The main insight is that program contributions in terms of function definitions can be regarded as extensions of the Scheme language. The reason is that the status, use, and appearance of a new function is similar to both core language constructs and the pre-existing Scheme functions and procedures. In most other languages, there is a clear distinction of core language constructs and contributions made in terms of programs written in the language.

Due to the uniform notation of language constructs and functions, a set of Scheme functions can be seen as an extension of the Scheme language

  • Incremental extension of the language

    • The functional paradigm is well-suited because application of functions can be nested into each other

    • The definitions of the functions implement the language

    • A possible approach:

      • the inner functions are more or less self evaluating

      • the outermost function is responsible for the realization of the language

A function call is an expression, which can be embedded into other function calls. In this way it is possible to build complex expressions by combination of programmed, functional abstractions.

As a contrast in the imperative programming paradigm, a procedure call is a command. A command can not normally be passed as a parameter to other commands. Thus, the combination of programmed abstractions is different, when we compare imperative and functional programming.

In section Section 22.4 we will see an example of the observations made above.

Programming in Lisp can be seen as incremental language development

 

22.4.  An example of fine grained abstraction
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

In this section we will study a simple formulation of a course home page, built by means of functions, and combined in the way discussed at the end of Section 22.3.

The course-home-page clause of Program 22.1 is to be processed somehow to create a set of course home pages. The functions used in Program 22.1 are defined in Program 22.2. As illustrated in Program 22.2 the subclauses of a course-home-page page are very simple, almost 'self-evaluating functions'. The course-home-page function itself is assumed to do the bulk part of the work - the real work so to say. This part of the program is only outlined in Program 22.2.

A fine grained implementation of a course home page language

Each of the forms in the language are implemented as a Scheme function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(course-home-page

  (name "Programming Paradigms")

  (number-of-lectures 15)

  (lecture-names
    "intr" "scheme" "higher-order-fn" 
    "eval-order" "lisp-languages")

  (current-lecture 3)

  (links
    "schemers.org" "http://www.schemers.org/"
    "LAML" "http://www.cs.auc.dk/~normark/laml/"
    "Haskell" "http://haskell.org/"
  )
)
Program 22.1    A sample document in a course home page language.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(define (course-home-page name-form number-form lecture-list current-form
                          link-form process-form)

  ; The real implementation of
  ; the course home page language

)



(define (name nm) 
 (list 'name nm))

(define (number-of-lectures n) 
 (list 'number-of-lectures n))

(define (lecture-names . name-lst) 
 (cons 'lecture-names name-lst))

(define (current-lecture n) 
 (list 'current-lecture n))

(define (links . lnk-list) 
 (cons 'links lnk-list))
Program 22.2    An almost empty outline of the functions that implement the course home page language.

The ideas in this section have been explored and developed in a LAML context. Early LAML languages, such as the 'Manual' language (for definition of library interface documentation) has been defined in the way illustrated above. More recent LAML-related language have been defined as XML-in-LAML language, and implemented as mirrors of an XML language in LAML. This approach is addressed in Chapter 24.

 

22.5.  Coarse grained linguistic abstraction in Lisp
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

As mentioned in Section 22.2 coarse grained linguistic abstraction is related to translation (compilation) and interpretation, as known from courses in compiler technology.

As a Scheme and Lisp topic related to transformation and interpretation, we notice on this page that parsing of an expression or program made in Lisp syntax (cf. parenthesized notation, Section 6.8), is very easy. The reason is that a generic parser can be written that translates a parenthesized string to a proper or improper list structure.

It is relatively easy and straightforward to establish a new language in Lisp syntax

  • Establishing a new 'Lisp language'

    • Generic parsing can be done by the Lisp reader

    • It is possible to concentrate on the semantic issues

    • Language checking and error handling should not be forgotten

 

22.6.  An example of coarse grained abstraction
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

In this section we will discuss how to 'process' the course home page document (see Program 22.1), which we discussed earlier in Section 22.4.

Below we will assume that the course home page fragment of Program 22.1 is located on the file named "new-document.lsp".

In Program 22.3 we show how to open, read and close the file (in blue color). The processing of the parsed expression is shown in red color.

1
2
3
4
5
6
7
8
9
10
11
(let* ((port (open-input-file "new-document.lsp"))
       (new-document (read port))
      )

   ; new-document is a reference to the list structure
   ; representation of the new document.

   (process-document! new-document)

   (close-input-port port)
)
Program 22.3    Reading the document as a list structure.

In this material we will not go into any detail of the transformation. In Program 22.4 we limit ourselves to a superficial demo processing, in which we extract and print the keyword of each subform of the course home page form.

The important thing to notice is that it is very easy to come to the point where the semantic processing (as sketched in Program 22.4) can begin. The only preparation is that of Program 22.3.

1
2
3
4
5
6
7
8
9
10
11
12
13
(define (process-document! doc)
  (file-write (transform-document doc) "res.lsp"))

(define (transform-document doc)
  (let ((top-level-forms (document-forms doc)))
   (map
     (lambda (subform) 
       (subform-keyword subform))
     top-level-forms)))


(define document-forms cdr)
(define subform-keyword car)
Program 22.4    A simple demo processing of the document.

In Program 22.5 we see a sample dialog and execution of the abstractions in Program 22.3 and Program 22.4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1> (let* ((port (open-input-file "new-document.lsp"))
          (new-document (read port))
         )

   ; new-document is a reference to the list structure
   ; representation of the new document.

   (process-document new-document)

   (close-input-port port))

name
number-of-lectures
lecture-names
current-lecture
links
do-process
Program 22.5    Execution dialogue.

 

22.7.  References
[Course-plan-examples]Example of the LAML course home page system
http://www.cs.auc.dk/~normark/scheme/examples/course-plan-xml-in-laml/index.html

Generated: Tuesday July 2, 2013, 09:15:56
Theme index -- Keyboard shortcut: 'u'  Previous theme in this lecture -- Keyboard shortcut: 'p'  Next slide in this lecture -- Keyboard shortcut: 'n'