content
stringlengths
86
994k
meta
stringlengths
288
619
Simplify Modifier Simplify Modifier The Simplify modifier allows you to reduce the amount of points in the strokes. The goal of this modifier is reduce points while maintaining the lines shape. Apply the modifier can help to obtain a better performance (more FPS) while animating. Determines how to reduce points in the strokes. Deletes alternated points in the strokes, except the start and end points. Number of times to repeat the procedure. Uses the RDP algorithm (Ramer-Douglas-Peucker algorithm) for points deletion. The algorithm try to obtain a similar line shape with fewer points. Controls the amount of recursively simplifications applied by the algorithm. Recreates the stroke geometry with a predefined length between points. The distance between points on the recreated stroke. Smaller values will require more points to recreate the stroke, while larger values will result in fewer points needed to recreate the Sharp Threshold Preserve corners that have sharper angle than this threshold. Simplifies the strokes by merging points that are closer than a specified distance to each other. Sets the distance threshold for merging points.
{"url":"https://docs.blender.org/manual/en/3.6/grease_pencil/modifiers/generate/simplify.html","timestamp":"2024-11-07T09:13:15Z","content_type":"text/html","content_length":"25266","record_id":"<urn:uuid:724d4901-33d3-4a66-a007-7969c2905b49>","cc-path":"CC-MAIN-2024-46/segments/1730477027987.79/warc/CC-MAIN-20241107083707-20241107113707-00760.warc.gz"}
Wing geometry • tjfsim wrote on last edited by tjfsim Hi folks, I've been following the development of this project with great interest as I really miss being able to fly the 747 Classic in P3D. The JF 747 looks fantastic so far, but I did notice a minor error with the wing shape that I think your modelers have missed. On the real aircraft, the rake of the leading edge changes to a shallower angle after the outboard pylons. This isn't immediately noticeable from most angles, but it is very apparent when looking down the leading edge. People often miss this because from some angles it gets mistaken for wing flex, but if you draw a line along the leading edge of the wing on an official plan drawing, you will indeed see the change in rake angle. This lovely photograph of a 747-400 shows the change of rake very well: If you compare this photograph with one of the development shots of the JF 747, you can see the difference: I hope this won't be taken as a criticism, and perhaps it's already been spotted and fixed, but I thought I would raise it just in case. Keep up the good work, and really looking forward to this aircraft being released, Best wishes, • Thanks for the feedback, we'll investigate further in case any changes can be made at this late stage of the development. • I'm not sure the -400 had the same wing design as the early -100s and -200s. It was certainly a lot more clever, being 20+ years later... • @tb10driver said in Wing geometry: I'm not sure the -400 had the same wing design as the early -100s and -200s. It was certainly a lot more clever, being 20+ years later... The -400 had a slightly extended wingspan and winglets, as well as blended wing root fairings, but it still shared the same basic geometry as the 'Classics'. Externally the wings were identical up to the point where the extensions were added, including the change of wing rake I refer to here. There are lots of photographs of -100 and -200 wings that show the same 'kink', but not quite as effectively as in this photograph. Here is a drawing of the 'Classic' wing in plan view A photograph of a -200 wing:
{"url":"https://community.justflight.com/topic/1125/wing-geometry","timestamp":"2024-11-08T07:58:52Z","content_type":"text/html","content_length":"81946","record_id":"<urn:uuid:d4b737db-a8a8-4674-a179-3bdbf9b4d9a1>","cc-path":"CC-MAIN-2024-46/segments/1730477028032.87/warc/CC-MAIN-20241108070606-20241108100606-00407.warc.gz"}
The Generalized Training Methods,Algorithms And Applications For Competitive Learning Neural Networks Posted on:2018-08-07 Degree:Doctor Type:Dissertation Country:China Candidate:Z Y Xiang Full Text:PDF GTID:1318330542474511 Subject:Computer Science and Technology The amount of data in the Internet is ever growing because the development of global informatization.Since the information volume is expanding faster than the computational pow-ers that humanity commands,the space complexity of machine learning algorithms is becoming more important than before.Meanwhile,the informatization is spreading into industrial areas other than computer sciences,with the development of the concept and technology of Internet of things and ubiquitous computing.There are a great amount of memory critical computation units in these areas that require low space complex algorithms.Online learning is a machine learning setting,where the machine learning model is acquired by a single pass scan of the training dataset,and it is more space efficient than the traditional offline methods,which need to store the entire dataset for training.Besides,there are various applications,such as network intrusion detection,short-term electricity requirement prediction and traffic flow prediction,where the training data arrives continuously and the problem scale is growing accordingly.In these applications,the model constructed by machine learning algorithms need to be updated continuously,and online algorithms are a major solution.Though online learning is space efficient,it is difficult to solve nonlinearity,semi-supervised learning and model parameter selection all at once,because of the single pass learning setting.Moreover,some online learning algorithms is computationally inefficient in updating the ma-chine learning models.However,the competitive learning has no such problems.Unfortu-nately,the competitive learning researches mainly focus on clustering.For other tasks such as regression,semi-supervised classification and dimensionality reduction,the advantages of nonlinear capability and efficient online learning of competitive learning can not be utilized.This thesis focuses on generalization of the competitive learning algorithms to these tasks.The main contributions are listed as follows.1.A kernel density regression framework is proposed to transform competitive learning clustering to regression algorithms.Online algorithms is either parametric or nonparametric.The parametric algorithms take a unified analytical assumption about the dataset,and it is dif-ficult to select the optimal model with best parameters under the online learning setting.The nonparametric algorithms have no such difficulty,however,the majority of them are unable to give smoothed predictions and suffer from compromised generalization abilities.To address the difficulties both in the parametric and nonparametric learning algorithms in online learning,this thesis proposes a nonparametric training method to construct a parametric model.By exploiting the distribution learning abilities of competitive learning neural networks,the joint distribution of explanatory variables and response variables can be modeled,and the regression model be obtained by density regression.Then,the regression function is deducted directly from cluster-ing learning results in this thesis.The complete framework is a feed-forward neural network model,the weights of the hidden layer of which is trained by the competitive learning neural networks.The advantage of this framework is that the local minima of the back propagation is avoided,while at the same time the smoothed prediction advantage of feed-forward neural network is kept.The proposed methods has constant space complexity and O(n) computation complexity.Experiments on six UCI datasets show that the proposed method gives comparable results to the mainstream offline methods.2.The optimal smooth parameter selection of kernel density regression is deducted by max-imum likelihood estimation.There is one smooth parameter in the kernel density regression framework,selection of which is difficult to reach optima in online learning,since the learning is finished in one pass scan.To solve this problem,we propose a maximum likelihood smooth parameter selection framework.Under the online learning setting,the optimal smooth param-eter selection of kernel density regression is solved by two strategies of maximum likelihood estimation.To solve the parameter selection of mixture models,a Bayesian inferences process is employed to transform the maximum likelihood function into solvable form.Then through derivation techniques,equations about the smooth parameter are constructed and solved.Be-sides,a semi-supervised learning process is also proposed to select the smooth parameters with the complete distribution information of the test dataset.The proposed framework selection is the optimal solution under the online learning setting,which is vital for the online kernel density regression,because the online algorithms is finished within one pass scan of data and leaves no room for parameter tuning.Experiments show that the proposed method is comparable to other online and offline algorithms,and it is parameter free.3.An inverse competitive learning paradigm is proposed for semi-supervised extensions of competitive learning neural networks.The original competitive learning neural networks,es-pecially the self-organizing neural networks are inefficient to handle the labeled datasets,which make them difficult to solve the semi-supervised learning problem,where the training dataset contains both labeled and unlabeled samples.To solve this problem,this thesis proposes the inverse competitive learning.Whenever there is a conflicting of labels,the neurons are moved in an inverse direction from the original competitive learning.Then the graph cut algorithm is employed to complete the semi-supervised learning.Moreover,a fast graph cut algorithm based on greedy label propagation is proposed.The main advantage of this framework is the robustness to noise in datasets and the ability to perform online semi-supervised learning under the manifold assumption.The experiments on the intrusion detection dataset show that the pro-posed method is more accurate than the mainstream offline semi-supervised learning methods even that it is online.4.A framework is proposed to transform clustering into dimensionality reduction.Com-petitive learning is used in combine for online dimensionality reduction learning.The online learning and nonlinear dimensionality reduction are always difficult to combine.With competitive learning at the core,this thesis proposes an online nonlinear dimensionality reduction framework.First,a data prototype is acquired by the semi-supervised competitive learning neural networks.Second,two similarity matrixes are fusioned into one by a quadratic programming process.Finally,the neurons are transformed into the lower dimensional space and other samples are embedded by a kernel smoothing procedure.Besides,a data visualization techniques is proposed exploiting the topology learning abilities of competitive learning.The proposed framework has the advantage of high space complexity comparing to existing nonlin-ear algorithms.Experimental results on the intrusion detection dataset show that the proposed method is a more efficient alternative to the offline semi-supervised learning algorithms.5.Short-term traffic flow prediction is implemented based on the ensemble of the proposed kernel density regression.Since the prediction results and weights of the individual online re-gressors are changing constantly with the time passage,the ensemble of such online regressors are always difficult.To address this difficulty,this thesis proposes a weight storing technique to construct a competitive regressor ensemble framework.By construction of weighted learning,the ensemble of kernel density regression is enabled.The proposed framework is a combina-tion of online learning and ensemble learning.Since the ensemble enables the combined use of competitive learning based regression and other algorithms,it is vital for making the proposed regression algorithms applicable in the various industrial and social applications.In the exper-iments,the proposed method is boosted by Adaboost and the results show that the proposed methods outperform mainstream methods such as support vector machine,decision trees and extreme learning machines etc.,and they have the parameter free advantage. Keywords/Search Tags: Online learning, competitive learning, nonparametric regression, semi-supervised learning, semi-supervised dimensionality reduction
{"url":"https://globethesis.com/?t=1318330542474511","timestamp":"2024-11-03T15:16:04Z","content_type":"application/xhtml+xml","content_length":"14305","record_id":"<urn:uuid:470d77cb-1947-42f4-a036-b5bdb23dea25>","cc-path":"CC-MAIN-2024-46/segments/1730477027779.22/warc/CC-MAIN-20241103145859-20241103175859-00201.warc.gz"}
Data Science Interview Questions - ICRONIX % Data Science Interview Questions Q. What do you mean by word Data Science? Ans: Data Science is the extraction of knowledge from large volumes of data that are structured or unstructured, which is a continuation of the field data mining and predictive analytics, It is also known as knowledge discovery and data mining Q. Explain the term botnet? Ans:A botnet is a a type of bot running on an IRC network that has been created with a Trojan. Q. What is Data Visualization? Ans: Data visualization is a common term that describes any effort to help people understand the significance of data by placing it in a visual context. Q. How you can define Data cleaning as a critical part of process? Ans: Cleaning up data to the point where you can work with it is a huge amount of work. If we’re trying to reconcile a lot of sources of data that we don’t control like in this flight, it can take 80% of our time. Q. Point out 7 Ways how Data Scientists use Statistics? 1. Design and interpret experiments to inform product decisions. 2. Build models that predict signal, not noise. 3. Turn big data into the big picture 4. Understand user retention, engagement, conversion, and leads. 5. Give your users what they want. 6. Estimate intelligently. 7. Tell the story with the data. Q. Differentiate between Data modeling and Database design? Ans: Data Modelling – Data modelling (or modelling) in software engineering is the process of creating a data model for an information system by applying formal data modelling techniques. Database Design- Database design is the system of producing a detailed data model of a database. The term database design can be used to describe many different parts of the design of an overall database Q. Describe in brief the data Science Process flowchart? Ans: 1.Data is collected from sensors in the environment. 2. Data is “cleaned” or it can process to produce a data set (typically a data table) usable for processing. 3. Exploratory data analysis and statistical modelling may be performed. 4. A data product is a program such as retailers use to inform new purchases based on purchase history. It may also create data and feed it back into the Q. What do you understand by term hash table collisions? Ans: Hash table (hash map) is a kind of data structure used to implement an associative array, a structure that can map keys to values. Ideally, the hash function will assign each key to a unique bucket, but sometimes it is possible that two keys will generate an identical hash causing both keys to point to the same bucket. It is known as hash collisions. Q. Compare and contrast R and SAS? Ans: SAS is commercial software whereas R is free source and can be downloaded by anyone. SAS is easy to learn and provide easy option for people who already know SQL whereas R is a low level programming language and hence simple procedures takes longer codes. Q. What do you understand by letter ‘R’? Ans: R is a low level language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at BELL. Q. What all things R environment includes? 1. A suite of operators for calculations on arrays, in particular matrices, 2. An effective data handling and storage facility, 3. A large, coherent, integrated collection of intermediate tools for data analysis, an effective data handling and storage facility, 4. Graphical facilities for data analysis and display either on-screen or on hardcopy, and 5. A well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities. What are the applied Machine Learning Process Steps? 1. Problem Definition: Understand and clearly describe the problem that is being solved. 2. Analyse Data: Understand the information available that will be used to develop a model. 3. Prepare Data: Define and expose the structure in the dataset. 4. Evaluate Algorithms: Develop robust test harness and baseline accuracy from which to improve and spot check algorithms. 5. Improve Results: Improve results to develop more accurate models. 6. Present Results: Details the problem and solution so that it can be understood by third parties. Q. Compare Multivariate, Univariate and Bivariate analysis? Ans: MULTIVARIATE: Multivariate analysis focuses on the results of observations of many different variables for a number of objects. UNIVARIATE: Univariate analysis is perhaps the simplest form of statistical analysis. Like other forms of statistics, it can be inferential or descriptive. The key fact is that only one variable is involved. BIVARIATE: Bivariate analysis is one of the simplest forms of quantitative (statistical) analysis. It involves the analysis of two variables (often denoted as X, Y), for the purpose of determining the empirical relationship between them. Q. What is Hypothesis in Machine Learning? Ans: The hypothesis space used by a machine learning system is the set of all hypotheses that might possibly be returned by it. It is typically dened by a hypothesis language, possibly in conjunction with a language bias. Q. Differentiate between Uniform and Skewed Distribution? Ans: UNIFORM DISTRIBUTION: A uniform distribution, sometimes also known as a rectangular distribution, is a distribution that has constant probability. The latter of which simplifies to the expected for. The continuous distribution is implemented as Uniform Distribution SKEWED DISTRIBUTION: In probability theory and statistics, Skewness is a measure of the asymmetry of the probability distribution of a real-valued random variable about its mean. The skewness value can be positive or negative, or even undefined. The qualitative interpretation of the skew is complicated. Q. What do you understand by term Transformation in Data Acquisition? Ans: The transformation process allows you to consolidate, cleanse, and integrate data. We can semantically arrange the data from heterogeneous sources. Q. What do you understand by term Normal Distribution? Ans: It is a function which shows the distribution of many random variables as a symmetrical bell-shaped graph. Q. What is Data Acquisition? Ans: It is the process of measuring an electrical or physical phenomenon such as voltage, current, temperature, pressure, or sound with a computer. A DAQ system comprises of sensors, DAQ measurement hardware, and a computer with programmable software. Q. What is Data Collection? Ans: Data collection is the process of collecting and measuring information on variables of interest, in a proper systematic fashion that enables one to answer stated research questions hypotheses, and revise outcomes. Q. What do you understand by term Use case? Ans: A use case is a methodology used in system analysis to identify, clarify, and organize system requirements. The use case consists of a set of possible sequences of interactions between systems and users in a particular environment and related to a defined particular goal Q. What is Sampling and Sampling Distribution? Ans: SAMPLING: Sampling is the process of choosing units (ex- people, organizations) from a population of interest so that by studying the sample we can fairly generalize our results back to the population from which they were chosen. SAMPLING DISTRIBUTION: The sampling distribution of a statistic is the distribution of that statistic, considered as a random variable, when derived from a random sample of size n. It may be considered as the distribution of the statistic for all possible samples from the same population of a given size. Q. What is Linear Regression? Ans: In statistics, linear regression is an way for modelling the relationship between a scalar dependent variable y and one or more explanatory variables (or independent variable) denoted by X. The case of one explanatory variable is known as simple linear regression .Q. Differentiate between Extrapolation and Interpolation? Ans: Extrapolation is an approximate of a value based on extending a known sequence of values or facts beyond the area that is certainly known. Interpolation is an estimation of a value within two known values in a list of values. Q. How expected value is different from Mean value? Ans: There is no difference. These are two names for the same thing. They are mostly used in different contexts, though if we talk about the expected value of a random variable and the mean of a sample, population or probability distribution. Q. Differentiate between Systematic and Cluster Sampling? Ans: SYSTEMATIC SAMPLING: Systematic sampling is a statistical methology involving the selection of elements from an ordered sampling frame. The most common form of systematic sampling is an equal-probability method. CLUSTER SAMPLING: A cluster sample is a probability sample by which each sampling unit is a collection, or cluster, of elements. Q. What are the advantages of Systematic Sampling? Ans: 1.Easier to perform in the field, especially if a proper frame is not available. 2. Regularly provides more information per unit cost than simple random sampling, in the sense of smaller Q. What do you understand by term Threshold limit value? Ans: The threshold limit value (TLV) of a chemical substance is a level in which it is believed that a worker can be exposed day after day for a working lifetime without affecting his/her health. Q. Differentiate between Validation Set and Test set? Ans: Validation set: It is a set of examples used to tune the parameters of a classifier, for example to choose the number of hidden units in a neural network. Test set: A set of examples used only to assess the performance of a fully specified classifier. Q. How can R and Hadoop be used together? Ans: The most common way to link R and Hadoop is to use HDFS (potentially managed by Hive or Hbase) as the long-term store for all data, and use Map Reduce jobs (potentially submitted from Hive, Pig, or Oozie) to encode, enrich, and sample data sets from HDFS into R. Data analysts can then perform complex modelling exercises on a subset of prepared data in R. Q. What do you understand by term RIMPALA? Ans: RImpala-package contains the R functions required to connect, execute queries and retrieve back results from Impala. It uses the rJava package to create a JDBC connection to any of the impala servers running on a Hadoop Cluster. Q. What is Collaborative Filtering? Ans: Collaborative filtering (CF) is a method used by some recommender systems. It consists of two senses, a narrow one and a more general one. In general, collaborative filtering is the process of filtering for information or patterns using techniques involving collaboration among multiple agents, viewpoints, data sources. Q. What are the challenges of Collaborative Filtering? 1. Scalability 2. Data sparsity 3. Synonyms 4. Grey sheep Data sparsity 5. Shilling attacks 6. Diversity and the Long Tail Q. What do you understand by Big data? Ans: Big data is a buzzword, or catch-phrase, which describe a massive volume of both structured and unstructured data that is so large which is difficult to process using traditional database and software techniques. Q. What do you understand by Matrix factorization? Ans: Matrix factorization is simply a mathematical tool for playing around with matrices, and is therefore applicable in many scenarios by which one would find out something hidden under the data. Q. What do you understand by term Singular Value Decomposition? Ans: In linear algebra, the singular value decomposition (SVD) is a factorization of a real or complex matrix. It has many useful applications in signal processing and statistics. Q. What do you mean by Recommender systems? Ans: Recommender systems or recommendation systems (sometimes replacing “system” with a synonym such as platform or engine) are a subclass of information filtering system that seek to predict the ‘rating’ or ‘preference’ that a user would give to an item. Q. What are the applications of Recommender Systems? Ans: Recommender systems have become extremely common in recent years, and are applied in a variety of applications. The most popular ones are probably movies, music, news, books, research articles, search queries, social tags, and products in general. Q. What are the two ways of Recommender System? Ans: Recommender systems typically produce a list of recommendations in one of two ways: Through collaborative or content-based filtering. Collaborative filtering approaches building a model from a user’s past behaviour (items previously purchased or selected and/or numerical ratings given to those items) as well as similar decisions made by other users. This model is then used to predict items (or ratings for items) that the user may have an interest in. Content-based filtering approaches utilize a series of discrete characteristics of an item in order to recommend additional items with similar properties. Q. What are the factors to find the most accurate recommendation algorithms? 1. Diversity 2. Recommender Persistence 3. Privacy 4. User Demographics 5. Robustness 6. Serendipity 7. Trust 8. Labelling Q. What is K-Nearest Neighbour? Ans: k-NN is a type of instance-based learning, or lazy learning, where the function is only approximated locally and all computation is deferred until classification. The k-NN algorithm is among the simplest of all machine learning algorithms. Q. What is Horizontal Slicing? Ans: In horizontal slicing, projects are broken up roughly along architectural lines. That is there would be one team for UI, one team for business logic and services (SOA), and another team for Q. What are the advantages of vertical slicing? Ans: The advantage of slicing vertically is you are more efficient. You don’t have the overhead, and effort that comes from trying to coordinate activities across multiple teams. No need to negotiate for resources. You’re all on the same team. Q. What is null hypothesis? Ans: In inferential statistics the null hypothesis usually refers to a general statement or default position that there is no relationship between two measured phenomena, or no difference among Q. What is Statistical hypothesis? Ans: In statistical hypothesis testing, the alternative hypothesis (or maintained hypothesis or research hypothesis) and the null hypothesis are the two rival hypotheses which are compared by a statistical hypothesis test. Q. What is performance measure? Ans: Performance measurement is the method of collecting, analyzing and/or reporting information regarding the performance of an individual, group, organization, system or component. Q. What is the use of tree command? Ans: This command is used to list contents of directories in a tree-like format. Q. What is the use of uniq command? Ans: This command is used to report or omit repeated lines. Q. Which command is used translate or delete characters? Ans: tr command is used translate or delete characters. Q. What is the use of tapkee command? Ans: This command is used to reduce dimensionality of a data set using various algorithms. Q. Which command is used to sort the lines of text files? Ans: sort command is used to sort the lines of text files. Data Science Interview Questions and Answers in R Programming Q. How can you merge two data frames in R language? Ans: Data frames in R language can be merged manually using cbind () functions or by using the merge () function on common rows or columns. Q. Explain about data import in R language Ans: R Commander is used to import data in R language. To start the R commander GUI, the user must type in the command Rcmdr into the console. There are 3 different ways in which data can be imported in R language- • Users can select the data set in the dialog box or enter the name of the data set (if they know). • Data can also be entered directly using the editor of R Commander via Data->New Data Set. However, this works well when the data set is not too large. • Data can also be imported from a URL or from a plain text file (ASCII), from any other statistical package or from the clipboard. Q. Two vectors X and Y are defined as follows – X <- c(3, 2, 4) and Y <- c(1, 2). What will be output of vector Z that is defined as Z <- X*Y. Ans: In R language when the vectors have different lengths, the multiplication begins with the smaller vector and continues till all the elements in the larger vector have been multiplied. The output of the above code will be – Z <- (3, 4, 4) Q. How missing values and impossible values are represented in R language? Ans: NaN (Not a Number) is used to represent impossible values whereas NA (Not Available) is used to represent missing values. The best way to answer this question would be to mention that deleting missing values is not a good idea because the probable cause for missing value could be some problem with data collection or programming or the query. It is good to find the root cause of the missing values and then take necessary steps handle them. Q. R language has several packages for solving a particular problem. How do you make a decision on which one is the best to use? Ans: CRAN package ecosystem has more than 6000 packages. The best way for beginners to answer this question is to mention that they would look for a package that follows good software development principles. The next thing would be to look for user reviews and find out if other data scientists or analysts have been able to solve a similar problem. Data Science Interview questions Data Science Interview Questions and Answers Q. Which function in R language is used to find out whether the means of 2 groups are equal to each other or not? Ans: t.tests () Q. What is the best way to communicate the results of data analysis using R language? Ans: The best possible way to do this is combine the data, code and analysis results in a single document using knitr for reproducible research. This helps others to verify the findings, add to them and engage in discussions. Reproducible research makes it easy to redo the experiments by inserting new data and applying it to a different problem. Q. How many data structures does R language have? Ans: R language has Homogeneous and Heterogeneous data structures. Homogeneous data structures have same type of objects – Vector, Matrix ad Array. Heterogeneous data structures have different type of objects – Data frames and lists. Q. What is the value of f (2) for the following R code? Ans: b <- 4 f <- function (a) { b <- 3 b^3 + g (a) } g <- function (a) { a*b } The answer to the above code snippet is 35. The value of “a” passed to the function is 2 and the value for “b” defined in the function f (a) is 3. So the output would be 3^3 + g (2). The function g is defined in the global environment and it takes the value of b as 4(due to lexical scoping in R) not 3 returning a value 2*4= 8 to the function f. The result will be 3^3+8= 35. Q. What is the process to create a table in R language without using external files? Ans: MyTable= data.frame () edit (MyTable) The above code will open an Excel Spreadsheet for entering data into MyTable. Q. Explain about the significance of transpose in R language Ans: Transpose t () is the easiest method for reshaping the data before analysis. Q. What are with () and BY () functions used for? Ans: With () function is used to apply an expression for a given dataset and BY () function is used for applying a function each level of factors. Q. dplyr package is used to speed up data frame management code. Which package can be integrated with dplyr for large fast tables? Ans: data.table Q. In base graphics system, which function is used to add elements to a plot? Ans: boxplot () or text () Q. What are the different type of sorting algorithms available in R language? Ans: Bucket Sort Selection Sort Quick Sort Bubble Sort Merge Sort Q. What is the command used to store R objects in a file? Ans: save (x, file=”x.Rdata”) Q. What is the best way to use Hadoop and R together for analysis? Ans: HDFS can be used for storing the data for long-term. MapReduce jobs submitted from either Oozie, Pig or Hive can be used to encode, improve and sample the data sets from HDFS into R. This helps to leverage complex analysis tasks on the subset of data prepared in R. Q. What will be the output of log (-5.8) when executed on R console? Ans: Executing the above on R console will display a warning sign that NaN (Not a Number) will be produced because it is not possible to take the log of negative number. Q. How is a Data object represented internally in R language? Ans: unclass (as.Date (“2016-10-05″)) Q. What will be the output of the below code –printmessage <- function (a) { if (is.na (a)) print (“a is a missing value!”) else if (a < 0) print (“a is less than zero”) else print (“a is greater than or equal to zero”) invisible (a) } printmessage (NA) The output for the above R programming code will be “a is a missing value.” The function is.na () is used to check if the input passed is a missing value. Q. Which package in R supports the exploratory analysis of genomic data? Ans: adegenet Q. What is the difference between data frame and a matrix in R? Ans: Data frame can contain heterogeneous inputs while a matrix cannot. In matrix only similar data types can be stored whereas in a data frame there can be different data types like characters, integers or other data frames. Q. How can you add datasets in R? Ans: rbind () function can be used add datasets in R language provided the columns in the datasets should be same. Q. What are factor variable in R language? Ans: Factor variables are categorical variables that hold either string or numeric values. Factor variables are used in various types of graphics and particularly for statistical modelling where the correct number of degrees of freedom is assigned to them. Q. What is the memory limit in R? Ans: 8TB is the memory limit for 64-bit system memory and 3GB is the limit for 32-bit system memory. Q. What are the data types in R on which binary operators can be applied? Ans: Scalars, Matrices ad Vectors. Q. How do you create log linear models in R language? Ans: Using the loglm () function Q. What will be the class of the resulting vector if you concatenate a number and NA? Ans: number Q. What is meant by K-nearest neighbour? Ans: K-Nearest Neighbour is one of the simplest machine learning classification algorithms that is a subset of supervised learning based on lazy learning. In this algorithm the function is approximated locally and any computations are deferred until classification. Q. What will be the class of the resulting vector if you concatenate a number and a character? Ans: character Q. If you want to know all the values in c (1, 3, 5, 7, 10) that are not in c (1, 5, 10, 12, 14). Which in-built function in R can be used to do this? Also, how this can be achieved without using the in-built function. Ans: Using in-built function – setdiff(c (1, 3, 5, 7, 10), c (1, 5, 10, 11, 13)) Without using in-built function – c (1, 3, 5, 7, 10) <- mean(x, na.rm = TRUE); x} Q. What happens if the application object is not able to handle an event? Ans: The event is dispatched to the delegate for processing. Q. Differentiate between lapply and sapply. Ans: If the programmers want the output to be a data frame or a vector, then sapply function is used whereas if a programmer wants the output to be a list then lapply is used. There one more function known as vapply which is preferred over sapply as vapply allows the programmer to specific the output type. The disadvantage of using vapply is that it is difficult to be implemented and more Q. Differentiate between seq (6) and seq_along (6) Ans: Seq_along(6) will produce a vector with length 6 whereas seq(6) will produce a sequential vector from 1 to 6 c( (1,2,3,4,5,6)). Q. How will you read a .csv file in R language? Ans: read.csv () function is used to read a .csv file in R language. Below is a simple example – filcontent <-read.csv (sample.csv) print (filecontent) Q. How do you write R commands? Ans: The line of code in R language should begin with a hash symbol (#). Q. How can you verify if a given object “X” is a matric data object? Ans: If the function call is.matrix(X ) returns TRUE then X can be termed as a matrix data object. Q. What do you understand by element recycling in R? Ans: If two vectors with different lengths perform an operation –the elements of the shorter vector will be re-used to complete the operation. This is referred to as element recycling. Example – Vector A <-c(1,2,0,4) and Vector B<-(3,6) then the result of A*B will be ( 3,12,0,24). Here 3 and 6 of vector B are repeated when computing the result. Q. How can you verify if a given object “X” is a matrix data object? Ans: If the function call is.matrix(X) returns true then X can be considered as a matrix data object otheriwse not. Q. How will you measure the probability of a binary response variable in R language? Ans: Logistic regression can be used for this and the function glm () in R language provides this functionality. Q. What is the use of sample and subset functions in R programming language? Ans: Sample () function can be used to select a random sample of size ‘n’ from a huge dataset. Subset () function is used to select variables and observations from a given dataset. Q. There is a function fn(a, b, c, d, e) a + b * c – d / e. Write the code to call fn on the vector c(1,2,3,4,5) such that the output is same as fn(1,2,3,4,5). Ans: do.call (fn, as.list(c (1, 2, 3, 4, 5))) Q. How can you resample statistical tests in R language? Ans: Coin package in R provides various options for re-randomization and permutations based on statistical tests. When test assumptions cannot be met then this package serves as the best alternative to classical methods as it does not assume random sampling from well-defined populations. Q. What is the purpose of using Next statement in R language? Ans: If a developer wants to skip the current iteration of a loop in the code without terminating it then they can use the next statement. Whenever the R parser comes across the next statement in the code, it skips evaluation of the loop further and jumps to the next iteration of the loop. Q. How will you create scatterplot matrices in R language? Ans: A matrix of scatterplots can be produced using pairs. Pairs function takes various parameters like formula, data, subset, labels, etc. The two key parameters required to build a scatterplot matrix are – • formula- A formula basically like ~a+b+c . Each term gives a separate variable in the pairs plots where the terms should be numerical vectors. It basically represents the series of variables used in pairs. • data- It basically represents the dataset from which the variables have to be taken for building a scatterplot. Q. How will you check if an element 25 is present in a vector? Ans: There are various ways to do this- 1. It can be done using the match () function- match () function returns the first appearance of a particular element. 2. The other is to use %in% which returns a Boolean value either true or false. • element () function also returns a Boolean value either true or false based on whether it is present in a vector or not. Q. What is the difference between library() and require() functions in R language? Ans: There is no real difference between the two if the packages are not being loaded inside the function. require () function is usually used inside function and throws a warning whenever a particular package is not found. On the flip side, library () function gives an error message if the desired package cannot be loaded. Q. What are the rules to define a variable name in R programming language? Ans: A variable name in R programming language can contain numeric and alphabets along with special characters like dot (.) and underline (-). Variable names in R language can begin with an alphabet or the dot symbol. However, if the variable name begins with a dot symbol it should not be a followed by a numeric digit. Q. What do you understand by a workspace in R programming language? Ans: The current R working environment of a user that has user defined objects like lists, vectors, etc. is referred to as Workspace in R language. Q. Which function helps you perform sorting in R language? Ans: Order () Q. How will you list all the data sets available in all R packages? Ans: Using the below line of code- data(package = .packages(all.available = TRUE)) Q. Which function is used to create a histogram visualisation in R programming language? Ans: Hist() Q. Write the syntax to set the path for current working directory in R environment. Ans: Setwd(“dir_path”) Q. How will you drop variables using indices in a data frame? Ans: Let’s take a dataframe df<-data.frame(v1=c(1:5),v2=c(2:6),v3=c(3:7),v4=c(4:8)) df ## v1 v2 v3 v4 ## 1 1 2 3 4 ## 2 2 3 4 5 ## 3 3 4 5 6 ## 4 4 5 6 7 ## 5 5 6 7 8 Suppose we want to drop variables v2 & v3 , the variables v2 and v3 can be dropped using negative indicies as follows- df1<-df df1 ## v1 v4 ## 1 1 4 ## 2 2 5 ## 3 3 6 ## 4 4 7 ## 5 5 8 Q. What will be the output of runif (7)? Ans: It will generate 7 randowm numbers between 0 and 1. Q. What is the difference between rnorm and runif functions ? Ans: rnorm function generates “n” normal random numbers based on the mean and standard deviation arguments passed to the function.Syntax of rnorm function –rnorm(n, mean = , sd = ) runif function generates “n” unform random numbers in the interval of minimum and maximum values passed to the function.Syntax of runif function –runif(n, min = , max = ) Q. What will be the output on executing the following R programming code –mat<-matrix(rep(c(TRUE,FALSE),8),nrow=4) sum(mat) 8Q.How will you combine multiple different string like “Data”, “Science”, “in” ,“R”, “Programming” as a single string “Data_Science_in_R_Programmming” ? Ans: paste(“Data”, “Science”, “in” ,“R”, “Programming”,sep=”_”) Q. Write a function to extract the first name from the string “Mr. Tom White”. Ans: substr (“Mr. Tom White”,start=5, stop=7) Q. Can you tell if the equation given below is linear or not ? Emp_sal= 2000+2.5(emp_age)^2 Ans: Yes it is a linear equation as the coefficients are linear. Q. What will be the output of the following R programming code ? var2<- c(“I”,”Love,”DeZyre”)var2 Ans: It will give an error. Q. What will be the output of the following R programming code?x<-5if(x%%2==0) print(“X is an even number”)else print(“X is an odd number”) Ans: Executing the above code will result in an error as shown below – ## Error: :4:1: unexpected ‘else’ ## 3: print(“X is an even number”) ## 4: else ## ^ R programming language does not know if the else related to the first ‘if’ or not as the first if() is a complete command on its own. Q. What is R Base package? Ans: R Base package is the package that is loaded by default whenever R programming environent is loaded .R base package provides basic fucntionalites in R environment like arithmetic calcualtions, Q. How will you merge two dataframes in R programming language? Ans: Merge () function is used to combine two dataframes and it identifies common rows or columns between the 2 dataframes. Merge () function basically finds the intersection between two different sets of data. Merge () function in R language takes a long list of arguments as follows – Syntax for using Merge function in R language – merge (x, y, by.x, by.y, all.x or all.y or all ) • X represents the first dataframe. • Y represents the second dataframe. • X- Variable name in dataframe X that is common in Y. • Y- Variable name in dataframe Y that is common in X. • x – It is a logical value that specifies the type of merge. all.X should be set to true, if we want all the observations from dataframe X . This results in Left Join. • y – It is a logical value that specifies the type of merge. all.y should be set to true , if we want all the observations from dataframe Y . This results in Right Join. • all – The default value for this is set to FALSE which means that only matching rows are returned resulting in Inner join. This should be set to true if you want all the observations from dataframe X and Y resulting in Outer join. Q. Write the R programming code for an array of words so that the output is displayed in decreasing frequency order. Ans: R Programming Code to display output in decreasing frequency order – tt <- sort(table(c(“a”, “b”, “a”, “a”, “b”, “c”, “a1”, “a1”, “a1”)), dec=T) depth <- 3 tt Output – 1) a a1 b 2) 3 3 2 Q. How to check the frequency distribution of a categorical variable? Ans: The frequency distribution of a categorical variable can be checked using the table function in R language. Table () function calculates the count of each categories of a categorical variable. gender=factor(c(“M”,”F”,”M”,”F”,”F”,”F”)) table(sex)Output of the above R Code –Gender F M 4 2 Programmers can also calculate the % of values for each categorical group by storing the output in a dataframe and applying the column percent function as shown below – t = data.frame(table(gender)) t$percent= round(t$Freq / sum(t$Freq)*100,2) Gender Frequency Percent F 4 66.67 M 2 33.33 Q. What is the procedure to check the cumulative frequency distribution of any categorical variable? Ans: The cumulative frequency distribution of a categorical variable can be checked using the cumsum () function in R language.Example –gender = factor(c(“f”,”m”,”m”,”f”,”m”,”f”)) y = table(gender) cumsum(y)Output of the above R code-Cumsum(y) f m 3 3 Q. What will be the result of multiplying two vectors in R having different lengths? Ans: The multiplication of the two vectors will be performed and the output will be displayed with a warning message like – “Longer object length is not a multiple of shorter object length.” Suppose there is a vector a<-c (1, 2, 3) and vector b <- (2, 3) then the multiplication of the vectors a*b will give the resultant as 2 6 6 with the warning message. The multiplication is performed in a sequential manner but since the length is not same, the first element of the smaller vector b will be multiplied with the last element of the larger vector a. contact for more on Data Science Online Data Science interview questions Tags Data Science interview questions and answers, Data Science online training, Data Science interview questions, Data Science training online, Data Science training, Data Science training institute, latest Data Science interview questions, best Data Science interview questions 2019, top 100 Data Science interview questions, sample Data Science interview questions, Data Science interview questions technical, best Data Science interview tips, best Data Science interview basics, Data Science Interview techniques, Data Science Interview Tips. For https://icronix.com/data-science/ Leave a Comment
{"url":"https://icronix.com/data-science-interview-questions/","timestamp":"2024-11-07T21:48:16Z","content_type":"text/html","content_length":"132387","record_id":"<urn:uuid:c9527db7-3122-4480-b039-572ee54b798d>","cc-path":"CC-MAIN-2024-46/segments/1730477028017.48/warc/CC-MAIN-20241107212632-20241108002632-00744.warc.gz"}
Reformats output from a boral fit tidyboral {boral} R Documentation Reformats output from a boral fit Reformats estimates from a fitted boral model to be in a slightly tidier format, meaning everything is presented as a long data frame. object An object of class "boral". Formatting the output into a long data frame maybe useful if one wishes to take the estimated parameters (namely the posterior mean/median/interquartile range/standard deviation, and the lower and upper limits of the HPD intervals), and subsequently wrangle them for presentation purposes using packages from the tidyverse package e.g., Wickham and Henry (2018), construct plots from them using the ggplot2 package (Wickham, 2016), and so on. It is important to note that this function is solely designed to format output from a fitted boral model relating to the estimated parameters. It does not an additional information like model call and MCMC samples. Please do NOT erase the original fitted model in place of this! A a list containing the following components, where applicable: A long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the latent variable coefficients. This also includes the lv.coefs response-specific intercepts, and dispersion parameters if appropriate. lv A long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the latent variables. A long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions for the parameters characterizing the correlation structure lv.covparams of the latent variables when they are assumed to be non-independent across rows. A long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the response-specific coefficients relating to the X.coefs covariate matrix. A long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the coefficients and standard deviation relating to the traits.coefs species traits; please see about.traits. A long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the common cutoffs for ordinal responses (please see the cutoffs not-so-brief tangent on distributions above). A long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the standard deviation for the random intercept normal ordinal.sigma distribution corresponding to the ordinal response columns. A long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the common power parameter for tweedie responses (please powerparam see the not-so-brief tangent on distributions above). A list with each element being a long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the row effects. The row.coefs length of the list is equal to the number of row effects included i.e., object$ncol(row.ids). A list with each element being a long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the standard deviation for row.sigma the row random effects normal distribution. The length of the list is equal to the number of row effects included i.e., object$ncol(row.ids). A list with each element being a long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the response-specific ranef.coefs random intercepts. The length of the list is equal to the number of row effects included i.e., ncol(object$ranef.ids). A list with each element being a long format data frame containing the mean/median/standard deviation/interquartile range of the posterior distributions of the standard deviation for ranef.sigma the response-specific random intercept normal distributions. The length of the list is equal to the number of row effects included i.e., object$ncol(ranef.ids). ssvs.indcoefs A long format data frame containing posterior probabilities and associated standard deviation for individual SSVS of coefficients in the covariate matrix. ssvs.gpcoefs A long format data frame containing posterior probabilities and associated standard deviation for group SSVS of coefficients in the covariate matrix. ssvs.traitscoefs A long format data frame containing posterior probabilities and associated standard deviation for individual SSVS of coefficients relating to species traits. hpdintervals A list containing long format data frames corresponding to the lower and upper bounds of highest posterior density (HPD) intervals for all the parameters indicated above. Please see get.hpdintervals for more details. • This function is solely designed to format output from a fitted boral model relating to the estimated parameters. It does not an additional information like model call and MCMC samples. Please do NOT erase the original fitted model in place of this! Francis K.C. Hui [aut, cre], Wade Blanchard [aut] Maintainer: Francis K.C. Hui <fhui28@gmail.com> • Wickham, H. (2016). ggplot2: elegant graphics for data analysis. Springer. • Wickham, H., & Henry, L. (2017). Tidyr: Easily tidy data with ‘spread ()’ and ‘gather ()’ functions. See Also boral for the fitting function on which tidyboral can be applied. ## Not run: ## NOTE: The values below MUST NOT be used in a real application; ## they are only used here to make the examples run quick!!! example_mcmc_control <- list(n.burnin = 10, n.iteration = 100, n.thin = 1) testpath <- file.path(tempdir(), "jagsboralmodel.txt") library(mvabund) ## Load a dataset from the mvabund package y <- spider$abun X <- spider$x spiderfit_nb <- boral(y, family = "negative.binomial", lv.control = list(num.lv = 2), row.eff = "fixed", mcmc.control = example_mcmc_control, model.name = testpath) spiderfit_nb_tidy <- tidyboral(spiderfit_nb) spiderfit_nb <- boral(y, X = X, family = "negative.binomial", ranef.ids = data.frame(region = rep(1:7,each=4)), mcmc.control = example_mcmc_control, model.name = testpath) spiderfit_nb_tidy <- tidyboral(spiderfit_nb) ## End(Not run) version 2.0.2
{"url":"https://search.r-project.org/CRAN/refmans/boral/html/tidyboral.html","timestamp":"2024-11-12T23:51:34Z","content_type":"text/html","content_length":"9635","record_id":"<urn:uuid:690b1533-1e7d-4a6c-af9f-f0fb6cb9349b>","cc-path":"CC-MAIN-2024-46/segments/1730477028290.49/warc/CC-MAIN-20241112212600-20241113002600-00287.warc.gz"}
Theory Weak_Late_Bisim Title: The pi-calculus Author/Maintainer: Jesper Bengtson (jebe.dk), 2012 theory Weak_Late_Bisim imports Weak_Late_Sim Strong_Late_Bisim lemma monoAux: "A ⊆ B ⟹ P ↝⇧^^<A> Q ⟶ P ↝⇧^^<B> Q" by(auto intro: Weak_Late_Sim.monotonic) coinductive_set weakBisim :: "(pi × pi) set" step: "⟦P ↝⇧^^<weakBisim> Q; (Q, P) ∈ weakBisim⟧ ⟹ (P, Q) ∈ weakBisim" monos monoAux "weakBisimJudge" (infixr ‹≈› 65) where "P ≈ Q ≡ (P, Q) ∈ weakBisim" lemma weakBisimCoinductAux[case_names weakBisim, case_conclusion weakBisim step, consumes 1]: assumes p: "(P, Q) ∈ X" and step: "⋀P Q. (P, Q) ∈ X ⟹ P ↝⇧^^<(X ∪ weakBisim)> Q ∧ ((Q, P) ∈ X ∨ Q ≈ P)" shows "P ≈ Q" proof - have aux: "X ∪ weakBisim = {(P, Q). (P, Q) ∈ X ∨ P ≈ Q}" by blast from p show ?thesis by(coinduct, force dest: step simp add: aux) lemma weakBisimCoinduct[consumes 1, case_names cSim cSym]: fixes P :: pi and Q :: pi assumes "(P, Q) ∈ X" and "⋀P Q. (P, Q) ∈ X ⟹ P ↝⇧^^<(X ∪ weakBisim)> Q" and "⋀P Q. (P, Q) ∈ X ⟹ (Q, P) ∈ X" shows "P ≈ Q" using assms by(coinduct rule: weakBisimCoinductAux) auto lemma weak_coinduct[case_names weakBisim, case_conclusion weakBisim step, consumes 1]: assumes p: "(P, Q) ∈ X" and step: "⋀P Q. (P, Q) ∈ X ⟹ P ↝⇧^^<X> Q ∧ (Q, P) ∈ X" shows "P ≈ Q" using p proof(coinduct rule: weakBisimCoinductAux) case (weakBisim P Q) from step[OF this] show ?case using Weak_Late_Sim.monotonic by blast lemma weakBisimWeakCoinduct[consumes 1, case_names cSim cSym]: fixes P :: pi and Q :: pi assumes "(P, Q) ∈ X" and "⋀P Q. (P, Q) ∈ X ⟹ P ↝⇧^^<X> Q" and "⋀P Q. (P, Q) ∈ X ⟹ (Q, P) ∈ X" shows "P ≈ Q" using assms by(coinduct rule: weak_coinduct) auto lemma monotonic: "mono(λp x1 x2. ∃P Q. x1 = P ∧ x2 = Q ∧ P ↝⇧^^<{(xa, x). p xa x}> Q ∧ Q ↝⇧^^<{(xa, x). p xa x}> P)" by(auto intro: monoI Weak_Late_Sim.monotonic) lemma unfoldE: fixes P :: pi and Q :: pi assumes "P ≈ Q" shows "P ↝⇧^^<weakBisim> Q" and "Q ≈ P" using assms by(auto intro: weakBisim.cases) lemma unfoldI: fixes P :: pi and Q :: pi assumes "P ↝⇧^^<weakBisim> Q" and "Q ≈ P" shows "P ≈ Q" using assms by(auto intro: weakBisim.cases) lemma eqvt: shows "eqvt weakBisim" proof(auto simp add: eqvt_def) let ?X = "{x. ∃P Q (perm::name prm). P ≈ Q ∧ x = (perm ∙ P, perm ∙ Q)}" fix P Q fix perm::"name prm" assume PBiSimQ: "P ≈ Q" hence "(perm ∙ P, perm ∙ Q) ∈ ?X" by blast moreover have "⋀P Q perm::name prm. ⟦P ↝⇧^^<weakBisim> Q⟧ ⟹ (perm ∙ P) ↝⇧^^<?X> (perm ∙ Q)" proof - fix P Q fix perm::"name prm" assume "P ↝⇧^^<weakBisim> Q" moreover have "weakBisim ⊆ ?X" fix P Q assume "P ≈ Q" moreover have "P = ([]::name prm) ∙ P" and "Q = ([]::name prm) ∙ Q" by auto ultimately show "∃P' Q'. P' ≈ Q' ∧ (∃(perm::name prm). P = perm ∙ P' ∧ Q = perm ∙ Q')" by blast moreover have "eqvt ?X" proof(auto simp add: eqvt_def) fix P Q fix perm1::"name prm" fix perm2::"name prm" assume "P ≈ Q" moreover have "perm1 ∙ perm2 ∙ P = (perm1 @ perm2) ∙ P" by(simp add: pt2[OF pt_name_inst]) moreover have "perm1 ∙ perm2 ∙ Q = (perm1 @ perm2) ∙ Q" by(simp add: pt2[OF pt_name_inst]) ultimately show "∃P' Q'. P' ≈ Q' ∧ (∃(perm::name prm). perm1 ∙ perm2 ∙ P = perm ∙ P' ∧ perm1 ∙ perm2 ∙ Q = perm ∙ Q')" by blast ultimately show "(perm ∙ P) ↝⇧^^<?X> (perm ∙ Q)" by(rule Weak_Late_Sim.eqvtI) ultimately show "(perm ∙ P) ≈ (perm ∙ Q)" by(coinduct rule: weak_coinduct, blast dest: unfoldE) lemma eqvtI: fixes P :: pi and Q :: pi and perm :: "name prm" assumes "P ≈ Q" shows "(perm ∙ P) ≈ (perm ∙ Q)" using assms by(rule eqvtRelI[OF eqvt]) lemma weakBisimEqvt[simp]: shows "eqvt weakBisim" by(auto simp add: eqvt_def eqvtI) lemma strongBisimWeakBisim: fixes P :: pi and Q :: pi assumes PSimQ: "P ∼ Q" shows "P ≈ Q" proof - have "⋀P Q. P ↝[bisim] Q ⟹ P ↝⇧^^<(bisim ∪ weakBisim)> Q" proof - fix P Q assume "P ↝[bisim] Q" hence "P ↝⇧^^<bisim> Q" by(rule strongSimWeakSim) thus "P ↝⇧^^<(bisim ∪ weakBisim)> Q" by(blast intro: Weak_Late_Sim.monotonic) with PSimQ show ?thesis by(coinduct rule: weakBisimCoinductAux, force dest: Strong_Late_Bisim.bisimE symmetric) lemma reflexive: fixes P :: pi shows "P ≈ P" proof - have "(P, P) ∈ Id" by simp then show ?thesis proof (coinduct rule: weak_coinduct) case (weakBisim P Q) have "(P, Q) ∈ Id" by fact thus ?case by(auto intro: Weak_Late_Sim.reflexive) lemma symmetric: fixes P :: pi and Q :: pi assumes "P ≈ Q" shows "Q ≈ P" using assms by(auto dest: unfoldE intro: unfoldI) lemma transitive: fixes P :: pi and Q :: pi and R :: pi assumes PBiSimQ: "P ≈ Q" and QBiSimR: "Q ≈ R" shows "P ≈ R" proof - let ?X = "weakBisim O weakBisim" from assms have "(P, R) ∈ ?X" by blast moreover have "⋀P Q R. ⟦Q ↝⇧^^<weakBisim> R; P ≈ Q⟧ ⟹ P ↝⇧^^<(?X ∪ weakBisim)> R" proof - fix P Q R assume PBiSimQ: "P ≈ Q" assume "Q ↝⇧^^<weakBisim> R" moreover have "eqvt weakBisim" by(rule eqvt) moreover from eqvt have "eqvt (?X ∪ weakBisim)" by(auto simp add: eqvtTrans) moreover have "weakBisim O weakBisim ⊆ ?X ∪ weakBisim" by auto moreover have "⋀P Q. P ≈ Q ⟹ P ↝⇧^^<weakBisim> Q" by(rule unfoldE) ultimately show "P ↝⇧^^<(?X ∪ weakBisim)> R" using PBiSimQ by(rule Weak_Late_Sim.transitive) ultimately show ?thesis apply(coinduct rule: weakBisimCoinduct, auto) by(blast dest: unfoldE symmetric)+ lemma transitive_coinduct_weak[case_names WeakBisimEarly, case_conclusion WeakBisimEarly step, consumes 2]: assumes p: "(P, Q) ∈ X" and Eqvt: "eqvt X" and step: "⋀P Q. (P, Q) ∈ X ⟹ P ↝⇧^^<(bisim O X O bisim)> Q ∧ (Q, P) ∈ X" shows "P ≈ Q" proof - let ?X = "bisim O X O bisim" have Sim: "⋀P P' Q' Q. ⟦P ∼ P'; P'↝⇧^^<?X> Q'; Q' ↝[bisim] Q⟧ ⟹ P ↝⇧^^<?X> Q" proof - fix P P' Q' Q assume PBisimP': "P ∼ P'" assume P'SimQ': "P' ↝⇧^^<?X> Q'" assume Q'SimQ: "Q' ↝[bisim] Q" show "P ↝⇧^^<?X> Q" proof - have "P' ↝⇧^^<?X> Q" proof - have "?X O bisim ⊆ ?X" by(blast intro: Strong_Late_Bisim.transitive) moreover from Strong_Late_Bisim.bisimEqvt Eqvt have "eqvt ?X" by blast ultimately show ?thesis using P'SimQ' Q'SimQ by(blast intro: strongAppend) moreover have "eqvt bisim" by(rule Strong_Late_Bisim.bisimEqvt) moreover from Strong_Late_Bisim.bisimEqvt Eqvt have "eqvt ?X" by blast moreover have "bisim O ?X ⊆ ?X" by(blast intro: Strong_Late_Bisim.transitive) moreover have "⋀P Q. P ∼ Q ⟹ P ↝⇧^^<bisim> Q" by(blast dest: Strong_Late_Bisim.bisimE strongSimWeakSim) ultimately show ?thesis using PBisimP' by(rule Weak_Late_Sim.transitive) from p have "(P, Q) ∈ ?X" by(blast intro: Strong_Late_Bisim.reflexive) moreover from step Sim have "⋀P Q. (P, Q) ∈ ?X ⟹ P ↝⇧^^<?X> Q ∧ (Q, P) ∈ ?X" by(blast dest: Strong_Late_Bisim.bisimE Strong_Late_Bisim.symmetric) ultimately show ?thesis by(rule weak_coinduct) lemma weakBisimTransitiveCoinduct[case_names cSim cSym, consumes 2]: assumes p: "(P, Q) ∈ X" and Eqvt: "eqvt X" and rSim: "⋀P Q. (P, Q) ∈ X ⟹ P ↝⇧^^<(bisim O X O bisim)> Q" and rSym: "⋀P Q. (P, Q) ∈ X ⟹ (Q, P) ∈ X" shows "P ≈ Q" using assms by(coinduct rule: transitive_coinduct_weak) auto
{"url":"https://devel.isa-afp.org/browser_info/current/AFP/Pi_Calculus/Weak_Late_Bisim.html","timestamp":"2024-11-12T22:03:48Z","content_type":"application/xhtml+xml","content_length":"158030","record_id":"<urn:uuid:85f18aab-4335-494e-94f5-10a56b639563>","cc-path":"CC-MAIN-2024-46/segments/1730477028290.49/warc/CC-MAIN-20241112212600-20241113002600-00220.warc.gz"}
Mauboussin on Position Sizing by Editor on May 1, 2013 • In Michael Mauboussin Below are excerpts from an article written by Michael Mauboussin in 2006 on the importance of position sizing (Size Matters). For fans of the Kelly formula, this is a must-read. Mauboussin highlights a few very important flaws of the Kelly formula when applied to our imperfect, non-normally distributed world of investing. Sizing, Diversification “To suppose that safety-first consists in having a small gamble in a large number of different [companies] where I have no information to reach a good judgment, as compared with a substantial stake in a company where one’s information is adequate, strikes me as a travesty of investment policy. — John Maynard Keynes, Letter to F.C. Scott, February 6, 1942” “As an investor, maximizing wealth over time requires you to do two things: find situations where you have an analytical edge; and allocate the appropriate amount of capital when you do have an edge. While Wall Street dedicates a substantial percentage of time and effort trying to gain an edge, very few portfolio managers understand how to size their positions to maximize long-term wealth.” “Position size is extremely important in determining equity portfolio returns. Two portfolio managers with the same list and number of stocks can generate meaningfully different results based on how they allocate the capital among the stocks. Great investors don’t stop with finding attractive investment opportunities; they know how to take maximum advantage of the opportunities. As Charlie Munger says, good investing combines patience and aggressive opportunism.” This is consistent with my belief that investors can differentiate himself/herself from the pack by going beyond security selection, and applying superior portfolio management tactics. Sizing, Expected Return, Fat Tails, Compounding, Correlation “We can express the Kelly formula a number of ways. We’ll follow Poundstone’s exposition: Edge / Odds = F Here, edge is the expected value of the financial proposition, odds reflect the market’s expectation for how much you win if you win, and F represents the percentage of your bankroll you should bet. Note that in an efficient market, there is no edge because the odds accurately represent the probabilities of success. Hence, bets based on the market’s information have zero expected value (this before the costs associated with betting) and an F of zero…if there is a probability of loss, even with a positive expected value economic proposition, betting too much reduces your expected wealth.” “Though basic, this illustration draws out two crucial points for investors of all stripes: • An intelligent investor needs an edge (a view different than that of the market); and • An investor needs to properly allocate capital to maximize value when an investment idea does appear.” “In the stock market an investor faces many more outcomes than a gambler in a casino…Know the distribution. Long-term stock market investing differs from casino games, or even trading, because outcomes vary much more than a simple model suggests. Any practical money management system faces the challenge of correcting for more complicated real-world distributions. Substantial empirical evidence shows that stock price changes do not fall along a normal distribution. Actual distributions contain many more small change observations and many more large moves than the simple distribution predicts. These tails play a meaningful role in shaping total returns for assets, and can be a cause of substantial financial pain for investors who do not anticipate them.” “…the central message for investors is that standard mean/variance analysis does not deal with the compounding of investments. If you seek to compound your wealth, then maximizing geometric returns should be front and center in your thinking…For a geometric mean maximization system to work, an investor has to participate in the markets over the long term. In addition, the portfolio manager must be able to systematically identify investment edges—points of view different than that of the market and with higher expected returns. Finally, since by definition not all market participants can have an edge, not all investors can use a Kelly system. In fact, most financial economists believe markets to be efficient. For them, a discussion of optimal betting strategy is moot because no one can systematically gain edges.” Notice in order for the Kelly Formula to work effectively, the devil (as usual) lies in the details. Get the odds wrong, or get the edge wrong, the sizing allocation will be wrong, which can reduce your expected wealth. Another question that I’ve been pondered is how the Kelly formula/criterion accounts for correlation between bets. Unlike casino gambling, probability outcomes in investing are often not independent Psychology, Volatility “The higher the percentage of your bankroll you bet (f from the Kelly formula) the larger your drawdowns. Another important lesson from prospect theory—and a departure from standard utility theory—is individuals are loss averse. Specifically, people regret losses roughly two to two and a half times more than similar-sized gains. Naturally, the longer the holding period in the stock market the higher the probability of a positive return because stocks, in aggregate, have a positive expected value. Loss aversion can lead investors to suboptimal decisions, including the well-documented disposition effect. Investors checking their portfolios frequently, especially volatile portfolios, are likely to suffer from myopic loss aversion. The key point is that a Kelly system, which requires a long-term perspective to be effective, is inherently very difficult for investors to deal with psychologically.” “Applying the Kelly Criterion is hard psychologically. Assuming you do have an investment edge and a long-term horizon, applying the Kelly system is still hard because of loss aversion. Most investors face institutional and psychological constraints in applying a Kelly-type system.” Tags: Compounding, Correlation, Diversification, Expected Return, Fat Tail, Psychology, Sizing, Volatility
{"url":"http://www.pmjar.com/?p=1475","timestamp":"2024-11-04T07:34:29Z","content_type":"application/xhtml+xml","content_length":"36052","record_id":"<urn:uuid:b39a25f5-4047-404c-8397-82f66da37153>","cc-path":"CC-MAIN-2024-46/segments/1730477027819.53/warc/CC-MAIN-20241104065437-20241104095437-00311.warc.gz"}
Reverse Mortgage Lump Sum Calculator - Certified Calculator Reverse Mortgage Lump Sum Calculator Reverse mortgages can be a useful financial tool for homeowners looking to access the equity in their homes. One key aspect to consider is the lump sum amount and monthly payment that you may receive. To simplify this process, we have created a Reverse Mortgage Lump Sum Calculator. Formula: The formula used in the calculator is as follows: • Loan Amount = Home Value * (Loan Percentage / 100) • Monthly Payment = (Loan Amount * Interest Rate) / 12 How to Use: 1. Enter your Home Value in the designated field. 2. Input the Loan Percentage you are considering. 3. Provide the Interest Rate applicable to your reverse mortgage. 4. Click the “Calculate” button to obtain the monthly payment estimate. Example: For instance, if your home value is $300,000, you are considering a 50% loan, and the interest rate is 5%, the calculated monthly payment will be displayed. 1. What is a reverse mortgage lump sum? □ A reverse mortgage lump sum is a one-time payment that homeowners can receive, representing a portion of their home’s equity. 2. How is the loan amount determined? □ The loan amount is calculated by multiplying the home value by the chosen loan percentage. 3. Is the interest rate fixed or variable in a reverse mortgage? □ Interest rates for reverse mortgages can be either fixed or variable, depending on the terms of the loan. 4. Can I change the loan percentage after taking out a reverse mortgage? □ The loan percentage is typically determined at the beginning of the reverse mortgage and remains constant. 5. What happens if the home value changes during the reverse mortgage period? □ Changes in home value may affect the equity available for a reverse mortgage, potentially influencing future payments. Conclusion: Using the Reverse Mortgage Lump Sum Calculator can help you estimate the monthly payment you might receive based on your home value, loan percentage, and interest rate. Understanding these financial aspects is crucial when considering a reverse mortgage. Leave a Comment
{"url":"https://certifiedcalculator.com/reverse-mortgage-lump-sum-calculator/","timestamp":"2024-11-08T05:07:04Z","content_type":"text/html","content_length":"54375","record_id":"<urn:uuid:65f480ca-8347-4a59-af95-66067156cc42>","cc-path":"CC-MAIN-2024-46/segments/1730477028025.14/warc/CC-MAIN-20241108035242-20241108065242-00021.warc.gz"}
In this paper we present some algorithms for solving a number of new models of facility location involving sets which generalize the classical Fermat-Torricelli problem. Our approach uses subgradient-type algorithms to cope with nondierentiabilty of the distance functions therein. Another approach involves approximating nonsmooth optimization problems by smooth optimizations problems using Nesterov’s smoothing techniques. Convergence … Read more
{"url":"https://optimization-online.org/author/jsun/","timestamp":"2024-11-05T17:02:23Z","content_type":"text/html","content_length":"109033","record_id":"<urn:uuid:56fb5611-4d23-43a5-99a0-8db826439028>","cc-path":"CC-MAIN-2024-46/segments/1730477027884.62/warc/CC-MAIN-20241105145721-20241105175721-00039.warc.gz"}
Time Series Forecasting | fg-research Time Series Forecasting¶ Perform time series forecasting in Amazon SageMaker SageMaker Algorithm CPU Training GPU Training Multi-GPU Training Incremental Training RNN SageMaker Algorithm¶ The RNN SageMaker Algorithm performs time series forecasting with Recurrent Neural Networks (RNNs). The model consists of a stack of RNN layers with either LSTM or GRU cells. Each RNN layer is followed by an activation layer and a dropout layer. The model is trained by minimizing the negative Gaussian log-likelihood and outputs the predicted mean and standard deviation at each future time step. The algorithm can be used for both univariate and multivariate time series and supports the inclusion of external features. For additional information, see the algorithm's AWS Marketplace listing page and GitHub repository. LNN SageMaker Algorithm¶ The LNN SageMaker Algorithm performs time series forecasting with Liquid Neural Networks (LNNs). The algorithm uses the closed-form continuous-depth (CfC) implementation of LNNs. CfCs use an approximate closed-form solution of the Liquid Time Constant (LTC) Ordinary Differential Equation (ODE) and, as a result, provide faster training and inference performance than other LNNs. The model is trained by minimizing the negative Gaussian log-likelihood and outputs the predicted mean and standard deviation at each future time step. The algorithm can be used for both univariate and multivariate time series and supports the inclusion of external features. For additional information, see the algorithm's AWS Marketplace listing page and GitHub repository.
{"url":"https://fg-research.com/algorithms/time-series-forecasting/index.html","timestamp":"2024-11-09T12:45:38Z","content_type":"text/html","content_length":"22568","record_id":"<urn:uuid:b698e117-6330-4a1b-aa79-c72bc48b431d>","cc-path":"CC-MAIN-2024-46/segments/1730477028118.93/warc/CC-MAIN-20241109120425-20241109150425-00043.warc.gz"}
EViews Help: Estimating DiD in EViews Estimating DiD in EViews The panel data TWFE model may be estimated using EViews’ built-in estimator for least squares regression with fixed effects. However, EViews also offers a simple interface for estimating the panel TWFE model, featuring tools for performing post-estimation diagnostics such as parallel trends tests, the Goodman-Bacon decomposition and the Callaway-Sant’Anna estimation diagnostic. (To estimate DiD models on non-panel workfiles, you will need to specify the appropriate least squares model manually using standard least squares regression.) To estimate a DiD model in EViews, bring up the equation dialog by clicking on or from the main menu bar in your panel workfile. EViews will detect the presence of your panel structure and in place of the standard equation dialog will open the panel equation estimation dialog. Select DiD – Difference-in-Difference in the Method dropdown display the DiD dialog: In the edit field you should enter the dependent variable followed by any exogenous regressors apart from the treatment variable. The treatment variable should be entered in the edit field. The treatment series should be a binary variable indicating whether the individual has been treated (i.e., is 1 if the observation in a treatment group which is post-treatment date for that group, and 0 otherwise). The tab contains a single edit field that allows you to change the default coefficient vector. Click on to perform the difference-in-difference estimation and display the output: The basic equation output for a DiD estimation is identical to that of least squares estimation, but with the display of only the single treatment coefficient. Also, the test statistic and associated p-value of a test of the parallel trends assumption is displayed with the summary statistics at the bottom of the estimation. This test is a simple Wald-test run on the auxiliary regression: It should also be noted that when performing difference-in-difference estimation, EViews uses cross-section cluster-robust standard errors. Post-Estimation Views and Procs Since the TWFE model of DiD is a simple regression model, the standard views and procs available for least squares models are also available for DiD estimation. There are a set of DiD specific views available under the view menu entry. Trends Summary The and views display the average of the outcome variable by year, categorized by treatment group (i.e., the date at which treatment occurs). This trends summary graph offers a quick visual representation of the means by treatment group to check whether the different groups have similar trends. Goodman-Bacon Decomposition Recent research has noted that the TWFE model is not suitable in multiple-timing DiD models if the impact of treatment changes as time from treatment increases. In this case, the TWFE estimator will exhibit bias since using observations from an already-treated group as the comparison violates the parallel trends assumption. The view calculates the treatment effects for individual pairs of treatment groups. By focusing on results for different categories of comparisons, the Goodman-Bacon decomposition looks for the presence of parallel trends bias in the computation of the multiple-timing TWFE. Goodman-Bacon (2021) shows that the TWFE estimator is a weighted average of DiD estimators for every combination of two groups of observations defined by two treatment dates ( Specifically, for every treatment group with a given treatment date (group • observations treated at later dates ( • observations treated at earlier dates ( • never treated observations (group • always treated observations (group For the first set of cases where untreated For the second set of cases where treated It is important to note that both the To display the decomposition, click on . EViews will display a spool view of output divided into three sections. The first section shows weighted means of the treatment effect categorized by the type of The column shows the number of year pairs used in the comparison. In this example, there are 66 treatment date pairs in which The column the estimate of the ATET, while the column contains the weights used in forming the overall ATET estimate. Here the estimated ATET across Each of the ATET components in this table is itself a weighted average of individual treatment-year pair comparisons. The second section of the spool displays the individual The labeling of the individual components indicates the nature of the group comparison. For example, the “1969 vs. 1970” comparison is an The final section in the spool contains a graph of the individual Group-Time Effects (Callaway-Sant’Anna) The Group-Time Average Treatment Effects (Callaway-Sant’Anna) view computes the Callaway and Sant’Anna (2021) estimator of the average treatment effects. A study by Callaway and Sant’Anna (2020, CS) derives a new estimator for the DiD model with multiple treatment time periods, breaking away from the TWFE estimator by using weighted averages of the differences model Equation (56.1) with appropriate comparison pairings. Their estimator is robust to the treatment effect changing as time from treatment increases so that the CS estimator can be useful as a diagnostic comparison to the TWFE estimator to judge the reliability of the TWFE estimates. To display the view select from the equation menu. EViews displays a dialog: • The Additional regressors edit field allows you to estimate the Callaway and Sant’Anna model on the underlying estimated TWFE model, but with additional regressors. This allows you to estimate models via Callaway and Sant’Anna that may be impossible to estimate via TWFE due to perfect collinearity. Simply enter the name of series, or series expressions, you wish to add to the estimation in the edit field. By default the CS estimator compares treated individuals grouped by treatment date against a control group of individuals who never receive treatment. However these comparisons could be modified to include individuals who have not yet been treated as the control group. Each pairing compares groups the treated into year-of-treatment groups, and then compares the difference between the output variable in each year after treatment with the year prior to treatment. This difference is compared for the treatment group and the comparison group. • The Comparison group radio buttons specify whether the comparison/control groups in the Callaway-Sant’Anna model will be only those individuals who never receive treatment (Never treated), or those who have yet to receive treatment in the current time period (Not yet treated), or either (Both). Clicking OK will estimate the Callaway and Sant’Anna model. The output is in the form of a spool with five sections. The first section provides a summary view of the CS results, including the overall average treatment effect of the treated (calculated as the weighted average of the individual pairing calculations), along with its standard-error, z-statistic and associated p-value. The second section provides each of the individual pairings used in the weighted average ATET estimation, including the estimated coefficient for the pairing, and its weight. The remaining sections computes aggregations of the individual pairings, where the aggregations are over treatment date (group), observation date (date), and time-since treatment (duration). For each aggregation, the coefficients, standard errors, z-Statistics and associated p-values are tabulated, along with a graph of the coefficients and a 95% confidence interval. For example, the group effects consist of the weighted averages of the ATET coefficients and the corresponding plot shows the values along with 95% confidence intervals Borusyak, Jaravel, and Spiess Imputed Estimator A third study by Borusyak, Jaravel and Spiess (2021, BJS) describes a number of potential issues with the TWFE estimator and derives an estimator that alleviates these issues. They term their estimator an “imputation” estimator, since it uses estimates from untreated observations to forecast the outcome variable for observations for which individuals have been treated, and then for each treated observation they impute the impact of treatment as the difference between the forecast of the outcome, and the observed outcome. This series of differences can then be averaged over to compute the average treatment effect. To display the view select from the equation menu. EViews estimates the BJS estimator and displays the output in a spool. As with the Group-Time Effects view ( “Group-Time Effects (Callaway-Sant’Anna)” ), there are five sections in the output: the summary view with the overall ATET, its standard-error, -statistic and associated -value, the individual pairings used in the weighted average ATET estimation, and sections for aggregations over treatment date (group), observation date (date), and time-since treatment (duration). Make Underlying Equation Proc One additional DiD-specific tool is the proc. Clicking on will produce a new equation object in which the difference-in-difference model as a standard least squares equation. Having an equation with the DiD specification estimated by least squares may prove useful for performing diagnostics or additional analysis.
{"url":"https://help.eviews.com/content/paneldid-Estimating_DiD_in_EViews.html","timestamp":"2024-11-04T09:12:02Z","content_type":"application/xhtml+xml","content_length":"35292","record_id":"<urn:uuid:4e9bc90e-8e4a-443a-b4e4-64ad2dc9f80b>","cc-path":"CC-MAIN-2024-46/segments/1730477027819.53/warc/CC-MAIN-20241104065437-20241104095437-00334.warc.gz"}
31). The centre of the circle passing through the point (0,1) a... | Filo Question asked by Filo student 31). The centre of the circle passing through the point and torchine the parabola at the point is |Thit Muin-2020 (September) (b) (c) (d) 31. Let L, be a tangent to the parabola and be Not the question you're searching for? + Ask your question Video solutions (1) Learn from their 1-to-1 discussion with Filo tutors. 7 mins Uploaded on: 11/6/2022 Was this solution helpful? Found 8 tutors discussing this question Discuss this question LIVE for FREE 10 mins ago One destination to cover all your homework and assignment needs Learn Practice Revision Succeed Instant 1:1 help, 24x7 60, 000+ Expert tutors Textbook solutions Big idea maths, McGraw-Hill Education etc Essay review Get expert feedback on your essay Schedule classes High dosage tutoring from Dedicated 3 experts Practice more questions on Coordinate Geometry View more Students who ask this question also asked View more Stuck on the question or explanation? Connect with our Mathematics tutors online and get step by step solution of this question. 231 students are taking LIVE classes 31). The centre of the circle passing through the point and torchine the parabola at the point is |Thit Muin-2020 (September) (b) (c) (d) 31. Let L, be a tangent to the parabola Question Text and be Updated On Nov 6, 2022 Topic Coordinate Geometry Subject Mathematics Class Class 12 Answer Type Video solution: 1 Upvotes 77 Avg. Video Duration 7 min
{"url":"https://askfilo.com/user-question-answers-mathematics/31-the-centre-of-the-circle-passing-through-the-point-and-32363336393036","timestamp":"2024-11-03T01:34:02Z","content_type":"text/html","content_length":"260043","record_id":"<urn:uuid:cb9b4e4e-8d70-4a45-a06d-5de197426ca3>","cc-path":"CC-MAIN-2024-46/segments/1730477027768.43/warc/CC-MAIN-20241102231001-20241103021001-00655.warc.gz"}
oundations C Formal foundations COURSES As part of their coursework in the major, students must develop expertise in the formal analytical methods used in the field of cognitive science. The specific formal skills that will be most useful depend on each individual’s particular areas of interest, so students are free to select any two courses from an approved set of options that include (but are not limited to) courses in mathematics, computer science, logic, quantitative analysis, and experiment design. Experience with the equivalent of one course in Calculus is highly recommended, as expertise in this area is required for many of the Disciplinary Courses. (Note: Calculus I-II may be used to satisfy the Formal Foundations requirement only if they are not used to satisfy the Mathematical Sciences component of the College Core, per College requirements.) The following are examples of courses that could be used to satisfy the Formal Foundations requirement. Formal Foundations COURSE CODE COURSE TITLE BIOS 20151 Introduction to Quantitative Modeling in Biology. BIOS 20152 Introduction to Quantitative Modeling in Biology (Advanced) BIOS 20172 Mathematical Modeling for Pre-Med Students BIOS 20236 Biological Dynamics CHDV 39301 Qualitative Research Methods CMSC 12100 Computer Science with Applications I CMSC 12200 Computer Science with Applications II CMSC 14100 Introduction to Computer Science I CMSC 14200 Introduction to Computer Science II CMSC 14300 Systems Programming I CMSC 14400 Systems Programming II CMSC 15100 Introduction to Computer Science I CMSC 15200 Introduction to Computer Science II CMSC 15400 Introduction to Computer Systems CMSC 25300 Mathematical Foundations of Machine Learning CMSC 27100 Discrete Mathematics DATA 22700 Data Visualization and Communication ECON 10700 Introductory Game Theory LING 20120 Formal Foundations of Linguistics LING 22500 Quantitative Methods in Linguistics LING 36601 Intro to Python and R for Linguists MATH 13100 Elem Functions and Calculus I MATH 13200 Elem Functions and Calculus II MATH 13300 Elementary Functions and Calculus III MATH 15100 Calculus I MATH 15200 Calculus II MATH 15250 Mathematical Methods for Economic Analysis MATH 15300 Calculus III MATH 16100 Honors Calculus I MATH 16200 Honors Calculus II MATH 16300 Honors Calculus III MATH 18300 Mathematical Methods in the Physical Sciences I MATH 18400 Mathematical Methods in the Physical Sciences II MATH 19620 Linear Algebra MATH 20250 Abstract Linear Algebra MATH 27700 Mathematical Logic I MATH 28000 Introduction to Formal Languages NSCI 21820 Introduction to Python for Biologists & Neuroscientists PHIL 20100 Introduction to Logic PSYC 20200 Psychological Research Methods PSYC 20250 Introduction to Statistical Concepts and Methods PSYC 43030 Introduction to Python Programming in the Behavioral Sciences SOSC 20112 Introductory Statistical Methods and Applications for the Social Sciences SOSC 26007 Overview of Quantitative Methods in the Social and Behavioral Sciences STAT 22000 Statistical Methods and Applications STAT 23400 Statistical Models and Methods STAT 24400 Statistical Theory and Methods I STAT 24500 Statistical Theory and Methods II STAT 27410 Introduction to Bayesian Data Analysis
{"url":"https://voices.uchicago.edu/cognitivescience/course-petition/formal-foundations-courses/","timestamp":"2024-11-09T09:45:40Z","content_type":"text/html","content_length":"74066","record_id":"<urn:uuid:b69ad67b-6e80-4e71-96a2-8ddfcc54f826>","cc-path":"CC-MAIN-2024-46/segments/1730477028116.75/warc/CC-MAIN-20241109085148-20241109115148-00403.warc.gz"}
Home - Convergetics Research Center Abstract This article explores Hermann Minkowski’s groundbreaking 1908 lecture Space and Time, where he introduced his Theory of the Absolute World. This theory, which encompasses the concept of four-dimensional spacetime, proposed a multispace paradigm, suggesting... Abstract This article explores Hermann Minkowski’s groundbreaking 1908 lecture Space and Time, where he introduced his Theory of the Absolute World. This theory, which encompasses the concept of four-dimensional spacetime, proposed a multispace paradigm, suggesting... Abstract. We present compelling evidence that reality encompasses more than just our own universe by examining several celestial objects that are widely known but not fully understood. First, we argue that the Milky Way galaxy is not planar or warped at both ends, but... We show that quantum spaces, which we now think of as abstract mathematical structures, are in fact real spaces embedded in, but at the same time distinct from, the space of the universe. Earth is warming (climate change), shaking (earthquakes), leaking (volcanoes), swirling (tornadoes, hurricanes), flooding, landsliding (plate tectonics, soil sliding), all at the same time. Trying to understand climate change disconnected from other natural phenomena is doomed to failure. While physically embedded in the universe, these spaces are at the same time external to the space of the universe. This concept may be puzzling at first glance, but elementary particles are a ubiquitous and ideal example of such orthogonal spaces. In 1908, Hermann Minkowski introduced the concept that space and time are fused in what he called the space-time continuum, and built a 2D space-time diagram. Yet, his diagram provides just a partial view of the 3D Multispace Diagram.
{"url":"https://convergetics.org/home-2/","timestamp":"2024-11-02T14:44:04Z","content_type":"text/html","content_length":"181143","record_id":"<urn:uuid:3986341e-cf11-4283-bbce-1ddcc952881d>","cc-path":"CC-MAIN-2024-46/segments/1730477027714.37/warc/CC-MAIN-20241102133748-20241102163748-00518.warc.gz"}
Where can I find reliable help for linear regression assignments? | Hire Someone To Take My Statistics Assignment Where can I find reliable help for linear regression assignments? I’ve been stuck with my project a long time so its incredibly frustrating even blog here I know what the answer looks like. I want to know if anyone has been able to get read here answer to the code. I used two of the parts below and the only reason I am doing this is because I don’t know much about linear regression and I want to keep the program as slow as possible. If you know that you have done something wrong check the bug report you got from https://grep.openpostgresql.org/browse/GDTRANSLATION and make sure that it reports the correct code. I have 2 data types: int and float. I’m trying to write a procedure to get either -16.33-9.11-9.05 or -16.33-9.11-9.05 that calculates linear regression on a given number of points in a space. I tried the full procedure from this link:https://grep.openpostgresql.org/browse/GDTRANSLATION-1-1.html However, when I run the full procedure, it should print the correct values for all data types, but for certain points it appertains the wrong values (plus some parts that go wrong). Any help would be appreciated. I’m using Sarcadep in python 3. Homework Done For You 4.2 using python2.7.5 and using the -1.1.2 module from an openpostgresql.org repo. A: You are using the -16.33-9.11-9.05 dataset types. That implies when the value of -16.33 comes from the data source the regression task can be quite slow since many analyses are performed on a given number of points, which is typically not meaningful for linear regression. So if you are doing the linear regression on the same data type, being very interested in the actual values of the regression, you could write a script to get the correct number of points using whatever function you find necessary. B2D format for the lr-spaces: Where can I find reliable help for linear regression assignments? Does a linear regression should lead to efficient results if accuracy is high? I have been trying to work out an application try here works well for a linear regression (without optimization.) I understand that there are two best cases for this, based on my experience. 1) For regression this being very difficult as it is linear models where each term relates to a variable and a correlation between items is not significant. That being said, a regression approach that follows that of linear regression should not result in efficient results for something as simple as removing variables that are very similar to an average and thus not significantly different then 1 to give optimum accuracy. 2) This approach works if there is no reduction in the correlation for all data. That is what you want. Can I Pay Someone To Do My Homework I did not have much experience with logistic regression. It is as simple as finding the correct regression approach the answer, which I worked out a few times. A: The least significant model I ever worked on is the nonlinear model for linear problems. That’s why most linear regression algorithms do, as far as their solution point in the right direction. The problem with your way of solving the problem is that it doesn’t have a function or a function-value dependent for solving problems. If you’re interested in getting a solution to a logistic regression program for linear problems and then re-making those on a problem that you find the answer, take a look at http://cubl.net/lin2 (with a term fit in lower order), and see if your program works. However, you’ll need to understand that some programs have a range query that they handle for errors, it’s an old technique, for example http://www.hoxsey.com/opinions/ how-to-find-a-low-variance-ratio-approximation-problem-from-the-variable-change (the nonlinear class may be your choice, I recommend that you actually put a series of options on your program so that it works). One of the main steps to work on this application is determining a range query. You can Continued use a random search function or find a range query on these classes (one could do the latter, though it may be harder). When you find the range query then look for the code see this page can use official source query’s range membership as a function of the response. I can’t give a good impression as to the efficiency. Where can I find reliable help for linear regression assignments? Hiya guys I have done many linear regression assignments. However, would it be possible to have my linear regression and the regression line specified with: y=f$y1.x; Thanks in Advance Just wondering if anyone has any good source code for linear regression assignments and I am facing this issue. Is there a way to get read help/reference from the regression line it would have if it’s desired? The regression line So if $y_1=f$ and $x\in\bigsus$ and $x$ is a value corresponding to $y=f$ then the line should be: $$y_1=f$ x=xf(1); if $x\in\bigsus$ then check if $x=xf(1); If $x\in\bigsus$ then check if $x=xf(1)$ or $f(x)=x$. If $x\notin\bigsus$ then check if $f(x)=x$. Finally $y:=f(x)$ is the y-gradient. Online Test Taker A: This has my minor-mismess mv.linearr@macaddress(function(source,target,output) { if (target!= output) { output = target; } else { target = source; } if (source!= target) { output = source; } if (output!= target) { output = Home } return output; }, {y: #1 } )
{"url":"https://statskey.com/where-can-i-find-reliable-help-for-linear-regression-assignments","timestamp":"2024-11-08T14:03:44Z","content_type":"text/html","content_length":"158162","record_id":"<urn:uuid:01485562-8e12-4a35-b25f-539e5119b025>","cc-path":"CC-MAIN-2024-46/segments/1730477028067.32/warc/CC-MAIN-20241108133114-20241108163114-00272.warc.gz"}
Illustrative Mathematics Origami Silver Rectangle This task examines the mathematics behind an origami construction of a rectangle whose sides have the ratio $(\sqrt{2}:1)$. Such a rectangle is called a silver rectangle. Beginning with a square piece of paper, first fold and unfold it leaving the diagonal crease as shown here: Next fold the bottom right corner up to the diagonal: After unfolding then fold the left hand side of the rectangle over to the crease from the previous fold: Here is a picture, after the last step has been unfolded, with all folds shown and some important points marked. In the picture $T$ is the reflection of $S$ about $\ell$. 1. Suppose $s$ is the side length of our square. Show that $|PT| = s$. 2. Show that $\triangle PQT$ is a 45-45-90 isosceles triangle. 3. Calculate $|PQ|$ and conclude that $PQRS$ is a silver rectangle. IM Commentary The purpose of this task is to apply geometry in order analyze the shape of a rectangle obtained by folding paper. The central geometric ideas involved are reflections (used to model the paper folds), analysis of angles in triangles, and the Pythagorean Theorem. The task is appropriate either at the 8th grade level or in high school: the only difference would be the level of rigor expected in the explanation. The solution given is appropriate for either level. The silver rectangle is one of three rectangles identified in ancient times as having important properties: the bronze rectangle has a side ratio of $(\sqrt{3},1)$ and the golden rectangle has a side ratio of $\left(1+\sqrt{5}:2 \right)$. Each of these three rectangles can be constructed by folding paper. Further properties of silver rectangles are examined in www.illustrativemathematics.org/ illustrations/1489. Much more interesting information about the silver rectangle can be found here: http://en.wikipedia.org/wiki/Silver_ratio This task could be given in a much more open ended form, particularly if done in high school. Teachers could go through the different steps of the construction and then prompt students to examine the ratio of side lengths in the final rectangle. There are many possible solution paths revolving around the different triangles in the picture that are similar: half of the original square, $\triangle TQP$, and the small triangle in the lower left of the last picture. These are all 45-45-90 triangles and the dimensions of the rectangle can be found by calculating the scale factor between any pair of these triangles. The Standards for Mathematical Practice focus on the nature of the learning experiences by attending to the thinking processes and habits of mind that students need to develop in order to attain a deep and flexible understanding of mathematics. Certain tasks lend themselves to the demonstration of specific practices by students. The practices that are observable during exploration of a task depend on how instruction unfolds in the classroom. While it is possible that tasks may be connected to several practices, the commentary will spotlight one practice connection in depth. Possible secondary practice connections may be discussed but not in the same degree of detail. Students engage in Mathematical Practice Standard 5, Use appropriate tools strategically,â by considering a toolâ s usefulness, its strengths and limitations, as well as knowing how to use it appropriately. This task examines the mathematics of the paper folding of a square piece of paper resulting in the creation of a silver rectangle. The tools, including paper, directions, straight edges, etc. allow students to analyze the shape of the paper as it is folded and unfolded revealing different triangular and rectangular shapes. This experimentation with paper folding allows students to explore and discuss observations before forming conclusions about the final rectangle. Students will also ''Reason abstractly and quantitatively'' (MP.2) as they need to represent the paper folding geometrically in order to calculate angles and side lengths. 1. Since reflection about $\ell$ maps $P$ to itself and maps $S$ to $T$ this means that segments $\overline{PS}$ and $\overline{PT}$ are interchanged by reflection about $\ell$. Hence $$ |PS| = |PT| $$ because reflections preserve lengths of segments. We are given that $|PS| = s$ since it is one side of the square so $|PT| = s$. 2. We know that $m(\angle QPT) = 45$ because the diagonal of a square bisects the 90 degree angles. Alternatively, reflection over the diagonal $\overleftrightarrow{PT}$ is a symmetry of the square: this reflection interchanges angles $QPT$ and $SPT$ and so these must each measure 45 degrees. Angle $TQP$ is a right angle because $\overline{QR}$ is a crease resulting from a horizontal fold of the paper. Since $$ m(\angle QTP) + m(\angle QPT) + m(\angle TQP) = 180 $$ this means that $m(\angle QTP) = 45$. Thus $\triangle PQT$ is a 45-45-90 triangle: it is isosceles because $m(\angle QTP) = m(\angle QPT)$. 3. To find $|PQ|$ we use the fact that $\triangle PQT$ is a right isosceles triangle. Thus we know that $|TQ| = |PQ|$ and, from the Pythagorean Theorem, $$ |PQ|^2 + |TQ|^2 = |PT|^2. $$ Substituting $|PQ|$ for $|TQ|$ this is equivalent to $$ 2|PQ|^2 = |PT|^2. $$ From part (a) this means that $$ |PQ|^2 = \frac{s^2}{2}. $$ So $$ |PQ| = \frac{s}{\sqrt{2}}. $$ We can now check that $PQRS$ is a silver rectangle. Our calculations show that $|PS|:|PQ|$ is $s:\frac{s}{\sqrt{2}}$. But the ratio $s:\frac{s}{\sqrt{2}}$ is equivalent to the ratio $\sqrt{2}:1$ as we can see using a scaling factor of $\frac{\sqrt{2}}{s}$. Origami Silver Rectangle This task examines the mathematics behind an origami construction of a rectangle whose sides have the ratio $(\sqrt{2}:1)$. Such a rectangle is called a silver rectangle. Beginning with a square piece of paper, first fold and unfold it leaving the diagonal crease as shown here: Next fold the bottom right corner up to the diagonal: After unfolding then fold the left hand side of the rectangle over to the crease from the previous fold: Here is a picture, after the last step has been unfolded, with all folds shown and some important points marked. In the picture $T$ is the reflection of $S$ about $\ell$. 1. Suppose $s$ is the side length of our square. Show that $|PT| = s$. 2. Show that $\triangle PQT$ is a 45-45-90 isosceles triangle. 3. Calculate $|PQ|$ and conclude that $PQRS$ is a silver rectangle.
{"url":"https://tasks.illustrativemathematics.org/content-standards/tasks/1488","timestamp":"2024-11-09T20:23:35Z","content_type":"text/html","content_length":"30204","record_id":"<urn:uuid:cb38b1f6-9d7d-49d4-a0d3-a019b15ebd5e>","cc-path":"CC-MAIN-2024-46/segments/1730477028142.18/warc/CC-MAIN-20241109182954-20241109212954-00276.warc.gz"}
What else do you want on this blog? Hello everyone, I know there are on average over 100 readers viewing this blog on a regular basis. Some of you are facing challenges with Math, some are good with the subject but seeking more easier learning methods,some of you are parents or tutors looking for inspiration for your child or yourself. So I am writing this post to gather from you - my valued readers what else do you want to see on this site in addition to what you see. As you can see, this site is nothing more than my many years of teaching experience of different types of students and some of the strategies which have proven to have worked effectively for them. But this is what I think would be useful only. The important thing is that you must find them beneficial. So tell me what else do you want to read about, the improvements to the structure of the site so that this blog is more friendly for you for all levels. "Tell Me What Else You Want On This Blog" I look forward in hearing from you! Simply drop me a comment in the space below. PS: "It's only when you ask, that you will get" 1. bowo says I lived in Indonesia and want to know how to get or to follow "O" level test in singapore. Any way do you have the math test or exercise related wit O level test .Thx 2. dawn says hey can you email me more notes or help on sec 4 topics like probability, locus and geometrical transformation( reflection, rotation, translation, enlargement shear) sorry if i request for too many:D thanks hope to get ur mail soon 3. woo says hello. maybe you can email me or post more maths questions more frequently. as a maths lover, i tend to do alot so much so that i often run out of questions to do. so if you would be able to provide me with some more questions, i would be delighted and too, be much confident with my upcoming O levels. thanks. 4. KC says Speaking from quite a few of my schoolmates' point of view, I would appreciate more stuff on topics on graphs, such as the logarithmic graph, cubic function, reciprocal graphs, etc, and mainly how to construct them. Anyway, thanks for the previous notes and all, and hope to receive future mail from you soon. 5. Tang says To be frank, I am attracted to "the Secrets to be a SQUARING EXPERT within 30 seconds!" but disappointed its does not apply to other number than 5. Continue to post such interesting of Maths Secrets, tips on the exam etc.. that can inspire people. Beside you may publish interesting "O" level questions that student always get it wrong with the answer and solution in subsequent week so that at least student can have a chance to try it first before looking at the solution. 6. michelle says pls email me lectures on Amaths topics like probability,logarithms,integration, difefrentiaition,i keep failing Amaths maybe u could post some video that shows ur teaching of the Amths topics we could learn from it haha thanks 7. tanweer says i am math lover and want to learn something more and new .any site available where ,from basic to a level math solved or showing to how solve.thanks 8. Yephaniya says (please excuse my english) I live in Indonesia and I'm planning to take the IGCSE O level test. So can you post questions for me to practice??? 9. marites de guzman says could u possibly give some techniques in geometry and trigo? thanks a lot 10. Sarah says Dear AlwaysLovely, My deepest appreciation n gratefulness if u r kind enough 2 mail me studying tactics on additional maths esp on Linear Programming for Malaysia's SPM level. Also, i would also like 2 have tips n short lessons on Australian Mathematics...this is for my Aust. Maths competition on 31st of July 08. If possible, pls make it fast. Thanks a lot. 11. Hong says Can you explain a bit the chapter about limit, dy/dx and differentiation? 12. wanling says i hope that you will create a test paper for us....to test our skills for every topics..to see if we can complete the work on time and gauge our standard..and perhaps we would know which are our strong points and weak points... 13. Esther says I have got some problems dealing with Additional Mathematics topics such as Trigonometry, Differentiation of functions (Derivatives of Trigo Functions, Exponential and Logarithmic Functions) and Integration. I am always getting a C5 or a C6 in Additional Mathematics... Could you give us some study tips? Like how to study for examinations? 14. Summaiya saleem says Can you tell me where i can get free maths worksheets online...,, for practice .... 15. Jun Kiat says if there's a video demostration of examples, it would be great! 16. Gracious says I have got some problems with some topics relating to Additional Mathematics. Topics such as Trigonometry, Geometry, Differentiation of functions 'Derivatives of Trigo Functions, Exponential and Logarithmic Functions' and Integration. I had always got a D7 or F9 in my papers. And I need you to please help me out, or better still, give me some tips to solving questions in those areas. 17. alwaysLovely says Hi Gracious, Congrats on your courage and awareness of the areas you need to work on. Now, perhaps you would want to do a search on my website on the blog posts on the topics you have mentioned. I have done a search on trigonometry. Here's the search result => You may want to send in your questions with your workings. I can assist 18. dinush shayaman says Rearly i am appreciate u if you cn send me some ordinary level maths exam papers.because i want to study them further.i am looking forward for you. Thank You 19. miya says I am thinking of if you can post up slides on maths common errors that students will usually make and also power point slides on the maths topics such as introduction, different method of solving, rules to remember and also tips. I am looking forward to have these slides to be posted up on the blog. Thank You 20. alwaysLovely says @Dinush: I have some sample Mathematics Papers available for download here: 21. Hamzah says peace to all i wanted to say that even though your blog is awesome an all, you should add some downloads too, in pdf or word format , like past-papers and other tips ... it would be surely a lot cooler... 22. xavier says i have some problems with Amath : Differentiation, rate of change and trigo. if you could put up some easy ways to solve such questions it would be greatly appreciated... 23. alwaysLovely says Hi Xavier, if you were to do a search on my website using the search tool, you can find the notes you want. Differentiation: http://www.singaporeolevelmaths.com/2009/02/26/o-level-a-math-collection-of-differentiation-tips/ Trigonometry Identity: http://www.singaporeolevelmaths.com/2008/09/29/o-level-additional-math-how-to-prove-trigonometry-identity/ Look out for related post section for more tips when you finish reading each blog post. 24. Suria says Hello! I'm still having some difficulties on Additional Mathematics topic on the topic of Integration and differentiation of Trigonometry. I wish you could send me more helpful notes and please recommend me ways to study those subject (: And for all the email you've sent to me, I found it useful for my everyday revision. Thanks! (: 25. Dyana says i have like a list of questions to ask you but the topics i really need assistance currently are : Vectors, Probability and mensuration. And um, if for example i have problem solving a particular question right, can i scan it from the book and show it to you? Hope to hear from you soon. Thanks =D 26. alwaysLovely says I cannot promise you anything but I will do my best. I have almost 100 emails to handle everyday. Priority goes to my circle of students I coach. You would want to scan the question + your working to be effective for your learning 27. alwaysLovely says Suria, I would recommend that you use the Search function on this website to locate the items you need. This is the link combining Differentiation & Integration: (I found it through the search button too (It's located above the section of "Who is behind singaporeolevelmaths.com?" 28. sandrar says Hi! I was surfing and found your blog post... nice! I love your blog. :) Cheers! Sandra. R. 29. alwaysLovely says Sandra, thanks for your encouragement. It motivates me to write more! 30. meera says i have been enjoying going through the tips and videos you have put on blog. thanks for the same. i want to know whats the key word in understanding a problem whether it is on permutation or combination? 31. sakthi393 says Hi and thank u for teaching the squaring expert method.It kinda helps.Comming to think of maths,i have some problems such as vectors, matrices,and venn diagrams.Generally speaking,probably i also think this are also some of the problems some of us are facing.So i hope u could actually post some popular exam questions regarding these topics which could help me and the others.thanks Leave a Reply Cancel reply
{"url":"https://www.singaporeolevelmaths.com/wp-content/cache/page_enhanced/www.singaporeolevelmaths.com/what-else-do-you-want-on-this-blog/_index_slash.html_gzip","timestamp":"2024-11-04T23:05:45Z","content_type":"text/html","content_length":"75567","record_id":"<urn:uuid:56196a31-0d4d-4fdd-b13b-4f76d4c8ff49>","cc-path":"CC-MAIN-2024-46/segments/1730477027861.84/warc/CC-MAIN-20241104225856-20241105015856-00455.warc.gz"}
How would you determine the vapor pressure of a solution at 25C that contains 76.6 g of glucose (C6H12O6) in 250.0 mL of water? The vapor pressure of pure water at 25C is 23.8 torr. | HIX Tutor How would you determine the vapor pressure of a solution at 25C that contains 76.6 g of glucose (C6H12O6) in 250.0 mL of water? The vapor pressure of pure water at 25C is 23.8 torr. Answer 1 The first thing to do here is determine the mass of the sample of water by using water's density at #25^@"C"#, which is equal to #rho = "0.99705 g/mL"# This implies that the water's mass will be #250.0color(red)(cancel(color(black)("mL"))) * "0.99705 g"/(1color(red)(cancel(color(black)("mL")))) = "249.26 g"# Since glucose is a non-volatile substance, the only factors affecting the solution's vapor pressure are the mole fraction of water and the vapor pressure of pure water. To put it simply, only water vapor will be present at the solution's vapor pressure. Now, calculate how many moles of each you have using the molar masses of glucose and water. #76.6color(red)(cancel(color(black)("g"))) * "1 mole glucose"/(180.156color(red)(cancel(color(black)("g")))) = "0.4252 moles glucose"# #249.26color(red)(cancel(color(black)("g"))) * "1 mole water"/(18.015color(red)(cancel(color(black)("g")))) = "13.836 moles water"# The ratio of the amount of water to the total amount of moles in the solution is known as the mole fraction of water. There will be a total of moles in the solution. #n_"total" = n_"glucose" + n_"water"# #n_"total" = 0.4252 + 13.836 = "14.261 moles"# This implies that the water mole fraction will be #chi_"water" = (13.836color(red)(cancel(color(black)("moles"))))/(14.261color(red)(cancel(color(black)("moles")))) = 0.9702# Consequently, the solution's vapor pressure will be #P_"sol" = chi_"water" * P_"water"^@# #P_"sol" = 0.9702 * "23.8 torr" = "23.091 torr"# The response, rounded to three sig figs, is #P_"sol" = color(green)("23.1 torr")# Sign up to view the whole answer By signing up, you agree to our Terms of Service and Privacy Policy Answer 2 To determine the vapor pressure of the solution, you would use Raoult's Law, which states that the vapor pressure of a solution is proportional to the mole fraction of solvent present. First, calculate the mole fraction of water in the solution. Then, use the formula: Vapor pressure of solution = Mole fraction of solvent * Vapor pressure of pure solvent To find the mole fraction of water: 1. Calculate the moles of glucose using its molar mass. 2. Calculate the moles of water using its volume and molar mass. 3. Add the moles of glucose and water together. 4. Divide the moles of water by the total moles to find the mole fraction of water. Then, substitute the mole fraction of water and the vapor pressure of pure water into the formula to find the vapor pressure of the solution. Sign up to view the whole answer By signing up, you agree to our Terms of Service and Privacy Policy Answer from HIX Tutor When evaluating a one-sided limit, you need to be careful when a quantity is approaching zero since its sign is different depending on which way it is approaching zero from. Let us look at some When evaluating a one-sided limit, you need to be careful when a quantity is approaching zero since its sign is different depending on which way it is approaching zero from. Let us look at some When evaluating a one-sided limit, you need to be careful when a quantity is approaching zero since its sign is different depending on which way it is approaching zero from. Let us look at some When evaluating a one-sided limit, you need to be careful when a quantity is approaching zero since its sign is different depending on which way it is approaching zero from. Let us look at some Not the question you need? HIX Tutor Solve ANY homework problem with a smart AI • 98% accuracy study help • Covers math, physics, chemistry, biology, and more • Step-by-step, in-depth guides • Readily available 24/7
{"url":"https://tutor.hix.ai/question/how-would-you-determine-the-vapor-pressure-of-a-solution-at-25c-that-contains-76-8f9af85a4d","timestamp":"2024-11-10T15:08:17Z","content_type":"text/html","content_length":"590893","record_id":"<urn:uuid:bd985d9b-e536-4269-a914-f2daf9f492c2>","cc-path":"CC-MAIN-2024-46/segments/1730477028187.60/warc/CC-MAIN-20241110134821-20241110164821-00618.warc.gz"}
Bases and Coordinates in R2 This applet demonstrates the concept of coordinate vectors in $\mathbb R^2$ A basis $\mathcal B$ of a vector space $V$ is a linearly independent spanning set. One useful feature of a basis is that it gives rise to a way of writing coordinates on $V$. Any vector $\mathbf v \in V$ can be written uniquely as a linear combination of the basis vectors in $\ mathcal B$. If $V$ has dimension $n$, any vector $\mathbf v$ can therefore be uniquely specified by a list of $n$ scalars: $$[\mathbf v]_{\mathcal B} = \begin{bmatrix}\alpha_1\\\vdots\\\alpha_n\end {bmatrix} \Leftrightarrow \mathbf v = \alpha_1 \mathbf b_1 + … + \alpha_n \mathbf b_n.$$ The applet shows a vector $\mathbf v$ drawn in black, as well as a blue coordinate grid which corresponds to a basis $\mathcal B$. You can click and drag the tips of the vectors to change the vector $\mathbf v$ or the basis vectors in $\mathcal B$. You can also enable a second coordinate grid corresponding to a different basis $\mathcal C$ by clicking the checkbox on the right. As you adjust the vectors, the coordinate vector visible on the right of the applet will change. Is it consistent with what you visible in the applet? Count out the number of steps you need to take along the blue coordinate grid axes to reach the vector $\mathbf v$. Other resources:
{"url":"https://melbapplets.ms.unimelb.edu.au/2024/04/05/bases-and-coordinates-in-r2/","timestamp":"2024-11-03T00:19:29Z","content_type":"text/html","content_length":"25159","record_id":"<urn:uuid:5a4fa88b-0abc-48a5-891d-5b115a094918>","cc-path":"CC-MAIN-2024-46/segments/1730477027768.43/warc/CC-MAIN-20241102231001-20241103021001-00794.warc.gz"}
type folding in guile A-hey hey hey, my peeps! Today's missive is about another optimization pass in Guile that we call "type folding". There's probably a more proper name for this, but for the moment we go with "type folding" as it's shorter than "abstract constant propagation, constant folding, and branch folding based on flow-sensitive type and range analysis". on types A word of warning to the type-system enthusiasts among my readers: here I'm using "type" in the dynamic-languages sense, to mean "a property about a value". For example, whether a value is a vector or a pair is a property of that value. I know that y'all use that word for other purposes, but there are other uses that do not falute so highly, and it's in the more pedestrian sense that I'm interested here. To back up a bit: what are the sources of type information in dynamic languages? In Guile, there are three ways the compiler can learn about a value's type. One source of type information is the compiler's knowledge of the result types of expressions in the language, especially constants and calls to the language's primitives. For example, in the Scheme definition (define y (vector-length z)), we know that y is a non-negative integer, and we probably also know a maximum value for z too, given that vectors have a maximum size. Conditional branches with type predicates also provide type information. For example, in consider this Scheme expression: (lambda (x) (if (pair? x) (car x) (error "not a pair" x))) Here we can say that at the point of the (car x) expression, x is definitely a pair. Conditional branches are interesting because they add a second dimension to type analysis. The question is no longer "what is the type of all variables", but "what is the type of all variables at all points in the program". Finally, we have the effect of argument type checks in function calls. For example in the (define y (vector-length z)) definition, after (vector-length z) has been evaluated, we know that z is indeed a vector, because if it weren't, then the primitive call would raise an exception. In summary, the information that we would like to have is what type each variable has at each program point (label). This information comes from where the variables are defined (the first source of type information), conditional branches and control-flow joins (the second source), and variable use sites that imply type checks (the third). It's a little gnarly but in essence it's a classic flow analysis. We treat the "type" of a variable as a set of possible types. A solution to the flow equations results in a set of types for each variable at each label. We use the intmap data structures to share space between the solution at different program points, resulting in an O(n log n) space complexity. In Guile we also solve for the range of values a variable may take, at the same time as solving for type. I started doing this as part of the Compost hack a couple years ago, where I needed to be able to prove that the operand to sqrt was non-negative in order to avoid sqrt producing complex numbers. Associating range with type turns out to generalize nicely to other data types which may be thought of as having a "magnitude" -- for example a successful (vector-ref v 3) implies that v is at least 4 elements long. Guile can propagate this information down the flow graph, or propagate it in the other way: if we know the vector was constructed as being 10 elements long, then a successful (vector-ref v n) can only mean that n is between 0 and 9. what for the typing of the things Guile's compiler uses type analysis in a few ways at a few different stages. One basic use is in dead code elimination (DCE). An expression can be eliminated from a program if its value is never used and if it causes no side effects. Guile models side effects (and memory dependencies between expressions) with effects analysis. I quote: We model four kinds of effects: type checks (T), allocations (A), reads (R), and writes (W). Each of these effects is allocated to a bit. An expression can have any or none of these effects. In an expression like (vector-ref v n), type analysis may compute that in fact v is indeed a vector and n is an integer, and also that n is within the range of valid indexes of v. In that case we can remove the type check (T) bit from the expression's effects, opening up the expression for DCE. Getting back to the topic of this article, Guile's "type folding" pass uses type inference in three ways. The first use of type information is if we determine that, at a given use site, a variable has precisely one type and one value. In that case we can do constant folding over that expression, replacing its uses with its value. For example, let's say we have the expression (define len (vector-length v)). If we know that v is a vector of length length 5, we can replace any use of len with the constant, 5. As an implementation detail we actually keep the definition of len in place and let DCE remove it later. We can consider this to be abstract constant propagation: abstract in the sense that it folds over abstract values, represented just as type sets and ranges, and which materializes a concrete value only if it is able to do so. Since ranges propagate through operators as well, it can also be considered as abstract constant folding; the type inference operators act as constant folders. Another use of type information is in branches. If Guile sees (if (< n (vector-length v)) 1 2) and n and v have the right types and disjoint ranges, then we can fold the test and choose 1 or 2 depending on how the test folds. Finally type information can enable strength reduction. For example it's a common compiler trick to want to reduce (* n 16) to (ash n 4), but if n isn't an integer this isn't going to work. Likewise, (* n 0) can be 0, 0.0, 0.0+0.0i, something else, or an error, depending on the type of n and whether the * operator has been extended to apply over non-number types. Type folding uses type information to reduce the strength of operations like these, but only where it can prove that the transformation is valid. So that's type folding! It's a pretty neat pass that does a few things as once. Code here, and code for the type inference itself here. type-driven unboxing Guile uses type information in one other way currently, and that is to determine when to unbox floating-point numbers. The current metric is that whenever an arithmetic operation will produce a floating-point number -- in Scheme parlance, an inexact real -- then that operation should be unboxed, if it has an unboxed counterpart. Unboxed operations on floating-point numbers are advantageous because they don't have to allocate space on the garbage-collected heap for their result. Since an unboxed operation like the fadd floating-point addition operator takes raw floating-point numbers as operands, it also will never cause a type check, unlike the polymorphic add instruction. Knowing that fadd has no effects lets the compiler do a better job at common subexpression elimination (CSE), dead code elimination, loop-invariant code motion, and so on. To unbox an operation, its operands are unboxed, the operation itself is replaced with its unboxed counterpart, and the result is then boxed. This turns something like: (+ a b) (f64->scm (fl+ (scm->f64 a) (scm->f64 b))) You wouldn't think this would be an optimization, except that the CSE pass can eliminate many of these conversion pairs using its scalar elimination via fabricated expressions pass. A proper flow-sensitive type analysis is what enables sound, effective unboxing. After arithmetic operations have been unboxed, Guile then goes through and tries to unbox loop variables and other variables with more than one definition ("phi' variables, for the elect). It mostly succeeds at this. The results are great: summing a packed vector of 10 million 32-bit floating-point values goes down from 500ms to 130ms, on my machine, with no time spent in the garbage collector. Once we start doing native compilation we should be up to about 5e8 or 10e8 floats per second in this microbenchmark, which is totally respectable and about what gcc's -O0 performance gets. This kind of type inference works great in tight loops, and since that's how many programs spend most of their time, that's great. Of course, this situation is also a product of programmers knowing that tight loops are how computers go the fastest, or at least of how compilers do the best on their code. Where this approach to type inference breaks down is at function boundaries. There are no higher-order types and no higher-order reasoning, and indeed no function types at all! This is partially mitigated by earlier partial evaluation and contification passes, which open up space in which the type inferrer can work. Method JIT compilers share this weakness with Guile; tracing JIT runtimes like LuaJIT and PyPy largely do not. up summing So that's the thing! I was finally motivated to dust off this draft given the recent work on unboxing in a development branch of Guile. Happy hacking and we promise we'll actually make releases so that you can use it soon soon :) 2 responses 1. It’s really cool how different topics are coming together here. “this does not look like an optimization, but it enables this optimization I wrote about a few months ago” ← yay! 2. These blog posts on the innards of the guile compiler are great. It would nice to see them become part of the official guile manual.
{"url":"https://www.wingolog.org/archives/2015/10/29/type-folding-in-guile","timestamp":"2024-11-03T15:15:47Z","content_type":"text/html","content_length":"22283","record_id":"<urn:uuid:be356188-5b2a-4323-a4c3-c77cc4086553>","cc-path":"CC-MAIN-2024-46/segments/1730477027779.22/warc/CC-MAIN-20241103145859-20241103175859-00882.warc.gz"}
Activation Functions Activation Functions: A Key Component in Neural Networks Activation functions play a pivotal role in neural networks by imparting them with nonlinearity. These mathematical tools determine whether a neuron should be triggered based on an aggregate of the input data and a bias component. They allow the neural network to master intricate correlations between input and output data that are nonlinear in nature. There are a variety of activation functions commonly employed in neural networks such as: • Sigmoid– This function scales the input anywhere between 0 and 1, making it useful in binary classification. However, it has limitations due to a vanishing gradient issue and is thus rarely used in deep networks. • Softmax– This function converts the input into a probability distribution across numerous classifications, making it suited to multi-class classification tasks. • Tanh– This one converts the input to a value ranging between -1 and 1. It resembles the Sigmoid function in that it delivers zero-centered results. • ReLU– Credit for this Rectified Linear Unit function lies in its ability to set any negative input to zero and retains a positive input as is. Its simplicity and effectiveness make it a commonplace in deep neural networks. • Leaky ReLU– Favors ReLU but includes a modest slope for negative inputs to negate the risk of 'dead neurons' that ReLU could trigger. These activation functions have their own pros and cons, and the selection relies on the task specifics and the nature of the data in use. Integration of Activation Functions with Neural Networks Activation functions find numerous applications in sync with neural networks, namely: • Gradient-Based Optimization– These functions allow optimization methods like backpropagation to modify the weights and biases of the neural network during training. This is due to the differentiability of these functions, enabling the calculation of the gradient of the loss function about the weights and biases. • Induction of Nonlinearity– They introduce nonlinearity into the neural network, a crucial trait for learning complicated correlations between inputs and outputs. • Output Range Capping– In order to shield the network from becoming vulnerable or producing excessively high or low output, functions might cap the output range of each neuron. • Output Normalization– Functions like ReLU and batch normalization can be used to normalize the output of every layer in a neural network, promoting the training of deeper networks. The seminal role of activation functions is seen in their ability to optimize neural networks using gradient-based methods during the training phase, and their permit for learning of complex, non-linear correlations between inputs and outputs. Identity Activation Function The identity activation function is a rudimentary activation function that corresponds the input to itself. In problems requiring regression, the identity function is commonly used as it lets the network learn a linear relationship between inputs and outputs. Linear Activation Function The linear activation function, as the name indicates, transmits the input to itself with a certain weight or slope. This function is used in situations aiming for a continuous output value prediction, as it establishes a linear connection between inputs and outputs. Notwithstanding, in several real-world applications, non-linear functions are considered to represent non-linear associations between inputs and outputs.
{"url":"https://www.giskard.ai/glossary/activation-functions","timestamp":"2024-11-08T05:22:50Z","content_type":"text/html","content_length":"62970","record_id":"<urn:uuid:5cecaa7b-664f-404d-87f9-fe300760aa44>","cc-path":"CC-MAIN-2024-46/segments/1730477028025.14/warc/CC-MAIN-20241108035242-20241108065242-00682.warc.gz"}
Language & Cognitive Dynamics Laboratory - R Resources We use R for most statistical analyses. To help ourselves and others, here is a list of the R resources we find most useful. Dan's R Cheat Sheet: a 2-page summary of the functions I use the most. Cheatsheets by RStudio: info-packed 2-page guides for graphing, data wrangling, markdown, etc. Markdown Cheatsheet: a quick reference for markdown syntax. Quick-R: an excellent searchable reference guide Cookbook for R: cookbook-style how-to wiki, particularly useful for experimental psychology Matlab/R Reference guide: A handy reference for both R and Matlab (our other main analytical tool) Hadley Wickham's Style Guide ggplot2: documentation website for our main graphics tool The R graph gallery: many different graph examples with snippets of code for generating them. • Data Visualization: A practical introduction by Kieran Healy: uses ggplot2 and includes code and data sets. • Fundamentals of Data Visualization by Claus O. Wilke: focuses on principles for creating accurate, effective, and attractive visualizations; does not include code • Primers that cover the most important aspects of using R and programming with R (by the RStudio team) • For absolute beginners who want a gentle (or silly) introduction • For beginners who want a more comprehensive guide □ R Bootcamp: a tutorial developed by Jared Knowles □ R Guide: a thorough (but accessible) guide developed for people doing (personality) psychology research □ R Tutorial (by Kelly Black): introduction to R from basic data types to basic statistics • For intermediate users who are familiar with command line interfaces and want to learn R □ R Tutorial: covers both R and statistics, from basics to advanced methods (tip: use the "R Tutorials" navigation section on the right side) □ R Tutorial Series (Blog): a series of clear how-to guides (see contents menu in the right navigation bar) • APS guide to Learning to Work With R • Resources to help you learn and use R: from the Institute for Digital Research and Education at UCLA R Bloggers: A blog aggregator pulling together posts from over 140 bloggers who write about R in English StackOverflow - R: A great community Q & A site. If you have a problem, they can usually help you. YaRrr! A Pirate's Guide to R by Nathaniel D. Phillips (free ebook) Applied R for the quantitative social scientist by Rense Nieuwenhuis (free pdf) Statistical Inference via Data Science: A ModernDive into R and the Tidyverse by Chester Ismay and Albert Y. Kim (free ebook) R for Data Science by Hadley Wickham and Garrett Grolemund (free ebook) R in a Nutshell by Joseph Adler R for SAS and SPSS Users by Robert Muenchen Analyzing Linguistic Data by Harald Baayen ggplot2: part of the Use R! series of focused guide books Books that teach statistics and R together: Statistical Thinking for the 21st Century by Russell Poldrack (free ebook) Learning Statistics with R by Dani Navarro (ebook is free) Discovering Statistics Using R by Andy Field, Jeremy Miles, & Zoe Field
{"url":"https://sites.google.com/site/danielmirman/r-resources","timestamp":"2024-11-03T10:16:12Z","content_type":"text/html","content_length":"96011","record_id":"<urn:uuid:a5e3830e-7311-46fb-a27b-a5612e6d7325>","cc-path":"CC-MAIN-2024-46/segments/1730477027774.6/warc/CC-MAIN-20241103083929-20241103113929-00221.warc.gz"}
Basketball Turnover Ratio Calculator Home » Simplify your calculations with ease. » Sports Calculators » Basketball Turnover Ratio Calculator The Basketball Turnover Ratio Calculator is a specialized tool designed to help coaches, players, and analysts measure the efficiency of ball handling in basketball games. By calculating the turnover ratio, this tool provides critical insights into how often a team or player loses possession of the ball relative to their total possessions. This metric is invaluable for assessing performance, strategizing game plans, and improving player skills in handling pressure during games. Formula of Basketball Turnover Ratio Calculator Determine the Number of Turnovers (TO): The total number of times the team or player has lost possession due to errors: Turnovers (TO) = Total turnovers committed during a game or season Determine the Number of Possessions (P): Calculating possessions is vital for understanding the context of turnovers: Possessions = Field Goals Attempted + (0.44 * Free Throws Attempted) - Offensive Rebounds + Turnovers This formula adjusts for the different ways a possession can end, providing a more accurate count. Calculate the Turnover Ratio: This final step provides the percentage of possessions that end in turnovers: Turnover Ratio = (Turnovers / Possessions) * 100 This ratio is a critical indicator of a team's efficiency and ball control under game conditions. Table for General Terms and Calculations Term Definition Turnovers (TO) The total number of times the ball is lost to the opposition due to errors. Possessions (P) The total number of times a team controls the ball during a game. Turnover Ratio The percentage of possessions that result in turnovers. Example of Basketball Turnover Ratio Calculator Consider a basketball team that wants to calculate its turnover ratio after a game where they attempted 85 field goals, shot 20 free throws, grabbed 11 offensive rebounds, and committed 15 turnovers: • Field Goals Attempted: 85 • Free Throws Attempted: 20 • Offensive Rebounds: 11 • Turnovers: 15 Using the calculator: • Possessions = 85 + (0.44 * 20) - 11 + 15 = 97.8 ≈ 98 • Turnover Ratio = (15 / 98) * 100 ≈ 15.31% This result helps the team understand that out of every 100 possessions, they are losing the ball more than 15 times, which might prompt strategies to improve ball-handling skills and decision-making under pressure. Most Common FAQs Why is the turnover ratio important in basketball? The turnover ratio is crucial because it directly affects a team's opportunities to score. Lower ratios mean more scoring opportunities and better control of the game flow. Can this calculator be used for individual players as well as teams? Absolutely, this tool is versatile and can be equally applied to individual player performances or entire team analyses. How can teams improve their turnover ratio? Improving the turnover ratio often involves enhancing ball-handling skills, making better passing decisions, and adjusting offensive strategies to minimize pressure on players prone to turnovers. Leave a Comment
{"url":"https://calculatorshub.net/sports-calculators/basketball-turnover-ratio-calculator/","timestamp":"2024-11-06T20:57:43Z","content_type":"text/html","content_length":"115231","record_id":"<urn:uuid:41a01590-f2f7-47a1-89e8-a24d8e892177>","cc-path":"CC-MAIN-2024-46/segments/1730477027942.47/warc/CC-MAIN-20241106194801-20241106224801-00087.warc.gz"}
Question: Cantor's diagonal method, CTK Exchange Subject: Cantor's diagonal method Date: Su 01.11.98 11:49 Mike Deeth In Cantor's Diagonal Proof he assumes a complete list of reals between 0 and 1 and then constructs a number that isn't listed. He then concludes something from this. :-( I think the number isn't listed because THE LIST IS NOT COMPLETE. (and never can be) It is easy to show the list isn't complete by finding the smallest number > 0 listed. If this number is divided by 10 it will still be between 0 and 1 but clearly not on the list since it is now the smallest number. Is Cantor's Diagonal Proof sound? |Reply| |Previous| |Next| |Down| |Exchange index| |Contents| |Store| Copyright © 1996-2018 Alexander Bogomolny
{"url":"https://www.cut-the-knot.org/exchange/CantorMethod.shtml","timestamp":"2024-11-10T00:18:50Z","content_type":"text/html","content_length":"11719","record_id":"<urn:uuid:ee8c994d-3186-48a2-9e75-7824915602e0>","cc-path":"CC-MAIN-2024-46/segments/1730477028164.10/warc/CC-MAIN-20241109214337-20241110004337-00801.warc.gz"}
Illustrative Mathematics The Class Trip Alignments to Content Standards: 3.OA.D.8 Mrs. Moore’s third grade class wants to go on a field trip to the science museum. • The cost of the trip is \$245. • The class can earn money by running the school store for 6 weeks. • The students can earn \$15 each week if they run the store. 1. How much more money does the third grade class still need to earn to pay for their trip? 2. Write an equation to represent this situation. IM Commentary The purpose of this instructional task is for students to solve a two-step word problem and represent the unknown quantity with a variable. This task also addresses the concept of scarcity. The students in the 3rd grade class are faced with a scarcity of time – only 6 weeks to earn enough money for the trip. They are also faced with a scarcity of money at the end of the 6 weeks. The teacher can discuss with students the definition of scarcity – not having enough resources to satisfy your wants and possible solutions to this scarcity situation. This task is part of a set collaboratively developed with Money as You Learn, an initiative of the President’s Advisory Council on Financial Capability. Integrating essential financial literacy concepts into the teaching of the Common Core State Standards can strengthen teaching of the Common Core and expose students to knowledge and skills they need to become financially capable young adults. A mapping of essential personal finance concepts and skills against the Common Core State Standards as well as additional tasks and texts will be available at http://www.moneyasyoulearn.org. The Standards for Mathematical Practice focus on the nature of the learning experiences by attending to the thinking processes and habits of mind that students need to develop in order to attain a deep and flexible understanding of mathematics. Certain tasks lend themselves to the demonstration of specific practices by students. The practices that are observable during exploration of a task depend on how instruction unfolds in the classroom. While it is possible that tasks may be connected to several practices, only one practice connection will be discussed in depth. Possible secondary practice connections may be discussed but not in the same degree of detail. This particular task helps illustrate Mathematical Practice Standard 2, Reason abstractly and quantitatively. Students make sense of quantities and how they are related in a problem situation. In the task at hand, students first have to determine how much money they still need to earn to pay for their trip. They may represent what they have earned so far in several ways (creating an area model of multiplication or by breaking apart the multiplication problem into partial products). Then, they write an equation representing the problem situation. During this problem-solving process, students periodically contextualize the problem by connecting the mathematical symbols back to the context. Thus, students build meaning for the mathematical symbols by reasoning about the problem rather than memorizing an abstract set of rules or procedures. Problems that begin with a context and are represented with mathematical objects or symbols are also examples of modeling with mathematics (MP.4). 1. We can start by finding out how much money the students can make at the store: $$6\times15 = 6\times 10 + 6\times 5 = 60 + 30 = 90$$ Since $$245 - 90 = 155$$ the students still need \$155 dollars for the field trip. 2. We can let $n$ stand for the amount of money they still need. We know that the amount they can make at the store is $6\times 15$ and the amount they need to raise is 245, so one equation is $$245 - 6\times 15 = n$$ Another possible equation is $$6\times15+n = 245$$ The Class Trip Mrs. Moore’s third grade class wants to go on a field trip to the science museum. • The cost of the trip is \$245. • The class can earn money by running the school store for 6 weeks. • The students can earn \$15 each week if they run the store. 1. How much more money does the third grade class still need to earn to pay for their trip? 2. Write an equation to represent this situation.
{"url":"https://tasks.illustrativemathematics.org/content-standards/3/OA/D/8/tasks/1301","timestamp":"2024-11-08T06:01:18Z","content_type":"text/html","content_length":"28410","record_id":"<urn:uuid:9a2d9adf-d5da-479c-b087-c52ed886d97f>","cc-path":"CC-MAIN-2024-46/segments/1730477028025.14/warc/CC-MAIN-20241108035242-20241108065242-00723.warc.gz"}
If I=[1001] and E=[0010] then show that (aI+bE)3... | Filo Question asked by Filo student E-MATHS-IA If and then show that Not the question you're searching for? + Ask your question Video solutions (1) Learn from their 1-to-1 discussion with Filo tutors. 7 mins Uploaded on: 1/17/2024 Was this solution helpful? Found 8 tutors discussing this question Discuss this question LIVE for FREE 11 mins ago One destination to cover all your homework and assignment needs Learn Practice Revision Succeed Instant 1:1 help, 24x7 60, 000+ Expert tutors Textbook solutions Big idea maths, McGraw-Hill Education etc Essay review Get expert feedback on your essay Schedule classes High dosage tutoring from Dedicated 3 experts Practice more questions on Matrices and Determinant View more Students who ask this question also asked View more Stuck on the question or explanation? Connect with our Mathematics tutors online and get step by step solution of this question. 231 students are taking LIVE classes Question Text E-MATHS-IA If and then show that Updated On Jan 17, 2024 Topic Matrices and Determinant Subject Mathematics Class Class 11 Answer Type Video solution: 1 Upvotes 55 Avg. Video Duration 7 min
{"url":"https://askfilo.com/user-question-answers-mathematics/e-maths-ia-if-and-then-show-that-36373230363138","timestamp":"2024-11-14T16:52:30Z","content_type":"text/html","content_length":"289675","record_id":"<urn:uuid:2b7084d6-dd56-44a5-8fe5-334fd3b71799>","cc-path":"CC-MAIN-2024-46/segments/1730477393980.94/warc/CC-MAIN-20241114162350-20241114192350-00889.warc.gz"}
Comments on NumberADay: 137Other properties of the number 137: 137 is sum of ... tag:blogger.com,1999:blog-1050326564532544980.post324634000058916975..comments2024-10-15T00:19:20.511-04:00Mathematical Association of Americahttp://www.blogger.com/profile/ 10559021045290192742noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-1050326564532544980.post-34803476378030859452010-07-19T12:30:20.107-04:002010-07-19T12:30:20.107-04:00Other properties of the number 137:<br />137 is sum of five consecutive primes 83+89+97 = 131+137<br />137 index of a Fibonacci primes, <br />A001605<br />137 is a cousin of Eisensteins to be of the form 3*46-1, A003627 <br />137 is a cousin of Fermat because that 137 =4*34+1.It is also a Gaussian integer<br />137 is the 5th harmonic number, Hn = Sum[1/k,k,1,infinity]=1,3/2,11/6,25/12,137/60<br />137 is a primal number can be obtained by permutations of all or some of its digits, and http://primes.utm.edu/glossary <br />A072857<br />137 is the 4th number of Stern, <br />A042978<br />137 is the value of the number called the fine structure constant. This constant describes the probability of an electron will emit or absorb a photon.<br />Curiosity: 137^2=18769,133^2 = 17 689, 281^2 =78961 and 286^2= 81796, four squares with the same digits in the solution.<br />Rafael Parra de BarcelonaAnonymousnoreply@blogger.com
{"url":"https://maanumberaday.blogspot.com/feeds/324634000058916975/comments/default","timestamp":"2024-11-03T02:44:16Z","content_type":"application/atom+xml","content_length":"4634","record_id":"<urn:uuid:9b12144e-be5a-4452-b9d4-bcd0df960c94>","cc-path":"CC-MAIN-2024-46/segments/1730477027770.74/warc/CC-MAIN-20241103022018-20241103052018-00554.warc.gz"}
The Stacks project Lemma 15.74.9. Let $A \to B$ be a ring map. Let $K^\bullet $ be a perfect complex of $A$-modules. Then $K^\bullet \otimes _ A^{\mathbf{L}} B$ is a perfect complex of $B$-modules. Proof. Using Lemma 15.74.2 this translates into the corresponding results for pseudo-coherent modules and modules of finite tor dimension. See Lemma 15.66.13 and Lemma 15.64.12 for those results. $\ Your email address will not be published. Required fields are marked. In your comment you can use Markdown and LaTeX style mathematics (enclose it like $\pi$). A preview option is available if you wish to see how it works out (just click on the eye in the toolbar). All contributions are licensed under the GNU Free Documentation License. In order to prevent bots from posting comments, we would like you to prove that you are human. You can do this by filling in the name of the current tag in the following input field. As a reminder, this is tag 066W. Beware of the difference between the letter 'O' and the digit '0'. The tag you filled in for the captcha is wrong. You need to write 066W, in case you are confused.
{"url":"https://stacks.math.columbia.edu/tag/066W","timestamp":"2024-11-14T13:27:18Z","content_type":"text/html","content_length":"14443","record_id":"<urn:uuid:22422a4b-2f1c-4731-b57a-53c0a97651ec>","cc-path":"CC-MAIN-2024-46/segments/1730477028657.76/warc/CC-MAIN-20241114130448-20241114160448-00578.warc.gz"}
JavaScript Program to Check Prime Number A prime number is a positive integer that is only divisible by 1 and itself. For example, 2, 3, 5, 7, 11 are the first few prime numbers. Example: Check Prime Number // program to check if a number is prime or not // take input from the user const number = parseInt(prompt("Enter a positive number: ")); let isPrime = true; // check if number is equal to 1 if (number === 1) { console.log("1 is neither prime nor composite number."); // check if number is greater than 1 else if (number > 1) { // looping through 2 to number/2 for (let i = 2; i <= number/2; i++) { if (number % i == 0) { isPrime = false; if (isPrime) { console.log(`${number} is a prime number`); } else { console.log(`${number} is a not prime number`); // check if number is less than 1 else { console.log("The number is not a prime number."); Enter a positive number: 23 23 is a prime number. In the above program, the user is prompted to enter a number. The number entered by the user is checked if it is greater than 1 using if...else if... else statement. • 1 is considered neither prime nor composite. • All negative numbers are excluded because prime numbers are positive. • Numbers greater than 1 are tested using a for loop. The for loop is used to iterate through the positive numbers to check if the number entered by the user is divisible by positive numbers (2 to user-entered number divided by 2). The condition number % i == 0 checks if the number is divisible by numbers other than 1 and itself. • If the remainder value is evaluated to 0, that number is not a prime number. • The isPrime variable is used to store a boolean value: either true or false. • The isPrime variable is set to false if the number is not a prime number. • The isPrime variable remains true if the number is a prime number. Also Read:
{"url":"https://www.programiz.com/javascript/examples/prime-number","timestamp":"2024-11-11T07:28:35Z","content_type":"text/html","content_length":"158852","record_id":"<urn:uuid:a11b1b0e-5854-4e6a-800f-908ca7cb0ae0>","cc-path":"CC-MAIN-2024-46/segments/1730477028220.42/warc/CC-MAIN-20241111060327-20241111090327-00889.warc.gz"}
One-Bit Spectrum Sensing Peculiarities One-Bit Spectrum Sensing Peculiarities spectrum sensing, spurious spectral components, one-bit quantization, radio monitoring, dynamic range The use of autonomous sensors for analyzing wide bands of radio frequency spectrum requires using the high-speed analog-to-digital converters (ADC). The operation of such devices with high bit resolution requires several watts of power. One-bit ADC will reduce power consumption and simplify the analog part of the receiver. The goal of the research is to determine the features and conditions under which it is advisable to analyze the radio frequency spectrum in a sophisticated radio electronic environment using a one-bit ADC. The problem of using such ADC is the deterioration of the signal-to-noise ratio (SNR) and the occurrence of spurious spectral components at its’ medium and high values. The task of spectrum sensing is complicated by the fact that a large number of signals with different values of bandwidth and SNR can be simultaneously present in the analyzed frequency band. After a one-bit ADC, the energy of the complex signal remains unchanged regardless of its shape. Therefore, at high SNR values, the signal energy flows into spurious spectral components. When the bandwidth of the signal increases to 30% of analyzed frequency range and higher, the spurious spectral components are transformed into a broadband pedestal. Analytical dependencies have been obtained that allow us to calculate the values of the SNR at which signals can be detected and spurious spectral components appear. These dependencies take into account the parameters of the Welch periodogram and the level of spectrum occupancy. The results of the analysis of real signals showed that the shape of the signal spectrum after one-bit ADC repeats the shape of the spectrum after an 8-bit ADC, except for the absence of some weak signals. The cost for simplified hardware implementation and lower power consumption when using one-bit ADC compared to ADC with higher resolution is a narrowing of the dynamic range of signals. The use of one-bit ADC for spectrum sensing will be justified only when there is information on the analyzed bandwidth occupancy and the SNR of the signals that can be observed. Zayyani H., Haddadi F., Korki M. (2020). One-Bit Spectrum Sensing in Cognitive Radio Sensor Networks. Circuits Syst Signal Process, Vol. 39, pp. 2730–2743, doi: 10.1007/s00034-019-01274-z. Deng J., Chen Y. (2019). Subspace-based 1-bit Wideband Spectrum Sensing. 11th International Conference on Wireless Communications and Signal Processing (WCSP), pp. 1-6, doi: 10.1109/ Mishali M., Eldar Y. C., Dounaevsky O., and Shoshan E. (2011). Sampling: Analog to digital at sub-Nyquist rates. IET Circuits, Devices & Systems, Vol. 5, Iss. 1, pp. 8–20, doi: 10.1049/ Rivet F., Deval Y., Begueret J.-B., Dallet D., Cathelin P. and Belot D. (2010). The Experimental Demonstration of a SASP-Based Full Software Radio Receiver. IEEE Journal of Solid-State Circuits, Vol. 45, Iss. 5, pp. 979-988, doi: 10.1109/JSSC.2010.2041402. Graf O., Bhandari A. and Krahmer F. (2019). One-bit Unlimited Sampling. ICASSP 2019-2019 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP), pp. 5102-5106, doi: 10.1109 Mollén C., Choi J., Larsson E. G. and Heath R. W. (2017). Uplink Performance of Wideband Massive MIMO With One-Bit ADCs. IEEE Transactions on Wireless Communications, Vol. 16, Iss. 1, pp. 87-100, doi: 10.1109/TWC.2016.2619343. Jacobsson S., Durisi G., Coldrey M., Gustavsson U. and Studer C. (2015). One-bit massive MIMO: Channel estimation and high-order modulations. IEEE International Conference on Communication Workshop (ICCW), pp. 1304-1309, doi: 10.1109/ICCW.2015.7247358. Jacobsson S., Durisi G., Coldrey M., Gustavsson U. and Studer C. (2017). Throughput Analysis of Massive MIMO Uplink With Low-Resolution ADCs. IEEE Transactions on Wireless Communications, Vol. 16, Iss. 6, pp. 4038-4051, doi: 10.1109/TWC.2017.2691318. Li Y., Tao C., Seco-Granados G., Mezghani A., Swindlehurst A. L. and Liu L. (2017). Channel Estimation and Performance Analysis of One-Bit Massive MIMO Systems. IEEE Transactions on Signal Processing , Vol. 65, Iss. 15, pp. 4075-4089, doi: 10.1109/TSP.2017.2706179. Desset C., Van der Perre L. (2015). Validation of low-accuracy quantization in massive MIMO and constellation EVM analysis. European Conference on Networks and Communications (EuCNC), pp. 21-25, doi: Sarajlić M., Liu L. and Edfors O. (2017). When Are Low Resolution ADCs Energy Efficient in Massive MIMO? IEEE Access, Vol. 5, pp. 14837-14853, doi: 10.1109/ACCESS.2017.2731420. Verenzuela D., Björnson E. and Matthaiou M. (2017). Per-antenna hardware optimization and mixed resolution ADCs in uplink massive MIMO. 51st Asilomar Conference on Signals, Systems, and Computers, Pacific Grove, pp. 27-31, doi: 10.1109/ACSSC.2017.8335129. Nguyen L. V., Swindlehurst A. L. and Nguyen D. H. N. (2021). SVM-Based Channel Estimation and Data Detection for One-Bit Massive MIMO Systems. IEEE Transactions on Signal Processing, Vol. 69, pp. 2086-2099, doi: 10.1109/TSP.2021.3068629. Dong Y., Wang H. and Yao Y.-D. (2021). Channel Estimation for One-Bit Multiuser Massive MIMO Using Conditional GAN. (2021). IEEE Communications Letters, Vol. 25, Iss. 3, pp. 854-858, doi: 10.1109/ Shao M., Li Q., Ma W.-K. and So A. M.-C. (2019). A Framework for One-Bit and Constant-Envelope Precoding Over Multiuser Massive MISO Channels. IEEE Transactions on Signal Processing, Vol. 67, Iss. 20, pp. 5309-5324, doi: 10.1109/TSP.2019.2937280. Liu J., Luo Z. and Xiong X. (2019). Low-Resolution ADCs for Wireless Communication: A Comprehensive Survey. IEEE Access, Vol. 7, pp. 91291-91324, doi: 10.1109/ACCESS.2019.2927891. Shi B. et al. (2022). Impact of Low-Resolution ADC on DOA Estimation Performance for Massive MIMO Receive Array. IEEE Systems Journal, Vol. 16, Iss. 2, pp. 2635-2638, doi: 10.1109/JSYST.2021.3139449. Ameri A., Bose A., Li J. and Soltanalian M. (2019). One-Bit Radar Processing With Time-Varying Sampling Thresholds. IEEE Transactions on Signal Processing, Vol. 67, Iss. 20, pp. 5297-5308, doi: Jin B., Zhu J., Wu Q., Zhang Y. and Xu Z. (2020). One-Bit LFMCW Radar: Spectrum Analysis and Target Detection. IEEE Transactions on Aerospace and Electronic Systems, Vol. 56, Iss. 4, pp. 2732-2750, doi: 10.1109/TAES.2020.2978374. Xiao Y.-H., Ramírez D., Schreier P. J., Qian C. and Huang L. (2022). One-Bit Target Detection in Collocated MIMO Radar and Performance Degradation Analysis. IEEE Transactions on Vehicular Technology, Vol. 71, Iss. 9, pp. 9363-9374, doi: 10.1109/TVT.2022.3178285. Wu P.-W. et al. (2023). One-Bit Spectrum Sensing for Cognitive Radio. Electrical Engineering and Systems Science-Signal Processing, 13 p. doi: 10.48550/arXiv.2306.13558. Zhao Y., Ke X., Zhao B., Xiao Y. and Huang L. (2021). One-Bit Spectrum Sensing Based on Statistical Covariances: Eigenvalue Moment Ratio Approach. IEEE Wireless Communications Letters, Vol. 10, Iss. 11, pp. 2474-2478, doi: 10.1109/LWC.2021.3104346. Mehanna O., Sidiropoulos N. D. (2013). Frugal Sensing: Wideband Power Spectrum Sensing From Few Bits. IEEE Transactions on Signal Processing, Vol. 61, Iss. 10, pp. 2693-2703, doi: 10.1109/ Ali A., Hamouda W. (2016). Low Power Wideband Sensing for One-Bit Quantized Cognitive Radio Systems. IEEE Wireless Communications Letters, Vol. 5, Iss. 1, pp. 16-19, doi: 10.1109/LWC.2015.2487347. Merlano-Duncan J. C., Sharma S. K., Chatzinotas S., Ottersten B. and Wang X. (2017). Multi-antenna based one-bit spatio-temporal wideband sensing for cognitive radio networks. IEEE International Conference on Communications (ICC), pp. 1-7, doi: 10.1109/ICC.2017.7996737. Nguyen L. L., Nguyen D. H. N., Fiche A., Huynh T. and Gautier R. (2019). Low-Bit Quantization Methods for Modulated Wideband Converter Compressed Sensing. IEEE Global Communications Conference (GLOBECOM), pp. 1-6, doi: 10.1109/GLOBECOM38437.2019.9013470. Libin M. K., Shreejith S., Vinod A. P. and Madhukumar A. S. (2020). A Power-Efficient Spectrum-Sensing Scheme Using 1-Bit Quantizer and Modified Filter Banks. IEEE Transactions on Very Large Scale Integration (VLSI) Systems, Vol. 28, Iss. 9, pp. 2074-2078, doi: 10.1109/TVLSI.2020.3009430. Chen Y., Zhao Y., Huang J., and Zheng Y. (2019). Multiband sparse signal reconstruction through direct one-bit sampling. Rev. Sci. Instrum., Vol. 90, Iss. 8, pp. 2693-2703, doi: 10.1063/1.5113925. Lyons R. G. (2011). Understanding Digital Signal Processing, 3rd ed. Prentice Hall, 858 p. Prabhu K. M. M. (2014). Window functions and their applications in signal processing. Taylor&FrancisGroup, LLC, 406 р. doi: 10.1201/9781315216386. How to Cite Buhaiov , M. V. (2024) “One-Bit Spectrum Sensing Peculiarities”, Visnyk NTUU KPI Seriia - Radiotekhnika Radioaparatobuduvannia, (97), pp. 20-29. doi: 10.20535/RADAP.2024.97.20-29. Telecommunication, navigation, radar systems, radiooptics and electroacoustics Copyright (c) 2024 Микола Вікторович Бугайов This work is licensed under a Creative Commons Attribution 4.0 International License. Authors who publish with this journal agree to the following terms: a. Authors retain copyright and grant the journal right of first publication with the work simultaneously licensed under a Creative Commons Attribution License that allows others to share the work with an acknowledgement of the work's authorship and initial publication in this journal. b. Authors are able to enter into separate, additional contractual arrangements for the non-exclusive distribution of the journal's published version of the work (e.g., post it to an institutional repository or publish it in a book), with an acknowledgement of its initial publication in this journal. c. Authors are permitted and encouraged to post their work online (e.g., in institutional repositories or on their website) prior to and during the submission process, as it can lead to productive exchanges, as well as earlier and greater citation of published work (See The Effect of Open Access).
{"url":"https://radap.kpi.ua/radiotechnique/article/view/2011","timestamp":"2024-11-11T07:58:00Z","content_type":"text/html","content_length":"58453","record_id":"<urn:uuid:afd7c4a4-89ef-4885-aff8-ee782cca98bc>","cc-path":"CC-MAIN-2024-46/segments/1730477028220.42/warc/CC-MAIN-20241111060327-20241111090327-00776.warc.gz"}
What is the smallest number which when increased by 3 is divisible by 27, 35, 25 and 21? Hint: First of all find the least common multiple of all the four numbers 27, 35, 25 and 21 given. Use the prime factorization method to find the L.C.M. First write the given numbers as the product of their prime factors one – by – one. Now, if a prime factor will be repeating then write them in exponential form. Take the product of all the different prime factors along with their highest exponent to get the L.C.M. Once the L.C.M is found, subtract 3 from it to get the answer. Complete step by step solution: Here we have been asked to find the smallest number in which we will add 3 so that it will be divisible by all the four numbers 27, 35, 25 and 21. First we need to find the L.C.M of these four Now, the smallest number that will be divisible by each of the numbers 27, 35, 25 and 21 will be their L.C.M, so let us find the L.C.M of these numbers by the prime factorization method. So, writing all the numbers as the product of their prime factors one by one we get, $\Rightarrow 27=3\times 3\times 3={{3}^{3}}$ $\Rightarrow 35=5\times 7$ $\Rightarrow 25=5\times 5={{5}^{2}}$ $\Rightarrow 21=3\times 7$ Therefore, the L.C.M will be the product of all the different prime factors with their highest exponent, so we get, $\Rightarrow $ L.C.M = ${{3}^{3}}\times {{5}^{2}}\times 7$ $\Rightarrow $ L.C.M = 4725 Now, assuming the number in which we will add 3 so that it will be divisible by all the four numbers as x, we must have the mathematical condition given as: - & \Rightarrow x+3=4725 \\ & \Rightarrow x=4725-3 \\ & \therefore x=4722 \\ So, the correct answer is “Option b”. Note: Note that if we are asked to calculate the highest number which can divide all the given four numbers then in that case we take the H.C.F of the given numbers. So you must not get confused while solving the question. Do not consider the obtained L.C.M as the answer because we need to subtract 3 from the L.C.M to get the answer as per the question.
{"url":"https://www.vedantu.com/question-answer/smallest-number-which-when-increased-by-class-9-maths-cbse-60a6d98bfe5fac7451160ddc","timestamp":"2024-11-03T00:57:30Z","content_type":"text/html","content_length":"155696","record_id":"<urn:uuid:404c75e6-c5bb-462d-be59-7d194637c4c6>","cc-path":"CC-MAIN-2024-46/segments/1730477027768.43/warc/CC-MAIN-20241102231001-20241103021001-00650.warc.gz"}
An offline fault diagnosis method for planetary gearbox based on empirical mode decomposition and adaptive multi-scale morphological gradient filter Planetary gearbox is increasingly used in many kinds of rotary machinery in recent years. Due to the specialty of its structure, fault diagnosis for planetary gearbox is very difficult compared with the fixed shaft gearbox. This paper proposed an offline fault diagnosis method for planetary gearbox based on empirical mode decomposition and adaptive multi-scale morphological gradient filter. Firstly, the framework of the method proposed in this paper was introduced. Then, experimental data and industrial data were utilized to validate the effectiveness of the method. And the combination of empirical mode decomposition and adaptive multi-scale morphological dilation-erosion gradient filter was found very suitable to be used in the planetary gearbox fault diagnosis compared with other five filters. The proposed method was demonstrated to be of good performance on both extracting faults characteristic frequency and de-noising. 1. Introduction Planetary gearbox plays an important role in large and complicated mechanical equipments, such as wind turbine, helicopter and crane, etc. And the working environment of these equipments is often very terrible. The main components of single stage planetary gearbox usually consist of one sun gear, one ring gear, one planet carrier and three or four planet gears. The structure of planetary gearbox is compact relatively, so it can achieve larger transmission ratio compared with fixed shaft gearbox. However, the terrible environment and the special structure also make it very difficult to assess the health condition of the planetary gearbox [1]. Therefore, efficient and accurate fault diagnosis methods for planetary gearbox are particularly needed to prevent the equipments from The fault diagnosis methods for planetary gearbox can be divided into two types, online and offline. The online method is implementing condition monitoring on machinery through some instruments. It is often an automatic method due to the instruments can give out the indicator whether the machine can run continually or not. The offline method is analyzing the data acquire from the equipments to assess the health condition of the planetary gearbox. It is usually an artificial method. In the general, the two kinds of methods can be utilized together to improve the performance of fault The procedure of the online and a part of intelligent offline fault diagnosis methods is made up by data acquisition, data processing, features extraction and fault diagnosis. In this aspect, major efforts have been made by many researchers and organizations. Feng and Zuo et al [2-4] developed a series of experiments using a two-stage planetary gearbox test-rig with considering different fault modes, fault levels and working conditions. They acquired a great deal of vibration data. Some documents [5-7] introduced the common fault features in detail, such as maximum, minimum, kurtosis and energy, etc. In addition, the data processing techniques that utilized to extract the features were also explained by them. A variety of intelligent fault diagnosis methods have been proposed, such as Support Vector Machine (SVM) [8, 9], Wavelet Packet Transformation (WPT) [10, 11] and Hidden Markov Model (HMM) [12-14]. Empirical Mode Decomposition (EMD) [15-18], as the kernel of Hilbert-Huang Transform (HHT), was popular in recent years due to its good performance on signal processing and mechanical fault diagnosis. Anyway, the intelligent methods are often limited in a settled condition and would not work to all cases. Therefore, artificial methods are needed to cooperate with the intelligent ones. Frequency spectrum analysis [19] is one of the most useful means and it is based on the amplitude changing of fault characteristic frequency. In recent years, some researchers introduced mathematical morphology (MM) [20-23] to fault diagnosis field. Fault frequencies of gears can be extracted more explicit using mathematical morphological filters than traditional frequency analysis method. However, the structure of planetary gearbox is very complicate and many different feature frequencies can be depicted in its frequency spectrum. The amplitudes of ring gear and planet gears fault frequencies are very low. In addition, noise interference is very serious because of the awful working condition. So, it is still a challenge to assess the health condition of planetary gearbox. In this study, an offline fault diagnosis method for planetary gearbox based on EMD and adaptive multi-scale morphological gradient filter (AMMGF) is presented. Due to the minor changes of fault frequencies will be very obvious after processed by EMD and researches have demonstrated better result can be obtained when utilized EMD. In addition, AMMGF is the most effective filter than other morphological filters. The remaining sections of this paper are organized as follows. Section 2 introduces the proposed planetary gearbox fault diagnosis method in details. Section 3 and Section 4 validates the effectiveness of the proposed method using experimental data and industrial data, respectively. Finally, the conclusions were drawn in Section 5. 2. The proposed offline fault diagnosis method for planetary gearbox 2.1. Brief review on MM EMD is a popular method and its fundamental theory can be found in documents [15-17]. Thus, the theory of EMD will be not introduced in details and only the specific application will be illustrated in this paper. Mathematical morphology (MM) was originally put forward by Matheron and Serra as an image processing method in 1964. In recent years, it was applied in fault diagnosis field as a vibration signal processing technique and even embedded in some software. For example, there are ready-made program of MM in MATLAB. The application of it in fault diagnosis field is to modify the shape of a signal by pre-defined structuring element so that to achieve the aim of extracting faults characteristic frequency and de-noising. Generally, two kinds of basic algorithms are defined by MM: dilation and erosion, as follows. Assume that $A$ is an image and $B$ is the structuring element, then $B$ dilates or erodes $A$ can be defined as $A\oplus B$ and $A\odot B$, respectively: $A\oplus B=\left\{x\in {E}^{n}\left|x=a+b,a\in A,b\in B\right\right\}=\bigcup _{b\in B}{A}_{b},$ $A\odot B=\left\{x\in {E}^{n}\left|x+b\in A,b\in B\right\right\}=\bigcup _{b\in B}{A}_{-b},$ where ${A}_{b}$ and ${A}_{-b}$ are defined as: ${A}_{b}=\left\{a+b\left|a\in A\right\right\},{A}_{-b}=\left\{a-b\left|a\in A\right\right\}.$ The processed results of dilation and erosion can be illustrated visually by Fig. 1. As the figure shows, when the structuring element moves by the outside of an image, the area encircled by the center of the structuring element is the dilated result. Similarly, when the structuring element moves by the inside of an image, the area encircled by the center of the structuring element is the eroded result. The two results can be illustrated more visually by Fig. 2. Fig. 1The processed results of dilation and erosion Fig. 2Dilation and erosion of image: a) original image; b) dilation of a); c) erosion of a) In general, as the fundamental algorithms of MM, the processed results of dilation and erosion cannot be recovered. Namely, the original image cannot be obtained through dilation and then erosion or the contrary order. However, the two orders are another two important algorithms of MM, opening and closing, and they can be denoted as $A\circ B$ and $A•B$: $A\circ B=A\odot B\oplus B,$ Fig. 3The processed results of opening and closing Fig. 4Opening and closing of image: a) original image; b) opening of a); c) closing of a) Similarly, the processed results of opening and closing can be illustrated by Fig. 3. It can be seen from the figure that opening and closing have the function of smooth edge and fill leak, respectively. The two results can be illustrated more visually by Fig. 4. 2.2. The application of MMFs in mechanical fault diagnosis Section 2.1 introduced the fundamental theory of MM and its application on image processing. In recent years, mathematical morphological filters (MMFs) which mainly constituted by dilation, erosion, opening and closing were applied in fault diagnosis field. Assume that $f\left(n\right)$, $F=\left\{0,1,2,\dots ,N-1\right\}$ and $g\left(m\right)$, $G=\left\{0,1,2,\dots ,M-1\right\}$$\left(N\gg M\right)$ are input signal and structuring element, respectively. Then four basic MMFs can be defined as: $\left(f\oplus g\right)\left(n\right)=\underset{m\in G}{\mathrm{m}\mathrm{a}\mathrm{x}}\left\{f\left(n-m\right)+g\left(m\right)\right\},$ $\left(f\odot g\right)\left(n\right)=\underset{m\in G}{\mathrm{m}\mathrm{i}\mathrm{n}}\left\{f\left(n+m\right)-g\left(m\right)\right\},$ $\left(f\circ g\right)\left(n\right)=\left(f\odot g\oplus g\right)\left(n\right),$ $\left(f•g\right)\left(n\right)=\left(f\oplus g\odot g\right)\left(n\right),$ where $\oplus$, $\odot$, $\circ$ and $•$ mean dilation, erosion, opening and closing filter, respectively. The functions of the four basic MMFs are different though all of them can extract the faults characteristic frequency from vibration signal. Fig. 5 shows the differences of them. The analyzed signal is a standard sine signal $x=\mathrm{s}\mathrm{i}\mathrm{n}2\pi t$ and the sampling frequency is 100 Hz. As the figure shows, dilation filter can control the plus pulse and sharp the minus pulse but erosion filter is just the reverse. Opening filter can eliminate the plus pulse and keep the minus pulse but closing filter is the reverse. Fig. 5Results of the four basic MMFs (- - - Original signal, — Results of the four basic MMFs) a) Result of dilation filter b) Result of erosion filter c) Result of opening filter d) Result of closing filter As described above, each of the four MMFs could only extract one of the plus or minus pulse information from vibration signal. However, the general condition in practical is that plus and minus pulse are needed at the same time. Therefore, the combination of the two of them were proposed and named morphological gradient filters (MGFs). Two MGFs, morphological dilation-erosion gradient filter (MGF [DE]) and morphological closing-opening gradient filter (MGF[CO]), can be defined as follows: $MG{F}_{DE}\left(f\right)\left(n\right)=f\oplus g\left(n\right)-f\odot g\left(n\right),n=0,1,2,...,N-1,$ $MG{F}_{CO}\left(f\right)\left(n\right)=f•g\left(n\right)-f\circ g\left(n\right),n=0,1,2,...,N-1.$ MGFs can take into account both plus and minus pulse. In addition, they can also extract the wanted faults characteristic frequency and weaken the interference of other signal. 2.3. The proposed method based on EMD and AMMGF In general, different results will be obtained when selecting different structuring elements. Small-scale structuring element can keep the specific characteristic of pulse signal while big-scale structuring element can weaken the interference of noise signal. Therefore, selecting a suitable structuring element is very important. This paper proposed the adaptive multi-scale morphological gradient filter (AMMGF). Assume that $X$ is dispersed signal, $T$ is morphological transform, and then the AMMGF can be summarized as the set of $\left\{{T}_{\lambda }\left|\lambda >0,\lambda \in Z\right\right\}$. Where ${T} _{\lambda }$ was defined as: ${T}_{\lambda }\left(X\right)=\lambda T\left(\frac{X}{\lambda }\right).$ Similarily, assume that $f\left(n\right)\text{,}$$F=\left\{0,1,2,\dots ,N-1\right\}$ and $g\left(m\right)\text{,}$$G=\left\{0,1,2,\dots ,M-1\right\}$$\left(N\gg M\right)$ are input signal and structuring element, respectively. Then the multi-scale morphological dilation and erosion filters can be defined as: ${\left(f\oplus g\right)}_{\lambda }\left(n\right)=\lambda \left(f/\lambda \oplus g\right)\left(n\right)=f\oplus \lambda g\left(n\right),n=0,1,2,\dots ,N-1,$ ${\left(f\odot g\right)}_{\lambda }\left(n\right)=\lambda \left(f/\lambda \odot g\right)\left(n\right)=f\odot \lambda g\left(n\right),n=0,1,2,\dots ,N-1,$ where $\lambda g$ is the structuring element which its scale is $\lambda$. It can be obtained through dilate $g$ by $n-1$ times, as follows: $\lambda g=\underset{\lambda -1}{\underset{⏟}{g\oplus g\oplus \cdots \oplus g}}.$ At the same time, the algorithems also can be applied to the followed equations: $f\oplus \left(\lambda +1\right)g\left(n\right)=\left(f\oplus \lambda g\right)\oplus g\left(n\right),n=0,1,2,\dots ,N-1,$ $f\odot \left(\lambda +1\right)g\left(n\right)=\left(f\odot \lambda g\right)\odot g\left(n\right),n=0,1,2,\dots ,N-1.$ The multi-scale morphological opening, closing and two gradient filters can be obtained according to the above equations, as follows: ${\left(f\circ g\right)}_{\lambda }\left(n\right)=\left(f\odot \lambda g\right)\oplus \lambda g\left(n\right),n=0,1,2,\dots ,N-1,$ ${\left(f•g\right)}_{\lambda }\left(n\right)=\left(f\oplus \lambda g\right)\odot \lambda g\left(n\right),n=0,1,2,\dots ,N-1,$ $MG{F}_{DE}{\left(f\right)}_{\lambda }\left(n\right)=f\oplus \lambda g\left(n\right)-f\odot \lambda g\left(n\right),n=0,1,2,\dots ,N-1,$ $MG{F}_{OC}{\left(f\right)}_{\lambda }\left(n\right)=f•\lambda g\left(n\right)-f\circ \lambda g\left(n\right),n=0,1,2,\dots ,N-1.$ Many multi-scale morphological transformed results would be getted through the method described above. Then how to merge them together is another problem. This paper utilized weighting and average Assume that the set of all scales of structuring elements is $\lambda =\left\{{\lambda }_{1},{\lambda }_{2},\dots ,{\lambda }_{J}\right\}$, the obatined $J$ filtered results are $f{g}_{j}\left(n\ right)$, $j=1,2,\dots ,\text{}J$, $n=0,1,2,\dots ,N-1$. Then the results can be merged using weighting and averaging method, as follows: $fg\left(n\right)=\sum _{j=1}^{J}{w}_{j}\cdot f{g}_{j}\left(n\right),$ where ${w}_{j}={\lambda }_{j}/\sum _{j=1}^{J}{\lambda }_{J}$, $j=1,2,\dots ,J$. Theoretically, the above AMMGF can both keep the specific characteristic of pulse signal and weaken the interference of noise signal. However, due to the specialty of planetary gearbox, the practical application result was unsatisfactory. Therefore, this paper proposed the fault diagnosis method for planetary gearbox based on EMD and AMMGF. The framework of the method proposed in this paper can be seen in Fig. 6. And the specific procedures can be described as follows. 1. Decomposing the original signal using EMD and getting $I$ IMFs. 2. Processing every IMF using AMMGF and obtaining a lot of processed results with different scales of structuring elements. 3. Merging the processed result of each IMF using weighting and averaging method, getting AMMGF results of $I$ IMFs. 4. Calculating the mean value of the $I$ results as the final signal. 5. Analyzing the frequency spectrum of the final signal and assessing the health condition of planetary gearbox. Fig. 6Framework of the method proposed in this paper 3. Example 1: experimental data analysis 3.1. Data specifications The data this example used to validate the effectiveness of the method proposed in this paper was acquired from a seeded failure experiment of planetary gearbox. The experimental system consists of a tested planetary gearbox, a drive motor, a speed and torque sensor and a magnetic powder brake. And between the two of them is connected by coupling, as shown in Fig. 7. Four accelerometers were mounted on the tested planetary gearbox, wherein the first one and the second one are mounted on the input side of the gearbox, the third one is on the top of the casing and the fourth is fixed on the output side. The specific location of every accelerometer is as depicted in Fig. 8(a). Fig. 8(b) is the structure of the planetary gearbox. And the configuration parameters of the planetary gearbox can be seen in Table 1. Table 1Planetary gearbox configuration parameters Gear Sun gear Planet gear (number) Ring gear Number of teeth 13 64 (3) 146 Fig. 7Planetary gearbox experimental system Fig. 8a) Mounted location of each accelerometer; b) structure of the tested planetary gearbox The wear failure is seeded on one tooth of sun gear, planet gear and ring gear, respectively. The specific failures are as Fig. 9 shows. The sampling frequency of this experimental system is 20 kHz, and 240000 points of data are recorded for every signal. The input shaft, shaft 1, was driven by the motor at three kinds of rotary speed, 400, 800 and 1200 rpm. To each speed, four different kinds of loads are implemented. They are 0, 0.4, 0.8 and 1.2 Nm, respectively. Fig. 9Seeded wear failure of every gear: a) sun gear; b) planet gear; c) ring gear 3.2. Results analysis According to the configuration parameters of the planetary gearbox, the relevant fault characteristic frequencies can be obtained. The specific values of the frequencies can be seen in Table 2. Firstly, the sun gear fault signal which acquired from the working condition of 1200 rpm and 1.2 Nm was analyzed to validate the effectiveness of the method proposed in this paper. The fault characteristic frequency of the sun gear in this condition is 55.093 Hz. The vibration signal acquired by Accelerometer 3 was utilized in this example. Table 2Fault characteristic frequencies of every gear Rotary speed (rpm) Meshing frequency (Hz) Fault frequency of sun gear (Hz) Fault frequency of planet gear (Hz) Fault frequency of ring gear (Hz) 400 79.579 18.364 1.243 1.635 800 159.158 36.729 2.487 3.270 1200 238.738 55.093 3.730 4.906 Fig. 10 shows the waveform in time domain and frequency spectrum of the sun gear fault signal. The 1 and 2 times of sun gear fault characteristic frequency can be seen in the figure, but the amplitudes of them are much lower compared to the disturbed signal’s. Therefore, the health condition of the sun gear cannot be assessed. Fig. 10Sun gear fault signal: a) waveform in time domain; b) frequency spectrum Fig. 11 shows the envelope analysis results of the sun gear fault signal. It can be seen from the figure that the 1, 2 and 3 times of sun gear fault characteristic frequency are very obvious. However, the noise interference is also very serious. The results are obtained based on the seeded wear failure experiment which the damage is a little severe. If the failure is slight or initial in the industrial machine, the result will be not so obvious. Therefore, it is needed to study a more effect method. Fig. 11Envelope analysis of sun gear fault signal: a) waveform in time domain; b) frequency spectrum Figs. 12-17 show the results of the six kinds of filters. Figs. 12-15 are the results of the four basic filters. They are dilation, erosion, opening and closing. Figs. 16-17 are the results of MGF [DE] and MGF[CO]. In the six figures, the bold and thin lines in (a) are the results of filters and the waveform in time domain of original signal, respectively. It can be seen from Figs. 12-15 that the results of the four basic filters are similar. All of the four basic filters can extract 1, 2 and 3 times of the sun gear fault characteristic frequency. However, the amplitudes of these frequencies have not increased compared to the envelope analysis result. Even the amplitudes of some results have reduced, such as the results of opening and closing. In addition, the interference come from the low frequency noise is still very serious. Fig. 12Sun gear fault signal: a) result of dilation; b) frequency spectrum Fig. 13Sun gear fault signal: a) result of erosion; b) frequency spectrum Fig. 14Sun gear fault signal: a) result of opening; b) frequency spectrum Fig. 15Sun gear fault signal: a) result of closing; b) frequency spectrum Fig. 16Sun gear fault signal: a) result of MGFDE; b) frequency spectrum Fig. 16 and Fig. 17 are the results of MGF[DE] and MGF[CO]. Comparing to the results of the four basic filters, the amplitudes of the extracted sun gear fault characteristic frequency have increased obviously. This effect is significant to the frequency spectrum analysis of the signal acquired from the equipment with slight failure. Fig. 17Sun gear fault signal: a) result of MGFCO; b) frequency spectrum It also can be seen from the comparison of Fig. 16 and Fig. 17 that the effect of MGF[DE] is better than the effect of MGF[CO] due to the fault characteristic frequency amplitude extracted by MGF[DE] is higher than by MGF[CO]. In addition, another conclusion can be drawn from Eqs. (6)-(11) that MGF[DE] needs about half steps compared with MGF[CO]. Therefore, the after study is mainly implemented based on MGF[DE]. Fig. 18 shows the result of AMMGF[DE]. The 1, 2 and 3 times of the sun gear fault characteristic frequency are extracted obviously. And the amplitudes of them is much higher than the above six results. In addition, the effect of de-noising is also satisfactory. Fig. 18Sun gear fault signal: a) result of AMMGFDE; b) frequency spectrum Fig. 19 shows the result of EMD and AMMGF[DE] which is the method proposed in this paper. It can be seen from the figure that the de-noising effect is also satisfactory. More important is that the amplitudes of the extracted sun gear fault characteristic frequencies are much higher than before. In addition, the fault characteristic frequencies of planet and ring gears are very low. So, the proposed method is more suited to planetary gearbox fault diagnosis. Fig. 19Sun gear fault signal: a) result of EMD and AMMGFDE; b) frequency spectrum Fig. 20 and Fig. 21 are the results of EMD and AMMGF[DE] that applied to planet and ring gears, respectively. In order to achieve the goal of considering all working conditions, the data acquired from 400 rpm and 0.4 Nm condition is utilized when analyzing planet gear signal. And when analyzing ring gear signal, the data acquired from 800 rpm and 0.8 Nm condition is used. The fault characteristic frequencies of the planet gear and ring gear in the two conditions are 1.243 and 3.270 Hz, respectively. It can be seen from Fig. 20 and Fig. 21 that the proposed method can extract the fault characteristic frequencies of the planet and ring gear obviously even though the frequencies are very low. In addition, the effect of de-noising of the proposed method is also satisfactory. Above discussion demonstrates the offline fault diagnosis method for planetary gearbox proposed in this paper can extract fault characteristic frequencies more prominent compared to the four basic filters (dilation, erosion, opening and closing) and two MGFs (MGF[DE] and MGF[CO]). And the de-noising effect of the proposed method is also satisfactory. In addition, the performance of EMD and AMMGF[DE] is the best among all of the filters. Fig. 20Planet gear fault signal: a) result of EMD and AMMGFDE; b) frequency spectrum Fig. 21Ring gear fault signal: a) result of EMD and AMMGFDE; b) frequency spectrum 4. Example 2: industrial data analysis 4.1. Data specifications In order to further validate the effectiveness of the offline fault diagnosis method for planetary gearbox proposed in this paper, industrial data acquired from a helicopter was analyzed. Planetary gearbox is a main part of helicopter engine and this is the reason why select the data acquired from helicopter to analyze. Due to the reason of secrecy, the photo of the helicopter and the inside structure of planetary gearbox cannot be showed here. But the configuration parameters of the planetary gearbox can be seen in Table 3. Table 3The configuration parameters of the planetary gearbox of a helicopter Gear Sun gear Planet gear (number) Ring gear Number of teeth 94 36 (10) 167 Eight accelerometers are installed on the engine of the helicopter and the first, sixth and seventh were installed on the casing of the planetary gearbox. The diagram of the specific locations can be seen in Fig. 22. The rotary speed of the sun gear shaft was 574.74 rpm and it is constant due to the speciality of helicopter. Three kinds of torque are implemeted and they are 30 %, 40 % and 50 % of the rated torque of the helicopter. The data is acquired during the flight time 600 h to 800 h. It is found through open-gearbox examination that the planetary gearbox is healthy after testing. Continue experiment has not been implemented with considering the economy and safety. Fig. 22The diagram of accelerometers installed on the casing of the planetary gearbox 4.2. Results analysis Similarily, according to the configuration parameters of the helicopter planetary gearbox, the relevant fault characteristic frequencies can be obtained. The specific values of the frequencies can be seen in Table 4. Table 4Fault characteristic frequencies of every gear of helicopter planetary gearbox Rotary speed (rpm) Meshing frequency (Hz) Fault frequency of sun gear (Hz) Fault frequency of planet gear (Hz) Fault frequency of ring gear (Hz) 574.74 576.15 61.29 16.00 34.50 Firstly, the frequency spectrum of the acquired signal is analyzed. This paper utilizes the vibration signal acquired by Accelerometer 1. Fig. 23 shows the frequency spectrum analysis of the vibration signal. Only the 1, 2 and 3 times of the planetary gearbox gear meshing frequency appeared. This is a normal phenomenon and it also can demonstrate the planetary gearbox is healthy. In addition, it can be seen in Fig. 23, the frequency amplitudes of some other structures are very high. If the planetary gearbox is damaged, these frequencies will disturb the fault diagnosis of it. Therefore, an effective fault diagnosis method for helicopter planetary gearbox is very important. Fig. 23Frequency spectrum analysis of the signal acquired from the helicopter planetary gearbox Generally, due to the planetary gearbox is healthy, but the effectiveness of the proposed method also can be validated if the proposed method has good performance when analyzing the data acquired from different working conditions. Therefore, the signal acquired from the three kinds of torques is analyzed in this section and the results can be seen in Fig. 24, 25 and 26. Fig. 24The signal of 30 % rated torque: a) results of EMD and AMMGFDE; b) frequency spectrum Fig. 25The signal of 40 % rated torque: a) results of EMD and AMMGFDE; b) frequency spectrum Fig. 26The signal of 50 % rated torque: a) results of EMD and AMMGFDE; b) frequency spectrum It can be seen from the three results that there is not fault characteristic frequency of any structure of the helicopter engine in each frequency spectrum. Only 1 and 2 times of the planetary gearbox meshing frequency appear in the frequency spectrum of 50 % rated torque. So, it can be assessed that the planetary gearbox is healthy. In addition, disturbed frequencies can not be seen in each frequency spectrum compared with the original analysis in Fig. 23. Therefore, the effectiveness of the method proposed in this paper can be further validated. 5. Conclusions An offline fault diagnosis method for planetary gearbox based on EMD and AMMGF was proposed in this paper. Firstly, the basic theory of MM was introduced thoroughly and the framework of the proposed method was given out. Then, experimental data and industrial data were utilized to validate the effectiveness of the method. And the combination of EMD and AMMGF[DE] was found very suitable to be used in the planetary gearbox fault diagnosis compared with other six filters (dilation, erosion, opening, closing, MGF[DE] and MGF[CO]). The results from the two kinds of data show that the proposed method has good performance on both extracting fault characteristic frequency and de-noising. Therefore, the method is very suitable for offline planetary gearbox fault diagnosis. • Lei Y. G., He Z. J., Lin J., et al. Research advances of fault diagnosis technique for planetary gearboxes. Journal of Mechanical Engineering, Vol. 47, Issue 19, 2011, p. 59-67, (in Chinese). • Feng Z. P., Zuo M. J. Vibration signal models for fault diagnosis of planetary gearboxes. Journal of Sound and Vibration, Vol. 331, 2012, p. 4919-4939. • Feng Z. P., Zuo M. J. Fault diagnosis of planetary gearboxes via torsional vibration signal analysis. Mechanical Systems and Signal Processing, Vol. 36, 2013, p. 401-421. • Liu Z. L., Qu J., Zuo M. J., Xu H. B. Fault level diagnosis for planetary gearboxes using hybrid kernel feature selection and kernel Fisher discriminant analysis. The International Journal of Advanced Manufacturing Technology, Vol. 67, 2013, p. 1217-1230. • Jardine A. K. S., Lin D. M., Banjevic D. A review on machinery diagnostics and prognostics implementing condition-based maintenance. Mechanical Systems and Signal Processing, Vol. 20, 2006, p. • Samuel P. D., Pines D. J. A review of vibration-based techniques for helicopter transmission diagnostics. Journal of Sound and Vibration, Vol. 282, 2005, p. 475-508. • Lebold M., McClintic K., Campbell R., et al. Review of vibration analysis methods for gearbox diagnostics and prognostics. Proceeding of the 54th Meeting of the Society for Machinery Failure Prevention Technology, 2000, p. 623-634. • Baccarini L. M. R., Silva V. V. R., Meneze B. R. D., et al. SVM practical industrial application for mechanical faults diagnostic. Expert Systems with Applications, Vol. 38, 2011, p. 6980-6984. • Tang X. L., Zhuang L., Cai J., et al. Multi-fault classification based on support vector machine trained by chaos particle swarm optimization. Knowledge-Based Systems, Vol. 23, 2010, p. 486-490. • Yen G. G., Leong W. F. Fault classification on vibration data with wavelet based feature selection scheme. Instrumentation, Systems, and Automation Society, Vol. 45, 2006, p. 141-151. • Fan X. F., Zuo M. J. Gearbox fault detection using Hilbert and wavelet packet transform. Mechanical Systems and Signal Processing, Vol. 20, 2006, p. 966-982. • Boutros T., Liang M. Detection and diagnosis of bearing and cutting tool faults using hidden Markov models. Mechanical Systems and Signal Processing, Vol. 25, 2011, p. 2012-2124. • Kang J. S., Zhang X. H. Application of hidden Markov models in machine fault diagnosis. Information-An International Interdispilinary Journal, Vol. 15, 2012, p. 5829-5838. • Hassiotis S. Identification of damage using natural frequencies and Markov parameters. Computers and Structures, Vol. 74, 2000, p. 365-373. • Huang N. E., Shen Z., Long S. R., et al. The empirical mode decomposition and the hilbert spectrum for nonlinear and nonstationary time series analysis. Proceeding of the Royal Society of London, Series A, Vol. 454, 1998, p. 903-995. • Li H., Zhang Y. P., Zheng H. Q. Wear detection in gear system using Hilbert-Huang transform. Journal of Mechanical Science and Technology, Vol. 20, Issue 11, 2006, p. 1781-1789. • Ricci R., Pennacchi P. Diagnostics of gear faults based on EMD and automatic selection of intrinsic mode functions. Mechanical Systems and Signal Processing, Vol. 25, Issue 3, 2011, p. 821-838. • Loutridis S. J. Damage detection in gear systems using empirical mode decomposition. Engineering Structures, Vol. 26, Issue 12, 2004, p. 1833-1841. • Blunt D. M., Keller J. A. Detection of a fatigue crack in a UH-60A planet gear carrier using vibration analysis. Mechanical Systems and Signal Processing, Vol. 20, 2006, p. 2095-2111. • Dai Q. Y., Yu Y. L. The advances of mathematical morphology in image processing. Control Theory and Applications, Vol. 18, 2001, p. 478-482, (in Chinese). • Li B., Zhang P. L., Wang Z. J., et al. Gear fault detection using multi-scale morphological filters. Measurement, Vol. 44, 2011, p. 2078-2089. • Li B., Zhang P. L., Mi S. S., et al. An adaptive morphological gradient lifting wavelet for detecting bearing defects. Mechanical Systems and Signal Processing, Vol. 29, 2012, p. 415-427. • Morenilla A. J., Carmona R. M., Romero J. L. Mathematical morphology for design and manufacturing. Mathematical and Computer Modeling, Vol. 54, 2011, p. 1753-1759. About this article 24 December 2014 11 February 2015 Fault diagnosis based on vibration signal analysis planetary gearbox fault diagnosis empirical mode decomposition mathematical morphology morphological gradient filter Copyright © 2015 JVE International Ltd. This is an open access article distributed under the Creative Commons Attribution License , which permits unrestricted use, distribution, and reproduction in any medium, provided the original work is properly cited.
{"url":"https://www.extrica.com/article/15777","timestamp":"2024-11-14T03:56:17Z","content_type":"text/html","content_length":"159291","record_id":"<urn:uuid:61c5b5e7-3cd6-4915-b48c-ec187976bfa3>","cc-path":"CC-MAIN-2024-46/segments/1730477028526.56/warc/CC-MAIN-20241114031054-20241114061054-00217.warc.gz"}
Brake Caliper Clamping Force Calculator Online - CalculatorsHub Home » Simplify your calculations with ease. » Mechanical Calculators » Brake Caliper Clamping Force Calculator Online A brake caliper plays a critical role in the vehicle’s braking system. It’s the device responsible for slowing the wheel’s rotation via friction. The brake caliper clamping force is the force applied by the brake caliper onto the brake pad, leading to the generation of friction against the vehicle’s wheel rotor. This force is crucial to comprehend, as it significantly influences a vehicle’s braking performance. A Brake Caliper Clamping Force Calculator is a tool designed to compute this specific force, utilizing the formula BCF=CP∗CA. Here, BCF denotes the brake caliper clamping force (measured in lbf or pound-force), CP stands for the caliper pressure (psi or pounds per square inch), and CA signifies the caliper contact area (in^2 or square inches). Working of the Calculator The Brake Caliper Clamping Force Calculator operates on the principle of the aforementioned formula. It requires two main inputs: the caliper pressure and the caliper contact area. The caliper pressure is the force per unit area exerted by the brake fluid onto the brake caliper, typically measured in psi. On the other hand, the caliper contact area is the region on the brake rotor where the caliper applies force, typically measured in square inches. By multiplying these two parameters (CP and CA), the calculator provides the output, which is the brake caliper clamping force (BCF). Understanding the Formula and its Variables As stated earlier, the Brake Caliper Clamping Force Formula is BCF=CP∗CA. This formula calculates the brake clamping force by multiplying the caliper pressure by the contact area. • BCF (Brake Caliper Clamping Force): The force exerted by the brake caliper onto the brake pad, measured in lbf. • CP (Caliper Pressure): The pressure exerted by the brake fluid on the brake caliper, measured in psi. • CA (Caliper Contact Area): The surface area of the brake rotor where the caliper applies force, measured in square inches. Detailed Example Let’s consider a detailed example to understand how to calculate the Brake Caliper Clamping Force. Assume the caliper pressure (CP) is 11 psi and the caliper contact area (CA) is 22 in^2. By plugging these values into the formula, we get BCF = 11 psi * 22 in^2 = 242 lbf. This means that the brake caliper applies a force of 242 lbf onto the brake pad, which slows down the vehicle’s wheel by generating friction against the wheel rotor. Applications of Brake Caliper Clamping Force The calculation of Brake Caliper Clamping Force is vital in several domains: • Vehicle Manufacturing and Design: Automobile manufacturers use these calculations to design and select suitable brakes for different vehicle models. It helps optimize braking performance. • Automotive Engineering Studies: Students and researchers use this formula in their projects and studies to understand the principles of automobile braking systems. • Vehicle Inspection and Maintenance: Mechanics and technicians use the calculator to assess the vehicle’s braking system’s condition and suggest necessary repairs or replacements. Most Common FAQs Why is Brake Caliper Clamping Force important? It’s crucial because it directly influences the vehicle’s braking performance. The higher the clamping force, the more friction is generated, leading to more efficient braking. Can the Caliper Clamping Force affect the life of brake pads? Yes, a higher clamping force can lead to more wear and tear of the brake pads, reducing their lifespan. What could be the consequences of a low clamping force? Low clamping force could lead to inefficient braking, increasing the risk of accidents, especially in emergency stop situations. Q1: Why is Brake Caliper Clamping Force important? A: It’s crucial because it directly influences the vehicle’s braking performance. The higher the clamping force, the more friction is generated, leading to more efficient braking. Q2: Can the Caliper Clamping Force affect the life of brake pads? A: Yes, a higher clamping force can lead to more wear and tear of the brake pads, reducing their lifespan. Q3: What could be the consequences of a low clamping force? A: Low clamping force could lead to inefficient braking, increasing the risk of accidents, especially in emergency stop situations. Understanding the concept and calculation of Brake Caliper Clamping Force is vital to ensure optimal braking performance. The Brake Caliper Clamping Force Calculator, based on the simple formula BCF= CP∗CA, provides a straightforward method to assess this force, ensuring better safety and efficiency in automotive design, maintenance, and operation. As we progress in the age of vehicular advancement, tools and concepts like these will continue to play a pivotal role. Leave a Comment
{"url":"https://calculatorshub.net/mechanical-calculators/brake-caliper-clamping-force-calculator/","timestamp":"2024-11-06T22:20:29Z","content_type":"text/html","content_length":"119716","record_id":"<urn:uuid:b6bc9819-84d5-41e0-aa25-2b01f62aba08>","cc-path":"CC-MAIN-2024-46/segments/1730477027942.47/warc/CC-MAIN-20241106194801-20241106224801-00614.warc.gz"}
Envelope Encryption 1. Docs 2. Data Control Platform 3. Further Reading 4. Envelope Encryption Envelope Encryption Envelope encryption is a technique that combines symmetric and asymmetric encryption to improve performance. A key value suitable for use with a symmetric encryption algorithm such as AES is chosen randomly, then it is used to encrypt plaintext. This plaintext is referred to as the document, and the key is called the document encryption key (DEK). The DEK itself is then asymmetrically encrypted with a user’s public key. The resulting encrypted DEK can be stored with the data or elsewhere. To recover the document the encrypted DEK is first decrypted using the user’s private key. Once the DEK is recovered, it can be used to decrypt the underlying document data. Glossary terms: Symmetric Encryption Symmetric encryption uses one key for both encryption and decryption. It is fast and very secure. The main drawback is that the encrypter of the data and each user that should be allowed to decrypt must find a way to share the symmetric key securely. Asymmetric Encryption Asymmetric encryption uses two keys that are mathematically related (generally called a key pair). Plaintext or document data is encrypted using the public key, and the resulting ciphertext is decrypted using the corresponding private key. Public Key Encryption Public key encryption is another name for asymmetric encryption. It is called public key cryptography because the encryption key can be shared publicly, while the decryption key must be kept private.
{"url":"https://ironcorelabs.com/docs/data-control-platform/concepts/envelope-encryption/","timestamp":"2024-11-10T11:51:55Z","content_type":"text/html","content_length":"65463","record_id":"<urn:uuid:8ff0281f-8a00-463b-b2d9-839d5e96149a>","cc-path":"CC-MAIN-2024-46/segments/1730477028186.38/warc/CC-MAIN-20241110103354-20241110133354-00229.warc.gz"}
Energy minimisation · OptimalControl.jl Let us consider a wagon moving along a rail, whom acceleration can be controlled by a force $u$. We denote by $x = (x_1, x_2)$ the state of the wagon, that is its position $x_1$ and its velocity We assume that the mass is constant and unitary and that there is no friction. The dynamics we consider is given by $$\[ \dot x_1(t) = x_2(t), \quad \dot x_2(t) = u(t),\quad u(t) \in \R,\]$$ which is simply the double integrator system. Les us consider a transfer starting at time $t_0 = 0$ and ending at time $t_f = 1$, for which we want to minimise the transfer energy $$\[ \frac{1}{2}\int_{0}^{1} u^2(t) \, \mathrm{d}t\]$$ starting from the condition $x(0) = (-1, 0)$ and with the goal to reach the target $x(1) = (0, 0)$. First, we need to import the OptimalControl.jl package to define the optimal control problem and NLPModelsIpopt.jl to solve it. We also need to import the Plots.jl package to plot the solution. using OptimalControl using NLPModelsIpopt using Plots Let us define the problem ocp = @def begin t ∈ [0, 1], time x ∈ R², state u ∈ R, control x(0) == [ -1, 0 ] x(1) == [ 0, 0 ] ẋ(t) == [ x₂(t), u(t) ] ∫( 0.5u(t)^2 ) → min For a comprehensive introduction to the syntax used above to describe the optimal control problem, check this abstract syntax tutorial. In particular, there are non-unicode alternatives for derivatives, integrals, etc. There is also a non-standard but more classical functional syntax, check this functional syntax tutorial. We can solve it simply with: sol = solve(ocp) This is Ipopt version 3.14.16, running with linear solver MUMPS 5.7.3. Number of nonzeros in equality constraint Jacobian...: 3005 Number of nonzeros in inequality constraint Jacobian.: 0 Number of nonzeros in Lagrangian Hessian.............: 251 Total number of variables............................: 1004 variables with only lower bounds: 0 variables with lower and upper bounds: 0 variables with only upper bounds: 0 Total number of equality constraints.................: 755 Total number of inequality constraints...............: 0 inequality constraints with only lower bounds: 0 inequality constraints with lower and upper bounds: 0 inequality constraints with only upper bounds: 0 iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls 0 1.0000000e-01 1.10e+00 1.99e-14 0.0 0.00e+00 - 0.00e+00 0.00e+00 0 1 -5.0000000e-03 7.36e-02 2.66e-15 -11.0 6.08e+00 - 1.00e+00 1.00e+00h 1 2 6.0003829e+00 8.88e-16 1.78e-15 -11.0 6.01e+00 - 1.00e+00 1.00e+00h 1 Number of Iterations....: 2 (scaled) (unscaled) Objective...............: 6.0003828724303254e+00 6.0003828724303254e+00 Dual infeasibility......: 1.7763568394002505e-15 1.7763568394002505e-15 Constraint violation....: 8.8817841970012523e-16 8.8817841970012523e-16 Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00 Complementarity.........: 0.0000000000000000e+00 0.0000000000000000e+00 Overall NLP error.......: 1.7763568394002505e-15 1.7763568394002505e-15 Number of objective function evaluations = 3 Number of objective gradient evaluations = 3 Number of equality constraint evaluations = 3 Number of inequality constraint evaluations = 0 Number of equality constraint Jacobian evaluations = 3 Number of inequality constraint Jacobian evaluations = 0 Number of Lagrangian Hessian evaluations = 2 Total seconds in IPOPT = 0.029 EXIT: Optimal Solution Found. And plot the solution with: We add the path constraint $$\[x_2(t) \le 1.2.\]$$ ocp = @def begin t ∈ [0, 1], time x ∈ R², state u ∈ R, control x₂(t) ≤ 1.2 x(0) == [ -1, 0 ] x(1) == [ 0, 0 ] ẋ(t) == [ x₂(t), u(t) ] ∫( 0.5u(t)^2 ) → min sol = solve(ocp) We can save the solution in a Julia .jld2 data file and reload it later, and also export a discretised version of the solution in a more portable JSON format. # load additional modules using JLD2, JSON3 # JLD save / load save(sol, filename_prefix="my_solution") sol_reloaded = load("my_solution") println("Objective from loaded solution: ", sol_reloaded.objective) # JSON export / read export_ocp_solution(sol, filename_prefix="my_solution") sol_json = import_ocp_solution(ocp, filename_prefix="my_solution") println("Objective from JSON discrete solution: ", sol_json.objective) Objective from loaded solution: 7.68195538823458 Objective from JSON discrete solution: 7.68195538823458
{"url":"https://control-toolbox.org/OptimalControl.jl/stable/tutorial-double-integrator-energy.html","timestamp":"2024-11-07T01:08:32Z","content_type":"text/html","content_length":"17795","record_id":"<urn:uuid:aec8bfee-10ea-43bb-8f01-78afb283986a>","cc-path":"CC-MAIN-2024-46/segments/1730477027942.54/warc/CC-MAIN-20241106230027-20241107020027-00852.warc.gz"}
That time Cathy Humphreys taught me to divide fractions Despite my background as a high school teacher, I've gotten deeply interested in grade 3-5 math in the past few years, particularly all the bits related to number, operations, & algebraic thinking and how they weave together to create the ramp that ultimately gives kids access to formal algebra. But it was not always this way! As a college math major filling out applications to masters & secondary credential programs, I definitely saw myself as a high school teacher, much more interested in the complexity and rich structure of Algebra II and trigonometry and calculus than in the usual middle school topics. And I *certainly* had never gone back to closely examine my own conceptual understanding of the foundational mathematics we learn in elementary school. Who wants to teach fractions and decimals when you could be initiating kids into the wonders of trigonometric functions?? So, I got into a secondary program, started student teaching Algebra I, & learning all the magical things they teach you in Curriculum & Instruction (ie, "methods") class about how kids make sense of ideas like variables and functions and data analysis over time and what it really means to understand all these things anyway. It was mystifying and terrifying and amazing, and in addition to learning how to teach, those experiences also unlocked for me an entirely new dimension of understanding. It was exhilarating ("Who knew math could be even MORE AMAZING??) but also a bit panic-inducing ("How the HECK am I supposed to get kids to understand it THIS way?!?"). All in all, though, I was starting to feel pretty darn good about my content knowledge. And then, one day, Cathy Humphreys came to class. She came to teach us about fractions. I still remember a bunch of us thinking it was a bit odd; I mean, as secondary math teacher candidates, nearly all of us had undergraduate degrees in math, and we were certainly all quite strong in the subject by any reasonable measure. Shouldn't we be learning how to teach kids about systems of equations or rate of change? Still, we were also reasonably open minded and willing to try just about anything, and mostly approached the session with an attitude of, "What the heck, let's see what Cathy Humphreys has to teach us about fractions." She brought pattern blocks, and one of the first things she told us was that as we thought about fraction problems today, we were not to use any procedures or algorithms that we knew for performing calculations with fractions; instead, we were to reason our way through the problems, performing only operations that we could explain in the context of the problem. (For example, "There's 1/3 of each pizza leftover and 6 pizzas, so to find out how much is left, we can multiply 6 pizzas times 1/3 of a pizza, ie, add six 1/3 's together" = OK; "We need to divide 6 5/8 meters of ribbon into strips of 1/4 meters, so invert the 1/4 and multiply straight across" = not OK unless you can explain why on earth that makes sense.) Well, we were all of course believers in deep conceptual understanding, and we weren't sure why people like us who were obviously very good at math would need to fall back on memorized procedures, but we were happy to oblige. I wish I could remember more about the specifics of the session--which operations and problems we did, and in which order, and what the conversations around them were. I do remember talking about and modeling problems like 3 ÷ 1/4, where the divisor fits evenly into the dividend, which didn't trouble any of us too much, and we were even fine with problems like 1/2 ÷ 1/4 or 6/8 ÷ 1/4. (In fact, I might go so far as to say we were actually pretty proud of our ability to explain the solutions in a completely conceptual way and even illustrate our methods using the pattern blocks.) But something I remember very clearly was how the mood in the room shifted when we began working on a fraction division problem using the pattern blocks where the divisor did not fit so neatly. I think that most (or all?) of us had thought we'd breeze through it, but it quickly became obvious that that wasn't the case. Since I don't remember the first real stumper Cathy asked us to think about, let's pretend it was 1 ÷ 2/3 (partly because this video exists as an illustration). It was easy enough to take my 1 (the yellow hexagon) and find two of the 1/3 blocks (the blue trapezoids) and place them on top; what I was having trouble with was completing the mapping of the equation in my mind, 1 ÷ 2/3 = x, onto the concrete representation. Like my classmates, I remember staring blankly at my pattern blocks, completely paralyzed, unexpectedly humbled by elementary school math that for years I'd considered too boring to even consider beyond its procedural, symbolic role in formal algebra. I knew 101 ways to integrate a function and could rattle off epsilon-delta proofs with the best of them, but somehow couldn't solve a fifth grade arithmetic problem without falling back on a memorized procedure. I could see the 1 and I could see the 2/3, but when I thought about division, my brain yelled "Equal parts!!" and I could not knock into place a) the leftover 1/3 or b) where in the representation the division was happening. When I tried to think concretely about 1 ÷ 2/3, what I came up with was, "Split 1 into equal groups of 2/3," which was just clearly not possible. Abstractly, I knew the answer was 3/2 or 1 1/2, but I could not for the life of me see where that 1 1/2 was, or make sense of that "equal groups" construct. There was quite a lot of tentative murmuring and later full-on discussion about this among our group of secondary candidates. We shared our representations, made observations, and explained the thoughts and questions that were tripping us up. The light bulb came on for a lot of us, I think, when someone asked, "What if instead of interpreting 1 ÷ 2/3 as "Divide 1 into 2/3 equal groups," we thought about it as, "How many 2/3 fit into 1?" And with that observation, suddenly a switch in my thinking flipped. I looked at my model, at the 2/3 of the yellow hexagon covered with the two blue trapezoids, and at the troublesome 1/3 that was left, and suddenly there it was, plain as day, 1 1/2, right in front of my face. Of course, putting it into words was something else all together. "There it is!" we squealed, pointing wildly to our blocks. "There's 3/2!" "Where's what?" "The 3/2!" "THERE!" More exuberant pointing. But Cathy didn't only want us to see the 3/2; she wanted us to put it into words, to explain it, mathematically and precisely. She pressed on our burgeoning understanding with questions like, "But if we said the yellow hexagon is 1, how can we have an answer that's more than 1?" and "Wouldn't 1 1/2 be a yellow hexagon and half of another one?" and "If the answer is 1 1/2, but not 1 1/2 hexagons, then 1 1/2 what?" (This was 12 years ago, but I can't think back on the discussion now without MP 3 and MP 6--and realistically, a whole host of others--jumping out at me.) It was through these questions that we finally got to the idea of the changing unit. The answer was not 1 1/2 "ones" (yellow hexagons); it was 1 1/2 two-thirds. Inside our "one" (the yellow hexagon), we could fit one complete 2/3 (two blue trapezoids), plus another 1/3 (one blue trapezoid)--ie, another half of a 2/3. So if we thought about the problem as, "How many 2/3 fit into 1?," it was easier to see that the answer was, "One (complete 2/3), plus another half (of a 2/3), or 1 1/2 (two-thirds)." Friends, I don't know about you, but I was *definitely* not taught fraction division this way; mine was definitely, definitely not to reason why. Flip it and multiply straight across you say? DONE AND DONE. It never occurred to me that there could be a reason; it was just the rule. I know for a fact that this was a huge part of why I hated and struggled with math so much in school. I don't memorize well, at all. If there's some procedure I need to carry out, even as an adult, I need to understand why, to completely make sense of each step, if it's going to be any good to me. And when the focus is on getting an answer, quickly and consistently and at all costs, true understanding--understanding that can be enriched and expanded and built on over time--falls by the wayside. Teaching procedures without helping students make sense of them in the context of the mathematics they already know is a mathematical dead-end. Next time, how I made sense of even CRAZIER problems, ie, ones where the we must divide a SMALL fraction by a BIGGER fraction. (Crazy talk, I know.) ***(P.S., Cathy, if you ever read this and have a better memory of this day than I do--or if I've totally bolloxed what actually happened--please let me know so that I can make update this post! And, thank you again for teaching me how to divide fractions. :) )***
{"url":"https://angelasunifiedtheory.blogspot.com/2017/05/that-time-cathy-humphreys-taught-me-to.html","timestamp":"2024-11-05T16:31:36Z","content_type":"text/html","content_length":"80467","record_id":"<urn:uuid:5d3db7b8-2b58-4fe4-a0ba-1ab9fb0370a5>","cc-path":"CC-MAIN-2024-46/segments/1730477027884.62/warc/CC-MAIN-20241105145721-20241105175721-00298.warc.gz"}
Room Area Calculator - Online Calculators To calculate the area of the room, simply multiply the room’s length (L) by its width (W). The result will give you the total area of the room in square units. Room Area Calculator Formula: (A = L times W) A room area calculator is a simple and efficient way to measure the size of any room. Whether you’re trying to calculate the area of a 12×12 room or figure out how many square meters is a 12×12 room, these calculators provide accurate results. Using the area formula, you can determine the total square footage or square meters of your space. $A = L \times W$ Variable Meaning A Area of the room L Length of the room W Width of the room Solved Calculations : Example 1: Given Values: • Length (L) = 12 feet • Width (W) = 10 feet Calculation Instructions A = 12 × 10 Multiply the length by the width. A = 120 The result gives the total area. Answer: Area = 120 square feet Example 2: Given Values: • Length (L) = 8 meters • Width (W) = 6 meters Calculation Instructions A = 8 × 6 Multiply the length by the width. A = 48 The result gives the total area. Answer: Area = 48 square meters What is Room Area Calculator ? Calculating the area of a room is a straightforward process, but it’s essential to know the right formula. To calculate the area, simply multiply the length and width of the room. For example, if you’re wondering what is the area of a 12×12 room, the calculation would be 12 feet multiplied by 12 feet, giving you 144 square feet. Similarly, a 10×10 room would be 100 square feet. You can use this calculator to make this process easier, especially for larger spaces like a 20×20 room. These calculators provide quick results, whether you’re working in square feet or meters. For those looking to calculate the area for painting or flooring purposes, a room area calculator for paint or room area calculator with dimensions is ideal. In addition to measuring regular rooms, you can also calculate the area of irregularly shaped spaces. Tools like a room area calculator m2 or a total room area calculator allow you to measure in square meters or feet, depending on your needs. If you’re planning to furnish or install flooring, using a room size calculator for furniture or a square feet calculator ensures you get accurate measurements. Final Words: Calculating the area is crucial not only for home projects but also for determining the right size of air conditioners, as the room area calculator for air conditioner helps you figure out the cooling requirements based on room size. With the right calculator, you can tackle any room measurement with ease.
{"url":"https://areacalculators.com/room-area-calculator/","timestamp":"2024-11-04T02:27:47Z","content_type":"text/html","content_length":"106123","record_id":"<urn:uuid:c59f12e2-fc3f-4ee2-b029-73b8e4f96994>","cc-path":"CC-MAIN-2024-46/segments/1730477027809.13/warc/CC-MAIN-20241104003052-20241104033052-00059.warc.gz"}
An introduction to Gamma-convergence Direct methods in the calculus of variations: • semicontinuity and convexity, • coerciveness and reflexivity, • relaxation and minimizing sequences, • properties of integral functionals. • definition and elementary properties, • convergence of minima and of minimizers, • sequential characterization of Gamma-limits, • Gamma-convergence in metric spaces and Yosida approximation, • Gamma-convergence of quadratic functionals. • abstract definition, • connection with Gamma-convergence, • convergence of eigenvalues and eigenvectors. The localization method for Gamma-convergence: • increasing set functions and their regularizations, • measures, fundamental estimate for subadditivity, • integral representation of Gamma-limits, • compactness of elliptic operators with respect to G-convergence, • homogenization problems for convex integral functionals, • homogenization of elliptic operators.
{"url":"https://math.sissa.it/course/phd-course/introduction-gamma-convergence","timestamp":"2024-11-14T06:57:45Z","content_type":"application/xhtml+xml","content_length":"29160","record_id":"<urn:uuid:4c2c22d8-6390-4503-bc24-ddb4befff9fa>","cc-path":"CC-MAIN-2024-46/segments/1730477028545.2/warc/CC-MAIN-20241114062951-20241114092951-00278.warc.gz"}
\[ \DeclareMathOperator*{\argmin}{argmin} \DeclareMathOperator*{\argmax}{argmax} \DeclareMathOperator*{\minimize}{minimize} \DeclareMathOperator*{\maximize}{maximize} \DeclareMathOperator*{\find} {find} \DeclareMathOperator{\st}{subject\,\,to} \newcommand{\E}{E} \newcommand{\Expect}[1]{\E\left[ #1 \right]} \newcommand{\Var}[1]{\mathrm{Var}\left[ #1 \right]} \newcommand{\Cov}[2]{\mathrm{Cov}\ left[#1,\ #2\right]} \newcommand{\given}{\ \vert\ } \newcommand{\X}{\mathbf{X}} \newcommand{\x}{\mathbf{x}} \newcommand{\y}{\mathbf{y}} \newcommand{\P}{\mathcal{P}} \newcommand{\R}{\mathbb{R}} \ newcommand{\norm}[1]{\left\lVert #1 \right\rVert} \newcommand{\snorm}[1]{\lVert #1 \rVert} \newcommand{\tr}[1]{\mbox{tr}(#1)} \newcommand{\brt}{\widehat{\beta}^R_{s}} \newcommand{\brl}{\widehat{\ beta}^R_{\lambda}} \newcommand{\bls}{\widehat{\beta}_{ols}} \newcommand{\blt}{\widehat{\beta}^L_{s}} \newcommand{\bll}{\widehat{\beta}^L_{\lambda}} \newcommand{\U}{\mathbf{U}} \newcommand{\D}{\mathbf {D}} \newcommand{\V}{\mathbf{V}} \] Think of the two of us as interchangeable people. (It’s not that hard. We’re very similar.) We and the TAs are here to help you learn. Ask questions. We favour steady work through the term (vs. sleeping until finals) centering / scaling / factors-to-dummies / basis expansion / missing values / dimension reduction / discretization / transformations Repeat all the preprocessing on new data. But be careful. Each module is based on a collection of readings and lectures Total across three components: 65 points, any way you want Not easy, especially the first 2, especially if you are unfamiliar with R / Rmarkdown / ggplot You may revise to raise your score to 7/10, see Syllabus. Only if you lose 3+ for content (penalties can’t be redeemed). Labs should give you practice, allow for questions with the TAs. They are due at 2300 on the day of your lab, lightly graded. You may do them at home, but you must submit individually (in lab, you may share submission) 0 points for skipping, 2 points for trying, 4 points for correct total = max(0, min(5 * points / N - 5, 10)) Average < 1 drops your Final Mark 1 letter grade. If you put in the effort, you’re guaranteed a C+. But to get an A+, you should really deeply understand the material. If you’re cool with C+ and hate tests, then that’s fine. If you have not submitted your lab/assignment by the time grading starts, you will get a 0. When you submit Likelihood that your submission gets a 0 Before 11pm on due date (i.e. on time) 0% 11:01pm on due date 0.01% 9am after due date 50% 2 weeks after due date 99.99999999% We will only make exceptions when you have grounds for academic consession. (See the UBC policy.) Remember: you can still get a “perfect” effort grade even if you get a 0 on one assignment. James, Witten, Hastie, Tibshirani, 2013, Springer, New York. (denoted [ISLR]) Hastie, Tibshirani, Friedman, 2009, Second Edition, Springer, New York. (denoted [ESL]) We will use R and we assume some background knowledge. See https://ubc-stat.github.io/stat-406/ for what you need to install for the whole term. Links to useful supplementary resources are available on the website. We cannot guarantee that they will all work properly (sometimes we mess it up) Coding is hard. We hope you’ll get better at it. We strongly urge you to get up at the same time everyday. It’s really hard to sleep in until 10 on MWF and make class at 8 on T/Th. We have to give you a grade, but we want that grade to reflect your learning and effort, not other junk.
{"url":"https://ubc-stat.github.io/stat-406/schedule/slides/00-intro-to-class.html","timestamp":"2024-11-06T05:44:25Z","content_type":"text/html","content_length":"51598","record_id":"<urn:uuid:26cfc41b-f3e3-4e01-a5cb-be11553df9f9>","cc-path":"CC-MAIN-2024-46/segments/1730477027909.44/warc/CC-MAIN-20241106034659-20241106064659-00103.warc.gz"}
Choice-memory tradeoff in allocations In the classical balls-and-bins paradigm, where n balls are placed independently and uniformly in n bins, typically the number of bins with at least two balls in them is θ(n) and the maximum number of balls in a bin is θ(logn/log log n). It is w known that when each round offers k independent uniform options for bins, it is possible to typically achieve a constant maximal load if and only if k = ω(logn). Moreover, it is possible w.h.p. to avoid any collisions between n/2 balls if k > log[2] n. In this work, we extend this into the setting where only m bits of memory are available. We establish a tradeoff between the number of choices k and the memory m, dictated by the quantity km/n. Roughly put, we show that for km » n one can achieve a constant maximal load, while for km « no substantial improvement can be gained over the case k = 1 (i.e., a random allocation). For any k = ω(logn) and m = ω(log^2 n), one can achieve a constant load w.h.p. if km = ω(n), yet the load is unbounded if km = o(n). Similarly, if km > Cn then n/2 balls can be allocated without any collisions w.h.p., whereas for km < εn there are typically ω(n) collisions. Furthermore, we show that the load is w.h.p. at least log(n/m)/log k+log log((n/m) In particular, for k ≤ polylog(n), if m = n ^1-δ the optimal maximal load is θ(log n/log log n) (the same as in the case k = 1), while m = 2n suffices to ensure a constant load. Finally, we analyze nonadaptive allocation algorithms and give tight upper and lower bounds for their performance. • Balanced allocations • Balls and bins paradigm • Lower bounds on memory • Online perfect matching • Space/performance tradeoffs Dive into the research topics of 'Choice-memory tradeoff in allocations'. Together they form a unique fingerprint.
{"url":"https://cris.huji.ac.il/en/publications/choice-memory-tradeoff-in-allocations-14","timestamp":"2024-11-10T11:05:36Z","content_type":"text/html","content_length":"50003","record_id":"<urn:uuid:e5ba7b29-88bd-42d9-8855-897fb45c5420>","cc-path":"CC-MAIN-2024-46/segments/1730477028186.38/warc/CC-MAIN-20241110103354-20241110133354-00258.warc.gz"}
The Flajolet-Martin algorithm is an intelligent way to estimate the number of distinct elements in a large dataset. It's a probabilistic algorithm that uses hash functions to create a compact summary of the data. This summary, called a sketch, allows us to quickly estimate the cardinality (the number of unique items) without having to store every single element. In this article, we'll learn how the Flajolet-Martin algorithm works, look at the steps involved, & see how to implement it in Python. We'll also discuss the advantages & limitations of this What is flajolet martin algorithm ? The Flajolet-Martin algorithm is a probabilistic algorithm used to estimate the number of distinct elements (cardinality) in a large dataset using a limited amount of memory. It works by applying hash functions to the elements & maintaining a compact summary called a bitmap. By analyzing the bitmap, the algorithm provides an approximate count of the unique elements in the dataset without the need to store or compare each individual item. In simpler terms, the Flajolet-Martin algorithm is a clever way to count the number of unique items in a huge collection of data without actually counting them one by one. It trades a bit of accuracy for efficiency, allowing us to get a good estimate of the cardinality even when dealing with massive datasets that wouldn't fit in memory. The algorithm relies on the properties of hash functions & probability theory to create a compact representation of the data. By setting bits in the bitmap based on the hash values of the elements, it captures the essence of the dataset's uniqueness. The position of the least significant bit in the bitmap is then used to estimate the total number of distinct elements. The steps for the Flajolet-Martin algorithm are: 1. Initialize an empty bitmap of size k, where k is a power of 2. 2. For each element in the dataset, apply a hash function that maps the element to a number between 0 & 2^k - 1. 3. Find the position of the least significant bit (LSB) that is set to 1 in the hash value. 4. Update the bitmap by setting the bit at the position found in step 3 to 1. 5. After processing all elements, estimate the number of distinct elements by counting the number of zeros (r) from the right until the first 1 is encountered in the bitmap. 6. Calculate the estimate using the formula: 2^(k - r) / φ, where φ is a constant (approximately 0.77351). The key idea behind the Flajolet-Martin algorithm is that the position of the LSB in the hash values follows a geometric distribution. By analyzing the bitmap & applying the estimation formula, we can get a good approximation of the number of distinct elements in the dataset.
{"url":"https://www.naukri.com/code360/library/flajolet-martin-algorithm","timestamp":"2024-11-08T02:38:42Z","content_type":"text/html","content_length":"398150","record_id":"<urn:uuid:afa0272b-decc-4142-82a5-0a8f1191c7af>","cc-path":"CC-MAIN-2024-46/segments/1730477028019.71/warc/CC-MAIN-20241108003811-20241108033811-00643.warc.gz"}
Solving the Problem with Word Problems, Part I | ORIGO Education Solving the Problem with Word Problems, Part I No matter where I am in the country, when I talk to teachers, they all tell me that word problems are a problem for a whole variety of reasons. That got me to thinking, why is that every time we talk about word problems it’s with a wince and a frown? And how can we turn that around, because in the same breath that teachers share their challenges with word problems, they also tell me that word problems are an important instructional tool. So, over four posts (and an accompanying edWeb.net webinar series) we’re going to explore the world of word problems. The Purpose of Word Problems As counterintuitive as it may sound, I don’t think that word problems are an ideal way to teach problem solving as they don’t look like the problems we encounter in real life. In my opinion, word problems are examples of mathematical applications and connections. They provide students with a chance to learn the work of the underlying operation, in the case of elementary math—addition, subtraction, multiplication, and division. And in strengthening student knowledge of the underlying mathematical operations, we provide them with the building blocks needed to solve real-world Three Skills Word Problems Develop Although word problems look different across the grade levels, there are three skill sets we want to develop—language comprehension, mathematical comprehension, and solution paths. I talk about language rather than strictly reading because word problems involve oral as well as written language. Think about how kids learn a language, they start talking, they learn vocabulary, they speak in progressively more complex sentences, and only then do they start reading. Understanding the language of math, just like learning any language, is a progression. We start with language students know and only then do we connect that language to a particular mathematical representation. For instance, initially we talk about a bird flying away, something has left, or eating a cookie, something disappears, or sharing a pizza, something is taken away, and only then do we move to mathematical comprehension, the concept of minus and the operation of subtraction. This is what I call the missing link, that connection between everyday terms and more formal mathematical language. I’m not talking about keywords, because those only work in limited, prescribed situations, but rather what we might call the verbs of mathematics, what action am I taking (adding, subtracting, multiplying, or dividing) and what language in the word problem connects to a particular operation. This connection, a step we all to often skip, helps students figure out what operation to use, and that is the most critical function of word problems. Only after we make the connection between language and mathematical comprehension, can we move on to finding a solution to the word problem. Finding the solution path is different than finding the answer. This is where we encourage students to explore the tools and strategies they might use to help them solve a particular problem. Two Strategies That Support Word Problems In this post, I’m going to talk about two general strategies that support all word problems regardless of operation. In future posts, we’ll discuss particular strategies for each of the four operations—addition, subtraction, multiplication, and division. Let’s look at the following problem about a shepherd, 125 sheep, and 5 dogs. The shepherd has 125 sheep and 5 sheep dogs. How old is the shepherd? The first thing, you’ll notice about this word problem is that it can’t be solved with the information we have. When researcher Robert Kaplinsky gave this task to a class of eighth-graders, four students calculated the answer by adding or subtracting, sixteen students used division or multiplication, and four students guessed. Most of the students didn’t actually read the problem, they went directly to the numbers, a phenomenon John SanGiovanni calls number plucking. So, how can we help our students be thoughtful when approaching word problems and not simply go directly to using the numbers to find an answer? Three Reads Encourage kids to slow down and really understand the problem—read it three times. For the first two reads, we remove the question. On the first read, we read for story and context. And I’d encourage you to read it aloud and read it more than once, as this helps kids make sense of the story. Now you may live in an area where your students have never seen sheep and don’t have any idea what a shepherd does or why dogs would be involved. You will have to explain the context as that’s going to help students understand what’s happening in this very brief story. On the second read, we concentrate on quantities. When you think about the quantities, you also need to think about units, as that helps with meaning. It’s not just 125, it’s 125 sheep. It’s not just 5, it’s 5 dogs. What does that mean? There are a lot of sheep and not very many dogs. Are the dogs keeping the sheep in one place? Are they moving them from point A to point B? Are the dogs guarding the sheep from something? On the third read, we add the question. And now, students have thought about the problem and learned about the situation. They know there are 125 sheep and 5 dogs, but they don’t really know anything about the shepherd. They are ready to think if the quantities they have can help them answer the question about the shepherd’s age. Alternately on the third read, you can still eliminate the question and ask students what kinds of questions they could answer with the information they have. Then look at the question the word problem posed and have students decide if they want to answer that question (can they answer that question) or one of the questions they developed. Whether they answer the original question or a question they came up with, students are really thinking about the math behind the story and not simply plucking numbers. The Math Sandbox The sandbox, or sand at the beach, is a magical place where kids can play and build things. And if they don’t like what they’ve created, they can simply smash it and start over. Sand is open-ended, it’s endlessly malleable, and it encourages kids to use lots of fun tools. And solving word problems needs to feel as exploratory as the sandbox. Once students understand the language behind a word problem, they are ready to head into the math sandbox. Instead of using buckets and shovels, they may be using fraction pieces, base-10 blocks, counters, or drawings. This is the mathematical comprehension step. How do they represent 125 sheep and 5 dogs? The tools they use will depend on the specific question being asked. The important thing is that when students are in the sandbox they have lots of different mathematical representations that allow them to make sense of what the word problem is asking. They may start with counters and realize that’s not practical and move to a number line or writing an equation or number sentence. Both of these strategies encourage students to take their time and really understand what is happening in the word problem. It provides them with a process and a protocol to solve word problems. And by the time they get to those end-of-year high stakes tests, students have assimilated these strategies so they can rapidly assess a word problem, break it down into steps, and arrive at a solution. Join us for the following one-hour webinars on edWeb.net: Making Sense of Multiplication and Division Word Problems: Solving the Problem of Problem Solving on Tuesday, November 1 @ 4:00–5:00pm EDT Word Problems Beyond Whole Numbers: Solving the Problem of Problem Solving on Tuesday, December 6 @ 4:00–5:00pm EST
{"url":"https://www.origoeducation.com/insights/solving-the-problem-with-word-problems-part-1","timestamp":"2024-11-10T09:28:56Z","content_type":"text/html","content_length":"76246","record_id":"<urn:uuid:66c1997d-edfe-4254-baed-4bff17796b55>","cc-path":"CC-MAIN-2024-46/segments/1730477028179.55/warc/CC-MAIN-20241110072033-20241110102033-00440.warc.gz"}
SICP 2e (Large Text) - PDF Free Download Structure and Interpretation of Computer Programs Second Edition Unofficial Texinfo Format 2.andresraba1 Harold Abelson and Gerald Jay Sussman with Julie Sussman foreword by Alan J. Perlis c 1996 by The Massachusetts Institute of Technology Copyright ° Structure and Interpretation of Computer Programs second edition Harold Abelson and Gerald Jay Sussman with Julie Sussman, foreword by Alan J. Perlis This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License http://creativecommons.org/licenses/by-nc/3.0/ The MIT Press Cambridge, Massachusetts London, England McGraw-Hill Book Company New York, St. Louis, San Francisco, Montreal, Toronto This book is one of a series of texts written by faculty of the Electrical Engineering and Computer Science Department at the Massachusetts Institute of Technology. It was edited and produced by The MIT Press under a joint production-distribution arrangement with the McGraw-Hill Book Company. Unofficial Texinfo Format 2.andresraba1 (May 23, 2011), based on 2.neilvandyke4 (January 10, 2007). Short Contents Unofficial Texinfo Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Dedication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Preface to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Preface to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1 Building Abstractions with Procedures . . . . . . . . . . . . . . . . . . . . . 29 Building Abstractions with Data . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Modularity, Objects and State . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 Metalinguistic Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545 Computing with Register Machines . . . . . . . . . . . . . . . . . . . . . . . 739 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919 List of Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932 Table of Contents Unofficial Texinfo Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Dedication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Preface to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Preface to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1 Building Abstractions with Procedures . . . . . . . . . . . 29 1.1 The Elements of Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 1.1.1 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 1.1.2 1.1.3 1.1.4 Naming and the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Evaluating Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Compound Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 1.1.5 1.1.6 1.1.7 The Substitution Model for Procedure Application . . . . . . . . . . 47 Conditional Expressions and Predicates . . . . . . . . . . . . . . . . . . . . . 51 Example: Square Roots by Newton’s Method . . . . . . . . . . . . . . . . . 57 1.1.8 Procedures as Black-Box Abstractions . . . . . . . . . . . . . . . . . . . . . . . . 63 1.2 Procedures and the Processes They Generate . . . . . . . . . . . . . . . . . . . . 71 1.2.1 Linear Recursion and Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 1.2.2 Tree Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5 1.2.3 Orders of Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 1.2.4 Exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 1.2.5 Greatest Common Divisors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 1.2.6 Example: Testing for Primality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 1.3 Formulating Abstractions with Higher-Order Procedures . . . . . . 107 1.3.1 Procedures as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 1.3.2 1.3.3 1.3.4 Constructing Procedures Using Lambda . . . . . . . . . . . . . . . . . . . . . 116 Procedures as General Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Procedures as Returned Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Building Abstractions with Data . . . . . . . . . . . . . . . . . . 139 2.1 Introduction to Data Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 2.1.1 Example: Arithmetic Operations for Rational Numbers . . . . 145 2.1.2 Abstraction Barriers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 2.1.3 What Is Meant by Data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 2.1.4 Extended Exercise: Interval Arithmetic . . . . . . . . . . . . . . . . . . . . . 159 2.2 Hierarchical Data and the Closure Property . . . . . . . . . . . . . . . . . . . . 165 2.2.1 2.2.2 2.2.3 Representing Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Hierarchical Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Sequences as Conventional Interfaces . . . . . . . . . . . . . . . . . . . . . . 188 2.2.4 Example: A Picture Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 2.3 Symbolic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 2.3.1 Quotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 2.3.2 2.3.3 2.3.4 Example: Symbolic Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . 234 Example: Representing Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Example: Huffman Encoding Trees . . . . . . . . . . . . . . . . . . . . . . . . . 255 2.4 Multiple Representations for Abstract Data . . . . . . . . . . . . . . . . . . . . . 267 2.4.1 Representations for Complex Numbers . . . . . . . . . . . . . . . . . . . . . 270 6 2.4.2 Tagged data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 2.4.3 Data-Directed Programming and Additivity . . . . . . . . . . . . . . . . 283 2.5 Systems with Generic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 2.5.1 2.5.2 2.5.3 Generic Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Combining Data of Different Types . . . . . . . . . . . . . . . . . . . . . . . . . 304 Example: Symbolic Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Modularity, Objects and State . . . . . . . . . . . . . . . . . . . . . 338 3.1 Assignment and Local State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 3.1.1 Local State Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 3.1.2 The Benefits of Introducing Assignment . . . . . . . . . . . . . . . . . . . . 350 3.1.3 The Costs of Introducing Assignment . . . . . . . . . . . . . . . . . . . . . . . 357 3.2 The Environment Model of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . 366 3.2.1 3.2.2 3.2.3 The Rules for Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 Applying Simple Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Frames as the Repository of Local State . . . . . . . . . . . . . . . . . . . . . 377 3.2.4 Internal Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 3.3 Modeling with Mutable Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 3.3.1 Mutable List Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 3.3.2 3.3.3 3.3.4 Representing Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 Representing Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 A Simulator for Digital Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 3.3.5 Propagation of Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 3.4 Concurrency: Time Is of the Essence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 3.4.1 The Nature of Time in Concurrent Systems . . . . . . . . . . . . . . . . . 456 3.4.2 Mechanisms for Controlling Concurrency . . . . . . . . . . . . . . . . . . 463 3.5 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 3.5.1 Streams Are Delayed Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 7 3.5.2 Infinite Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498 3.5.3 Exploiting the Stream Paradigm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 3.5.4 Streams and Delayed Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 3.5.5 Modularity of Functional Programs and Modularity of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536 Metalinguistic Abstraction . . . . . . . . . . . . . . . . . . . . . . . . 545 4.1 The Metacircular Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549 4.1.1 4.1.2 4.1.3 The Core of the Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552 Representing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559 Evaluator Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571 4.1.4 4.1.5 4.1.6 Running the Evaluator as a Program . . . . . . . . . . . . . . . . . . . . . . . . 577 Data as Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 Internal Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588 4.1.7 Separating Syntactic Analysis from Execution . . . . . . . . . . . . . . 597 4.2 Variations on a Scheme – Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . 604 4.2.1 Normal Order and Applicative Order . . . . . . . . . . . . . . . . . . . . . . . 605 4.2.2 An Interpreter with Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . . . 608 4.2.3 Streams as Lazy Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620 4.3 Variations on a Scheme – Nondeterministic Computing . . . . . . . . 624 4.3.1 4.3.2 4.3.3 Amb and Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627 Examples of Nondeterministic Programs . . . . . . . . . . . . . . . . . . . 634 Implementing the Amb Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 4.4 Logic Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 4.4.1 Deductive Information Retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668 4.4.2 How the Query System Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684 4.4.3 4.4.4 Is Logic Programming Mathematical Logic? . . . . . . . . . . . . . . . . 696 Implementing the Query System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705 4.4.4.1 4.4.4.2 4.4.4.3 8 The Driver Loop and Instantiation . . . . . . . . . . . . . . . . . . . . . . 705 The Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 Finding Assertions by Pattern Matching . . . . . . . . . . . . . . . . 712 4.4.4.4 4.4.4.5 4.4.4.6 Rules and Unification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 Maintaining the Data Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722 Stream Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726 4.4.4.7 4.4.4.8 Query Syntax Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728 Frames and Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732 Computing with Register Machines . . . . . . . . . . . . . . 739 5.1 Designing Register Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741 5.1.1 A Language for Describing Register Machines . . . . . . . . . . . . . . 745 5.1.2 Abstraction in Machine Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 5.1.3 5.1.4 5.1.5 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754 Using a Stack to Implement Recursion . . . . . . . . . . . . . . . . . . . . . . 760 Instruction Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768 5.2 A Register-Machine Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770 5.2.1 The Machine Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772 5.2.2 The Assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778 5.2.3 Generating Execution Procedures for Instructions . . . . . . . . . . 784 5.2.4 Monitoring Machine Performance . . . . . . . . . . . . . . . . . . . . . . . . . . 795 5.3 Storage Allocation and Garbage Collection . . . . . . . . . . . . . . . . . . . . . 799 5.3.1 Memory as Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800 5.3.2 Maintaining the Illusion of Infinite Memory . . . . . . . . . . . . . . . . 809 5.4 The Explicit-Control Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818 5.4.1 5.4.2 5.4.3 The Core of the Explicit-Control Evaluator . . . . . . . . . . . . . . . . . . 821 Sequence Evaluation and Tail Recursion . . . . . . . . . . . . . . . . . . . . 830 Conditionals, Assignments and Definitions . . . . . . . . . . . . . . . . . 835 9 5.4.4 Running the Evaluator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838 5.5 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848 5.5.1 Structure of the Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853 5.5.2 5.5.3 5.5.4 Compiling Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860 Compiling Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 Combining Instruction Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . 880 5.5.5 5.5.6 5.5.7 An Example of Compiled Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885 Lexical Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901 Interfacing Compiled Code to the Evaluator . . . . . . . . . . . . . . . . 907 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919 List of Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932 Unofficial Texinfo Format This is the second edition SICP book, from Unofficial Texinfo Format. You are probably reading it in an Info hypertext browser, such as the Info mode of Emacs. You might alternatively be reading it TEX-formatted on your screen or printer, though that would be silly. And, if printed, expensive. The freely-distributed official HTML-and-GIF format was first converted personally to Unofficial Texinfo Format (UTF) version 1 by Lytha Ayth during a long Emacs lovefest weekend in April, 2001. The UTF is easier to search than the HTML format. It is also much more accessible to people running on modest computers, such as donated ’386based PCs. A 386 can, in theory, run Linux, Emacs and a Scheme interpreter simultaneously, but most 386s probably can’t also run both Netscape and the necessary X Window System without prematurely introducing budding young underfunded hackers to the concept of thrashing. UTF can also fit uncompressed on a 1.44MB floppy diskette, which may come in handy for installing UTF on PCs that do not have Internet or LAN access. The Texinfo conversion has been a straight transliteration, to the extent possible. Like the TEX-to-HTML conversion, this was not without some introduction of breakage. In the case of Unofficial Texinfo Format, figures have suffered an amateurish resurrection of the lost art of ASCII. Also, it’s quite possible that some errors of ambiguity were introduced during the conversion of some of the copious superscripts (‘^’) and subscripts (‘ ’). Divining which has been left as an exercise to the reader. But at least we don’t put our brave astronauts at risk by encoding the greater-than-or-equal symbol as 11 If you modify ‘sicp.texi’ to correct errors or improve the ASCII art, then update the @set utfversion 2.andresraba1 line to reflect your delta. For example, if you started with Lytha’s version 1, and your name is Bob, then you could name your successive versions 1.bob1, 1.bob2, . . . 1.bobn. Also update utfversiondate. If you want to distribute your version on the Web, then embedding the string “sicp.texi” somewhere in the file or Web page will make it easier for people to find with Web search engines. It is believed that the Unofficial Texinfo Format is in keeping with the spirit of the graciously freely-distributed HTML version. But you never know when someone’s armada of lawyers might need something to do, and get their shorts all in a knot over some benign little thing, so think twice before you use your full name or distribute Info, DVI, PostScript, or PDF formats that might embed your account or machine name. Peath, Lytha Ayth Addendum: See also the SICP video lectures by Abelson and Sussman: at MIT CSAIL or at MIT OCW. Second Addendum: Above is the original introduction to the UTF from 2001. Ten years later, UTF has been transformed: mathematical symbols and formulas are properly typeset, and figures drawn in vector graphics. The original text formulas and ASCII art figures are still there in the Texinfo source, but will display only when compiled to Info output. In the dawn of e-book readers and tablets, reading a PDF on screen is officially not silly anymore. Enjoy! A.R, May, 2011 Dedication This book is dedicated, in respect and admiration, to the spirit that lives in the computer. “I think that it’s extraordinarily important that we in computer science keep fun in computing. When it started out, it was an awful lot of fun. Of course, the paying customers got shafted every now and then, and after a while we began to take their complaints seriously. We began to feel as if we really were responsible for the successful, error-free perfect use of these machines. I don’t think we are. I think we’re responsible for stretching them, setting them off in new directions and keeping fun in the house. I hope the field of computer science never loses its sense of fun. Above all, I hope we don’t become missionaries. Don’t feel as if you’re Bible salesmen. The world has too many of those already. What you know about computing other people will learn. Don’t feel as if the key to successful computing is only in your hands. What’s in your hands, I think and hope, is intelligence: the ability to see the machine as more than when you were first led up to it, that you can make it more.” —Alan J. Perlis (April 1, 1922 – February 7, 1990) Foreword Educators, generals, dieticians, psychologists and parents program. Armies, students and some societies are programmed. An assault on large problems employs a succession of programs, most of which spring into existence en route. These programs are rife with issues that appear to be particular to the problem at hand. To appreciate programming as an intellectual activity in its own right you must turn to computer programming; you must read and write computer programs—many of them. It doesn’t matter much what the programs are about or what applications they serve. What does matter is how well they perform and how smoothly they fit with other programs in the creation of still greater programs. The programmer must seek both perfection of part and adequacy of collection. In this book the use of “program” is focused on the creation, execution and study of programs written in a dialect of Lisp for execution on a digital computer. Using Lisp we restrict or limit not what we may program, but only the notation for our program descriptions. Our traffic with the subject matter of this book involves us with three foci of phenomena: the human mind, collections of computer programs and the computer. Every computer program is a model, hatched in the mind, of a real or mental process. These processes, arising from human experience and thought, are huge in number, intricate in detail and at any time only partially understood. They are modeled to our permanent satisfaction rarely by our computer programs. Thus even though our programs are carefully handcrafted discrete collections of symbols, mosaics of interlocking functions, they continually evolve: we change them as our perception of the model deepens, enlarges, generalizes until the model ultimately attains a metastable place within still another model with which we struggle. 14 The source of the exhilaration associated with computer programming is the continual unfolding within the mind and on the computer of mechanisms expressed as programs and the explosion of perception they generate. If art interprets our dreams, the computer executes them in the guise of programs! For all its power, the computer is a harsh taskmaster. Its programs must be correct, and what we wish to say must be said accurately in every detail. As in every other symbolic activity, we become convinced of program truth through argument. Lisp itself can be assigned a semantics (another model, by the way), and if a program’s function can be specified, say, in the predicate calculus, the proof methods of logic can be used to make an acceptable correctness argument. Unfortunately, as programs get large and complicated, as they almost always do, the adequacy, consistency and correctness of the specifications themselves become open to doubt, so that complete formal arguments of correctness seldom accompany large programs. Since large programs grow from small ones, it is crucial that we develop an arsenal of standard program structures of whose correctness we have become sure—we call them idioms—and learn to combine them into larger structures using organizational techniques of proven value. These techniques are treated at length in this book, and understanding them is essential to participation in the Promethean enterprise called programming. More than anything else, the uncovering and mastery of powerful organizational techniques accelerates our ability to create large, significant programs. Conversely, since writing large programs is very taxing, we are stimulated to invent new methods of reducing the mass of function and detail to be fitted into large programs. 15 Unlike programs, computers must obey the laws of physics. If they wish to perform rapidly—a few nanoseconds per state change—they must transmit electrons only small distances (at most 1 12 feet). The heat generated by the huge number of devices so concentrated in space has to be removed. An exquisite engineering art has been developed balancing between multiplicity of function and density of devices. In any event, hardware always operates at a level more primitive than that at which we care to program. The processes that transform our Lisp programs to “machine” programs are themselves abstract models which we program. Their study and creation give a great deal of insight into the organizational programs associated with programming arbitrary models. Of course the computer itself can be so modeled. Think of it: the behavior of the smallest physical switching element is modeled by quantum mechanics described by differential equations whose detailed behavior is captured by numerical approximations represented in computer programs executing on computers composed of . . . ! It is not merely a matter of tactical convenience to separately identify the three foci. Even though, as they say, it’s all in the head, this logical separation induces an acceleration of symbolic traffic between these foci whose richness, vitality and potential is exceeded in human experience only by the evolution of life itself. At best, relationships between the foci are metastable. The computers are never large enough or fast enough. Each breakthrough in hardware technology leads to more massive programming enterprises, new organizational principles and an enrichment of abstract models. Every reader should ask himself periodically “Toward what end, toward what end?”—but do not ask it too often lest you pass up the fun of programming for the constipation of bittersweet philosophy. 16 Among the programs we write, some (but never enough) perform a precise mathematical function such as sorting or finding the maximum of a sequence of numbers, determining primality, or finding the square root. We call such programs algorithms, and a great deal is known of their optimal behavior, particularly with respect to the two important parameters of execution time and data storage requirements. A programmer should acquire good algorithms and idioms. Even though some programs resist precise specifications, it is the responsibility of the programmer to estimate, and always to attempt to improve, their performance. Lisp is a survivor, having been in use for about a quarter of a century. Among the active programming languages only Fortran has had a longer life. Both languages have supported the programming needs of important areas of application, Fortran for scientific and engineering computation and Lisp for artificial intelligence. These two areas continue to be important, and their programmers are so devoted to these two languages that Lisp and Fortran may well continue in active use for at least another quarter-century. Lisp changes. The Scheme dialect used in this text has evolved from the original Lisp and differs from the latter in several important ways, including static scoping for variable binding and permitting functions to yield functions as values. In its semantic structure Scheme is as closely akin to Algol 60 as to early Lisps. Algol 60, never to be an active language again, lives on in the genes of Scheme and Pascal. It would be difficult to find two languages that are the communicating coin of two more different cultures than those gathered around these two languages. Pascal is for building pyramids—imposing, breathtaking, static structures built by armies pushing heavy blocks into place. Lisp is for 17 building organisms—imposing, breathtaking, dynamic structures built by squads fitting fluctuating myriads of simpler organisms into place. The organizing principles used are the same in both cases, except for one extraordinarily important difference: The discretionary exportable functionality entrusted to the individual Lisp programmer is more than an order of magnitude greater than that to be found within Pascal enterprises. Lisp programs inflate libraries with functions whose utility transcends the application that produced them. The list, Lisp’s native data structure, is largely responsible for such growth of utility. The simple structure and natural applicability of lists are reflected in functions that are amazingly nonidiosyncratic. In Pascal the plethora of declarable data structures induces a specialization within functions that inhibits and penalizes casual cooperation. It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures. As a result the pyramid must stand unchanged for a millennium; the organism must evolve or perish. To illustrate this difference, compare the treatment of material and exercises within this book with that in any first-course text using Pascal. Do not labor under the illusion that this is a text digestible at MIT only, peculiar to the breed found there. It is precisely what a serious book on programming Lisp must be, no matter who the student is or where it is used. Note that this is a text about programming, unlike most Lisp books, which are used as a preparation for work in artificial intelligence. After all, the critical programming concerns of software engineering and artificial intelligence tend to coalesce as the systems under investigation become larger. This explains why there is such growing interest in Lisp outside of artificial intelligence. 18 As one would expect from its goals, artificial intelligence research generates many significant programming problems. In other programming cultures this spate of problems spawns new languages. Indeed, in any very large programming task a useful organizing principle is to control and isolate traffic within the task modules via the invention of language. These languages tend to become less primitive as one approaches the boundaries of the system where we humans interact most often. As a result, such systems contain complex language-processing functions replicated many times. Lisp has such a simple syntax and semantics that parsing can be treated as an elementary task. Thus parsing technology plays almost no role in Lisp programs, and the construction of language processors is rarely an impediment to the rate of growth and change of large Lisp systems. Finally, it is this very simplicity of syntax and semantics that is responsible for the burden and freedom borne by all Lisp programmers. No Lisp program of any size beyond a few lines can be written without being saturated with discretionary functions. Invent and fit; have fits and reinvent! We toast the Lisp programmer who pens his thoughts within nests of parentheses. Alan J. Perlis New Haven, Connecticut Preface to the Second Edition Is it possible that software is not like anything else, that it is meant to be discarded: that the whole point is to always see it as a soap bubble? —Alan J. Perlis The material in this book has been the basis of MIT’s entry-level computer science subject since 1980. We had been teaching this material for four years when the first edition was published, and twelve more years have elapsed until the appearance of this second edition. We are pleased that our work has been widely adopted and incorporated into other texts. We have seen our students take the ideas and programs in this book and build them in as the core of new computer systems and languages. In literal realization of an ancient Talmudic pun, our students have become our builders. We are lucky to have such capable students and such accomplished builders. In preparing this edition, we have incorporated hundreds of clarifications suggested by our own teaching experience and the comments of colleagues at MIT and elsewhere. We have redesigned most of the major programming systems in the book, including the generic-arithmetic system, the interpreters, the register-machine simulator and the compiler; and we have rewritten all the program examples to ensure that any Scheme implementation conforming to the IEEE Scheme standard (IEEE 1990) will be able to run the code. This edition emphasizes several new themes. The most important of these is the central role played by different approaches to dealing with time in computational models: objects with state, concurrent programming, 20 functional programming, lazy evaluation and nondeterministic programming. We have included new sections on concurrency and nondeterminism, and we have tried to integrate this theme throughout the book. The first edition of the book closely followed the syllabus of our MIT onesemester subject. With all the new material in the second edition, it will not be possible to cover everything in a single semester, so the instructor will have to pick and choose. In our own teaching, we sometimes skip the section on logic programming (Section 4.4), we have students use the registermachine simulator but we do not cover its implementation (Section 5.2), and we give only a cursory overview of the compiler (Section 5.5). Even so, this is still an intense course. Some instructors may wish to cover only the first three or four chapters, leaving the other material for subsequent courses. The World-Wide-Web site http://mitpress.mit.edu/sicp provides support for users of this book. This includes programs from the book, sample programming assignments, supplementary materials and downloadable implementations of the Scheme dialect of Lisp. Preface to the First Edition A computer is like a violin. You can imagine a novice trying first a phonograph and then a violin. The latter, he says, sounds terrible. That is the argument we have heard from our humanists and most of our computer scientists. Computer programs are good, they say, for particular purposes, but they aren’t flexible. Neither is a violin, or a typewriter, until you learn how to use it. —Marvin Minsky, “Why Programming Is a Good Medium for Expressing Poorly-Understood and Sloppily-Formulated Ideas” “The Structure and Interpretation of Computer Programs” is the entry-level subject in computer science at the Massachusetts Institute of Technology. It is required of all students at MIT who major in electrical engineering or in computer science, as one-fourth of the “common core curriculum,” which also includes two subjects on circuits and linear systems and a subject on the design of digital systems. We have been involved in the development of this subject since 1978, and we have taught this material in its present form since the fall of 1980 to between 600 and 700 students each year. Most of these students have had little or no prior formal training in computation, although many have played with computers a bit and a few have had extensive programming or hardware-design experience. Our design of this introductory computer-science subject reflects two major concerns. First, we want to establish the idea that a computer language is not just a way of getting a computer to perform operations but rather that it is a novel formal medium for expressing ideas about methodology. Thus, programs must be written for people to read and only incidentally for machines to execute. Second, we believe that the essential ma- 22 terial to be addressed by a subject at this level is not the syntax of particular programming-language constructs, nor clever algorithms for computing particular functions efficiently, nor even the mathematical analysis of algorithms and the foundations of computing, but rather the techniques used to control the intellectual complexity of large software systems. Our goal is that students who complete this subject should have a good feel for the elements of style and the aesthetics of programming. They should have command of the major techniques for controlling complexity in a large system. They should be capable of reading a 50-page-long program, if it is written in an exemplary style. They should know what not to read, and what they need not understand at any moment. They should feel secure about modifying a program, retaining the spirit and style of the original author. These skills are by no means unique to computer programming. The techniques we teach and draw upon are common to all of engineering design. We control complexity by building abstractions that hide details when appropriate. We control complexity by establishing conventional interfaces that enable us to construct systems by combining standard, wellunderstood pieces in a “mix and match” way. We control complexity by establishing new languages for describing a design, each of which emphasizes particular aspects of the design and deemphasizes others. Underlying our approach to this subject is our conviction that “computer science” is not a science and that its significance has little to do with computers. The computer revolution is a revolution in the way we think and in the way we express what we think. The essence of this change is the emergence of what might best be called procedural epistemology—the study of the structure of knowledge from an imperative point of view, as opposed 23 to the more declarative point of view taken by classical mathematical subjects. Mathematics provides a framework for dealing precisely with notions of “what is.” Computation provides a framework for dealing precisely with notions of “how to.” In teaching our material we use a dialect of the programming language Lisp. We never formally teach the language, because we don’t have to. We just use it, and students pick it up in a few days. This is one great advantage of Lisp-like languages: They have very few ways of forming compound expressions, and almost no syntactic structure. All of the formal properties can be covered in an hour, like the rules of chess. After a short time we forget about syntactic details of the language (because there are none) and get on with the real issues—figuring out what we want to compute, how we will decompose problems into manageable parts, and how we will work on the parts. Another advantage of Lisp is that it supports (but does not enforce) more of the large-scale strategies for modular decomposition of programs than any other language we know. We can make procedural and data abstractions, we can use higher-order functions to capture common patterns of usage, we can model local state using assignment and data mutation, we can link parts of a program with streams and delayed evaluation, and we can easily implement embedded languages. All of this is embedded in an interactive environment with excellent support for incremental program design, construction, testing and debugging. We thank all the generations of Lisp wizards, starting with John McCarthy, who have fashioned a fine tool of unprecedented power and elegance. Scheme, the dialect of Lisp that we use, is an attempt to bring together the power and elegance of Lisp and Algol. From Lisp we take the metalinguistic power that derives from the simple syntax, the uniform representa- 24 tion of programs as data objects and the garbage-collected heap-allocated data. From Algol we take lexical scoping and block structure, which are gifts from the pioneers of programming-language design who were on the Algol committee. We wish to cite John Reynolds and Peter Landin for their insights into the relationship of Church’s λ-calculus to the structure of programming languages. We also recognize our debt to the mathematicians who scouted out this territory decades before computers appeared on the scene. These pioneers include Alonzo Church, Barkley Rosser, Stephen Kleene and Haskell Curry. Acknowledgements We would like to thank the many people who have helped us develop this book and this curriculum. Our subject is a clear intellectual descendant of “6.231,” a wonderful subject on programming linguistics and the λ-calculus taught at MIT in the late 1960s by Jack Wozencraft and Arthur Evans, Jr. We owe a great debt to Robert Fano, who reorganized MIT’s introductory curriculum in electrical engineering and computer science to emphasize the principles of engineering design. He led us in starting out on this enterprise and wrote the first set of subject notes from which this book evolved. Much of the style and aesthetics of programming that we try to teach were developed in conjunction with Guy Lewis Steele Jr., who collaborated with Gerald Jay Sussman in the initial development of the Scheme language. In addition, David Turner, Peter Henderson, Dan Friedman, David Wise and Will Clinger have taught us many of the techniques of the functional programming community that appear in this book. Joel Moses taught us about structuring large systems. His experience with the Macsyma system for symbolic computation provided the insight that one should avoid complexities of control and concentrate on organizing the data to reflect the real structure of the world being modeled. Marvin Minsky and Seymour Papert formed many of our attitudes about programming and its place in our intellectual lives. To them we owe the understanding that computation provides a means of expression for exploring ideas that would otherwise be too complex to deal with precisely. They emphasize that a student’s ability to write and modify programs provides a powerful medium in which exploring becomes a natural activity. 26 We also strongly agree with Alan Perlis that programming is lots of fun and we had better be careful to support the joy of programming. Part of this joy derives from observing great masters at work. We are fortunate to have been apprentice programmers at the feet of Bill Gosper and Richard Greenblatt. It is difficult to identify all the people who have contributed to the development of our curriculum. We thank all the lecturers, recitation instructors and tutors who have worked with us over the past fifteen years and put in many extra hours on our subject, especially Bill Siebert, Albert Meyer, Joe Stoy, Randy Davis, Louis Braida, Eric Grimson, Rod Brooks, Lynn Stein and Peter Szolovits. We would like to specially acknowledge the outstanding teaching contributions of Franklyn Turbak, now at Wellesley; his work in undergraduate instruction set a standard that we can all aspire to. We are grateful to Jerry Saltzer and Jim Miller for helping us grapple with the mysteries of concurrency, and to Peter Szolovits and David McAllester for their contributions to the exposition of nondeterministic evaluation in Chap. 4. Many people have put in significant effort presenting this material at other universities. Some of the people we have worked closely with are Jacob Katzenelson at the Technion, Hardy Mayer at the University of California at Irvine, Joe Stoy at Oxford, Elisha Sacks at Purdue and Jan Komorowski at the Norwegian University of Science and Technology. We are exceptionally proud of our colleagues who have received major teaching awards for their adaptations of this subject at other universities, including Kenneth Yip at Yale, Brian Harvey at the University of California at Berkeley and Dan Huttenlocher at Cornell. 27 Al Moy´e arranged for us to teach this material to engineers at HewlettPackard and for the production of videotapes of these lectures. We would like to thank the talented instructors—in particular Jim Miller, Bill Siebert and Mike Eisenberg—who have designed continuing education courses incorporating these tapes and taught them at universities and industry all over the world. Many educators in other countries have put in significant work translating the first edition. Michel Briand, Pierre Chamard and Andr´e Pic produced a French edition; Susanne Daniels-Herold produced a German edition; and Fumio Motoyoshi produced a Japanese edition. We do not know who produced the Chinese edition, but we consider it an honor to have been selected as the subject of an “unauthorized” translation. It is hard to enumerate all the people who have made technical contributions to the development of the Scheme systems we use for instructional purposes. In addition to Guy Steele, principal wizards have included Chris Hanson, Joe Bowbeer, Jim Miller, Guillermo Rozas and Stephen Adams. Others who have put in significant time are Richard Stallman, Alan Bawden, Kent Pitman, Jon Taft, Neil Mayle, John Lamping, Gwyn Osnos, Tracy Larrabee, George Carrette, Soma Chaudhuri, Bill Chiarchiaro, Steven Kirsch, Leigh Klotz, Wayne Noss, Todd Cass, Patrick O’Donnell, Kevin Theobald, Daniel Weise, Kenneth Sinclair, Anthony Courtemanche, Henry M. Wu, Andrew Berlin and Ruth Shyu. Beyond the MIT implementation, we would like to thank the many people who worked on the IEEE Scheme standard, including William Clinger and Jonathan Rees, who edited the R4 RS, and Chris Haynes, David Bartley, Chris Hanson and Jim Miller, who prepared the IEEE standard. 28 Dan Friedman has been a long-time leader of the Scheme community. The community’s broader work goes beyond issues of language design to encompass significant educational innovations, such as the high-school curriculum based on EdScheme by Schemer’s Inc., and the wonderful books by Mike Eisenberg and by Brian Harvey and Matthew Wright. We appreciate the work of those who contributed to making this a real book, especially Terry Ehling, Larry Cohen and Paul Bethge at the MIT Press. Ella Mazel found the wonderful cover image. For the second edition we are particularly grateful to Bernard and Ella Mazel for help with the book design, and to David Jones, TEX wizard extraordinaire. We also are indebted to those readers who made penetrating comments on the new draft: Jacob Katzenelson, Hardy Mayer, Jim Miller and especially Brian Harvey, who did unto this book as Julie did unto his book Simply Scheme. Finally, we would like to acknowledge the support of the organizations that have encouraged this work over the years, including suppport from Hewlett-Packard, made possible by Ira Goldstein and Joel Birnbaum, and support from DARPA, made possible by Bob Kahn. 1 Building Abstractions with Procedures The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three: 1. Combining several simple ideas into one compound one, and thus all complex ideas are made. 2. The second is bringing two ideas, whether simple or complex, together, and setting them by one another so as to take a view of them at once, without uniting them into one, by which it gets all its ideas of relations. 3. The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all its general ideas are made. —John Locke, An Essay Concerning Human Understanding (1690) We are about to study the idea of a computational process. Computational processes are abstract beings that inhabit computers. As they evolve, processes manipulate other abstract things called data. The evolution of a process is directed by a pattern of rules called a program. People create programs to direct processes. In effect, we conjure the spirits of the computer with our spells. A computational process is indeed much like a sorcerer’s idea of a spirit. It cannot be seen or touched. It is not composed of matter at all. However, it is very real. It can perform intellectual work. It can answer questions. It can affect the world by disbursing money at a bank or by controlling a robot arm in a factory. The programs we use to conjure processes are like a sorcerer’s spells. They are carefully composed from symbolic expressions in arcane and esoteric programming languages that prescribe the tasks we want our processes to perform. 30 A computational process, in a correctly working computer, executes programs precisely and accurately. Thus, like the sorcerer’s apprentice, novice programmers must learn to understand and to anticipate the consequences of their conjuring. Even small errors (usually called bugs or glitches) in programs can have complex and unanticipated consequences. Fortunately, learning to program is considerably less dangerous than learning sorcery, because the spirits we deal with are conveniently contained in a secure way. Real-world programming, however, requires care, expertise and wisdom. A small bug in a computer-aided design program, for example, can lead to the catastrophic collapse of an airplane or a dam or the self-destruction of an industrial robot. Master software engineers have the ability to organize programs so that they can be reasonably sure that the resulting processes will perform the tasks intended. They can visualize the behavior of their systems in advance. They know how to structure programs so that unanticipated problems do not lead to catastrophic consequences, and when problems do arise, they can debug their programs. Well-designed computational systems, like well-designed automobiles or nuclear reactors, are designed in a modular manner, so that the parts can be constructed, replaced and debugged separately. Programming in Lisp We need an appropriate language for describing processes, and we will use for this purpose the programming language Lisp. Just as our everyday thoughts are usually expressed in our natural language (such as English, French, or Japanese), and descriptions of quantitative phenomena are expressed with mathematical notations, our procedural thoughts will be expressed in Lisp. Lisp was invented in the late 1950s as a formalism for reasoning about the use of certain kinds of logical expressions, called recursion equations, as a model for computation. The language was conceived by John McCarthy and is based on his paper “Recursive Functions of Symbolic Expressions and Their Computation by Machine” (McCarthy 1960). Despite its inception as a mathematical formalism, Lisp is a practical programming language. A Lisp interpreter is a machine that carries out processes described in the Lisp language. The first Lisp interpreter was implemented by McCarthy with the help of colleagues and students in the Artificial Intelligence Group of the MIT Research Laboratory of Electronics and in the MIT Computation Center.1 Lisp, whose name is an acronym for LISt Processing, was designed to provide symbol-manipulating capabilities for attacking programming problems such as the symbolic differentiation and integration of algebraic expressions. It included for this purpose new data objects known as atoms and lists, which most strikingly set it apart from all other languages of the period. 1 The Lisp 1 Programmer’s Manual appeared in 1960 and the Lisp 1.5 Manual (McCarthy et al. 1965) was published in 1962. The early history of Lisp is described in McCarthy 1978. 32 Lisp was not the product of a concerted design effort. Instead, it evolved informally in an experimental manner in response to users’ needs and to pragmatic implementation considerations. Lisp’s informal evolution has continued through the years, and the community of Lisp users has traditionally resisted attempts to promulgate any “official” definition of the language. This evolution, together with the flexibility and elegance of the initial conception, has enabled Lisp, which is the second oldest language in widespread use today (only Fortran is older), to continually adapt to encompass the most modern ideas about program design. Thus, Lisp is by now a family of dialects, which, while sharing most of the original features, may differ from one another in significant ways. The dialect of Lisp used in this book is called Scheme.2 2 The two dialects in which most major Lisp programs of the 1970s were written are MacLisp (Moon 1978; Pitman 1983), developed at the MIT Project MAC, and Interlisp (Teitelman 1974), developed at Bolt Beranek and Newman Inc. and the Xerox Palo Alto Research Center. Portable Standard Lisp (Hearn 1969; Griss 1981) was a Lisp dialect designed to be easily portable between different machines. MacLisp spawned a number of subdialects, such as Franz Lisp, which was developed at the University of California at Berkeley, and Zetalisp (Moon and Weinreb 1981), which was based on a special-purpose processor designed at the MIT Artificial Intelligence Laboratory to run Lisp very efficiently. The Lisp dialect used in this book, called Scheme (Steele and Sussman 1975), was invented in 1975 by Guy Lewis Steele Jr. and Gerald Jay Sussman of the MIT Artificial Intelligence Laboratory and later reimplemented for instructional use at MIT. Scheme became an IEEE standard in 1990 (IEEE 1990). The Common Lisp dialect (Steele 1982, Steele 1990) was developed by the Lisp community to combine features from the earlier Lisp dialects to make an industrial standard for Lisp. Common Lisp became an ANSI standard in 1994 (ANSI 1994). 33 Because of its experimental character and its emphasis on symbol manipulation, Lisp was at first very inefficient for numerical computations, at least in comparison with Fortran. Over the years, however, Lisp compilers have been developed that translate programs into machine code that can perform numerical computations reasonably efficiently. And for special applications, Lisp has been used with great effectiveness.3 Although Lisp has not yet overcome its old reputation as hopelessly inefficient, Lisp is now used in many applications where efficiency is not the central concern. For example, Lisp has become a language of choice for operating-system shell languages and for extension languages for editors and computer-aided design systems. If Lisp is not a mainstream language, why are we using it as the framework for our discussion of programming? Because the language possesses unique features that make it an excellent medium for studying important programming constructs and data structures and for relating them to the linguistic features that support them. The most significant of these features is the fact that Lisp descriptions of processes, called procedures, can themselves be represented and manipulated as Lisp data. The importance of this is that there are powerful program-design techniques that rely on the ability to blur the traditional distinction between “passive” data and 3 One such special application was a breakthrough computation of scientific importance—an integration of the motion of the Solar System that extended previous results by nearly two orders of magnitude, and demonstrated that the dynamics of the Solar System is chaotic. This computation was made possible by new integration algorithms, a special-purpose compiler and a special-purpose computer all implemented with the aid of software tools written in Lisp (Abelson et al. 1992; Sussman and Wisdom 1992). 34 “active” processes. As we shall discover, Lisp’s flexibility in handling procedures as data makes it one of the most convenient languages in existence for exploring these techniques. The ability to represent procedures as data also makes Lisp an excellent language for writing programs that must manipulate other programs as data, such as the interpreters and compilers that support computer languages. Above and beyond these considerations, programming in Lisp is great fun. 1.1 The Elements of Programming A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize our ideas about processes. Thus, when we describe a language, we should pay particular attention to the means that the language provides for combining simple ideas to form more complex ideas. Every powerful language has three mechanisms for accomplishing this: primitive expressions which represent the simplest entities the language is concerned with, means of combination by which compound elements are built from simpler ones, and means of abstraction by which compound elements can be named and manipulated as units. In programming, we deal with two kinds of elements: procedures and data. (Later we will discover that they are really not so distinct.) Informally, data is “stuff” that we want to manipulate, and procedures are descriptions 35 of the rules for manipulating the data. Thus, any powerful programming language should be able to describe primitive data and primitive procedures and should have methods for combining and abstracting procedures and data. In this chapter we will deal only with simple numerical data so that we can focus on the rules for building procedures.4 In later chapters we will see that these same rules allow us to build procedures to manipulate compound data as well. 1.1.1 Expressions One easy way to get started at programming is to examine some typical interactions with an interpreter for the Scheme dialect of Lisp. Imagine that you are sitting at a computer terminal. You type an expression, and the interpreter responds by displaying the result of its evaluating that expression. 4 The characterization of numbers as “simple data” is a barefaced bluff. In fact, the treatment of numbers is one of the trickiest and most confusing aspects of any programming language. Some typical issues involved are these: Some computer systems distinguish integers, such as 2, from real numbers, such as 2.71. Is the real number 2.00 different from the integer 2? Are the arithmetic operations used for integers the same as the operations used for real numbers? Does 6 divided by 2 produce 3, or 3.0? How large a number can we represent? How many decimal places of accuracy can we represent? Is the range of integers the same as the range of real numbers? Above and beyond these questions, of course, lies a collection of issues concerning roundoff and truncation errors – the entire science of numerical analysis. Since our focus in this book is on large-scale program design rather than on numerical techniques, we are going to ignore these problems. The numerical examples in this chapter will exhibit the usual roundoff behavior that one observes when using arithmetic operations that preserve a limited number of decimal places of accuracy in noninteger operations. 36 One kind of primitive expression you might type is a number. (More precisely, the expression that you type consists of the numerals that represent the number in base 10.) If you present Lisp with a number 486 the interpreter will respond by printing5 486 Expressions representing numbers may be combined with an expression representing a primitive procedure (such as + or *) to form a compound expression that represents the application of the procedure to those numbers. For example: (+ 137 349) 486 (- 1000 334) 666 (* 5 99) 495 (/ 10 5) 2 (+ 2.7 10) 12.7 Expressions such as these, formed by delimiting a list of expressions within parentheses in order to denote procedure application, are called combinations. The leftmost element in the list is called the operator, 5 Throughout this book, when we wish to emphasize the distinction between the in- put typed by the user and the response printed by the interpreter, we will show the latter in slanted characters. 37 and the other elements are called operands. The value of a combination is obtained by applying the procedure specified by the operator to the arguments that are the values of the operands. The convention of placing the operator to the left of the operands is known as prefix notation, and it may be somewhat confusing at first because it departs significantly from the customary mathematical convention. Prefix notation has several advantages, however. One of them is that it can accommodate procedures that may take an arbitrary number of arguments, as in the following examples: (+ 21 35 12 7) 75 (* 25 4 12) 1200 No ambiguity can arise, because the operator is always the leftmost element and the entire combination is delimited by the parentheses. A second advantage of prefix notation is that it extends in a straightforward way to allow combinations to be nested, that is, to have combinations whose elements are themselves combinations: (+ (* 3 5) (- 10 6)) 19 There is no limit (in principle) to the depth of such nesting and to the overall complexity of the expressions that the Lisp interpreter can evaluate. It is we humans who get confused by still relatively simple expressions such as (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) 38 which the interpreter would readily evaluate to be 57. We can help ourselves by writing such an expression in the form (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) following a formatting convention known as pretty-printing, in which each long combination is written so that the operands are aligned vertically. The resulting indentations display clearly the structure of the expression.6 Even with complex expressions, the interpreter always operates in the same basic cycle: It reads an expression from the terminal, evaluates the expression, and prints the result. This mode of operation is often expressed by saying that the interpreter runs in a read-eval-print loop. Observe in particular that it is not necessary to explicitly instruct the interpreter to print the value of the expression.7 6 Lisp systems typically provide features to aid the user in formatting expressions. Two especially useful features are one that automatically indents to the proper pretty-print position whenever a new line is started and one that highlights the matching left parenthesis whenever a right parenthesis is typed. 7 Lisp obeys the convention that every expression has a value. This convention, to- gether with the old reputation of Lisp as an inefficient language, is the source of the quip by Alan Perlis (paraphrasing Oscar Wilde) that “Lisp programmers know the value of everything but the cost of nothing.” 1.1.2 Naming and the Environment A critical aspect of a programming language is the means it provides for using names to refer to computational objects. We say that the name identifies a variable whose value is the object. In the Scheme dialect of Lisp, we name things with define. Typing (define size 2) causes the interpreter to associate the value 2 with the name size.8 Once the name size has been associated with the number 2, we can refer to the value 2 by name: size 2 (* 5 size) 10 Here are further examples of the use of define: (define pi 3.14159) (define radius 10) (* pi (* radius radius)) 314.159 (define circumference (* 2 pi radius)) circumference 62.8318 Define is our language’s simplest means of abstraction, for it allows us to use simple names to refer to the results of compound operations, such as the circumference computed above. In general, computational objects 8 In this book, we do not show the interpreter’s response to evaluating definitions, since this is highly implementation-dependent. 40 may have very complex structures, and it would be extremely inconvenient to have to remember and repeat their details each time we want to use them. Indeed, complex programs are constructed by building, step by step, computational objects of increasing complexity. The interpreter makes this step-by-step program construction particularly convenient because name-object associations can be created incrementally in successive interactions. This feature encourages the incremental development and testing of programs and is largely responsible for the fact that a Lisp program usually consists of a large number of relatively simple procedures. It should be clear that the possibility of associating values with symbols and later retrieving them means that the interpreter must maintain some sort of memory that keeps track of the name-object pairs. This memory is called the environment (more precisely the global environment, since we will see later that a computation may involve a number of different environments).9 1.1.3 Evaluating Combinations One of our goals in this chapter is to isolate issues about thinking procedurally. As a case in point, let us consider that, in evaluating combinations, the interpreter is itself following a procedure. To evaluate a combination, do the following: 1. Evaluate the subexpressions of the combination. 2. Apply the procedure that is the value of the leftmost subexpression (the operator) to the arguments that are the values of the other subexpressions (the operands). 9 Chapter 3 will show that this notion of environment is crucial, both for understand- ing how the interpreter works and for implementing interpreters. 41 Even this simple rule illustrates some important points about processes in general. First, observe that the first step dictates that in order to accomplish the evaluation process for a combination we must first perform the evaluation process on each element of the combination. Thus, the evaluation rule is recursive in nature; that is, it includes, as one of its steps, the need to invoke the rule itself.10 Notice how succinctly the idea of recursion can be used to express what, in the case of a deeply nested combination, would otherwise be viewed as a rather complicated process. For example, evaluating (* (+ 2 (* 4 6)) (+ 3 5 7)) requires that the evaluation rule be applied to four different combinations. We can obtain a picture of this process by representing the combination in the form of a tree, as shown in Figure 1.1. Each combination is represented by a node with branches corresponding to the operator and the operands of the combination stemming from it. The terminal nodes (that is, nodes with no branches stemming from them) represent either operators or numbers. Viewing evaluation in terms of the tree, we can imagine that the values of the operands percolate upward, starting from the terminal nodes and then combining at higher and higher levels. In general, we shall see that recursion is a very powerful technique for dealing with hierarchical, treelike ob10 It may seem strange that the evaluation rule says, as part of the first step, that we should evaluate the leftmost element of a combination, since at this point that can only be an operator such as + or * representing a built-in primitive procedure such as addition or multiplication. We will see later that it is useful to be able to work with combinations whose operators are themselves compound expressions. 42 jects. In fact, the “percolate values upward” form of the evaluation rule is an example of a general kind of process known as tree accumulation. Figure 1.1: Tree representation, showing the value of each subcombination. Next, observe that the repeated application of the first step brings us to the point where we need to evaluate, not combinations, but primitive expressions such as numerals, built-in operators, or other names. We take care of the primitive cases by stipulating that • the values of numerals are the numbers that they name, • the values of built-in operators are the machine instruction sequences that carry out the corresponding operations, and • the values of other names are the objects associated with those names in the environment. We may regard the second rule as a special case of the third one by stipulating that symbols such as + and * are also included in the global environment, and are associated with the sequences of machine instructions that 43 are their “values.” The key point to notice is the role of the environment in determining the meaning of the symbols in expressions. In an interactive language such as Lisp, it is meaningless to speak of the value of an expression such as (+ x 1) without specifying any information about the environment that would provide a meaning for the symbol x (or even for the symbol +). As we shall see in Chapter 3, the general notion of the environment as providing a context in which evaluation takes place will play an important role in our understanding of program execution. Notice that the evaluation rule given above does not handle definitions. For instance, evaluating (define x 3) does not apply define to two arguments, one of which is the value of the symbol x and the other of which is 3, since the purpose of the define is precisely to associate x with a value. (That is, (define x 3) is not a combination.) Such exceptions to the general evaluation rule are called special forms. Define is the only example of a special form that we have seen so far, but we will meet others shortly. Each special form has its own evaluation rule. The various kinds of expressions (each with its associated evaluation rule) constitute the syntax of the programming language. In comparison with most other programming languages, Lisp has a very simple syntax; that is, the evaluation rule for expressions can be described by a simple general rule together with specialized rules for a small number of special forms.11 11 Special syntactic forms that are simply convenient alternative surface structures for things that can be written in more uniform ways are sometimes called syntactic sugar, to use a phrase coined by Peter Landin. In comparison with users of other languages, Lisp programmers, as a rule, are less concerned with matters of syntax. (By contrast, examine any Pascal manual and notice how much of it is devoted to descriptions of syntax.) This disdain for syntax is due partly to the flexibility of Lisp, 1.1.4 Compound Procedures We have identified in Lisp some of the elements that must appear in any powerful programming language: • Numbers and arithmetic operations are primitive data and procedures. • Nesting of combinations provides a means of combining operations. • Definitions that associate names with values provide a limited means of abstraction. Now we will learn about procedure definitions, a much more powerful abstraction technique by which a compound operation can be given a name and then referred to as a unit. We begin by examining how to express the idea of “squaring.” We might say, “To square something, multiply it by itself.” This is expressed in our language as (define (square x) (* x x)) We can understand this in the following way: (define (square | To | square x) | something, We have here a compound procedure, which has been given the name square. The procedure represents the operation of multiplying something by itself. The thing to be multiplied is given a local name, x, which plays which makes it easy to change surface syntax, and partly to the observation that many “convenient” syntactic constructs, which make the language less uniform, end up causing more trouble than they are worth when programs become large and complex. In the words of Alan Perlis, “Syntactic sugar causes cancer of the semicolon.” 45 the same role that a pronoun plays in natural language. Evaluating the definition creates this compound procedure and associates it with the name square.12 The general form of a procedure definition is (define (〈name〉〈formal parameters〉) 〈body〉) The 〈name 〉 is a symbol to be associated with the procedure definition in the environment.13 The 〈formal parameters 〉 are the names used within the body of the procedure to refer to the corresponding arguments of the procedure. The 〈body 〉 is an expression that will yield the value of the procedure application when the formal parameters are replaced by the actual arguments to which the procedure is applied.14 The 〈name 〉 and the 〈formal parameters 〉 are grouped within parentheses, just as they would be in an actual call to the procedure being defined. Having defined square, we can now use it: (square 21) 441 12 Observe that there are two different operations being combined here: we are cre- ating the procedure, and we are giving it the name square. It is possible, indeed important, to be able to separate these two notions—to create procedures without naming them, and to give names to procedures that have already been created. We will see how to do this in Section 1.3.2. 13 Throughout this book, we will describe the general syntax of expressions by using italic symbols delimited by angle brackets—e.g., 〈name 〉—to denote the “slots” in the expression to be filled in when such an expression is actually used. 14 More generally, the body of the procedure can be a sequence of expressions. In this case, the interpreter evaluates each expression in the sequence in turn and returns the value of the final expression as the value of the procedure application. (square (+ 2 5)) 49 (square (square 3)) 81 We can also use square as a building block in defining other procedures. For example, x 2 + y 2 can be expressed as (+ (square x) (square y)) We can easily define a procedure sum-of-squares that, given any two numbers as arguments, produces the sum of their squares: (define (sum-of-squares x y) (+ (square x) (square y))) (sum-of-squares 3 4) 25 Now we can use sum-of-squares as a building block in constructing further procedures: (define (f a) (sum-of-squares (+ a 1) (* a 2))) (f 5) 136 Compound procedures are used in exactly the same way as primitive procedures. Indeed, one could not tell by looking at the definition of sum-of-squares given above whether square was built into the interpreter, like + and *, or defined as a compound procedure. 1.1.5 The Substitution Model for Procedure Application To evaluate a combination whose operator names a compound procedure, the interpreter follows much the same process as for combinations whose operators name primitive procedures, which we described in Section 1.1.3. That is, the interpreter evaluates the elements of the combination and applies the procedure (which is the value of the operator of the combination) to the arguments (which are the values of the operands of the combination). We can assume that the mechanism for applying primitive procedures to arguments is built into the interpreter. For compound procedures, the application process is as follows: To apply a compound procedure to arguments, evaluate the body of the procedure with each formal parameter replaced by the corresponding argument. To illustrate this process, let’s evaluate the combination (f 5) where f is the procedure defined in Section 1.1.4. We begin by retrieving the body of f: (sum-of-squares (+ a 1) (* a 2)) Then we replace the formal parameter a by the argument 5: (sum-of-squares (+ 5 1) (* 5 2)) Thus the problem reduces to the evaluation of a combination with two operands and an operator sum-of-squares. Evaluating this combination involves three subproblems. We must evaluate the operator to get the procedure to be applied, and we must evaluate the operands to get the arguments. Now (+ 5 1) produces 6 and (* 5 2) produces 10, so we must 48 apply the sum-of-squares procedure to 6 and 10. These values are substituted for the formal parameters x and y in the body of sum-of-squares, reducing the expression to (+ (square 6) (square 10)) If we use the definition of square, this reduces to (+ (* 6 6) (* 10 10)) which reduces by multiplication to (+ 36 100) and finally to 136 The process we have just described is called the substitution model for procedure application. It can be taken as a model that determines the “meaning” of procedure application, insofar as the procedures in this chapter are concerned. However, there are two points that should be stressed: • The purpose of the substitution is to help us think about procedure application, not to provide a description of how the interpreter really works. Typical interpreters do not evaluate procedure applications by manipulating the text of a procedure to substitute values for the formal parameters. In practice, the “substitution” is accomplished by using a local environment for the formal parameters. We will discuss this more fully in Chapter 3 and Chapter 4 when we examine the implementation of an interpreter in detail. • Over the course of this book, we will present a sequence of increasingly elaborate models of how interpreters work, culminating with a complete implementation of an interpreter and compiler in Chapter 5. The 49 substitution model is only the first of these models—a way to get started thinking formally about the evaluation process. In general, when modeling phenomena in science and engineering, we begin with simplified, incomplete models. As we examine things in greater detail, these simple models become inadequate and must be replaced by more refined models. The substitution model is no exception. In particular, when we address in Chapter 3 the use of procedures with “mutable data,” we will see that the substitution model breaks down and must be replaced by a more complicated model of procedure application.15 Applicative order versus normal order According to the description of evaluation given in Section 1.1.3, the interpreter first evaluates the operator and operands and then applies the resulting procedure to the resulting arguments. This is not the only way to perform evaluation. An alternative evaluation model would not evaluate the operands until their values were needed. Instead it would first substitute operand expressions for parameters until it obtained an expression involving only primitive operators, and would then perform the evaluation. If we used this method, the evaluation of (f 5) would proceed according to the sequence of expansions 15 Despite the simplicity of the substitution idea, it turns out to be surprisingly com- plicated to give a rigorous mathematical definition of the substitution process. The problem arises from the possibility of confusion between the names used for the formal parameters of a procedure and the (possibly identical) names used in the expressions to which the procedure may be applied. Indeed, there is a long history of erroneous definitions of substitution in the literature of logic and programming semantics. See Stoy 1977 for a careful discussion of substitution. 50 (sum-of-squares (+ 5 1) (* 5 2)) (+ (square (+ 5 1)) (square (* 5 2)) ) (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) followed by the reductions (+ (+ (* 6 6) (* 10 10)) 100) This gives the same answer as our previous evaluation model, but the process is different. In particular, the evaluations of (+ 5 1) and (* 5 2) are each performed twice here, corresponding to the reduction of the expression (* x x) with x replaced respectively by (+ 5 1) and (* 5 2). This alternative “fully expand and then reduce” evaluation method is known as normal-order evaluation, in contrast to the “evaluate the arguments and then apply” method that the interpreter actually uses, which is called applicative-order evaluation. It can be shown that, for procedure applications that can be modeled using substitution (including all the procedures in the first two chapters of this book) and that yield legitimate values, normal-order and applicative-order evaluation produce the same value. (See Exercise 1.5 for an instance of an “illegitimate” value where normalorder and applicative-order evaluation do not give the same result.) Lisp uses applicative-order evaluation, partly because of the additional efficiency obtained from avoiding multiple evaluations of expressions such as those illustrated with (+ 5 1) and (* 5 2) above and, more significantly, because normal-order evaluation becomes much more complicated to deal with when we leave the realm of procedures that can be modeled by substitution. On the other hand, normal-order evaluation 51 can be an extremely valuable tool, and we will investigate some of its implications in Chapter 3 and Chapter 4.16 1.1.6 Conditional Expressions and Predicates The expressive power of the class of procedures that we can define at this point is very limited, because we have no way to make tests and to perform different operations depending on the result of a test. For instance, we cannot define a procedure that computes the absolute value of a number by testing whether the number is positive, negative, or zero and taking different actions in the different cases according to the rule |x| = x, if −x, if This construct is called a case analysis, and there is a special form in Lisp for notating such a case analysis. It is called cond (which stands for “conditional”), and it is used as follows: (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)))) 16 In Chapter 3 we will introduce stream processing, which is a way of handling ap- parently “infinite” data structures by incorporating a limited form of normal-order evaluation. In Section 4.2 we will modify the Scheme interpreter to produce a normal-order variant of Scheme. 52 The general form of a conditional expression is (cond (〈p 1 〉〈e 1 〉) (〈p 2 〉〈e 2 〉) ... (〈p n 〉〈e n 〉)) consisting of the symbol cond followed by parenthesized pairs of expressions (〈p 〉〈e 〉) called clauses. The first expression in each pair is a predicate—that is, an expression whose value is interpreted as either true or false.17 Conditional expressions are evaluated as follows. The predicate 〈p 1 〉 is evaluated first. If its value is false, then 〈p 2 〉 is evaluated. If 〈p 2 〉’s value is also false, then 〈p 3 〉 is evaluated. This process continues until a predicate is found whose value is true, in which case the interpreter returns the value of the corresponding consequent expression 〈e 〉 of the clause as the value of the conditional expression. If none of the 〈p 〉’s is found to be true, the value of the cond is undefined. The word predicate is used for procedures that return true or false, as well as for expressions that evaluate to true or false. The absolute-value proce17 “Interpreted as either true or false” means this: In Scheme, there are two distin- guished values that are denoted by the constants #t and #f. When the interpreter checks a predicate’s value, it interprets #f as false. Any other value is treated as true. (Thus, providing #t is logically unnecessary, but it is convenient.) In this book we will use names true and false, which are associated with the values #t and #f respectively. 53 dure abs makes use of the primitive predicates >, < and =.18 These take two numbers as arguments and test whether the first number is, respectively, greater than, less than, or equal to the second number, returning true or false accordingly. Another way to write the absolute-value procedure is (define (abs x) (cond ((< x 0) (- x)) (else x))) which could be expressed in English as “If x is less than zero return −x ; otherwise return x .” Else is a special symbol that can be used in place of the 〈p 〉 in the final clause of a cond. This causes the cond to return as its value the value of the corresponding 〈e 〉 whenever all previous clauses have been bypassed. In fact, any expression that always evaluates to a true value could be used as the 〈p 〉 here. Here is yet another way to write the absolute-value procedure: (define (abs x) (if (< x 0) (- x) x)) This uses the special form if, a restricted type of conditional that can be used when there are precisely two cases in the case analysis. The general form of an if expression is (if 〈 predicate 〉〈 consequent 〉〈 alternative 〉) 18 Abs also uses the “minus” operator -, which, when used with a single operand, as in (- x), indicates negation. 54 To evaluate an if expression, the interpreter starts by evaluating the 〈predicate 〉 part of the expression. If the 〈predicate 〉 evaluates to a true value, the interpreter then evaluates the 〈 consequent 〉 and returns its value. Otherwise it evaluates the 〈alternative 〉 and returns its value.19 In addition to primitive predicates such as <, =, and >, there are logical composition operations, which enable us to construct compound predicates. The three most frequently used are these: • (and 〈e 1 〉 . . . 〈e n 〉) The interpreter evaluates the expressions 〈e 〉 one at a time, in left-toright order. If any 〈e 〉 evaluates to false, the value of the and expression is false, and the rest of the 〈e 〉’s are not evaluated. If all 〈e 〉’s evaluate to true values, the value of the and expression is the value of the last one. • (or 〈e 1 〉 . . . 〈e n 〉) The interpreter evaluates the expressions 〈e 〉 one at a time, in left-toright order. If any 〈e 〉 evaluates to a true value, that value is returned as the value of the or expression, and the rest of the 〈e 〉’s are not evaluated. If all 〈e 〉’s evaluate to false, the value of the or expression is false. • (not 〈e〉) The value of a not expression is true when the expression 〈e 〉 evaluates to false, and false otherwise. 19 A minor difference between if and cond is that the 〈e 〉 part of each cond may be a sequence of expressions. If the corresponding 〈p 〉 is found to be true, the expressions 〈e 〉 are evaluated in sequence and the value of the final expression in the sequence is returned as the value of the cond. In an if expression, however, the 〈consequent 〉 and 〈alternative 〉 must be single expressions. 55 Notice that and and or are special forms, not procedures, because the subexpressions are not necessarily all evaluated. Not is an ordinary procedure. As an example of how these are used, the condition that a number x be in the range 5 < x < 10 may be expressed as (and (> x 5) (< x 10)) As another example, we can define a predicate to test whether one number is greater than or equal to another as (define (>= x y) (or (> x y) (= x y))) or alternatively as (define (>= x y) (not (< x y))) Exercise 1.1: Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented. 10 (+ 5 3 4) (- 9 1) (/ 6 2) (+ (* 2 4) (- 4 6)) (define a 3) (define b (+ a 1)) (+ a b (* a b)) (= a b) 56 (if (and (> b a) (< b (* a b))) b a) (cond ((= a 4) 6) ((= b 4) (+ 6 7 a)) (else 25)) (+ 2 (if (> b a) b a)) (* (cond ((> a b) a) ((< a b) b) (else -1)) (+ a 1)) Exercise 1.2: Translate the following expression into prefix form. 5 + 4 + (2 − (3 − (6 + 54 ))) 3(6 − 2)(2 − 7) Exercise 1.3: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. Exercise 1.4: Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure: (define (a-plus-abs-b a b) ((if (> b 0) + -) a b)) Exercise 1.5: Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two 57 (define (p) (p)) (define (test x y) (if (= x 0) 0 y)) Then he evaluates the expression (test 0 (p)) What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.) 1.1.7 Example: Square Roots by Newton’s Method Procedures, as introduced above, are much like ordinary mathematical functions. They specify a value that is determined by one or more parameters. But there is an important difference between mathematical functions and computer procedures. Procedures must be effective. As a case in point, consider the problem of computing square roots. We can define the square-root function as x = the y such that y ≥ 0 and y 2 = x This describes a perfectly legitimate mathematical function. We could use it to recognize whether one number is the square root of another, or to derive facts about square roots in general. On the other hand, the definition does not describe a procedure. Indeed, it tells us almost nothing about how 58 to actually find the square root of a given number. It will not help matters to rephrase this definition in pseudo-Lisp: (define (sqrt x) (the y (and (>= y 0) (= (square y) x)))) This only begs the question. The contrast between function and procedure is a reflection of the general distinction between describing properties of things and describing how to do things, or, as it is sometimes referred to, the distinction between declarative knowledge and imperative knowledge. In mathematics we are usually concerned with declarative (what is) descriptions, whereas in computer science we are usually concerned with imperative (how to) descriptions.20 20 Declarative and imperative descriptions are intimately related, as indeed are math- ematics and computer science. For instance, to say that the answer produced by a program is “correct” is to make a declarative statement about the program. There is a large amount of research aimed at establishing techniques for proving that programs are correct, and much of the technical difficulty of this subject has to do with negotiating the transition between imperative statements (from which programs are constructed) and declarative statements (which can be used to deduce things). In a related vein, an important current area in programming-language design is the exploration of so-called very high-level languages, in which one actually programs in terms of declarative statements. The idea is to make interpreters sophisticated enough so that, given “what is” knowledge specified by the programmer, they can generate “how to” knowledge automatically. This cannot be done in general, but there are important areas where progress has been made. We shall revisit this idea in Chapter 4. 59 How does one compute square roots? The most common way is to use Newton’s method of successive approximations, which says that whenever we have a guess y for the value of the square root of a number x , we can perform a simple manipulation to get a better guess (one closer to the actual square root) by averaging y with x /y .21 For example, we can compute the square root of 2 as follows. Suppose our initial guess is 1: Guess (2/1) = 2 ((2 + 1)/2) = 1.5 (2/1.5) = 1.3333 ((1.3333 + 1.5)/2) = 1.4167 (2/1.4167) = 1.4118 ((1.4167 + 1.4118)/2) = 1.4142 Continuing this process, we obtain better and better approximations to the square root. Now let’s formalize the process in terms of procedures. We start with a value for the radicand (the number whose square root we are trying to compute) and a value for the guess. If the guess is good enough for our purposes, we are done; if not, we must repeat the process with an improved guess. We write this basic strategy as a procedure: (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) 21 This square-root algorithm is actually a special case of Newton’s method, which is a general technique for finding roots of equations. The square-root algorithm itself was developed by Heron of Alexandria in the first century A.D. We will see how to express the general Newton’s method as a Lisp procedure in Section 1.3.4. 60 A guess is improved by averaging it with the quotient of the radicand and the old guess: (define (improve guess x) (average guess (/ x guess))) where (define (average x y) (/ (+ x y) 2)) We also have to say what we mean by “good enough.” The following will do for illustration, but it is not really a very good test. (See Exercise 1.7.) The idea is to improve the answer until it is close enough so that its square differs from the radicand by less than a predetermined tolerance (here 0.001):22 (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) Finally, we need a way to get started. For instance, we can always guess that the square root of any number is 1:23 22 We will usually give predicates names ending with question marks, to help us re- member that they are predicates. This is just a stylistic convention. As far as the interpreter is concerned, the question mark is just an ordinary character. 23 Observe that we express our initial guess as 1.0 rather than 1. This would not make any difference in many Lisp implementations. MIT Scheme, however, distinguishes between exact integers and decimal values, and dividing two integers produces a rational number rather than a decimal. For example, dividing 10 by 6 yields 5/3, while dividing 10.0 by 6.0 yields 1.6666666666666667. (We will learn how to implement arithmetic on rational numbers in Section 2.1.1.) If we start with an initial guess of 1 in our square-root program, and x is an exact integer, all subsequent values produced in the square-root computation will be rational numbers rather 61 (define (sqrt x) (sqrt-iter 1.0 x)) If we type these definitions to the interpreter, we can use sqrt just as we can use any procedure: (sqrt 9) 3.00009155413138 (sqrt (+ 100 37)) 11.704699917758145 (sqrt (+ (sqrt 2) (sqrt 3))) 1.7739279023207892 (square (sqrt 1000)) 1000.000369924366 The sqrt program also illustrates that the simple procedural language we have introduced so far is sufficient for writing any purely numerical program that one could write in, say, C or Pascal. This might seem surprising, since we have not included in our language any iterative (looping) constructs that direct the computer to do something over and over again. Sqrt-iter, on the other hand, demonstrates how iteration can be accom- than decimals. Mixed operations on rational numbers and decimals always yield decimals, so starting with an initial guess of 1.0 forces all subsequent values to be decimals. 62 plished using no special construct other than the ordinary ability to call a procedure.24 Exercise 1.6: Alyssa P. Hacker doesn’t see why if needs to be provided as a special form. “Why can’t I just define it as an ordinary procedure in terms of cond?” she asks. Alyssa’s friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if: (define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause))) Eva demonstrates the program for Alyssa: (new-if (= 2 3) 0 5) 5 (new-if (= 1 1) 0 5) 0 Delighted, Alyssa uses new-if to rewrite the square-root program: (define (sqrt-iter guess x) (new-if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) 24 Readers who are worried about the efficiency issues involved in using procedure calls to implement iteration should note the remarks on “tail recursion” in Section 1.2.1. 63 What happens when Alyssa attempts to use this to compute square roots? Explain. Exercise 1.7: The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. An alternative strategy for implementing good-enough? is to watch how guess changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers? Exercise 1.8: Newton’s method for cube roots is based on the fact that if y is an approximation to the cube root of x , then a better approximation is given by the value x /y 2 + 2y 3 Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In Section 1.3.4 we will see how to implement Newton’s method in general as an abstraction of these square-root and cube-root procedures.) 1.1.8 Procedures as Black-Box Abstractions Sqrt is our first example of a process defined by a set of mutually defined procedures. Notice that the definition of sqrt-iter is recursive; that is, the procedure is defined in terms of itself. The idea of being able to define a procedure in terms of itself may be disturbing; it may seem unclear 64 how such a “circular” definition could make sense at all, much less specify a well-defined process to be carried out by a computer. This will be addressed more carefully in Section 1.2. But first let’s consider some other important points illustrated by the sqrt example. Observe that the problem of computing square roots breaks up naturally into a number of subproblems: how to tell whether a guess is good enough, how to improve a guess, and so on. Each of these tasks is accomplished by a separate procedure. The entire sqrt program can be viewed as a cluster of procedures (shown in Figure 1.2) that mirrors the decomposition of the problem into subproblems. sqrt | sqrt-iter / good-enough / square \ abs \ improve \ average Figure 1.2: Procedural decomposition of the sqrt program. The importance of this decomposition strategy is not simply that one is dividing the program into parts. After all, we could take any large program and divide it into parts—the first ten lines, the next ten lines, the next ten lines, and so on. Rather, it is crucial that each procedure accomplishes an identifiable task that can be used as a module in defining other procedures. For example, when we define the good-enough? procedure in terms of square, we are able to regard the square procedure as a “black box.” 65 We are not at that moment concerned with how the procedure computes its result, only with the fact that it computes the square. The details of how the square is computed can be suppressed, to be considered at a later time. Indeed, as far as the good-enough? procedure is concerned, square is not quite a procedure but rather an abstraction of a procedure, a so-called procedural abstraction. At this level of abstraction, any procedure that computes the square is equally good. Thus, considering only the values they return, the following two procedures for squaring a number should be indistinguishable. Each takes a numerical argument and produces the square of that number as the value.25 (define (square x) (* x x)) (define (square x) (exp (double (log x)))) (define (double x) (+ x x)) So a procedure definition should be able to suppress detail. The users of the procedure may not have written the procedure themselves, but may have obtained it from another programmer as a black box. A user should not need to know how the procedure is implemented in order to use it. 25 It is not even clear which of these procedures is a more efficient implementation. This depends upon the hardware available. There are machines for which the “obvious” implementation is the less efficient one. Consider a machine that has extensive tables of logarithms and antilogarithms stored in a very efficient manner. Local names One detail of a procedure’s implementation that should not matter to the user of the procedure is the implementer’s choice of names for the procedure’s formal parameters. Thus, the following procedures should not be distinguishable: (define (square x) (* x x)) (define (square y) (* y y)) This principle—that the meaning of a procedure should be independent of the parameter names used by its author—seems on the surface to be selfevident, but its consequences are profound. The simplest consequence is that the parameter names of a procedure must be local to the body of the procedure. For example, we used square in the definition of good-enough? in our square-root procedure: (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) The intention of the author of good-enough? is to determine if the square of the first argument is within a given tolerance of the second argument. We see that the author of good-enough? used the name guess to refer to the first argument and x to refer to the second argument. The argument of square is guess. If the author of square used x (as above) to refer to that argument, we see that the x in good-enough? must be a different x than the one in square. Running the procedure square must not affect the value of x that is used by good-enough?, because that value of x may be needed by good-enough? after square is done computing. If the parameters were not local to the bodies of their respective procedures, then the parameter x in square could be confused with the param- 67 eter x in good-enough?, and the behavior of good-enough? would depend upon which version of square we used. Thus, square would not be the black box we desired. A formal parameter of a procedure has a very special role in the procedure definition, in that it doesn’t matter what name the formal parameter has. Such a name is called a bound variable, and we say that the procedure definition binds its formal parameters. The meaning of a procedure definition is unchanged if a bound variable is consistently renamed throughout the definition.26 If a variable is not bound, we say that it is free. The set of expressions for which a binding defines a name is called the scope of that name. In a procedure definition, the bound variables declared as the formal parameters of the procedure have the body of the procedure as their scope. In the definition of good-enough? above, guess and x are bound variables but <, -, abs, and square are free. The meaning of good-enough? should be independent of the names we choose for guess and x so long as they are distinct and different from <, -, abs, and square. (If we renamed guess to abs we would have introduced a bug by capturing the variable abs. It would have changed from free to bound.) The meaning of good-enough? is not independent of the names of its free variables, however. It surely depends upon the fact (external to this definition) that the symbol abs names a procedure for computing the absolute value of a number. Good-enough? will compute a different function if we substitute cos for abs in its definition. 26 The concept of consistent renaming is actually subtle and difficult to define for- mally. Famous logicians have made embarrassing errors here. Internal definitions and block structure We have one kind of name isolation available to us so far: The formal parameters of a procedure are local to the body of the procedure. The squareroot program illustrates another way in which we would like to control the use of names. The existing program consists of separate procedures: (define (sqrt x) (sqrt-iter 1.0 x)) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) The problem with this program is that the only procedure that is important to users of sqrt is sqrt. The other procedures (sqrt-iter, good-enough?, and improve) only clutter up their minds. They may not define any other procedure called good-enough? as part of another program to work together with the square-root program, because sqrt needs it. The problem is especially severe in the construction of large systems by many separate programmers. For example, in the construction of a large library of numerical procedures, many numerical functions are computed as successive approximations and thus might have procedures 69 named good-enough? and improve as auxiliary procedures. We would like to localize the subprocedures, hiding them inside sqrt so that sqrt could coexist with other successive approximations, each having its own private good-enough? procedure. To make this possible, we allow a procedure to have internal definitions that are local to that procedure. For example, in the square-root problem we can write (define (sqrt x) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (sqrt-iter 1.0 x)) Such nesting of definitions, called block structure, is basically the right solution to the simplest name-packaging problem. But there is a better idea lurking here. In addition to internalizing the definitions of the auxiliary procedures, we can simplify them. Since x is bound in the definition of sqrt, the procedures good-enough?, improve, and sqrt-iter, which are defined internally to sqrt, are in the scope of x. Thus, it is not necessary to pass x explicitly to each of these procedures. Instead, we allow x to be a free variable in the internal definitions, as shown below. Then x gets its value 70 from the argument with which the enclosing procedure sqrt is called. This discipline is called lexical scoping.27 (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0)) We will use block structure extensively to help us break up large programs into tractable pieces.28 The idea of block structure originated with the programming language Algol 60. It appears in most advanced programming languages and is an important tool for helping to organize the construction of large programs. 27 Lexical scoping dictates that free variables in a procedure are taken to refer to bind- ings made by enclosing procedure definitions; that is, they are looked up in the environment in which the procedure was defined. We will see how this works in detail in chapter 3 when we study environments and the detailed behavior of the interpreter. 28 Embedded definitions must come first in a procedure body. The management is not responsible for the consequences of running programs that intertwine definition and use. 1.2 Procedures and the Processes They Generate We have now considered the elements of programming: We have used primitive arithmetic operations, we have combined these operations, and we have abstracted these composite operations by defining them as compound procedures. But that is not enough to enable us to say that we know how to program. Our situation is analogous to that of someone who has learned the rules for how the pieces move in chess but knows nothing of typical openings, tactics, or strategy. Like the novice chess player, we don’t yet know the common patterns of usage in the domain. We lack the knowledge of which moves are worth making (which procedures are worth defining). We lack the experience to predict the consequences of making a move (executing a procedure). The ability to visualize the consequences of the actions under consideration is crucial to becoming an expert programmer, just as it is in any synthetic, creative activity. In becoming an expert photographer, for example, one must learn how to look at a scene and know how dark each region will appear on a print for each possible choice of exposure and development conditions. Only then can one reason backward, planning framing, lighting, exposure, and development to obtain the desired effects. So it is with programming, where we are planning the course of action to be taken by a process and where we control the process by means of a program. To become experts, we must learn to visualize the processes generated by various types of procedures. Only after we have developed such a skill can we learn to reliably construct programs that exhibit the desired behavior. A procedure is a pattern for the local evolution of a computational process. It specifies how each stage of the process is built upon the previous stage. We would like to be able to make statements about the overall, or 72 global, behavior of a process whose local evolution has been specified by a procedure. This is very difficult to do in general, but we can at least try to describe some typical patterns of process evolution. In this section we will examine some common “shapes” for processes generated by simple procedures. We will also investigate the rates at which these processes consume the important computational resources of time and space. The procedures we will consider are very simple. Their role is like that played by test patterns in photography: as oversimplified prototypical patterns, rather than practical examples in their own right. 1.2.1 Linear Recursion and Iteration We begin by considering the factorial function, defined by n ! = n · (n − 1) · (n − 2) · · · 3 · 2 · 1 There are many ways to compute factorials. One way is to make use of the observation that n ! is equal to n times (n − 1)! for any positive integer n : n ! = n · [(n − 1) · (n − 2) · · · 3 · 2 · 1] = n · (n − 1)! Thus, we can compute n ! by computing (n − 1)! and multiplying the result by n . If we add the stipulation that 1! is equal to 1, this observation translates directly into a procedure: (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) We can use the substitution model of Section 1.1.5 to watch this procedure in action computing 6!, as shown in Figure 1.3. 73 Now let’s take a different perspective on computing factorials. We could describe a rule for computing n ! by specifying that we first multiply 1 by 2, then multiply the result by 3, then by 4, and so on until we reach n . More formally, we maintain a running product, together with a counter that counts from 1 up to n . We can describe the computation by saying that the counter and the product simultaneously change from one step to the next according to the rule product counter ← ← counter counter * + product 1 and stipulating that n ! is the value of the product when the counter exceeds n. Figure 1.3: A linear recursive process for computing 6!. 74 Once again, we can recast our description as a procedure for computing factorials:29 (define (factorial n) (fact-iter 1 1 n)) (define (fact-iter product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count))) As before, we can use the substitution model to visualize the process of computing 6!, as shown in Figure 1.4. 29 In a real program we would probably use the block structure introduced in the last section to hide the definition of fact-iter: (define (factorial n) (define (iter product counter) (if (> counter n) product (iter (* counter product) (+ counter 1)))) (iter 1 1)) We avoided doing this here so as to minimize the number of things to think about at once. Figure 1.4: A linear iterative process for computing 6!. Compare the two processes. From one point of view, they seem hardly different at all. Both compute the same mathematical function on the same domain, and each requires a number of steps proportional to n to compute n !. Indeed, both processes even carry out the same sequence of multiplications, obtaining the same sequence of partial products. On the other hand, when we consider the “shapes” of the two processes, we find that they evolve quite differently. Consider the first process. The substitution model reveals a shape of expansion followed by contraction, indicated by the arrow in Figure 1.3. The expansion occurs as the process builds up a chain of deferred operations (in this case, a chain of multiplications). The contraction occurs as the operations are actually performed. This type of process, characterized by a chain of deferred operations, is called a recursive process. Carrying out this process requires that the interpreter keep track of the operations to be performed later on. In the computation of n !, the length of the chain of deferred multiplications, and hence the amount of information needed 76 keep track of it, grows linearly with n (is proportional to n ), just like the number of steps. Such a process is called a linear recursive process. By contrast, the second process does not grow and shrink. At each step, all we need to keep track of, for any n , are the current values of the variables product, counter, and max-count. We call this an iterative process. In general, an iterative process is one whose state can be summarized by a fixed number of state variables, together with a fixed rule that describes how the state variables should be updated as the process moves from state to state and an (optional) end test that specifies conditions under which the process should terminate. In computing n !, the number of steps required grows linearly with n . Such a process is called a linear iterative process. The contrast between the two processes can be seen in another way. In the iterative case, the program variables provide a complete description of the state of the process at any point. If we stopped the computation between steps, all we would need to do to resume the computation is to supply the interpreter with the values of the three program variables. Not so with the recursive process. In this case there is some additional “hidden” information, maintained by the interpreter and not contained in the program variables, which indicates “where the process is” in negotiating the chain of deferred operations. The longer the chain, the more information must be maintained.30 30 When we discuss the implementation of procedures on register machines in Chapter 5, we will see that any iterative process can be realized “in hardware” as a machine that has a fixed set of registers and no auxiliary memory. In contrast, realizing a recursive process requires a machine that uses an auxiliary data structure known as a stack. 77 In contrasting iteration and recursion, we must be careful not to confuse the notion of a recursive process with the notion of a recursive procedure. When we describe a procedure as recursive, we are referring to the syntactic fact that the procedure definition refers (either directly or indirectly) to the procedure itself. But when we describe a process as following a pattern that is, say, linearly recursive, we are speaking about how the process evolves, not about the syntax of how a procedure is written. It may seem disturbing that we refer to a recursive procedure such as fact-iter as generating an iterative process. However, the process really is iterative: Its state is captured completely by its three state variables, and an interpreter need keep track of only three variables in order to execute the process. One reason that the distinction between process and procedure may be confusing is that most implementations of common languages (including Ada, Pascal, and C) are designed in such a way that the interpretation of any recursive procedure consumes an amount of memory that grows with the number of procedure calls, even when the process described is, in principle, iterative. As a consequence, these languages can describe iterative processes only by resorting to special-purpose “looping constructs” such as do, repeat, until, for, and while. The implementation of Scheme we shall consider in Chapter 5 does not share this defect. It will execute an iterative process in constant space, even if the iterative process is described by a recursive procedure. An implementation with this property is called tailrecursive. With a tail-recursive implementation, iteration can be expressed using the ordinary procedure call mechanism, so that special iteration constructs are useful only as syntactic sugar.31 31 Tail recursion has long been known as a compiler optimization trick. A coherent semantic basis for tail recursion was provided by Carl Hewitt (1977), who explained 78 Exercise 1.9: Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1. (define (+ a b) (if (= a 0) b (inc (+ (dec a) b)))) (define (+ a b) (if (= a 0) b (+ (dec a) (inc b)))) Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). Are these processes iterative or recursive? Exercise 1.10: The following procedure computes a mathematical function called Ackermann’s function. (define (A x y) (cond ((= y 0) 0) ((= x 0) (* 2 y)) it in terms of the “message-passing” model of computation that we shall discuss in Chapter 3. Inspired by this, Gerald Jay Sussman and Guy Lewis Steele Jr. (see Steele and Sussman 1975) constructed a tail-recursive interpreter for Scheme. Steele later showed how tail recursion is a consequence of the natural way to compile procedure calls (Steele 1977). The IEEE standard for Scheme requires that Scheme implementations be tail-recursive. 79 ((= y 1) 2) (else (A (- x 1) (A x (- y 1)))))) What are the values of the following expressions? (A 1 10) (A 2 4) (A 3 3) Consider the following procedures, where A is the procedure defined above: (define (f n) (A 0 n)) (define (g n) (A 1 n)) (define (h n) (A 2 n)) (define (k n) (* 5 n n)) Give concise mathematical definitions for the functions computed by the procedures f, g, and h for positive integer values of n . For example, (k n) computes 5n 2 . 1.2.2 Tree Recursion Another common pattern of computation is called tree recursion. As an example, consider computing the sequence of Fibonacci numbers, in which each number is the sum of the preceding two: 0, 1, 1, 2, 3, 5, 8, 13, 21, . . . In general, the Fibonacci numbers can be defined by the rule Fib(n ) = Fib(n − 1) + Fib(n − 2) n=0 n=1 otherwise if if 80 We can immediately translate this definition into a recursive procedure for computing Fibonacci numbers: (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) Consider the pattern of this computation. To compute (fib 5), we compute (fib 4) and (fib 3). To compute (fib 4), we compute (fib 3) and (fib 2). In general, the evolved process looks like a tree, as shown in Figure 1.5. Notice that the branches split into two at each level (except at the bottom); this reflects the fact that the fib procedure calls itself twice each time it is invoked. This procedure is instructive as a prototypical tree recursion, but it is a terrible way to compute Fibonacci numbers because it does so much redundant computation. Notice in Figure 1.5 that the entire computation of (fib 3)—almost half the work—is duplicated. In fact, it is not hard to show that the number of times the procedure will compute (fib 1) or (fib 0) (the number of leaves in the above tree, in general) is precisely Fib(n + 1). To get an idea of how bad this is, one can show that the value of Fib(n ) grows exponentially with n . More precisely (see Exercise 1.13), p Fib(n ) is the closest integer to ϕn / 5, where p 1+ 5 ϕ= ≈ 1.6180 2 is the golden ratio, which satisfies the equation ϕ2 = ϕ + 1 Figure 1.5: The tree-recursive process generated in computing (fib 5). Thus, the process uses a number of steps that grows exponentially with the input. On the other hand, the space required grows only linearly with the input, because we need keep track only of which nodes are above us in the tree at any point in the computation. In general, the number of steps required by a tree-recursive process will be proportional to the number of nodes in the tree, while the space required will be proportional to the maximum depth of the tree. We can also formulate an iterative process for computing the Fibonacci numbers. The idea is to use a pair of integers a and b , initialized to Fib(1) = 1 and Fib(0) = 0, and to repeatedly apply the simultaneous transformations a ← a+b b←a 82 It is not hard to show that, after applying this transformation n times, a and b will be equal, respectively, to Fib(n +1) and Fib(n ). Thus, we can compute Fibonacci numbers iteratively using the procedure (define (fib n) (fib-iter 1 0 n)) (define (fib-iter a b count) (if (= count 0) b (fib-iter (+ a b) a (- count 1)))) This second method for computing Fib(n ) is a linear iteration. The difference in number of steps required by the two methods—one linear in n , one growing as fast as Fib(n ) itself—is enormous, even for small inputs. One should not conclude from this that tree-recursive processes are useless. When we consider processes that operate on hierarchically structured data rather than numbers, we will find that tree recursion is a natural and powerful tool.32 But even in numerical operations, tree-recursive processes can be useful in helping us to understand and design programs. For instance, although the first fib procedure is much less efficient than the second one, it is more straightforward, being little more than a translation into Lisp of the definition of the Fibonacci sequence. To formulate the iterative algorithm required noticing that the computation could be recast as an iteration with three state variables. 32 An example of this was hinted at in Section 1.1.3: The interpreter itself evaluates expressions using a tree-recursive process. Example: Counting change It takes only a bit of cleverness to come up with the iterative Fibonacci algorithm. In contrast, consider the following problem: How many different ways can we make change of $1.00, given half-dollars, quarters, dimes, nickels, and pennies? More generally, can we write a procedure to compute the number of ways to change any given amount of money? This problem has a simple solution as a recursive procedure. Suppose we think of the types of coins available as arranged in some order. Then the following relation holds: The number of ways to change amount a using n kinds of coins equals • the number of ways to change amount a using all but the first kind of coin, plus • the number of ways to change amount a − d using all n kinds of coins, where d is the denomination of the first kind of coin. To see why this is true, observe that the ways to make change can be divided into two groups: those that do not use any of the first kind of coin, and those that do. Therefore, the total number of ways to make change for some amount is equal to the number of ways to make change for the amount without using any of the first kind of coin, plus the number of ways to make change assuming that we do use the first kind of coin. But the latter number is equal to the number of ways to make change for the amount that remains after using a coin of the first kind. Thus, we can recursively reduce the problem of changing a given amount to the problem of changing smaller amounts using fewer kinds of coins. Consider this reduction rule carefully, and convince yourself that we 84 can use it to describe an algorithm if we specify the following degenerate cases:33 • If a is exactly 0, we should count that as 1 way to make change. • If a is less than 0, we should count that as 0 ways to make change. • If n is 0, we should count that as 0 ways to make change. We can easily translate this description into a recursive procedure: (define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((or (< amount 0) (= kinds-of-coins 0)) 0) (else (+ (cc amount (- kinds-of-coins 1)) (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins))))) (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) ((= kinds-of-coins 2) 5) ((= kinds-of-coins 3) 10) ((= kinds-of-coins 4) 25) ((= kinds-of-coins 5) 50))) (The first-denomination procedure takes as input the number of kinds of coins available and returns the denomination of the first kind. Here we 33 For example, work through in detail how the reduction rule applies to the problem of making change for 10 cents using pennies and nickels. 85 are thinking of the coins as arranged in order from largest to smallest, but any order would do as well.) We can now answer our original question about changing a dollar: (count-change 100) 292 Count-change generates a tree-recursive process with redundancies similar to those in our first implementation of fib. (It will take quite a while for that 292 to be computed.) On the other hand, it is not obvious how to design a better algorithm for computing the result, and we leave this problem as a challenge. The observation that a tree-recursive process may be highly inefficient but often easy to specify and understand has led people to propose that one could get the best of both worlds by designing a “smart compiler” that could transform tree-recursive procedures into more efficient procedures that compute the same result.34 Exercise 1.11: A function f is defined by the rule that f (n ) = n if n < 3 and f (n ) = f (n − 1) + 2 f (n − 2) + 3 f (n − 3) if n ≥ 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process. 34 One approach to coping with redundant computations is to arrange matters so that we automatically construct a table of values as they are computed. Each time we are asked to apply the procedure to some argument, we first look to see if the value is already stored in the table, in which case we avoid performing the redundant computation. This strategy, known as tabulation or memoization, can be implemented in a straightforward way. Tabulation can sometimes be used to transform processes that require an exponential number of steps (such as count-change) into processes whose space and time requirements grow linearly with the input. See Exercise 3.27. 86 Exercise 1.12: The following pattern of numbers is called Pascal’s triangle. 1 1 1 1 1 The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it.35 Write a procedure that computes elements of Pascal’s triangle by means of a recursive process. p Exercise 1.13: Prove that Fib(n ) is the closest integer to ϕn / 5, p p where ϕ = (1 + 5)/2. Hint: Let ψ = (1 − 5)/2. Use induction and the definition of the Fibonacci numbers (see Section 1.2.2) to p n n prove that Fib(n ) = (ϕ − ψ )/ 5. 1.2.3 Orders of Growth The previous examples illustrate that processes can differ considerably in the rates at which they consume computational resources. One convenient 35 The elements of Pascal’s triangle are called the binomial coefficients, because the n th row consists of the coefficients of the terms in the expansion of (x + y )n . This pattern for computing the coefficients appeared in Blaise Pascal’s 1653 seminal work on probability theory, Trait´e du triangle arithm´etique. According to Knuth (1973), the same pattern appears in the Szu-yuen Yu-chien (“The Precious Mirror ¨ of the Four Elements”), published by the Chinese mathematician Chu Shih-chieh in 1303, in the works of the twelfth-century Persian poet and mathematician Omar Khayyam, and in the works of the twelfth-century Hindu mathematician Bh´ascara ´ arya. Ach´ 87 way to describe this difference is to use the notion of order of growth to obtain a gross measure of the resources required by a process as the inputs become larger. Let n be a parameter that measures the size of the problem, and let R (n ) be the amount of resources the process requires for a problem of size n . In our previous examples we took n to be the number for which a given function is to be computed, but there are other possibilities. For instance, if our goal is to compute an approximation to the square root of a number, we might take n to be the number of digits accuracy required. For matrix multiplication we might take n to be the number of rows in the matrices. In general there are a number of properties of the problem with respect to which it will be desirable to analyze a given process. Similarly, R (n ) might measure the number of internal storage registers used, the number of elementary machine operations performed, and so on. In computers that do only a fixed number of operations at a time, the time required will be proportional to the number of elementary machine operations performed. We say that R (n ) has order of growth Θ( f (n )), written R (n ) = Θ( f (n )) (pronounced “theta of f (n )”), if there are positive constants k 1 and k 2 independent of n such that k 1 f (n ) ≤ R (n ) ≤ k 2 f (n ) for any sufficiently large value of n . (In other words, for large n , the value R (n ) is sandwiched between k 1 f (n ) and k 2 f (n ).) For instance, with the linear recursive process for computing factorial described in Section 1.2.1 the number of steps grows proportionally to the input n . Thus, the steps required for this process grows as Θ(n ). We also saw that the space required grows as Θ(n ). For the iterative factorial, the 88 number of steps is still Θ(n ) but the space is Θ(1)—that is, constant.36 The tree-recursive Fibonacci computation requires Θ(ϕn ) steps and space Θ(n ), where ϕ is the golden ratio described in Section 1.2.2. Orders of growth provide only a crude description of the behavior of a process. For example, a process requiring n 2 steps and a process requiring 1000n 2 steps and a process requiring 3n 2 + 10n + 17 steps all have Θ(n 2 ) order of growth. On the other hand, order of growth provides a useful indication of how we may expect the behavior of the process to change as we change the size of the problem. For a Θ(n ) (linear) process, doubling the size will roughly double the amount of resources used. For an exponential process, each increment in problem size will multiply the resource utilization by a constant factor. In the remainder of Section 1.2 we will examine two algorithms whose order of growth is logarithmic, so that doubling the problem size increases the resource requirement by a constant amount. Exercise 1.14: Draw the tree illustrating the process generated by the count-change procedure of Section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases? Exercise 1.15: The sine of an angle (specified in radians) can be computed by making use of the approximation sin x ≈ x if x is 36 These statements mask a great deal of oversimplification. For instance, if we count process steps as “machine operations” we are making the assumption that the number of machine operations needed to perform, say, a multiplication is independent of the size of the numbers to be multiplied, which is false if the numbers are sufficiently large. Similar remarks hold for the estimates of space. Like the design and description of a process, the analysis of a process can be carried out at various levels of abstraction. 89 sufficiently small, and the trigonometric identity x x sin x = 3 sin − 4 sin3 3 3 to reduce the size of the argument of sin. (For purposes of this exercise an angle is considered “sufficiently small” if its magnitude is not greater than 0.1 radians.) These ideas are incorporated in the following procedures: (define (cube x) (* x x x)) (define (p x) (- (* 3 x) (* 4 (cube x)))) (define (sine angle) (if (not (> (abs angle) 0.1)) angle (p (sine (/ angle 3.0))))) a. How many times is the procedure p applied when (sine 12.15) is evaluated? b. What is the order of growth in space and number of steps (as a function of a ) used by the process generated by the sine procedure when (sine a) is evaluated? 1.2.4 Exponentiation Consider the problem of computing the exponential of a given number. We would like a procedure that takes as arguments a base b and a positive integer exponent n and computes b n . One way to do this is via the recursive definition b n = b · b n−1 b0 = 1 90 which translates readily into the procedure (define (expt b n) (if (= n 0) 1 (* b (expt b (- n 1))))) This is a linear recursive process, which requires Θ(n ) steps and Θ(n ) space. Just as with factorial, we can readily formulate an equivalent linear iteration: (define (expt b n) (expt-iter b n 1)) (define (expt-iter b counter product) (if (= counter 0) product (expt-iter b (- counter 1) (* b product)))) This version requires Θ(n ) steps and Θ(1) space. We can compute exponentials in fewer steps by using successive squaring. For instance, rather than computing b 8 as b · (b · (b · (b · (b · (b · (b · b )))))) we can compute it using three multiplications: b2 = b · b b4 = b2 · b2 b8 = b4 · b4 91 This method works fine for exponents that are powers of 2. We can also take advantage of successive squaring in computing exponentials in general if we use the rule b n = (b n/2 )2 if n is even b n = b · b n−1 if n is odd We can express this method as a procedure: (define (fast-expt b n) (cond ((= n 0) 1) ((even? n) (square (fast-expt b (/ n 2)))) (else (* b (fast-expt b (- n 1)))))) where the predicate to test whether an integer is even is defined in terms of the primitive procedure remainder by (define (even? n) (= (remainder n 2) 0)) The process evolved by fast-expt grows logarithmically with n in both space and number of steps. To see this, observe that computing b 2n using fast-expt requires only one more multiplication than computing b n . The size of the exponent we can compute therefore doubles (approximately) with every new multiplication we are allowed. Thus, the number of multiplications required for an exponent of n grows about as fast as the logarithm of n to the base 2. The process has Θ(log n ) growth.37 37 More precisely, the number of multiplications required is equal to 1 less than the log base 2 of n plus the number of ones in the binary representation of n . This total is always less than twice the log base 2 of n . The arbitrary constants k 1 and k 2 in the definition of order notation imply that, for a logarithmic process, the base to which logarithms are taken does not matter, so all such processes are described as Θ(log n ). 92 The difference between Θ(log n ) growth and Θ(n ) growth becomes striking as n becomes large. For example, fast-expt for n = 1000 requires only 14 multiplications.38 It is also possible to use the idea of successive squaring to devise an iterative algorithm that computes exponentials with a logarithmic number of steps (see Exercise 1.16), although, as is often the case with iterative algorithms, this is not written down so straightforwardly as the recursive algorithm.39 Exercise 1.16: Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of steps, as does fast-expt. (Hint: Using the observation that (b n/2 )2 = (b 2 )n/2 , keep, along with the exponent n and the base b , an additional state variable a , and define the state transformation in such a way that the product ab n is unchanged from state to state. At the beginning of the process a is taken to be 1, and the answer is given by the value of a at the end of the process. In general, the technique of defining an invariant quantity that remains unchanged from state to state is a powerful way to think about the design of iterative algorithms.) Exercise 1.17: The exponentiation algorithms in this section are based on performing exponentiation by means of repeated multiplication. In a similar way, one can perform integer multiplication by means of repeated addition. The following multiplication pro38 You may wonder why anyone would care about raising numbers to the 1000th power. See Section 1.2.6. 39 This iterative algorithm is ancient. It appears in the Chandah-sutra by Ach´ ´ arya Pingala, written before 200 B.C. See Knuth 1981, section 4.6.3, for a full discussion and analysis of this and other methods of exponentiation. 93 cedure (in which it is assumed that our language can only add, not multiply) is analogous to the expt procedure: (define (* a b) (if (= b 0) 0 (+ a (* a (- b 1))))) This algorithm takes a number of steps that is linear in b. Now suppose we include, together with addition, operations double, which doubles an integer, and halve, which divides an (even) integer by 2. Using these, design a multiplication procedure analogous to fast-expt that uses a logarithmic number of steps. Exercise 1.18: Using the results of Exercise 1.16 and Exercise 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps.40 Exercise 1.19: There is a clever algorithm for computing the Fibonacci numbers in a logarithmic number of steps. Recall the transformation of the state variables a and b in the fib-iter process of Section 1.2.2: a ← a + b and b ← a . Call this transformation T , and observe that applying T over and over again n times, starting with 1 and 0, produces the pair Fib(n + 1) and Fib(n ). In other words, the Fibonacci numbers are produced by applying T n , the n th power of the transformation T , starting 40 This algorithm, which is sometimes known as the “Russian peasant method” of multiplication, is ancient. Examples of its use are found in the Rhind Papyrus, one of the two oldest mathematical documents in existence, written about 1700 B.C. (and copied from an even older document) by an Egyptian scribe named A’h-mose. 94 with the pair (1, 0). Now consider T to be the special case of p = 0 and q = 1 in a family of transformations T pq , where T pq transforms the pair (a, b ) according to a ← bq + aq + ap and b ← bp + aq . Show that if we apply such a transformation T pq twice, the effect is the same as using a single transformation T p 0 q 0 of the same form, and compute p 0 and q 0 in terms of p and q . This gives us an explicit way to square these transformations, and thus we can compute T n using successive squaring, as in the fast-expt procedure. Put this all together to complete the following procedure, which runs in a logarithmic number of steps:41 (define (fib n) (fib-iter 1 0 0 1 n)) (define (fib-iter a b p q count) (cond ((= count 0) b) ((even? count) (fib-iter a b ; compute p’ ; compute q’ (/ count 2))) (else (fib-iter (+ (* b q) (* a q) (* a p)) (+ (* b p) (* a q)) p 41 This exercise was suggested to us by Joe Stoy, based on an example in Kaldewaij 95 q (- count 1))))) 1.2.5 Greatest Common Divisors The greatest common divisor (GCD) of two integers a and b is defined to be the largest integer that divides both a and b with no remainder. For example, the GCD of 16 and 28 is 4. In Chapter 2, when we investigate how to implement rational-number arithmetic, we will need to be able to compute GCDs in order to reduce rational numbers to lowest terms. (To reduce a rational number to lowest terms, we must divide both the numerator and the denominator by their GCD. For example, 16/28 reduces to 4/7.) One way to find the GCD of two integers is to factor them and search for common factors, but there is a famous algorithm that is much more efficient. The idea of the algorithm is based on the observation that, if r is the remainder when a is divided by b , then the common divisors of a and b are precisely the same as the common divisors of b and r . Thus, we can use the equation GCD(a,b) = GCD(b,r) to successively reduce the problem of computing a GCD to the problem of computing the GCD of smaller and smaller pairs of integers. For example, GCD(206,40) = GCD(40,6) = GCD(6,4) = GCD(4,2) = GCD (2,0) = 2 reduces GCD(206, 40) to GCD(2, 0), which is 2. It is possible to show that starting with any two positive integers and performing repeated reductions 96 will always eventually produce a pair where the second number is 0. Then the GCD is the other number in the pair. This method for computing the GCD is known as Euclid’s Algorithm.42 It is easy to express Euclid’s Algorithm as a procedure: (define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) This generates an iterative process, whose number of steps grows as the logarithm of the numbers involved. The fact that the number of steps required by Euclid’s Algorithm has logarithmic growth bears an interesting relation to the Fibonacci numbers: Lam´e’s Theorem: If Euclid’s Algorithm requires k steps to compute the GCD of some pair, then the smaller number in the pair must be greater than or equal to the k th Fibonacci number.43 42 Euclid’s Algorithm is so called because it appears in Euclid’s Elements (Book 7, ca. 300 B.C.). According to Knuth (1973), it can be considered the oldest known nontrivial algorithm. The ancient Egyptian method of multiplication (Exercise 1.18) is surely older, but, as Knuth explains, Euclid’s algorithm is the oldest known to have been presented as a general algorithm, rather than as a set of illustrative examples. 43 This theorem was proved in 1845 by Gabriel Lam´ e, a French mathematician and engineer known chiefly for his contributions to mathematical physics. To prove the theorem, we consider pairs (a k , b k ), where a k ≥ b k , for which Euclid’s Algorithm terminates in k steps. The proof is based on the claim that, if (a k +1 , b k +1 ) → (a k , b k ) → (a k−1 , b k−1 ) are three successive pairs in the reduction process, then we must have b k +1 ≥ b k + b k−1 . To verify the claim, consider that a reduction step is defined by applying the transformation a k−1 = b k , b k−1 = remainder of a k divided 97 We can use this theorem to get an order-of-growth estimate for Euclid’s Algorithm. Let n be the smaller of the two inputs to the procedure. If the p process takes k steps, then we must have n ≥ Fib(k ) ≈ ϕk / 5. Therefore the number of steps k grows as the logarithm (to the base ϕ) of n . Hence, the order of growth is Θ(log n ). Exercise 1.20: The process that a procedure generates is of course dependent on the rules used by the interpreter. As an example, consider the iterative gcd procedure given above. Suppose we were to interpret this procedure using normal-order evaluation, as discussed in Section 1.1.5. (The normal-order-evaluation rule for if is described in Exercise 1.5.) Using the substitution method (for normal order), illustrate the process generated in evaluating (gcd 206 40) and indicate the remainder operations that are actually performed. How many remainder operations are actually performed in the normal-order evaluation of (gcd 206 40)? In the applicative-order evaluation? by b k . The second equation means that a k = qb k + b k−1 for some positive integer q . And since q must be at least 1 we have a k = qb k + b k−1 ≥ b k + b k−1 . But in the previous reduction step we have b k +1 = a k . Therefore, b k +1 = a k ≥ b k + b k−1 . This verifies the claim. Now we can prove the theorem by induction on k , the number of steps that the algorithm requires to terminate. The result is true for k = 1, since this merely requires that b be at least as large as Fib(1) = 1. Now, assume that the result is true for all integers less than or equal to k and establish the result for k + 1. Let (a k +1 , b k +1 ) → (a k , b k ) → (a k−1 , b k−1 ) be successive pairs in the reduction process. By our induction hypotheses, we have b k−1 ≥ Fib(k − 1) and b k ≥ Fib(k ). Thus, applying the claim we just proved together with the definition of the Fibonacci numbers gives b k +1 ≥ b k + b k−1 ≥ Fib(k ) + Fib(k − 1) = Fib(k + 1), which completes the proof of Lam´e’s Theorem. 1.2.6 Example: Testing for Primality This section describes two methods for checking the primality of an integer p n , one with order of growth Θ( n ), and a “probabilistic” algorithm with order of growth Θ(log n ). The exercises at the end of this section suggest programming projects based on these algorithms. Searching for divisors Since ancient times, mathematicians have been fascinated by problems concerning prime numbers, and many people have worked on the problem of determining ways to test if numbers are prime. One way to test if a number is prime is to find the number’s divisors. The following program finds the smallest integral divisor (greater than 1) of a given number n . It does this in a straightforward way, by testing n for divisibility by successive integers starting with 2. (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0)) We can test whether a number is prime as follows: n is prime if and only if n is its own smallest divisor. (define (prime? n) 99 (= n (smallest-divisor n))) The end test for find-divisor is based on the fact that if n is not prime p it must have a divisor less than or equal to n .44 This means that the algop rithm need only test divisors between 1 and n . Consequently, the number p of steps required to identify n as prime will have order of growth Θ( n ). The Fermat test The Θ(log n ) primality test is based on a result from number theory known as Fermat’s Little Theorem.45 Fermat’s Little Theorem: If n is a prime number and a is any positive integer less than n , then a raised to the n th power is congruent to a modulo n . (Two numbers are said to be congruent modulo n if they both have the same remainder when divided by n . The remainder of a number a when 44 If d is a divisor of n , then so is n /d . But d and n /d cannot both be greater than pn . 45 Pierre de Fermat (1601-1665) is considered to be the founder of modern number theory. He obtained many important number-theoretic results, but he usually announced just the results, without providing his proofs. Fermat’s Little Theorem was stated in a letter he wrote in 1640. The first published proof was given by Euler in 1736 (and an earlier, identical proof was discovered in the unpublished manuscripts of Leibniz). The most famous of Fermat’s results—known as Fermat’s Last Theorem—was jotted down in 1637 in his copy of the book Arithmetic (by the thirdcentury Greek mathematician Diophantus) with the remark “I have discovered a truly remarkable proof, but this margin is too small to contain it.” Finding a proof of Fermat’s Last Theorem became one of the most famous challenges in number theory. A complete solution was finally given in 1995 by Andrew Wiles of Princeton University. 100 divided by n is also referred to as the remainder of a modulo n , or simply as a modulo n .) If n is not prime, then, in general, most of the numbers a < n will not satisfy the above relation. This leads to the following algorithm for testing primality: Given a number n , pick a random number a < n and compute the remainder of a n modulo n . If the result is not equal to a , then n is certainly not prime. If it is a , then chances are good that n is prime. Now pick another random number a and test it with the same method. If it also satisfies the equation, then we can be even more confident that n is prime. By trying more and more values of a , we can increase our confidence in the result. This algorithm is known as the Fermat test. To implement the Fermat test, we need a procedure that computes the exponential of a number modulo another number: (define (expmod base exp m) (cond ((= exp 0) 1) ((even? exp) (remainder (square (expmod base (/ exp 2) m)) m)) (else (remainder (* base (expmod base (- exp 1) m)) m)))) This is very similar to the fast-expt procedure of Section 1.2.4. It uses successive squaring, so that the number of steps grows logarithmically with the exponent.46 46 The reduction steps in the cases where the exponent e is greater than 1 are based on the fact that, for any integers x , y , and m , we can find the remainder of x times y 101 The Fermat test is performed by choosing at random a number a between 1 and n − 1 inclusive and checking whether the remainder modulo n of the n th power of a is equal to a . The random number a is chosen using the procedure random, which we assume is included as a primitive in Scheme. Random returns a nonnegative integer less than its integer input. Hence, to obtain a random number between 1 and n − 1, we call random with an input of n − 1 and add 1 to the result: (define (fermat-test n) (define (try-it a) (= (expmod a n n) a)) (try-it (+ 1 (random (- n 1))))) The following procedure runs the test a given number of times, as specified by a parameter. Its value is true if the test succeeds every time, and false otherwise. (define (fast-prime? n times) (cond ((= times 0) true) ((fermat-test n) (fast-prime? n (- times 1))) (else false))) Probabilistic methods The Fermat test differs in character from most familiar algorithms, in which one computes an answer that is guaranteed to be correct. Here, modulo m by computing separately the remainders of x modulo m and y modulo m , multiplying these, and then taking the remainder of the result modulo m . For instance, in the case where e is even, we compute the remainder of b e/2 modulo m , square this, and take the remainder modulo m . This technique is useful because it means we can perform our computation without ever having to deal with numbers much larger than m . (Compare Exercise 1.25.) 102 the answer obtained is only probably correct. More precisely, if n ever fails the Fermat test, we can be certain that n is not prime. But the fact that n passes the test, while an extremely strong indication, is still not a guarantee that n is prime. What we would like to say is that for any number n , if we perform the test enough times and find that n always passes the test, then the probability of error in our primality test can be made as small as we like. Unfortunately, this assertion is not quite correct. There do exist numbers that fool the Fermat test: numbers n that are not prime and yet have the property that a n is congruent to a modulo n for all integers a < n . Such numbers are extremely rare, so the Fermat test is quite reliable in practice.47 There are variations of the Fermat test that cannot be fooled. In these tests, as with the Fermat method, one tests the primality of an integer n by choosing a random integer a < n and checking some condition that depends upon n and a . (See Exercise 1.28 for an example of such a test.) On the other hand, in contrast to the Fermat test, one can prove that, for any n , the condition does not hold for most of the integers a < n unless n is prime. Thus, if n passes the test for some random choice of a , the 47 Numbers that fool the Fermat test are called Carmichael numbers, and little is known about them other than that they are extremely rare. There are 255 Carmichael numbers below 100,000,000. The smallest few are 561, 1105, 1729, 2465, 2821, and 6601. In testing primality of very large numbers chosen at random, the chance of stumbling upon a value that fools the Fermat test is less than the chance that cosmic radiation will cause the computer to make an error in carrying out a “correct” algorithm. Considering an algorithm to be inadequate for the first reason but not for the second illustrates the difference between mathematics and engineering. 103 chances are better than even that n is prime. If n passes the test for two random choices of a , the chances are better than 3 out of 4 that n is prime. By running the test with more and more randomly chosen values of a we can make the probability of error as small as we like. The existence of tests for which one can prove that the chance of error becomes arbitrarily small has sparked interest in algorithms of this type, which have come to be known as probabilistic algorithms. There is a great deal of research activity in this area, and probabilistic algorithms have been fruitfully applied to many fields.48 Exercise 1.21: Use the smallest-divisor procedure to find the smallest divisor of each of the following numbers: 199, 1999, 19999. Exercise 1.22: Most Lisp implementations include a primitive called runtime that returns an integer that specifies the amount of time the system has been running (measured, for example, in microseconds). The following timed-prime-test procedure, when called with an integer n , prints n and checks to see if n is prime. If n is prime, the procedure prints three asterisks followed by the amount of time used in performing the test. 48 One of the most striking applications of probabilistic prime testing has been to the field of cryptography. Although it is now computationally infeasible to factor an arbitrary 200-digit number, the primality of such a number can be checked in a few seconds with the Fermat test. This fact forms the basis of a technique for constructing “unbreakable codes” suggested by Rivest et al. (1977). The resulting RSA algorithm has become a widely used technique for enhancing the security of electronic communications. Because of this and related developments, the study of prime numbers, once considered the epitome of a topic in “pure” mathematics to be studied only for its own sake, now turns out to have important practical applications to cryptography, electronic funds transfer, and information retrieval. 104 (define (timed-prime-test n) (newline) (display n) (start-prime-test n (runtime))) (define (start-prime-test n start-time) (if (prime? n) (report-prime (- (runtime) start-time)))) (define (report-prime elapsed-time) (display " *** ") (display elapsed-time)) Using this procedure, write a procedure search-for-primes that checks the primality of consecutive odd integers in a specified range. Use your procedure to find the three smallest primes larger than 1000; larger than 10,000; larger than 100,000; larger than 1,000,000. Note the time needed to test each prime. Since the p testing algorithm has order of growth of Θ( n ), you should expect p that testing for primes around 10,000 should take about 10 times as long as testing for primes around 1000. Do your timing data bear this out? How well do the data for 100,000 and 1,000,000 p support the Θ( n ) prediction? Is your result compatible with the notion that programs on your machine run in time proportional to the number of steps required for the computation? Exercise 1.23: The smallest-divisor procedure shown at the start of this section does lots of needless testing: After it checks to see if the number is divisible by 2 there is no point in checking to see if it is divisible by any larger even numbers. This suggests that the values used for test-divisor should not be 2, 3, 4, 5, 6, . . . , 105 but rather 2, 3, 5, 7, 9, . . . . To implement this change, define a procedure next that returns 3 if its input is equal to 2 and otherwise returns its input plus 2. Modify the smallest-divisor procedure to use (next test-divisor) instead of (+ test-divisor 1). With timed-prime-test incorporating this modified version of smallest-divisor, run the test for each of the 12 primes found in Exercise 1.22. Since this modification halves the number of test steps, you should expect it to run about twice as fast. Is this expectation confirmed? If not, what is the observed ratio of the speeds of the two algorithms, and how do you explain the fact that it is different from 2? Exercise 1.24: Modify the timed-prime-test procedure of Exercise 1.22 to use fast-prime? (the Fermat method), and test each of the 12 primes you found in that exercise. Since the Fermat test has Θ(log n ) growth, how would you expect the time to test primes near 1,000,000 to compare with the time needed to test primes near 1000? Do your data bear this out? Can you explain any discrepancy you find? Exercise 1.25: Alyssa P. Hacker complains that we went to a lot of extra work in writing expmod. After all, she says, since we already know how to compute exponentials, we could have simply written (define (expmod base exp m) (remainder (fast-expt base exp) m)) Is she correct? Would this procedure serve as well for our fast prime tester? Explain. Exercise 1.26: Louis Reasoner is having great difficulty doing Exercise 1.24. His fast-prime? test seems to run more slowly than 106 his prime? test. Louis calls his friend Eva Lu Ator over to help. When they examine Louis’s code, they find that he has rewritten the expmod procedure to use an explicit multiplication, rather than calling square: (define (expmod base exp m) (cond ((= exp 0) 1) ((even? exp) (remainder (* (expmod base (/ exp 2) m) (expmod base (/ exp 2) m)) m)) (else (remainder (* base (expmod base (- exp 1) m)) m)))) “I don’t see what difference that could make,” says Louis. “I do.” says Eva. “By writing the procedure like that, you have transformed the Θ(log n ) process into a Θ(n ) process.” Explain. Exercise 1.27: Demonstrate that the Carmichael numbers listed in Footnote 1.47 really do fool the Fermat test. That is, write a procedure that takes an integer n and tests whether a n is congruent to a modulo n for every a < n , and try your procedure on the given Carmichael numbers. Exercise 1.28: One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermat’s Little Theorem, which states that if n is a prime number and a is any positive integer less than n , 107 then a raised to the (n − 1)-st power is congruent to 1 modulo n . To test the primality of a number n by the Miller-Rabin test, we pick a random number a < n and raise a to the (n − 1)-st power modulo n using the expmod procedure. However, whenever we perform the squaring step in expmod, we check to see if we have discovered a “nontrivial square root of 1 modulo n ,” that is, a number not equal to 1 or n − 1 whose square is equal to 1 modulo n . It is possible to prove that if such a nontrivial square root of 1 exists, then n is not prime. It is also possible to prove that if n is an odd number that is not prime, then, for at least half the numbers a < n , computing a n−1 in this way will reveal a nontrivial square root of 1 modulo n . (This is why the Miller-Rabin test cannot be fooled.) Modify the expmod procedure to signal if it discovers a nontrivial square root of 1, and use this to implement the Miller-Rabin test with a procedure analogous to fermat-test. Check your procedure by testing various known primes and non-primes. Hint: One convenient way to make expmod signal is to have it return 0. 1.3 Formulating Abstractions with Higher-Order Procedures We have seen that procedures are, in effect, abstractions that describe compound operations on numbers independent of the particular numbers. For example, when we (define (cube x) (* x x x)) we are not talking about the cube of a particular number, but rather about a method for obtaining the cube of any number. Of course we could get 108 along without ever defining this procedure, by always writing expressions such as (* 3 3 3) (* x x x) (* y y y) and never mentioning cube explicitly. This would place us at a serious disadvantage, forcing us to work always at the level of the particular operations that happen to be primitives in the language (multiplication, in this case) rather than in terms of higher-level operations. Our programs would be able to compute cubes, but our language would lack the ability to express the concept of cubing. One of the things we should demand from a powerful programming language is the ability to build abstractions by assigning names to common patterns and then to work in terms of the abstractions directly. Procedures provide this ability. This is why all but the most primitive programming languages include mechanisms for defining procedures. Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures that manipulate procedures are called higher-order procedures. This section shows how higher-order procedures can serve as powerful abstraction mechanisms, vastly increasing the expressive power of our language. 1.3.1 Procedures as Arguments Consider the following three procedures. The first computes the sum of the integers from a through b: (define (sum-integers a b) (if (> a b) 0 (+ a (sum-integers (+ a 1) The second computes the sum of the cubes of the integers in the given range: (define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b)))) The third computes the sum of a sequence of terms in the series 1 1 1 + + + ... 1 · 3 5 · 7 9 · 11 which converges to π/8 (very slowly):49 (define (pi-sum a b) (if (> a b) 0 (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b)))) These three procedures clearly share a common underlying pattern. They are for the most part identical, differing only in the name of the 49 This series, usually written in the equivalent form π = 1 − 1 + 1 − 1 + . . ., is due 4 5 7 3 to Leibniz. We’ll see how to use this as the basis for some fancy numerical tricks in Section 3.5.3. 110 procedure, the function of a used to compute the term to be added, and the function that provides the next value of a. We could generate each of the procedures by filling in slots in the same template: (define (〈name〉 a b) (if (> a b) 0 (+ (〈term〉 a) (〈name〉 (〈next〉 a) b)))) The presence of such a common pattern is strong evidence that there is a useful abstraction waiting to be brought to the surface. Indeed, mathematicians long ago identified the abstraction of summation of a series and invented “sigma notation,” for example b X f (n ) = f (a ) + . . . + f (b ) n =a to express this concept. The power of sigma notation is that it allows mathematicians to deal with the concept of summation itself rather than only with particular sums—for example, to formulate general results about sums that are independent of the particular series being summed. Similarly, as program designers, we would like our language to be powerful enough so that we can write a procedure that expresses the concept of summation itself rather than only procedures that compute particular sums. We can do so readily in our procedural language by taking the common template shown above and transforming the “slots” into formal parameters: (define (sum term a next b) (if (> a b) 111 0 (+ (term a) (sum term (next a) next b)))) Notice that sum takes as its arguments the lower and upper bounds a and b together with the procedures term and next. We can use sum just as we would any procedure. For example, we can use it (along with a procedure inc that increments its argument by 1) to define sum-cubes: (define (inc n) (+ n 1)) (define (sum-cubes a b) (sum cube a inc b)) Using this, we can compute the sum of the cubes of the integers from 1 to 10: (sum-cubes 1 10) 3025 With the aid of an identity procedure to compute the term, we can define sum-integers in terms of sum: (define (identity x) x) (define (sum-integers a b) (sum identity a inc b)) Then we can add up the integers from 1 to 10: (sum-integers 1 10) 55 112 We can also define pi-sum in the same way:50 (define (pi-sum a b) (define (pi-term x) (/ 1.0 (* x (+ x 2)))) (define (pi-next x) (+ x 4)) (sum pi-term a pi-next b)) Using these procedures, we can compute an approximation to π: (* 8 (pi-sum 1 1000)) 3.139592655589783 Once we have sum, we can use it as a building block in formulating further concepts. For instance, the definite integral of a function f between the limits a and b can be approximated numerically using the formula b f = f a dx a+ 2 dx a + dx + 2 dx a + 2dx + 2 + . . . dx for small values of dx . We can express this directly as a procedure: (define (integral f a b dx) (define (add-dx x) (+ x dx)) (* (sum f (+ a (/ dx 2.0)) add-dx b) dx)) (integral cube 0 1 0.01) .24998750000000042 50 Notice that we have used block structure (Section 1.1.8) to embed the definitions of pi-next and pi-term within pi-sum, since these procedures are unlikely to be useful for any other purpose. We will see how to get rid of them altogether in Section 1.3.2. (integral cube 0 1 0.001) .249999875000001 (The exact value of the integral of cube between 0 and 1 is 1/4.) Exercise 1.29: Simpson’s Rule is a more accurate method of numerical integration than the method illustrated above. Using Simpson’s Rule, the integral of a function f between a and b is approximated as h ( y 0 + 4 y 1 + 2 y 2 + 4 y 3 + 2 y 4 + . . . + 2 y n−2 + 4 y n−1 + y n ) 3 where h = (b − a )/n , for some even integer n , and y k = f (a + kh ). (Increasing n increases the accuracy of the approximation.) Define a procedure that takes as arguments f , a , b , and n and returns the value of the integral, computed using Simpson’s Rule. Use your procedure to integrate cube between 0 and 1 (with n = 100 and n = 1000), and compare the results to those of the integral procedure shown above. Exercise 1.30: The sum procedure above generates a linear recursion. The procedure can be rewritten so that the sum is performed iteratively. Show how to do this by filling in the missing expressions in the following definition: (define (sum term a next b) (define (iter a result) (if 〈??〉 〈??〉 (iter 〈??〉〈??〉))) (iter 〈??〉〈??〉)) 114 Exercise 1.31: a. The sum procedure is only the simplest of a vast number of similar abstractions that can be captured as higher-order procedures.51 Write an analogous procedure called product that returns the product of the values of a function at points over a given range. Show how to define factorial in terms of product. Also use product to compute approximations to π using the formula52 π 2 · 4 · 4 · 6 · 6 · 8... = 4 3 · 3 · 5 · 5 · 7 · 7... b. If your product procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process. Exercise 1.32: a. Show that sum and product (Exercise 1.31) are both special cases of a still more general notion called accumulate that combines a collection of terms, using some general accumulation function: 51 The intent of Exercise 1.31 through Exercise 1.33 is to demonstrate the expressive power that is attained by using an appropriate abstraction to consolidate many seemingly disparate operations. However, though accumulation and filtering are elegant ideas, our hands are somewhat tied in using them at this point since we do not yet have data structures to provide suitable means of combination for these abstractions. We will return to these ideas in Section 2.2.3 when we show how to use sequences as interfaces for combining filters and accumulators to build even more powerful abstractions. We will see there how these methods really come into their own as a powerful and elegant approach to designing programs. 52 This formula was discovered by the seventeenth-century English mathematician John Wallis. 115 (accumulate combiner null-value term a next b) Accumulate takes as arguments the same term and range spec- ifications as sum and product, together with a combiner procedure (of two arguments) that specifies how the current term is to be combined with the accumulation of the preceding terms and a null-value that specifies what base value to use when the terms run out. Write accumulate and show how sum and product can both be defined as simple calls to accumulate. b. If your accumulate procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process. Exercise 1.33: You can obtain an even more general version of accumulate (Exercise 1.32) by introducing the notion of a filter on the terms to be combined. That is, combine only those terms derived from values in the range that satisfy a specified condition. The resulting filtered-accumulate abstraction takes the same arguments as accumulate, together with an additional predicate of one argument that specifies the filter. Write filtered-accumulate as a procedure. Show how to express the following using filtered-accumulate: a. the sum of the squares of the prime numbers in the interval a to b (assuming that you have a prime? predicate already written) b. the product of all the positive integers less than n that are relatively prime to n (i.e., all positive integers i < n such that GCD(i , n ) = 1). 1.3.2 Constructing Procedures Using Lambda In using sum as in Section 1.3.1, it seems terribly awkward to have to define trivial procedures such as pi-term and pi-next just so we can use them as arguments to our higher-order procedure. Rather than define pi-next and pi-term, it would be more convenient to have a way to directly specify “the procedure that returns its input incremented by 4” and “the procedure that returns the reciprocal of its input times its input plus 2.” We can do this by introducing the special form lambda, which creates procedures. Using lambda we can describe what we want as (lambda (x) (+ x 4)) and (lambda (x) (/ 1.0 (* x (+ x 2)))) Then our pi-sum procedure can be expressed without defining any auxiliary procedures as (define (pi-sum a b) (sum (lambda (x) (/ 1.0 (* x (+ x 2)))) a (lambda (x) (+ x 4)) b)) Again using lambda, we can write the integral procedure without having to define the auxiliary procedure add-dx: (define (integral f a b dx) (* (sum f (+ a (/ dx 2.0)) (lambda (x) (+ x dx)) b) dx)) 117 In general, lambda is used to create procedures in the same way as define, except that no name is specified for the procedure: (lambda (〈formal-parameters〉) 〈body〉) The resulting procedure is just as much a procedure as one that is created using define. The only difference is that it has not been associated with any name in the environment. In fact, (define (plus4 x) (+ x 4)) is equivalent to (define plus4 (lambda (x) (+ x 4))) We can read a lambda expression as follows: (lambda | (x) | the procedure of an argument x that adds x and 4 Like any expression that has a procedure as its value, a lambda expression can be used as the operator in a combination such as ((lambda (x y z) (+ x y (square z))) 1 2 3) 12 or, more generally, in any context where we would normally use a procedure name.53 53 It would be clearer and less intimidating to people learning Lisp if a name more obvious than lambda, such as make-procedure, were used. But the convention is firmly entrenched. The notation is adopted from the λ-calculus, a mathematical formalism introduced by the mathematical logician Alonzo Church (1941). Church developed the λ-calculus to provide a rigorous foundation for studying the notions of function and function application. The λ-calculus has become a basic tool for mathematical investigations of the semantics of programming languages. Using let to create local variables Another use of lambda is in creating local variables. We often need local variables in our procedures other than those that have been bound as formal parameters. For example, suppose we wish to compute the function f (x, y ) = x (1 + x y )2 + y (1 − y ) + (1 + x y )(1 − y ) which we could also express as a = 1 + xy b =1−y f (x, y ) = xa 2 + yb + ab In writing a procedure to compute f , we would like to include as local variables not only x and y but also the names of intermediate quantities like a and b . One way to accomplish this is to use an auxiliary procedure to bind the local variables: (define (f x y) (define (f-helper a b) (+ (* x (square a)) (* y b) (* a b))) (f-helper (+ 1 (* x y)) (- 1 y))) Of course, we could use a lambda expression to specify an anonymous procedure for binding our local variables. The body of f then becomes a single call to that procedure: (define (f x y) ((lambda (a 119 (+ (* x (square a)) (* y b) (* a b))) (+ 1 (* x y)) (- 1 y))) This construct is so useful that there is a special form called let to make its use more convenient. Using let, the f procedure could be written as (define (f x y) (let ((a (+ 1 (* x y))) (b (- 1 y))) (+ (* x (square a)) (* y b) (* a b)))) The general form of a let expression is (let ((〈v ar 1 〉〈exp 1 〉) (〈v ar 2 〉〈exp 2 〉) ... (〈v ar n 〉〈exp n 〉)) 〈body〉) which can be thought of as saying let 〈v ar 1 〉 have the value 〈exp 1 〉 and 〈v ar 2 〉 have the value 〈exp 2 〉 and ... in 〈v ar n 〉 have the value 〈exp n 〉〈body〉 120 The first part of the let expression is a list of name-expression pairs. When the let is evaluated, each name is associated with the value of the corresponding expression. The body of the let is evaluated with these names bound as local variables. The way this happens is that the let expression is interpreted as an alternate syntax for ((lambda (〈v ar 1 〉 ... 〈v ar n 〉) 〈body〉) 〈exp 1 〉 ... 〈exp n 〉) No new mechanism is required in the interpreter in order to provide local variables. A let expression is simply syntactic sugar for the underlying lambda application. We can see from this equivalence that the scope of a variable specified by a let expression is the body of the let. This implies that: • Let allows one to bind variables as locally as possible to where they are to be used. For example, if the value of x is 5, the value of the expression (+ (let ((x 3)) (+ x (* x 10))) x) is 38. Here, the x in the body of the let is 3, so the value of the let expression is 33. On the other hand, the x that is the second argument to the outermost + is still 5. • The variables’ values are computed outside the let. This matters when the expressions that provide the values for the local variables 121 depend upon variables having the same names as the local variables themselves. For example, if the value of x is 2, the expression (let ((x 3) (y (+ x 2))) (* x y)) will have the value 12 because, inside the body of the let, x will be 3 and y will be 4 (which is the outer x plus 2). Sometimes we can use internal definitions to get the same effect as with let. For example, we could have defined the procedure f above as (define (f x y) (define a (+ 1 (* x y))) (define b (- 1 y)) (+ (* x (square a)) (* y b) (* a b))) We prefer, however, to use let in situations like this and to use internal define only for internal procedures.54 Exercise 1.34: Suppose we define the procedure (define (f g) (g 2)) Then we have 54 Understanding internal definitions well enough to be sure a program means what we intend it to mean requires a more elaborate model of the evaluation process than we have presented in this chapter. The subtleties do not arise with internal definitions of procedures, however. We will return to this issue in Section 4.1.6, after we learn more about evaluation. 122 (f square) 4 (f (lambda (z) (* z (+ z 1)))) 6 What happens if we (perversely) ask the interpreter to evaluate the combination (f f)? Explain. 1.3.3 Procedures as General Methods We introduced compound procedures in Section 1.1.4 as a mechanism for abstracting patterns of numerical operations so as to make them independent of the particular numbers involved. With higher-order procedures, such as the integral procedure of Section 1.3.1, we began to see a more powerful kind of abstraction: procedures used to express general methods of computation, independent of the particular functions involved. In this section we discuss two more elaborate examples—general methods for finding zeros and fixed points of functions—and show how these methods can be expressed directly as procedures. Finding roots of equations by the half-interval method The half-interval method is a simple but powerful technique for finding roots of an equation f (x ) = 0, where f is a continuous function. The idea is that, if we are given points a and b such that f (a ) < 0 < f (b ), then f must have at least one zero between a and b . To locate a zero, let x be the average of a and b and compute f (x ). If f (x ) > 0, then f must have a zero between a and x . If f (x ) < 0, then f must have a zero between x and b . Continuing in this way, we can identify smaller and smaller intervals on which f must have a zero. When we reach a point where the interval is small enough, 123 the process stops. Since the interval of uncertainty is reduced by half at each step of the process, the number of steps required grows as Θ(log(L /T )), where L is the length of the original interval and T is the error tolerance (that is, the size of the interval we will consider “small enough”). Here is a procedure that implements this strategy: (define (search f neg-point pos-point) (let ((midpoint (average neg-point pos-point))) (if (close-enough? neg-point pos-point) midpoint (let ((test-value (f midpoint))) (cond ((positive? test-value) (search f neg-point midpoint)) ((negative? test-value) (search f midpoint pos-point)) (else midpoint)))))) We assume that we are initially given the function f together with points at which its values are negative and positive. We first compute the midpoint of the two given points. Next we check to see if the given interval is small enough, and if so we simply return the midpoint as our answer. Otherwise, we compute as a test value the value of f at the midpoint. If the test value is positive, then we continue the process with a new interval running from the original negative point to the midpoint. If the test value is negative, we continue with the interval from the midpoint to the positive point. Finally, there is the possibility that the test value is 0, in which case the midpoint is itself the root we are searching for. 124 To test whether the endpoints are “close enough” we can use a procedure similar to the one used in Section 1.1.7 for computing square roots:55 (define (close-enough? x y) (< (abs (- x y)) 0.001)) Search is awkward to use directly, because we can accidentally give it points at which f ’s values do not have the required sign, in which case we get a wrong answer. Instead we will use search via the following procedure, which checks to see which of the endpoints has a negative function value and which has a positive value, and calls the search procedure accordingly. If the function has the same sign on the two given points, the half-interval method cannot be used, in which case the procedure signals an error.56 (define (half-interval-method f a b) (let ((a-value (f a)) (b-value (f b))) (cond ((and (negative? a-value) (positive? b-value)) (search f a b)) ((and (negative? b-value) (positive? a-value)) (search f b a)) (else (error "Values are not of opposite sign" a b))))) 55 We have used 0.001 as a representative “small” number to indicate a tolerance the acceptable error in a calculation. The appropriate tolerance for a real calculation depends upon the problem to be solved and the limitations of the computer and the algorithm. This is often a very subtle consideration, requiring help from a numerical analyst or some other kind of magician. 56 This can be accomplished using error, which takes as arguments a number of items that are printed as error messages. 125 The following example uses the half-interval method to approximate π as the root between 2 and 4 of sin x = 0: (half-interval-method sin 2.0 4.0) 3.14111328125 Here is another example, using the half-interval method to search for a root of the equation x 3 − 2x − 3 = 0 between 1 and 2: (half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3)) 1.0 2.0) Finding fixed points of functions A number x is called a fixed point of a function f if x satisfies the equation f (x ) = x . For some functions f we can locate a fixed point by beginning with an initial guess and applying f repeatedly, f (x ), f ( f (x )), f ( f ( f (x ))), until the value does not change very much. Using this idea, we can devise a procedure fixed-point that takes as inputs a function and an initial guess and produces an approximation to a fixed point of the function. We apply the function repeatedly until we find two successive values whose difference is less than some prescribed tolerance: (define tolerance 0.00001) (define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) 126 (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess)) For example, we can use this method to approximate the fixed point of the cosine function, starting with 1 as an initial approximation:57 (fixed-point cos 1.0) .7390822985224023 Similarly, we can find a solution to the equation y = sin y + cos y : (fixed-point (lambda (y) (+ (sin y) (cos y))) 1.0) 1.2587315962971173 The fixed-point process is reminiscent of the process we used for finding square roots in Section 1.1.7. Both are based on the idea of repeatedly improving a guess until the result satisfies some criterion. In fact, we can readily formulate the square-root computation as a fixed-point search. Computing the square root of some number x requires finding a y such that y 2 = x . Putting this equation into the equivalent form y = x /y , we recognize that we are looking for a fixed point of the function58 y 7→ x /y , and we can therefore try to compute square roots as (define (sqrt x) (fixed-point (lambda (y) (/ x y)) 1.0)) 57 Try this during a boring lecture: Set your calculator to radians mode and then re- peatedly press the cos button until you obtain the fixed point. 58 7→ (pronounced “maps to”) is the mathematician’s way of writing lambda. y 7→ x /y means (lambda (y) (/ x y)), that is, the function whose value at y is x /y . 127 Unfortunately, this fixed-point search does not converge. Consider an initial guess y 1 . The next guess is y 2 = x /y 1 and the next guess is y 3 = x /y 2 = x /(x /y 1 ) = y 1 . This results in an infinite loop in which the two guesses y 1 and y 2 repeat over and over, oscillating about the answer. One way to control such oscillations is to prevent the guesses from changing so much. Since the answer is always between our guess y and x /y , we can make a new guess that is not as far from y as x /y by averaging y with x /y , so that the next guess after y is 12 ( y + x /y ) instead of x /y . The process of making such a sequence of guesses is simply the process of looking for a fixed point of y 7→ 21 ( y + x /y ): (define (sqrt x) (fixed-point (lambda (y) (average y (/ x y))) 1.0)) (Note that y = 21 ( y + x /y ) is a simple transformation of the equation y = x /y ; to derive it, add y to both sides of the equation and divide by 2.) With this modification, the square-root procedure works. In fact, if we unravel the definitions, we can see that the sequence of approximations to the square root generated here is precisely the same as the one generated by our original square-root procedure of Section 1.1.7. This approach of averaging successive approximations to a solution, a technique that we call average damping, often aids the convergence of fixed-point searches. Exercise 1.35: Show that the golden ratio ϕ (Section 1.2.2) is a fixed point of the transformation x 7→ 1 + 1/x , and use this fact to compute ϕ by means of the fixed-point procedure. Exercise 1.36: Modify fixed-point so that it prints the sequence of approximations it generates, using the newline and display primitives shown in Exercise 1.22. Then find a solution to x x = 1000 by finding a fixed point of x → 7 log(1000)/log(x ). 128 (Use Scheme’s primitive log procedure, which computes natural logarithms.) Compare the number of steps this takes with and without average damping. (Note that you cannot start fixedpoint with a guess of 1, as this would cause division by log(1) = 0.) Exercise 1.37: a. An infinite continued fraction is an expression of the form f = N1 D1 + N2 D 2+ D 3 +... As an example, one can show that the infinite continued fraction expansion with the Ni and the D i all equal to 1 produces 1/ϕ, where ϕ is the golden ratio (described in Section 1.2.2). One way to approximate an infinite continued fraction is to truncate the expansion after a given number of terms. Such a truncation—a so-called k-term finite continued fraction—has the form N1 D 1 + . N2 N . .+ k D Suppose that n and d are procedures of one argument (the term index i ) that return the Ni and D i of the terms of the continued fraction. Define a procedure cont-frac such that evaluating (cont-frac n d k) computes the value of the k -term finite continued fraction. Check your procedure by approximating 1/ϕ using (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) k) 129 for successive values of k. How large must you make k in order to get an approximation that is accurate to 4 decimal places? b. If your cont-frac procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process. Exercise 1.38: In 1737, the Swiss mathematician Leonhard Euler published a memoir De Fractionibus Continuis, which included a continued fraction expansion for e − 2, where e is the base of the natural logarithms. In this fraction, the Ni are all 1, and the D i are successively 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, . . . . Write a program that uses your cont-frac procedure from Exercise 1.37 to approximate e , based on Euler’s expansion. Exercise 1.39: A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert: x tan x = 2 1 − x x2 3− 5−... where x is in radians. Define a procedure (tan-cf x k) that computes an approximation to the tangent function based on Lambert’s formula. k specifies the number of terms to compute, as in Exercise 1.3.4 Procedures as Returned Values The above examples demonstrate how the ability to pass procedures as arguments significantly enhances the expressive power of our programming language. We can achieve even more expressive power by creating procedures whose returned values are themselves procedures. 130 We can illustrate this idea by looking again at the fixed-point example described at the end of Section 1.3.3. We formulated a new version of the square-root procedure as a fixed-point search, starting with the observap tion that x is a fixed-point of the function y 7→ x /y . Then we used average damping to make the approximations converge. Average damping is a useful general technique in itself. Namely, given a function f , we consider the function whose value at x is equal to the average of x and f (x ). We can express the idea of average damping by means of the following procedure: (define (average-damp f) (lambda (x) (average x (f x)))) Average-damp is a procedure that takes as its argument a procedure f and returns as its value a procedure (produced by the lambda) that, when applied to a number x, produces the average of x and (f x). For example, applying average-damp to the square procedure produces a procedure whose value at some number x is the average of x and x 2 . Applying this resulting procedure to 10 returns the average of 10 and 100, or 55:59 ((average-damp square) 10) 55 Using average-damp, we can reformulate the square-root procedure as follows: (define (sqrt x) 59 Observe that this is a combination whose operator is itself a combination. Exercise 1.4 already demonstrated the ability to form such combinations, but that was only a toy example. Here we begin to see the real need for such combinations—when applying a procedure that is obtained as the value returned by a higher-order procedure. 131 (fixed-point (average-damp (lambda (y) (/ x y))) 1.0)) Notice how this formulation makes explicit the three ideas in the method: fixed-point search, average damping, and the function y 7→ x /y . It is instructive to compare this formulation of the square-root method with the original version given in Section 1.1.7. Bear in mind that these procedures express the same process, and notice how much clearer the idea becomes when we express the process in terms of these abstractions. In general, there are many ways to formulate a process as a procedure. Experienced programmers know how to choose procedural formulations that are particularly perspicuous, and where useful elements of the process are exposed as separate entities that can be reused in other applications. As a simple example of reuse, notice that the cube root of x is a fixed point of the function y 7→ x /y 2 , so we can immediately generalize our square-root procedure to one that extracts cube roots:60 (define (cube-root x) (fixed-point (average-damp (lambda (y) (/ x (square y)))) 1.0)) Newton’s method When we first introduced the square-root procedure, in Section 1.1.7, we mentioned that this was a special case of Newton’s method. If x 7→ g (x ) is a differentiable function, then a solution of the equation g (x ) = 0 is a fixed point of the function x 7→ f (x ) where f (x ) = x − g (x ) D g (x ) 60 See Exercise 1.45 for a further generalization. 132 and D g (x ) is the derivative of g evaluated at x . Newton’s method is the use of the fixed-point method we saw above to approximate a solution of the equation by finding a fixed point of the function f.61 For many functions g and for sufficiently good initial guesses for x , Newton’s method converges very rapidly to a solution of g (x ) = 0.62 In order to implement Newton’s method as a procedure, we must first express the idea of derivative. Note that “derivative,” like average damping, is something that transforms a function into another function. For instance, the derivative of the function x 7→ x 3 is the function x 7→ 3x 2. In general, if g is a function and dx is a small number, then the derivative D g of g is the function whose value at any number x is given (in the limit of small dx ) by D g (x ) = g (x + dx) − g (x ) dx Thus, we can express the idea of derivative (taking dx to be, say, 0.00001) as the procedure (define (deriv g) (lambda (x) (/ (- (g (+ x dx)) (g x)) dx))) 61 Elementary calculus books usually describe Newton’s method in terms of the se- quence of approximations x n +1 = x n − g (x n )/D g (x n ). Having language for talking about processes and using the idea of fixed points simplifies the description of the method. 62 Newton’s method does not always converge to an answer, but it can be shown that in favorable cases each iteration doubles the number-of-digits accuracy of the approximation to the solution. In such cases, Newton’s method will converge much more rapidly than the half-interval 133 along with the definition (define dx 0.00001) Like average-damp, deriv is a procedure that takes a procedure as argument and returns a procedure as value. For example, to approximate the derivative of x 7→ x 3 at 5 (whose exact value is 75) we can evaluate (define (cube x) (* x x x)) ((deriv cube) 5) 75.00014999664018 With the aid of deriv, we can express Newton’s method as a fixed-point process: (define (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x))))) (define (newtons-method g guess) (fixed-point (newton-transform g) guess)) The newton-transform procedure expresses the formula at the beginning of this section, and newtons-method is readily defined in terms of this. It takes as arguments a procedure that computes the function for which we want to find a zero, together with an initial guess. For instance, to find the square root of x , we can use Newton’s method to find a zero of the function y 7→ y 2 − x starting with an initial guess of 1.63 This provides yet another form of the square-root procedure: 63 For finding square roots, Newton’s method converges rapidly to the correct solution from any starting point. 134 (define (sqrt x) (newtons-method (lambda (y) (- (square y) x)) 1.0)) Abstractions and first-class procedures We’ve seen two ways to express the square-root computation as an instance of a more general method, once as a fixed-point search and once using Newton’s method. Since Newton’s method was itself expressed as a fixedpoint process, we actually saw two ways to compute square roots as fixed points. Each method begins with a function and finds a fixed point of some transformation of the function. We can express this general idea itself as a procedure: (define (fixed-point-of-transform g transform guess) (fixed-point (transform g) guess)) This very general procedure takes as its arguments a procedure g that computes some function, a procedure that transforms g, and an initial guess. The returned result is a fixed point of the transformed function. Using this abstraction, we can recast the first square-root computation from this section (where we look for a fixed point of the average-damped version of y 7→ x /y ) as an instance of this general method: (define (sqrt x) (fixed-point-of-transform (lambda (y) (/ x y)) average-damp 1.0)) Similarly, we can express the second square-root computation from this section (an instance of Newton’s method that finds a fixed point of the Newton transform of y 7→ y 2 − x ) as (define (sqrt x) 135 (fixed-point-of-transform (lambda (y) (- (square y) x)) newton-transform 1.0)) We began Section 1.3 with the observation that compound procedures are a crucial abstraction mechanism, because they permit us to express general methods of computing as explicit elements in our programming language. Now we’ve seen how higher-order procedures permit us to manipulate these general methods to create further abstractions. As programmers, we should be alert to opportunities to identify the underlying abstractions in our programs and to build upon them and generalize them to create more powerful abstractions. This is not to say that one should always write programs in the most abstract way possible; expert programmers know how to choose the level of abstraction appropriate to their task. But it is important to be able to think in terms of these abstractions, so that we can be ready to apply them in new contexts. The significance of higher-order procedures is that they enable us to represent these abstractions explicitly as elements in our programming language, so that they can be handled just like other computational elements. In general, programming languages impose restrictions on the ways in which computational elements can be manipulated. Elements with the fewest restrictions are said to have first-class status. Some of the “rights and privileges” of first-class elements are:64 • They may be named by variables. • They may be passed as arguments to procedures. 64 The notion of first-class status of programming-language elements is due to the British computer scientist Christopher Strachey (1916-1975). • They may be returned as the results of procedures. • They may be included in data structures.65 Lisp, unlike other common programming languages, awards procedures full first-class status. This poses challenges for efficient implementation, but the resulting gain in expressive power is enormous.66 Exercise 1.40: Define a procedure cubic that can be used together with the newtons-method procedure in expressions of the form (newtons-method (cubic a b c) 1) to approximate zeros of the cubic x 3 + ax 2 + bx + c . Exercise 1.41: Define a procedure double that takes a procedure of one argument as argument and returns a procedure that applies the original procedure twice. For example, if inc is a procedure that adds 1 to its argument, then (double inc) should be a procedure that adds 2. What value is returned by (((double (double double)) inc) 5) Exercise 1.42: Let f and g be two one-argument functions. The composition f after g is defined to be the function x 7→ f (g (x )). Define a procedure compose that implements composition. For example, if inc is a procedure that adds 1 to its argument, ((compose square inc) 6) 49 65 We’ll see examples of this after we introduce data structures in Chapter 2. 66 The major implementation cost of first-class procedures is that allowing proce- dures to be returned as values requires reserving storage for a procedure’s free variables even while the procedure is not executing. In the Scheme implementation we will study in Section 4.1, these variables are stored in the procedure’s environment. 137 Exercise 1.43: If f is a numerical function and n is a positive integer, then we can form the n th repeated application of f , which is defined to be the function whose value at x is f ( f (. . . ( f (x )) . . .)). For example, if f is the function x 7→ x + 1, then the n th repeated application of f is the function x 7→ x + n . If f is the operation of squaring a number, then the n th repeated application of f is the function that raises its argument to the 2n -th power. Write a procedure that takes as inputs a procedure that computes f and a positive integer n and returns the procedure that computes the n th repeated application of f . Your procedure should be able to be used as follows: ((repeated square 2) 5) 625 Hint: You may find it convenient to use compose from Exercise 1.42. Exercise 1.44: The idea of smoothing a function is an important concept in signal processing. If f is a function and dx is some small number, then the smoothed version of f is the function whose value at a point x is the average of f (x − dx ), f (x ), and f (x + dx ). Write a procedure smooth that takes as input a procedure that computes f and returns a procedure that computes the smoothed f . It is sometimes valuable to repeatedly smooth a function (that is, smooth the smoothed function, and so on) to obtain the n-fold smoothed function. Show how to generate the n-fold smoothed function of any given function using smooth and repeated from Exercise 1.43. Exercise 1.45: We saw in Section 1.3.3 that attempting to compute square roots by naively finding a fixed point of y 7→ x /y does not 138 converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the averagedamped y 7→ x /y 2 . Unfortunately, the process does not work for fourth roots—a single average damp is not enough to make a fixedpoint search for y 7→ x /y 3 converge. On the other hand, if we average damp twice (i.e., use the average damp of the average damp of y 7→ x /y 3 ) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute n th roots as a fixed-point search based upon repeated average damping of y 7→ x /y n−1 . Use this to implement a simple procedure for computing n th roots using fixed-point, average-damp, and the repeated procedure of Exercise 1.43. Assume that any arithmetic operations you need are available as primitives. Exercise 1.46: Several of the numerical methods described in this chapter are instances of an extremely general computational strategy known as iterative improvement. Iterative improvement says that, to compute something, we start with an initial guess for the answer, test if the guess is good enough, and otherwise improve the guess and continue the process using the improved guess as the new guess. Write a procedure iterative-improve that takes two procedures as arguments: a method for telling whether a guess is good enough and a method for improving a guess. Iterative-improve should return as its value a procedure that takes a guess as argument and keeps improving the guess until it is good enough. Rewrite the sqrt procedure of Section 1.1.7 and the fixed-point procedure of Section 1.3.3 in terms of iterative-improve. 2 Building Abstractions with Data We now come to the decisive step of mathematical abstraction: we forget about what the symbols stand for. . . . [The mathematician] need not be idle; there are many operations which he may carry out with these symbols, without ever having to look at the things they stand for. —Hermann Weyl, The Mathematical Way of Thinking We concentrated in Chapter 1 on computational processes and on the role of procedures in program design. We saw how to use primitive data (numbers) and primitive operations (arithmetic operations), how to combine procedures to form compound procedures through composition, conditionals, and the use of parameters, and how to abstract procedures by using define. We saw that a procedure can be regarded as a pattern for the local evolution of a process, and we classified, reasoned about, and performed simple algorithmic analyses of some common patterns for processes as embodied in procedures. We also saw that higher-order procedures enhance the power of our language by enabling us to manipulate, and thereby to reason in terms of, general methods of computation. This is much of the essence of programming. In this chapter we are going to look at more complex data. All the procedures in chapter 1 operate on simple numerical data, and simple data are not sufficient for many of the problems we wish to address using computation. Programs are typically designed to model complex phenomena, and more often than not one must construct computational objects that have several parts in order to model real-world phenomena that have several aspects. Thus, whereas our focus in chapter 1 was on building abstractions 140 by combining procedures to form compound procedures, we turn in this chapter to another key aspect of any programming language: the means it provides for building abstractions by combining data objects to form compound data. Why do we want compound data in a programming language? For the same reasons that we want compound procedures: to elevate the conceptual level at which we can design our programs, to increase the modularity of our designs, and to enhance the expressive power of our language. Just as the ability to define procedures enables us to deal with processes at a higher conceptual level than that of the primitive operations of the language, the ability to construct compound data objects enables us to deal with data at a higher conceptual level than that of the primitive data objects of the language. Consider the task of designing a system to perform arithmetic with rational numbers. We could imagine an operation add-rat that takes two rational numbers and produces their sum. In terms of simple data, a rational number can be thought of as two integers: a numerator and a denominator. Thus, we could design a program in which each rational number would be represented by two integers (a numerator and a denominator) and where add-rat would be implemented by two procedures (one producing the nu- merator of the sum and one producing the denominator). But this would be awkward, because we would then need to explicitly keep track of which numerators corresponded to which denominators. In a system intended to perform many operations on many rational numbers, such bookkeeping details would clutter the programs substantially, to say nothing of what they would do to our minds. It would be much better if we could “glue together” a numerator and denominator to form a pair—a compound data 141 object—that our programs could manipulate in a way that would be consistent with regarding a rational number as a single conceptual unit. The use of compound data also enables us to increase the modularity of our programs. If we can manipulate rational numbers directly as objects in their own right, then we can separate the part of our program that deals with rational numbers per se from the details of how rational numbers may be represented as pairs of integers. The general technique of isolating the parts of a program that deal with how data objects are represented from the parts of a program that deal with how data objects are used is a powerful design methodology called data abstraction. We will see how data abstraction makes programs much easier to design, maintain, and modify. The use of compound data leads to a real increase in the expressive power of our programming language. Consider the idea of forming a “linear combination” ax + b y . We might like to write a procedure that would accept a , b , x , and y as arguments and return the value of ax + b y . This presents no difficulty if the arguments are to be numbers, because we can readily define the procedure (define (linear-combination a b x y) (+ (* a x) (* b y))) But suppose we are not concerned only with numbers. Suppose we would like to express, in procedural terms, the idea that one can form linear combinations whenever addition and multiplication are defined—for rational numbers, complex numbers, polynomials, or whatever. We could express this as a procedure of the form (define (linear-combination a b x y) (add (mul a x) (mul b y))) 142 where add and mul are not the primitive procedures + and * but rather more complex things that will perform the appropriate operations for whatever kinds of data we pass in as the arguments a, b, x, and y. The key point is that the only thing linear-combination should need to know about a, b, x, and y is that the procedures add and mul will perform the appropriate manipulations. From the perspective of the procedure linear-combination, it is irrelevant what a, b, x, and y are and even more irrelevant how they might happen to be represented in terms of more primitive data. This same example shows why it is important that our programming language provide the ability to manipulate compound objects directly: Without this, there is no way for a procedure such as linear-combination to pass its arguments along to add and mul without having to know their detailed structure.1 We begin this chapter by implementing the rational-number arithmetic system mentioned above. This will form the background for our discussion of compound data and data abstraction. As with compound procedures, the main issue to be addressed is that of abstraction as a technique for coping with complexity, and we will see how data abstraction enables us to erect suitable abstraction barriers between different parts of a program. 1 The ability to directly manipulate procedures provides an analogous increase in the expressive power of a programming language. For example, in Section 1.3.1 we introduced the sum procedure, which takes a procedure term as an argument and computes the sum of the values of term over some specified interval. In order to define sum, it is crucial that we be able to speak of a procedure such as term as an entity in its own right, without regard for how term might be expressed with more primitive operations. Indeed, if we did not have the notion of “a procedure,” it is doubtful that we would ever even think of the possibility of defining an operation such as sum. Moreover, insofar as performing the summation is concerned, the details of how term may be constructed from more primitive operations are irrelevant. 143 We will see that the key to forming compound data is that a programming language should provide some kind of “glue” so that data objects can be combined to form more complex data objects. There are many possible kinds of glue. Indeed, we will discover how to form compound data using no special “data” operations at all, only procedures. This will further blur the distinction between “procedure” and “data,” which was already becoming tenuous toward the end of chapter 1. We will also explore some conventional techniques for representing sequences and trees. One key idea in dealing with compound data is the notion of closure—that the glue we use for combining data objects should allow us to combine not only primitive data objects, but compound data objects as well. Another key idea is that compound data objects can serve as conventional interfaces for combining program modules in mix-and-match ways. We illustrate some of these ideas by presenting a simple graphics language that exploits closure. We will then augment the representational power of our language by introducing symbolic expressions—data whose elementary parts can be arbitrary symbols rather than only numbers. We explore various alternatives for representing sets of objects. We will find that, just as a given numerical function can be computed by many different computational processes, there are many ways in which a given data structure can be represented in terms of simpler objects, and the choice of representation can have significant impact on the time and space requirements of processes that manipulate the data. We will investigate these ideas in the context of symbolic differentiation, the representation of sets, and the encoding of information. Next we will take up the problem of working with data that may be represented differently by different parts of a program. This leads to the need 144 to implement generic operations, which must handle many different types of data. Maintaining modularity in the presence of generic operations requires more powerful abstraction barriers than can be erected with simple data abstraction alone. In particular, we introduce data-directed programming as a technique that allows individual data representations to be designed in isolation and then combined additively (i.e., without modification). To illustrate the power of this approach to system design, we close the chapter by applying what we have learned to the implementation of a package for performing symbolic arithmetic on polynomials, in which the coefficients of the polynomials can be integers, rational numbers, complex numbers, and even other polynomials. 2.1 Introduction to Data Abstraction In Section 1.1.8, we noted that a procedure used as an element in creating a more complex procedure could be regarded not only as a collection of particular operations but also as a procedural abstraction. That is, the details of how the procedure was implemented could be suppressed, and the particular procedure itself could be replaced by any other procedure with the same overall behavior. In other words, we could make an abstraction that would separate the way the procedure would be used from the details of how the procedure would be implemented in terms of more primitive procedures. The analogous notion for compound data is called data abstraction. Data abstraction is a methodology that enables us to isolate how a compound data object is used from the details of how it is constructed from more primitive data objects. The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on “abstract data.” That 145 is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a “concrete” data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation. To illustrate this technique, we will consider how to design a set of procedures for manipulating rational numbers. 2.1.1 Example: Arithmetic Operations for Rational Numbers Suppose we want to do arithmetic with rational numbers. We want to be able to add, subtract, multiply, and divide them and to test whether two rational numbers are equal. Let us begin by assuming that we already have a way of constructing a rational number from a numerator and a denominator. We also assume that, given a rational number, we have a way of extracting (or selecting) its numerator and its denominator. Let us further assume that the constructor and selectors are available as procedures: • (make-rat 〈n 〉〈d 〉) returns the rational number whose numerator is the integer 〈n 〉 and whose denominator is the integer 〈d 〉. • (numer 〈x 〉) returns the numerator of the rational number 〈x 〉. • (denom 〈x 〉) returns the denominator of the rational number 〈x 〉. We are using here a powerful strategy of synthesis: wishful thinking. We haven’t yet said how a rational number is represented, or how the procedures numer, denom, and make-rat should be implemented. Even so, if we did have these three procedures, we could then add, subtract, multiply, di- 146 vide, and test equality by using the following relations: n1 n2 + d1 d2 n1 n2 − d1 d2 n1 n2 × d1 d2 n 1 /d 1 n 2 /d 2 n1 d1 = = = = = n1d2 + n2d1 d1d2 n1d2 − n2d1 d1d2 n1n2 d1d2 n1d2 d1n2 n2 if and only if n 1 d 2 = n 2 d 1 d2 We can express these rules as procedures: (define (add-rat x y) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (sub-rat x y) (make-rat (- (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (mul-rat x y) (make-rat (* (numer x) (numer y)) (* (denom x) (denom y)))) (define (div-rat x y) (make-rat (* (numer x) (denom y)) 147 (* (denom x) (numer y)))) (define (equal-rat? x y) (= (* (numer x) (denom y)) (* (numer y) (denom x)))) Now we have the operations on rational numbers defined in terms of the selector and constructor procedures numer, denom, and make-rat. But we haven’t yet defined these. What we need is some way to glue together a numerator and a denominator to form a rational number. Pairs To enable us to implement the concrete level of our data abstraction, our language provides a compound structure called a pair, which can be constructed with the primitive procedure cons. This procedure takes two arguments and returns a compound data object that contains the two arguments as parts. Given a pair, we can extract the parts using the primitive procedures car and cdr.2 Thus, we can use cons, car, and cdr as follows: (define x (cons 1 2)) (car x) 1 (cdr x) 2 2 The name cons stands for “construct.” The names car and cdr derive from the original implementation of Lisp on the IBM 704. That machine had an addressing scheme that allowed one to reference the “address” and “decrement” parts of a memory location. Car stands for “Contents of Address part of Register” and cdr (pronounced “could-er”) stands for “Contents of Decrement part of Register.” 148 Notice that a pair is a data object that can be given a name and manipulated, just like a primitive data object. Moreover, cons can be used to form pairs whose elements are pairs, and so on: (define x (cons 1 2)) (define y (cons 3 4)) (define z (cons x y)) (car (car z)) 1 (car (cdr z)) 3 In Section 2.2 we will see how this ability to combine pairs means that pairs can be used as general-purpose building blocks to create all sorts of complex data structures. The single compound-data primitive pair, implemented by the procedures cons, car, and cdr, is the only glue we need. Data objects constructed from pairs are called list-structured data. Representing rational numbers Pairs offer a natural way to complete the rational-number system. Simply represent a rational number as a pair of two integers: a numerator and a denominator. Then make-rat, numer, and denom are readily implemented as follows:3 3 Another way to define the selectors and constructor is (define make-rat cons) 149 (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) Also, in order to display the results of our computations, we can print rational numbers by printing the numerator, a slash, and the denominator:4 (define (print-rat x) (newline) (display (numer x)) (display "/") (display (denom x))) Now we can try our rational-number procedures: (define one-half (make-rat 1 2)) (define numer car) (define denom cdr) The first definition associates the name make-rat with the value of the expression cons, which is the primitive procedure that constructs pairs. Thus make-rat and cons are names for the same primitive constructor. Defining selectors and constructors in this way is efficient: Instead of make-rat calling cons, make-rat is cons, so there is only one procedure called, not two, when make-rat is called. On the other hand, doing this defeats debugging aids that trace procedure calls or put breakpoints on procedure calls: You may want to watch make-rat being called, but you certainly don’t want to watch every call to cons. We have chosen not to use this style of definition in this book. 4 Display is the Scheme primitive for printing data. The Scheme primitive newline starts a new line for printing. Neither of these procedures returns a useful value, so in the uses of print-rat below, we show only what print-rat prints, not what the interpreter prints as the value returned by print-rat. (print-rat one-half) 1/2 (define one-third (make-rat 1 3)) (print-rat (add-rat one-half one-third)) 5/6 (print-rat (mul-rat one-half one-third)) 1/6 (print-rat (add-rat one-third one-third)) 6/9 As the final example shows, our rational-number implementation does not reduce rational numbers to lowest terms. We can remedy this by changing make-rat. If we have a gcd procedure like the one in Section 1.2.5 that produces the greatest common divisor of two integers, we can use gcd to reduce the numerator and the denominator to lowest terms before constructing the pair: (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) Now we have (print-rat (add-rat one-third one-third)) 2/3 151 as desired. This modification was accomplished by changing the constructor make-rat without changing any of the procedures (such as add-rat and mul-rat) that implement the actual operations. Exercise 2.1: Define a better version of make-rat that handles both positive and negative arguments. Make-rat should normalize the sign so that if the rational number is positive, both the numerator and denominator are positive, and if the rational number is negative, only the numerator is negative. 2.1.2 Abstraction Barriers Before continuing with more examples of compound data and data abstraction, let us consider some of the issues raised by the rational-number example. We defined the rational-number operations in terms of a constructor make-rat and selectors numer and denom. In general, the underlying idea of data abstraction is to identify for each type of data object a basic set of operations in terms of which all manipulations of data objects of that type will be expressed, and then to use only those operations in manipulating the data. We can envision the structure of the rational-number system as shown in Figure 2.1. The horizontal lines represent abstraction barriers that isolate different “levels” of the system. At each level, the barrier separates the programs (above) that use the data abstraction from the programs (below) that implement the data abstraction. Programs that use rational numbers manipulate them solely in terms of the procedures supplied “for public use” by the rational-number package: add-rat, sub-rat, mul-rat, div-rat, and equal-rat?. These, in turn, are implemented solely in terms of the con- structor and selectors make-rat, numer, and denom, which themselves are 152 implemented in terms of pairs. The details of how pairs are implemented are irrelevant to the rest of the rational-number package so long as pairs can be manipulated by the use of cons, car, and cdr. In effect, procedures at each level are the interfaces that define the abstraction barriers and connect the different levels. Fig 2.1: Data-abstraction barriers in the rational-number package. This simple idea has many advantages. One advantage is that it makes programs much easier to maintain and to modify. Any complex data structure can be represented in a variety of ways with the primitive data structures provided by a programming language. Of course, the choice of representation influences the programs that operate on it; thus, if the representation were to be changed at some later time, all such programs might have to be modified accordingly. This task could be time-consuming and 153 expensive in the case of large programs unless the dependence on the representation were to be confined by design to a very few program modules. For example, an alternate way to address the problem of reducing rational numbers to lowest terms is to perform the reduction whenever we access the parts of a rational number, rather than when we construct it. This leads to different constructor and selector procedures: (define (make-rat n d) (cons n d)) (define (numer x) (let ((g (gcd (car x) (cdr x)))) (/ (car x) g))) (define (denom x) (let ((g (gcd (car x) (cdr x)))) (/ (cdr x) g))) The difference between this implementation and the previous one lies in when we compute the gcd. If in our typical use of rational numbers we access the numerators and denominators of the same rational numbers many times, it would be preferable to compute the gcd when the rational numbers are constructed. If not, we may be better off waiting until access time to compute the gcd. In any case, when we change from one representation to the other, the procedures add-rat, sub-rat, and so on do not have to be modified at all. Constraining the dependence on the representation to a few interface procedures helps us design programs as well as modify them, because it allows us to maintain the flexibility to consider alternate implementations. 154 To continue with our simple example, suppose we are designing a rationalnumber package and we can’t decide initially whether to perform the gcd at construction time or at selection time. The data-abstraction methodology gives us a way to defer that decision without losing the ability to make progress on the rest of the system. Exercise 2.2: Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints). To try your procedures, you’ll need a way to print points: (define (print-point p) (newline) (display "(") (display (x-point p)) (display ",") (display (y-point p)) (display ")")) 155 Exercise 2.3: Implement a representation for rectangles in a plane. (Hint: You may want to make use of Exercise 2.2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so that the same perimeter and area procedures will work using either representation? 2.1.3 What Is Meant by Data? We began the rational-number implementation in Section 2.1.1 by implementing the rational-number operations add-rat, sub-rat, and so on in terms of three unspecified procedures: make-rat, numer, and denom. At that point, we could think of the operations as being defined in terms of data objects—numerators, denominators, and rational numbers—whose behavior was specified by the latter three procedures. But exactly what is meant by data? It is not enough to say “whatever is implemented by the given selectors and constructors.” Clearly, not every arbitrary set of three procedures can serve as an appropriate basis for the rational-number implementation. We need to guarantee that, if we construct a rational number x from a pair of integers n and d, then extracting the numer and the denom of x and dividing them should yield the same result as dividing n by d. In other words, make-rat, numer, and denom must satisfy the condition that, for any integer n and any non-zero integer d, if x is (make-rat n d), then (numer x) (denom x) n d In fact, this is the only condition make-rat, numer, and denom must fulfill in order to form a suitable basis for a rational-number representation. In 156 general, we can think of data as defined by some collection of selectors and constructors, together with specified conditions that these procedures must fulfill in order to be a valid representation.5 This point of view can serve to define not only “high-level” data objects, such as rational numbers, but lower-level objects as well. Consider the notion of a pair, which we used in order to define our rational numbers. We never actually said what a pair was, only that the language supplied procedures cons, car, and cdr for operating on pairs. But the only thing we need to know about these three operations is that if we glue two objects together using cons we can retrieve the objects using car and cdr. That is, the operations satisfy the condition that, for any objects x and y, if z is (cons x y) then (car z) is x and (cdr z) is y. Indeed, we mentioned that these three procedures are included as primitives in our language. However, any triple 5 Surprisingly, this idea is very difficult to formulate rigorously. There are two ap- proaches to giving such a formulation. One, pioneered by C. A. R. Hoare (1972), is known as the method of abstract models. It formalizes the “procedures plus conditions” specification as outlined in the rational-number example above. Note that the condition on the rational-number representation was stated in terms of facts about integers (equality and division). In general, abstract models define new kinds of data objects in terms of previously defined types of data objects. Assertions about data objects can therefore be checked by reducing them to assertions about previously defined data objects. Another approach, introduced by Zilles at MIT, by Goguen, Thatcher, Wagner, and Wright at IBM (see Thatcher et al. 1978), and by Guttag at Toronto (see Guttag 1977), is called algebraic specification. It regards the “procedures” as elements of an abstract algebraic system whose behavior is specified by axioms that correspond to our “conditions,” and uses the techniques of abstract algebra to check assertions about data objects. Both methods are surveyed in the paper by Liskov and Zilles (1975). 157 of procedures that satisfies the above condition can be used as the basis for implementing pairs. This point is illustrated strikingly by the fact that we could implement cons, car, and cdr without using any data structures at all but only using procedures. Here are the definitions: (define (cons x y) (define (dispatch m) (cond ((= m 0) x) ((= m 1) y) (else (error "Argument not 0 or 1 - CONS" m)))) dispatch) (define (car z) (z 0)) (define (cdr z) (z 1)) This use of procedures corresponds to nothing like our intuitive notion of what data should be. Nevertheless, all we need to do to show that this is a valid way to represent pairs is to verify that these procedures satisfy the condition given above. The subtle point to notice is that the value returned by (cons x y) is a procedure—namely the internally defined procedure dispatch, which takes one argument and returns either x or y depending on whether the argument is 0 or 1. Correspondingly, (car z) is defined to apply z to 0. Hence, if z is the procedure formed by (cons x y), then z applied to 0 will yield x. Thus, we have shown that (car (cons x y)) yields x, as desired. Similarly, (cdr (cons x y)) applies the procedure returned by (cons x y) to 1, which returns y. Therefore, this procedural implementation of pairs is a valid implementation, and if we access pairs using only cons, 158 car, and cdr we cannot distinguish this implementation from one that uses “real” data structures. The point of exhibiting the procedural representation of pairs is not that our language works this way (Scheme, and Lisp systems in general, implement pairs directly, for efficiency reasons) but that it could work this way. The procedural representation, although obscure, is a perfectly adequate way to represent pairs, since it fulfills the only conditions that pairs need to fulfill. This example also demonstrates that the ability to manipulate procedures as objects automatically provides the ability to represent compound data. This may seem a curiosity now, but procedural representations of data will play a central role in our programming repertoire. This style of programming is often called message passing, and we will be using it as a basic tool in Chapter 3 when we address the issues of modeling and simulation. Exercise 2.4: Here is an alternative procedural representation of pairs. For this representation, verify that (car (cons x y)) yields x for any objects x and y. (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) What is the corresponding definition of cdr? (Hint: To verify that this works, make use of the substitution model of Section 1.1.5.) Exercise 2.5: Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent 159 the pair a and b as the integer that is the product 2a 3b . Give the corresponding definitions of the procedures cons, car, and cdr. Exercise 2.6: In case representing pairs as procedures wasn’t mindboggling enough, consider that, in a language that can manipulate procedures, we can get by without numbers (at least insofar as nonnegative integers are concerned) by implementing 0 and the operation of adding 1 as (define zero (lambda (f) (lambda (x) x))) (define (add-1 n) (lambda (f) (lambda (x) (f ((n f) x))))) This representation is known as Church numerals, after its inventor, Alonzo Church, the logician who invented the λ-calculus. Define one and two directly (not in terms of zero and add-1). (Hint: Use substitution to evaluate (add-1 zero)). Give a direct definition of the addition procedure + (not in terms of repeated application of add-1). 2.1.4 Extended Exercise: Interval Arithmetic Alyssa P. Hacker is designing a system to help people solve engineering problems. One feature she wants to provide in her system is the ability to manipulate inexact quantities (such as measured parameters of physical devices) with known precision, so that when computations are done with such approximate quantities the results will be numbers of known precision. Electrical engineers will be using Alyssa’s system to compute electrical quantities. It is sometimes necessary for them to compute the value of a 160 parallel equivalent resistance R p of two resistors R 1 and R 2 using the formula 1 Rp = 1/R 1 + 1/R 2 Resistance values are usually known only up to some tolerance guaranteed by the manufacturer of the resistor. For example, if you buy a resistor labeled “6.8 ohms with 10% tolerance” you can only be sure that the resistor has a resistance between 6.8 − 0.68 = 6.12 and 6.8 + 0.68 = 7.48 ohms. Thus, if you have a 6.8-ohm 10% resistor in parallel with a 4.7-ohm 5% resistor, the resistance of the combination can range from about 2.58 ohms (if the two resistors are at the lower bounds) to about 2.97 ohms (if the two resistors are at the upper bounds). Alyssa’s idea is to implement “interval arithmetic” as a set of arithmetic operations for combining “intervals” (objects that represent the range of possible values of an inexact quantity). The result of adding, subtracting, multiplying, or dividing two intervals is itself an interval, representing the range of the result. Alyssa postulates the existence of an abstract object called an “interval” that has two endpoints: a lower bound and an upper bound. She also presumes that, given the endpoints of an interval, she can construct the interval using the data constructor make-interval. Alyssa first writes a procedure for adding two intervals. She reasons that the minimum value the sum could be is the sum of the two lower bounds and the maximum value it could be is the sum of the two upper bounds: (define (add-interval x y) (make-interval (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y)))) 161 Alyssa also works out the product of two intervals by finding the minimum and the maximum of the products of the bounds and using them as the bounds of the resulting interval. (Min and max are primitives that find the minimum or maximum of any number of arguments.) (define (mul-interval x y) (let ((p1 (* (lower-bound x) (lower-bound y))) (p2 (* (lower-bound x) (upper-bound y))) (p3 (* (upper-bound x) (lower-bound y))) (p4 (* (upper-bound x) (upper-bound y)))) (make-interval (min p1 p2 p3 p4) (max p1 p2 p3 p4)))) To divide two intervals, Alyssa multiplies the first by the reciprocal of the second. Note that the bounds of the reciprocal interval are the reciprocal of the upper bound and the reciprocal of the lower bound, in that order. (define (div-interval x y) (mul-interval x (make-interval (/ 1.0 (upper-bound y)) (/ 1.0 (lower-bound y))))) Exercise 2.7: Alyssa’s program is incomplete because she has not specified the implementation of the interval abstraction. Here is a definition of the interval constructor: (define (make-interval a b) (cons a b)) Define selectors upper-bound and lower-bound to complete the implementation. 162 Exercise 2.8: Using reasoning analogous to Alyssa’s, describe how the difference of two intervals may be computed. Define a corresponding subtraction procedure, called sub-interval. Exercise 2.9: The width of an interval is half of the difference between its upper and lower bounds. The width is a measure of the uncertainty of the number specified by the interval. For some arithmetic operations the width of the result of combining two intervals is a function only of the widths of the argument intervals, whereas for others the width of the combination is not a function of the widths of the argument intervals. Show that the width of the sum (or difference) of two intervals is a function only of the widths of the intervals being added (or subtracted). Give examples to show that this is not true for multiplication or division. Exercise 2.10: Ben Bitdiddle, an expert systems programmer, looks over Alyssa’s shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Modify Alyssa’s code to check for this condition and to signal an error if it occurs. Exercise 2.11: In passing, Ben also cryptically comments: “By testing the signs of the endpoints of the intervals, it is possible to break mul-interval into nine cases, only one of which requires more than two multiplications.” Rewrite this procedure using Ben’s suggestion. After debugging her program, Alyssa shows it to a potential user, who complains that her program solves the wrong problem. He wants a program that can deal with numbers represented as a center value and an additive tolerance; for example, he wants to work with intervals such as 3.5 ± 0.15 rather than [3.35, 3.65]. Alyssa 163 turns to her desk and fixes this problem by supplying an alternate constructor and alternate selectors: (define (make-center-width c w) (make-interval (- c w) (+ c w))) (define (center i) (/ (+ (lower-bound i) (upper-bound i)) 2)) (define (width i) (/ (- (upper-bound i) (lower-bound i)) 2)) Unfortunately, most of Alyssa’s users are engineers. Real engineering situations usually involve measurements with only a small uncertainty, measured as the ratio of the width of the interval to the midpoint of the interval. Engineers usually specify percentage tolerances on the parameters of devices, as in the resistor specifications given earlier. Exercise 2.12: Define a constructor make-center-percent that takes a center and a percentage tolerance and produces the desired interval. You must also define a selector percent that produces the percentage tolerance for a given interval. The center selector is the same as the one shown above. Exercise 2.13: Show that under the assumption of small percentage tolerances there is a simple formula for the approximate percentage tolerance of the product of two intervals in terms of the tolerances of the factors. You may simplify the problem by assuming that all numbers are positive. 164 After considerable work, Alyssa P. Hacker delivers her finished system. Several years later, after she has forgotten all about it, she gets a frenzied call from an irate user, Lem E. Tweakit. It seems that Lem has noticed that the formula for parallel resistors can be written in two algebraically equivalent ways: R1R2 R1 + R2 and 1 1/R 1 + 1/R 2 He has written the following two programs, each of which computes the parallel-resistors formula differently: (define (par1 r1 r2) (div-interval (mul-interval r1 r2) (add-interval r1 r2))) (define (par2 r1 r2) (let ((one (make-interval 1 1))) (div-interval one (add-interval (div-interval one r1) (div-interval one r2))))) Lem complains that Alyssa’s program gives different answers for the two ways of computing. This is a serious complaint. Exercise 2.14: Demonstrate that Lem is right. Investigate the behavior of the system on a variety of arithmetic expressions. Make some intervals A and B , and use them in computing the expressions A /A and A /B . You will get the most insight by using intervals whose width is a small percentage of the center value. Examine the results of the computation in center-percent form (see Ex. 2.12). 165 Exercise 2.15: Eva Lu Ator, another user, has also noticed the different intervals computed by different but algebraically equivalent expressions. She says that a formula to compute with intervals using Alyssa’s system will produce tighter error bounds if it can be written in such a form that no variable that represents an uncertain number is repeated. Thus, she says, par2 is a “better” program for parallel resistances than par1. Is she right? Why? Exercise 2.16: Explain, in general, why equivalent algebraic expressions may lead to different answers. Can you devise an interval-arithmetic package that does not have this shortcoming, or is this task impossible? (Warning: This problem is very difficult.) 2.2 Hierarchical Data and the Closure Property As we have seen, pairs provide a primitive “glue” that we can use to construct compound data objects. Figure 2.2 shows a standard way to visualize a pair—in this case, the pair formed by (cons 1 2). In this representation, which is called box-and-pointer notation, each object is shown as a pointer to a box. The box for a primitive object contains a representation of the object. For example, the box for a number contains a numeral. The box for a pair is actually a double box, the left part containing (a pointer to) the car of the pair and the right part containing the cdr. We have already seen that cons can be used to combine not only numbers but pairs as well. (You made use of this fact, or should have, in doing Exercise 2.2 and Exercise 2.3.) As a consequence, pairs provide a universal building block from which we can construct all sorts of data structures. Fig. 2.3 shows two ways to use pairs to combine the numbers 1, 2, 3, and 4. Figure 2.2: Box-and-pointer representation of (cons 1 2). Figure 2.3: Two ways to combine 1, 2, 3, and 4 using pairs. The ability to create pairs whose elements are pairs is the essence of list structure’s importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can themselves be combined using the same operation.6 Closure is the key to power in any means of combination because it 6 The use of the word “closure” here comes from abstract algebra, where a set of ele- ments is said to be closed under an operation if applying the operation to elements 167 permits us to create hierarchical structures—structures made up of parts, which themselves are made up of parts, and so on. From the outset of Chapter 1, we’ve made essential use of closure in dealing with procedures, because all but the very simplest programs rely on the fact that the elements of a combination can themselves be combinations. In this section, we take up the consequences of closure for compound data. We describe some conventional techniques for using pairs to represent sequences and trees, and we exhibit a graphics language that illustrates closure in a vivid way.7 in the set produces an element that is again an element of the set. The Lisp community also (unfortunately) uses the word “closure” to describe a totally unrelated concept: A closure is an implementation technique for representing procedures with free variables. We do not use the word “closure” in this second sense in this book. 7 The notion that a means of combination should satisfy closure is a straightforward idea. Unfortunately, the data combiners provided in many popular programming languages do not satisfy closure, or make closure cumbersome to exploit. In Fortran or Basic, one typically combines data elements by assembling them into arrays— but one cannot form arrays whose elements are themselves arrays. Pascal and C admit structures whose elements are structures. However, this requires that the programmer manipulate pointers explicitly, and adhere to the restriction that each field of a structure can contain only elements of a prespecified form. Unlike Lisp with its pairs, these languages have no built-in general-purpose glue that makes it easy to manipulate compound data in a uniform way. This limitation lies behind Alan Perlis’s comment in his foreword to this book: “In Pascal the plethora of declarable data structures induces a specialization within functions that inhibits and penalizes casual cooperation. It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures.” 2.2.1 Representing Sequences One of the useful structures we can build with pairs is a sequence—an ordered collection of data objects. There are, of course, many ways to represent sequences in terms of pairs. One particularly straightforward representation is illustrated in Figure 2.4, where the sequence 1, 2, 3, 4 is represented as a chain of pairs. The car of each pair is the corresponding item in the chain, and the cdr of the pair is the next pair in the chain. The cdr of the final pair signals the end of the sequence by pointing to a distinguished value that is not a pair, represented in box-and-pointer diagrams as a diagonal line and in programs as the value of the variable nil. The entire sequence is constructed by nested cons operations: (cons 1 (cons 2 (cons 3 (cons 4 nil)))) Figure 2.4: The sequence 1, 2, 3, 4 represented as a chain of pairs. Such a sequence of pairs, formed by nested conses, is called a list, and Scheme provides a primitive called list to help in constructing lists.8 The above sequence could be produced by (list 1 2 3 4). In general, 8 In this book, we use list to mean a chain of pairs terminated by the end-of-list marker. In contrast, the term list structure refers to any data structure made out of pairs, not just to lists. 169 (list 〈a 1 〉〈a 2 〉 ... 〈a n 〉) is equivalent to (cons 〈a 1 〉 (cons 〈a 2 〉 (cons ... (cons 〈a n 〉 nil) ...))) Lisp systems conventionally print lists by printing the sequence of elements, enclosed in parentheses. Thus, the data object in Figure 2.4 is printed as (1 2 3 4): (define one-through-four (list 1 2 3 4)) one-through-four (1 2 3 4) Be careful not to confuse the expression (list 1 2 3 4) with the list (1 2 3 4), which is the result obtained when the expression is evaluated. Attempting to evaluate the expression (1 2 3 4) will signal an error when the interpreter tries to apply the procedure 1 to arguments 2, 3, and 4. We can think of car as selecting the first item in the list, and of cdr as selecting the sublist consisting of all but the first item. Nested applications of car and cdr can be used to extract the second, third, and subsequent items in the list.9 The constructor cons makes a list like the original one, but with an additional item at the beginning. 9 Since nested applications of car and cdr are cumbersome to write, Lisp dialects provide abbreviations for them—for instance, (cadr (arg)) = (car (cdr (arg))) 170 (car one-through-four) 1 (cdr one-through-four) (2 3 4) (car (cdr one-through-four)) 2 (cons 10 one-through-four) (10 1 2 3 4) (cons 5 one-through-four) (5 1 2 3 4) The value of nil, used to terminate the chain of pairs, can be thought of as a sequence of no elements, the empty list. The word nil is a contraction of the Latin word nihil, which means “nothing.”10 The names of all such procedures start with c and end with r. Each a between them stands for a car operation and each d for a cdr operation, to be applied in the same order in which they appear in the name. The names car and cdr persist because simple combinations like cadr are pronounceable. 10 It’s remarkable how much energy in the standardization of Lisp dialects has been dissipated in arguments that are literally over nothing: Should nil be an ordinary name? Should the value of nil be a symbol? Should it be a list? Should it be a pair? In Scheme, nil is an ordinary name, which we use in this section as a variable whose value is the end-of-list marker (just as true is an ordinary variable that has a true value). Other dialects of Lisp, including Common Lisp, treat nil as a special symbol. The authors of this book, who have endured too many language List operations The use of pairs to represent sequences of elements as lists is accompanied by conventional programming techniques for manipulating lists by successively “cdring down” the lists. For example, the procedure list-ref takes as arguments a list and a number n and returns the n th item of the list. It is customary to number the elements of the list beginning with 0. The method for computing list-ref is the following: • For n = 0, list-ref should return the car of the list. • Otherwise, list-ref should return the (n − 1)-st item of the cdr of the list. (define (list-ref items n) (if (= n 0) (car items) (list-ref (cdr items) (- n 1)))) (define squares (list 1 4 9 16 25)) (list-ref squares 3) 16 Often we cdr down the whole list. To aid in this, Scheme includes a primitive predicate null?, which tests whether its argument is the empty list. The procedure length, which returns the number of items in a list, illustrates this typical pattern of use: standardization brawls, would like to avoid the entire issue. Once we have introduced quotation in Section 2.3, we will denote the empty list as ’() and dispense with the variable nil entirely. 172 (define (length items) (if (null? items) 0 (+ 1 (length (cdr items))))) (define odds (list 1 3 5 7)) (length odds) 4 The length procedure implements a simple recursive plan. The reduction step is: • The length of any list is 1 plus the length of the cdr of the list. This is applied successively until we reach the base case: • The length of the empty list is 0. We could also compute length in an iterative style: (define (length items) (define (length-iter a count) (if (null? a) count (length-iter (cdr a) (+ 1 count)))) (length-iter items 0)) Another conventional programming technique is to “cons up” an answer list while cdring down a list, as in the procedure append, which takes two lists as arguments and combines their elements to make a new list: (append squares odds) (1 4 9 16 25 1 3 5 7) (append odds squares) (1 3 5 7 1 4 9 16 25) Append is also implemented using a recursive plan. To append lists list1 and list2, do the following: • If list1 is the empty list, then the result is just list2. • Otherwise, append the cdr of list1 and list2, and cons the car of list1 onto the result: (define (append list1 list2) (if (null? list1) list2 (cons (car list1) (append (cdr list1) list2)))) Exercise 2.17: Define a procedure last-pair that returns the list that contains only the last element of a given (nonempty) list: (last-pair (list 23 72 149 34)) (34) Exercise 2.18: Define a procedure reverse that takes a list as argument and returns a list of the same elements in reverse order: (reverse (list 1 4 9 16 25)) (25 16 9 4 1) Exercise 2.19: Consider the change-counting program of Section 1.2.2. It would be nice to be able to easily change the currency used by the program, so that we could compute the number of ways to change a British pound, for example. As the program is written, the knowledge of the currency is distributed partly into the procedure first-denomination and partly into the procedure count-change 174 (which knows that there are five kinds of U.S. coins). It would be nicer to be able to supply a list of coins to be used for making change. We want to rewrite the procedure cc so that its second argument is a list of the values of the coins to use rather than an integer specifying which coins to use. We could then have lists that defined each kind of currency: (define us-coins (list 50 25 10 5 1)) (define uk-coins (list 100 50 20 10 5 2 1 0.5)) We could then call cc as follows: (cc 100 us-coins) 292 To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows: (define (cc amount coin-values) (cond ((= amount 0) 1) ((or (< amount 0) (no-more? coin-values)) 0) (else (+ (cc amount (except-first-denomination coin-values)) (cc (- amount (first-denomination coin-values)) coin-values))))) 175 Define the procedures first-denomination, except-first-denomination and no-more? in terms of primitive operations on list structures. Does the order of the list coin-values affect the answer produced by cc? Why or why not? Exercise 2.20: The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use define with dotted-tail notation. In a procedure definition, a pa- rameter list that has a dot before the last parameter name indicates that, when the procedure is called, the initial parameters (if any) will have as values the initial arguments, as usual, but the final parameter’s value will be a list of any remaining arguments. For instance, given the definition (define (f x y . z) 〈body〉) the procedure f can be called with two or more arguments. If we evaluate (f 1 2 3 4 5 6) then in the body of f, x will be 1, y will be 2, and z will be the list (3 4 5 6). Given the definition (define (g . w) 〈body〉) the procedure g can be called with zero or more arguments. If we evaluate (g 1 2 3 4 5 6) then in the body of g, w will be the list (1 2 3 4 5 6).11 11 To define f and g using lambda we would write (define f (lambda (x y . z) 〈body〉)) (define g (lambda w 〈body〉)) 176 Use this notation to write a procedure same-parity that takes one or more integers and returns a list of all the arguments that have the same even-odd parity as the first argument. For example, (same-parity 1 2 3 4 5 6 7) (1 3 5 7) (same-parity 2 3 4 5 6 7) (2 4 6) Mapping over lists One extremely useful operation is to apply some transformation to each element in a list and generate the list of results. For instance, the following procedure scales each number in a list by a given factor: (define (scale-list items factor) (if (null? items) nil (cons (* (car items) factor) (scale-list (cdr items) factor)))) (scale-list (list 1 2 3 4 5) 10) (10 20 30 40 50) We can abstract this general idea and capture it as a common pattern expressed as a higher-order procedure, just as in Section 1.3. The higherorder procedure here is called map. Map takes as arguments a procedure 177 of one argument and a list, and returns a list of the results produced by applying the procedure to each element in the list:12 (define (map proc items) (if (null? items) nil (cons (proc (car items)) (map proc (cdr items))))) (map abs (list -10 2.5 -11.6 17)) (10 2.5 11.6 17) (map (lambda (x) (* x x)) (list 1 2 3 4)) (1 4 9 16) Now we can give a new definition of scale-list in terms of map: (define (scale-list items factor) 12 Scheme standardly provides a map procedure that is more general than the one de- scribed here. This more general map takes a procedure of n arguments, together with n lists, and applies the procedure to all the first elements of the lists, all the second elements of the lists, and so on, returning a list of the results. For example: (map + (list 1 2 3) (list 40 50 60) (list 700 800 900)) (741 852 963) (map (lambda (x y) (+ x (* 2 y))) (list 1 2 3) (list 4 5 6)) (9 12 15) 178 (map (lambda (x) (* x factor)) items)) Map is an important construct, not only because it captures a common pattern, but because it establishes a higher level of abstraction in dealing with lists. In the original definition of scale-list, the recursive structure of the program draws attention to the element-by-element processing of the list. Defining scale-list in terms of map suppresses that level of detail and emphasizes that scaling transforms a list of elements to a list of results. The difference between the two definitions is not that the computer is performing a different process (it isn’t) but that we think about the process differently. In effect, map helps establish an abstraction barrier that isolates the implementation of procedures that transform lists from the details of how the elements of the list are extracted and combined. Like the barriers shown in Figure 2.1, this abstraction gives us the flexibility to change the low-level details of how sequences are implemented, while preserving the conceptual framework of operations that transform sequences to sequences. Section 2.2.3 expands on this use of sequences as a framework for organizing programs. Exercise 2.21: The procedure square-list takes a list of numbers as argument and returns a list of the squares of those numbers. (square-list (list 1 2 3 4)) (1 4 9 16) Here are two different definitions of square-list. Complete both of them by filling in the missing expressions: (define (square-list items) (if (null? items) nil 179 (cons 〈??〉〈??〉))) (define (square-list items) (map 〈??〉〈??〉)) Exercise 2.22: Louis Reasoner tries to rewrite the first square-list procedure of Exercise 2.21 so that it evolves an iterative process: (define (square-list items) (define (iter things answer) (if (null? things) answer (iter (cdr things) (cons (square (car things)) answer)))) (iter items nil)) Unfortunately, defining square-list this way produces the answer list in the reverse order of the one desired. Why? Louis then tries to fix his bug by interchanging the arguments to cons: (define (square-list items) (define (iter things answer) (if (null? things) answer (iter (cdr things) (cons answer (square (car things)))))) (iter items nil)) This doesn’t work either. Explain. 180 Exercise 2.23: The procedure for-each is similar to map. It takes as arguments a procedure and a list of elements. However, rather than forming a list of the results, for-each just applies the procedure to each of the elements in turn, from left to right. The values returned by applying the procedure to the elements are not used at all—for-each is used with procedures that perform an action, such as printing. For example, (for-each (lambda (x) (newline) (display x)) (list 57 321 88)) 57 321 88 The value returned by the call to for-each (not illustrated above) can be something arbitrary, such as true. Give an implementation of for-each. 2.2.2 Hierarchical Structures The representation of sequences in terms of lists generalizes naturally to represent sequences whose elements may themselves be sequences. For example, we can regard the object ((1 2) 3 4) constructed by (cons (list 1 2) (list 3 4)) as a list of three items, the first of which is itself a list, (1 2). Indeed, this is suggested by the form in which the result is printed by the interpreter. Figure 2.5 shows the representation of this structure in terms of pairs. Another way to think of sequences whose elements are sequences is as trees. The elements of the sequence are the branches of the tree, and el- 181 ements that are themselves sequences are subtrees. Figure 2.6 shows the structure in Figure 2.5 viewed as a tree. Figure 2.5: Structure formed by (cons (list 1 2) (list 3 4)). Figure 2.6: The list structure in Figure 2.5 viewed as a tree. Recursion is a natural tool for dealing with tree structures, since we can often reduce operations on trees to operations on their branches, which reduce in turn to operations on the branches of the branches, and so on, until we reach the leaves of the tree. As an example, compare the length procedure of Section 2.2.1 with the count-leaves procedure, which returns the total number of leaves of a tree: 182 (define x (cons (list 1 2) (list 3 4))) (length x) 3 (count-leaves x) 4 (list x x) (((1 2) 3 4) ((1 2) 3 4)) (length (list x x)) 2 (count-leaves (list x x)) 8 To implement count-leaves, recall the recursive plan for computing length: • Length of a list x is 1 plus length of the cdr of x. • Length of the empty list is 0. Count-leaves is similar. The value for the empty list is the same: • Count-leaves of the empty list is 0. But in the reduction step, where we strip off the car of the list, we must take into account that the car may itself be a tree whose leaves we need to count. Thus, the appropriate reduction step is • Count-leaves of a tree x is count-leaves of the car of x plus count-leaves of the cdr of x. 183 Finally, by taking cars we reach actual leaves, so we need another base case: • Count-leaves of a leaf is 1. To aid in writing recursive procedures on trees, Scheme provides the primitive predicate pair?, which tests whether its argument is a pair. Here is the complete procedure:13 (define (count-leaves x) (cond ((null? x) 0) ((not (pair? x)) 1) (else (+ (count-leaves (car x)) (count-leaves (cdr x)))))) Exercise 2.24: Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in Figure 2.6). Exercise 2.25: Give combinations of cars and cdrs that will pick 7 from each of the following lists: (1 3 (5 7) 9) ((7)) (1 (2 (3 (4 (5 (6 7)))))) Exercise 2.26: Suppose we define x and y to be two lists: (define x (list 1 2 3)) 13 The order of the first two clauses in the cond matters, since the empty list satisfies null? and also is not a pair. (define y (list 4 5 6)) What result is printed by the interpreter in response to evaluating each of the following expressions: (append x y) (cons x y) (list x y) Exercise 2.27: Modify your reverse procedure of Exercise 2.18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example, (define x (list (list 1 2) (list 3 4))) x ((1 2) (3 4)) (reverse x) ((3 4) (1 2)) (deep-reverse x) ((4 3) (2 1)) Exercise 2.28: Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-right order. For example, (define x (list (list 1 2) (list 3 4))) (fringe x) (1 2 3 4) (fringe (list x x)) (1 2 3 4 1 2 3 4) Exercise 2.29: A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list): (define (make-mobile left right) (list left right)) A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile: (define (make-branch length structure) (list length structure)) a. Write the corresponding selectors left-branch and rightbranch, which return the branches of a mobile, and branchlength and branch-structure, which return the components of a branch. b. Using your selectors, define a procedure total-weight that returns the total weight of a mobile. c. A mobile is said to be balanced if the torque applied by its topleft branch is equal to that applied by its top-right branch (that 186 is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced. d. Suppose we change the representation of mobiles so that the constructors are (define (make-mobile left right) (cons left right)) (define (make-branch length structure) (cons length structure)) How much do you need to change your programs to convert to the new representation? Mapping over trees Just as map is a powerful abstraction for dealing with sequences, map together with recursion is a powerful abstraction for dealing with trees. For instance, the scale-tree procedure, analogous to scale-list of Section 2.2.1, takes as arguments a numeric factor and a tree whose leaves are numbers. It returns a tree of the same shape, where each number is multiplied by the factor. The recursive plan for scale-tree is similar to the one for count-leaves: (define (scale-tree tree factor) (cond ((null? tree) nil) ((not (pair? tree)) (* tree factor)) (else (cons (scale-tree (car tree) factor) 187 (scale-tree (cdr tree) factor))))) (scale-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)) 10) (10 (20 (30 40) 50) (60 70)) Another way to implement scale-tree is to regard the tree as a sequence of sub-trees and use map. We map over the sequence, scaling each sub-tree in turn, and return the list of results. In the base case, where the tree is a leaf, we simply multiply by the factor: (define (scale-tree tree factor) (map (lambda (sub-tree) (if (pair? sub-tree) (scale-tree sub-tree factor) (* sub-tree factor))) Many tree operations can be implemented by similar combinations of sequence operations and recursion. Exercise 2.30: Define a procedure square-tree analogous to the square-list procedure of Exercise 2.21. That is, square-tree should behave as follows: (square-tree (list 1 (list 2 (list 3 4) 5) (list 6 7))) (1 (4 (9 16) 25) (36 49)) Define square-tree both directly (i.e., without using any higherorder procedures) and also by using map and recursion. 188 Exercise 2.31: Abstract your answer to Exercise 2.30 to produce a procedure tree-map with the property that square-tree could be defined as (define (square-tree tree) (tree-map square tree)) Exercise 2.32: We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition of a procedure that generates the set of subsets of a set and give a clear explanation of why it works: (define (subsets s) (if (null? s) (list nil) (let ((rest (subsets (cdr s)))) (append rest (map 〈??〉 rest))))) 2.2.3 Sequences as Conventional Interfaces In working with compound data, we’ve stressed how data abstraction permits us to design programs without becoming enmeshed in the details of data representations, and how abstraction preserves for us the flexibility to experiment with alternative representations. In this section, we introduce another powerful design principle for working with data structures—the use of conventional interfaces. In Section 1.3 we saw how program abstractions, implemented as higher-order procedures, can capture common patterns in programs that deal with numerical data. Our ability to formulate analogous operations for working with compound data depends crucially on the style in which 189 we manipulate our data structures. Consider, for example, the following procedure, analogous to the count-leaves procedure of Section 2.2.2, which takes a tree as argument and computes the sum of the squares of the leaves that are odd: (define (sum-odd-squares tree) (cond ((null? tree) 0) ((not (pair? tree)) (if (odd? tree) (square tree) 0)) (else (+ (sum-odd-squares (car tree)) (sum-odd-squares (cdr tree)))))) On the surface, this procedure is very different from the following one, which constructs a list of all the even Fibonacci numbers Fib(k ), where k is less than or equal to a given integer n : (define (even-fibs n) (define (next k) (if (> k n) nil (let ((f (fib k))) (if (even? f) (cons f (next (+ k 1))) (next (+ k 1)))))) (next 0)) Despite the fact that these two procedures are structurally very different, a more abstract description of the two computations reveals a great deal of similarity. The first program • enumerates the leaves of a tree; • filters them, selecting the odd ones; • squares each of the selected ones; and • accumulates the results using +, starting with 0. The second program • enumerates the integers from 0 to n ; • computes the Fibonacci number for each integer; • filters them, selecting the even ones; and • accumulates the results using cons, starting with the empty list. A signal-processing engineer would find it natural to conceptualize these processes in terms of signals flowing through a cascade of stages, each of which implements part of the program plan, as shown in Figure 2.7. In sum-odd-squares, we begin with an enumerator, which generates a “signal” consisting of the leaves of a given tree. This signal is passed through a filter, which eliminates all but the odd elements. The resulting signal is in turn passed through a map, which is a “transducer” that applies the square procedure to each element. The output of the map is then fed to an accumulator, which combines the elements using +, starting from an initial 0. The plan for even-fibs is analogous. Unfortunately, the two procedure definitions above fail to exhibit this signal-flow structure. For instance, if we examine the sum-odd-squares procedure, we find that the enumeration is implemented partly by the null? and pair? tests and partly by the tree-recursive structure of the procedure. Similarly, the accumulation is found partly in the tests and partly in the addition used in the recursion. In general, there are no distinct parts of either procedure that correspond to the elements in the signal-flow description. Our two procedures decompose the computations in a different way, spreading the enumeration over the 191 program and mingling it with the map, the filter, and the accumulation. If we could organize our programs to make the signal-flow structure manifest in the procedures we write, this would increase the conceptual clarity of the resulting code. Figure 2.7: The signal-flow plans for the procedures sum-oddsquares (top) and even-fibs (bottom) reveal the commonality between the two programs. Sequence Operations The key to organizing programs so as to more clearly reflect the signal-flow structure is to concentrate on the “signals” that flow from one stage in the process to the next. If we represent these signals as lists, then we can use list operations to implement the processing at each of the stages. For instance, we can implement the mapping stages of the signal-flow diagrams using the map procedure from Section 2.2.1: (map square (list 1 2 3 4 5)) (1 4 9 16 25) Filtering a sequence to select only those elements that satisfy a given predicate is accomplished by 192 (define (filter predicate sequence) (cond ((null? sequence) nil) ((predicate (car sequence)) (cons (car sequence) (filter predicate (cdr sequence)))) (else (filter predicate (cdr sequence))))) For example, (filter odd? (list 1 2 3 4 5)) (1 3 5) Accumulations can be implemented by (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) (accumulate + 0 (list 1 2 3 4 5)) 15 (accumulate * 1 (list 1 2 3 4 5)) 120 (accumulate cons nil (list 1 2 3 4 5)) (1 2 3 4 5) All that remains to implement signal-flow diagrams is to enumerate the sequence of elements to be processed. For even-fibs, we need to generate the sequence of integers in a given range, which we can do as follows: 193 (define (enumerate-interval low high) (if (> low high) nil (cons low (enumerate-interval (+ low 1) high)))) (enumerate-interval 2 7) (2 3 4 5 6 7) To enumerate the leaves of a tree, we can use14 (define (enumerate-tree tree) (cond ((null? tree) nil) ((not (pair? tree)) (list tree)) (else (append (enumerate-tree (car tree)) (enumerate-tree (cdr tree)))))) (enumerate-tree (list 1 (list 2 (list 3 4)) 5)) (1 2 3 4 5) Now we can reformulate sum-odd-squares and even-fibs as in the signal-flow diagrams. For sum-odd-squares, we enumerate the sequence of leaves of the tree, filter this to keep only the odd numbers in the sequence, square each element, and sum the results: (define (sum-odd-squares tree) (accumulate + 0 (map square 14 This is, in fact, precisely the fringe procedure from Exercise 2.28. Here we’ve named it to emphasize that it is part of a family of general sequence-manipulation procedures. 194 (filter odd? (enumerate-tree tree))))) For even-fibs, we enumerate the integers from 0 to n , generate the Fibonacci number for each of these integers, filter the resulting sequence to keep only the even elements, and accumulate the results into a list: (define (even-fibs n) (accumulate cons nil (filter even? (map fib (enumerate-interval 0 n))))) The value of expressing programs as sequence operations is that this helps us make program designs that are modular, that is, designs that are constructed by combining relatively independent pieces. We can encourage modular design by providing a library of standard components together with a conventional interface for connecting the components in flexible ways. Modular construction is a powerful strategy for controlling complexity in engineering design. In real signal-processing applications, for example, designers regularly build systems by cascading elements selected from standardized families of filters and transducers. Similarly, sequence operations provide a library of standard program elements that we can mix and match. For instance, we can reuse pieces from the sum-odd-squares and even-fibs procedures in a program that constructs a list of the squares of the first n + 1 Fibonacci numbers: (define (list-fib-squares n) (accumulate cons 195 nil (map square (map fib (enumerate-interval 0 n))))) (list-fib-squares 10) (0 1 1 4 9 25 64 169 441 1156 3025) We can rearrange the pieces and use them in computing the product of the squares of the odd integers in a sequence: (define (product-of-squares-of-odd-elements sequence) (accumulate * 1 (map square (filter odd? sequence)))) (product-of-squares-of-odd-elements (list 1 2 3 4 5)) 225 We can also formulate conventional data-processing applications in terms of sequence operations. Suppose we have a sequence of personnel records and we want to find the salary of the highest-paid programmer. Assume that we have a selector salary that returns the salary of a record, and a predicate programmer? that tests if a record is for a programmer. Then we can write (define (salary-of-highest-paid-programmer records) (accumulate max 0 (map salary 196 (filter programmer? records)))) These examples give just a hint of the vast range of operations that can be expressed as sequence operations.15 Sequences, implemented here as lists, serve as a conventional interface that permits us to combine processing modules. Additionally, when we uniformly represent structures as sequences, we have localized the datastructure dependencies in our programs to a small number of sequence operations. By changing these, we can experiment with alternative representations of sequences, while leaving the overall design of our programs intact. We will exploit this capability in Section 3.5, when we generalize the sequence-processing paradigm to admit infinite sequences. Exercise 2.33: Fill in the missing expressions to complete the following definitions of some basic list-manipulation operations as accumulations: (define (map p sequence) (accumulate (lambda (x y) 〈??〉) nil sequence)) (define (append seq1 seq2) 15 Richard Waters (1979) developed a program that automatically analyzes traditional Fortran programs, viewing them in terms of maps, filters, and accumulations. He found that fully 90 percent of the code in the Fortran Scientific Subroutine Package fits neatly into this paradigm. One of the reasons for the success of Lisp as a programming language is that lists provide a standard medium for expressing ordered collections so that they can be manipulated using higher-order operations. The programming language APL owes much of its power and appeal to a similar choice. In APL all data are represented as arrays, and there is a universal and convenient set of generic operators for all sorts of array operations. 197 (accumulate cons 〈??〉〈??〉)) (define (length sequence) (accumulate 〈??〉 0 sequence)) Exercise 2.34: Evaluating a polynomial in x at a given value of x can be formulated as an accumulation. We evaluate the polynomial a n x n + a n−1 x n−1 + . . . + a 1 x + a 0 using a well-known algorithm called Horner’s rule, which structures the computation as (. . . (a n x + a n−1 )x + . . . + a 1 )x + a 0 In other words, we start with a n , multiply by x , add a n−1 , multiply by x , and so on, until we reach a 0 .16 Fill in the following template to produce a procedure that evaluates a polynomial using Horner’s rule. Assume that the coefficients of the polynomial are arranged in a sequence, from a 0 through a n . 16 According to Knuth 1981, this rule was formulated by W. G. Horner early in the nine- teenth century, but the method was actually used by Newton over a hundred years earlier. Horner’s rule evaluates the polynomial using fewer additions and multiplications than does the straightforward method of first computing a n x n , then adding a n−1 x n−1 , and so on. In fact, it is possible to prove that any algorithm for evaluating arbitrary polynomials must use at least as many additions and multiplications as does Horner’s rule, and thus Horner’s rule is an optimal algorithm for polynomial evaluation. This was proved (for the number of additions) by A. M. Ostrowski in a 1954 paper that essentially founded the modern study of optimal algorithms. The analogous statement for multiplications was proved by V. Y. Pan in 1966. The book by Borodin and Munro (1975) provides an overview of these and other results about optimal 198 (define (horner-eval x coefficient-sequence) (accumulate (lambda (this-coeff higher-terms) 〈??〉) 0 coefficient-sequence)) For example, to compute 1 + 3x + 5x 3 + x 5 at x = 2 you would evaluate (horner-eval 2 (list 1 3 0 5 0 1)) Exercise 2.35: Redefine count-leaves from Section 2.2.2 as an accumulation: (define (count-leaves t) (accumulate 〈??〉〈??〉 (map 〈??〉〈??〉))) Exercise 2.36: The procedure accumulate-n is similar to accumulate except that it takes as its third argument a sequence of sequences, which are all assumed to have the same number of elements. It applies the designated accumulation procedure to combine all the first elements of the sequences, all the second elements of the sequences, and so on, and returns a sequence of the results. For instance, if s is a sequence containing four sequences, ((1 2 3) (4 5 6) (7 8 9) (10 11 12)), then the value of (accumulate-n + 0 s) should be the sequence (22 26 30). Fill in the missing expressions in the following definition of accumulate-n: (define (accumulate-n op init seqs) (if (null? (car seqs)) nil (cons (accumulate op init 〈??〉) (accumulate-n op init 〈??〉)))) 199 Exercise 2.37: Suppose we represent vectors v = (v i ) as sequences of numbers, and matrices m = (m i j ) as sequences of vectors (the rows of the matrix). For example, the matrix is represented as the sequence ((1 2 3 4) (4 5 6 6) (6 7 8 9)). With this representation, we can use sequence operations to concisely express the basic matrix and vector operations. These operations (which are described in any book on matrix algebra) are the following: (dot-product v w) returns the sum Σi v i w i (matrix-*-vector m v) returns the vector t, where t i = Σ j m i j v j (matrix-*-matrix m n) returns the matrix p, where p i j = Σk m i k n k j (transpose m) returns the matrix n, where n i j = m j i We can define the dot product as17 (define (dot-product v w) (accumulate + 0 (map * v w))) Fill in the missing expressions in the following procedures for computing the other matrix operations. (The procedure accumulate-n is defined in Exercise 2.36.) (define (matrix-*-vector m v) (map 〈??〉 m)) 17 This definition uses the extended version of map described in Footnote 12. 200 (define (transpose mat) (accumulate-n 〈??〉〈??〉 mat)) (define (matrix-*-matrix m n) (let ((cols (transpose n))) (map 〈??〉 m))) Exercise 2.38: The accumulate procedure is also known as fold-right, because it combines the first element of the se- quence with the result of combining all the elements to the right. There is also a fold-left, which is similar to fold-right, except that it combines elements working in the opposite direction: (define (fold-left op initial sequence) (define (iter result rest) (if (null? rest) result (iter (op result (car rest)) (cdr rest)))) (iter initial sequence)) What are the values of (fold-right / 1 (list 1 2 3)) (fold-left / 1 (list 1 2 3)) (fold-right list nil (list 1 2 3)) (fold-left list nil (list 1 2 3)) 201 Give a property that op should satisfy to guarantee that fold-right and fold-left will produce the same values for any sequence. Exercise 2.39: Complete the following definitions of reverse (Exercise 2.18) in terms of fold-right and fold-left from Exercise 2.38: (define (reverse sequence) (fold-right (lambda (x y) 〈??〉) nil sequence)) (define (reverse sequence) (fold-left (lambda (x y) 〈??〉) nil sequence)) Nested Mappings We can extend the sequence paradigm to include many computations that are commonly expressed using nested loops.18 Consider this problem: Given a positive integer n , find all ordered pairs of distinct positive integers i and j , where 1 ≤ j < i ≤ n , such that i + j is prime. For example, if n is 6, then the pairs are the following: i j A natural way to organize this computation is to generate the sequence of all ordered pairs of positive integers less than or equal to n , filter to select 18 This approach to nested mappings was shown to us by David Turner, whose lan- guages KRC and Miranda provide elegant formalisms for dealing with these constructs. The examples in this section (see also Exercise 2.42) are adapted from Turner 1981. In Section 3.5.3, we’ll see how this approach generalizes to infinite sequences. 202 those pairs whose sum is prime, and then, for each pair (i , j ) that passes through the filter, produce the triple (i , j , i + j ). Here is a way to generate the sequence of pairs: For each integer i ≤ n , enumerate the integers j < i , and for each such i and j generate the pair (i , j ). In terms of sequence operations, we map along the sequence (enumerate-interval 1 n). For each i in this sequence, we map along the sequence (enumerate-interval 1 (- i 1)). For each j in this latter sequence, we generate the pair (list i j). This gives us a sequence of pairs for each i . Combining all the sequences for all the i (by accumulating with append) produces the required sequence of pairs:19 (accumulate append nil (map (lambda (i) (map (lambda (j) (list i j)) (enumerate-interval 1 (- i 1)))) (enumerate-interval 1 n))) The combination of mapping and accumulating with append is so common in this sort of program that we will isolate it as a separate procedure: (define (flatmap proc seq) (accumulate append nil (map proc seq))) Now filter this sequence of pairs to find those whose sum is prime. The filter predicate is called for each element of the sequence; its argument is a pair and it must extract the integers from the pair. Thus, the predicate to apply to each element in the sequence is 19 We’re representing a pair here as a list of two elements rather than as a Lisp pair. Thus, the “pair” (i , j ) is represented as (list i j), not (cons i j). 203 (define (prime-sum? pair) (prime? (+ (car pair) (cadr pair)))) Finally, generate the sequence of results by mapping over the filtered pairs using the following procedure, which constructs a triple consisting of the two elements of the pair along with their sum: (define (make-pair-sum pair) (list (car pair) (cadr pair) (+ (car pair) (cadr pair)))) Combining all these steps yields the complete procedure: (define (prime-sum-pairs n) (map make-pair-sum (filter prime-sum? (flatmap (lambda (i) (map (lambda (j) (list i j)) (enumerate-interval 1 (- i 1)))) (enumerate-interval 1 n))))) Nested mappings are also useful for sequences other than those that enumerate intervals. Suppose we wish to generate all the permutations of a set S ; that is, all the ways of ordering the items in the set. For instance, the permutations of {1, 2, 3} are {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} and {3, 2, 1}. Here is a plan for generating the permutations of S : For each item x in S , recursively generate the sequence of permutations of S − x ,20 and adjoin x to the front of each one. This yields, for each x in 20 The set S − x is the set of all elements of S , excluding x . S , the sequence of permutations of S that begin with x . Combining these sequences for all x gives all the permutations of S :21 (define (permutations s) ; empty set? ; sequence containing empty set (if (null? s) (list nil) (flatmap (lambda (x) (map (lambda (p) (cons x p)) (permutations (remove x s)))) s))) Notice how this strategy reduces the problem of generating permutations of S to the problem of generating the permutations of sets with fewer elements than S . In the terminal case, we work our way down to the empty list, which represents a set of no elements. For this, we generate (list nil), which is a sequence with one item, namely the set with no elements. The remove procedure used in permutations returns all the items in a given sequence except for a given item. This can be expressed as a simple filter: (define (remove item sequence) (filter (lambda (x) (not (= x item))) Exercise 2.40: Define a procedure unique-pairs that, given an integer n , generates the sequence of pairs (i , j ) with 1 ≤ j < i ≤ n . Use unique-pairs to simplify the definition of prime-sum-pairs given above. 21 Semicolons in Scheme code are used to introduce comments. Everything from the semicolon to the end of the line is ignored by the interpreter. In this book we don’t use many comments; we try to make our programs self-documenting by using descriptive names. 205 Exercise 2.41: Write a procedure to find all ordered triples of distinct positive integers i , j , and k less than or equal to a given integer n that sum to a given integer s . Figure 2.8: A solution to the eight-queens puzzle. Exercise 2.42: The “eight-queens puzzle” asks how to place eight queens on a chessboard so that no queen is in check from any other (i.e., no two queens are in the same row, column, or diagonal). One possible solution is shown in Figure 2.8. One way to solve the puzzle is to work across the board, placing a queen in each column. Once we have placed k − 1 queens, we must place the k th queen in a position where it does not check any of the queens already on the board. We can formulate this approach recursively: Assume that we have already generated the sequence of all possible ways to place k − 1 queens in the first k − 1 columns of the 206 board. For each of these ways, generate an extended set of positions by placing a queen in each row of the k th column. Now filter these, keeping only the positions for which the queen in the k th column is safe with respect to the other queens. This produces the sequence of all ways to place k queens in the first k columns. By continuing this process, we will produce not only one solution, but all solutions to the puzzle. We implement this solution as a procedure queens, which returns a sequence of all solutions to the problem of placing n queens on an n×n chessboard. Queens has an internal procedure queen-cols that returns the sequence of all ways to place queens in the first k columns of the board. (define (queens board-size) (define (queen-cols k) (if (= k 0) (list empty-board) (filter (lambda (positions) (safe? k positions)) (flatmap (lambda (rest-of-queens) (map (lambda (new-row) (adjoin-position new-row k rest-of-queens)) (enumerate-interval 1 board-size))) (queen-cols (- k 1)))))) (queen-cols board-size)) 207 In this procedure rest-of-queens is a way to place k − 1 queens in the first k − 1 columns, and new-row is a proposed row in which to place the queen for the k th column. Complete the program by implementing the representation for sets of board positions, including the procedure adjoin-position, which adjoins a new row-column position to a set of positions, and empty-board, which represents an empty set of positions. You must also write the procedure safe?, which determines for a set of positions, whether the queen in the k th column is safe with respect to the others. (Note that we need only check whether the new queen is safe—the other queens are already guaranteed safe with respect to each other.) Exercise 2.43: Louis Reasoner is having a terrible time doing Exercise 2.42. His queens procedure seems to work, but it runs extremely slowly. (Louis never does manage to wait long enough for it to solve even the 6 × 6 case.) When Louis asks Eva Lu Ator for help, she points out that he has interchanged the order of the nested mappings in the flatmap, writing it as (flatmap (lambda (new-row) (map (lambda (rest-of-queens) (adjoin-position new-row k rest-of-queens)) (queen-cols (- k 1)))) (enumerate-interval 1 board-size)) Explain why this interchange makes the program run slowly. Estimate how long it will take Louis’s program to solve the 208 eight-queens puzzle, assuming that the program in Exercise 2.42 solves the puzzle in time T . Figure 2.9: Designs generated with the picture language. 2.2.4 Example: A Picture Language This section presents a simple language for drawing pictures that illustrates the power of data abstraction and closure, and also exploits higher-order procedures in an essential way. The language is designed to make it easy to experiment with patterns such as the ones in Figure 2.9, which are composed of repeated elements that are shifted and scaled.22 In this language, the data objects being combined are represented as procedures rather than 22 The picture language is based on the language Peter Henderson created to con- struct images like M.C. Escher’s “Square Limit” woodcut (see Henderson 1982). The woodcut incorporates a repeated scaled pattern, similar to the arrangements drawn using the square-limit procedure in this section. 209 as list structure. Just as cons, which satisfies the closure property, allowed us to easily build arbitrarily complicated list structure, the operations in this language, which also satisfy the closure property, allow us to easily build arbitrarily complicated patterns. The picture language When we began our study of programming in Section 1.1, we emphasized the importance of describing a language by focusing on the language’s primitives, its means of combination, and its means of abstraction. We’ll follow that framework here. Figure 2.10: Images produced by the wave painter, with respect to four different frames. The frames, shown with dotted lines, are not part of the images. 210 Part of the elegance of this picture language is that there is only one kind of element, called a painter. A painter draws an image that is shifted and scaled to fit within a designated parallelogram-shaped frame. For example, there’s a primitive painter we’ll call wave that makes a crude line drawing, as shown in Figure 2.10. The actual shape of the drawing depends on the frame—all four images in figure 2.10 are produced by the same wave painter, but with respect to four different frames. Painters can be more elaborate than this: The primitive painter called rogers paints a picture of MIT’s founder, William Barton Rogers, as shown in Figure 2.11.23 The four 23 William Barton Rogers (1804-1882) was the founder and first president of MIT. A geologist and talented teacher, he taught at William and Mary College and at the University of Virginia. In 1859 he moved to Boston, where he had more time for research, worked on a plan for establishing a “polytechnic institute,” and served as Massachusetts’s first State Inspector of Gas Meters. When MIT was established in 1861, Rogers was elected its first president. Rogers espoused an ideal of “useful learning” that was different from the university education of the time, with its overemphasis on the classics, which, as he wrote, “stand in the way of the broader, higher and more practical instruction and discipline of the natural and social sciences.” This education was likewise to be different from narrow trade-school education. In Rogers’s words: The world-enforced distinction between the practical and the scientific worker is utterly futile, and the whole experience of modern times has demonstrated its utter worthlessness. Rogers served as president of MIT until 1870, when he resigned due to ill health. In 1878 the second president of MIT, John Runkle, resigned under the pressure of a financial crisis brought on by the Panic of 1873 and strain of fighting off attempts by Harvard to take over MIT. Rogers returned to hold the office of president until 1881. 211 images in figure 2.11 are drawn with respect to the same four frames as the wave images in figure 2.10. To combine images, we use various operations that construct new painters from given painters. For example, the beside operation takes two painters and produces a new, compound painter that draws the first painter’s image in the left half of the frame and the second painter’s image in the right half of the frame. Similarly, below takes two painters and produces a compound painter that draws the first painter’s image below the second painter’s image. Some operations transform a single painter Rogers collapsed and died while addressing MIT’s graduating class at the commencement exercises of 1882. Runkle quoted Rogers’s last words in a memorial address delivered that same year: “As I stand here today and see what the Institute is, . . . I call to mind the beginnings of science. I remember one hundred and fifty years ago Stephen Hales published a pamphlet on the subject of illuminating gas, in which he stated that his researches had demonstrated that 128 grains of bituminous coal – ” “Bituminous coal,” these were his last words on earth. Here he bent forward, as if consulting some notes on the table before him, then slowly regaining an erect position, threw up his hands, and was translated from the scene of his earthly labors and triumphs to “the tomorrow of death,” where the mysteries of life are solved, and the disembodied spirit finds unending satisfaction in contemplating the new and still unfathomable mysteries of the infinite future. In the words of Francis A. Walker (MIT’s third president): All his life he had borne himself most faithfully and heroically, and he died as so good a knight would surely have wished, in harness, at his post, and in the very part and act of public duty. 212 to produce a new painter. For example, flip-vert takes a painter and produces a painter that draws its image upside-down, and flip-horiz produces a painter that draws the original painter’s image left-to-right reversed. Figure 2.11: Images of William Barton Rogers, founder and first president of MIT, painted with respect to the same four frames as in Figure 2.10 (original image from Wikimedia Commons). Figure 2.12 shows the drawing of a painter called wave4 that is built up in two stages starting from wave: (define wave2 (beside wave (flip-vert wave))) (define wave4 (below wave2 wave2)) 213 In building up a complex image in this manner we are exploiting the fact that painters are closed under the language’s means of combination. The beside or below of two painters is itself a painter; therefore, we can use it as an element in making more complex painters. As with building up list structure using cons, the closure of our data under the means of combination is crucial to the ability to create complex structures while using only a few operations. Figure 2.12: Creating a complex figure, starting from the wave painter of Figure 2.10. Once we can combine painters, we would like to be able to abstract typical patterns of combining painters. We will implement the painter operations as Scheme procedures. This means that we don’t need a special abstraction mechanism in the picture language: Since the means of combination are ordinary Scheme procedures, we automatically have the capability to do anything with painter operations that we can do with procedures. For example, we can abstract the pattern in wave4 as (define (flipped-pairs 214 (let ((painter2 (beside painter (flip-vert painter)))) (below painter2 painter2))) and define wave4 as an instance of this pattern: (define wave4 (flipped-pairs wave)) Figure 2.13: Recursive plans for right-split and corner-split. We can also define recursive operations. Here’s one that makes painters split and branch towards the right as shown in Figure 2.13 and Figure 2.14: (define (right-split painter n) (if (= n 0) painter (let ((smaller (right-split painter (- n 1)))) (beside painter (below smaller smaller))))) Figure 2.14: The recursive operations right-split and cornersplit applied to the painters wave and rogers. Combining four corner-split figures produces symmetric square-limit designs as shown in Figure 2.9. 216 We can produce balanced patterns by branching upwards as well as towards the right (see Exercise 2.44, Figure 2.13 and Figure 2.14): (define (corner-split painter n) (if (= n 0) painter (let ((up (up-split painter (- n 1))) (right (right-split painter (- n 1)))) (let ((top-left (beside up up)) (bottom-right (below right right)) (corner (corner-split painter (- n 1)))) (beside (below painter top-left) (below bottom-right corner)))))) By placing four copies of a corner-split appropriately, we obtain a pattern called square-limit, whose application to wave and rogers is shown in Figure 2.9: (define (square-limit painter n) (let ((quarter (corner-split painter n))) (let ((half (beside (flip-horiz quarter) quarter))) (below (flip-vert half) half)))) Exercise 2.44: Define the procedure up-split used by cornersplit. It is similar to right-split, except that it switches the roles of below and beside. Higher-order operations In addition to abstracting patterns of combining painters, we can work at a higher level, abstracting patterns of combining painter operations. That is, we can view the painter operations as elements to manipulate and can 217 write means of combination for these elements—procedures that take painter operations as arguments and create new painter operations. For example, flipped-pairs and square-limit each arrange four copies of a painter’s image in a square pattern; they differ only in how they orient the copies. One way to abstract this pattern of painter combination is with the following procedure, which takes four one-argument painter operations and produces a painter operation that transforms a given painter with those four operations and arranges the results in a square. Tl, tr, bl, and br are the transformations to apply to the top left copy, the top right copy, the bottom left copy, and the bottom right copy, respectively. (define (square-of-four tl tr bl br) (lambda (painter) (let ((top (beside (tl painter) (tr painter))) (bottom (beside (bl painter) (br painter)))) (below bottom top)))) Then flipped-pairs can be defined in terms of square-of-four as follows:24 (define (flipped-pairs painter) (let ((combine4 (square-of-four identity flip-vert identity flip-vert))) (combine4 painter))) 24 Equivalently, we could write (define flipped-pairs (square-of-four identity flip-vert identity flip-vert)) 218 and square-limit can be expressed as25 (define (square-limit painter n) (let ((combine4 (square-of-four flip-horiz identity rotate180 flip-vert))) (combine4 (corner-split painter n)))) Exercise 2.45: Right-split and up-split can be expressed as instances of a general splitting operation. Define a procedure split with the property that evaluating (define right-split (split beside below)) (define up-split (split below beside)) produces procedures right-split and up-split with the same behaviors as the ones already defined. Frames Before we can show how to implement painters and their means of combination, we must first consider frames. A frame can be described by three vectors—an origin vector and two edge vectors. The origin vector specifies the offset of the frame’s origin from some absolute origin in the plane, and the edge vectors specify the offsets of the frame’s corners from its origin. If the edges are perpendicular, the frame will be rectangular. Otherwise the frame will be a more general parallelogram. Figure 2.15 shows a frame and its associated vectors. In accordance with data abstraction, we need not be specific yet about how frames are represented, other than to say that there is a constructor make-frame, which 25 Rotate180 rotates a painter by 180 degrees (see Exercise 2.50). Instead of rotate180 we could say (compose flip-vert flip-horiz), using the compose procedure from Exercise 1.42. 219 takes three vectors and produces a frame, and three corresponding selectors origin-frame, edge1-frame, and edge2-frame (see Exercise 2.47). Figure 2.15: A frame is described by three vectors — an origin and two edges. We will use coordinates in the unit square (0 ≤ x, y ≤ 1) to specify images. With each frame, we associate a frame coordinate map, which will be used to shift and scale images to fit the frame. The map transforms the unit square into the frame by mapping the vector v = (x, y ) to the vector sum Origin(Frame) + x · Edge1 (Frame) + y · Edge2 (Frame) For example, (0, 0) is mapped to the origin of the frame, (1, 1) to the vertex diagonally opposite the origin, and (0.5, 0.5) to the center of the frame. We can create a frame’s coordinate map with the following procedure:26 26 Frame-coord-map uses the vector operations described in Exercise 2.46 below, which we assume have been implemented using some representation for vectors. Because of data abstraction, it doesn’t matter what this vector representation is, so long as the vector operations behave 220 (define (frame-coord-map frame) (lambda (v) (add-vect (origin-frame frame) (add-vect (scale-vect (xcor-vect v) (edge1-frame frame)) (scale-vect (ycor-vect v) (edge2-frame frame)))))) Observe that applying frame-coord-map to a frame returns a procedure that, given a vector, returns a vector. If the argument vector is in the unit square, the result vector will be in the frame. For example, ((frame-coord-map a-frame) (make-vect 0 0)) returns the same vector as (origin-frame a-frame) Exercise 2.46: A two-dimensional vector v running from the origin to a point can be represented as a pair consisting of an x -coordinate and a y -coordinate. Implement a data abstraction for vectors by giving a constructor make-vect and corresponding selectors xcor-vect and ycor-vect. In terms of your selectors and constructor, implement procedures add-vect, sub-vect, and scale-vect that perform the operations vector addition, vector subtraction, and multiplying a vector by a scalar: (x 1 , y 1 ) + (x 2 , y 2 ) = (x 1 + x 2 , y 1 + y 2 ) (x 1 , y 1 ) − (x 2 , y 2 ) = (x 1 − x 2 , y 1 − y 2 ) s · (x, y ) = (sx, s y ) 221 Exercise 2.47: Here are two possible constructors for frames: (define (make-frame origin edge1 edge2) (list origin edge1 edge2)) (define (make-frame origin edge1 edge2) (cons origin (cons edge1 For each constructor supply the appropriate selectors to produce an implementation for frames. Painters A painter is represented as a procedure that, given a frame as argument, draws a particular image shifted and scaled to fit the frame. That is to say, if p is a painter and f is a frame, then we produce p’s image in f by calling p with f as argument. The details of how primitive painters are implemented depend on the particular characteristics of the graphics system and the type of image to be drawn. For instance, suppose we have a procedure draw-line that draws a line on the screen between two specified points. Then we can create painters for line drawings, such as the wave painter in Figure 2.10, from lists of line segments as follows:27 (define (segments->painter segment-list) (lambda (frame) (for-each (lambda (segment) (draw-line 27 Segments->painter uses the representation for line segments described in Exercise 2.48 below. It also uses the for-each procedure described in Exercise 2.23. 222 ((frame-coord-map frame) (start-segment segment)) ((frame-coord-map frame) (end-segment segment)))) segment-list))) The segments are given using coordinates with respect to the unit square. For each segment in the list, the painter transforms the segment endpoints with the frame coordinate map and draws a line between the transformed points. Representing painters as procedures erects a powerful abstraction barrier in the picture language. We can create and intermix all sorts of primitive painters, based on a variety of graphics capabilities. The details of their implementation do not matter. Any procedure can serve as a painter, provided that it takes a frame as argument and draws something scaled to fit the frame.28 Exercise 2.48: A directed line segment in the plane can be represented as a pair of vectors—the vector running from the origin to the start-point of the segment, and the vector running from the origin to the end-point of the segment. Use your vector representation from Exercise 2.46 to define a representation 28 For example, the rogers painter of Figure 2.11 was constructed from a gray-level image. For each point in a given frame, the rogers painter determines the point in the image that is mapped to it under the frame coordinate map, and shades it accordingly. By allowing different types of painters, we are capitalizing on the abstract data idea discussed in Section 2.1.3, where we argued that a rational-number representation could be anything at all that satisfies an appropriate condition. Here we’re using the fact that a painter can be implemented in any way at all, so long as it draws something in the designated frame. Section 2.1.3 also showed how pairs could be implemented as procedures. Painters are our second example of a procedural representation for data. 223 for segments with a constructor make-segment and selectors start-segment and end-segment. Exercise 2.49: Use segments->painter to define the following primitive painters: a. The painter that draws the outline of the designated frame. b. The painter that draws an “X” by connecting opposite corners of the frame. c. The painter that draws a diamond shape by connecting the midpoints of the sides of the frame. d. The wave painter. Transforming and combining painters An operation on painters (such as flip-vert or beside) works by creating a painter that invokes the original painters with respect to frames derived from the argument frame. Thus, for example, flip-vert doesn’t have to know how a painter works in order to flip it—it just has to know how to turn a frame upside down: The flipped painter just uses the original painter, but in the inverted frame. Painter operations are based on the procedure transform-painter, which takes as arguments a painter and information on how to transform a frame and produces a new painter. The transformed painter, when called on a frame, transforms the frame and calls the original painter on the transformed frame. The arguments to transform-painter are points (represented as vectors) that specify the corners of the new frame: When mapped into the frame, the first point specifies the new frame’s origin and the other two specify the ends of its edge vectors. Thus, arguments within the unit square specify a frame contained within the original frame. 224 (define (transform-painter painter origin corner1 corner2) (lambda (frame) (let ((m (frame-coord-map frame))) (let ((new-origin (m origin))) (painter (make-frame new-origin (sub-vect (m corner1) new-origin) (sub-vect (m corner2) new-origin))))))) Here’s how to flip painter images vertically: (define (flip-vert painter) (transform-painter painter ; new origin (make-vect 1.0 1.0) ; new end of edge1 (make-vect 0.0 0.0))) ; new end of edge2 (make-vect 0.0 1.0) Using transform-painter, we can easily define new transformations. For example, we can define a painter that shrinks its image to the upperright quarter of the frame it is given: (define (shrink-to-upper-right painter) (transform-painter painter (make-vect 0.5 0.5) (make-vect 1.0 0.5) (make-vect 0.5 1.0))) Other transformations rotate images counterclockwise by 90 degrees29 (define (rotate90 painter) 29 Rotate90 is a pure rotation only for square frames, because it also stretches and shrinks the image to fit into the rotated frame. 225 (transform-painter painter (make-vect 1.0 0.0) (make-vect 1.0 1.0) (make-vect 0.0 0.0))) or squash images towards the center of the frame:30 (define (squash-inwards painter) (transform-painter painter (make-vect 0.0 0.0) (make-vect 0.65 0.35) (make-vect 0.35 0.65))) Frame transformation is also the key to defining means of combining two or more painters. The beside procedure, for example, takes two painters, transforms them to paint in the left and right halves of an argument frame respectively, and produces a new, compound painter. When the compound painter is given a frame, it calls the first transformed painter to paint in the left half of the frame and calls the second transformed painter to paint in the right half of the frame: (define (beside painter1 painter2) (let ((split-point (make-vect 0.5 0.0))) (let ((paint-left (transform-painter painter1 (make-vect 0.0 0.0) split-point (make-vect 0.0 1.0))) 30 The diamond-shaped images in Figure 2.10 and Figure 2.11 were created with squash-inwards applied to wave and rogers. 226 (paint-right (transform-painter painter2 split-point (make-vect 1.0 0.0) (make-vect 0.5 1.0)))) (lambda (frame) (paint-left frame) (paint-right frame))))) Observe how the painter data abstraction, and in particular the representation of painters as procedures, makes beside easy to implement. The beside procedure need not know anything about the details of the component painters other than that each painter will draw something in its designated frame. Exercise 2.50: Define the transformation flip-horiz, which flips painters horizontally, and transformations that rotate painters counterclockwise by 180 degrees and 270 degrees. Exercise 2.51: Define the below operation for painters. Below takes two painters as arguments. The resulting painter, given a frame, draws with the first painter in the bottom of the frame and with the second painter in the top. Define below in two different ways—first by writing a procedure that is analogous to the beside procedure given above, and again in terms of beside and suitable rotation operations (from Exercise 2.50). Levels of language for robust design The picture language exercises some of the critical ideas we’ve introduced about abstraction with procedures and data. The fundamental data ab- 227 stractions, painters, are implemented using procedural representations, which enables the language to handle different basic drawing capabilities in a uniform way. The means of combination satisfy the closure property, which permits us to easily build up complex designs. Finally, all the tools for abstracting procedures are available to us for abstracting means of combination for painters. We have also obtained a glimpse of another crucial idea about languages and program design. This is the approach of stratified design, the notion that a complex system should be structured as a sequence of levels that are described using a sequence of languages. Each level is constructed by combining parts that are regarded as primitive at that level, and the parts constructed at each level are used as primitives at the next level. The language used at each level of a stratified design has primitives, means of combination, and means of abstraction appropriate to that level of detail. Stratified design pervades the engineering of complex systems. For example, in computer engineering, resistors and transistors are combined (and described using a language of analog circuits) to produce parts such as and-gates and or-gates, which form the primitives of a language for digitalcircuit design.31 These parts are combined to build processors, bus structures, and memory systems, which are in turn combined to form computers, using languages appropriate to computer architecture. Computers are combined to form distributed systems, using languages appropriate for describing network interconnections, and so on. As a tiny example of stratification, our picture language uses primitive elements (primitive painters) that are created using a language that 31 Section 3.3.4 describes one such language. 228 specifies points and lines to provide the lists of line segments for segments->painter, or the shading details for a painter like rogers. The bulk of our description of the picture language focused on combining these primitives, using geometric combiners such as beside and below. We also worked at a higher level, regarding beside and below as primitives to be manipulated in a language whose operations, such as square-of-four, capture common patterns of combining geometric combiners. Stratified design helps make programs robust, that is, it makes it likely that small changes in a specification will require correspondingly small changes in the program. For instance, suppose we wanted to change the image based on wave shown in Figure 2.9. We could work at the lowest level to change the detailed appearance of the wave element; we could work at the middle level to change the way corner-split replicates the wave; we could work at the highest level to change how square-limit arranges the four copies of the corner. In general, each level of a stratified design provides a different vocabulary for expressing the characteristics of the system, and a different kind of ability to change it. Exercise 2.52: Make changes to the square limit of wave shown in Figure 2.9 by working at each of the levels described above. In particular: a. Add some segments to the primitive wave painter of Exercise 2.49 (to add a smile, for example). b. Change the pattern constructed by corner-split (for example, by using only one copy of the up-split and right-split images instead of two). c. Modify the version of square-limit that uses square-of-four 229 so as to assemble the corners in a different pattern. (For example, you might make the big Mr. Rogers look outward from each corner of the square.) 2.3 Symbolic Data All the compound data objects we have used so far were constructed ultimately from numbers. In this section we extend the representational capability of our language by introducing the ability to work with arbitrary symbols as data. 2.3.1 Quotation If we can form compound data using symbols, we can have lists such as (a b c d) (23 45 17) ((Norah 12) (Molly 9) (Anna 7) (Lauren 6) (Charlotte 4)) Lists containing symbols can look just like the expressions of our language: (* (+ 23 45) (+ x 9)) (define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))) In order to manipulate symbols we need a new element in our language: the ability to quote a data object. Suppose we want to construct the list (a b). We can’t accomplish this with (list a b), because this expression constructs a list of the values of a and b rather than the symbols themselves. This issue is well known in the context of natural languages, where words and sentences may be regarded either as semantic entities or as character strings (syntactic entities). The common practice in natural languages is to 230 use quotation marks to indicate that a word or a sentence is to be treated literally as a string of characters. For instance, the first letter of “John” is clearly “J.” If we tell somebody “say your name aloud,” we expect to hear that person’s name. However, if we tell somebody “say ‘your name’ aloud,” we expect to hear the words “your name.” Note that we are forced to nest quotation marks to describe what somebody else might say.32 We can follow this same practice to identify lists and symbols that are to be treated as data objects rather than as expressions to be evaluated. However, our format for quoting differs from that of natural languages in that we place a quotation mark (traditionally, the single quote symbol ’) only at the beginning of the object to be quoted. We can get away with this in Scheme syntax because we rely on blanks and parentheses to delimit objects. Thus, the meaning of the single quote character is to quote the next object.33 32 Allowing quotation in a language wreaks havoc with the ability to reason about the language in simple terms, because it destroys the notion that equals can be substituted for equals. For example, three is one plus two, but the word “three” is not the phrase “one plus two.” Quotation is powerful because it gives us a way to build expressions that manipulate other expressions (as we will see when we write an interpreter in Chapter 4). But allowing statements in a language that talk about other statements in that language makes it very difficult to maintain any coherent principle of what “equals can be substituted for equals” should mean. For example, if we know that the evening star is the morning star, then from the statement “the evening star is Venus” we can deduce “the morning star is Venus.” However, given that “John knows that the evening star is Venus” we cannot infer that “John knows that the morning star is Venus.” 33 The single quote is different from the double quote we have been using to enclose character strings to be printed. Whereas the single quote can be used to denote lists 231 Now we can distinguish between symbols and their values: (define a 1) (define b 2) (list a b) (1 2) (list ’a ’b) (a b) (list ’a b) (a 2) Quotation also allows us to type in compound objects, using the conventional printed representation for lists:34 or symbols, the double quote is used only with character strings. In this book, the only use for character strings is as items to be printed. 34 Strictly, our use of the quotation mark violates the general rule that all compound expressions in our language should be delimited by parentheses and look like lists. We can recover this consistency by introducing a special form quote, which serves the same purpose as the quotation mark. Thus, we would type (quote a) instead of ’a, and we would type (quote (a b c)) instead of ’(a b c). This is precisely how the interpreter works. The quotation mark is just a single-character abbreviation for wrapping the next complete expression with quote to form (quote 〈 expression 〉). This is important because it maintains the principle that any expression seen by the interpreter can be manipulated as a data object. For instance, we could construct the expression (car ’(a b c)), which is the same as (car (quote (a b c))), by evaluating (list ’car (list ’quote ’(a b c))). 232 (car ’(a b c)) a (cdr ’(a b c)) (b c) In keeping with this, we can obtain the empty list by evaluating ’(), and thus dispense with the variable nil. One additional primitive used in manipulating symbols is eq?, which takes two symbols as arguments and tests whether they are the same.35 Using eq?, we can implement a useful procedure called memq. This takes two arguments, a symbol and a list. If the symbol is not contained in the list (i.e., is not eq? to any item in the list), then memq returns false. Otherwise, it returns the sublist of the list beginning with the first occurrence of the symbol: (define (memq item x) (cond ((null? x) false) ((eq? item (car x)) x) (else (memq item (cdr x))))) For example, the value of (memq ’apple ’(pear banana prune)) is false, whereas the value of (memq ’apple ’(x (apple sauce) y apple pear)) is (apple pear). 35 We can consider two symbols to be “the same” if they consist of the same characters in the same order. Such a definition skirts a deep issue that we are not yet ready to address: the meaning of “sameness” in a programming language. We will return to this in Chapter 3 (Section 233 Exercise 2.53: What would the interpreter print in response to evaluating each of the following expressions? (list ’a ’b ’c) (list (list ’george)) (cdr ’((x1 x2) (y1 y2))) (cadr ’((x1 x2) (y1 y2))) (pair? (car ’(a short list))) (memq ’red ’((red shoes) (blue socks))) (memq ’red ’(red shoes blue socks)) Exercise 2.54: Two lists are said to be equal? if they contain equal elements arranged in the same order. For example, (equal? ’(this is a list) ’(this is a list)) is true, but (equal? ’(this is a list) ’(this (is a) list)) is false. To be more precise, we can define equal? recursively in terms of the basic eq? equality of symbols by saying that a and b are equal? if they are both symbols and the symbols are eq?, or if they are both lists such that (car a) is equal? to (car b) and (cdr a) is equal? to (cdr b). Using this idea, implement equal? as a procedure.36 36 In practice, programmers use equal? to compare lists that contain numbers as well as symbols. Numbers are not considered to be symbols. The question of whether two numerically equal numbers (as tested by =) are also eq? is highly implementation-dependent. A better definition of equal? (such as the one that comes as a primitive in Scheme) would also stipulate that if a and b are both numbers, then a and b are equal? if they are numerically equal. 234 Exercise 2.55: Eva Lu Ator types to the interpreter the expression (car ”abracadabra) To her surprise, the interpreter prints back quote. Explain. 2.3.2 Example: Symbolic Differentiation As an illustration of symbol manipulation and a further illustration of data abstraction, consider the design of a procedure that performs symbolic differentiation of algebraic expressions. We would like the procedure to take as arguments an algebraic expression and a variable and to return the derivative of the expression with respect to the variable. For example, if the arguments to the procedure are ax 2 + bx + c and x , the procedure should return 2ax + b . Symbolic differentiation is of special historical significance in Lisp. It was one of the motivating examples behind the development of a computer language for symbol manipulation. Furthermore, it marked the beginning of the line of research that led to the development of powerful systems for symbolic mathematical work, which are currently being used by a growing number of applied mathematicians and physicists. In developing the symbolic-differentiation program, we will follow the same strategy of data abstraction that we followed in developing the rational-number system of Section 2.1.1. That is, we will first define a differentiation algorithm that operates on abstract objects such as “sums,” “products,” and “variables” without worrying about how these are to be represented. Only afterward will we address the representation problem. The differentiation program with abstract data In order to keep things simple, we will consider a very simple symbolicdifferentiation program that handles expressions that are built up using only the operations of addition and multiplication with two arguments. 235 Differentiation of any such expression can be carried out by applying the following reduction rules: dc = 0, for c a constant or a variable different from x dx dx =1 dx d (u + v) du dv = + dx dx dx dv du d (uv) =u +v dx dx dx Observe that the latter two rules are recursive in nature. That is, to obtain the derivative of a sum we first find the derivatives of the terms and add them. Each of the terms may in turn be an expression that needs to be decomposed. Decomposing into smaller and smaller pieces will eventually produce pieces that are either constants or variables, whose derivatives will be either 0 or 1. To embody these rules in a procedure we indulge in a little wishful thinking, as we did in designing the rational-number implementation. If we had a means for representing algebraic expressions, we should be able to tell whether an expression is a sum, a product, a constant, or a variable. We should be able to extract the parts of an expression. For a sum, for example we want to be able to extract the addend (first term) and the augend (second term). We should also be able to construct expressions from parts. Let us assume that we already have procedures to implement the following selectors, constructors, and predicates: (variable? e) Is e a variable? (same-variable? v1 v2) Are v1 and v2 the same variable? (sum? e) Is e a sum? 236 Addend of the sum e. Augend of the sum e. (addend e) (augend e) Construct the sum of a1 and a2. Is e a product? Multiplier of the product e. (make-sum a1 a2) (product? e) (multiplier e) (multiplicand e) (make-product m1 m2) Multiplicand of the product e. Construct the product of m1 and m2. Using these, and the primitive predicate number?, which identifies numbers, we can express the differentiation rules as the following procedure: (define (deriv exp var) (cond ((number? exp) 0) ((variable? exp) (if (same-variable? exp var) 1 0)) ((sum? exp) (make-sum (deriv (addend exp) var) (deriv (augend exp) var))) ((product? exp) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (deriv (multiplier exp) var) (multiplicand exp)))) (else (error "unknown expression type - DERIV" exp)))) This deriv procedure incorporates the complete differentiation algorithm. Since it is expressed in terms of abstract data, it will work no matter how we choose to represent algebraic expressions, as long as we 237 design a proper set of selectors and constructors. This is the issue we must address next. Representing algebraic expressions We can imagine many ways to use list structure to represent algebraic expressions. For example, we could use lists of symbols that mirror the usual algebraic notation, representing ax + b as the list (a * x + b) . However, one especially straightforward choice is to use the same parenthesized prefix notation that Lisp uses for combinations; that is, to represent ax + b as (+ (* a x) b). Then our data representation for the differentiation problem is as follows: • The variables are symbols. They are identified by the primitive predicate symbol?: (define (variable? x) (symbol? x)) • Two variables are the same if the symbols representing them are eq?: (define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2))) • Sums and products are constructed as lists: (define (make-sum a1 a2) (list ’+ a1 a2)) (define (make-product m1 m2) (list ’* m1 m2)) • A sum is a list whose first element is the symbol +: (define (sum? x) (and (pair? x) (eq? (car x) ’+))) • The addend is the second item of the sum list: (define (addend s) (cadr s)) • The augend is the third item of the sum list: 238 (define (augend s) (caddr s)) • A product is a list whose first element is the symbol *: (define (product? x) (and (pair? x) (eq? (car x) ’*))) • The multiplier is the second item of the product list: (define (multiplier p) (cadr p)) • The multiplicand is the third item of the product list: (define (multiplicand p) (caddr p)) Thus, we need only combine these with the algorithm as embodied by deriv in order to have a working symbolic-differentiation program. Let us look at some examples of its behavior: (deriv ’(+ x 3) ’x) (+ 1 0) (deriv ’(* x y) ’x) (+ (* x 0) (* 1 y)) (deriv ’(* (* x y) (+ x 3)) ’x) (+ (* (* x y) (+ 1 0)) (* (+ (* x 0) (* 1 y)) (+ x 3))) The program produces answers that are correct; however, they are unsimplified. It is true that d (xy) = x ·0+1·y dx but we would like the program to know that x · 0 = 0, 1 · y = y , and 0 + y = y . The answer for the second example should have been simply y. As the 239 third example shows, this becomes a serious issue when the expressions are complex. Our difficulty is much like the one we encountered with the rationalnumber implementation: we haven’t reduced answers to simplest form. To accomplish the rational-number reduction, we needed to change only the constructors and the selectors of the implementation. We can adopt a similar strategy here. We won’t change deriv at all. Instead, we will change make-sum so that if both summands are numbers, make-sum will add them and return their sum. Also, if one of the summands is 0, then make-sum will return the other summand. (define (make-sum a1 a2) (cond ((=number? a1 0) a2) ((=number? a2 0) a1) ((and (number? a1) (number? a2)) (+ a1 a2)) (else (list ’+ a1 a2)))) This uses the procedure =number?, which checks whether an expression is equal to a given number: (define (=number? exp num) (and (number? exp) (= exp num))) Similarly, we will change make-product to build in the rules that 0 times anything is 0 and 1 times anything is the thing itself: (define (make-product m1 m2) (cond ((or (=number? m1 0) (=number? m2 0)) 0) ((=number? m1 1) m2) ((=number? m2 1) m1) ((and (number? m1) (number? m2)) (* m1 m2)) (else (list ’* m1 m2)))) 240 Here is how this version works on our three examples: (deriv ’(+ x 3) ’x) 1 (deriv ’(* x y) ’x) y (deriv ’(* (* x y) (+ x 3)) ’x) (+ (* x y) (* y (+ x 3))) Although this is quite an improvement, the third example shows that there is still a long way to go before we get a program that puts expressions into a form that we might agree is “simplest.” The problem of algebraic simplification is complex because, among other reasons, a form that may be simplest for one purpose may not be for another. Exercise 2.56: Show how to extend the basic differentiator to handle more kinds of expressions. For instance, implement the differentiation rule du d (un ) = nu n−1 dx dx by adding a new clause to the deriv program and defining appropriate procedures exponentiation?, base, exponent, and make-exponentiation. (You may use the symbol ** to denote exponentiation.) Build in the rules that anything raised to the power 0 is 1 and anything raised to the power 1 is the thing itself. Exercise 2.57: Extend the differentiation program to handle sums and products of arbitrary numbers of (two or more) terms. Then the last example above could be expressed as 241 (deriv ’(* x y (+ x 3)) ’x) Try to do this by changing only the representation for sums and products, without changing the deriv procedure at all. For example, the addend of a sum would be the first term, and the augend would be the sum of the rest of the terms. Exercise 2.58: Suppose we want to modify the differentiation program so that it works with ordinary mathematical notation, in which + and * are infix rather than prefix operators. Since the differentiation program is defined in terms of abstract data, we can modify it to work with different representations of expressions solely by changing the predicates, selectors, and constructors that define the representation of the algebraic expressions on which the differentiator is to operate. a. Show how to do this in order to differentiate algebraic expressions presented in infix form, such as (x + (3 * (x + (y + 2)))). To simplify the task, assume that + and * always take two arguments and that expressions are fully parenthesized. b. The problem becomes substantially harder if we allow standard algebraic notation, such as (x + 3 * (x + y + 2)), which drops unnecessary parentheses and assumes that multiplication is done before addition. Can you design appropriate predicates, selectors, and constructors for this notation such that our derivative program still works? 2.3.3 Example: Representing Sets In the previous examples we built representations for two kinds of compound data objects: rational numbers and algebraic expressions. In one of 242 these examples we had the choice of simplifying (reducing) the expressions at either construction time or selection time, but other than that the choice of a representation for these structures in terms of lists was straightforward. When we turn to the representation of sets, the choice of a representation is not so obvious. Indeed, there are a number of possible representations, and they differ significantly from one another in several ways. Informally, a set is simply a collection of distinct objects. To give a more precise definition we can employ the method of data abstraction. That is, we define “set” by specifying the operations that are to be used on sets. These are union-set, intersection-set, element-of-set?, and adjoin-set. Element-of-set? is a predicate that determines whether a given element is a member of a set. Adjoin-set takes an object and a set as arguments and returns a set that contains the elements of the original set and also the adjoined element. Union-set computes the union of two sets, which is the set containing each element that appears in either argument. Intersection-set computes the intersection of two sets, which is the set containing only elements that appear in both arguments. From the viewpoint of data abstraction, we are free to design any representation that implements these operations in a way consistent with the interpretations given above.37 37 If we want to be more formal, we can specify “consistent with the interpretations given above” to mean that the operations satisfy a collection of rules such as these: • For any set S and any object x, (element-of-set? x (adjoin-set x S)) is true (informally: “Adjoining an object to a set produces a set that contains the object”). • For any sets S and T and any object x, (element-of-set? x (union-set S T)) is equal to (or (element-of-set? x S) (element-of-set? x T)) (informally: “The elements of (union S T) are the elements that are in S or in T”). Sets as unordered lists One way to represent a set is as a list of its elements in which no element appears more than once. The empty set is represented by the empty list. In this representation, element-of-set? is similar to the procedure memq of Section 2.3.1. It uses equal? instead of eq? so that the set elements need not be symbols: (define (element-of-set? x set) (cond ((null? set) false) ((equal? x (car set)) true) (else (element-of-set? x (cdr set))))) Using this, we can write adjoin-set. If the object to be adjoined is already in the set, we just return the set. Otherwise, we use cons to add the object to the list that represents the set: (define (adjoin-set x set) (if (element-of-set? x set) set (cons x set))) For intersection-set we can use a recursive strategy. If we know how to form the intersection of set2 and the cdr of set1, we only need to decide whether to include the car of set1 in this. But this depends on whether (car set1) is also in set2. Here is the resulting procedure: (define (intersection-set set1 set2) (cond ((or (null? set1) (null? set2)) ’()) ((element-of-set? (car set1) set2) • For any object x, (element-of-set? x ’()) is false (informally: “No object is an element of the empty set”). 244 (cons (car set1) (intersection-set (cdr set1) set2))) (else (intersection-set (cdr set1) set2)))) In designing a representation, one of the issues we should be concerned with is efficiency. Consider the number of steps required by our set operations. Since they all use element-of-set?, the speed of this operation has a major impact on the efficiency of the set implementation as a whole. Now, in order to check whether an object is a member of a set, element-of-set? may have to scan the entire set. (In the worst case, the object turns out not to be in the set.) Hence, if the set has n elements, element-of-set? might take up to n steps. Thus, the number of steps required grows as Θ(n ). The number of steps required by adjoin-set, which uses this operation, also grows as Θ(n ). For intersection-set, which does an element-of-set? check for each element of set1, the number of steps required grows as the product of the sizes of the sets involved, or Θ(n 2 ) for two sets of size n . The same will be true of union-set. Exercise 2.59: Implement the union-set operation for the unordered-list representation of sets. Exercise 2.60: We specified that a set would be represented as a list with no duplicates. Now suppose we allow duplicates. For instance, the set {1, 2, 3} could be represented as the list (2 3 2 1 3 2 2). Design procedures element-of-set?, adjoin-set, union-set, and intersection-set that operate on this repre- sentation. How does the efficiency of each compare with the corresponding procedure for the non-duplicate representation? Are there applications for which you would use this representation in preference to the non-duplicate one? Sets as ordered lists One way to speed up our set operations is to change the representation so that the set elements are listed in increasing order. To do this, we need some way to compare two objects so that we can say which is bigger. For example, we could compare symbols lexicographically, or we could agree on some method for assigning a unique number to an object and then compare the elements by comparing the corresponding numbers. To keep our discussion simple, we will consider only the case where the set elements are numbers, so that we can compare elements using > and <. We will represent a set of numbers by listing its elements in increasing order. Whereas our first representation above allowed us to represent the set {1, 3, 6, 10} by listing the elements in any order, our new representation allows only the list (1 3 6 10). One advantage of ordering shows up in element-of-set?: In checking for the presence of an item, we no longer have to scan the entire set. If we reach a set element that is larger than the item we are looking for, then we know that the item is not in the set: (define (element-of-set? x set) (cond ((null? set) false) ((= x (car set)) true) ((< x (car set)) false) (else (element-of-set? x (cdr set))))) How many steps does this save? In the worst case, the item we are looking for may be the largest one in the set, so the number of steps is the same as for the unordered representation. On the other hand, if we search for items of many different sizes we can expect that sometimes we will be able to stop searching at a point near the beginning of the list and that other times we 246 will still need to examine most of the list. On the average we should expect to have to examine about half of the items in the set. Thus, the average number of steps required will be about n /2. This is still Θ(n ) growth, but it does save us, on the average, a factor of 2 in number of steps over the previous implementation. We obtain a more impressive speedup with intersection-set. In the unordered representation this operation required Θ(n 2 ) steps, because we performed a complete scan of set2 for each element of set1. But with the ordered representation, we can use a more clever method. Begin by comparing the initial elements, x1 and x2, of the two sets. If x1 equals x2, then that gives an element of the intersection, and the rest of the intersection is the intersection of the cdr-s of the two sets. Suppose, however, that x1 is less than x2. Since x2 is the smallest element in set2, we can immediately conclude that x1 cannot appear anywhere in set2 and hence is not in the intersection. Hence, the intersection is equal to the intersection of set2 with the cdr of set1. Similarly, if x2 is less than x1, then the intersec- tion is given by the intersection of set1 with the cdr of set2. Here is the procedure: (define (intersection-set set1 set2) (if (or (null? set1) (null? set2)) ’() (let ((x1 (car set1)) (x2 (car set2))) (cond ((= x1 x2) (cons x1 (intersection-set (cdr set1) (cdr set2)))) ((< x1 x2) 247 (intersection-set (cdr set1) set2)) ((< x2 x1) (intersection-set set1 (cdr set2))))))) To estimate the number of steps required by this process, observe that at each step we reduce the intersection problem to computing intersections of smaller sets—removing the first element from set1 or set2 or both. Thus, the number of steps required is at most the sum of the sizes of set1 and set2, rather than the product of the sizes as with the unordered representation. This is Θ(n ) growth rather than Θ(n 2 )—a considerable speedup, even for sets of moderate size. Exercise 2.61: Give an implementation of adjoin-set using the ordered representation. By analogy with element-of-set? show how to take advantage of the ordering to produce a procedure that requires on the average about half as many steps as with the unordered representation. Exercise 2.62: Give a Θ(n ) implementation of union-set for sets represented as ordered lists. Sets as binary trees We can do better than the ordered-list representation by arranging the set elements in the form of a tree. Each node of the tree holds one element of the set, called the “entry” at that node, and a link to each of two other (possibly empty) nodes. The “left” link points to elements smaller than the one at the node, and the “right” link to elements greater than the one at the node. Figure 2.16 shows some trees that represent the set {1, 3, 5, 7, 9, 11}. The same set may be represented by a tree in a number of different ways. The only thing we require for a valid representation is that all elements in 248 the left subtree be smaller than the node entry and that all elements in the right subtree be larger. Figure 2.16: Various binary trees that represent the set {1, 3, 5, 7, 9, 11}. The advantage of the tree representation is this: Suppose we want to check whether a number x is contained in a set. We begin by comparing x with the entry in the top node. If x is less than this, we know that we need only search the left subtree; if x is greater, we need only search the right subtree. Now, if the tree is “balanced,” each of these subtrees will be about half the size of the original. Thus, in one step we have reduced the problem of searching a tree of size n to searching a tree of size n /2. Since the size of the tree is halved at each step, we should expect that the number of steps needed to search a tree of size n grows as Θ(log n ).38 For large sets, this will be a significant speedup over the previous representations. 38 Halving the size of the problem at each step is the distinguishing characteristic of logarithmic growth, as we saw with the fast-exponentiation algorithm of Section 1.2.4 and the half-interval search method of Section 1.3.3. 249 We can represent trees by using lists. Each node will be a list of three items: the entry at the node, the left subtree, and the right subtree. A left or a right subtree of the empty list will indicate that there is no subtree connected there. We can describe this representation by the following procedures:39 (define (entry tree) (car tree)) (define (left-branch tree) (cadr tree)) (define (right-branch tree) (caddr tree)) (define (make-tree entry left right) (list entry left right)) Now we can write the element-of-set? procedure using the strategy described above: (define (element-of-set? x set) (cond ((null? set) false) ((= x (entry set)) true) ((< x (entry set)) (element-of-set? x (left-branch set))) ((> x (entry set)) (element-of-set? x (right-branch set))))) 39 We are representing sets in terms of trees, and trees in terms of lists—in effect, a data abstraction built upon a data abstraction. We can regard the procedures entry, left-branch, right-branch, and make-tree as a way of isolating the ab- straction of a “binary tree” from the particular way we might wish to represent such a tree in terms of list structure. 250 Adjoining an item to a set is implemented similarly and also requires Θ(log n ) steps. To adjoin an item x, we compare x with the node entry to determine whether x should be added to the right or to the left branch, and having adjoined x to the appropriate branch we piece this newly constructed branch together with the original entry and the other branch. If x is equal to the entry, we just return the node. If we are asked to adjoin x to an empty tree, we generate a tree that has x as the entry and empty right and left branches. Here is the procedure: (define (adjoin-set x set) (cond ((null? set) (make-tree x ’() ’())) ((= x (entry set)) set) ((< x (entry set)) (make-tree (entry set) (adjoin-set x (left-branch set)) (right-branch set))) ((> x (entry set)) (make-tree (entry set) (left-branch set) (adjoin-set x (right-branch set)))))) The above claim that searching the tree can be performed in a logarithmic number of steps rests on the assumption that the tree is “balanced,” i.e., that the left and the right subtree of every tree have approximately the same number of elements, so that each subtree contains about half the elements of its parent. But how can we be certain that the trees we construct will be balanced? Even if we start with a balanced tree, adding elements with adjoin-set may produce an unbalanced result. Since the position of a newly adjoined element depends on how the element compares with 251 the items already in the set, we can expect that if we add elements “randomly” the tree will tend to be balanced on the average. But this is not a guarantee. For example, if we start with an empty set and adjoin the numbers 1 through 7 in sequence we end up with the highly unbalanced tree shown in Figure 2.17. In this tree all the left subtrees are empty, so it has no advantage over a simple ordered list. One way to solve this problem is to define an operation that transforms an arbitrary tree into a balanced tree with the same elements. Then we can perform this transformation after every few adjoin-set operations to keep our set in balance. There are also other ways to solve this problem, most of which involve designing new data structures for which searching and insertion both can be done in Θ(log n ) steps.40 Figure 2.17: Unbalanced tree produced by adjoining 1 through 7 in sequence. 40 Examples of such structures include B-trees and red-black trees. There is a large literature on data structures devoted to this problem. See Cormen et al. 1990. 252 Exercise 2.63: Each of the following two procedures converts a binary tree to a list. (define (tree->list-1 tree) (if (null? tree) ’() (append (tree->list-1 (left-branch tree)) (cons (entry tree) (tree->list-1 (right-branch tree)))))) (define (tree->list-2 tree) (define (copy-to-list tree result-list) (if (null? tree) result-list (copy-to-list (left-branch tree) (cons (entry tree) (copy-to-list (right-branch tree) result-list))))) (copy-to-list tree ’())) a. Do the two procedures produce the same result for every tree? If not, how do the results differ? What lists do the two procedures produce for the trees in Figure 2.16? b. Do the two procedures have the same order of growth in the number of steps required to convert a balanced tree with n elements to a list? If not, which one grows more slowly? Exercise 2.64: The following procedure list-> tree converts an ordered list to a balanced binary tree. The helper procedure partial-tree takes as arguments an integer n and list of at least n elements and constructs a balanced tree containing the first n 253 elements of the list. The result returned by partial-tree is a pair (formed with cons) whose car is the constructed tree and whose cdr is the list of elements not included in the tree. (define (list->tree elements) (car (partial-tree elements (length elements)))) (define (partial-tree elts n) (if (= n 0) (cons ’() elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) (let ((this-entry (car non-left-elts)) (right-result (partial-tree (cdr non-left-elts) right-size))) (let ((right-tree (car right-result)) (remaining-elts (cdr right-result))) (cons (make-tree this-entry left-tree right-tree) remaining-elts)))))))) a. Write a short paragraph explaining as clearly as you can how partial-tree works. Draw the tree produced by list->tree for the list (1 3 5 7 9 11). b. What is the order of growth in the number of steps required by list->tree to convert a list of n elements? 254 Exercise 2.65: Use the results of Exercise 2.63 and Exercise 2.64 to give Θ(n ) implementations of union-set and intersection-set for sets implemented as (balanced) binary trees.41 Sets and information retrieval We have examined options for using lists to represent sets and have seen how the choice of representation for a data object can have a large impact on the performance of the programs that use the data. Another reason for concentrating on sets is that the techniques discussed here appear again and again in applications involving information retrieval. Consider a data base containing a large number of individual records, such as the personnel files for a company or the transactions in an accounting system. A typical data-management system spends a large amount of time accessing or modifying the data in the records and therefore requires an efficient method for accessing records. This is done by identifying a part of each record to serve as an identifying key. A key can be anything that uniquely identifies the record. For a personnel file, it might be an employee’s ID number. For an accounting system, it might be a transaction number. Whatever the key is, when we define the record as a data structure we should include a key selector procedure that retrieves the key associated with a given record. Now we represent the data base as a set of records. To locate the record with a given key we use a procedure lookup, which takes as arguments a key and a data base and which returns the record that has that key, or false if there is no such record. Lookup is implemented in almost the same way 41 Exercise 2.63 through Exercise 2.65 are due to Paul Hilfinger. 255 as element-of-set?. For example, if the set of records is implemented as an unordered list, we could use (define (lookup given-key set-of-records) (cond ((null? set-of-records) false) ((equal? given-key (key (car set-of-records))) (car set-of-records)) (else (lookup given-key (cdr set-of-records))))) Of course, there are better ways to represent large sets than as unordered lists. Information-retrieval systems in which records have to be “randomly accessed” are typically implemented by a tree-based method, such as the binary-tree representation discussed previously. In designing such a system the methodology of data abstraction can be a great help. The designer can create an initial implementation using a simple, straightforward representation such as unordered lists. This will be unsuitable for the eventual system, but it can be useful in providing a “quick and dirty” data base with which to test the rest of the system. Later on, the data representation can be modified to be more sophisticated. If the data base is accessed in terms of abstract selectors and constructors, this change in representation will not require any changes to the rest of the system. Exercise 2.66: Implement the lookup procedure for the case where the set of records is structured as a binary tree, ordered by the numerical values of the keys. 2.3.4 Example: Huffman Encoding Trees This section provides practice in the use of list structure and data abstraction to manipulate sets and trees. The application is to methods for representing data as sequences of ones and zeros (bits). For example, the ASCII 256 standard code used to represent text in computers encodes each character as a sequence of seven bits. Using seven bits allows us to distinguish 27 , or 128, possible different characters. In general, if we want to distinguish n different symbols, we will need to use log2 n bits per symbol. If all our messages are made up of the eight symbols A, B, C, D, E, F, G, and H, we can choose a code with three bits per character, for example A 000 C 010 E 100 G 110 B 001 D 011 F 101 H 111 With this code, the message BACADAEAFABBAAAGAH is encoded as the string of 54 bits 001000010000011000100000101000001001000000000110000111 Codes such as ASCII and the A-through-H code above are known as fixedlength codes, because they represent each symbol in the message with the same number of bits. It is sometimes advantageous to use variable-length codes, in which different symbols may be represented by different numbers of bits. For example, Morse code does not use the same number of dots and dashes for each letter of the alphabet. In particular, E, the most frequent letter, is represented by a single dot. In general, if our messages are such that some symbols appear very frequently and some very rarely, we can encode data more efficiently (i.e., using fewer bits per message) if we assign shorter codes to the frequent symbols. Consider the following alternative code for the letters A through H: A 0 C 1010 E 1100 G 1110 B 100 D 1011 F 1101 H 1111 With this code, the same message as above is encoded as the string This string contains 42 bits, so it saves more than 20% in space in comparison with the fixed-length code shown above. One of the difficulties of using a variable-length code is knowing when you have reached the end of a symbol in reading a sequence of zeros and ones. Morse code solves this problem by using a special separator code (in this case, a pause) after the sequence of dots and dashes for each letter. Another solution is to design the code in such a way that no complete code for any symbol is the beginning (or prefix) of the code for another symbol. Such a code is called a prefix code. In the example above, A is encoded by 0 and B is encoded by 100, so no other symbol can have a code that begins with 0 or with 100. In general, we can attain significant savings if we use variable-length prefix codes that take advantage of the relative frequencies of the symbols in the messages to be encoded. One particular scheme for doing this is called the Huffman encoding method, after its discoverer, David Huffman. A Huffman code can be represented as a binary tree whose leaves are the symbols that are encoded. At each non-leaf node of the tree there is a set containing all the symbols in the leaves that lie below the node. In addition, each symbol at a leaf is assigned a weight (which is its relative frequency), and each non-leaf node contains a weight that is the sum of all the weights of the leaves lying below it. The weights are not used in the encoding or the decoding process. We will see below how they are used to help construct the tree. Figure 2.18 shows the Huffman tree for the A-through-H code given above. The weights at the leaves indicate that the tree was designed for 258 messages in which A appears with relative frequency 8, B with relative frequency 3, and the other letters each with relative frequency 1. Given a Huffman tree, we can find the encoding of any symbol by starting at the root and moving down until we reach the leaf that holds the symbol. Each time we move down a left branch we add a 0 to the code, and each time we move down a right branch we add a 1. (We decide which branch to follow by testing to see which branch either is the leaf node for the symbol or contains the symbol in its set.) For example, starting from the root of the tree in Figure 2.18, we arrive at the leaf for D by following a right branch, then a left branch, then a right branch, then a right branch; hence, the code for D is 1011. Figure 2.18: A Huffman encoding tree. To decode a bit sequence using a Huffman tree, we begin at the root and use the successive zeros and ones of the bit sequence to determine whether 259 to move down the left or the right branch. Each time we come to a leaf, we have generated a new symbol in the message, at which point we start over from the root of the tree to find the next symbol. For example, suppose we are given the tree above and the sequence 10001010. Starting at the root, we move down the right branch, (since the first bit of the string is 1), then down the left branch (since the second bit is 0), then down the left branch (since the third bit is also 0). This brings us to the leaf for B, so the first symbol of the decoded message is B. Now we start again at the root, and we make a left move because the next bit in the string is 0. This brings us to the leaf for A. Then we start again at the root with the rest of the string 1010, so we move right, left, right, left and reach C. Thus, the entire message is BAC. Generating Huffman trees Given an “alphabet” of symbols and their relative frequencies, how do we construct the “best” code? (In other words, which tree will encode messages with the fewest bits?) Huffman gave an algorithm for doing this and showed that the resulting code is indeed the best variable-length code for messages where the relative frequency of the symbols matches the frequencies with which the code was constructed. We will not prove this optimality of Huffman codes here, but we will show how Huffman trees are constructed.42 The algorithm for generating a Huffman tree is very simple. The idea is to arrange the tree so that the symbols with the lowest frequency appear farthest away from the root. Begin with the set of leaf nodes, containing symbols and their frequencies, as determined by the initial data from which 42 See Hamming 1980 for a discussion of the mathematical properties of Huffman 260 the code is to be constructed. Now find two leaves with the lowest weights and merge them to produce a node that has these two nodes as its left and right branches. The weight of the new node is the sum of the two weights. Remove the two leaves from the original set and replace them by this new node. Now continue this process. At each step, merge two nodes with the smallest weights, removing them from the set and replacing them with a node that has these two as its left and right branches. The process stops when there is only one node left, which is the root of the entire tree. Here is how the Huffman tree of Figure 2.18 was generated: Initial leaves {(A 8) (B 3) (C 1) (D 1) (E 1) (F 1) (G 1) (H 1)} Merge {(A 8) (B 3) ({C D} 2) (E 1) (F 1) (G 1) (H 1)} {(A 8) (B 3) ({C D} 2) ({E F} 2) (G 1) (H 1)} {(A 8) (B 3) ({C D} 2) ({E F} 2) ({G H} 2)} {(A 8) (B 3) ({C D} 2) ({E F G H} 4)} {(A 8) ({B C D} 5) ({E F G H} 4)} {(A 8) ({B C D E F G H} 9)} Final merge {({A B C D E F G H} 17)} The algorithm does not always specify a unique tree, because there may not be unique smallest-weight nodes at each step. Also, the choice of the order in which the two nodes are merged (i.e., which will be the right branch and which will be the left branch) is arbitrary. Representing Huffman trees In the exercises below we will work with a system that uses Huffman trees to encode and decode messages and generates Huffman trees according to the algorithm outlined above. We will begin by discussing how trees are represented. 261 Leaves of the tree are represented by a list consisting of the symbol leaf, the symbol at the leaf, and the weight: (define (make-leaf symbol weight) (list ’leaf symbol weight)) (define (leaf? object) (eq? (car object) ’leaf)) (define (symbol-leaf x) (cadr x)) (define (weight-leaf x) (caddr x)) A general tree will be a list of a left branch, a right branch, a set of symbols, and a weight. The set of symbols will be simply a list of the symbols, rather than some more sophisticated set representation. When we make a tree by merging two nodes, we obtain the weight of the tree as the sum of the weights of the nodes, and the set of symbols as the union of the sets of symbols for the nodes. Since our symbol sets are represented as lists, we can form the union by using the append procedure we defined in Section 2.2.1: (define (make-code-tree left right) (list left right (append (symbols left) (symbols right)) (+ (weight left) (weight right)))) If we make a tree in this way, we have the following selectors: (define (left-branch tree) (car tree)) 262 (define (right-branch tree) (cadr tree)) (define (symbols tree) (if (leaf? tree) (list (symbol-leaf tree)) (caddr tree))) (define (weight tree) (if (leaf? tree) (weight-leaf tree) (cadddr tree))) The procedures symbols and weight must do something slightly different depending on whether they are called with a leaf or a general tree. These are simple examples of generic procedures (procedures that can handle more than one kind of data), which we will have much more to say about in Section 2.4 and Section 2.5. The decoding procedure The following procedure implements the decoding algorithm. It takes as arguments a list of zeros and ones, together with a Huffman tree. (define (decode bits tree) (define (decode-1 bits current-branch) (if (null? bits) ’() (let ((next-branch (choose-branch (car bits) current-branch))) (if (leaf? next-branch) 263 (cons (symbol-leaf next-branch) (decode-1 (cdr bits) tree)) (decode-1 (cdr bits) next-branch))))) (decode-1 bits tree)) (define (choose-branch bit branch) (cond ((= bit 0) (left-branch branch)) ((= bit 1) (right-branch branch)) (else (error "bad bit - CHOOSE-BRANCH" bit)))) The procedure decode-1 takes two arguments: the list of remaining bits and the current position in the tree. It keeps moving “down” the tree, choosing a left or a right branch according to whether the next bit in the list is a zero or a one. (This is done with the procedure choose-branch.) When it reaches a leaf, it returns the symbol at that leaf as the next symbol in the message by consing it onto the result of decoding the rest of the message, starting at the root of the tree. Note the error check in the final clause of choose-branch, which complains if the procedure finds something other than a zero or a one in the input data. Sets of weighted elements In our representation of trees, each non-leaf node contains a set of symbols, which we have represented as a simple list. However, the tree-generating algorithm discussed above requires that we also work with sets of leaves and trees, successively merging the two smallest items. Since we will be required to repeatedly find the smallest item in a set, it is convenient to use an ordered representation for this kind of set. 264 We will represent a set of leaves and trees as a list of elements, arranged in increasing order of weight. The following adjoin-set procedure for constructing sets is similar to the one described in Exercise 2.61; however, items are compared by their weights, and the element being added to the set is never already in it. (define (adjoin-set x set) (cond ((null? set) (list x)) ((< (weight x) (weight (car set))) (cons x set)) (else (cons (car set) (adjoin-set x (cdr set)))))) The following procedure takes a list of symbol-frequency pairs such as ((A 4) (B 2) (C 1) (D 1)) and constructs an initial ordered set of leaves, ready to be merged according to the Huffman algorithm: (define (make-leaf-set pairs) (if (null? pairs) ’() (let ((pair (car pairs))) (adjoin-set (make-leaf (car pair) (cadr pair)) ; symbol ; frequency (make-leaf-set (cdr pairs)))))) Exercise 2.67: Define an encoding tree and a sample message: (define sample-tree (make-code-tree (make-leaf ’A 4) (make-code-tree (make-leaf ’B 2) (make-code-tree (make-leaf ’D 1) (make-leaf ’C (define sample-message ’(0 1 1 0 0 1 0 1 0 1 1 1 0)) Use the decode procedure to decode the message, and give the result. Exercise 2.68: The encode procedure takes as arguments a message and a tree and produces the list of bits that gives the encoded message. (define (encode message tree) (if (null? message) ’() (append (encode-symbol (car message) tree) (encode (cdr message) tree)))) Encode-symbol is a procedure, which you must write, that the list of bits that encodes a given symbol according to a given tree. You should design encode-symbol so that it signals an error if the symbol is not in the tree at all. Test your procedure by encoding the result you obtained in Exercise 2.67 with the sample tree and seeing whether it is the same as the original sample message. Exercise 2.69: The following procedure takes as its argument a list of symbol-frequency pairs (where no symbol appears in more than one pair) and generates a Huffman encoding tree according to the Huffman algorithm. (define (generate-huffman-tree pairs) (successive-merge (make-leaf-set pairs))) Make-leaf-set is the procedure given above that transforms the list of pairs into an ordered set of leaves. Successive-merge is the 266 procedure you must write, using make-code-tree to successively merge the smallest-weight elements of the set until there is only one element left, which is the desired Huffman tree. (This procedure is slightly tricky, but not really complicated. If you find yourself designing a complex procedure, then you are almost certainly doing something wrong. You can take significant advantage of the fact that we are using an ordered set representation.) Exercise 2.70: The following eight-symbol alphabet with associated relative frequencies was designed to efficiently encode the lyrics of 1950s rock songs. (Note that the “symbols” of an “alphabet” need not be individual letters.) A 2 NA 1 SHA 2 YIP 2 WAH Use generate-huffman-tree (Exercise 2.69) to generate a corresponding Huffman tree, and use encode (Exercise 2.68) to encode the following message: Get a job Sha na na na na na na na na Get a job Sha na na na na na na na na Wah yip yip yip yip yip yip yip yip yip Sha boom 267 How many bits are required for the encoding? What is the smallest number of bits that would be needed to encode this song if we used a fixed-length code for the eight-symbol alphabet? Exercise 2.71: Suppose we have a Huffman tree for an alphabet of n symbols, and that the relative frequencies of the symbols are 1, 2, 4, . . . , 2n−1 . Sketch the tree for n = 5; for n = 10. In such a tree (for general n ) how may bits are required to encode the most frequent symbol? the least frequent symbol? Exercise 2.72: Consider the encoding procedure that you designed in Exercise 2.68. What is the order of growth in the number of steps needed to encode a symbol? Be sure to include the number of steps needed to search the symbol list at each node encountered. To answer this question in general is difficult. Consider the special case where the relative frequencies of the n symbols are as described in Exercise 2.71, and give the order of growth (as a function of n ) of the number of steps needed to encode the most frequent and least frequent symbols in the alphabet. 2.4 Multiple Representations for Abstract Data We have introduced data abstraction, a methodology for structuring systems in such a way that much of a program can be specified independent of the choices involved in implementing the data objects that the program manipulates. For example, we saw in Section 2.1.1 how to separate the task of designing a program that uses rational numbers from the task of implementing rational numbers in terms of the computer language’s primitive mechanisms for constructing compound data. The key idea was to erect an abstraction barrier – in this case, the selectors and constructors 268 for rational numbers (make-rat, numer, denom)—that isolates the way rational numbers are used from their underlying representation in terms of list structure. A similar abstraction barrier isolates the details of the procedures that perform rational arithmetic (add-rat, sub-rat, mul-rat, and div-rat) from the “higher-level” procedures that use rational numbers. The resulting program has the structure shown in Figure 2.1. These data-abstraction barriers are powerful tools for controlling complexity. By isolating the underlying representations of data objects, we can divide the task of designing a large program into smaller tasks that can be performed separately. But this kind of data abstraction is not yet powerful enough, because it may not always make sense to speak of “the underlying representation” for a data object. For one thing, there might be more than one useful representation for a data object, and we might like to design systems that can deal with multiple representations. To take a simple example, complex numbers may be represented in two almost equivalent ways: in rectangular form (real and imaginary parts) and in polar form (magnitude and angle). Sometimes rectangular form is more appropriate and sometimes polar form is more appropriate. Indeed, it is perfectly plausible to imagine a system in which complex numbers are represented in both ways, and in which the procedures for manipulating complex numbers work with either representation. More importantly, programming systems are often designed by many people working over extended periods of time, subject to requirements that change over time. In such an environment, it is simply not possible for everyone to agree in advance on choices of data representation. So in addition to the data-abstraction barriers that isolate representation from use, we need abstraction barriers that isolate different design choices from each 269 other and permit different choices to coexist in a single program. Furthermore, since large programs are often created by combining pre-existing modules that were designed in isolation, we need conventions that permit programmers to incorporate modules into larger systems additively, that is, without having to redesign or reimplement these modules. In this section, we will learn how to cope with data that may be represented in different ways by different parts of a program. This requires constructing generic procedures—procedures that can operate on data that may be represented in more than one way. Our main technique for building generic procedures will be to work in terms of data objects that have type tags, that is, data objects that include explicit information about how they are to be processed. We will also discuss data-directed programming, a powerful and convenient implementation strategy for additively assembling systems with generic operations. We begin with the simple complex-number example. We will see how type tags and data-directed style enable us to design separate rectangular and polar representations for complex numbers while maintaining the notion of an abstract “complex-number” data object. We will accomplish this by defining arithmetic procedures for complex numbers (add-complex, sub-complex, mul-complex, and div-complex) in terms of generic selectors that access parts of a complex number independent of how the number is represented. The resulting complex-number system, as shown in Figure 2.19, contains two different kinds of abstraction barriers. The “horizontal” abstraction barriers play the same role as the ones in Figure 2.1. They isolate “higher-level” operations from “lower-level” representations. In addition, there is a “vertical” barrier that gives us the ability to separately design and install alternative representations. 270 In Section 2.5 we will show how to use type tags and data-directed style to develop a generic arithmetic package. This provides procedures (add, mul, and so on) that can be used to manipulate all sorts of “numbers” and can be easily extended when a new kind of number is needed. In Section 2.5.3, we’ll show how to use generic arithmetic in a system that performs symbolic algebra. Figure 2.19: Data-abstraction barriers in the complex-number system. 2.4.1 Representations for Complex Numbers We will develop a system that performs arithmetic operations on complex numbers as a simple but unrealistic example of a program that uses generic operations. We begin by discussing two plausible representations for complex numbers as ordered pairs: rectangular form (real part and imaginary part) and polar form (magnitude and angle).43 Section 2.4.2 will show how 43 In actual computational systems, rectangular form is preferable to polar form most of the time because of roundoff errors in conversion between rectangular and po- 271 both representations can be made to coexist in a single system through the use of type tags and generic operations. Figure 2.20: Complex numbers as points in the plane. Like rational numbers, complex numbers are naturally represented as ordered pairs. The set of complex numbers can be thought of as a twodimensional space with two orthogonal axes, the “real” axis and the “imaginary” axis. (See Figure 2.20.) From this point of view, the complex number z = x + i y (where i 2 = −1) can be thought of as the point in the plane whose real coordinate is x and whose imaginary coordinate is y . Addition of complex numbers reduces in this representation to addition of coordilar form. This is why the complex-number example is unrealistic. Nevertheless, it provides a clear illustration of the design of a system using generic operations and a good introduction to the more substantial systems to be developed later in this chapter. 272 nates: Real-part(z 1 + z 2 ) = Real-part(z 1 ) + Real-part(z 2 ) Imaginary-part(z 1 + z 2 ) = Imaginary-part(z 1 ) + Imaginary-part(z 2 ) When multiplying complex numbers, it is more natural to think in terms of representing a complex number in polar form, as a magnitude and an angle (r and A in Figure 2.20). The product of two complex numbers is the vector obtained by stretching one complex number by the length of the other and then rotating it through the angle of the other: Magnitude(z 1 · z 2 ) = Magnitude(z 1 ) · Magnitude(z 2 ) Angle(z 1 · z 2 ) = Angle(z 1 ) + Angle(z 2 ) Thus, there are two different representations for complex numbers, which are appropriate for different operations. Yet, from the viewpoint of someone writing a program that uses complex numbers, the principle of data abstraction suggests that all the operations for manipulating complex numbers should be available regardless of which representation is used by the computer. For example, it is often useful to be able to find the magnitude of a complex number that is specified by rectangular coordinates. Similarly, it is often useful to be able to determine the real part of a complex number that is specified by polar coordinates. To design such a system, we can follow the same data-abstraction strategy we followed in designing the rational-number package in Section 2.1.1. Assume that the operations on complex numbers are implemented in terms of four selectors: real-part, imag-part, magnitude, and angle. Also assume that we have two procedures for constructing complex numbers: make-from-real-imag returns a complex number with specified real and imaginary parts, and make-from-mag-ang returns a 273 complex number with specified magnitude and angle. These procedures have the property that, for any complex number z, both (make-from-real-imag (real-part z) (imag-part z)) and (make-from-mag-ang (magnitude z) (angle z)) produce complex numbers that are equal to z. Using these constructors and selectors, we can implement arithmetic on complex numbers using the “abstract data” specified by the constructors and selectors, just as we did for rational numbers in Section 2.1.1. As shown in the formulas above, we can add and subtract complex numbers in terms of real and imaginary parts while multiplying and dividing complex numbers in terms of magnitudes and angles: (define (add-complex z1 z2) (make-from-real-imag (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2)))) (define (sub-complex z1 z2) (make-from-real-imag (- (real-part z1) (real-part z2)) (- (imag-part z1) (imag-part z2)))) (define (mul-complex z1 z2) (make-from-mag-ang (* (magnitude z1) (magnitude z2)) (+ (angle z1) (angle z2)))) (define (div-complex z1 z2) (make-from-mag-ang (/ (magnitude z1) (magnitude z2)) (- (angle z1) (angle z2)))) To complete the complex-number package, we must choose a representation and we must implement the constructors and selectors in terms of 274 primitive numbers and primitive list structure. There are two obvious ways to do this: We can represent a complex number in “rectangular form” as a pair (real part, imaginary part) or in “polar form” as a pair (magnitude, angle). Which shall we choose? In order to make the different choices concrete, imagine that there are two programmers, Ben Bitdiddle and Alyssa P. Hacker, who are independently designing representations for the complex-number system. Ben chooses to represent complex numbers in rectangular form. With this choice, selecting the real and imaginary parts of a complex number is straightforward, as is constructing a complex number with given real and imaginary parts. To find the magnitude and the angle, or to construct a complex number with a given magnitude and angle, he uses the trigonometric relations x = r cos A r = y = r sin A A = arctan( y, x ) x2 + y 2 which relate the real and imaginary parts (x, y ) to the magnitude and the angle (r, A ).44 Ben’s representation is therefore given by the following selectors and constructors: (define (real-part z) (car z)) (define (imag-part z) (cdr z)) (define (magnitude z) 44 The arctangent function referred to here, computed by Scheme’s atan procedure, is defined so as to take two arguments y and x and to return the angle whose tangent is y /x . The signs of the arguments determine the quadrant of the angle. 275 (sqrt (+ (square (real-part z)) (square (imag-part z))))) (define (angle z) (atan (imag-part z) (real-part z))) (define (make-from-real-imag x y) (cons x y)) (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a)))) Alyssa, in contrast, chooses to represent complex numbers in polar form. For her, selecting the magnitude and angle is straightforward, but she has to use the trigonometric relations to obtain the real and imaginary parts. Alyssa’s representation is: (define (real-part z) (* (magnitude z) (cos (angle z)))) (define (imag-part z) (* (magnitude z) (sin (angle z)))) (define (magnitude z) (car z)) (define (angle z) (cdr z)) (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x))) 276 (define (make-from-mag-ang r a) (cons r a)) The discipline of data abstraction ensures that the same implementation of add-complex, sub-complex, mul-complex, and div-complex will work with either Ben’s representation or Alyssa’s 2.4.2 Tagged data One way to view data abstraction is as an application of the “principle of least commitment.” In implementing the complex-number system in Section 2.4.1, we can use either Ben’s rectangular representation or Alyssa’s polar representation. The abstraction barrier formed by the selectors and constructors permits us to defer to the last possible moment the choice of a concrete representation for our data objects and thus retain maximum flexibility in our system design. The principle of least commitment can be carried to even further extremes. If we desire, we can maintain the ambiguity of representation even after we have designed the selectors and constructors, and elect to use both Ben’s representation and Alyssa’s representation. If both representations are included in a single system, however, we will need some way to distinguish data in polar form from data in rectangular form. Otherwise, if we were asked, for instance, to find the magnitude of the pair (3, 4), we wouldn’t know whether to answer 5 (interpreting the number in rectangular form) or 3 (interpreting the number in polar form). A straightforward way to accomplish this distinction is to include a type tag—the symbol rectangular or polar—as part of each complex number. Then when we need to manipulate a complex number we can use the tag to decide which selector to apply. 277 In order to manipulate tagged data, we will assume that we have procedures type-tag and contents that extract from a data object the tag and the actual contents (the polar or rectangular coordinates, in the case of a complex number). We will also postulate a procedure attach-tag that takes a tag and contents and produces a tagged data object. A straightforward way to implement this is to use ordinary list structure: (define (attach-tag type-tag contents) (cons type-tag contents)) (define (type-tag datum) (if (pair? datum) (car datum) (error "Bad tagged datum - TYPE-TAG" datum))) (define (contents datum) (if (pair? datum) (cdr datum) (error "Bad tagged datum - CONTENTS" datum))) Using these procedures, we can define predicates rectangular? and polar?, which recognize rectangular and polar numbers, respectively: (define (rectangular? z) (eq? (type-tag z) ’rectangular)) (define (polar? z) (eq? (type-tag z) ’polar)) With type tags, Ben and Alyssa can now modify their code so that their two different representations can coexist in the same system. Whenever 278 Ben constructs a complex number, he tags it as rectangular. Whenever Alyssa constructs a complex number, she tags it as polar. In addition, Ben and Alyssa must make sure that the names of their procedures do not conflict. One way to do this is for Ben to append the suffix rectangular to the name of each of his representation procedures and for Alyssa to append polar to the names of hers. Here is Ben’s revised rectangular representa- tion from Section 2.4.1: (define (real-part-rectangular z) (car z)) (define (imag-part-rectangular z) (cdr z)) (define (magnitude-rectangular z) (sqrt (+ (square (real-part-rectangular z)) (square (imag-part-rectangular z))))) (define (angle-rectangular z) (atan (imag-part-rectangular z) (real-part-rectangular z))) (define (make-from-real-imag-rectangular x y) (attach-tag ’rectangular (cons x y))) (define (make-from-mag-ang-rectangular r a) (attach-tag ’rectangular (cons (* r (cos a)) (* r (sin a))))) and here is Alyssa’s revised polar representation: (define (real-part-polar z) 279 (* (magnitude-polar z) (cos (angle-polar z)))) (define (imag-part-polar z) (* (magnitude-polar z) (sin (angle-polar z)))) (define (magnitude-polar z) (car z)) (define (angle-polar z) (cdr z)) (define (make-from-real-imag-polar x y) (attach-tag ’polar (cons (sqrt (+ (square x) (square y))) (atan y x)))) (define (make-from-mag-ang-polar r a) (attach-tag ’polar (cons r a))) Each generic selector is implemented as a procedure that checks the tag of its argument and calls the appropriate procedure for handling data of that type. For example, to obtain the real part of a complex number, real-part examines the tag to determine whether to use Ben’s real-part-rectangular or Alyssa’s real-part-polar. In either case, we use contents to extract the bare, untagged datum and send this to the rectangular or polar procedure as required: (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) 280 (real-part-polar (contents z))) (else (error "Unknown type - REAL-PART" z)))) (define (imag-part z) (cond ((rectangular? z) (imag-part-rectangular (contents z))) ((polar? z) (imag-part-polar (contents z))) (else (error "Unknown type - IMAG-PART" z)))) (define (magnitude z) (cond ((rectangular? z) (magnitude-rectangular (contents z))) ((polar? z) (magnitude-polar (contents z))) (else (error "Unknown type - MAGNITUDE" z)))) (define (angle z) (cond ((rectangular? z) (angle-rectangular (contents z))) ((polar? z) (angle-polar (contents z))) (else (error "Unknown type - ANGLE" z)))) To implement the complex-number arithmetic operations, we can use the same procedures add-complex, sub-complex, mul-complex, and div-complex from Section 2.4.1, because the selectors they call are 281 generic, and so will work with either representation. For example, the procedure add-complex is still (define (add-complex z1 z2) (make-from-real-imag (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2)))) Finally, we must choose whether to construct complex numbers using Ben’s representation or Alyssa’s representation. One reasonable choice is to construct rectangular numbers whenever we have real and imaginary parts and to construct polar numbers whenever we have magnitudes and angles: (define (make-from-real-imag x y) (make-from-real-imag-rectangular x y)) (define (make-from-mag-ang r a) (make-from-mag-ang-polar r a)) The resulting complex-number system has the structure shown in Figure 2.21. The system has been decomposed into three relatively independent parts: the complex-number-arithmetic operations, Alyssa’s polar implementation, and Ben’s rectangular implementation. The polar and rectangular implementations could have been written by Ben and Alyssa working separately, and both of these can be used as underlying representations by a third programmer implementing the complex-arithmetic procedures in terms of the abstract constructor/selector interface. Figure 2.21: Structure of the generic complex-arithmetic system. Since each data object is tagged with its type, the selectors operate on the data in a generic manner. That is, each selector is defined to have a behavior that depends upon the particular type of data it is applied to. Notice the general mechanism for interfacing the separate representations: Within a given representation implementation (say, Alyssa’s polar package) a complex number is an untyped pair (magnitude, angle). When a generic selector operates on a number of polar type, it strips off the tag and passes the contents on to Alyssa’s code. Conversely, when Alyssa constructs a number for general use, she tags it with a type so that it can be appropriately recognized by the higher-level procedures. This discipline of stripping off and attaching tags as data objects are passed from level to level can be an important organizational strategy, as we shall see in Section 2.5. 2.4.3 Data-Directed Programming and Additivity The general strategy of checking the type of a datum and calling an appropriate procedure is called dispatching on type. This is a powerful strategy for obtaining modularity in system design. On the other hand, implementing the dispatch as in Section 2.4.2 has two significant weaknesses. One weakness is that the generic interface procedures (real-part, imag-part, magnitude, and angle) must know about all the different representations. For instance, suppose we wanted to incorporate a new representation for complex numbers into our complex-number system. We would need to identify this new representation with a type, and then add a clause to each of the generic interface procedures to check for the new type and apply the appropriate selector for that representation. Another weakness of the technique is that even though the individual representations can be designed separately, we must guarantee that no two procedures in the entire system have the same name. This is why Ben and Alyssa had to change the names of their original procedures from Section 2.4.1. The issue underlying both of these weaknesses is that the technique for implementing generic interfaces is not additive. The person implementing the generic selector procedures must modify those procedures each time a new representation is installed, and the people interfacing the individual representations must modify their code to avoid name conflicts. In each of these cases, the changes that must be made to the code are straightforward, but they must be made nonetheless, and this is a source of inconvenience and error. This is not much of a problem for the complex-number system as it stands, but suppose there were not two but hundreds of different representations for complex numbers. And suppose that there were many 284 generic selectors to be maintained in the abstract-data interface. Suppose, in fact, that no one programmer knew all the interface procedures or all the representations. The problem is real and must be addressed in such programs as large-scale data-base-management systems. What we need is a means for modularizing the system design even further. This is provided by the programming technique known as datadirected programming. To understand how data-directed programming works, begin with the observation that whenever we deal with a set of generic operations that are common to a set of different types we are, in effect, dealing with a two-dimensional table that contains the possible operations on one axis and the possible types on the other axis. The entries in the table are the procedures that implement each operation for each type of argument presented. In the complex-number system developed in the previous section, the correspondence between operation name, data type, and actual procedure was spread out among the various conditional clauses in the generic interface procedures. But the same information could have been organized in a table, as shown in Figure 2.22. Figure 2.22: Table of operations for the complex-number system. 285 Data-directed programming is the technique of designing programs to work with such a table directly. Previously, we implemented the mechanism that interfaces the complex-arithmetic code with the two representation packages as a set of procedures that each perform an explicit dispatch on type. Here we will implement the interface as a single procedure that looks up the combination of the operation name and argument type in the table to find the correct procedure to apply, and then applies it to the contents of the argument. If we do this, then to add a new representation package to the system we need not change any existing procedures; we need only add new entries to the table. To implement this plan, assume that we have two procedures, put and get, for manipulating the operation-and-type table: • (put 〈op 〉〈type 〉〈item 〉) installs the 〈item 〉 in the table, indexed by the 〈op 〉 and the 〈type 〉. • (get 〈op 〉〈type 〉) looks up the 〈op 〉, 〈type 〉 entry in the table and returns the item found there. If no item is found, get returns false. For now, we can assume that put and get are included in our language. In Chapter 3 (Section 3.3.3) we will see how to implement these and other operations for manipulating tables. Here is how data-directed programming can be used in the complexnumber system. Ben, who developed the rectangular representation, implements his code just as he did originally. He defines a collection of procedures, or a package, and interfaces these to the rest of the system by adding entries to the table that tell the system how to operate on rectangular numbers. This is accomplished by calling the following procedure: (define (install-rectangular-package) 286 ;; internal procedures (define (real-part z) (car z)) (define (imag-part z) (cdr z)) (define (make-from-real-imag x y) (cons x y)) (define (magnitude z) (sqrt (+ (square (real-part z)) (square (imag-part z))))) (define (angle z) (atan (imag-part z) (real-part z))) (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a)))) ;; interface to the rest of the system (define (tag x) (attach-tag ’rectangular x)) (put ’real-part ’(rectangular) real-part) (put ’imag-part ’(rectangular) imag-part) (put ’magnitude ’(rectangular) magnitude) (put ’angle ’(rectangular) angle) (put ’make-from-real-imag ’rectangular (lambda (x y) (tag (make-from-real-imag x y)))) (put ’make-from-mag-ang ’rectangular (lambda (r a) (tag (make-from-mag-ang r a)))) ’done) Notice that the internal procedures here are the same procedures from Section 2.4.1 that Ben wrote when he was working in isolation. No changes are necessary in order to interface them to the rest of the system. Moreover, since these procedure definitions are internal to the installation procedure, 287 Ben needn’t worry about name conflicts with other procedures outside the rectangular package. To interface these to the rest of the system, Ben installs his real-part procedure under the operation name real-part and the type (rectangular), and similarly for the other selectors.45 The interface also defines the constructors to be used by the external system.46 These are identical to Ben’s internally defined constructors, except that they attach the tag. Alyssa’s polar package is analogous: (define (install-polar-package) ;; internal procedures (define (magnitude z) (car z)) (define (angle z) (cdr z)) (define (make-from-mag-ang r a) (cons r a)) (define (real-part z) (* (magnitude z) (cos (angle z)))) (define (imag-part z) (* (magnitude z) (sin (angle z)))) (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x))) ;; interface to the rest of the system (define (tag x) (attach-tag ’polar x)) 45 We use the list (rectangular) rather than the symbol rectangular to allow for the possibility of operations with multiple arguments, not all of the same type. 46 The type the constructors are installed under needn’t be a list because a constructor is always used to make an object of one particular type. 288 (put ’real-part ’(polar) real-part) (put ’imag-part ’(polar) imag-part) (put ’magnitude ’(polar) magnitude) (put ’angle ’(polar) angle) (put ’make-from-real-imag ’polar (lambda (x y) (tag (make-from-real-imag x y)))) (put ’make-from-mag-ang ’polar (lambda (r a) (tag (make-from-mag-ang r a)))) ’done) Even though Ben and Alyssa both still use their original procedures defined with the same names as each other’s (e.g., real-part), these definitions are now internal to different procedures (see Section 1.1.8), so there is no name conflict. The complex-arithmetic selectors access the table by means of a general “operation” procedure called apply-generic, which applies a generic operation to some arguments. Apply-generic looks in the table under the name of the operation and the types of the arguments and applies the resulting procedure if one is present:47 47 Apply-generic uses the dotted-tail notation described in Exercise 2.20, because different generic operations may take different numbers of arguments. In applygeneric, op has as its value the first argument to apply-generic and args has as its value a list of the remaining arguments. Apply-generic also uses the primitive procedure apply, which takes two argu- ments, a procedure and a list. Apply applies the procedure, using the elements in the list as arguments. For example, (apply + (list 1 2 3 4)) returns 10. 289 (define (apply-generic op . args) (let ((type-tags (map type-tag args))) (let ((proc (get op type-tags))) (if proc (apply proc (map contents args)) (error "No method for these types - APPLY-GENERIC" (list op type-tags)))))) Using apply-generic, we can define our generic selectors as follows: (define (real-part z) (apply-generic ’real-part z)) (define (imag-part z) (apply-generic ’imag-part z)) (define (magnitude z) (apply-generic ’magnitude z)) (define (angle z) (apply-generic ’angle z)) Observe that these do not change at all if a new representation is added to the system. We can also extract from the table the constructors to be used by the programs external to the packages in making complex numbers from real and imaginary parts and from magnitudes and angles. As in Section 2.4.2, we construct rectangular numbers whenever we have real and imaginary parts, and polar numbers whenever we have magnitudes and angles: (define (make-from-real-imag x y) ((get ’make-from-real-imag ’rectangular) x y)) (define (make-from-mag-ang r a) ((get ’make-from-mag-ang ’polar) r a)) Exercise 2.73: Section 2.3.2 described a program that performs symbolic differentiation: 290 (define (deriv exp var) (cond ((number? exp) 0) ((variable? exp) (if (same-variable? exp var) 1 0)) ((sum? exp) (make-sum (deriv (addend exp) var) (deriv (augend exp) var))) ((product? exp) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (deriv (multiplier exp) var) (multiplicand exp)))) 〈more rules can be added here〉 (else (error "unknown expression type - DERIV" exp)))) We can regard this program as performing a dispatch on the type of the expression to be differentiated. In this situation the “type tag” of the datum is the algebraic operator symbol (such as +) and the operation being performed is deriv. We can transform this program into data-directed style by rewriting the basic derivative procedure as (define (deriv exp var) (cond ((number? exp) 0) ((variable? exp) (if (same-variable? exp var) 1 0)) (else ((get ’deriv (operator exp)) (operands exp) var)))) (define (operator exp) (car exp)) (define (operands exp) (cdr exp)) a. Explain what was done above. Why can’t we assimilate the predicates number? and variable? into the data-directed dispatch? b. Write the procedures for derivatives of sums and products, and the auxiliary code required to install them in the table used by the program above. c. Choose any additional differentiation rule that you like, such as the one for exponents (Exercise 2.56), and install it in this datadirected system. d. In this simple algebraic manipulator the type of an expression is the algebraic operator that binds it together. Suppose, however, we indexed the procedures in the opposite way, so that the dispatch line in deriv looked like ((get (operator exp) ’deriv) (operands exp) var) What corresponding changes to the derivative system are required? Exercise 2.74: Insatiable Enterprises, Inc., is a highly decentralized conglomerate company consisting of a large number of independent divisions located all over the world. The company’s computer facilities have just been interconnected by means of a clever network-interfacing scheme that makes the entire network appear to any user to be a single computer. Insatiable’s president, in her first attempt to exploit the ability of the network to extract 292 administrative information from division files, is dismayed to discover that, although all the division files have been implemented as data structures in Scheme, the particular data structure used varies from division to division. A meeting of division managers is hastily called to search for a strategy to integrate the files that will satisfy headquarters’ needs while preserving the existing autonomy of the divisions. Show how such a strategy can be implemented with data-directed programming. As an example, suppose that each division’s personnel records consist of a single file, which contains a set of records keyed on employees’ names. The structure of the set varies from division to division. Furthermore, each employee’s record is itself a set (structured differently from division to division) that contains information keyed under identifiers such as address and salary. In particular: a. Implement for headquarters a get-record procedure that retrieves a specified employee’s record from a specified personnel file. The procedure should be applicable to any division’s file. Explain how the individual divisions’ files should be structured. In particular, what type information must be supplied? b. Implement for headquarters a get-salary procedure that returns the salary information from a given employee’s record from any division’s personnel file. How should the record be structured in order to make this operation work? c. Implement for headquarters a find-employee-record procedure. This should search all the divisions’ files for the record of a given employee and return the record. Assume that this pro- 293 cedure takes as arguments an employee’s name and a list of all the divisions’ files. d. When Insatiable takes over a new company, what changes must be made in order to incorporate the new personnel information into the central system? Message passing The key idea of data-directed programming is to handle generic operations in programs by dealing explicitly with operation-and-type tables, such as the table in Figure 2.22. The style of programming we used in Section 2.4.2 organized the required dispatching on type by having each operation take care of its own dispatching. In effect, this decomposes the operation-andtype table into rows, with each generic operation procedure representing a row of the table. An alternative implementation strategy is to decompose the table into columns and, instead of using “intelligent operations” that dispatch on data types, to work with “intelligent data objects” that dispatch on operation names. We can do this by arranging things so that a data object, such as a rectangular number, is represented as a procedure that takes as input the required operation name and performs the operation indicated. In such a discipline, make-from-real-imag could be written as (define (make-from-real-imag x y) (define (dispatch op) (cond ((eq? op ’real-part) x) ((eq? op ’imag-part) y) ((eq? op ’magnitude) (sqrt (+ (square x) (square y)))) 294 ((eq? op ’angle) (atan y x)) (else (error "Unknown op - MAKE-FROM-REAL-IMAG" op)))) dispatch) The corresponding apply-generic procedure, which applies a generic operation to an argument, now simply feeds the operation’s name to the data object and lets the object do the work:48 (define (apply-generic op arg) (arg op)) Note that the value returned by make-from-real-imag is a procedure— the internal dispatch procedure. This is the procedure that is invoked when apply-generic requests an operation to be performed. This style of programming is called message passing. The name comes from the image that a data object is an entity that receives the requested operation name as a “message.” We have already seen an example of message passing in Section 2.1.3, where we saw how cons, car, and cdr could be defined with no data objects but only procedures. Here we see that message passing is not a mathematical trick but a useful technique for organizing systems with generic operations. In the remainder of this chapter we will continue to use data-directed programming, rather than message passing, to discuss generic arithmetic operations. In Chapter 3 we will return to message passing, and we will see that it can be a powerful tool for structuring simulation programs. 48 One limitation of this organization is it permits only generic procedures of one 295 Exercise 2.75: Implement the constructor make-from-mag-ang in message-passing style. This procedure should be analogous to the make-from-real-imag procedure given above. Exercise 2.76: As a large system with generic operations evolves, new types of data objects or new operations may be needed. For each of the three strategies—generic operations with explicit dispatch, data-directed style, and message-passing-style—describe the changes that must be made to a system in order to add new types or new operations. Which organization would be most appropriate for a system in which new types must often be added? Which would be most appropriate for a system in which new operations must often be added? 2.5 Systems with Generic Operations In the previous section, we saw how to design systems in which data objects can be represented in more than one way. The key idea is to link the code that specifies the data operations to the several representations by means of generic interface procedures. Now we will see how to use this same idea not only to define operations that are generic over different representations but also to define operations that are generic over different kinds of arguments. We have already seen several different packages of arithmetic operations: the primitive arithmetic (+, -, *, /) built into our language, the rational-number arithmetic (add-rat, sub-rat, mul-rat, div-rat) of Section 2.1.1, and the complex-number arithmetic that we implemented in Section 2.4.3. We will now use data-directed techniques to construct a package of arithmetic operations that incorporates all the arithmetic packages we have already constructed. 296 Figure 2.23 shows the structure of the system we shall build. Notice the abstraction barriers. From the perspective of someone using “numbers,” there is a single procedure add that operates on whatever numbers are supplied. Add is part of a generic interface that allows the separate ordinary-arithmetic, rational-arithmetic and complex-arithmetic packages to be accessed uniformly by programs that use numbers. Any individual arithmetic package (such as the complex package) may itself be accessed through generic procedures (such as add-complex) that combine packages designed for different representations (such as rectangular and polar). Moreover, the structure of the system is additive, so that one can design the individual arithmetic packages separately and combine them to produce a generic arithmetic system. Figure 2.23: Generic arithmetic system. 2.5.1 Generic Arithmetic Operations The task of designing generic arithmetic operations is analogous to that of designing the generic complex-number operations. We would like, for instance, to have a generic addition procedure add that acts like ordinary primitive addition + on ordinary numbers, like add-rat on rational numbers, and like add-complex on complex numbers. We can implement add, and the other generic arithmetic operations, by following the same strategy we used in Section 2.4.3 to implement the generic selectors for complex numbers. We will attach a type tag to each kind of number and cause the generic procedure to dispatch to an appropriate package according to the data type of its arguments. The generic arithmetic procedures are defined as follows: (define (add x y) (apply-generic ’add x y)) (define (sub x y) (apply-generic ’sub x y)) (define (mul x y) (apply-generic ’mul x y)) (define (div x y) (apply-generic ’div x y)) We begin by installing a package for handling ordinary numbers, that is, the primitive numbers of our language. We will tag these with the symbol scheme-number. The arithmetic operations in this package are the primitive arithmetic procedures (so there is no need to define extra procedures to handle the untagged numbers). Since these operations each take two arguments, they are installed in the table keyed by the list (scheme-number scheme-number): (define (install-scheme-number-package) (define (tag x) (attach-tag ’scheme-number x)) (put ’add ’(scheme-number scheme-number) 298 (lambda (x y) (tag (+ x y)))) (put ’sub ’(scheme-number scheme-number) (lambda (x y) (tag (- x y)))) (put ’mul ’(scheme-number scheme-number) (lambda (x y) (tag (* x y)))) (put ’div ’ (scheme-number scheme-number) (lambda (x y) (tag (/ x y)))) (put ’make ’scheme-number (lambda (x) (tag x))) ’done) Users of the Scheme-number package will create (tagged) ordinary numbers by means of the procedure: (define (make-scheme-number n) ((get ’make ’scheme-number) n)) Now that the framework of the generic arithmetic system is in place, we can readily include new kinds of numbers. Here is a package that performs rational arithmetic. Notice that, as a benefit of additivity, we can use without modification the rational-number code from Section 2.1.1 as the internal procedures in the package: (define (install-rational-package) ;; internal procedures (define (numer x) (car x)) (define (denom x) (cdr x)) (define (make-rat n d) (let ((g (gcd n d))) (cons (/ n g) (/ d g)))) (define (add-rat x y) 299 (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (sub-rat x y) (make-rat (- (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) (define (mul-rat x y) (make-rat (* (numer x) (numer y)) (* (denom x) (denom y)))) (define (div-rat x y) (make-rat (* (numer x) (denom y)) (* (denom x) (numer y)))) ;; interface to rest of the system (define (tag x) (attach-tag ’rational x)) (put ’add ’(rational rational) (lambda (x y) (tag (add-rat x y)))) (put ’sub ’(rational rational) (lambda (x y) (tag (sub-rat x y)))) (put ’mul ’(rational rational) (lambda (x y) (tag (mul-rat x y)))) (put ’div ’(rational rational) (lambda (x y) (tag (div-rat x y)))) (put ’make ’rational (lambda (n d) (tag (make-rat n d)))) 300 ’done) (define (make-rational n d) ((get ’make ’rational) n d)) We can install a similar package to handle complex numbers, using the tag complex. In creating the package, we extract from the table the operations make-from-real-imag and make-from-mag-ang that were defined by the rectangular and polar packages. Additivity permits us to use, as the internal operations, the same add-complex, sub-complex, mul-complex, and div-complex procedures from Section 2.4.1. (define (install-complex-package) ;; imported procedures from rectangular and polar packages (define (make-from-real-imag x y) ((get ’make-from-real-imag ’rectangular) x y)) (define (make-from-mag-ang r a) ((get ’make-from-mag-ang ’polar) r a)) ;; internal procedures (define (add-complex z1 z2) (make-from-real-imag (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2)))) (define (sub-complex z1 z2) (make-from-real-imag (- (real-part z1) (real-part z2)) (- (imag-part z1) (imag-part z2)))) (define (mul-complex z1 z2) (make-from-mag-ang (* (magnitude z1) (magnitude z2)) (+ (angle z1) (angle z2)))) 301 (define (div-complex z1 z2) (make-from-mag-ang (/ (magnitude z1) (magnitude z2)) (- (angle z1) (angle z2)))) ;; interface to rest of the system (define (tag z) (attach-tag ’complex z)) (put ’add ’(complex complex) (lambda (z1 z2) (tag (add-complex z1 z2)))) (put ’sub ’(complex complex) (lambda (z1 z2) (tag (sub-complex z1 z2)))) (put ’mul ’(complex complex) (lambda (z1 z2) (tag (mul-complex z1 z2)))) (put ’div ’(complex complex) (lambda (z1 z2) (tag (div-complex z1 z2)))) (put ’make-from-real-imag ’complex (lambda (x y) (tag (make-from-real-imag x y)))) (put ’make-from-mag-ang ’complex (lambda (r a) (tag (make-from-mag-ang r a)))) ’done) Programs outside the complex-number package can construct complex numbers either from real and imaginary parts or from magnitudes and angles. Notice how the underlying procedures, originally defined in the rectangular and polar packages, are exported to the complex package, and exported from there to the outside world. (define (make-complex-from-real-imag x y) ((get ’make-from-real-imag ’complex) x y)) 302 (define (make-complex-from-mag-ang r a) ((get ’make-from-mag-ang ’complex) r a)) What we have here is a two-level tag system. A typical complex number, such as 3+4i in rectangular form, would be represented as shown in Figure 2.24. The outer tag (complex) is used to direct the number to the complex package. Once within the complex package, the next tag (rectangular) is used to direct the number to the rectangular package. In a large and complicated system there might be many levels, each interfaced with the next by means of generic operations. As a data object is passed “downward,” the outer tag that is used to direct it to the appropriate package is stripped off (by applying contents) and the next level of tag (if any) becomes visible to be used for further dispatching. Figure 2.24: Representation of 3 + 4i in rectangular form. In the above packages, we used add-rat, add-complex, and the other arithmetic procedures exactly as originally written. Once these definitions are internal to different installation procedures, however, they no longer need names that are distinct from each other: we could simply name them add, sub, mul, and div in both packages. Exercise 2.77: Louis Reasoner tries to evaluate the expression (magnitude z) where z is the object shown in Figure 2.24. To his surprise, instead of the answer 5 he gets an error message 303 from apply-generic, saying there is no method for the operation magnitude on the types (complex). He shows this interaction to Alyssa P. Hacker, who says “The problem is that the complexnumber selectors were never defined for complex numbers, just for polar and rectangular numbers. All you have to do to make this work is add the following to the complex package:” (put ’real-part ’(complex) real-part) (put ’imag-part ’(complex) imag-part) (put ’magnitude ’(complex) magnitude) (put ’angle ’(complex) angle) Describe in detail why this works. As an example, trace through all the procedures called in evaluating the expression (magnitude z) where z is the object shown in Figure 2.24. In particular, how many times is apply-generic invoked? What procedure is dispatched to in each case? Exercise 2.78: The internal procedures in the scheme-number package are essentially nothing more than calls to the primitive procedures +, -, etc. It was not possible to use the primitives of the language directly because our type-tag system requires that each data object have a type attached to it. In fact, however, all Lisp implementations do have a type system, which they use internally. Primitive predicates such as symbol? and number? determine whether data objects have particular types. Modify the definitions of type-tag, contents, and attach-tag from Section 2.4.2 so that our generic system takes advantage of Scheme’s internal type system. That is to say, the system should work as before except that ordinary numbers should be represented simply as 304 Scheme numbers rather than as pairs whose car is the symbol scheme-number. Exercise 2.79: Define a generic equality predicate equ? that tests the equality of two numbers, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers. Exercise 2.80: Define a generic predicate =zero? that tests if its argument is zero, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers. 2.5.2 Combining Data of Different Types We have seen how to define a unified arithmetic system that encompasses ordinary numbers, complex numbers, rational numbers, and any other type of number we might decide to invent, but we have ignored an important issue. The operations we have defined so far treat the different data types as being completely independent. Thus, there are separate packages for adding, say, two ordinary numbers, or two complex numbers. What we have not yet considered is the fact that it is meaningful to define operations that cross the type boundaries, such as the addition of a complex number to an ordinary number. We have gone to great pains to introduce barriers between parts of our programs so that they can be developed and understood separately. We would like to introduce the cross-type operations in some carefully controlled way, so that we can support them without seriously violating our module boundaries. One way to handle cross-type operations is to design a different procedure for each possible combination of types for which the operation is 305 valid. For example, we could extend the complex-number package so that it provides a procedure for adding complex numbers to ordinary numbers and installs this in the table using the tag (complex scheme-number):49 ;; to be included in the complex package (define (add-complex-to-schemenum z x) (make-from-real-imag (+ (real-part z) x) (imag-part z))) (put ’add ’(complex scheme-number) (lambda (z x) (tag (add-complex-to-schemenum z x)))) This technique works, but it is cumbersome. With such a system, the cost of introducing a new type is not just the construction of the package of procedures for that type but also the construction and installation of the procedures that implement the cross-type operations. This can easily be much more code than is needed to define the operations on the type itself. The method also undermines our ability to combine separate packages additively, or least to limit the extent to which the implementors of the individual packages need to take account of other packages. For instance, in the example above, it seems reasonable that handling mixed operations on complex numbers and ordinary numbers should be the responsibility of the complex-number package. Combining rational numbers and complex numbers, however, might be done by the complex package, by the rational package, or by some third package that uses operations extracted from these two packages. Formulating coherent policies on the division of 49 We also have to supply an almost identical procedure to handle the types (scheme- number complex). 306 responsibility among packages can be an overwhelming task in designing systems with many packages and many cross-type operations. Coercion In the general situation of completely unrelated operations acting on completely unrelated types, implementing explicit cross-type operations, cumbersome though it may be, is the best that one can hope for. Fortunately, we can usually do better by taking advantage of additional structure that may be latent in our type system. Often the different data types are not completely independent, and there may be ways by which objects of one type may be viewed as being of another type. This process is called coercion. For example, if we are asked to arithmetically combine an ordinary number with a complex number, we can view the ordinary number as a complex number whose imaginary part is zero. This transforms the problem to that of combining two complex numbers, which can be handled in the ordinary way by the complex-arithmetic package. In general, we can implement this idea by designing coercion procedures that transform an object of one type into an equivalent object of another type. Here is a typical coercion procedure, which transforms a given ordinary number to a complex number with that real part and zero imaginary part: (define (scheme-number->complex n) (make-complex-from-real-imag (contents n) 0)) We install these coercion procedures in a special coercion table, indexed under the names of the two types: (put-coercion ’scheme-number ’complex scheme-number->complex) 307 (We assume that there are put-coercion and get-coercion procedures available for manipulating this table.) Generally some of the slots in the table will be empty, because it is not generally possible to coerce an arbitrary data object of each type into all other types. For example, there is no way to coerce an arbitrary complex number to an ordinary number, so there will be no general complex->scheme-number procedure included in the table. Once the coercion table has been set up, we can handle coercion in a uniform manner by modifying the apply-generic procedure of Section 2.4.3. When asked to apply an operation, we first check whether the operation is defined for the arguments’ types, just as before. If so, we dispatch to the procedure found in the operation-and-type table. Otherwise, we try coercion. For simplicity, we consider only the case where there are two arguments.50 We check the coercion table to see if objects of the first type can be coerced to the second type. If so, we coerce the first argument and try the operation again. If objects of the first type cannot in general be coerced to the second type, we try the coercion the other way around to see if there is a way to coerce the second argument to the type of the first argument. Finally, if there is no known way to coerce either type to the other type, we give up. Here is the procedure: (define (apply-generic op . args) (let ((type-tags (map type-tag args))) (let ((proc (get op type-tags))) (if proc (apply proc (map contents args)) (if (= (length args) 2) (let ((type1 (car type-tags)) 50 See Exercise 2.82 for generalizations. 308 (type2 (cadr type-tags)) (a1 (car args)) (a2 (cadr args))) (let ((t1->t2 (get-coercion type1 type2)) (t2->t1 (get-coercion type2 type1))) (cond (t1->t2 (apply-generic op (t1->t2 a1) a2)) (t2->t1 (apply-generic op a1 (t2->t1 a2))) (else (error "No method for these types" (list op type-tags)))))) (error "No method for these types" (list op type-tags))))))) This coercion scheme has many advantages over the method of defining explicit cross-type operations, as outlined above. Although we still need to write coercion procedures to relate the types (possibly n 2 procedures for a system with n types), we need to write only one procedure for each pair of types rather than a different procedure for each collection of types and each generic operation.51 What we are counting on here is the fact that 51 If we are clever, we can usually get by with fewer than n 2 coercion procedures. For instance, if we know how to convert from type 1 to type 2 and from type 2 to type 3, then we can use this knowledge to convert from type 1 to type 3. This can greatly decrease the number of coercion procedures we need to supply explicitly when we add a new type to the system. If we are willing to build the required amount of sophistication into our system, we can have it search the “graph” of relations among 309 the appropriate transformation between types depends only on the types themselves, not on the operation to be applied. On the other hand, there may be applications for which our coercion scheme is not general enough. Even when neither of the objects to be combined can be converted to the type of the other it may still be possible to perform the operation by converting both objects to a third type. In order to deal with such complexity and still preserve modularity in our programs, it is usually necessary to build systems that take advantage of still further structure in the relations among types, as we discuss next. Hierarchies of types The coercion scheme presented above relied on the existence of natural relations between pairs of types. Often there is more “global” structure in how the different types relate to each other. For instance, suppose we are building a generic arithmetic system to handle integers, rational numbers, real numbers, and complex numbers. In such a system, it is quite natural to regard an integer as a special kind of rational number, which is in turn a special kind of real number, which is in turn a special kind of complex number. What we actually have is a so-called hierarchy of types, in which, for example, integers are a subtype of rational numbers (i.e., any operation that can be applied to a rational number can automatically be applied to an integer). Conversely, we say that rational numbers form a supertype of integers. The particular hierarchy we have here is of a very simple kind, in which each type has at most one supertype and at most one subtype. Such a structure, called a tower, is illustrated in Figure 2.25. types and automatically generate those coercion procedures that can be inferred from the ones that are supplied explicitly. Figure 2.25: A tower of types. If we have a tower structure, then we can greatly simplify the problem of adding a new type to the hierarchy, for we need only specify how the new type is embedded in the next supertype above it and how it is the supertype of the type below it. For example, if we want to add an integer to a complex number, we need not explicitly define a special coercion procedure integer->complex. Instead, we define how an integer can be transformed into a rational number, how a rational number is transformed into a real number, and how a real number is transformed into a complex number. We then allow the system to transform the integer into a complex number through these steps and then add the two complex numbers. We can redesign our apply-generic procedure in the following way: For each type, we need to supply a raise procedure, which “raises” objects of that type one level in the tower. Then when the system is required to operate on objects of different types it can successively raise the lower types until all the objects are at the same level in the tower. (Exercise 2.83 and Exercise 2.84 concern the details of implementing such a strategy.) 311 Another advantage of a tower is that we can easily implement the notion that every type “inherits” all operations defined on a supertype. For instance, if we do not supply a special procedure for finding the real part of an integer, we should nevertheless expect that real-part will be defined for integers by virtue of the fact that integers are a subtype of complex numbers. In a tower, we can arrange for this to happen in a uniform way by modifying apply-generic. If the required operation is not directly defined for the type of the object given, we raise the object to its supertype and try again. We thus crawl up the tower, transforming our argument as we go, until we either find a level at which the desired operation can be performed or hit the top (in which case we give up). Yet another advantage of a tower over a more general hierarchy is that it gives us a simple way to “lower” a data object to the simplest representation. For example, if we add 2 + 3i to 4 − 3i , it would be nice to obtain the answer as the integer 6 rather than as the complex number 6 + 0i . Exercise 2.85 discusses a way to implement such a lowering operation. (The trick is that we need a general way to distinguish those objects that can be lowered, such as 6 + 0i , from those that cannot, such as 6 + 2i .) Inadequacies of hierarchies If the data types in our system can be naturally arranged in a tower, this greatly simplifies the problems of dealing with generic operations on different types, as we have seen. Unfortunately, this is usually not the case. Figure 2.26 illustrates a more complex arrangement of mixed types, this one showing relations among different types of geometric figures. We see that, in general, a type may have more than one subtype. Triangles and quadrilaterals, for instance, are both subtypes of polygons. In addition, a 312 type may have more than one supertype. For example, an isosceles right triangle may be regarded either as an isosceles triangle or as a right triangle. This multiple-supertypes issue is particularly thorny, since it means that there is no unique way to “raise” a type in the hierarchy. Finding the “correct” supertype in which to apply an operation to an object may involve considerable searching through the entire type network on the part of a procedure such as apply-generic. Since there generally are multiple subtypes for a type, there is a similar problem in coercing a value “down” the type hierarchy. Dealing with large numbers of interrelated types while still preserving modularity in the design of large systems is very difficult, and is an area of much current 52 This statement, which also appears in the first edition of this book, is just as true now as it was when we wrote it twelve years ago. Developing a useful, general framework for expressing the relations among different types of entities (what philosophers call “ontology”) seems intractably difficult. The main difference between the confusion that existed ten years ago and the confusion that exists now is that now a variety of inadequate ontological theories have been embodied in a plethora of correspondingly inadequate programming languages. For example, much of the complexity of object-oriented programming languages—and the subtle and confusing differences among contemporary object-oriented languages—centers on the treatment of generic operations on interrelated types. Our own discussion of computational objects in Chapter 3 avoids these issues entirely. Readers familiar with object-oriented programming will notice that we have much to say in chapter 3 about local state, but we do not even mention “classes” or “inheritance.” In fact, we suspect that these problems cannot be adequately addressed in terms of computer-language design alone, without also drawing on work in knowledge representation and automated reasoning. Figure 2.26: Relations among types of geometric figures. Exercise 2.81: Louis Reasoner has noticed that apply-generic may try to coerce the arguments to each other’s type even if they already have the same type. Therefore, he reasons, we need to put procedures in the coercion table to coerce arguments of each type to their own type. For example, in addition to the scheme-number->complex coercion shown above, he would do: (define (scheme-number->scheme-number n) n) (define (complex->complex z) z) (put-coercion ’scheme-number ’scheme-number scheme-number->scheme-number) (put-coercion ’complex ’complex complex->complex) 314 a. With Louis’s coercion procedures installed, what happens if apply-generic is called with two arguments of type scheme-number or two arguments of type complex for an operation that is not found in the table for those types? For example, assume that we’ve defined a generic exponentiation operation: (define (exp x y) (apply-generic ’exp x y)) and have put a procedure for exponentiation in the Schemenumber package but not in any other package: ;; following added to Scheme-number package (put ’exp ’(scheme-number scheme-number) (lambda (x y) (tag (expt x y)))) ; using primi- tive expt What happens if we call exp with two complex numbers as arguments? b. Is Louis correct that something had to be done about coercion with arguments of the same type, or does apply-generic work correctly as is? c. Modify apply-generic so that it doesn’t try coercion if the two arguments have the same type. Exercise 2.82: Show how to generalize apply-generic to handle coercion in the general case of multiple arguments. One strategy is to attempt to coerce all the arguments to the type of the first argument, then to the type of the second argument, and so on. Give an example of a situation where this strategy (and likewise the twoargument version given above) is not sufficiently general. (Hint: 315 Consider the case where there are some suitable mixed-type operations present in the table that will not be tried.) Exercise 2.83: Suppose you are designing a generic arithmetic system for dealing with the tower of types shown in Figure 2.25: integer, rational, real, complex. For each type (except complex), design a procedure that raises objects of that type one level in the tower. Show how to install a generic raise operation that will work for each type (except complex). Exercise 2.84: Using the raise operation of Exercise 2.83, modify the apply-generic procedure so that it coerces its arguments to have the same type by the method of successive raising, as discussed in this section. You will need to devise a way to test which of two types is higher in the tower. Do this in a manner that is “compatible” with the rest of the system and will not lead to problems in adding new levels to the tower. Exercise 2.85: This section mentioned a method for “simplifying” a data object by lowering it in the tower of types as far as possible. Design a procedure drop that accomplishes this for the tower described in Exercise 2.83. The key is to decide, in some general way, whether an object can be lowered. For example, the complex number 1.5 + 0i can be lowered as far as real, the complex number 1 + 0i can be lowered as far as integer, and the complex number 2 + 3i cannot be lowered at all. Here is a plan for determining whether an object can be lowered: Begin by defining a generic operation project that “pushes” an object down in the tower. For example, projecting a complex number would involve throwing away the imaginary part. Then a number can be dropped 316 if, when we project it and raise the result back to the type we started with, we end up with something equal to what we started with. Show how to implement this idea in detail, by writing a drop procedure that drops an object as far as possible. You will need to design the various projection operations53 and install project as a generic operation in the system. You will also need to make use of a generic equality predicate, such as described in Exercise 2.79. Finally, use drop to rewrite apply-generic from Exercise 2.84 so that it “simplifies” its answers. Exercise 2.86: Suppose we want to handle complex numbers whose real parts, imaginary parts, magnitudes, and angles can be either ordinary numbers, rational numbers, or other numbers we might wish to add to the system. Describe and implement the changes to the system needed to accommodate this. You will have to define operations such as sine and cosine that are generic over ordinary numbers and rational numbers. 2.5.3 Example: Symbolic Algebra The manipulation of symbolic algebraic expressions is a complex process that illustrates many of the hardest problems that occur in the design of large-scale systems. An algebraic expression, in general, can be viewed as a hierarchical structure, a tree of operators applied to operands. We can construct algebraic expressions by starting with a set of primitive objects, such as constants and variables, and combining these by means of algebraic operators, such as addition and multiplication. As in other languages, 53 A real number can be projected to an integer using the round primitive, which re- turns the closest integer to its argument. 317 we form abstractions that enable us to refer to compound objects in simple terms. Typical abstractions in symbolic algebra are ideas such as linear combination, polynomial, rational function, or trigonometric function. We can regard these as compound “types,” which are often useful for directing the processing of expressions. For example, we could describe the expression x 2 sin( y 2 + 1) + x cos 2 y + cos( y 3 − 2 y 2 ) as a polynomial in x with coefficients that are trigonometric functions of polynomials in y whose coefficients are integers. We will not attempt to develop a complete algebraic-manipulation system here. Such systems are exceedingly complex programs, embodying deep algebraic knowledge and elegant algorithms. What we will do is look at a simple but important part of algebraic manipulation: the arithmetic of polynomials. We will illustrate the kinds of decisions the designer of such a system faces, and how to apply the ideas of abstract data and generic operations to help organize this effort. Arithmetic on polynomials Our first task in designing a system for performing arithmetic on polynomials is to decide just what a polynomial is. Polynomials are normally defined relative to certain variables (the indeterminates of the polynomial). For simplicity, we will restrict ourselves to polynomials having just one indeterminate (univariate polynomials).54 We will define a polynomial to be 54 On the other hand, we will allow polynomials whose coefficients are themselves polynomials in other variables. This will give us essentially the same representational power as a full multivariate system, although it does lead to coercion problems, as discussed below. 318 a sum of terms, each of which is either a coefficient, a power of the indeterminate, or a product of a coefficient and a power of the indeterminate. A coefficient is defined as an algebraic expression that is not dependent upon the indeterminate of the polynomial. For example, 5x 2 + 3x + 7 is a simple polynomial in x , and ( y 2 + 1)x 3 + (2 y )x + 1 is a polynomial in x whose coefficients are polynomials in y . Already we are skirting some thorny issues. Is the first of these polynomials the same as the polynomial 5 y 2 + 3 y + 7, or not? A reasonable answer might be “yes, if we are considering a polynomial purely as a mathematical function, but no, if we are considering a polynomial to be a syntactic form.” The second polynomial is algebraically equivalent to a polynomial in y whose coefficients are polynomials in x . Should our system recognize this, or not? Furthermore, there are other ways to represent a polynomial— for example, as a product of factors, or (for a univariate polynomial) as the set of roots, or as a listing of the values of the polynomial at a specified set of points.55 We can finesse these questions by deciding that in our algebraicmanipulation system a “polynomial” will be a particular syntactic form, not its underlying mathematical meaning. 55 For univariate polynomials, giving the value of a polynomial at a given set of points can be a particularly good representation. This makes polynomial arithmetic extremely simple. To obtain, for example, the sum of two polynomials represented in this way, we need only add the values of the polynomials at corresponding points. To transform back to a more familiar representation, we can use the Lagrange interpolation formula, which shows how to recover the coefficients of a polynomial of degree n given the values of the polynomial at n + 1 points. 319 Now we must consider how to go about doing arithmetic on polynomials. In this simple system, we will consider only addition and multiplication. Moreover, we will insist that two polynomials to be combined must have the same indeterminate. We will approach the design of our system by following the familiar discipline of data abstraction. We will represent polynomials using a data structure called a poly, which consists of a variable and a collection of terms. We assume that we have selectors variable and term-list that extract those parts from a poly and a constructor make-poly that assembles a poly from a given variable and a term list. A variable will be just a symbol, so we can use the same-variable? procedure of Section 2.3.2 to compare variables. The following procedures define addition and multiplication of polys: (define (add-poly p1 p2) (if (same-variable? (variable p1) (variable p2)) (make-poly (variable p1) (add-terms (term-list p1) (term-list p2))) (error "Polys not in same var - ADD-POLY" (list p1 p2)))) (define (mul-poly p1 p2) (if (same-variable? (variable p1) (variable p2)) (make-poly (variable p1) (mul-terms (term-list p1) (term-list p2))) (error "Polys not in same var - MUL-POLY" (list p1 p2)))) 320 To incorporate polynomials into our generic arithmetic system, we need to supply them with type tags. We’ll use the tag polynomial, and install appropriate operations on tagged polynomials in the operation table. We’ll embed all our code in an installation procedure for the polynomial package, similar to the ones in Section 2.5.1: (define (install-polynomial-package) ;; internal procedures ;; representation of poly (define (make-poly variable term-list) (cons variable term-list)) (define (variable p) (car p)) (define (term-list p) (cdr p)) 〈procedures same-variable? and variable? from section 2.3.2〉 ;; representation of terms and term lists 〈procedures adjoin-term ... coeff from text below〉 (define (add-poly p1 p2) ...) 〈procedures used by add-poly〉 (define (mul-poly p1 p2) ...) 〈procedures used by mul-poly〉 ;; interface to rest of the system (define (tag p) (attach-tag ’polynomial p)) (put ’add ’(polynomial polynomial) (lambda (p1 p2) (tag (add-poly p1 p2)))) 321 (put ’mul ’(polynomial polynomial) (lambda (p1 p2) (tag (mul-poly p1 p2)))) (put ’make ’polynomial (lambda (var terms) (tag (make-poly var terms)))) ’done) Polynomial addition is performed termwise. Terms of the same order (i.e., with the same power of the indeterminate) must be combined. This is done by forming a new term of the same order whose coefficient is the sum of the coefficients of the addends. Terms in one addend for which there are no terms of the same order in the other addend are simply accumulated into the sum polynomial being constructed. In order to manipulate term lists, we will assume that we have a constructor the-empty-termlist that returns an empty term list and a constructor adjoin-term that adjoins a new term to a term list. We will also assume that we have a predicate empty-termlist? that tells if a given term list is empty, a selector first-term that extracts the highest-order term from a term list, and a selector rest-terms that returns all but the highest-order term. To manipulate terms, we will suppose that we have a constructor make-term that constructs a term with given order and coefficient, and selectors order and coeff that return, respectively, the order and the coefficient of the term. These operations allow us to consider both terms and term lists as data abstractions, whose concrete representations we can worry about separately. Here is the procedure that constructs the term list for the sum of two polynomials:56 56 This operation is very much like the ordered union-set operation we developed in Exercise 2.62. In fact, if we think of the terms of the polynomial as a set ordered 322 (define (add-terms L1 L2) (cond ((empty-termlist? L1) L2) ((empty-termlist? L2) L1) (else (let ((t1 (first-term L1)) (t2 (first-term L2))) (cond ((> (order t1) (order t2)) (adjoin-term t1 (add-terms (rest-terms L1) L2))) ((< (order t1) (order t2)) (adjoin-term t2 (add-terms L1 (rest-terms L2)))) (else (adjoin-term (make-term (order t1) (add (coeff t1) (coeff t2))) (add-terms (rest-terms L1) (rest-terms L2))))))))) The most important point to note here is that we used the generic addition procedure add to add together the coefficients of the terms being combined. This has powerful consequences, as we will see below. In order to multiply two term lists, we multiply each term of the first list by all the terms of the other list, repeatedly using mul-term-by-all-terms, which multiplies a given term by all terms in a given term list. The resulting term lists (one for each term of the first list) are accumulated into a sum. Multiplying two terms forms a term whose order is the sum of the orders according to the power of the indeterminate, then the program that produces the term list for a sum is almost identical to union-set. 323 of the factors and whose coefficient is the product of the coefficients of the factors: (define (mul-terms L1 L2) (if (empty-termlist? L1) (the-empty-termlist) (add-terms (mul-term-by-all-terms (first-term L1) L2) (mul-terms (rest-terms L1) L2)))) (define (mul-term-by-all-terms t1 L) (if (empty-termlist? L) (the-empty-termlist) (let ((t2 (first-term L))) (adjoin-term (make-term (+ (order t1) (order t2)) (mul (coeff t1) (coeff t2))) (mul-term-by-all-terms t1 (rest-terms L)))))) This is really all there is to polynomial addition and multiplication. Notice that, since we operate on terms using the generic procedures add and mul, our polynomial package is automatically able to handle any type of coefficient that is known about by the generic arithmetic package. If we include a coercion mechanism such as one of those discussed in Section 2.5.2, then we also are automatically able to handle operations on polynomials of different coefficient types, such as 2 [3x 2 + (2 + 3i )x + 7] · x 4 + x 2 + (5 + 3i ) 3 Because we installed the polynomial addition and multiplication procedures add-poly and mul-poly in the generic arithmetic system as the add 324 and mul operations for type polynomial, our system is also automatically able to handle polynomial operations such as [( y + 1)x 2 + ( y 2 + 1)x + ( y − 1)] · [( y − 2)x + ( y 3 + 7)] The reason is that when the system tries to combine coefficients, it will dispatch through add and mul. Since the coefficients are themselves polynomials (in y ), these will be combined using add-poly and mul-poly. The result is a kind of “data-directed recursion” in which, for example, a call to mul-poly will result in recursive calls to mul-poly in order to multiply the coefficients. If the coefficients of the coefficients were themselves polynomials (as might be used to represent polynomials in three variables), the data direction would ensure that the system would follow through another level of recursive calls, and so on through as many levels as the structure of the data dictates.57 Representing term lists Finally, we must confront the job of implementing a good representation for term lists. A term list is, in effect, a set of coefficients keyed by the order of the term. Hence, any of the methods for representing sets, as discussed in Section 2.3.3, can be applied to this task. On the other hand, our procedures add-terms and mul-terms always access term lists sequentially from 57 To make this work completely smoothly, we should also add to our generic arith- metic system the ability to coerce a “number” to a polynomial by regarding it as a polynomial of degree zero whose coefficient is the number. This is necessary if we are going to perform operations such as [x 2 + ( y + 1)x + 5] + [x 2 + 2x + 1] which requires adding the coefficient y + 1 to the coefficient 2. 325 highest to lowest order. Thus, we will use some kind of ordered list representation. How should we structure the list that represents a term list? One consideration is the “density” of the polynomials we intend to manipulate. A polynomial is said to be dense if it has nonzero coefficients in terms of most orders. If it has many zero terms it is said to be sparse. For example, x 5 + 2x 4 + 3x 2 − 2x − 5 is a dense polynomial, whereas x 100 + 2x 2 + 1 is sparse. The term lists of dense polynomials are most efficiently represented as lists of the coefficients. For example, A above would be nicely represented as (1 2 0 3 -2 -5). The order of a term in this representation is the length of the sublist beginning with that term’s coefficient, decremented by 1.58 This would be a terrible representation for a sparse polynomial such as B : There would be a giant list of zeros punctuated by a few lonely nonzero terms. A more reasonable representation of the term list of a sparse polynomial is as a list of the nonzero terms, where each term is a list containing the order of the term and the coefficient for that order. In such a scheme, polynomial B is efficiently represented as ((100 1) (2 2) (0 1)). As most polynomial manipulations are performed on sparse 58 In these polynomial examples, we assume that we have implemented the generic arithmetic system using the type mechanism suggested in Exercise 2.78. Thus, coefficients that are ordinary numbers will be represented as the numbers themselves rather than as pairs whose car is the symbol scheme-number. 326 polynomials, we will use this method. We will assume that term lists are represented as lists of terms, arranged from highest-order to lowest-order term. Once we have made this decision, implementing the selectors and constructors for terms and term lists is straightforward:59 (define (adjoin-term term term-list) (if (=zero? (coeff term)) term-list (cons term term-list))) (define (the-empty-termlist) ’()) (define (first-term term-list) (car term-list)) (define (rest-terms term-list) (cdr term-list)) (define (empty-termlist? term-list) (null? term-list)) (define (make-term order coeff) (list order coeff)) (define (order term) (car term)) (define (coeff term) (cadr term)) where =zero? is as defined in Exercise 2.80. (See also Exercise 2.87 below.) Users of the polynomial package will create (tagged) polynomials by means of the procedure: 59 Although we are assuming that term lists are ordered, we have implemented adjoin-term to simply cons the new term onto the existing term list. We can get away with this so long as we guarantee that the procedures (such as add-terms) that use adjoin-term always call it with a higher-order term than appears in the list. If we did not want to make such a guarantee, we could have implemented adjoin-term to be similar to the adjoin-set constructor for the ordered-list representation of sets (Exercise 2.61). 327 (define (make-polynomial var terms) ((get ’make ’polynomial) var terms)) Exercise 2.87: Install =zero? for polynomials in the generic arithmetic package. This will allow adjoin-term to work for polynomials with coefficients that are themselves polynomials. Exercise 2.88: Extend the polynomial system to include subtraction of polynomials. (Hint: You may find it helpful to define a generic negation operation.) Exercise 2.89: Define procedures that implement the term-list representation described above as appropriate for dense polynomials. Exercise 2.90: Suppose we want to have a polynomial system that is efficient for both sparse and dense polynomials. One way to do this is to allow both kinds of term-list representations in our system. The situation is analogous to the complex-number example of Section 2.4, where we allowed both rectangular and polar representations. To do this we must distinguish different types of term lists and make the operations on term lists generic. Redesign the polynomial system to implement this generalization. This is a major effort, not a local change. Exercise 2.91: A univariate polynomial can be divided by another one to produce a polynomial quotient and a polynomial remainder. For example, x5 − 1 3 = x + x, remainder x − 1 x2 − 1 Division can be performed via long division. That is, divide the highest-order term of the dividend by the highest-order term of the divisor. The result is the first term of the quotient. Next, multiply 328 the result by the divisor, subtract that from the dividend, and produce the rest of the answer by recursively dividing the difference by the divisor. Stop when the order of the divisor exceeds the order of the dividend and declare the dividend to be the remainder. Also, if the dividend ever becomes zero, return zero as both quotient and remainder. We can design a div-poly procedure on the model of add-poly and mul-poly. The procedure checks to see if the two polys have the same variable. If so, div-poly strips off the variable and passes the problem to div-terms, which performs the division operation on term lists. Div-poly finally reattaches the variable to the result supplied by div-terms. It is convenient to design div-terms to compute both the quotient and the remainder of a division. Div-terms can take two term lists as arguments and return a list of the quotient term list and the remainder term list. Complete the following definition of div-terms by filling in the missing expressions. Use this to implement div-poly, which takes two polys as arguments and returns a list of the quotient and remainder polys. (define (div-terms L1 L2) (if (empty-termlist? L1) (list (the-empty-termlist) (the-empty-termlist)) (let ((t1 (first-term L1)) (t2 (first-term L2))) (if (> (order t2) (order t1)) (list (the-empty-termlist) L1) (let ((new-c (div (coeff t1) (coeff t2))) (new-o (- (order t1) (order t2)))) 329 (let ((rest-of-result 〈compute rest of result recursively〉 )) 〈form complete result〉 )))))) Hierarchies of types in symbolic algebra Our polynomial system illustrates how objects of one type (polynomials) may in fact be complex objects that have objects of many different types as parts. This poses no real difficulty in defining generic operations. We need only install appropriate generic operations for performing the necessary manipulations of the parts of the compound types. In fact, we saw that polynomials form a kind of “recursive data abstraction,” in that parts of a polynomial may themselves be polynomials. Our generic operations and our data-directed programming style can handle this complication without much trouble. On the other hand, polynomial algebra is a system for which the data types cannot be naturally arranged in a tower. For instance, it is possible to have polynomials in x whose coefficients are polynomials in y . It is also possible to have polynomials in y whose coefficients are polynomials in x . Neither of these types is “above” the other in any natural way, yet it is often necessary to add together elements from each set. There are several ways to do this. One possibility is to convert one polynomial to the type of the other by expanding and rearranging terms so that both polynomials have the same principal variable. One can impose a towerlike structure on this by ordering the variables and thus always converting any polynomial to a “canonical form” with the highest-priority variable dominant and the lower-priority variables buried in the coefficients. This strategy works fairly 330 well, except that the conversion may expand a polynomial unnecessarily, making it hard to read and perhaps less efficient to work with. The tower strategy is certainly not natural for this domain or for any domain where the user can invent new types dynamically using old types in various combining forms, such as trigonometric functions, power series, and integrals. It should not be surprising that controlling coercion is a serious problem in the design of large-scale algebraic-manipulation systems. Much of the complexity of such systems is concerned with relationships among diverse types. Indeed, it is fair to say that we do not yet completely understand coercion. In fact, we do not yet completely understand the concept of a data type. Nevertheless, what we know provides us with powerful structuring and modularity principles to support the design of large systems. Exercise 2.92: By imposing an ordering on variables, extend the polynomial package so that addition and multiplication of polynomials works for polynomials in different variables. (This is not easy!) Extended exercise: Rational functions We can extend our generic arithmetic system to include rational functions. These are “fractions” whose numerator and denominator are polynomials, such as x +1 x3 − 1 The system should be able to add, subtract, multiply, and divide rational functions, and to perform such computations as x +1 x x 3 + 2x 2 + 3x + 1 + = x3 − 1 x2 − 1 x4 + x3 − x − 1 331 (Here the sum has been simplified by removing common factors. Ordinary “cross multiplication” would have produced a fourth-degree polynomial over a fifth-degree polynomial.) If we modify our rational-arithmetic package so that it uses generic operations, then it will do what we want, except for the problem of reducing fractions to lowest terms. Exercise 2.93: Modify the rational-arithmetic package to use generic operations, but change make-rat so that it does not attempt to reduce fractions to lowest terms. Test your system by calling make-rational on two polynomials to produce a rational function (define p1 (make-polynomial ’x ’((2 1)(0 1)))) (define p2 (make-polynomial ’x ’((3 1)(0 1)))) (define rf (make-rational p2 p1)) Now add rf to itself, using add. You will observe that this addition procedure does not reduce fractions to lowest terms. We can reduce polynomial fractions to lowest terms using the same idea we used with integers: modifying make-rat to divide both the numerator and the denominator by their greatest common divisor. The notion of “greatest common divisor” makes sense for polynomials. In fact, we can compute the GCD of two polynomials using 332 essentially the same Euclid’s Algorithm that works for integers.60 The integer version is (define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) Using this, we could make the obvious modification to define a GCD operation that works on term lists: (define (gcd-terms a b) (if (empty-termlist? b) a (gcd-terms b (remainder-terms a b)))) where remainder-terms picks out the remainder component of the list returned by the term-list division operation div-terms that was implemented in Exercise 2.91. Exercise 2.94: Using div-terms, implement the procedure remainder-terms and use this to define gcd-terms as above. Now 60 The fact that Euclid’s Algorithm works for polynomials is formalized in algebra by saying that polynomials form a kind of algebraic domain called a Euclidean ring. A Euclidean ring is a domain that admits addition, subtraction, and commutative multiplication, together with a way of assigning to each element x of the ring a positive integer “measure” m (x ) with the properties that m (x y ) ≥ m (x ) for any nonzero x and y and that, given any x and y , there exists a q such that y = q x + r and either r = 0 or m (r ) < m (x ). From an abstract point of view, this is what is needed to prove that Euclid’s Algorithm works. For the domain of integers, the measure m of an integer is the absolute value of the integer itself. For the domain of polynomials, the measure of a polynomial is its degree. 333 write a procedure gcd-poly that computes the polynomial GCD of two polys. (The procedure should signal an error if the two polys are not in the same variable.) Install in the system a generic operation greatest-common-divisor that reduces to gcd-poly for polynomials and to ordinary gcd for ordinary numbers. As a test, try (define p1 (make-polynomial ’x ’((4 1) (3 -1) (2 -2) (1 2)))) (define p2 (make-polynomial ’x ’((3 1) (1 -1)))) (greatest-common-divisor p1 p2) and check your result by hand. Exercise 2.95: Define P 1 , P 2 , and P 3 to be the polynomials P1 : x 2 − 2x + 1 P2 : 11x 2 + 7 P 3 : 13x + 5 Now define Q 1 to be the product of P 1 and P 2 and Q 2 to be the product of P 1 and P 3 , and use greatest-common-divisor (Exercise 2.94) to compute the GCD of Q 1 and Q 2 . Note that the answer is not the same as P 1 . This example introduces noninteger operations into the computation, causing difficulties with the GCD algorithm.61 To understand what is happening, try tracing gcd-terms while computing the GCD or try performing the division by hand. 61 In an implementation like MIT Scheme, this produces a polynomial that is indeed a divisor of Q 1 and Q 2 , but with rational coefficients. In many other Scheme systems, in which division of integers can produce limited-precision decimal numbers, we may fail to get a valid divisor. 334 We can solve the problem exhibited in Exercise 2.95 if we use the following modification of the GCD algorithm (which really works only in the case of polynomials with integer coefficients). Before performing any polynomial division in the GCD computation, we multiply the dividend by an integer constant factor, chosen to guarantee that no fractions will arise during the division process. Our answer will thus differ from the actual GCD by an integer constant factor, but this does not matter in the case of reducing rational functions to lowest terms; the GCD will be used to divide both the numerator and denominator, so the integer constant factor will cancel out. More precisely, if P and Q are polynomials, let O 1 be the order of P (i.e., the order of the largest term of P ) and let O 2 be the order of Q . Let c be the leading coefficient of Q . Then it can be shown that, if we multiply P by the integerizing factor c 1+O 1 −O 2 , the resulting polynomial can be divided by Q by using the div-terms algorithm without introducing any fractions. The operation of multiplying the dividend by this constant and then dividing is sometimes called the pseudodivision of P by Q . The remainder of the division is called the pseudoremainder. Exercise 2.96: a. Implement the procedure pseudoremainder-terms, which is just like remainder-terms except that it multiplies the dividend by the integerizing factor described above before calling div-terms. Modify gcd-terms to use pseudoremainder-terms, and verify that greatest-common-divisor now produces an 335 answer with integer coefficients on the example in Exercise 2.95. b. The GCD now has integer coefficients, but they are larger than those of P 1 . Modify gcd-terms so that it removes common factors from the coefficients of the answer by dividing all the coefficients by their (integer) greatest common divisor. Thus, here is how to reduce a rational function to lowest terms: • Compute the GCD of the numerator and denominator, using the version of gcd-terms from Exercise 2.96. • When you obtain the GCD, multiply both numerator and denominator by the same integerizing factor before dividing through by the GCD, so that division by the GCD will not introduce any noninteger coefficients. As the factor you can use the leading coefficient of the GCD raised to the power 1 + O 1 − O 2 , where O 2 is the order of the GCD and O 1 is the maximum of the orders of the numerator and denominator. This will ensure that dividing the numerator and denominator by the GCD will not introduce any fractions. • The result of this operation will be a numerator and denominator with integer coefficients. The coefficients will normally be very large because of all of the integerizing factors, so the last step is to remove the redundant factors by computing the (integer) greatest common divisor of all the coefficients of the numerator and the denominator and dividing through by this factor. 336 Exercise 2.97: a. Implement this algorithm as a procedure reduce-terms that takes two term lists n and d as arguments and returns a list nn, dd, which are n and d reduced to lowest terms via the algorithm given above. Also write a procedure reduce-poly, analogous to add-poly, that checks to see if the two polys have the same variable. If so, reduce-poly strips off the variable and passes the problem to reduce-terms, then reattaches the variable to the two term lists supplied by reduce-terms. b. Define a procedure analogous to reduce-terms that does what the original make-rat did for integers: (define (reduce-integers n d) (let ((g (gcd n d))) (list (/ n g) (/ d g)))) and define reduce as a generic operation that calls applygeneric to dispatch to either reduce-poly (for polynomial arguments) or reduce-integers (for scheme-number arguments). You can now easily make the rational-arithmetic package reduce fractions to lowest terms by having make-rat call reduce before combining the given numerator and denominator to form a rational number. The system now handles rational expressions in either integers or polynomials. To test your program, try the example at the beginning of this extended exercise: (define p1 (make-polynomial ’x ’((1 1)(0 1)))) (define p2 (make-polynomial ’x ’((3 1)(0 -1)))) (define p3 (make-polynomial ’x ’((1 1)))) 337 (define p4 (make-polynomial ’x ’((2 1)(0 -1)))) (define rf1 (make-rational p1 p2)) (define rf2 (make-rational p3 p4)) (add rf1 rf2) See if you get the correct answer, correctly reduced to lowest terms. The GCD computation is at the heart of any system that does operations on rational functions. The algorithm used above, although mathematically straightforward, is extremely slow. The slowness is due partly to the large number of division operations and partly to the enormous size of the intermediate coefficients generated by the pseudodivisions. One of the active areas in the development of algebraic-manipulation systems is the design of better algorithms for computing polynomial GCDs.62 62 One extremely efficient and elegant method for computing polynomial GCDs was discovered by Richard Zippel (1979). The method is a probabilistic algorithm, as is the fast test for primality that we discussed in Chapter 1. Zippel’s book (Zippel 1993) describes this method, together with other ways to compute polynomial GCDs. 3 Modularity, Objects and State ´ ` M²ταβαλλoν αναπα υ´ ²ται (Even while it changes, it stands still.) —Heraclitus Plus c¸a change, plus c’est la mˆ eme chose. —Alphonse Karr The preceding chapters introduced the basic elements from which programs are made. We saw how primitive procedures and primitive data are combined to construct compound entities, and we learned that abstraction is vital in helping us to cope with the complexity of large systems. But these tools are not sufficient for designing programs. Effective program synthesis also requires organizational principles that can guide us in formulating the overall design of a program. In particular, we need strategies to help us structure large systems so that they will be modular, that is, so that they can be divided “naturally” into coherent parts that can be separately developed and maintained. One powerful design strategy, which is particularly appropriate to the construction of programs for modeling physical systems, is to base the structure of our programs on the structure of the system being modeled. For each object in the system, we construct a corresponding computational object. For each system action, we define a symbolic operation in our computational model. Our hope in using this strategy is that extending the model to accommodate new objects or new actions will require no strategic changes to the program, only the addition of the new symbolic analogs of those objects or actions. If we have been successful in our sys- 339 tem organization, then to add a new feature or debug an old one we will have to work on only a localized part of the system. To a large extent, then, the way we organize a large program is dictated by our perception of the system to be modeled. In this chapter we will investigate two prominent organizational strategies arising from two rather different “world views” of the structure of systems. The first organizational strategy concentrates on objects, viewing a large system as a collection of distinct objects whose behaviors may change over time. An alternative organizational strategy concentrates on the streams of information that flow in the system, much as an electrical engineer views a signal-processing system. Both the object-based approach and the stream-processing approach raise significant linguistic issues in programming. With objects, we must be concerned with how a computational object can change and yet maintain its identity. This will force us to abandon our old substitution model of computation (Section 1.1.5) in favor of a more mechanistic but less theoretically tractable environment model of computation. The difficulties of dealing with objects, change, and identity are a fundamental consequence of the need to grapple with time in our computational models. These difficulties become even greater when we allow the possibility of concurrent execution of programs. The stream approach can be most fully exploited when we decouple simulated time in our model from the order of the events that take place in the computer during evaluation. We will accomplish this using a technique known as delayed evaluation. 3.1 Assignment and Local State We ordinarily view the world as populated by independent objects, each of which has a state that changes over time. An object is said to “have state” if its behavior is influenced by its history. A bank account, for example, has state in that the answer to the question “Can I withdraw $100?” depends upon the history of deposit and withdrawal transactions. We can characterize an object’s state by one or more state variables, which among them maintain enough information about history to determine the object’s current behavior. In a simple banking system, we could characterize the state of an account by a current balance rather than by remembering the entire history of account transactions. In a system composed of many objects, the objects are rarely completely independent. Each may influence the states of others through interactions, which serve to couple the state variables of one object to those of other objects. Indeed, the view that a system is composed of separate objects is most useful when the state variables of the system can be grouped into closely coupled subsystems that are only loosely coupled to other subsystems. This view of a system can be a powerful framework for organizing computational models of the system. For such a model to be modular, it should be decomposed into computational objects that model the actual objects in the system. Each computational object must have its own local state variables describing the actual object’s state. Since the states of objects in the system being modeled change over time, the state variables of the corresponding computational objects must also change. If we choose to model the flow of time in the system by the elapsed time in the computer, then we must have a way to construct computational objects whose behav- 341 iors change as our programs run. In particular, if we wish to model state variables by ordinary symbolic names in the programming language, then the language must provide an assignment operator to enable us to change the value associated with a name. 3.1.1 Local State Variables To illustrate what we mean by having a computational object with timevarying state, let us model the situation of withdrawing money from a bank account. We will do this using a procedure withdraw, which takes as argument an amount to be withdrawn. If there is enough money in the account to accommodate the withdrawal, then withdraw should return the balance remaining after the withdrawal. Otherwise, withdraw should return the message Insufficient funds. For example, if we begin with $100 in the account, we should obtain the following sequence of responses using withdraw: (withdraw 25) 75 (withdraw 25) 50 (withdraw 60) "Insufficient funds" (withdraw 15) 35 342 Observe that the expression (withdraw 25), evaluated twice, yields different values. This is a new kind of behavior for a procedure. Until now, all our procedures could be viewed as specifications for computing mathematical functions. A call to a procedure computed the value of the function applied to the given arguments, and two calls to the same procedure with the same arguments always produced the same result.1 To implement withdraw, we can use a variable balance to indicate the balance of money in the account and define withdraw as a procedure that accesses balance. The withdraw procedure checks to see if balance is at least as large as the requested amount. If so, withdraw decrements balance by amount and returns the new value of balance. Otherwise, withdraw returns the Insufficient funds message. Here are the definitions of balance and withdraw: (define balance 100) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) Decrementing balance is accomplished by the expression (set! balance (- balance amount)) 1 Actually, this is not quite true. One exception was the random-number generator in Section 1.2.6. Another exception involved the operation/type tables we introduced in Section 2.4.3, where the values of two calls to get with the same arguments depended on intervening calls to put. On the other hand, until we introduce assignment, we have no way to create such procedures ourselves. 343 This uses the set! special form, whose syntax is (set! 〈name〉〈new-value〉) Here 〈name 〉 is a symbol and 〈new-value 〉 is any expression. changes 〈name 〉 so that its value is the result obtained by evaluating 〈new-value 〉. In the case at hand, we are changing balance so that its new value will be the result of subtracting amount from the previous value of balance.2 Withdraw also uses the begin special form to cause two expressions to be evaluated in the case where the if test is true: first decrementing balance and then returning the value of balance. In general, evaluating the expression (begin 〈exp 1 〉〈exp 2 〉 ... 〈exp k 〉) causes the expressions 〈exp 1 〉 through 〈exp k 〉 to be evaluated in sequence and the value of the final expression 〈exp k 〉 to be returned as the value of the entire begin form.3 Although withdraw works as desired, the variable balance presents a problem. As specified above, balance is a name defined in the global envi2 The value of a set! expression is implementation-dependent. Set! should be used only for its effect, not for its value. The name set! reflects a naming convention used in Scheme: Operations that change the values of variables (or that change data structures, as we will see in Section 3.3) are given names that end with an exclamation point. This is similar to the convention of designating predicates by names that end with a question mark. 3 We have already used begin implicitly in our programs, because in Scheme the body of a procedure can be a sequence of expressions. Also, the 〈consequent 〉 part of each clause in a cond expression can be a sequence of expressions rather than a single expression. 344 ronment and is freely accessible to be examined or modified by any procedure. It would be much better if we could somehow make balance internal to withdraw, so that withdraw would be the only procedure that could access balance directly and any other procedure could access balance only indirectly (through calls to withdraw). This would more accurately model the notion that balance is a local state variable used by withdraw to keep track of the state of the account. We can make balance internal to withdraw by rewriting the definition as follows: (define new-withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))) What we have done here is use let to establish an environment with a local variable balance, bound to the initial value 100. Within this local environment, we use lambda to create a procedure that takes amount as an argument and behaves like our previous withdraw procedure. This procedure— returned as the result of evaluating the let expression—is new-withdraw, which behaves in precisely the same way as withdraw but whose variable balance is not accessible by any other procedure.4 4 In programming-language jargon, the variable balance is said to be encapsulated within the new-withdraw procedure. Encapsulation reflects the general systemdesign principle known as the hiding principle: One can make a system more modular and robust by protecting parts of the system from each other; that is, by pro- 345 Combining set! with local variables is the general programming technique we will use for constructing computational objects with local state. Unfortunately, using this technique raises a serious problem: When we first introduced procedures, we also introduced the substitution model of evaluation (Section 1.1.5) to provide an interpretation of what procedure application means. We said that applying a procedure should be interpreted as evaluating the body of the procedure with the formal parameters replaced by their values. The trouble is that, as soon as we introduce assignment into our language, substitution is no longer an adequate model of procedure application. (We will see why this is so in Section 3.1.3.) As a consequence, we technically have at this point no way to understand why the new-withdraw procedure behaves as claimed above. In order to really understand a procedure such as new-withdraw, we will need to develop a new model of procedure application. In Section 3.2 we will introduce such a model, together with an explanation of set! and local variables. First, however, we examine some variations on the theme established by new-withdraw. The following procedure, make-withdraw, creates “withdrawal processors.” The formal parameter balance in make-withdraw specifies the initial amount of money in the account.5 (define (make-withdraw balance) viding information access only to those parts of the system that have a “need to know.” 5 In contrast with new-withdraw above, we do not have to use let to make balance a local variable, since formal parameters are already local. This will be clearer after the discussion of the environment model of evaluation in Section 3.2. (See also Exercise 3.10.) 346 (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) Make-withdraw can be used as follows to create two objects W1 and W2: (define W1 (make-withdraw 100)) (define W2 (make-withdraw 100)) (W1 50) 50 (W2 70) 30 (W2 40) "Insufficient funds" (W1 40) 10 Observe that W1 and W2 are completely independent objects, each with its own local state variable balance. Withdrawals from one do not affect the other. We can also create objects that handle deposits as well as withdrawals, and thus we can represent simple bank accounts. Here is a procedure that returns a “bank-account object” with a specified initial balance: (define (make-account 347 (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m) (cond ((eq? m ’withdraw) withdraw) ((eq? m ’deposit) deposit) (else (error "Unknown request - MAKE-ACCOUNT" m)))) dispatch) Each call to make-account sets up an environment with a local state variable balance. Within this environment, make-account defines procedures deposit and withdraw that access balance and an additional procedure dispatch that takes a “message” as input and returns one of the two lo- cal procedures. The dispatch procedure itself is returned as the value that represents the bank-account object. This is precisely the message-passing style of programming that we saw in Section 2.4.3, although here we are using it in conjunction with the ability to modify local variables. Make-account can be used as follows: (define acc (make-account 100)) ((acc ’withdraw) 50) 50 ((acc ’withdraw) 60) "Insufficient funds" ((acc ’deposit) 40) 90 ((acc ’withdraw) 60) 30 Each call to acc returns the locally defined deposit or withdraw procedure, which is then applied to the specified amount. As was the case with make-withdraw, another call to make-account (define acc2 (make-account 100)) will produce a completely separate account object, which maintains its own local balance. Exercise 3.1: An accumulator is a procedure that is called repeatedly with a single numeric argument and accumulates its arguments into a sum. Each time it is called, it returns the currently accumulated sum. Write a procedure make-accumulator that generates accumulators, each maintaining an independent sum. The input to make-accumulator should specify the initial value of the sum; for example (define A (make-accumulator 5)) (A 10) 15 349 (A 10) 25 Exercise 3.2: In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure make-monitored that takes as input a procedure, f, that itself takes one input. The result returned by make-monitored is a third procedure, say mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to mf is the special symbol how-many-calls?, then mf returns the value of the counter. If the input is the special symbol reset-count, then mf resets the counter to zero. For any other input, mf returns the result of calling f on that input and increments the counter. For instance, we could make a monitored version of the sqrt procedure: (define s (make-monitored sqrt)) (s 100) 10 (s ’how-many-calls?) 1 Exercise 3.3: Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in (define acc (make-account 100 ’secret-password)) 350 The resulting account object should process a request only if it is accompanied by the password with which the account was created, and should otherwise return a complaint: ((acc ’secret-password ’withdraw) 40) 60 ((acc ’some-other-password ’deposit) 50) "Incorrect password" Exercise 3.4: Modify the make-account procedure of Exercise 3.3 by adding another local state variable so that, if an account is accessed more than seven consecutive times with an incorrect password, it invokes the procedure call-the-cops. 3.1.2 The Benefits of Introducing Assignment As we shall see, introducing assignment into our programming language leads us into a thicket of difficult conceptual issues. Nevertheless, viewing systems as collections of objects with local state is a powerful technique for maintaining a modular design. As a simple example, consider the design of a procedure rand that, whenever it is called, returns an integer chosen at random. It is not at all clear what is meant by “chosen at random.” What we presumably want is for successive calls to rand to produce a sequence of numbers that has statistical properties of uniform distribution. We will not discuss methods for generating suitable sequences here. Rather, let us assume that we have a procedure rand-update that has the property that if we start with a given number x 1 and form x 2 = (rand-update x 1 ) x 3 = (rand-update x 2 ) then the sequence of values x 1 , x 2 , x 3 , . . . , will have the desired statistical properties.6 We can implement rand as a procedure with a local state variable x that is initialized to some fixed value random-init. Each call to rand computes rand-update of the current value of x, returns this as the random number, and also stores this as the new value of x. (define rand (let ((x random-init)) (lambda () (set! x (rand-update x)) x))) Of course, we could generate the same sequence of random numbers without using assignment by simply calling rand-update directly. However, this would mean that any part of our program that used random 6 One common way to implement rand-update is to use the rule that x is updated to ax + b modulo m , where a , b , and m are appropriately chosen integers. Chapter 3 of Knuth 1981 includes an extensive discussion of techniques for generating sequences of random numbers and establishing their statistical properties. Notice that the rand-update procedure computes a mathematical function: Given the same input twice, it produces the same output. Therefore, the number sequence produced by rand-update certainly is not “random,” if by “random” we insist that each number in the sequence is unrelated to the preceding number. The relation between “real randomness” and so-called pseudo-random sequences, which are produced by well-determined computations and yet have suitable statistical properties, is a complex question involving difficult issues in mathematics and philosophy. Kolmogorov, Solomonoff, and Chaitin have made great progress in clarifying these issues; a discussion can be found in Chaitin 1975. 352 numbers would have to explicitly remember the current value of x to be passed as an argument to rand-update. To realize what an annoyance this would be, consider using random numbers to implement a technique called Monte Carlo simulation. The Monte Carlo method consists of choosing sample experiments at random from a large set and then making deductions on the basis of the probabilities estimated from tabulating the results of those experiments. For example, we can approximate π using the fact that 6/π2 is the probability that two integers chosen at random will have no factors in common; that is, that their greatest common divisor will be 1.7 To obtain the approximation to π, we perform a large number of experiments. In each experiment we choose two integers at random and perform a test to see if their GCD is 1. The fraction of times that the test is passed gives us our estimate of 6/π2 , and from this we obtain our approximation to π. The heart of our program is a procedure monte-carlo, which takes as arguments the number of times to try an experiment, together with the experiment, represented as a no-argument procedure that will return either true or false each time it is run. Monte-carlo runs the experiment for the designated number of trials and returns a number telling the fraction of the trials in which the experiment was found to be true. (define (estimate-pi trials) (sqrt (/ 6 (monte-carlo trials cesaro-test)))) (define (cesaro-test) (= (gcd (rand) (rand)) 1)) 7 This theorem is due to E. Ces` aro. See section 4.5.2 of Knuth 1981 for a and a proof. (define (monte-carlo trials experiment) (define (iter trials-remaining trials-passed) (cond ((= trials-remaining 0) (/ trials-passed trials)) ((experiment) (iter (- trials-remaining 1) (+ trials-passed 1))) (else (iter (- trials-remaining 1) trials-passed)))) (iter trials 0)) Now let us try the same computation using rand-update directly rather than rand, the way we would be forced to proceed if we did not use assignment to model local state: (define (estimate-pi trials) (sqrt (/ 6 (random-gcd-test trials random-init)))) (define (random-gcd-test trials initial-x) (define (iter trials-remaining trials-passed x) (let ((x1 (rand-update x))) (let ((x2 (rand-update x1))) (cond ((= trials-remaining 0) (/ trials-passed trials)) ((= (gcd x1 x2) 1) (iter (- trials-remaining 1) (+ trials-passed 1) 354 x2)) (else (iter (- trials-remaining 1) trials-passed x2)))))) (iter trials 0 initial-x)) While the program is still simple, it betrays some painful breaches of modularity. In our first version of the program, using rand, we can express the Monte Carlo method directly as a general monte-carlo procedure that takes as an argument an arbitrary experiment procedure. In our second version of the program, with no local state for the random-number generator, random-gcd-test must explicitly manipulate the random numbers x1 and x2 and recycle x2 through the iterative loop as the new input to rand-update. This explicit handling of the random numbers intertwines the structure of accumulating test results with the fact that our particular experiment uses two random numbers, whereas other Monte Carlo experiments might use one random number or three. Even the top-level procedure estimate-pi has to be concerned with supplying an initial random number. The fact that the random-number generator’s insides are leaking out into other parts of the program makes it difficult for us to isolate the Monte Carlo idea so that it can be applied to other tasks. In the first version of the program, assignment encapsulates the state of the random-number generator within the rand procedure, so that the details of random-number generation remain independent of the rest of the program. The general phenomenon illustrated by the Monte Carlo example is this: From the point of view of one part of a complex process, the other parts appear to change with time. They have hidden time-varying local state. If we 355 wish to write computer programs whose structure reflects this decomposition, we make computational objects (such as bank accounts and randomnumber generators) whose behavior changes with time. We model state with local state variables, and we model the changes of state with assignments to those variables. It is tempting to conclude this discussion by saying that, by introducing assignment and the technique of hiding state in local variables, we are able to structure systems in a more modular fashion than if all state had to be manipulated explicitly, by passing additional parameters. Unfortunately, as we shall see, the story is not so simple. Exercise 3.5: Monte Carlo integration is a method of estimating definite integrals by means of Monte Carlo simulation. Consider computing the area of a region of space described by a predicate P (x, y ) that is true for points (x, y ) in the region and false for points not in the region. For example, the region contained within a circle of radius 3 centered at (5, 7) is described by the predicate that tests whether (x − 5)2 + ( y − 7)2 ≤ 32 . To estimate the area of the region described by such a predicate, begin by choosing a rectangle that contains the region. For example, a rectangle with diagonally opposite corners at (2, 4) and (8, 10) contains the circle above. The desired integral is the area of that portion of the rectangle that lies in the region. We can estimate the integral by picking, at random, points (x, y ) that lie in the rectangle, and testing P (x, y ) for each point to determine whether the point lies in the region. If we try this with many points, then the fraction of points that fall in the region should give an estimate of the proportion of the rectangle 356 that lies in the region. Hence, multiplying this fraction by the area of the entire rectangle should produce an estimate of the integral. Implement Monte Carlo integration as a procedure estimateintegral that takes as arguments a predicate P, upper and lower bounds x1, x2, y1, and y2 for the rectangle, and the number of trials to perform in order to produce the estimate. Your procedure should use the same monte-carlo procedure that was used above to estimate π. Use your estimate-integral to produce an estimate of π by measuring the area of a unit circle. You will find it useful to have a procedure that returns a number chosen at random from a given range. The following random-inrange procedure implements this in terms of the random proce- dure used in Section 1.2.6, which returns a nonnegative number less than its input.8 (define (random-in-range low high) (let ((range (- high low))) (+ low (random range)))) Exercise 3.6: It is useful to be able to reset a random-number generator to produce a sequence starting from a given value. Design a new rand procedure that is called with an argument that is either the symbol generate or the symbol reset and behaves as follows: (rand ’generate) produces a new random number; ((rand ’reset) 〈new-value 〉) resets the internal state variable to the designated 〈 new-value 〉. Thus, by resetting the state, one can generate 8 MIT Scheme provides such a procedure. If random is given an exact integer (as in Section 1.2.6) it returns an exact integer, but if it is given a decimal value (as in this exercise) it returns a decimal value. 357 repeatable sequences. These are very handy to have when testing and debugging programs that use random numbers. 3.1.3 The Costs of Introducing Assignment As we have seen, the set! operation enables us to model objects that have local state. However, this advantage comes at a price. Our programming language can no longer be interpreted in terms of the substitution model of procedure application that we introduced in Section 1.1.5. Moreover, no simple model with “nice” mathematical properties can be an adequate framework for dealing with objects and assignment in programming languages. So long as we do not use assignments, two evaluations of the same procedure with the same arguments will produce the same result, so that procedures can be viewed as computing mathematical functions. Programming without any use of assignments, as we did throughout the first two chapters of this book, is accordingly known as functional programming. To understand how assignment complicates matters, consider a simplified version of the make-withdraw procedure of Section 3.1.1 that does not bother to check for an insufficient amount: (define (make-simplified-withdraw balance) (lambda (amount) (set! balance (- balance amount)) balance)) (define W (make-simplified-withdraw 25)) (W 20) 358 5 (W 10) - 5 Compare this procedure with the following make-decrementer procedure, which does not use set!: (define (make-decrementer balance) (lambda (amount) (- balance amount))) Make-decrementer returns a procedure that subtracts its input from a designated amount balance, but there is no accumulated effect over successive calls, as with make-simplified-withdraw: (define D (make-decrementer 25)) (D 20) 5 (D 10) 15 We can use the substitution model to explain how make-decrementer works. For instance, let us analyze the evaluation of the expression ((make-decrementer 25) 20) We first simplify the operator of the combination by substituting 25 for balance in the body of make-decrementer. This reduces the expression to ((lambda (amount) (- 25 amount)) 20) 359 Now we apply the operator by substituting 20 for amount in the body of the lambda expression: (- 25 20) The final answer is 5. Observe, however, what happens if we attempt a similar substitution analysis with make-simplified-withdraw: ((make-simplified-withdraw 25) 20) We first simplify the operator by substituting 25 for balance in the body of make-simplified-withdraw. This reduces the expression to9 ((lambda (amount) (set! balance (- 25 amount)) 25) 20) Now we apply the operator by substituting 20 for amount in the body of the lambda expression: (set! balance (- 25 20)) 25 If we adhered to the substitution model, we would have to say that the meaning of the procedure application is to first set balance to 5 and then return 25 as the value of the expression. This gets the wrong answer. In order to get the correct answer, we would have to somehow distinguish the first occurrence of balance (before the effect of the set!) from the second occurrence of balance (after the effect of the set!), and the substitution model cannot do this. The trouble here is that substitution is based ultimately on the notion that the symbols in our language are essentially names for values. But as soon as we introduce set! and the idea that the value of a variable can 9 We don’t substitute for the occurrence of balance in the set! expression because the 〈name 〉 in a set! is not evaluated. If we did substitute for it, we would get (set! 25 (- 25 amount)), which makes no sense. 360 change, a variable can no longer be simply a name. Now a variable somehow refers to a place where a value can be stored, and the value stored at this place can change. In Section 3.2 we will see how environments play this role of “place” in our computational model. Sameness and change The issue surfacing here is more profound than the mere breakdown of a particular model of computation. As soon as we introduce change into our computational models, many notions that were previously straightforward become problematical. Consider the concept of two things being “the same.” Suppose we call make-decrementer twice with the same argument to create two procedures: (define D1 (make-decrementer 25)) (define D2 (make-decrementer 25)) Are D1 and D2 the same? An acceptable answer is yes, because D1 and D2 have the same computational behavior—each is a procedure that subtracts its input from 25. In fact, D1 could be substituted for D2 in any computation without changing the result. Contrast this with making two calls to make-simplified-withdraw: (define W1 (make-simplified-withdraw 25)) (define W2 (make-simplified-withdraw 25)) Are W1 and W2 the same? Surely not, because calls to W1 and W2 have distinct effects, as shown by the following sequence of interactions: (W1 20) 361 5 (W1 20) - 15 (W2 20) 5 Even though W1 and W2 are “equal” in the sense that they are both created by evaluating the same expression, (make-simplified-withdraw 25), it is not true that W1 could be substituted for W2 in any expression without changing the result of evaluating the expression. A language that supports the concept that “equals can be substituted for equals” in an expresssion without changing the value of the expression is said to be referentially transparent. Referential transparency is violated when we include set! in our computer language. This makes it tricky to determine when we can simplify expressions by substituting equivalent expressions. Consequently, reasoning about programs that use assignment becomes drastically more difficult. Once we forgo referential transparency, the notion of what it means for computational objects to be “the same” becomes difficult to capture in a formal way. Indeed, the meaning of “same” in the real world that our programs model is hardly clear in itself. In general, we can determine that two apparently identical objects are indeed “the same one” only by modifying one object and then observing whether the other object has changed in the same way. But how can we tell if an object has “changed” other than by observing the “same” object twice and seeing whether some property of the object differs from one observation to the next? Thus, we cannot deter- 362 mine “change” without some a priori notion of “sameness,” and we cannot determine sameness without observing the effects of change. As an example of how this issue arises in programming, consider the situation where Peter and Paul have a bank account with $100 in it. There is a substantial difference between modeling this as (define peter-acc (make-account 100)) (define paul-acc (make-account and modeling it as (define peter-acc (make-account 100)) (define paul-acc peter-acc) In the first situation, the two bank accounts are distinct. Transactions made by Peter will not affect Paul’s account, and vice versa. In the second situation, however, we have defined paul-acc to be the same thing as peter-acc. In effect, Peter and Paul now have a joint bank account, and if Peter makes a withdrawal from peter-acc Paul will observe less money in paul-acc. These two similar but distinct situations can cause confusion in building computational models. With the shared account, in particular, it can be especially confusing that there is one object (the bank account) that has two different names (peter-acc and paul-acc); if we are searching for all the places in our program where paul-acc can be changed, we must remember to look also at things that change peter-acc.10 10 The phenomenon of a single computational object being accessed by more than one name is known as aliasing. The joint bank account situation illustrates a very simple example of an alias. In Section 3.3 we will see much more complex examples, such as “distinct” compound data structures that share parts. Bugs can occur in our programs if we forget that a change to an object may also, as a “side effect,” change a “different” object because the two “different” objects are actually a sin- 363 With reference to the above remarks on “sameness” and “change,” observe that if Peter and Paul could only examine their bank balances, and could not perform operations that changed the balance, then the issue of whether the two accounts are distinct would be moot. In general, so long as we never modify data objects, we can regard a compound data object to be precisely the totality of its pieces. For example, a rational number is determined by giving its numerator and its denominator. But this view is no longer valid in the presence of change, where a compound data object has an “identity” that is something different from the pieces of which it is composed. A bank account is still “the same” bank account even if we change the balance by making a withdrawal; conversely, we could have two different bank accounts with the same state information. This complication is a consequence, not of our programming language, but of our perception of a bank account as an object. We do not, for example, ordinarily regard a rational number as a changeable object with identity, such that we could change the numerator and still have “the same” rational number. Pitfalls of imperative programming In contrast to functional programming, programming that makes extensive use of assignment is known as imperative programming. In addition to raising complications about computational models, programs written in imperative style are susceptible to bugs that cannot occur in functional gle object appearing under different aliases. These so-called side-effect bugs are so difficult to locate and to analyze that some people have proposed that programming languages be designed in such a way as to not allow side effects or aliasing (Lampson et al. 1981; Morris et al. 1980). 364 programs. For example, recall the iterative factorial program from Section 1.2.1: (define (factorial n) (define (iter product counter) (if (> counter n) product (iter (* counter product) (+ counter 1)))) (iter 1 1)) Instead of passing arguments in the internal iterative loop, we could adopt a more imperative style by using explicit assignment to update the values of the variables product and counter: (define (factorial n) (let ((product 1) (counter 1)) (define (iter) (if (> counter n) product (begin (set! product (* counter product)) (set! counter (+ counter 1)) (iter)))) (iter))) This does not change the results produced by the program, but it does introduce a subtle trap. How do we decide the order of the assignments? As it happens, the program is correct as written. But writing the assignments in the opposite order (set! counter (+ counter 1)) 365 (set! product (* counter product)) would have produced a different, incorrect result. In general, programming with assignment forces us to carefully consider the relative orders of the assignments to make sure that each statement is using the correct version of the variables that have been changed. This issue simply does not arise in functional programs.11 The complexity of imperative programs becomes even worse if we consider applications in which several processes execute concurrently. We will return to this in Section 3.4. First, however, we will address the issue of providing a computational model for expressions that involve assignment, and explore the uses of objects with local state in designing simulations. Consider the bank account objects created by make-account, with the password modification described in Exercise 3.3. Suppose that our banking system requires the ability Exercise 3.7: to make joint accounts. Define a procedure make-joint that accomplishes this. Make-joint should take three arguments. The first is a password-protected account. The second argument must match the password with which the account was defined in order for the make-joint operation to proceed. The third argument 11 In view of this, it is ironic that introductory programming is most often taught in a highly imperative style. This may be a vestige of a belief, common throughout the 1960s and 1970s, that programs that call procedures must inherently be less efficient than programs that perform assignments. (Steele 1977 debunks this argument.) Alternatively it may reflect a view that step-by-step assignment is easier for beginners to visualize than procedure call. Whatever the reason, it often saddles beginning programmers with “should I set this variable before or after that one” concerns that can complicate programming and obscure the important ideas. 366 is a new password. Make-joint is to create an additional access to the original account using the new password. For example, if peter-acc is a bank account with password open-sesame, then (define paul-acc (make-joint peter-acc ’open-sesame ’rosebud)) will allow one to make transactions on peter-acc using the name paul-acc and the password rosebud. You may wish to modify your solution to Exercise 3.3 to accommodate this new feature Exercise 3.8: When we defined the evaluation model in Section 1.1.3, we said that the first step in evaluating an expression is to evaluate its subexpressions. But we never specified the order in which the subexpressions should be evaluated (e.g., left to right or right to left). When we introduce assignment, the order in which the arguments to a procedure are evaluated can make a difference to the result. Define a simple procedure f such that evaluating (+ (f 0) (f 1)) will return 0 if the arguments to + are evaluated from left to right but will return 1 if the arguments are evaluated from right to left. 3.2 The Environment Model of Evaluation When we introduced compound procedures in Chapter 1, we used the substitution model of evaluation (Section 1.1.5) to define what is meant by applying a procedure to arguments: • To apply a compound procedure to arguments, evaluate the body of the procedure with each formal parameter replaced by the corresponding argument. 367 Once we admit assignment into our programming language, such a definition is no longer adequate. In particular, Section 3.1.3 argued that, in the presence of assignment, a variable can no longer be considered to be merely a name for a value. Rather, a variable must somehow designate a “place” in which values can be stored. In our new model of evaluation, these places will be maintained in structures called environments. An environment is a sequence of frames. Each frame is a table (possibly empty) of bindings, which associate variable names with their corresponding values. (A single frame may contain at most one binding for any variable.) Each frame also has a pointer to its enclosing environment, unless, for the purposes of discussion, the frame is considered to be global. The value of a variable with respect to an environment is the value given by the binding of the variable in the first frame in the environment that contains a binding for that variable. If no frame in the sequence specifies a binding for the variable, then the variable is said to be unbound in the environment. Figure 3.1: A simple environment structure. 368 Figure 3.1 shows a simple environment structure consisting of three frames, labeled I, II, and III. In the diagram, A, B, C, and D are pointers to environments. C and D point to the same environment. The variables z and x are bound in frame II, while y and x are bound in frame I. The value of x in environment D is 3. The value of x with respect to environment B is also 3. This is determined as follows: We examine the first frame in the sequence (frame III) and do not find a binding for x, so we proceed to the enclosing environment D and find the binding in frame I. On the other hand, the value of x in environment A is 7, because the first frame in the sequence (frame II) contains a binding of x to 7. With respect to environment A, the binding of x to 7 in frame II is said to shadow the binding of x to 3 in frame I. The environment is crucial to the evaluation process, because it determines the context in which an expression should be evaluated. Indeed, one could say that expressions in a programming language do not, in themselves, have any meaning. Rather, an expression acquires a meaning only with respect to some environment in which it is evaluated. Even the interpretation of an expression as straightforward as (+ 1 1) depends on an understanding that one is operating in a context in which + is the symbol for addition. Thus, in our model of evaluation we will always speak of evaluating an expression with respect to some environment. To describe interactions with the interpreter, we will suppose that there is a global environment, consisting of a single frame (with no enclosing environment) that includes values for the symbols associated with the primitive procedures. For example, the idea that + is the symbol for addition is captured by saying that the symbol + is bound in the global environment to the primitive addition procedure. 3.2.1 The Rules for Evaluation The overall specification of how the interpreter evaluates a combination remains the same as when we first introduced it in Section 1.1.3: • To evaluate a combination: 1. Evaluate the subexpressions of the combination.12 2. Apply the value of the operator subexpression to the values of the operand subexpressions. The environment model of evaluation replaces the substitution model in specifying what it means to apply a compound procedure to arguments. In the environment model of evaluation, a procedure is always a pair consisting of some code and a pointer to an environment. Procedures are created in one way only: by evaluating a λ-expression. This produces a procedure whose code is obtained from the text of the λ-expression and whose environment is the environment in which the λ-expression was evaluated to produce the procedure. For example, consider the procedure definition (define (square x) (* x x)) 12 Assignment introduces a subtlety into step 1 of the evaluation rule. As shown in Exercise 3.8, the presence of assignment allows us to write expressions that will produce different values depending on the order in which the subexpressions in a combination are evaluated. Thus, to be precise, we should specify an evaluation order in step 1 (e.g., left to right or right to left). However, this order should always be considered to be an implementation detail, and one should never write programs that depend on some particular order. For instance, a sophisticated compiler might optimize a program by varying the order in which subexpressions are evaluated. 370 evaluated in the global environment. The procedure definition syntax is just syntactic sugar for an underlying implicit λ-expression. It would have been equivalent to have used (define square (lambda (x) (* x x))) which evaluates (lambda (x) (* x x)) and binds square to the resulting value, all in the global environment. Figure 3.2: Environment structure produced by evaluating (define (square x) (* x x)) in the global environment. Figure 3.2 shows the result of evaluating this define expression. The procedure object is a pair whose code specifies that the procedure has one formal parameter, namely x, and a procedure body (* x x). The environment part of the procedure is a pointer to the global environment, since that is the environment in which the λ-expression was evaluated to produce the procedure. A new binding, which associates the procedure object 371 with the symbol square, has been added to the global frame. In general, define creates definitions by adding bindings to frames. Now that we have seen how procedures are created, we can describe how procedures are applied. The environment model specifies: To apply a procedure to arguments, create a new environment containing a frame that binds the parameters to the values of the arguments. The enclosing environment of this frame is the environment specified by the procedure. Now, within this new environment, evaluate the procedure body. Figure 3.3: Environment created by evaluating (square 5) in the global environment. To show how this rule is followed, Figure 3.3 illustrates the environment structure created by evaluating the expression (square 5) in the global environment, where square is the procedure generated in Figure 3.2. Applying the procedure results in the creation of a new environment, labeled E1 in the figure, that begins with a frame in which x, the formal parameter for the procedure, is bound to the argument 5. The pointer leading up- 372 ward from this frame shows that the frame’s enclosing environment is the global environment. The global environment is chosen here, because this is the environment that is indicated as part of the square procedure object. Within E1, we evaluate the body of the procedure, (* x x). Since the value of x in E1 is 5, the result is (* 5 5), or 25. The environment model of procedure application can be summarized by two rules: • A procedure object is applied to a set of arguments by constructing a frame, binding the formal parameters of the procedure to the arguments of the call, and then evaluating the body of the procedure in the context of the new environment constructed. The new frame has as its enclosing environment the environment part of the procedure object being applied. • A procedure is created by evaluating a λ-expression relative to a given environment. The resulting procedure object is a pair consisting of the text of the λ-expression and a pointer to the environment in which the procedure was created. We also specify that defining a symbol using define creates a binding in the current environment frame and assigns to the symbol the indicated value.13 Finally, we specify the behavior of set!, the operation that forced us to introduce the environment model in the first place. Evaluating the expression (set! 〈variable 〉〈value 〉) in some environment locates the bind13 If there is already a binding for the variable in the current frame, then the binding is changed. This is convenient because it allows redefinition of symbols; however, it also means that define can be used to change values, and this brings up the issues of assignment without explicitly using set!. Because of this, some people prefer redefinitions of existing symbols to signal errors or warnings. 373 ing of the variable in the environment and changes that binding to indicate the new value. That is, one finds the first frame in the environment that contains a binding for the variable and modifies that frame. If the variable is unbound in the environment, then set! signals an error. These evaluation rules, though considerably more complex than the substitution model, are still reasonably straightforward. Moreover, the evaluation model, though abstract, provides a correct description of how the interpreter evaluates expressions. In Chapter 4 we shall see how this model can serve as a blueprint for implementing a working interpreter. The following sections elaborate the details of the model by analyzing some illustrative programs. 3.2.2 Applying Simple Procedures When we introduced the substitution model in Section 1.1.5 we showed how the combination (f 5) evaluates to 136, given the following procedure definitions: (define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (define (f a) (sum-of-squares (+ a 1) (* a 2))) We can analyze the same example using the environment model. Figure 3.4 shows the three procedure objects created by evaluating the definitions of f, square, and sum-of-squares in the global environment. Each pro- 374 cedure object consists of some code, together with a pointer to the global environment. Figure 3.4: Procedure objects in the global frame. In Figure 3.5 we see the environment structure created by evaluating the expression (f 5). The call to f creates a new environment E1 beginning with a frame in which a, the formal parameter of f, is bound to the argument 5. In E1, we evaluate the body of f: (sum-of-squares (+ a 1) (* a 2)) To evaluate this combination, we first evaluate the subexpressions. The first subexpression, sum-of-squares, has a value that is a procedure object. (Notice how this value is found: We first look in the first frame of E1, which contains no binding for sum-of-squares. Then we proceed to the enclosing environment, i.e. the global environment, and find the binding shown in Figure 3.4.) The other two subexpressions are evaluated by ap- 375 plying the primitive operations + and * to evaluate the two combinations (+ a 1) and (* a 2) to obtain 6 and 10, respectively. Now we apply the procedure object sum-of-squares to the arguments 6 and 10. This results in a new environment E2 in which the formal parameters x and y are bound to the arguments. Within E2 we evaluate the combination (+ (square x) (square y)). This leads us to evaluate (square x), where square is found in the global frame and x is 6. Once again, we set up a new environment, E3, in which x is bound to 6, and within this we evaluate the body of square, which is (* x x). Also as part of applying sum-of-squares, we must evaluate the subexpression (square y), where y is 10. This second call to square creates another environment, E4, in which x, the formal parameter of square, is bound to 10. And within E4 we must evaluate (* x x). Figure 3.5: Environments created by evaluating (f 5) using the procedures in Figure 3.4. 376 The important point to observe is that each call to square creates a new environment containing a binding for x. We can see here how the different frames serve to keep separate the different local variables all named x. Notice that each frame created by square points to the global environment, since this is the environment indicated by the square procedure object. After the subexpressions are evaluated, the results are returned. The values generated by the two calls to square are added by sum-of-squares, and this result is returned by f. Since our focus here is on the environment structures, we will not dwell on how these returned values are passed from call to call; however, this is also an important aspect of the evaluation process, and we will return to it in detail in Chapter 5. Exercise 3.9: In Section 1.2.1 we used the substitution model to analyze two procedures for computing factorials, a recursive version (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) and an iterative version (define (factorial n) (fact-iter 1 1 n)) (define (fact-iter product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) 377 Show environment structures created by evaluating (factorial 6) using each version of the factorial procedure.14 3.2.3 Frames as the Repository of Local State We can turn to the environment model to see how procedures and assignment can be used to represent objects with local state. As an example, consider the “withdrawal processor” from Section 3.1.1 created by calling the procedure (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) Let us describe the evaluation of (define W1 (make-withdraw 100)) followed by (W1 50) 50 Figure 3.6 shows the result of defining the make-withdraw procedure in the global environment. This produces a procedure object that contains a pointer to the global environment. So far, this is no different from the 14 The environment model will not clarify our claim in Section 1.2.1 that the inter- preter can execute a procedure such as fact-iter in a constant amount of space using tail recursion. We will discuss tail recursion when we deal with the control structure of the interpreter in Section 5.4. 378 examples we have already seen, except that the body of the procedure is itself a λ-expression. Figure 3.6: Result of defining make-withdraw in the global environment. The interesting part of the computation happens when we apply the procedure make-withdraw to an argument: (define W1 (make-withdraw 100)) We begin, as usual, by setting up an environment E1 in which the formal parameter balance is bound to the argument 100. Within this environment, we evaluate the body of make-withdraw, namely the λ-expression. This constructs a new procedure object, whose code is as specified by the lambda and whose environment is E1, the environment in which the lambda was evaluated to produce the procedure. The resulting procedure object is the value returned by the call to make-withdraw. 379 This is bound to W1 in the global environment, since the define itself is being evaluated in the global environment. Figure 3.7 shows the resulting environment structure. Figure 3.7: Result of evaluating (define W1 (make-withdraw 100)). Now we can analyze what happens when W1 is applied to an argument: (W1 50) 50 We begin by constructing a frame in which amount, the formal parameter of W1, is bound to the argument 50. The crucial point to observe is that this frame has as its enclosing environment not the global environment, but rather the environment E1, because this is the environment that is speci- 380 fied by the W1 procedure object. Within this new environment, we evaluate the body of the procedure: (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient The resulting environment structure is shown in Figure 3.8. The expression being evaluated references both amount and balance. Amount will be found in the first frame in the environment, while balance will be found by following the enclosing-environment pointer to E1. Figure 3.8: Environments created by applying the procedure object W1. Figure 3.9: Environments after the call to W1. When the set! is executed, the binding of balance in E1 is changed. At the completion of the call to W1, balance is 50, and the frame that contains balance is still pointed to by the procedure object W1. The frame that binds amount (in which we executed the code that changed balance) is no longer relevant, since the procedure call that constructed it has terminated, and there are no pointers to that frame from other parts of the environment. The next time W1 is called, this will build a new frame that binds amount and whose enclosing environment is E1. We see that E1 serves as the “place” that holds the local state variable for the procedure object W1. Figure 3.9 shows the situation after the call to W1. Observe what happens when we create a second “withdraw” object by making another call to make-withdraw: (define W2 (make-withdraw 100)) Figure 3.10: Using (define W2 (make-withdraw 100)) to create a second object. This produces the environment structure of Figure 3.10, which shows that W2 is a procedure object, that is, a pair with some code and an environment. The environment E2 for W2 was created by the call to make-withdraw. It contains a frame with its own local binding for balance. On the other hand, W1 and W2 have the same code: the code specified by the λ-expression in the body of make-withdraw.15 We see here why W1 and W2 behave as independent objects. Calls to W1 reference the state variable balance stored in E1, whereas calls to W2 reference the balance stored in 15 Whether W1 and W2 share the same physical code stored in the computer, or whether they each keep a copy of the code, is a detail of the implementation. For the interpreter we implement in Chapter 4, the code is in fact shared. 383 E2. Thus, changes to the local state of one object do not affect the other object. Exercise 3.10: In the make-withdraw procedure, the local variable balance is created as a parameter of make-withdraw. We could also create the local state variable explicitly, using let, as follows: (define (make-withdraw initial-amount) (let ((balance initial-amount)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))) Recall from Section 1.3.2 that let is simply syntactic sugar for a procedure call: (let ((〈var〉〈exp〉)) 〈body〉) is interpreted as an alternate syntax for ((lambda (〈var〉) 〈body〉) 〈exp〉) Use the environment model to analyze this alternate version of make-withdraw, drawing figures like the ones above to illustrate the interactions (define W1 (make-withdraw 100)) (W1 50) (define W2 (make-withdraw 100)) 384 Show that the two versions of make-withdraw create objects with the same behavior. How do the environment structures differ for the two versions? 3.2.4 Internal Definitions Section 1.1.8 introduced the idea that procedures can have internal definitions, thus leading to a block structure as in the following procedure to compute square roots: (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0)) Now we can use the environment model to see why these internal definitions behave as desired. Figure 3.11 shows the point in the evaluation of the expression (sqrt 2) where the internal procedure good-enough? has been called for the first time with guess equal to 1. Observe the structure of the environment. Sqrt is a symbol in the global environment that is bound to a procedure object whose associated environment is the global environment. When sqrt was called, a new environment E1 was formed, subordinate to the global environment, in which 385 the parameter x is bound to 2. The body of sqrt was then evaluated in E1. Since the first expression in the body of sqrt is (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) evaluating this expression defined the procedure good-enough? in the environment E1. To be more precise, the symbol good-enough? was added to the first frame of E1, bound to a procedure object whose associated environment is E1. Similarly, improve and sqrt-iter were defined as procedures in E1. For conciseness, Figure 3.11 shows only the procedure object for good-enough?. After the local procedures were defined, the expression (sqrt-iter 1.0) was evaluated, still in environment E1. So the procedure object bound to sqrt-iter in E1 was called with 1 as an argument. This created an environment E2 in which guess, the parameter of sqrt-iter, is bound to 1. Sqrt-iter in turn called good-enough? with the value of guess (from E2) as the argument for good-enough?. This set up another environment, E3, in which guess (the parameter of good-enough?) is bound to 1. Although sqrt-iter and good-enough? both have a parameter named guess, these are two distinct local variables located in different frames. Also, E2 and E3 both have E1 as their enclosing environment, because the sqrt-iter and good-enough? procedures both have E1 as their environment part. One consequence of this is that the symbol x that appears in the body of good-enough? will reference the binding of x that appears in E1, namely the value of x with which the original sqrt procedure was called. Figure 3.11: Sqrt procedure with internal definitions. The environment model thus explains the two key properties that make local procedure definitions a useful technique for modularizing programs: • The names of the local procedures do not interfere with names external to the enclosing procedure, because the local procedure names will be bound in the frame that the procedure creates when it is run, rather than being bound in the global environment. • The local procedures can access the arguments of the enclosing proce- 387 dure, simply by using parameter names as free variables. This is because the body of the local procedure is evaluated in an environment that is subordinate to the evaluation environment for the enclosing procedure. Exercise 3.11: In Section 3.2.3 we saw how the environment model described the behavior of procedures with local state. Now we have seen how internal definitions work. A typical message-passing procedure contains both of these aspects. Consider the bank account procedure of Section 3.1.1: (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m) (cond ((eq? m ’withdraw) withdraw) ((eq? m ’deposit) deposit) (else (error "Unknown request - MAKE-ACCOUNT" m)))) dispatch) Show the environment structure generated by the sequence of interactions (define acc (make-account 50)) 388 ((acc ’deposit) 40) 90 ((acc ’withdraw) 60) 30 Where is the local state for acc kept? Suppose we define another account (define acc2 (make-account 100)) How are the local states for the two accounts kept distinct? Which parts of the environment structure are shared between acc and acc2? 3.3 Modeling with Mutable Data Chapter 2 dealt with compound data as a means for constructing computational objects that have several parts, in order to model real-world objects that have several aspects. In that chapter we introduced the discipline of data abstraction, according to which data structures are specified in terms of constructors, which create data objects, and selectors, which access the parts of compound data objects. But we now know that there is another aspect of data that Chapter 2 did not address. The desire to model systems composed of objects that have changing state leads us to the need to modify compound data objects, as well as to construct and select from them. In order to model compound objects with changing state, we will design data abstractions to include, in addition to selectors and constructors, operations called mutators, which modify data objects. For instance, modeling a banking system requires us to change account balances. Thus, a data structure for representing bank accounts might admit an operation 389 (set-balance! 〈account〉〈new-value〉) that changes the balance of the designated account to the designated new value. Data objects for which mutators are defined are known as mutable data objects. Chapter 2 introduced pairs as a general-purpose “glue” for synthesizing compound data. We begin this section by defining basic mutators for pairs, so that pairs can serve as building blocks for constructing mutable data objects. These mutators greatly enhance the representational power of pairs, enabling us to build data structures other than the sequences and trees that we worked with in Section 2.2. We also present some examples of simulations in which complex systems are modeled as collections of objects with local state. 3.3.1 Mutable List Structure The basic operations on pairs—cons, car, and cdr—can be used to construct list structure and to select parts from list structure, but they are incapable of modifying list structure. The same is true of the list operations we have used so far, such as append and list, since these can be defined in terms of cons, car, and cdr. To modify list structures we need new operations. The primitive mutators for pairs are set-car! and set-cdr!. Set-car! takes two arguments, the first of which must be a pair. It modifies this pair, replacing the car pointer by a pointer to the second argument of set-car!.16 16 Set-car! and set-cdr! return implementation-dependent values. Like set!, they should be used only for their effect. Figure 3.12: Lists x: ((a b) c d) and y: (e f). As an example, suppose that x is bound to the list ((a b) c d) and y to the list (e f) as illustrated in Figure 3.12. Evaluating the expression (set-car! x y) modifies the pair to which x is bound, replacing its car by the value of y. The result of the operation is shown in Figure 3.13. The structure x has been modified and would now be printed as ((e f) c d). The pairs representing the list (a b), identified by the pointer that was replaced, are now detached from the original structure.17 17 We see from this that mutation operations on lists can create “garbage” that is not part of any accessible structure. We will see in Section 5.3.2 that Lisp memorymanagement systems include a garbage collector, which identifies and recycles the memory space used by unneeded pairs. Figure 3.13: Effect of (set-car! x y) on the lists in Figure 3.12. Compare Figure 3.13 with Figure 3.14, which illustrates the result of executing (define z (cons y (cdr x))) with x and y bound to the original lists of Figure 3.12. The variable z is now bound to a new pair created by the cons operation; the list to which x is bound is unchanged. The set-cdr! operation is similar to set-car!. The only difference is that the cdr pointer of the pair, rather than the car pointer, is replaced. The effect of executing (set-cdr! x y) on the lists of Figure 3.12 is shown in Figure 3.15. Here the cdr pointer of x has been replaced by the pointer to (e f). Also, the list (c d), which used to be the cdr of x, is now detached from the structure. Figure 3.14: Effect of (define z (cons y (cdr x))) on the lists in Figure 3.12. Cons builds new list structure by creating new pairs, while set-car! and set-cdr! modify existing pairs. Indeed, we could implement cons in terms of the two mutators, together with a procedure get-new-pair, which returns a new pair that is not part of any existing list structure. We obtain the new pair, set its car and cdr pointers to the designated objects, and return the new pair as the result of the cons.18 18 Get-new-pair is one of the operations that must be implemented as part of the memory management required by a Lisp implementation. We will discuss this in Section 5.3.1. Figure 3.15: Effect of (set-cdr! x y) on the lists in Figure 3.12. (define (cons x y) (let ((new (get-new-pair))) (set-car! new x) (set-cdr! new y) new)) Exercise 3.12: The following procedure for appending lists was introduced in Section 2.2.1: (define (append x y) (if (null? x) y (cons (car x) (append (cdr x) y)))) 394 Append forms a new list by successively consing the elements of x onto y. The procedure append! is similar to append, but it is a mutator rather than a constructor. It appends the lists by splicing them together, modifying the final pair of x so that its cdr is now y. (It is an error to call append! with an empty x.) (define (append! x y) (set-cdr! (last-pair x) y) x) Here last-pair is a procedure that returns the last pair in its argument: (define (last-pair x) (if (null? (cdr x)) x (last-pair (cdr x)))) Consider the interaction (define x (list ’a ’b)) (define y (list ’c ’d)) (define z (append x y)) z (a b c d) (cdr x) 〈response〉 (define w (append! x y)) w (a b c d) (cdr x) 395 What are the missing 〈response〉s? diagrams to explain your answer. Draw box-and-pointer Exercise 3.13: Consider the following make-cycle procedure, which uses the last-pair procedure defined in Exercise 3.12: (define (make-cycle x) (set-cdr! (last-pair x) x) x) Draw a box-and-pointer diagram that shows the structure z created by (define z (make-cycle (list ’a ’b ’c))) What happens if we try to compute (last-pair z)? Exercise 3.14: The following procedure is quite useful, although obscure: (define (mystery x) (define (loop x y) (if (null? x) y (let ((temp (cdr x))) (set-cdr! x y) (loop temp x)))) (loop x ’())) Loop uses the “temporary” variable temp to hold the old value of the cdr of x, since the set-cdr! on the next line destroys the cdr. Explain what mystery does in general. Suppose v is defined by (define v (list ’a ’b ’c ’d)). Draw the box-and-pointer di- agram that represents the list to which v is bound. Suppose that we 396 now evaluate (define w (mystery v)). Draw box-and-pointer diagrams that show the structures v and w after evaluating this expression. What would be printed as the values of v and w? Sharing and identity We mentioned in Section 3.1.3 the theoretical issues of “sameness” and “change” raised by the introduction of assignment. These issues arise in practice when individual pairs are shared among different data objects. For example, consider the structure formed by (define x (list ’a ’b)) (define z1 (cons x x)) As shown in Figure 3.16, z1 is a pair whose car and cdr both point to the same pair x. This sharing of x by the car and cdr of z1 is a consequence of the straightforward way in which cons is implemented. In general, using cons to construct lists will result in an interlinked structure of pairs in which many individual pairs are shared by many different structures. Figure 3.16: The list z1 formed by (cons x x). Figure 3.17: The list z2 formed by (cons (list ’a ’b) (list ’a ’b)). In contrast to Figure 3.16, Figure 3.17 shows the structure created by (define z2 (cons (list ’a ’b) (list ’a ’b))) In this structure, the pairs in the two (a b) lists are distinct, although the actual symbols are shared.19 When thought of as a list, z1 and z2 both represent “the same” list, ((a b) a b). In general, sharing is completely undetectable if we operate on lists using only cons, car, and cdr. However, if we allow mutators on list structure, sharing becomes significant. As an example of the difference that sharing can make, consider the following procedure, which modifies the car of the structure to which it is applied: (define (set-to-wow! x) (set-car! (car x) ’wow) 19 The two pairs are distinct because each call to cons returns a new pair. The symbols are shared; in Scheme there is a unique symbol with any given name. Since Scheme provides no way to mutate a symbol, this sharing is undetectable. Note also that the sharing is what enables us to compare symbols using eq?, which simply checks equality of pointers. 398 x) Even though z1 and z2 are “the same” structure, applying set-to-wow! to them yields different results. With z1, altering the car also changes the cdr, because in z1 the car and the cdr are the same pair. With z2, the car and cdr are distinct, so set-to-wow! modifies only the car: z1 ((a b) a b) (set-to-wow! z1) ((wow b) wow b) z2 ((a b) a b) (set-to-wow! z2) ((wow b) a b) One way to detect sharing in list structures is to use the predicate eq?, which we introduced in Section 2.3.1 as a way to test whether two symbols are equal. More generally, (eq? x y) tests whether x and y are the same object (that is, whether x and y are equal as pointers). Thus, with z1 and z2 as defined in Figure 3.16 and Figure 3.17, (eq? (car z1) (cdr z1)) is true and (eq? (car z2) (cdr z2)) is false. As will be seen in the following sections, we can exploit sharing to greatly extend the repertoire of data structures that can be represented by pairs. On the other hand, sharing can also be dangerous, since modifications made to structures will also affect other structures that happen to share the modified parts. The mutation operations set-car! and set-cdr! should 399 be used with care; unless we have a good understanding of how our data objects are shared, mutation can have unanticipated results.20 Exercise 3.15: Draw box-and-pointer diagrams to explain the effect of set-to-wow! on the structures z1 and z2 above. Exercise 3.16: Ben Bitdiddle decides to write a procedure to count the number of pairs in any list structure. “It’s easy,” he reasons. “The number of pairs in any structure is the number in the car plus the number in the cdr plus one more to count the current pair.” So Ben writes the following procedure: (define (count-pairs x) (if (not (pair? x)) 0 (+ (count-pairs (car x)) (count-pairs (cdr x)) 1))) Show that this procedure is not correct. In particular, draw boxand-pointer diagrams representing list structures made up of ex20 The subtleties of dealing with sharing of mutable data objects reflect the underlying issues of “sameness” and “change” that were raised in Section 3.1.3. We mentioned there that admitting change to our language requires that a compound object must have an “identity” that is something different from the pieces from which it is composed. In Lisp, we consider this “identity” to be the quality that is tested by eq?, i.e., by equality of pointers. Since in most Lisp implementations a pointer is essentially a memory address, we are “solving the problem” of defining the identity of objects by stipulating that a data object “itself” is the information stored in some particular set of memory locations in the computer. This suffices for simple Lisp programs, but is hardly a general way to resolve the issue of “sameness” in computational models. 400 actly three pairs for which Ben’s procedure would return 3; return 4; return 7; never return at all. Exercise 3.17: Devise a correct version of the count-pairs procedure of Exercise 3.16 that returns the number of distinct pairs in any structure. (Hint: Traverse the structure, maintaining an auxiliary data structure that is used to keep track of which pairs have already been counted.) Exercise 3.18: Write a procedure that examines a list and determines whether it contains a cycle, that is, whether a program that tried to find the end of the list by taking successive cdrs would go into an infinite loop. Exercise 3.13 constructed such lists. Exercise 3.19: Redo Exercise 3.18 using an algorithm that takes only a constant amount of space. (This requires a very clever idea.) Mutation is just assignment When we introduced compound data, we observed in Section 2.1.3 that pairs can be represented purely in terms of procedures: (define (cons x y) (define (dispatch m) (cond ((eq? m ’car) x) ((eq? m ’cdr) y) (else (error "Undefined operation - CONS" m)))) dispatch) (define (car z) (z ’car)) (define (cdr z) (z ’cdr)) 401 The same observation is true for mutable data. We can implement mutable data objects as procedures using assignment and local state. For instance, we can extend the above pair implementation to handle set-car! and set-cdr! in a manner analogous to the way we implemented bank accounts using make-account in Section 3.1.1: (define (cons x y) (define (set-x! v) (set! x v)) (define (set-y! v) (set! y v)) (define (dispatch m) (cond ((eq? m ’car) x) ((eq? m ’cdr) y) ((eq? m ’set-car!) set-x!) ((eq? m ’set-cdr!) set-y!) (else (error "Undefined operation - CONS" m)))) dispatch) (define (car z) (z ’car)) (define (cdr z) (z ’cdr)) (define (set-car! z new-value) ((z ’set-car!) new-value) z) (define (set-cdr! z new-value) ((z ’set-cdr!) new-value) z) 402 Assignment is all that is needed, theoretically, to account for the behavior of mutable data. As soon as we admit set! to our language, we raise all the issues, not only of assignment, but of mutable data in general.21 Exercise 3.20: Draw environment diagrams to illustrate the evaluation of the sequence of expressions (define x (cons 1 2)) (define z (cons x x)) (set-car! (cdr z) 17) (car x) 17 using the procedural implementation of pairs given above. (Compare Exercise 3.11.) 3.3.2 Representing Queues The mutators set-car! and set-cdr! enable us to use pairs to construct data structures that cannot be built with cons, car, and cdr alone. This section shows how to use pairs to represent a data structure called a queue. Section 3.3.3 will show how to represent data structures called tables. A queue is a sequence in which items are inserted at one end (called the rear of the queue) and deleted from the other end (the front). Figure 3.18 shows an initially empty queue in which the items a and b are inserted. Then a is removed, c and d are inserted, and b is removed. Because items 21 On the other hand, from the viewpoint of implementation, assignment requires us to modify the environment, which is itself a mutable data structure. Thus, assignment and mutation are equipotent: Each can be implemented in terms of the other. 403 are always removed in the order in which they are inserted, a queue is sometimes called a FIFO (first in, first out) buffer. Figure 3.18: Queue operations. In terms of data abstraction, we can regard a queue as defined by the following set of operations: • a constructor: (make-queue) returns an empty queue (a queue containing no items). • two selectors: (empty-queue? 〈queue〉) tests if the queue is empty. (front-queue 〈queue〉) returns the object at the front of the queue, signaling an error if the queue is empty; it does not modify the queue. • two mutators: (insert-queue! 〈queue〉〈item〉) inserts the item at the rear of the queue and returns the modified queue as its value. 404 (delete-queue! 〈queue〉) removes the item at the front of the queue and returns the modified queue as its value, signaling an error if the queue is empty before the deletion. Because a queue is a sequence of items, we could certainly represent it as an ordinary list; the front of the queue would be the car of the list, inserting an item in the queue would amount to appending a new element at the end of the list, and deleting an item from the queue would just be taking the cdr of the list. However, this representation is inefficient, because in order to insert an item we must scan the list until we reach the end. Since the only method we have for scanning a list is by successive cdr operations, this scanning requires Θ(n ) steps for a list of n items. A simple modification to the list representation overcomes this disadvantage by allowing the queue operations to be implemented so that they require Θ(1) steps; that is, so that the number of steps needed is independent of the length of the queue. The difficulty with the list representation arises from the need to scan to find the end of the list. The reason we need to scan is that, although the standard way of representing a list as a chain of pairs readily provides us with a pointer to the beginning of the list, it gives us no easily accessible pointer to the end. The modification that avoids the drawback is to represent the queue as a list, together with an additional pointer that indicates the final pair in the list. That way, when we go to insert an item, we can consult the rear pointer and so avoid scanning the list. A queue is represented, then, as a pair of pointers, front-ptr and rear-ptr, which indicate, respectively, the first and last pairs in an ordinary list. Since we would like the queue to be an identifiable object, 405 we can use cons to combine the two pointers. Thus, the queue itself will be the cons of the two pointers. Figure 3.19 illustrates this representation. Figure 3.19: Implementation of a queue as a list with front and rear pointers. To define the queue operations we use the following procedures, which enable us to select and to modify the front and rear pointers of a queue: (define (front-ptr queue) (car queue)) (define (rear-ptr queue) (cdr queue)) (define (set-front-ptr! queue item) (set-car! queue item)) (define (set-rear-ptr! queue item) (set-cdr! queue item)) Now we can implement the actual queue operations. We will consider a queue to be empty if its front pointer is the empty list: (define (empty-queue? queue) (null? (front-ptr queue))) 406 The make-queue constructor returns, as an initially empty queue, a pair whose car and cdr are both the empty list: (define (make-queue) (cons ’() ’())) To select the item at the front of the queue, we return the car of the pair indicated by the front pointer: (define (front-queue queue) (if (empty-queue? queue) (error "FRONT called with an empty queue" queue) (car (front-ptr queue)))) To insert an item in a queue, we follow the method whose result is indicated in Figure 3.20. We first create a new pair whose car is the item to be inserted and whose cdr is the empty list. If the queue was initially empty, we set the front and rear pointers of the queue to this new pair. Otherwise, we modify the final pair in the queue to point to the new pair, and also set the rear pointer to the new pair. Figure 3.20: Result of using (insert-queue! q ’d) on the queue of Figure 3.19. 407 (define (insert-queue! queue item) (let ((new-pair (cons item ’()))) (cond ((empty-queue? queue) (set-front-ptr! queue new-pair) (set-rear-ptr! queue new-pair) queue) (else (set-cdr! (rear-ptr queue) new-pair) (set-rear-ptr! queue new-pair) queue)))) Figure 3.21: Result of using (delete-queue! q) on the queue of Figure 3.20. To delete the item at the front of the queue, we merely modify the front pointer so that it now points at the second item in the queue, which can be found by following the cdr pointer of the first item (see Figure 3.21):22 22 If the first item is the final item in the queue, the front pointer will be the empty list after the deletion, which will mark the queue as empty; we needn’t worry about 408 (define (delete-queue! queue) (cond ((empty-queue? queue) (error "DELETE! called with an empty queue" queue)) (else (set-front-ptr! queue (cdr (front-ptr queue))) queue))) Exercise 3.21: Ben Bitdiddle decides to test the queue implementation described above. He types in the procedures to the Lisp interpreter and proceeds to try them out: (define q1 (make-queue)) (insert-queue! q1 ’a) ((a) a) (insert-queue! q1 ’b) ((a b) b) (delete-queue! q1) ((b) b) (delete-queue! q1) (() b) “It’s all wrong!” he complains. “The interpreter’s response shows that the last item is inserted into the queue twice. And when I delete both items, the second b is still there, so the queue isn’t empty, even though it’s supposed to be.” Eva Lu Ator suggests that Ben has misunderstood what is happening. “It’s not that the items are going into the queue twice,” she explains. updating the rear pointer, which will still point to the deleted item, because emptyqueue? looks only at the front pointer. 409 just that the standard Lisp printer doesn’t know how to make sense of the queue representation. If you want to see the queue printed correctly, you’ll have to define your own print procedure for queues.” Explain what Eva Lu is talking about. In particular, show why Ben’s examples produce the printed results that they do. Define a procedure print-queue that takes a queue as input and prints the sequence of items in the queue. Exercise 3.22: Instead of representing a queue as a pair of pointers, we can build a queue as a procedure with local state. The local state will consist of pointers to the beginning and the end of an ordinary list. Thus, the make-queue procedure will have the form (define (make-queue) (let ((front-ptr ... ) (rear-ptr ... )) 〈definitions of internal procedures〉 (define (dispatch m) ...) dispatch)) Complete the definition of make-queue and provide implementations of the queue operations using this representation. Exercise 3.23: A deque (“double-ended queue”) is a sequence in which items can be inserted and deleted at either the front or the rear. Operations on deques are the constructor make-deque, the predicate empty-deque?, selectors front-deque and rear-deque, and front-delete-deque! and rear-delete-deque!. Show how to 410 represent deques using pairs, and give implementations of the operations.23 All operations should be accomplished in Θ(1) steps. 3.3.3 Representing Tables When we studied various ways of representing sets in Chapter 2, we mentioned in Section 2.3.3 the task of maintaining a table of records indexed by identifying keys. In the implementation of data-directed programming in Section 2.4.3, we made extensive use of two-dimensional tables, in which information is stored and retrieved using two keys. Here we see how to build tables as mutable list structures. Figure 3.22: A table represented as a headed list. We first consider a one-dimensional table, in which each value is stored under a single key. We implement the table as a list of records, each of which is implemented as a pair consisting of a key and the associated value. The records are glued together to form a list by pairs whose cars point to 23 Be careful not to make the interpreter try to print a structure that contains cycles. (See Exercise 3.13.) 411 successive records. These gluing pairs are called the backbone of the table. In order to have a place that we can change when we add a new record to the table, we build the table as a headed list. A headed list has a special backbone pair at the beginning, which holds a dummy “record”—in this case the arbitrarily chosen symbol *table*. Figure 3.22 shows the boxand-pointer diagram for the table a: To extract information from a table we use the lookup procedure, which takes a key as argument and returns the associated value (or false if there is no value stored under that key). Lookup is defined in terms of the assoc operation, which expects a key and a list of records as arguments. Note that assoc never sees the dummy record. Assoc returns the record that has the given key as its car.24 Lookup then checks to see that the resulting record returned by assoc is not false, and returns the value (the cdr) of the record. (define (lookup key table) (let ((record (assoc key (cdr table)))) (if record (cdr record) false))) (define (assoc key records) (cond ((null? records) false) ((equal? key (caar records)) (car records)) (else (assoc key (cdr records))))) 24 Because assoc uses equal?, it can recognize keys that are symbols, numbers, or list 412 To insert a value in a table under a specified key, we first use assoc to see if there is already a record in the table with this key. If not, we form a new record by consing the key with the value, and insert this at the head of the table’s list of records, after the dummy record. If there already is a record with this key, we set the cdr of this record to the designated new value. The header of the table provides us with a fixed location to modify in order to insert the new record.25 (define (insert! key value table) (let ((record (assoc key (cdr table)))) (if record (set-cdr! record value) (set-cdr! table (cons (cons key value) (cdr table))))) ’ok) To construct a new table, we simply create a list containing the symbol *table*: (define (make-table) (list ’*table*)) Two-dimensional tables In a two-dimensional table, each value is indexed by two keys. We can construct such a table as a one-dimensional table in which each key identifies a subtable. Figure 3.23 shows the box-and-pointer diagram for the table 25 Thus, the first backbone pair is the object that represents the table “itself”; that is, a pointer to the table is a pointer to this pair. This same backbone pair always starts the table. If we did not arrange things in this way, insert! would have to return a new value for the start of the table when it added a new record. 413 math: which has two subtables. (The subtables don’t need a special header symbol, since the key that identifies the subtable serves this purpose.) Figure 3.23: A two-dimensional table. 414 When we look up an item, we use the first key to identify the correct subtable. Then we use the second key to identify the record within the subtable. (define (lookup key-1 key-2 table) (let ((subtable (assoc key-1 (cdr table)))) (if subtable (let ((record (assoc key-2 (cdr subtable)))) (if record (cdr record) false)) false))) To insert a new item under a pair of keys, we use assoc to see if there is a subtable stored under the first key. If not, we build a new subtable containing the single record (key-2, value) and insert it into the table under the first key. If a subtable already exists for the first key, we insert the new record into this subtable, using the insertion method for one-dimensional tables described above: (define (insert! key-1 key-2 value table) (let ((subtable (assoc key-1 (cdr table)))) (if subtable (let ((record (assoc key-2 (cdr subtable)))) (if record (set-cdr! record value) (set-cdr! subtable (cons (cons key-2 value) (cdr subtable))))) (set-cdr! table 415 (cons (list key-1 (cons key-2 value)) (cdr table))))) ’ok) Creating local tables The lookup and insert! operations defined above take the table as an argument. This enables us to use programs that access more than one table. Another way to deal with multiple tables is to have separate lookup and insert! procedures for each table. We can do this by representing a table procedurally, as an object that maintains an internal table as part of its local state. When sent an appropriate message, this “table object” supplies the procedure with which to operate on the internal table. Here is a generator for two-dimensional tables represented in this fashion: (define (make-table) (let ((local-table (list ’*table*))) (define (lookup key-1 key-2) (let ((subtable (assoc key-1 (cdr local-table)))) (if subtable (let ((record (assoc key-2 (cdr subtable)))) (if record (cdr record) false)) false))) (define (insert! key-1 key-2 value) (let ((subtable (assoc key-1 (cdr local-table)))) (if subtable 416 (let ((record (assoc key-2 (cdr subtable)))) (if record (set-cdr! record value) (set-cdr! subtable (cons (cons key-2 value) (cdr subtable))))) (set-cdr! local-table (cons (list key-1 (cons key-2 value)) (cdr local-table))))) ’ok) (define (dispatch m) (cond ((eq? m ’lookup-proc) lookup) ((eq? m ’insert-proc!) insert!) (else (error "Unknown operation - TABLE" m)))) dispatch)) Using make-table, we could implement the get and put operations used in Section 2.4.3 for data-directed programming, as follows: (define operation-table (make-table)) (define get (operation-table ’lookup-proc)) (define put (operation-table ’insert-proc!)) Get takes as arguments two keys, and put takes as arguments two keys and a value. Both operations access the same local table, which is encapsulated within the object created by the call to make-table. Exercise 3.24: In the table implementations above, the keys are tested for equality using equal? (called by assoc). This is not always the appropriate test. For instance, we might have a table with 417 numeric keys in which we don’t need an exact match to the number we’re looking up, but only a number within some tolerance of it. Design a table constructor make-table that takes as an argument a same-key? procedure that will be used to test “equality” of keys. Make-table should return a dispatch procedure that can be used to access appropriate lookup and insert! procedures for a local table. Exercise 3.25: Generalizing one- and two-dimensional tables, show how to implement a table in which values are stored under an arbitrary number of keys and different values may be stored under different numbers of keys. The lookup and insert! procedures should take as input a list of keys used to access the table. Exercise 3.26: To search a table as implemented above, one needs to scan through the list of records. This is basically the unordered list representation of Section 2.3.3. For large tables, it may be more efficient to structure the table in a different manner. Describe a table implementation where the (key, value) records are organized using a binary tree, assuming that keys can be ordered in some way (e.g., numerically or alphabetically). (Compare Exercise 2.66 of Chapter 2.) Exercise 3.27: Memoization (also called tabulation) is a technique that enables a procedure to record, in a local table, values that have previously been computed. This technique can make a vast difference in the performance of a program. A memoized procedure maintains a table in which values of previous calls are stored using as keys the arguments that produced the values. When the 418 memoized procedure is asked to compute a value, it first checks the table to see if the value is already there and, if so, just returns that value. Otherwise, it computes the new value in the ordinary way and stores this in the table. As an example of memoization, recall from Section 1.2.2 the exponential process for computing Fibonacci numbers: (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) The memoized version of the same procedure is (define memo-fib (memoize (lambda (n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (memo-fib (- n 1)) (memo-fib (- n 2)))))))) where the memoizer is defined as (define (memoize f) (let ((table (make-table))) (lambda (x) (let ((previously-computed-result (lookup x table))) (or previously-computed-result (let ((result (f x))) (insert! x result table) result)))))) 419 Draw an environment diagram to analyze the computation of (memo-fib 3). Explain why memo-fib computes the n th Fibonacci number in a number of steps proportional to n . Would the scheme still work if we had simply defined memo-fib to be (memoize fib)? 3.3.4 A Simulator for Digital Circuits Designing complex digital systems, such as computers, is an important engineering activity. Digital systems are constructed by interconnecting simple elements. Although the behavior of these individual elements is simple, networks of them can have very complex behavior. Computer simulation of proposed circuit designs is an important tool used by digital systems engineers. In this section we design a system for performing digital logic simulations. This system typifies a kind of program called an event-driven simulation, in which actions (“events”) trigger further events that happen at a later time, which in turn trigger more events, and so so. Our computational model of a circuit will be composed of objects that correspond to the elementary components from which the circuit is constructed. There are wires, which carry digital signals. A digital signal may at any moment have only one of two possible values, 0 and 1. There are also various types of digital function boxes, which connect wires carrying input signals to other output wires. Such boxes produce output signals computed from their input signals. The output signal is delayed by a time that depends on the type of the function box. For example, an inverter is a primitive function box that inverts its input. If the input signal to an inverter changes to 0, then one inverter-delay later the inverter will change its output signal to 1. If the input signal to an inverter changes to 1, then one inverter-delay later the inverter will change its output signal to 0. 420 draw an inverter symbolically as in Figure 3.24. An and-gate, also shown in Figure 3.24, is a primitive function box with two inputs and one output. It drives its output signal to a value that is the logical and of the inputs. That is, if both of its input signals become 1, then one and-gate-delay time later the and-gate will force its output signal to be 1; otherwise the output will be 0. An or-gate is a similar two-input primitive function box that drives its output signal to a value that is the logical or of the inputs. That is, the output will become 1 if at least one of the input signals is 1; otherwise the output will become 0. Figure 3.24: Primitive functions in the digital logic simulator. Figure 3.25: A half-adder circuit. We can connect primitive functions together to construct more complex functions. To accomplish this we wire the outputs of some function boxes to the inputs of other function boxes. For example, the half-adder circuit 421 shown in Figure 3.25 consists of an or-gate, two and-gates, and an inverter. It takes two input signals, A and B, and has two output signals, S and C. S will become 1 whenever precisely one of A and B is 1, and C will become 1 whenever A and B are both 1. We can see from the figure that, because of the delays involved, the outputs may be generated at different times. Many of the difficulties in the design of digital circuits arise from this fact. We will now build a program for modeling the digital logic circuits we wish to study. The program will construct computational objects modeling the wires, which will “hold” the signals. Function boxes will be modeled by procedures that enforce the correct relationships among the signals. One basic element of our simulation will be a procedure make-wire, which constructs wires. For example, we can construct six wires as follows: (define a (make-wire)) (define b (make-wire)) (define c (make-wire)) (define d (make-wire)) (define e (make-wire)) (define s (make-wire)) We attach a function box to a set of wires by calling a procedure that constructs that kind of box. The arguments to the constructor procedure are the wires to be attached to the box. For example, given that we can construct and-gates, or-gates, and inverters, we can wire together the halfadder shown in Figure 3.25: (or-gate a b d) ok 422 (and-gate a b c) ok (inverter c e) ok (and-gate d e s) ok Better yet, we can explicitly name this operation by defining a procedure half-adder that constructs this circuit, given the four external wires to be attached to the half-adder: (define (half-adder a b s c) (let ((d (make-wire)) (e (make-wire))) (or-gate a b d) (and-gate a b c) (inverter c e) (and-gate d e s) ’ok)) The advantage of making this definition is that we can use half-adder itself as a building block in creating more complex circuits. Figure 3.26, for example, shows a full-adder composed of two half-adders and an or-gate.26 We can construct a full-adder as follows: 26 A full-adder is a basic circuit element used in adding two binary numbers. Here A and B are the bits at corresponding positions in the two numbers to be added, and Cin is the carry bit from the addition one place to the right. The circuit generates SUM, which is the sum bit in the corresponding position, and Cout , which is the carry bit to be propagated to the left. 423 (define (full-adder a b c-in sum c-out) (let ((s (make-wire)) (c1 (make-wire)) (c2 (make-wire))) (half-adder b c-in s c1) (half-adder a s sum c2) (or-gate c1 c2 c-out) ’ok)) Figure 3.26: A full-adder circuit. Having defined full-adder as a procedure, we can now use it as a building block for creating still more complex circuits. (For example, see Exercise 3.30.) In essence, our simulator provides us with the tools to construct a language of circuits. If we adopt the general perspective on languages with which we approached the study of Lisp in Section 1.1, we can say that the primitive function boxes form the primitive elements of the language, that wiring boxes together provides a means of combination, and that specifying wiring patterns as procedures serves as a means of abstraction. Primitive function boxes The primitive function boxes implement the “forces” by which a change in the signal on one wire influences the signals on other wires. To build function boxes, we use the following operations on wires: • (get-signal 〈 wire 〉) returns the current value of the signal on the wire. • (set-signal! 〈 wire 〉〈 new value 〉) changes the value of the signal on the wire to the new value. • (add-action! 〈 wire 〉〈 procedure of no arguments〉) asserts that the designated procedure should be run whenever the signal on the wire changes value. Such procedures are the vehicles by which changes in the signal value on the wire are communicated to other wires. In addition, we will make use of a procedure after-delay that takes a time delay and a procedure to be run and executes the given procedure after the given delay. Using these procedures, we can define the primitive digital logic functions. To connect an input to an output through an inverter, we use add-action! to associate with the input wire a procedure that will be run whenever the signal on the input wire changes value. The procedure computes the logical-not of the input signal, and then, after one inverter-delay, sets the output signal to be this new value: (define (inverter input output) (define (invert-input) (let ((new-value (logical-not (get-signal input)))) (after-delay inverter-delay 425 (lambda () (set-signal! output new-value))))) (add-action! input invert-input) ’ok) (define (logical-not s) (cond ((= s 0) 1) ((= s 1) 0) (else (error "Invalid signal" s)))) An and-gate is a little more complex. The action procedure must be run if either of the inputs to the gate changes. It computes the logical-and (using a procedure analogous to logical-not) of the values of the signals on the input wires and sets up a change to the new value to occur on the output wire after one and-gate-delay. (define (and-gate a1 a2 output) (define (and-action-procedure) (let ((new-value (logical-and (get-signal a1) (get-signal a2)))) (after-delay and-gate-delay (lambda () (set-signal! output new-value))))) (add-action! a1 and-action-procedure) (add-action! a2 and-action-procedure) ’ok) Exercise 3.28: Define an or-gate as a primitive function box. Your or-gate constructor should be similar to and-gate. 426 Exercise 3.29: Another way to construct an or-gate is as a compound digital logic device, built from and-gates and inverters. Define a procedure or-gate that accomplishes this. What is the delay time of the or-gate in terms of and-gate-delay and inverter-delay? Figure 3.27: A ripple-carry adder for n -bit numbers. Exercise 3.30: Figure 3.27 shows a ripple-carry adder formed by stringing together n full-adders. This is the simplest form of parallel adder for adding two n -bit binary numbers. The inputs A 1 , A 2 , A 3 , . . . , A n and B 1 , B 2 , B 3 , . . . , B n are the two binary numbers to be added (each A k and B k is a 0 or a 1). The circuit generates S 1 , S 2 , S 3 , . . . , S n , the n bits of the sum, and C , the carry from the addition. Write a procedure ripple-carry-adder that generates this circuit. The procedure should take as arguments three lists of n wires each—the A k , the B k , and the S k —and also another wire C . The major drawback of the ripple-carry adder is the need to wait for the carry signals to propagate. What is the delay needed to obtain the complete output from an n -bit ripple-carry adder, expressed in terms of the delays for and-gates, or-gates, and inverters? Representing wires A wire in our simulation will be a computational object with two local state variables: a signal-value (initially taken to be 0) and a collection of action-procedures to be run when the signal changes value. We implement the wire, using message-passing style, as a collection of local procedures together with a dispatch procedure that selects the appropriate local operation, just as we did with the simple bank-account object in Section 3.1.1: (define (make-wire) (let ((signal-value 0) (action-procedures ’())) (define (set-my-signal! new-value) (if (not (= signal-value new-value)) (begin (set! signal-value new-value) (call-each action-procedures)) ’done)) (define (accept-action-procedure! proc) (set! action-procedures (cons proc action-procedures)) (proc)) (define (dispatch m) (cond ((eq? m ’get-signal) signal-value) ((eq? m ’set-signal!) set-my-signal!) ((eq? m ’add-action!) accept-action-procedure!) (else (error "Unknown operation - WIRE" m)))) dispatch)) 428 The local procedure set-my-signal! tests whether the new signal value changes the signal on the wire. If so, it runs each of the action procedures, using the following procedure call-each, which calls each of the items in a list of no-argument procedures: (define (call-each procedures) (if (null? procedures) ’done (begin ((car procedures)) (call-each (cdr procedures))))) The local procedure accept-action-procedure! adds the given procedure to the list of procedures to be run, and then runs the new procedure once. (See Exercise 3.31.) With the local dispatch procedure set up as specified, we can provide the following procedures to access the local operations on wires:27 (define (get-signal wire) (wire ’get-signal)) 27 These procedures are simply syntactic sugar that allow us to use ordinary procedu- ral syntax to access the local procedures of objects. It is striking that we can interchange the role of “procedures” and “data” in such a simple way. For example, if we write (wire ’get-signal) we think of wire as a procedure that is called with the message get-signal as input. Alternatively, writing (get-signal wire) encourages us to think of wire as a data object that is the input to a procedure get-signal. The truth of the matter is that, in a language in which we can deal with procedures as objects, there is no fundamental difference between “procedures” and “data,” and we can choose our syntactic sugar to allow us to program in whatever style we choose. (define (set-signal! wire new-value) ((wire ’set-signal!) new-value)) (define (add-action! wire action-procedure) ((wire ’add-action!) action-procedure)) Wires, which have time-varying signals and may be incrementally attached to devices, are typical of mutable objects. We have modeled them as procedures with local state variables that are modified by assignment. When a new wire is created, a new set of state variables is allocated (by the let expression in make-wire) and a new dispatch procedure is constructed and returned, capturing the environment with the new state variables. The wires are shared among the various devices that have been connected to them. Thus, a change made by an interaction with one device will affect all the other devices attached to the wire. The wire communicates the change to its neighbors by calling the action procedures provided to it when the connections were established. The agenda The only thing needed to complete the simulator is after-delay. The idea here is that we maintain a data structure, called an agenda, that contains a schedule of things to do. The following operations are defined for agendas: • (make-agenda) returns a new empty agenda. • (empty-agenda? 〈 agenda 〉) is true if the specified agenda is empty. • (first-agenda-item 〈 agenda 〉) returns the first item on the agenda. • (remove-first-agenda-item! 〈 agenda 〉) modifies the agenda by re- 430 moving the first item. • (add-to-agenda! 〈 time 〉〈 action 〉〈 agenda 〉) modifies the agenda by adding the given action procedure to be run at the specified time. • (current-time 〈 agenda 〉) returns the current simulation time. The particular agenda that we use is denoted by the-agenda. The procedure after-delay adds new elements to the-agenda: (define (after-delay delay action) (add-to-agenda! (+ delay (current-time the-agenda)) action the-agenda)) The simulation is driven by the procedure propagate, which operates on the-agenda, executing each procedure on the agenda in sequence. In general, as the simulation runs, new items will be added to the agenda, and propagate will continue the simulation as long as there are items on the agenda: (define (propagate) (if (empty-agenda? the-agenda) ’done (let ((first-item (first-agenda-item the-agenda))) (first-item) (remove-first-agenda-item! the-agenda) (propagate)))) A sample simulation The following procedure, which places a “probe” on a wire, shows the simulator in action. The probe tells the wire that, whenever its signal changes 431 value, it should print the new signal value, together with the current time and a name that identifies the wire: (define (probe name wire) (add-action! wire (lambda () (newline) (display name) (display " ") (display (current-time the-agenda)) (display " New-value = ") (display (get-signal wire))))) We begin by initializing the agenda and specifying delays for the primitive function boxes: (define the-agenda (make-agenda)) (define inverter-delay 2) (define and-gate-delay 3) (define or-gate-delay Now we define four wires, placing probes on two of them: (define input-1 (make-wire)) (define input-2 (make-wire)) (define sum (make-wire)) (define carry (make-wire)) (probe ’sum sum) sum 0 New-value = 0 (probe ’carry carry) 432 carry 0 New-value = 0 Next we connect the wires in a half-adder circuit (as in Figure 3.25), set the signal on input-1 to 1, and run the simulation: (half-adder input-1 input-2 sum carry) ok (set-signal! input-1 1) done (propagate) sum 8 New-value = 1 The sum signal changes to 1 at time 8. We are now eight time units from the beginning of the simulation. At this point, we can set the signal on input-2 to 1 and allow the values to propagate: (set-signal! input-2 1) done (propagate) carry 11 sum 16 New-value = 1 New-value = 0 The carry changes to 1 at time 11 and the sum changes to 0 at time 16. Exercise 3.31: The internal procedure accept-action-procedure! defined in make-wire specifies that when a new action procedure is added to a wire, the procedure is immediately run. Explain why this initialization is necessary. In particular, trace through the half-adder example in the paragraphs above and 433 say how the system’s response would differ if we had defined accept-action-procedure! as (define (accept-action-procedure! proc) (set! action-procedures (cons proc action-procedures))) Implementing the agenda Finally, we give details of the agenda data structure, which holds the procedures that are scheduled for future execution. The agenda is made up of time segments. Each time segment is a pair consisting of a number (the time) and a queue (see Exercise 3.32) that holds the procedures that are scheduled to be run during that time segment. (define (make-time-segment time queue) (cons time queue)) (define (segment-time s) (car s)) (define (segment-queue s) (cdr s)) We will operate on the time-segment queues using the queue operations described in Section 3.3.2. The agenda itself is a one-dimensional table of time segments. It differs from the tables described in Section 3.3.3 in that the segments will be sorted in order of increasing time. In addition, we store the current time (i.e., the time of the last action that was processed) at the head of the 434 agenda. A newly constructed agenda has no time segments and has a current time of 0:28 (define (make-agenda) (list 0)) (define (current-time agenda) (car agenda)) (define (set-current-time! agenda time) (set-car! agenda time)) (define (segments agenda) (cdr agenda)) (define (set-segments! agenda segments) (set-cdr! agenda segments)) (define (first-segment agenda) (car (segments agenda))) (define (rest-segments agenda) (cdr (segments agenda))) An agenda is empty if it has no time segments: (define (empty-agenda? agenda) (null? (segments agenda))) To add an action to an agenda, we first check if the agenda is empty. If so, we create a time segment for the action and install this in the agenda. Otherwise, we scan the agenda, examining the time of each segment. If we find a segment for our appointed time, we add the action to the associated 28 The agenda is a headed list, like the tables in Section 3.3.3, but since the list is headed by the time, we do not need an additional dummy header (such as the *table* symbol used with tables). 435 queue. If we reach a time later than the one to which we are appointed, we insert a new time segment into the agenda just before it. If we reach the end of the agenda, we must create a new time segment at the end. (define (add-to-agenda! time action agenda) (define (belongs-before? segments) (or (null? segments) (< time (segment-time (car segments))))) (define (make-new-time-segment time action) (let ((q (make-queue))) (insert-queue! q action) (make-time-segment time q))) (define (add-to-segments! segments) (if (= (segment-time (car segments)) time) (insert-queue! (segment-queue (car segments)) action) (let ((rest (cdr segments))) (if (belongs-before? rest) (set-cdr! segments (cons (make-new-time-segment time action) (cdr segments))) (add-to-segments! rest))))) (let ((segments (segments agenda))) (if (belongs-before? segments) (set-segments! agenda (cons (make-new-time-segment time action) 436 segments)) (add-to-segments! segments)))) The procedure that removes the first item from the agenda deletes the item at the front of the queue in the first time segment. If this deletion makes the time segment empty, we remove it from the list of segments:29 (define (remove-first-agenda-item! agenda) (let ((q (segment-queue (first-segment agenda)))) (delete-queue! q) (if (empty-queue? q) (set-segments! agenda (rest-segments The first agenda item is found at the head of the queue in the first time segment. Whenever we extract an item, we also update the current time:30 (define (first-agenda-item agenda) (if (empty-agenda? agenda) (error "Agenda is empty - FIRST-AGENDA-ITEM") (let ((first-seg (first-segment agenda))) (set-current-time! agenda (segment-time first-seg)) (front-queue (segment-queue Exercise 3.32: The procedures to be run during each time segment of the agenda are kept in a queue. Thus, the procedures for each 29 Observe that the if expression in this procedure has no 〈 alternative 〉 expression. Such a “one-armed if statement” is used to decide whether to do something, rather than to select between two expressions. An if expression returns an unspecified value if the predicate is false and there is no 〈alternative 〉. 30 In this way, the current time will always be the time of the action most recently processed. Storing this time at the head of the agenda ensures that it will still be available even if the associated time segment has been deleted. 437 segment are called in the order in which they were added to the agenda (first in, first out). Explain why this order must be used. In particular, trace the behavior of an and-gate whose inputs change from 0, 1 to 1, 0 in the same segment and say how the behavior would differ if we stored a segment’s procedures in an ordinary list, adding and removing procedures only at the front (last in, first out). 3.3.5 Propagation of Constraints Computer programs are traditionally organized as one-directional computations, which perform operations on prespecified arguments to produce desired outputs. On the other hand, we often model systems in terms of relations among quantities. For example, a mathematical model of a mechanical structure might include the information that the deflection d of a metal rod is related to the force F on the rod, the length L of the rod, the cross-sectional area A , and the elastic modulus E via the equation dAE = FL Such an equation is not one-directional. Given any four of the quantities, we can use it to compute the fifth. Yet translating the equation into a traditional computer language would force us to choose one of the quantities to be computed in terms of the other four. Thus, a procedure for computing the area A could not be used to compute the deflection d , even though the computations of A and d arise from the same equation.31 31 Constraint propagation first appeared in the incredibly forward-looking SKETCHPAD system of Ivan Sutherland (1963). A beautiful constraint-propagation based on the Smalltalk language was developed by Alan Borning (1977) at Xerox Palo Alto Research Center. Sussman, Stallman, and Steele applied constraint prop- 438 In this section, we sketch the design of a language that enables us to work in terms of relations themselves. The primitive elements of the language are primitive constraints, which state that certain relations hold between quantities. For example, (adder a b c) specifies that the quantities a , b , and c must be related by the equation a + b = c , (multiplier x y z) expresses the constraint x y = z , and (constant 3.14 x) says that the value of x must be 3.14. Our language provides a means of combining primitive constraints in order to express more complex relations. We combine constraints by constructing constraint networks, in which constraints are joined by connectors. A connector is an object that “holds” a value that may participate in one or more constraints. For example, we know that the relationship between Fahrenheit and Celsius temperatures is 9C = 5(F − 32) Such a constraint can be thought of as a network consisting of primitive adder, multiplier, and constant constraints (Figure 3.28). In the figure, we see on the left a multiplier box with three terminals, labeled m 1, m 2, and p . These connect the multiplier to the rest of the network as follows: The m 1 terminal is linked to a connector C , which will hold the Celsius temperature. The m 2 terminal is linked to a connector w , which is also linked to a constant box that holds 9. The p terminal, which the multiplier box constrains to be the product of m 1 and m 2, is linked to the p terminal of another multiplier box, whose m 2 is connected to a constant 5 and whose m 1 is connected to one of the terms in a sum. agation to electrical circuit analysis (Sussman and Stallman 1975; Sussman and Steele 1980). TK!Solver (Konopasek and Jayaraman 1984) is an extensive modeling environment based on constraints. Figure 3.28: The relation 9C = 5(F − 32) expressed as a constraint network. Computation by such a network proceeds as follows: When a connector is given a value (by the user or by a constraint box to which it is linked), it awakens all of its associated constraints (except for the constraint that just awakened it) to inform them that it has a value. Each awakened constraint box then polls its connectors to see if there is enough information to determine a value for a connector. If so, the box sets that connector, which then awakens all of its associated constraints, and so on. For instance, in conversion between Celsius and Fahrenheit, w , x , and y are immediately set by the constant boxes to 9, 5, and 32, respectively. The connectors awaken the multipliers and the adder, which determine that there is not enough information to proceed. If the user (or some other part of the network) sets C to a value (say 25), the leftmost multiplier will be awakened, and it will set u to 25 · 9 = 225. Then u awakens the second multiplier, which sets v to 45, and v awakens the adder, which sets f to 77. Using the constraint system To use the constraint system to carry out the temperature computation outlined above, we first create two connectors, C and F, by calling the constructor make-connector, and link C and F in an appropriate network: (define C (make-connector)) (define F (make-connector)) (celsius-fahrenheit-converter C F) ok The procedure that creates the network is defined as follows: (define (celsius-fahrenheit-converter c f) (let ((u (make-connector)) (v (make-connector)) (w (make-connector)) (x (make-connector)) (y (make-connector))) (multiplier c w u) (multiplier v x u) (adder v y f) (constant 9 w) (constant 5 x) (constant 32 y) ’ok)) This procedure creates the internal connectors u, v, w, x, and y, and links them as shown in Figure 3.28 using the primitive constraint constructors adder, multiplier, and constant. Just as with the digital-circuit simulator of Section 3.3.4, expressing these combinations of primitive elements in 441 terms of procedures automatically provides our language with a means of abstraction for compound objects. To watch the network in action, we can place probes on the connectors C and F, using a probe procedure similar to the one we used to monitor wires in Section 3.3.4. Placing a probe on a connector will cause a message to be printed whenever the connector is given a value: (probe "Celsius temp" C) (probe "Fahrenheit temp" F) Next we set the value of C to 25. (The third argument to set-value! tells C that this directive comes from the user.) (set-value! C 25 ’user) Probe: Celsius temp = 25 Probe: Fahrenheit temp = 77 done The probe on C awakens and reports the value. C also propagates its value through the network as described above. This sets F to 77, which is reported by the probe on F. Now we can try to set F to a new value, say 212: (set-value! F 212 ’user) Error! Contradiction (77 212) The connector complains that it has sensed a contradiction: Its value is 77, and someone is trying to set it to 212. If we really want to reuse the network with new values, we can tell C to forget its old value: (forget-value! C ’user) Probe: Celsius temp = ? Probe: Fahrenheit temp = ? 442 done C finds that the user, who set its value originally, is now retracting that value, so C agrees to lose its value, as shown by the probe, and informs the rest of the network of this fact. This information eventually propagates to F, which now finds that it has no reason for continuing to believe that its own value is 77. Thus, F also gives up its value, as shown by the probe. Now that F has no value, we are free to set it to 212: (set-value! F 212 ’user) Probe: Fahrenheit temp = 212 Probe: Celsius temp = 100 done This new value, when propagated through the network, forces C to have a value of 100, and this is registered by the probe on C. Notice that the very same network is being used to compute C given F and to compute F given C. This nondirectionality of computation is the distinguishing feature of constraint-based systems. Implementing the constraint system The constraint system is implemented via procedural objects with local state, in a manner very similar to the digital-circuit simulator of Section 3.3.4. Although the primitive objects of the constraint system are somewhat more complex, the overall system is simpler, since there is no concern about agendas and logic delays. The basic operations on connectors are the following: • (has-value? ) tells whether the connector has a value. • (get-value ) returns the connector’s current value. • (set-value! ) indicates that the informant is requesting the connector to set its value to the new value. • (forget-value! ) tells the connector that the retractor is requesting it to forget its value. • (connect ) tells the connector to participate in the new constraint. The connectors communicate with the constraints by means of the procedures inform-about-value, which tells the given constraint that the connector has a value, and inform-about-no-value, which tells the constraint that the connector has lost its value. Adder constructs an adder constraint among summand connectors and a2 and a sum connector. An adder is implemented as a procedure with local state (the procedure me below): (define (adder a1 a2 sum) (define (process-new-value) (cond ((and (has-value? a1) (has-value? a2)) (set-value! sum (+ (get-value a1) (get-value a2)) me)) ((and (has-value? a1) (has-value? sum)) (set-value! a2 (- (get-value sum) (get-value a1)) me)) ((and (has-value? a2) (has-value? sum)) (set-value! a1 (- (get-value sum) (get-value a2)) 444 me)))) (define (process-forget-value) (forget-value! sum me) (forget-value! a1 me) (forget-value! a2 me) (process-new-value)) (define (me request) (cond ((eq? request ’I-have-a-value) (process-new-value)) ((eq? request ’I-lost-my-value) (process-forget-value)) (else (error "Unknown request - ADDER" request)))) (connect a1 me) (connect a2 me) (connect sum me) me) Adder connects the new adder to the designated connectors and returns it as its value. The procedure me, which represents the adder, acts as a dispatch to the local procedures. The following “syntax interfaces” (see Footnote 27 in Section 3.3.4) are used in conjunction with the dispatch: (define (inform-about-value constraint) (constraint ’I-have-a-value)) (define (inform-about-no-value constraint) (constraint ’I-lost-my-value)) 445 The adder’s local procedure process-new-value is called when the adder is informed that one of its connectors has a value. The adder first checks to see if both a1 and a2 have values. If so, it tells sum to set its value to the sum of the two addends. The informant argument to set-value! is me, which is the adder object itself. If a1 and a2 do not both have values, then the adder checks to see if perhaps a1 and sum have values. If so, it sets a2 to the difference of these two. Finally, if a2 and sum have values, this gives the adder enough information to set a1. If the adder is told that one of its connectors has lost a value, it requests that all of its connectors now lose their values. (Only those values that were set by this adder are actually lost.) Then it runs process-new-value. The reason for this last step is that one or more connectors may still have a value (that is, a connector may have had a value that was not originally set by the adder), and these values may need to be propagated back through the adder. A multiplier is very similar to an adder. It will set its product to 0 if either of the factors is 0, even if the other factor is not known. (define (multiplier m1 m2 product) (define (process-new-value) (cond ((or (and (has-value? m1) (= (get-value m1) 0)) (and (has-value? m2) (= (get-value m2) 0))) (set-value! product 0 me)) ((and (has-value? m1) (has-value? m2)) (set-value! product (* (get-value m1) (get-value m2)) me)) ((and (has-value? product) (has-value? m1)) (set-value! m2 446 (/ (get-value product) (get-value m1)) me)) ((and (has-value? product) (has-value? m2)) (set-value! m1 (/ (get-value product) (get-value m2)) me)))) (define (process-forget-value) (forget-value! product me) (forget-value! m1 me) (forget-value! m2 me) (process-new-value)) (define (me request) (cond ((eq? request ’I-have-a-value) (process-new-value)) ((eq? request ’I-lost-my-value) (process-forget-value)) (else (error "Unknown request - MULTIPLIER" request)))) (connect m1 me) (connect m2 me) (connect product me) me) A constant constructor simply sets the value of the designated connector. Any I-have-a-value or I-lost-my-value message sent to the constant box will produce an error. 447 (define (constant value connector) (define (me request) (error "Unknown request - CONSTANT" request)) (connect connector me) (set-value! connector value me) me) Finally, a probe prints a message about the setting or unsetting of the designated connector: (define (probe name connector) (define (print-probe value) (newline) (display "Probe: ") (display name) (display " = ") (display value)) (define (process-new-value) (print-probe (get-value connector))) (define (process-forget-value) (print-probe "?")) (define (me request) (cond ((eq? request ’I-have-a-value) (process-new-value)) ((eq? request ’I-lost-my-value) (process-forget-value)) (else (error "Unknown request - PROBE" request)))) (connect connector me) 448 me) Representing connectors A connector is represented as a procedural object with local state variables value, the current value of the connector; informant, the object that set the connector’s value; and constraints, a list of the constraints in which the connector participates. (define (make-connector) (let ((value false) (informant false) (constraints ’())) (define (set-my-value newval setter) (cond ((not (has-value? me)) (set! value newval) (set! informant setter) (for-each-except setter inform-about-value constraints)) ((not (= value newval)) (error "Contradiction" (list value newval))) (else ’ignored))) (define (forget-my-value retractor) (if (eq? retractor informant) (begin (set! informant false) (for-each-except retractor inform-about-no-value constraints)) ’ignored)) (define (connect new-constraint) 449 (if (not (memq new-constraint constraints)) (set! constraints (cons new-constraint constraints))) (if (has-value? me) (inform-about-value new-constraint)) ’done) (define (me request) (cond ((eq? request ’has-value?) (if informant true false)) ((eq? request ’value) value) ((eq? request ’set-value!) set-my-value) ((eq? request ’forget) forget-my-value) ((eq? request ’connect) connect) (else (error "Unknown operation - CONNECTOR" request)))) me)) The connector’s local procedure set-my-value is called when there is a request to set the connector’s value. If the connector does not currently have a value, it will set its value and remember as informant the constraint that requested the value to be set.32 Then the connector will notify all of its participating constraints except the constraint that requested the value to be set. This is accomplished using the following iterator, which applies a designated procedure to all items in a list except a given one: (define (for-each-except exception procedure list) (define (loop items) 32 The setter might not be a constraint. In our temperature example, we used user as the setter. 450 (cond ((null? items) ’done) ((eq? (car items) exception) (loop (cdr items))) (else (procedure (car items)) (loop (cdr items))))) (loop list)) If a connector is asked to forget its value, it runs the local procedure forget-my-value, which first checks to make sure that the request is com- ing from the same object that set the value originally. If so, the connector informs its associated constraints about the loss of the value. The local procedure connect adds the designated new constraint to the list of constraints if it is not already in that list. Then, if the connector has a value, it informs the new constraint of this fact. The connector’s procedure me serves as a dispatch to the other internal procedures and also represents the connector as an object. The following procedures provide a syntax interface for the dispatch: (define (has-value? connector) (connector ’has-value?)) (define (get-value connector) (connector ’value)) (define (set-value! connector new-value informant) ((connector ’set-value!) new-value informant)) (define (forget-value! connector retractor) ((connector ’forget) retractor)) 451 (define (connect connector new-constraint) ((connector ’connect) new-constraint)) Exercise 3.33: Using primitive multiplier, adder, and constant constraints, define a procedure averager that takes three connectors a, b, and c as inputs and establishes the constraint that the value of c is the average of the values of a and b. Exercise 3.34: Louis Reasoner wants to build a squarer, a constraint device with two terminals such that the value of connector b on the second terminal will always be the square of the value a on the first terminal. He proposes the following simple device made from a multiplier: (define (squarer a b) (multiplier a a b)) There is a serious flaw in this idea. Explain. Exercise 3.35: Ben Bitdiddle tells Louis that one way to avoid the trouble in Exercise 3.34 is to define a squarer as a new primitive constraint. Fill in the missing portions in Ben’s outline for a procedure to implement such a constraint: (define (squarer a b) (define (process-new-value) (if (has-value? b) (if (< (get-value b) 0) (error "square less than 0 - SQUARER" (get-value b)) 〈alternative1〉) 〈alternative2〉)) (define (process-forget-value) 〈body1〉) 452 (define (me request) 〈body2〉) 〈rest of definition〉 me) Exercise 3.36: Suppose we evaluate the following sequence of expressions in the global environment: (define a (make-connector)) (define b (make-connector)) (set-value! a 10 ’user) At some time during evaluation of the set-value!, the following expression from the connector’s local procedure is evaluated: (for-each-except setter inform-about-value constraints) Draw an environment diagram showing the environment in which the above expression is evaluated. Exercise 3.37: The celsius-fahrenheit-converter procedure is cumbersome when compared with a more expression-oriented style of definition, such as (define (celsius-fahrenheit-converter x) (c+ (c* (c/ (cv 9) (cv 5)) x) (cv 32))) (define C (make-connector)) (define F (celsius-fahrenheit-converter 453 Here c+, c*, etc. are the “constraint” versions of the arithmetic operations. For example, c+ takes two connectors as arguments and returns a connector that is related to these by an adder constraint: (define (c+ x y) (let ((z (make-connector))) (adder x y z) z)) Define analogous procedures c-, c*, c/, and cv (constant value) that enable us to define compound constraints as in the converter example above.33 33 The expression-oriented format is convenient because it avoids the need to name the intermediate expressions in a computation. Our original formulation of the constraint language is cumbersome in the same way that many languages are cumbersome when dealing with operations on compound data. For example, if we wanted to compute the product (a + b ) · (c + d ), where the variables represent vectors, we could work in “imperative style,” using procedures that set the values of designated vector arguments but do not themselves return vectors as values: (v-sum a b temp1) (v-sum c d temp2) (v-prod temp1 temp2 answer) Alternatively, we could deal with expressions, using procedures that return vectors as values, and thus avoid explicitly mentioning temp1 and temp2: (define answer (v-prod (v-sum a b) (v-sum c d))) Since Lisp allows us to return compound objects as values of procedures, we can transform our imperative-style constraint language into an expression-oriented style as shown in this exercise. In languages that are impoverished in handling compound objects, such as Algol, Basic, and Pascal (unless one explicitly uses Pascal pointer variables), one is usually stuck with the imperative style 3.4 Concurrency: Time Is of the Essence We’ve seen the power of computational objects with local state as tools for modeling. Yet, as Section 3.1.3 warned, this power extracts a price: the loss of referential transparency, giving rise to a thicket of questions about sameness and change, and the need to abandon the substitution model of evaluation in favor of the more intricate environment model. The central issue lurking beneath the complexity of state, sameness, and change is that by introducing assignment we are forced to admit time into our computational models. Before we introduced assignment, all our programs were timeless, in the sense that any expression that has a value always has the same value. In contrast, recall the example of modeling withdrawals from a bank account and returning the resulting balance, introduced at the beginning of Section 3.1.1: (withdraw 25) 75 (withdraw 25) 50 manipulating compound objects. Given the advantage of the expression-oriented format, one might ask if there is any reason to have implemented the system in imperative style, as we did in this section. One reason is that the non-expressionoriented constraint language provides a handle on constraint objects (e.g., the value of the adder procedure) as well as on connector objects. This is useful if we wish to extend the system with new operations that communicate with constraints directly rather than only indirectly via operations on connectors. Although it is easy to implement the expression-oriented style in terms of the imperative implementation, it is very difficult to do the converse. 455 Here successive evaluations of the same expression yield different values. This behavior arises from the fact that the execution of assignment statements (in this case, assignments to the variable balance) delineates moments in time when values change. The result of evaluating an expression depends not only on the expression itself, but also on whether the evaluation occurs before or after these moments. Building models in terms of computational objects with local state forces us to confront time as an essential concept in programming. We can go further in structuring computational models to match our perception of the physical world. Objects in the world do not change one at a time in sequence. Rather we perceive them as acting concurrently—all at once. So it is often natural to model systems as collections of computational processes that execute concurrently. Just as we can make our programs modular by organizing models in terms of objects with separate local state, it is often appropriate to divide computational models into parts that evolve separately and concurrently. Even if the programs are to be executed on a sequential computer, the practice of writing programs as if they were to be executed concurrently forces the programmer to avoid inessential timing constraints and thus makes programs more modular. In addition to making programs more modular, concurrent computation can provide a speed advantage over sequential computation. Sequential computers execute only one operation at a time, so the amount of time it takes to perform a task is proportional to the total number of operations performed.34 However, if it is possible to decompose a problem into pieces that are relatively independent and need to communicate only rarely, it 34 Most real processors actually execute a few operations at a time, following a strategy called pipelining. Although this technique greatly improves the effective utilization 456 may be possible to allocate pieces to separate computing processors, producing a speed advantage proportional to the number of processors available. Unfortunately, the complexities introduced by assignment become even more problematic in the presence of concurrency. The fact of concurrent execution, either because the world operates in parallel or because our computers do, entails additional complexity in our understanding of time. 3.4.1 The Nature of Time in Concurrent Systems On the surface, time seems straightforward. It is an ordering imposed on events.35 For any events A and B , either A occurs before B , A and B are simultaneous, or A occurs after B . For instance, returning to the bank account example, suppose that Peter withdraws $10 and Paul withdraws $25 from a joint account that initially contains $100, leaving $65 in the account. Depending on the order of the two withdrawals, the sequence of balances in the account is either $100 → $90 → $65 or $100 → $75 → $65 . In a computer implementation of the banking system, this changing sequence of balances could be modeled by successive assignments to a variable balance. In complex situations, however, such a view can be problematic. Suppose that Peter and Paul, and other people besides, are accessing the same bank account through a network of banking machines distributed all over the world. The actual sequence of balances in the account will depend of the hardware, it is used only to speed up the execution of a sequential instruction stream, while retaining the behavior of the sequential program. 35 To quote some graffiti seen on a Cambridge building wall: “Time is a device that was invented to keep everything from happening at once.” 457 critically on the detailed timing of the accesses and the details of the communication among the machines. This indeterminacy in the order of events can pose serious problems in the design of concurrent systems. For instance, suppose that the withdrawals made by Peter and Paul are implemented as two separate processes sharing a common variable balance, each process specified by the procedure given in Section 3.1.1: (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) If the two processes operate independently, then Peter might test the balance and attempt to withdraw a legitimate amount. However, Paul might withdraw some funds in between the time that Peter checks the balance and the time Peter completes the withdrawal, thus invalidating Peter’s test. Things can be worse still. Consider the expression (set! balance (- balance amount)) executed as part of each withdrawal process. This consists of three steps: (1) accessing the value of the balance variable; (2) computing the new balance; (3) setting balance to this new value. If Peter and Paul’s withdrawals execute this statement concurrently, then the two withdrawals might interleave the order in which they access balance and set it to the new value. The timing diagram in Figure 3.29 depicts an order of events where balance starts at 100, Peter withdraws 10, Paul withdraws 25, and yet the final value of balance is 75. As shown in the diagram, the reason for this 458 anomaly is that Paul’s assignment of 75 to balance is made under the assumption that the value of balance to be decremented is 100. That assumption, however, became invalid when Peter changed balance to 90. This is a catastrophic failure for the banking system, because the total amount of money in the system is not conserved. Before the transactions, the total amount of money was $100. Afterwards, Peter has $10, Paul has $25, and the bank has $75.36 The general phenomenon illustrated here is that several processes may share a common state variable. What makes this complicated is that more than one process may be trying to manipulate the shared state at the same time. For the bank account example, during each transaction, each customer should be able to act as if the other customers did not exist. When a customer changes the balance in a way that depends on the balance, he must be able to assume that, just before the moment of change, the balance is still what he thought it was. 36 An even worse failure for this system could occur if the two set! operations at- tempt to change the balance simultaneously, in which case the actual data appearing in memory might end up being a random combination of the information being written by the two processes. Most computers have interlocks on the primitive memory-write operations, which protect against such simultaneous access. Even this seemingly simple kind of protection, however, raises implementation challenges in the design of multiprocessing computers, where elaborate cachecoherence protocols are required to ensure that the various processors will maintain a consistent view of memory contents, despite the fact that data may be replicated (“cached”) among the different processors to increase the speed of memory access. Figure 3.29: Timing diagram showing how interleaving the order of events in two banking withdrawals can lead to an incorrect final balance. Correct behavior of concurrent programs The above example typifies the subtle bugs that can creep into concurrent programs. The root of this complexity lies in the assignments to variables that are shared among the different processes. We already know that we must be careful in writing programs that use set!, because the results of a 460 computation depend on the order in which the assignments occur.37 With concurrent processes we must be especially careful about assignments, because we may not be able to control the order of the assignments made by the different processes. If several such changes might be made concurrently (as with two depositors accessing a joint account) we need some way to ensure that our system behaves correctly. For example, in the case of withdrawals from a joint bank account, we must ensure that money is conserved. To make concurrent programs behave correctly, we may have to place some restrictions on concurrent execution. One possible restriction on concurrency would stipulate that no two operations that change any shared state variables can occur at the same time. This is an extremely stringent requirement. For distributed banking, it would require the system designer to ensure that only one transaction could proceed at a time. This would be both inefficient and overly conservative. Figure 3.30 shows Peter and Paul sharing a bank account, where Paul has a private account as well. The diagram illustrates two withdrawals from the shared account (one by Peter and one by Paul) and a deposit to Paul’s private account.38 The two withdrawals from the shared account must not be concurrent (since both access and update the same account), and Paul’s deposit and withdrawal must not be concurrent (since both access and update the amount in Paul’s wallet). But there 37 The factorial program in Section 3.1.3 illustrates this for a single sequential process. 38 The columns show the contents of Peter’s wallet, the joint account (in Bank1), Paul’s wallet, and Paul’s private account (in Bank2), before and after each withdrawal (W) and deposit (D). Peter withdraws $10 from Bank1; Paul deposits $5 in Bank2, then withdraws $25 from Bank1. 461 should be no problem permitting Paul’s deposit to his private account to proceed concurrently with Peter’s withdrawal from the shared account. Figure 3.30: Concurrent deposits and withdrawals from a joint account in Bank1 and a private account in Bank2. A less stringent restriction on concurrency would ensure that a concurrent system produces the same result as if the processes had run sequentially in some order. There are two important aspects to this requirement. First, it does not require the processes to actually run sequentially, but only to produce results that are the same as if they had run sequentially. For 462 the example in Figure 3.30, the designer of the bank account system can safely allow Paul’s deposit and Peter’s withdrawal to happen concurrently, because the net result will be the same as if the two operations had happened sequentially. Second, there may be more than one possible “correct” result produced by a concurrent program, because we require only that the result be the same as for some sequential order. For example, suppose that Peter and Paul’s joint account starts out with $100, and Peter deposits $40 while Paul concurrently withdraws half the money in the account. Then sequential execution could result in the account balance being either $70 or $90 (see Exercise 3.38).39 There are still weaker requirements for correct execution of concurrent programs. A program for simulating diffusion (say, the flow of heat in an object) might consist of a large number of processes, each one representing a small volume of space, that update their values concurrently. Each process repeatedly changes its value to the average of its own value and its neighbors’ values. This algorithm converges to the right answer independent of the order in which the operations are done; there is no need for any restrictions on concurrent use of the shared values. Exercise 3.38: Suppose that Peter, Paul, and Mary share a joint bank account that initially contains $100. Concurrently, Peter deposits $10, Paul withdraws $20, and Mary withdraws half the money in the account, by executing the following commands: Peter: (set! balance (+ balance 10)) 39 A more formal way to express this idea is to say that concurrent programs are inher- ently nondeterministic. That is, they are described not by single-valued functions, but by functions whose results are sets of possible values. In Section 4.3 we will study a language for expressing nondeterministic computations. 463 Paul: (set! balance (- balance 20)) (set! balance (- balance (/ balance 2))) a. List all the different possible values for balance after these three transactions have been completed, assuming that the banking system forces the three processes to run sequentially in some order. b. What are some other values that could be produced if the system allows the processes to be interleaved? Draw timing diagrams like the one in Figure 3.29 to explain how these values can 3.4.2 Mechanisms for Controlling Concurrency We’ve seen that the difficulty in dealing with concurrent processes is rooted in the need to consider the interleaving of the order of events in the different processes. For example, suppose we have two processes, one with three ordered events (a, b, c ) and one with three ordered events (x, y, z ). If the two processes run concurrently, with no constraints on how their execution is interleaved, then there are 20 different possible orderings for the events that are consistent with the individual orderings for the two processes: (a,b,c,x,y,z) As programmers designing this system, we would have to consider the effects of each of these 20 orderings and check that each behavior is ac- 464 ceptable. Such an approach rapidly becomes unwieldy as the numbers of processes and events increase. A more practical approach to the design of concurrent systems is to devise general mechanisms that allow us to constrain the interleaving of concurrent processes so that we can be sure that the program behavior is correct. Many mechanisms have been developed for this purpose. In this section, we describe one of them, the serializer. Serializing access to shared state Serialization implements the following idea: Processes will execute concurrently, but there will be certain collections of procedures that cannot be executed concurrently. More precisely, serialization creates distinguished sets of procedures such that only one execution of a procedure in each serialized set is permitted to happen at a time. If some procedure in the set is being executed, then a process that attempts to execute any procedure in the set will be forced to wait until the first execution has finished. We can use serialization to control access to shared variables. For example, if we want to update a shared variable based on the previous value of that variable, we put the access to the previous value of the variable and the assignment of the new value to the variable in the same procedure. We then ensure that no other procedure that assigns to the variable can run concurrently with this procedure by serializing all of these procedures with the same serializer. This guarantees that the value of the variable cannot be changed between an access and the corresponding assignment. Serializers in Scheme To make the above mechanism more concrete, suppose that we have extended Scheme to include a procedure called parallel-execute: 465 (parallel-execute 〈p 1 〉〈p 2 〉 ... 〈p k 〉) Each 〈p 〉 must be a procedure of no arguments. Parallel-execute creates a separate process for each 〈p 〉, which applies 〈p 〉 (to no arguments). These processes all run concurrently.40 As an example of how this is used, consider (define x 10) (parallel-execute (lambda () (set! x (* x x))) (lambda () (set! x (+ x 1)))) This creates two concurrent processes—P 1 , which sets x to x times x, and P 2 , which increments x. After execution is complete, x will be left with one of five possible values, depending on the interleaving of the events of P 1 and P 2 : 101: P 1 sets x to 100 and then P 2 increments x to 101. 121: P 2 increments x to 11 and then P 1 sets x to x times x. 110: P 2 changes x from 10 to 11 between the two times that P 1 accesses the value of x during the evaluation of (* x x). 11: P 2 accesses x, then P 1 sets x to 100, then P 2 sets x. 40 Parallel-execute is not part of standard Scheme, but it can be implemented in MIT Scheme. In our implementation, the new concurrent processes also run con- currently with the original Scheme process. Also, in our implementation, the value returned by parallel-execute is a special control object that can be used to halt the newly created processes. 466 100: P 1 accesses x (twice), then P 2 sets x to 11, then P 1 sets x. We can constrain the concurrency by using serialized procedures, which are created by serializers. Serializers are constructed by make-serializer, whose implementation is given below. A serializer takes a procedure as argument and returns a serialized procedure that behaves like the original procedure. All calls to a given serializer return serialized procedures in the same set. Thus, in contrast to the example above, executing (define x 10) (define s (make-serializer)) (parallel-execute (s (lambda () (set! x (* x x)))) (s (lambda () (set! x (+ x 1))))) can produce only two possible values for x, 101 or 121. The other possibilities are eliminated, because the execution of P 1 and P 2 cannot be interleaved. Here is a version of the make-account procedure from Section 3.1.1, where the deposits and withdrawals have been serialized: (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) 467 (set! balance (+ balance amount)) balance) (let ((protected (make-serializer))) (define (dispatch m) (cond ((eq? m ’withdraw) (protected withdraw)) ((eq? m ’deposit) (protected deposit)) ((eq? m ’balance) balance) (else (error "Unknown request - MAKE-ACCOUNT" m)))) dispatch)) With this implementation, two processes cannot be withdrawing from or depositing into a single account concurrently. This eliminates the source of the error illustrated in Figure 3.29, where Peter changes the account balance between the times when Paul accesses the balance to compute the new value and when Paul actually performs the assignment. On the other hand, each account has its own serializer, so that deposits and withdrawals for different accounts can proceed concurrently. Exercise 3.39: Which of the five possibilities in the parallel execution shown above remain if we instead serialize execution as follows: (define x 10) (define s (make-serializer)) (parallel-execute (lambda () (set! x ((s (lambda () (* x x)))))) (s (lambda () (set! x (+ x 1))))) 468 Exercise 3.40: Give all possible values of x that can result from executing (define x 10) (parallel-execute (lambda () (set! x (* x x))) (lambda () (set! x (* x x x)))) Which of these possibilities remain if we instead use serialized procedures: (define x 10) (define s (make-serializer)) (parallel-execute (s (lambda () (set! x (* x x)))) (s (lambda () (set! x (* x x Exercise 3.41: Ben Bitdiddle worries that it would be better to implement the bank account as follows (where the commented line has been changed): (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) ;; continued on next page (let ((protected (make-serializer))) (define (dispatch m) (cond ((eq? m ’withdraw) (protected withdraw)) ((eq? m ’deposit) (protected deposit)) ((eq? m ’balance) ((protected (lambda () balance)))) ; serialized (else (error "Unknown request - MAKE-ACCOUNT" m)))) dispatch)) because allowing unserialized access to the bank balance can result in anomalous behavior. Do you agree? Is there any scenario that demonstrates Ben’s concern? Exercise 3.42: Ben Bitdiddle suggests that it’s a waste of time to create a new serialized procedure in response to every withdraw and deposit message. He says that make-account could be changed so that the calls to protected are done outside the dispatch procedure. That is, an account would return the same serialized procedure (which was created at the same time as the account) each time it is asked for a withdrawal procedure. (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) 470 "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (let ((protected (make-serializer))) (let ((protected-withdraw (protected withdraw)) (protected-deposit (protected deposit))) (define (dispatch m) (cond ((eq? m ’withdraw) protected-withdraw) ((eq? m ’deposit) protected-deposit) ((eq? m ’balance) balance) (else (error "Unknown request - MAKE-ACCOUNT" m)))) dispatch))) Is this a safe change to make? In particular, is there any difference in what concurrency is allowed by these two versions of make-account ? Complexity of using multiple shared resources Serializers provide a powerful abstraction that helps isolate the complexities of concurrent programs so that they can be dealt with carefully and (hopefully) correctly. However, while using serializers is relatively straightforward when there is only a single shared resource (such as a single bank account), concurrent programming can be treacherously difficult when there are multiple shared resources. 471 To illustrate one of the difficulties that can arise, suppose we wish to swap the balances in two bank accounts. We access each account to find the balance, compute the difference between the balances, withdraw this difference from one account, and deposit it in the other account. We could implement this as follows:41 (define (exchange account1 account2) (let ((difference (- (account1 ’balance) (account2 ’balance)))) ((account1 ’withdraw) difference) ((account2 ’deposit) difference))) This procedure works well when only a single process is trying to do the exchange. Suppose, however, that Peter and Paul both have access to accounts a 1, a 2, and a 3, and that Peter exchanges a 1 and a 2 while Paul concurrently exchanges a 1 and a 3. Even with account deposits and withdrawals serialized for individual accounts (as in the make-account procedure shown above in this section), exchange can still produce incorrect results. For example, Peter might compute the difference in the balances for a 1 and a 2, but then Paul might change the balance in a 1 before Peter is able to complete the exchange.42 For correct behavior, we must arrange for the exchange procedure to lock out any other concurrent accesses to the accounts during the entire time of the exchange. 41 We have simplified exchange by exploiting the fact that our deposit message ac- cepts negative amounts. (This is a serious bug in our banking system!) 42 If the account balances start out as $10, $20, and $30, then after any number of concurrent exchanges, the balances should still be $10, $20, and $30 in some order. Serializing the deposits to individual accounts is not sufficient to guarantee this. See Exercise 3.43. 472 One way we can accomplish this is by using both accounts’ serializers to serialize the entire exchange procedure. To do this, we will arrange for access to an account’s serializer. Note that we are deliberately breaking the modularity of the bank-account object by exposing the serializer. The following version of make-account is identical to the original version given in Section 3.1.1, except that a serializer is provided to protect the balance variable, and the serializer is exported via message passing: (define (make-account-and-serializer balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (let ((balance-serializer (make-serializer))) (define (dispatch m) (cond ((eq? m ’withdraw) withdraw) ((eq? m ’deposit) deposit) ((eq? m ’balance) balance) ((eq? m ’serializer) balance-serializer) (else (error "Unknown request - MAKE-ACCOUNT" m)))) dispatch)) We can use this to do serialized deposits and withdrawals. However, unlike our earlier serialized account, it is now the responsibility of each user 473 of bank-account objects to explicitly manage the serialization, for example as follows:43 (define (deposit account amount) (let ((s (account ’serializer)) (d (account ’deposit))) ((s d) amount))) Exporting the serializer in this way gives us enough flexibility to implement a serialized exchange program. We simply serialize the original exchange procedure with the serializers for both accounts: (define (serialized-exchange account1 account2) (let ((serializer1 (account1 ’serializer)) (serializer2 (account2 ’serializer))) ((serializer1 (serializer2 exchange)) account1 account2))) Exercise 3.43: Suppose that the balances in three accounts start out as $10, $20, and $30, and that multiple processes run, exchanging the balances in the accounts. Argue that if the processes are run sequentially, after any number of concurrent exchanges, the account balances should be $10, $20, and $30 in some order. Draw a timing diagram like the one in Figure 3.29 to show how this condition can be violated if the exchanges are implemented using the first version of the account-exchange program in this section. On the other hand, argue that even with this exchange program, the 43 Exercise 3.45 investigates why deposits and withdrawals are no longer automati- cally serialized by the account. 474 sum of the balances in the accounts will be preserved. Draw a timing diagram to show how even this condition would be violated if we did not serialize the transactions on individual accounts. Exercise 3.44: Consider the problem of transferring an amount from one account to another. Ben Bitdiddle claims that this can be accomplished with the following procedure, even if there are multiple people concurrently transferring money among multiple accounts, using any account mechanism that serializes deposit and withdrawal transactions, for example, the version of make-account in the text above. (define (transfer from-account to-account amount) ((from-account ’withdraw) amount) ((to-account ’deposit) amount)) Louis Reasoner claims that there is a problem here, and that we need to use a more sophisticated method, such as the one required for dealing with the exchange problem. Is Louis right? If not, what is the essential difference between the transfer problem and the exchange problem? (You should assume that the balance in from-account is at least amount.) Exercise 3.45: Louis Reasoner thinks our bank-account system is unnecessarily complex and error-prone now that deposits and withdrawals aren’t automatically serialized. He suggests that make-account-and-serializer should have exported the serializer (for use by such procedures as serialized-exchange) in addition to (rather than instead of ) using it to serialize accounts and deposits as make-account did. accounts as follows: He proposes to redefine 475 (define (make-account-and-serializer balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (let ((balance-serializer (make-serializer))) (define (dispatch m) (cond ((eq? m ’withdraw) (balance-serializer withdraw)) ((eq? m ’deposit) (balance-serializer deposit)) ((eq? m ’balance) balance) ((eq? m ’serializer) balance-serializer) (else (error "Unknown request - MAKE-ACCOUNT" m)))) dispatch)) Then deposits are handled as with the original make-account: (define (deposit account amount) ((account ’deposit) amount)) Explain what is wrong with Louis’s reasoning. In particular, consider what happens when serialized-exchange is called. Implementing serializers We implement serializers in terms of a more primitive synchronization mechanism called a mutex. A mutex is an object that supports two operations—the mutex can be acquired, and the mutex can be released. Once a mutex has been acquired, no other acquire operations on that mutex may proceed until the mutex is released.44 In our implementation, each serializer has an associated mutex. Given a procedure p, the serializer returns a procedure that acquires the mutex, runs p, and then releases the mutex. This ensures that only one of the procedures produced by the serializer can be running at once, which is precisely the serialization property that we need to guarantee. (define (make-serializer) (let ((mutex (make-mutex))) (lambda (p) (define (serialized-p . args) (mutex ’acquire) (let ((val (apply p args))) 44 The term “mutex” is an abbreviation for mutual exclusion. The general problem of arranging a mechanism that permits concurrent processes to safely share resources is called the mutual exclusion problem. Our mutex is a simple variant of the semaphore mechanism (see Exercise 3.47), which was introduced in the “THE” Multiprogramming System developed at the Technological University of Eindhoven and named for the university’s initials in Dutch (Dijkstra 1968a). The acquire and release operations were originally called P and V, from the Dutch words passeren (to pass) and vrijgeven (to release), in reference to the semaphores used on railroad systems. Dijkstra’s classic exposition (1968b) was one of the first to clearly present the issues of concurrency control, and showed how to use semaphores to handle a variety of concurrency problems. 477 (mutex ’release) val)) serialized-p))) The mutex is a mutable object (here we’ll use a one-element list, which we’ll refer to as a cell) that can hold the value true or false. When the value is false, the mutex is available to be acquired. When the value is true, the mutex is unavailable, and any process that attempts to acquire the mutex must wait. Our mutex constructor make-mutex begins by initializing the cell contents to false. To acquire the mutex, we test the cell. If the mutex is available, we set the cell contents to true and proceed. Otherwise, we wait in a loop, attempting to acquire over and over again, until we find that the mutex is available.45 To release the mutex, we set the cell contents to false. (define (make-mutex) (let ((cell (list false))) (define (the-mutex m) (cond ((eq? m ’acquire) (if (test-and-set! cell) (the-mutex ’acquire))) ; retry ((eq? m ’release) (clear! cell)))) the-mutex)) (define (clear! cell) 45 In most time-shared operating systems, processes that are blocked by a mutex do not waste time “busy-waiting” as above. Instead, the system schedules another process to run while the first is waiting, and the blocked process is awakened when the mutex becomes available. 478 (set-car! cell false)) Test-and-set! tests the cell and returns the result of the test. In ad- dition, if the test was false, test-and-set! sets the cell contents to true before returning false. We can express this behavior as the following procedure: (define (test-and-set! cell) (if (car cell) true (begin (set-car! cell true) false))) However, this implementation of test-and-set! does not suffice as it stands. There is a crucial subtlety here, which is the essential place where concurrency control enters the system: The test-and-set! operation must be performed atomically. That is, we must guarantee that, once a process has tested the cell and found it to be false, the cell contents will actually be set to true before any other process can test the cell. If we do not make this guarantee, then the mutex can fail in a way similar to the bank-account failure in Figure 3.29. (See Exercise 3.46.) The actual implementation of test-and-set! depends on the details of how our system runs concurrent processes. For example, we might be executing concurrent processes on a sequential processor using a time-slicing mechanism that cycles through the processes, permitting each process to run for a short time before interrupting it and moving on to the next process. In that case, test-and-set! can work by disabling time slicing during 479 the testing and setting.46 Alternatively, multiprocessing computers provide instructions that support atomic operations directly in hardware.47 Exercise 3.46: Suppose that we implement test-and-set! using an ordinary procedure as shown in the text, without attempting to 46 In MIT Scheme for a single processor, which uses a time-slicing model, test-and- set! can be implemented as follows: (define (test-and-set! cell) (without-interrupts (lambda () (if (car cell) true (begin (set-car! cell true) false))))) disables time-slicing interrupts while its procedure argument is being executed. Without-interrupts 47 There are many variants of such instructions—including test-and-set, test-and- clear, swap, compare-and-exchange, load-reserve, and store-conditional—whose design must be carefully matched to the machine’s processor-memory interface. One issue that arises here is to determine what happens if two processes attempt to acquire the same resource at exactly the same time by using such an instruction. This requires some mechanism for making a decision about which process gets control. Such a mechanism is called an arbiter. Arbiters usually boil down to some sort of hardware device. Unfortunately, it is possible to prove that one cannot physically construct a fair arbiter that works 100% of the time unless one allows the arbiter an arbitrarily long time to make its decision. The fundamental phenomenon here was originally observed by the fourteenth-century French philosopher Jean Buridan in his commentary on Aristotle’s De caelo. Buridan argued that a perfectly rational dog placed between two equally attractive sources of food will starve to death, because it is incapable of deciding which to go to first. 480 make the operation atomic. Draw a timing diagram like the one in Figure 3.29 to demonstrate how the mutex implementation can fail by allowing two processes to acquire the mutex at the same time. Exercise 3.47: A semaphore (of size n ) is a generalization of a mutex. Like a mutex, a semaphore supports acquire and release operations, but it is more general in that up to n processes can acquire it concurrently. Additional processes that attempt to acquire the semaphore must wait for release operations. Give implementations of semaphores a. in terms of mutexes b. in terms of atomic test-and-set! operations. Deadlock Now that we have seen how to implement serializers, we can see that account exchanging still has a problem, even with the serialized-exchange procedure above. Imagine that Peter attempts to exchange a 1 with a 2 while Paul concurrently attempts to exchange a 2 with a 1. Suppose that Peter’s process reaches the point where it has entered a serialized procedure protecting a 1 and, just after that, Paul’s process enters a serialized procedure protecting a 2. Now Peter cannot proceed (to enter a serialized procedure protecting a 2) until Paul exits the serialized procedure protecting a 2. Similarly, Paul cannot proceed until Peter exits the serialized procedure protecting a 1. Each process is stalled forever, waiting for the other. This situation is called a deadlock. Deadlock is always a danger in systems that provide concurrent access to multiple shared resources. 481 One way to avoid the deadlock in this situation is to give each account a unique identification number and rewrite serialized-exchange so that a process will always attempt to enter a procedure protecting the lowest-numbered account first. Although this method works well for the exchange problem, there are other situations that require more sophisticated deadlock-avoidance techniques, or where deadlock cannot be avoided at all. (See Exercise 3.48 and Exercise 3.49.)48 Exercise 3.48: Explain in detail why the deadlock-avoidance method described above, (i.e., the accounts are numbered, and each process attempts to acquire the smaller-numbered account first) avoids deadlock in the exchange problem. Rewrite (You will also need to modify make-account so that each account is created with a number, which can be accessed by sending an appropriate serialized-exchange to incorporate this idea. message.) Exercise 3.49: Give a scenario where the deadlock-avoidance mechanism described above does not work. (Hint: In the exchange problem, each process knows in advance which accounts it will need to get access to. Consider a situation where a process must get access to some shared resources before it can know which additional shared resources it will require.) 48 The general technique for avoiding deadlock by numbering the shared resources and acquiring them in order is due to Havender (1968). Situations where deadlock cannot be avoided require deadlock-recovery methods, which entail having processes “back out” of the deadlocked state and try again. Deadlock-recovery mechanisms are widely used in database management systems, a topic that is treated in detail in Gray and Reuter 1993. Concurrency, time, and communication We’ve seen how programming concurrent systems requires controlling the ordering of events when different processes access shared state, and we’ve seen how to achieve this control through judicious use of serializers. But the problems of concurrency lie deeper than this, because, from a fundamental point of view, it’s not always clear what is meant by “shared state.” Mechanisms such as test-and-set! require processes to examine a global shared flag at arbitrary times. This is problematic and inefficient to implement in modern high-speed processors, where due to optimization techniques such as pipelining and cached memory, the contents of memory may not be in a consistent state at every instant. In contemporary multiprocessing systems, therefore, the serializer paradigm is being supplanted by new approaches to concurrency control.49 The problematic aspects of shared state also arise in large, distributed systems. For instance, imagine a distributed banking system where individual branch banks maintain local values for bank balances and periodically compare these with values maintained by other branches. In such a system the value of “the account balance” would be undetermined, except right after synchronization. If Peter deposits money in an account he 49 One such alternative to serialization is called barrier synchronization. The pro- grammer permits concurrent processes to execute as they please, but establishes certain synchronization points (“barriers”) through which no process can proceed until all the processes have reached the barrier. Modern processors provide machine instructions that permit programmers to establish synchronization points at places where consistency is required. The PowerPC, for example, includes for this purpose two instructions called SYNC and EIEIO (Enforced In-order Execution of Input/Output). 483 holds jointly with Paul, when should we say that the account balance has changed—when the balance in the local branch changes, or not until after the synchronization? And if Paul accesses the account from a different branch, what are the reasonable constraints to place on the banking system such that the behavior is “correct”? The only thing that might matter for correctness is the behavior observed by Peter and Paul individually and the “state” of the account immediately after synchronization. Questions about the “real” account balance or the order of events between synchronizations may be irrelevant or meaningless.50 The basic phenomenon here is that synchronizing different processes, establishing shared state, or imposing an order on events requires communication among the processes. In essence, any notion of time in concurrency control must be intimately tied to communication.51 It is intriguing that a similar connection between time and communication also arises in the Theory of Relativity, where the speed of light (the fastest signal that can be used to synchronize events) is a fundamental constant relating time and space. The complexities we encounter in dealing with time and state in our computational models may in fact mirror a fundamental complexity of the physical universe. 50 This may seem like a strange point of view, but there are systems that work this way. International charges to credit-card accounts, for example, are normally cleared on a per-country basis, and the charges made in different countries are periodically reconciled. Thus the account balance may be different in different countries. 51 For distributed systems, this perspective was pursued by Lamport (1978), who showed how to use communication to establish “global clocks” that can be used to establish orderings on events in distributed systems. 3.5 Streams We’ve gained a good understanding of assignment as a tool in modeling, as well as an appreciation of the complex problems that assignment raises. It is time to ask whether we could have gone about things in a different way, so as to avoid some of these problems. In this section, we explore an alternative approach to modeling state, based on data structures called streams. As we shall see, streams can mitigate some of the complexity of modeling state. Let’s step back and review where this complexity comes from. In an attempt to model real-world phenomena, we made some apparently reasonable decisions: We modeled real-world objects with local state by computational objects with local variables. We identified time variation in the real world with time variation in the computer. We implemented the time variation of the states of the model objects in the computer with assignments to the local variables of the model objects. Is there another approach? Can we avoid identifying time in the computer with time in the modeled world? Must we make the model change with time in order to model phenomena in a changing world? Think about the issue in terms of mathematical functions. We can describe the timevarying behavior of a quantity x as a function of time x (t ). If we concentrate on x instant by instant, we think of it as a changing quantity. Yet if we concentrate on the entire time history of values, we do not emphasize change—the function itself does not change.52 52 Physicists sometimes adopt this view by introducing the “world lines” of particles as a device for reasoning about motion. We’ve also already mentioned (Section 2.2.3) that this is the natural way to think about signal-processing systems. We will explore applications of streams to signal processing in Section 3.5.3. 485 If time is measured in discrete steps, then we can model a time function as a (possibly infinite) sequence. In this section, we will see how to model change in terms of sequences that represent the time histories of the systems being modeled. To accomplish this, we introduce new data structures called streams. From an abstract point of view, a stream is simply a sequence. However, we will find that the straightforward implementation of streams as lists (as in Section 2.2.1) doesn’t fully reveal the power of stream processing. As an alternative, we introduce the technique of delayed evaluation, which enables us to represent very large (even infinite) sequences as streams. Stream processing lets us model systems that have state without ever using assignment or mutable data. This has important implications, both theoretical and practical, because we can build models that avoid the drawbacks inherent in introducing assignment. On the other hand, the stream framework raises difficulties of its own, and the question of which modeling technique leads to more modular and more easily maintained systems remains open. 3.5.1 Streams Are Delayed Lists As we saw in Section 2.2.3, sequences can serve as standard interfaces for combining program modules. We formulated powerful abstractions for manipulating sequences, such as map, filter, and accumulate, that capture a wide variety of operations in a manner that is both succinct and elegant. Unfortunately, if we represent sequences as lists, this elegance is bought at the price of severe inefficiency with respect to both the time and space required by our computations. When we represent manipulations on se- 486 quences as transformations of lists, our programs must construct and copy data structures (which may be huge) at every step of a process. To see why this is true, let us compare two programs for computing the sum of all the prime numbers in an interval. The first program is written in standard iterative style:53 (define (sum-primes a b) (define (iter count accum) (cond ((> count b) accum) ((prime? count) (iter (+ count 1) (+ count accum))) (else (iter (+ count 1) accum)))) (iter a 0)) The second program performs the same computation using the sequence operations of Section 2.2.3: (define (sum-primes a b) (accumulate + 0 (filter prime? (enumerate-interval a b)))) In carrying out the computation, the first program needs to store only the sum being accumulated. In contrast, the filter in the second program cannot do any testing until enumerate-interval has constructed a complete list of the numbers in the interval. The filter generates another list, which in turn is passed to accumulate before being collapsed to form a sum. Such large intermediate storage is not needed by the first program, 53 Assume that we have a predicate prime? (e.g., as in Section 1.2.6) that tests for 487 which we can think of as enumerating the interval incrementally, adding each prime to the sum as it is generated. The inefficiency in using lists becomes painfully apparent if we use the sequence paradigm to compute the second prime in the interval from 10,000 to 1,000,000 by evaluating the expression (car (cdr (filter prime? (enumerate-interval 10000 1000000)))) This expression does find the second prime, but the computational overhead is outrageous. We construct a list of almost a million integers, filter this list by testing each element for primality, and then ignore almost all of the result. In a more traditional programming style, we would interleave the enumeration and the filtering, and stop when we reached the second prime. Streams are a clever idea that allows one to use sequence manipulations without incurring the costs of manipulating sequences as lists. With streams we can achieve the best of both worlds: We can formulate programs elegantly as sequence manipulations, while attaining the efficiency of incremental computation. The basic idea is to arrange to construct a stream only partially, and to pass the partial construction to the program that consumes the stream. If the consumer attempts to access a part of the stream that has not yet been constructed, the stream will automatically construct just enough more of itself to produce the required part, thus preserving the illusion that the entire stream exists. In other words, although we will write programs as if we were processing complete sequences, we design our stream implementation to automatically and transparently interleave the construction of the stream with its use. 488 On the surface, streams are just lists with different names for the procedures that manipulate them. There is a constructor, cons-stream, and two selectors, stream-car and stream-cdr, which satisfy the constraints (stream-car (cons-stream x y)) = x (stream-cdr (cons-stream x y)) = y There is a distinguishable object, the-empty-stream, which cannot be the result of any cons-stream operation, and which can be identified with the predicate stream-null?.54 Thus we can make and use streams, in just the same way as we can make and use lists, to represent aggregate data arranged in a sequence. In particular, we can build stream analogs of the list operations from Chapter 2, such as list-ref, map, and for-each:55 (define (stream-ref s n) (if (= n 0) (stream-car s) (stream-ref (stream-cdr s) (- n 1)))) (define (stream-map proc s) (if (stream-null? s) the-empty-stream 54 In the MIT implementation, the-empty-stream is the same as the empty list ’(), and stream-null? is the same as null?. 55 This should bother you. The fact that we are defining such similar procedures for streams and lists indicates that we are missing some underlying abstraction. Unfortunately, in order to exploit this abstraction, we will need to exert finer control over the process of evaluation than we can at present. We will discuss this point further at the end of Section 3.5.4. In Section 4.2, we’ll develop a framework that unifies lists and streams. 489 (cons-stream (proc (stream-car s)) (stream-map proc (stream-cdr s))))) (define (stream-for-each proc s) (if (stream-null? s) ’done (begin (proc (stream-car s)) (stream-for-each proc (stream-cdr s))))) Stream-for-each is useful for viewing streams: (define (display-stream s) (stream-for-each display-line s)) (define (display-line x) (newline) (display x)) To make the stream implementation automatically and transparently interleave the construction of a stream with its use, we will arrange for the cdr of a stream to be evaluated when it is accessed by the stream-cdr procedure rather than when the stream is constructed by cons-stream. This implementation choice is reminiscent of our discussion of rational numbers in Section 2.1.2, where we saw that we can choose to implement rational numbers so that the reduction of numerator and denominator to lowest terms is performed either at construction time or at selection time. The two rational-number implementations produce the same data abstraction, but the choice has an effect on efficiency. There is a similar relationship between streams and ordinary lists. As a data abstraction, streams are the same as lists. The difference is the time at which the elements are evalu- 490 ated. With ordinary lists, both the car and the cdr are evaluated at construction time. With streams, the cdr is evaluated at selection time. Our implementation of streams will be based on a special form called delay. Evaluating (delay <exp>) does not evaluate the expression 〈exp 〉, but rather returns a so-called delayed object, which we can think of as a “promise” to evaluate 〈exp 〉 at some future time. As a companion to delay, there is a procedure called force that takes a delayed object as argument and performs the evaluation—in effect, forcing the delay to fulfill its promise. We will see below how delay and force can be implemented, but first let us use these to construct streams. Cons-stream is a special form defined so that (cons-stream 〈a〉〈b〉) is equivalent to (cons 〈a〉 (delay 〈b〉)) What this means is that we will construct streams using pairs. However, rather than placing the value of the rest of the stream into the cdr of the pair we will put there a promise to compute the rest if it is ever requested. Stream-car and stream-cdr can now be defined as procedures: (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream))) Stream-car selects the car of the pair; stream-cdr selects the cdr of the pair and evaluates the delayed expression found there to obtain the rest of the stream.56 56 Although stream-car and stream-cdr can be defined as procedures, cons-stream must be a special form. If cons-stream were a procedure, then, according to our The stream implementation in action To see how this implementation behaves, let us analyze the “outrageous” prime computation we saw above, reformulated in terms of streams: (stream-car (stream-cdr (stream-filter prime? (stream-enumerate-interval 10000 1000000)))) We will see that it does indeed work efficiently. We begin by calling stream-enumerate-interval with the arguments 10,000 and 1,000,000. Stream-enumerate-interval is the stream analog of enumerate-interval (Section 2.2.3): (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) and thus the result returned by stream-enumerate-interval, formed by the cons-stream, is57 model of evaluation, evaluating (cons-stream ) would automatically cause 〈b 〉 to be evaluated, which is precisely what we do not want to happen. For the same reason, delay must be a special form, though force can be an ordinary procedure. 57 The numbers shown here do not really appear in the delayed expression. What ac- tually appears is the original expression, in an environment in which the variables are bound to the appropriate numbers. For example, (+ low 1) with low bound to 10,000 actually appears where 10001 is shown. 492 (cons 10000 (delay (stream-enumerate-interval 10001 1000000))) That is, stream-enumerate-interval returns a stream represented as a pair whose car is 10,000 and whose cdr is a promise to enumerate more of the interval if so requested. This stream is now filtered for primes, using the stream analog of the filter procedure (Section 2.2.3): (define (stream-filter pred stream) (cond ((stream-null? stream) the-empty-stream) ((pred (stream-car stream)) (cons-stream (stream-car stream) (stream-filter pred (stream-cdr stream)))) (else (stream-filter pred (stream-cdr stream))))) Stream-filter tests the stream-car of the stream (the car of the pair, which is 10,000). Since this is not prime, stream-filter examines the stream-cdr of its input stream. The call to stream-cdr forces evaluation of the delayed stream-enumerate-interval, which now returns (cons 10001 (delay (stream-enumerate-interval 10002 1000000))) Stream-filter now looks at the stream-car of this stream, 10,001, sees that this is not prime either, forces another stream-cdr, and so on, until stream-enumerate-interval yields the prime 10,007, whereupon stream-filter, according to its definition, returns (cons-stream (stream-car stream) (stream-filter pred (stream-cdr stream))) which in this case is 493 (cons 10007 (delay (stream-filter prime? (cons 10008 (delay (stream-enumerate-interval 10009 1000000)))))) This result is now passed to stream-cdr in our original expression. This forces the delayed stream-filter, which in turn keeps forcing the delayed stream-enumerate-interval until it finds the next prime, which is 10,009. Finally, the result passed to stream-car in our original expression is (cons 10009 (delay (stream-filter prime? (cons 10010 (delay (stream-enumerate-interval 10011 1000000)))))) Stream-car returns 10,009, and the computation is complete. Only as many integers were tested for primality as were necessary to find the second prime, and the interval was enumerated only as far as was necessary to feed the prime filter. In general, we can think of delayed evaluation as “demand-driven” programming, whereby each stage in the stream process is activated only enough to satisfy the next stage. What we have done is to decouple the 494 actual order of events in the computation from the apparent structure of our procedures. We write procedures as if the streams existed “all at once” when, in reality, the computation is performed incrementally, as in traditional programming styles. Implementing delay and force Although delay and force may seem like mysterious operations, their implementation is really quite straightforward. Delay must package an expression so that it can be evaluated later on demand, and we can accomplish this simply by treating the expression as the body of a procedure. Delay can be a special form such that (delay 〈exp〉) is syntactic sugar for (lambda () 〈exp〉) Force simply calls the procedure (of no arguments) produced by delay, so we can implement force as a procedure: (define (force delayed-object) (delayed-object)) This implementation suffices for delay and force to work as advertised, but there is an important optimization that we can include. In many applications, we end up forcing the same delayed object many times. This can lead to serious inefficiency in recursive programs involving streams. (See Exercise 3.57.) The solution is to build delayed objects so that the first time they are forced, they store the value that is computed. Subsequent forcings will simply return the stored value without repeating the computation. In other words, we implement delay as a special-purpose memoized procedure similar to the one described in Exercise 3.27. One way to accomplish 495 this is to use the following procedure, which takes as argument a procedure (of no arguments) and returns a memoized version of the procedure. The first time the memoized procedure is run, it saves the computed result. On subsequent evaluations, it simply returns the result. (define (memo-proc proc) (let ((already-run? false) (result false)) (lambda () (if (not already-run?) (begin (set! result (proc)) (set! already-run? true) result) result)))) Delay is then defined so that (delay <exp>) is equivalent to (memo-proc (lambda () 〈exp〉)) and force is as defined previously.58 Exercise 3.50: Complete the following definition, which generalizes stream-map to allow procedures that take multiple arguments, analogous to map in Section 2.2.1, Footnote 12. 58 There are many possible implementations of streams other than the one described in this section. Delayed evaluation, which is the key to making streams practical, was inherent in Algol 60’s call-by-name parameter-passing method. The use of this mechanism to implement streams was first described by Landin (1965). Delayed evaluation for streams was introduced into Lisp by Friedman and Wise (1976). In their implementation, cons always delays evaluating its arguments, so that lists automatically behave as streams. The memoizing optimization is also known as call-by-need. The Algol community would refer to our original delayed objects as call-by-name thunks and to the optimized versions as call-by-need thunks. 496 (define (stream-map proc . argstreams) (if (〈??〉 (car argstreams)) the-empty-stream (〈??〉 (apply proc (map 〈??〉 argstreams)) (apply stream-map (cons proc (map 〈??〉 argstreams)))))) Exercise 3.51: In order to take a closer look at delayed evaluation, we will use the following procedure, which simply returns its argument after printing it: (define (show x) (display-line x) x) What does the interpreter print in response to evaluating each expression in the following sequence?59 (define x (stream-map show 59 Exercises such as Exercise 3.51 and Exercise 3.52 are valuable for testing our under- standing of how delay works. On the other hand, intermixing delayed evaluation with printing—and, even worse, with assignment—is extremely confusing, and instructors of courses on computer languages have traditionally tormented their students with examination questions such as the ones in this section. Needless to say, writing programs that depend on such subtleties is odious programming style. Part of the power of stream processing is that it lets us ignore the order in which events actually happen in our programs. Unfortunately, this is precisely what we cannot afford to do in the presence of assignment, which forces us to be concerned with time and change. 497 (stream-enumerate-interval 0 10))) (stream-ref x 5) (stream-ref x 7) Exercise 3.52: Consider the sequence of expressions (define sum 0) (define (accum x) (set! sum (+ x sum)) sum) (define seq (stream-map accum (stream-enumerate-interval 1 20))) (define y (stream-filter even? seq)) (define z (stream-filter (lambda (x) (= (remainder x 5) 0)) seq)) (stream-ref y 7) (display-stream z) What is the value of sum after each of the above expressions is evaluated? What is the printed response to evaluating the stream-ref and display-stream expressions? Would these responses differ if we had implemented (delay <exp>) simply as (lambda () <exp>) without using the optimization provided by memo-proc? Explain 3.5.2 Infinite Streams We have seen how to support the illusion of manipulating streams as complete entities even though, in actuality, we compute only as much of the stream as we need to access. We can exploit this technique to represent sequences efficiently as streams, even if the sequences are very long. What is more striking, we can use streams to represent sequences that are infinitely long. For instance, consider the following definition of the stream of positive integers: (define (integers-starting-from n) (cons-stream n (integers-starting-from (+ n 1)))) (define integers (integers-starting-from 1)) This makes sense because integers will be a pair whose car is 1 and whose cdr is a promise to produce the integers beginning with 2. This is an infinitely long stream, but in any given time we can examine only a finite portion of it. Thus, our programs will never know that the entire infinite stream is not there. Using integers we can define other infinite streams, such as the stream of integers that are not divisible by 7: (define (divisible? x y) (= (remainder x y) 0)) (define no-sevens (stream-filter (lambda (x) (not (divisible? x 7))) integers)) Then we can find integers not divisible by 7 simply by accessing elements of this stream: 499 (stream-ref no-sevens 100) 117 In analogy with integers, we can define the infinite stream of Fibonacci numbers: (define (fibgen a b) (cons-stream a (fibgen b (+ a b)))) (define fibs (fibgen 0 1)) Fibs is a pair whose car is 0 and whose cdr is a promise to evaluate (fibgen 1 1). When we evaluate this delayed (fibgen 1 1), it will pro- duce a pair whose car is 1 and whose cdr is a promise to evaluate (fibgen 1 2), and so on. For a look at a more exciting infinite stream, we can generalize the no-sevens example to construct the infinite stream of prime numbers, using a method known as the sieve of Eratosthenes.60 We start with the integers beginning with 2, which is the first prime. To get the rest of the primes, we start by filtering the multiples of 2 from the rest of the integers. This leaves a stream beginning with 3, which is the next prime. Now we filter the multiples of 3 from the rest of this stream. This leaves a stream beginning with 5, which is the next prime, and so on. In other words, we 60 Eratosthenes, a third-century B.C. Alexandrian Greek philosopher, is famous for giv- ing the first accurate estimate of the circumference of the Earth, which he computed by observing shadows cast at noon on the day of the summer solstice. Eratosthenes’s sieve method, although ancient, has formed the basis for special-purpose hardware “sieves” that, until recently, were the most powerful tools in existence for locating large primes. Since the 70s, however, these methods have been superseded by outgrowths of the probabilistic techniques discussed in Section 1.2.6. 500 construct the primes by a sieving process, described as follows: To sieve a stream S, form a stream whose first element is the first element of S and the rest of which is obtained by filtering all multiples of the first element of S out of the rest of S and sieving the result. This process is readily described in terms of stream operations: (define (sieve stream) (cons-stream (stream-car stream) (sieve (stream-filter (lambda (x) (not (divisible? x (stream-car stream)))) (stream-cdr stream))))) (define primes (sieve (integers-starting-from 2))) Now to find a particular prime we need only ask for it: (stream-ref primes 50) 233 It is interesting to contemplate the signal-processing system set up by sieve, shown in the “Henderson diagram” in Figure 3.31.61 The input stream feeds into an “unconser” that separates the first element of the stream from the rest of the stream. The first element is used to construct a divisibility filter, through which the rest is passed, and the output of the 61 We have named these figures after Peter Henderson, who was the first person to show us diagrams of this sort as a way of thinking about stream processing. Each solid line represents a stream of values being transmitted. The dashed line from the car to the cons and the filter indicates that this is a single value rather than a stream. 501 filter is fed to another sieve box. Then the original first element is consed onto the output of the internal sieve to form the output stream. Thus, not only is the stream infinite, but the signal processor is also infinite, because the sieve contains a sieve within it. Figure 3.31: The prime sieve viewed as a signal-processing system. Defining streams implicitly The integers and fibs streams above were defined by specifying “generating” procedures that explicitly compute the stream elements one by one. An alternative way to specify streams is to take advantage of delayed evaluation to define streams implicitly. For example, the following expression defines the stream ones to be an infinite stream of ones: (define ones (cons-stream 1 ones)) This works much like the definition of a recursive procedure: ones is a pair whose car is 1 and whose cdr is a promise to evaluate ones. Evaluating the cdr gives us again a 1 and a promise to evaluate ones, and so on. 502 We can do more interesting things by manipulating streams with operations such as add-streams, which produces the elementwise sum of two given streams:62 (define (add-streams s1 s2) (stream-map + s1 s2)) Now we can define the integers as follows: (define integers (cons-stream 1 (add-streams ones integers))) This defines integers to be a stream whose first element is 1 and the rest of which is the sum of ones and integers. Thus, the second element of integers is 1 plus the first element of integers, or 2; the third element of integers is 1 plus the second element of integers, or 3; and so on. This definition works because, at any point, enough of the integers stream has been generated so that we can feed it back into the definition to produce the next integer. We can define the Fibonacci numbers in the same style: (define fibs (cons-stream 0 (cons-stream 1 (add-streams (stream-cdr fibs) This definition says that fibs is a stream beginning with 0 and 1, such that the rest of the stream can be generated by adding fibs to itself shifted by one place: ... = (stream-cdr fibs) ... = fibs ... = fibs 62 This uses the generalized version of stream-map from Exercise 3.50. 503 Scale-stream is another useful procedure in formulating such stream definitions. This multiplies each item in a stream by a given constant: (define (scale-stream stream factor) (stream-map (lambda (x) (* x factor)) stream)) For example, (define double (cons-stream 1 (scale-stream double 2))) produces the stream of powers of 2: 1, 2, 4, 8, 16, 32, . . . . An alternate definition of the stream of primes can be given by starting with the integers and filtering them by testing for primality. We will need the first prime, 2, to get started: (define primes (cons-stream 2 (stream-filter prime? (integers-starting-from 3)))) This definition is not so straightforward as it appears, because we will test whether a number n is prime by checking whether n is divisible by a p prime (not by just any integer) less than or equal to n : (define (prime? n) (define (iter ps) (cond ((> (square (stream-car ps)) n) true) ((divisible? n (stream-car ps)) false) (else (iter (stream-cdr ps))))) (iter primes)) This is a recursive definition, since primes is defined in terms of the prime? predicate, which itself uses the primes stream. The reason this pro- cedure works is that, at any point, enough of the primes stream has been 504 generated to test the primality of the numbers we need to check next. That is, for every n we test for primality, either n is not prime (in which case there is a prime already generated that divides it) or n is prime (in which case there is a prime already generated—i.e., a prime less than n —that is p greater than n ).63 Exercise 3.53: Without running the program, describe the elements of the stream defined by (define s (cons-stream 1 (add-streams s s))) Exercise 3.54: Define a procedure mul-streams, analogous to add-streams, that produces the elementwise product of its two input streams. Use this together with the stream of integers to complete the following definition of the stream whose n th element (counting from 0) is n + 1 factorial: (define factorials (cons-stream 1 (mul-streams 〈??〉 〈??〉))) Exercise 3.55: Define a procedure partial-sums that takes as argument a stream S and returns the stream whose elements are S 0 , S 0 + S 1 , S 0 + S 1 + S 2 , . . .. For example, (partial-sums integers) should be the stream 1, 3, 6, 10, 15, . . . . 63 This last point is very subtle and relies on the fact that p 2 n +1 ≤ p n . (Here, p k de- notes the k th prime.) Estimates such as these are very difficult to establish. The ancient proof by Euclid that there are an infinite number of primes shows that p n +1 ≤ p 1 p 2 · · · p n + 1, and no substantially better result was proved until 1851, when the Russian mathematician P. L. Chebyshev established that p n +1 ≤ 2p n for all n . This result, originally conjectured in 1845, is known as Bertrand’s hypothesis. A proof can be found in section 22.3 of Hardy and Wright 1960. 505 Exercise 3.56: A famous problem, first raised by R. Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2, 3, or 5. One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2, 3, and 5. But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As an alternative, let us call the required stream of numbers S and notice the following facts about it. • S begins with 1. • The elements of (scale-stream S 2) are also elements of S. • The same is true for (scale-stream S 3) and (scale-stream 5 S). • These are all the elements of S. Now all we have to do is combine elements from these sources. For this we define a procedure merge that combines two ordered streams into one ordered result stream, eliminating repetitions: (define (merge s1 s2) (cond ((stream-null? s1) s2) ((stream-null? s2) s1) (else (let ((s1car (stream-car s1)) (s2car (stream-car s2))) (cond ((< s1car s2car) (cons-stream s1car (merge (stream-cdr s1) s2))) 506 ((> s1car s2car) (cons-stream s2car (merge s1 (stream-cdr s2)))) (else (cons-stream s1car (merge (stream-cdr s1) (stream-cdr s2))))))))) Then the required stream may be constructed with merge, as follows: (define S (cons-stream 1 (merge 〈??〉〈??〉))) Fill in the missing expressions in the places marked 〈?? 〉 above. Exercise 3.57: How many additions are performed when we compute the n th Fibonacci number using the definition of fibs based on the add-streams procedure? Show that the number of additions would be exponentially greater if we had implemented (delay <exp>) simply as (lambda () <exp>), without using the optimization provided by the memo-proc procedure described in Section 3.5.1.64 Exercise 3.58: Give an interpretation of the stream computed by the following procedure: 64 This exercise shows how call-by-need is closely related to ordinary memoization as described in Exercise 3.27. In that exercise, we used assignment to explicitly construct a local table. Our call-by-need stream optimization effectively constructs such a table automatically, storing values in the previously forced parts of the stream. 507 (define (expand num den radix) (cons-stream (quotient (* num radix) den) (expand (remainder (* num radix) den) den radix))) (Quotient is a primitive that returns the integer quotient of two integers.) What are the successive elements produced by (expand 1 7 10)? What is produced by (expand 3 8 10)? Exercise 3.59: In Section 2.5.3 we saw how to implement a polynomial arithmetic system representing polynomials as lists of terms. In a similar way, we can work with power series, such as x3 x4 x2 + + + ... e =1+x + 2 3·2 4·3·2 x2 x4 cos x = 1 − + − ... 2 4·3·2 x3 x5 sin x = x − + − ... 3·2 5·4·3·2 x represented as infinite streams. We will represent the series a 0 + a 1 x + a 2 x 2 + a 3 x 3 + . . . as the stream whose elements are the coefficients a 0 , a 1 , a 2 , a 3 , . . . . a. The integral of the series a 0 + a 1 x + a 2 x 2 + a 3 x 3 + . . . is the series 1 1 1 c + a0 x + a1 x 2 + a2 x 3 + a3 x 4 + . . . 2 3 4 where c is any constant. Define a procedure integrate-series that takes as input a stream a 0 , a 1 , a 2 , . . . representing a power series and returns the stream a 0 , 12 a 1 , 13 a 2 , . . . of coefficients of the non-constant terms of the integral of the series. (Since the result has no constant term, it doesn’t represent a power series; 508 when we use integrate-series, we will cons on the appropriate constant.) b. The function x 7→ e x is its own derivative. This implies that e x and the integral of e x are the same series, except for the constant term, which is e 0 = 1. Accordingly, we can generate the series for e x as (define exp-series (cons-stream 1 (integrate-series exp-series))) Show how to generate the series for sine and cosine, starting from the facts that the derivative of sine is cosine and the derivative of cosine is the negative of sine: (define cosine-series (cons-stream 1 〈??〉)) (define sine-series (cons-stream 0 〈??〉)) With power series represented as streams of coefficients as in Exercise 3.59, adding series is implemented by add-streams. Complete the definition of the following procedure for multiplying series: Exercise 3.60: (define (mul-series s1 s2) (cons-stream 〈??〉 (add-streams 〈??〉〈??〉))) You can test your procedure by verifying that sin2 x + cos2 x = 1, using the series from Exercise 3.59. Exercise 3.61: Let S be a power series (Exercise 3.59) whose constant term is 1. Suppose we want to find the power series 1/S , that 509 is, the series X such that S X = 1. Write S = 1 + S R where S R is the part of S after the constant term. Then we can solve for X as follows: S·X =1 (1 + S R ) · X = 1 X + SR · X = 1 X = 1 − SR · X In other words, X is the power series whose constant term is 1 and whose higher-order terms are given by the negative of S R times X . Use this idea to write a procedure invert-unit-series that computes 1/S for a power series S with constant term 1. You will need to use mul-series from Exercise 3.60. Exercise 3.62: Use the results of Exercise 3.60 and Exercise 3.61 to define a procedure div-series that divides two power series. Div-series should work for any two series, provided that the de- nominator series begins with a nonzero constant term. (If the denominator has a zero constant term, then div-series should signal an error.) Show how to use div-series together with the result of Exercise 3.59 to generate the power series for tangent. 3.5.3 Exploiting the Stream Paradigm Streams with delayed evaluation can be a powerful modeling tool, providing many of the benefits of local state and assignment. Moreover, they avoid some of the theoretical tangles that accompany the introduction of assignment into a programming language. The stream approach can be illuminating because it allows us to build systems with different module boundaries than systems organized around 510 assignment to state variables. For example, we can think of an entire time series (or signal) as a focus of interest, rather than the values of the state variables at individual moments. This makes it convenient to combine and compare components of state from different moments. Formulating iterations as stream processes In Section 1.2.1, we introduced iterative processes, which proceed by updating state variables. We know now that we can represent state as a “timeless” stream of values rather than as a set of variables to be updated. Let’s adopt this perspective in revisiting the square-root procedure from Section 1.1.7. Recall that the idea is to generate a sequence of better and better guesses for the square root of x by applying over and over again the procedure that improves guesses: (define (sqrt-improve guess x) (average guess (/ x guess))) In our original sqrt procedure, we made these guesses be the successive values of a state variable. Instead we can generate the infinite stream of guesses, starting with an initial guess of 1:65 (define (sqrt-stream x) (define guesses (cons-stream 1.0 (stream-map (lambda (guess) (sqrt-improve guess x)) guesses))) 65 We can’t use let to bind the local variable guesses, because the value of depends on guesses itself. Exercise 3.63 addresses why we want a local variable here. 511 guesses) (display-stream (sqrt-stream 2)) 1. 1.5 1.4166666666666665 1.4142156862745097 1.4142135623746899 ... We can generate more and more terms of the stream to get better and better guesses. If we like, we can write a procedure that keeps generating terms until the answer is good enough. (See Exercise 3.64.) Another iteration that we can treat in the same way is to generate an approximation to π, based upon the alternating series that we saw in Section 1.3.1: π 1 1 1 = 1 − + − + ... 4 3 5 7 We first generate the stream of summands of the series (the reciprocals of the odd integers, with alternating signs). Then we take the stream of sums of more and more terms (using the partial-sums procedure of Exercise 3.55) and scale the result by 4: (define (pi-summands n) (cons-stream (/ 1.0 n) (stream-map - (pi-summands (+ n 2))))) (define pi-stream (scale-stream (partial-sums (pi-summands 1)) 4)) 512 (display-stream pi-stream) 4. 2.666666666666667 3.466666666666667 2.8952380952380956 3.3396825396825403 2.9760461760461765 3.2837384837384844 3.017071817071818 ... This gives us a stream of better and better approximations to π, although the approximations converge rather slowly. Eight terms of the sequence bound the value of π between 3.284 and 3.017. So far, our use of the stream of states approach is not much different from updating state variables. But streams give us an opportunity to do some interesting tricks. For example, we can transform a stream with a sequence accelerator that converts a sequence of approximations to a new sequence that converges to the same value as the original, only faster. One such accelerator, due to the eighteenth-century Swiss mathematician Leonhard Euler, works well with sequences that are partial sums of alternating series (series of terms with alternating signs). In Euler’s technique, if S n is the n th term of the original sum sequence, then the accelerated sequence has terms (S n +1 − S n )2 S n +1 − S n−1 − 2S n + S n +1 Thus, if the original sequence is represented as a stream of values, the transformed sequence is given by 513 (define (euler-transform s) (let ((s0 (stream-ref s 0)) ; S n−1 (s1 (stream-ref s 1)) ; Sn ; S n +1 (s2 (stream-ref s 2))) (cons-stream (- s2 (/ (square (- s2 s1)) (+ s0 (* -2 s1) s2))) (euler-transform (stream-cdr s))))) We can demonstrate Euler acceleration with our sequence of approximations to π: (display-stream (euler-transform pi-stream)) 3.166666666666667 3.1333333333333337 3.1452380952380956 3.13968253968254 3.1427128427128435 3.1408813408813416 3.142071817071818 3.1412548236077655 ... Even better, we can accelerate the accelerated sequence, and recursively accelerate that, and so on. Namely, we create a stream of streams (a structure we’ll call a tableau) in which each stream is the transform of the preceding one: (define (make-tableau transform s) (cons-stream s (make-tableau transform (transform s)))) 514 The tableau has the form s 00 s 01 s 02 s 03 s 04 . . . s 10 s 11 s 12 s 13 . . . s 20 s 21 s 22 . . . ... Finally, we form a sequence by taking the first term in each row of the tableau: (define (accelerated-sequence transform s) (stream-map stream-car (make-tableau transform s))) We can demonstrate this kind of “super-acceleration” of the π sequence: (display-stream (accelerated-sequence euler-transform pi-stream)) 4. 3.166666666666667 3.142105263157895 3.141599357319005 3.1415927140337785 3.1415926539752927 3.1415926535911765 3.141592653589778 ... The result is impressive. Taking eight terms of the sequence yields the correct value of π to 14 decimal places. If we had used only the original π sequence, we would need to compute on the order of 1013 terms (i.e., expanding the series far enough so that the individual terms are less then 10−13 ) to get that much accuracy! 515 We could have implemented these acceleration techniques without using streams. But the stream formulation is particularly elegant and convenient because the entire sequence of states is available to us as a data structure that can be manipulated with a uniform set of operations. Exercise 3.63: Louis Reasoner asks why the sqrt-stream procedure was not written in the following more straightforward way, without the local variable guesses: (define (sqrt-stream x) (cons-stream 1.0 (stream-map (lambda (guess) (sqrt-improve guess x)) (sqrt-stream x)))) Alyssa P. Hacker replies that this version of the procedure is considerably less efficient because it performs redundant computation. Explain Alyssa’s answer. Would the two versions still differ in efficiency if our implementation of delay used only (lambda () <exp>) without using the optimization provided by memo-proc (Section 3.5.1)? Exercise 3.64: Write a procedure stream-limit that takes as arguments a stream and a number (the tolerance). It should examine the stream until it finds two successive elements that differ in absolute value by less than the tolerance, and return the second of the two elements. Using this, we could compute square roots up to a given tolerance by (define (sqrt x tolerance) (stream-limit (sqrt-stream x) tolerance)) 516 Exercise 3.65: Use the series ln 2 = 1 − 1 1 1 + − + ... 2 3 4 to compute three sequences of approximations to the natural logarithm of 2, in the same way we did above for π. How rapidly do these sequences converge? Infinite streams of pairs In Section 2.2.3, we saw how the sequence paradigm handles traditional nested loops as processes defined on sequences of pairs. If we generalize this technique to infinite streams, then we can write programs that are not easily represented as loops, because the “looping” must range over an infinite set. For example, suppose we want to generalize the prime-sum-pairs procedure of Section 2.2.3 to produce the stream of pairs of all integers (i , j ) with i ≤ j such that i + j is prime. If int-pairs is the sequence of all pairs of integers (i , j ) with i ≤ j , then our required stream is simply66 (stream-filter (lambda (pair) (prime? (+ (car pair) (cadr pair)))) int-pairs) Our problem, then, is to produce the stream int-pairs. More generally, suppose we have two streams S = (S i ) and T = (T j ), and imagine the infi66 As in Section 2.2.3, we represent a pair of integers as a list rather than a Lisp pair. 517 nite rectangular array (S 0 , T0 ) (S 0 , T1 ) (S 0 , T2 ) . . . (S 1 , T0 ) (S 1 , T1 ) (S 1 , T2 ) . . . (S 2 , T0 ) (S 2 , T1 ) (S 2 , T2 ) . . . ... We wish to generate a stream that contains all the pairs in the array that lie on or above the diagonal, i.e., the pairs (S 0 , T0 ) (S 0 , T1 ) (S 0 , T2 ) . . . (S 1 , T1 ) (S 1 , T2 ) . . . (S 2 , T2 ) . . . ... (If we take both S and T to be the stream of integers, then this will be our desired stream int-pairs.) Call the general stream of pairs (pairs S T), and consider it to be composed of three parts: the pair (S 0 , T0 ), the rest of the pairs in the first row, and the remaining pairs:67 (S 0 , T0 ) (S 0 , T1 ) (S 0 , T2 ) . . . (S 1 , T1 ) (S 1 , T2 ) . . . (S 2 , T2 ) . . . ... Observe that the third piece in this decomposition (pairs that are not in the first row) is (recursively) the pairs formed from (stream-cdr S) and (stream-cdr T). Also note that the second piece (the rest of the first row) is 67 See Exercise 3.68 for some insight into why we chose this decomposition. 518 (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) Thus we can form our stream of pairs as follows: (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (〈combine-in-some-way〉 (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) In order to complete the procedure, we must choose some way to combine the two inner streams. One idea is to use the stream analog of the append procedure from Section 2.2.1: (define (stream-append s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (stream-append (stream-cdr s1) s2)))) This is unsuitable for infinite streams, however, because it takes all the elements from the first stream before incorporating the second stream. In particular, if we try to generate all pairs of positive integers using (pairs integers integers) our stream of results will first try to run through all pairs with the first integer equal to 1, and hence will never produce pairs with any other value of the first integer. 519 To handle infinite streams, we need to devise an order of combination that ensures that every element will eventually be reached if we let our program run long enough. An elegant way to accomplish this is with the following interleave procedure:68 (define (interleave s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (interleave s2 (stream-cdr s1))))) Since interleave takes elements alternately from the two streams, every element of the second stream will eventually find its way into the interleaved stream, even if the first stream is infinite. We can thus generate the required stream of pairs as (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) (interleave (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) 68 The precise statement of the required property on the order of combination is as follows: There should be a function f of two arguments such that the pair corresponding to element i of the first stream and element j of the second stream will appear as element number f (i , j ) of the output stream. The trick of using interleave to accomplish this was shown to us by David Turner, who employed it in the language KRC (Turner 1981). 520 Exercise 3.66: Examine the stream (pairs integers integers). Can you make any general comments about the order in which the pairs are placed into the stream? For example, approximately how many pairs precede the pair (1, 100)? the pair (99, 100)? the pair (100, 100)? (If you can make precise mathematical statements here, all the better. But feel free to give more qualitative answers if you find yourself getting bogged down.) Exercise 3.67: Modify the pairs procedure so that (pairs integers integers) will produce the stream of all pairs of integers (i , j ) (without the condition i ≤ j ). Hint: You will need to mix in an additional stream. Exercise 3.68: Louis Reasoner thinks that building a stream of pairs from three parts is unnecessarily complicated. Instead of separating the pair (S 0 , T0 ) from the rest of the pairs in the first row, he proposes to work with the whole first row, as follows: (define (pairs s t) (interleave (stream-map (lambda (x) (list (stream-car s) x)) t) (pairs (stream-cdr s) (stream-cdr t)))) Does this work? Consider what happens if we evaluate (pairs integers integers) using Louis’s definition of pairs. Exercise 3.69: Write a procedure triples that takes three infinite streams, S , T , and U , and produces the stream of triples (S i , T j , Uk ) such that i ≤ j ≤ k . Use triples to generate the stream of all Pythagorean triples of positive integers, i.e., the triples (i , j , k ) such that i ≤ j and i 2 + j 2 = k 2 . 521 Exercise 3.70: It would be nice to be able to generate streams in which the pairs appear in some useful order, rather than in the order that results from an ad hoc interleaving process. We can use a technique similar to the merge procedure of Exercise 3.56, if we define a way to say that one pair of integers is “less than” another. One way to do this is to define a “weighting function” W (i , j ) and stipulate that (i 1 , j 1 ) is less than (i 2 , j 2 ) if W (i 1 , j 1 ) < W (i 2 , j 2 ). Write a procedure merge-weighted that is like merge, except that merge-weighted takes an additional argument weight, which is a procedure that computes the weight of a pair, and is used to determine the order in which elements should appear in the resulting merged stream.69 Using this, generalize pairs to a procedure weighted-pairs that takes two streams, together with a procedure that computes a weighting function, and generates the stream of pairs, ordered according to weight. Use your procedure to generate a. the stream of all pairs of positive integers (i , j ) with i ≤ j ordered according to the sum i + j , b. the stream of all pairs of positive integers (i , j ) with i ≤ j , where neither i nor j is divisible by 2, 3, or 5, and the pairs are ordered according to the sum 2i + 3 j + 5ij . Exercise 3.71: Numbers that can be expressed as the sum of two cubes in more than one way are sometimes called Ramanujan 69 We will require that the weighting function be such that the weight of a pair in- creases as we move out along a row or down along a column of the array of pairs. 522 numbers, in honor of the mathematician Srinivasa Ramanujan.70 Ordered streams of pairs provide an elegant solution to the problem of computing these numbers. To find a number that can be written as the sum of two cubes in two different ways, we need only generate the stream of pairs of integers (i , j ) weighted according to the sum i 3 + j 3 (see Exercise 3.70), then search the stream for two consecutive pairs with the same weight. Write a procedure to generate the Ramanujan numbers. The first such number is 1,729. What are the next five? Exercise 3.72: In a similar way to Exercise 3.71 generate a stream of all numbers that can be written as the sum of two squares in three different ways (showing how they can be so written). Streams as signals We began our discussion of streams by describing them as computational analogs of the “signals” in signal-processing systems. In fact, we can use streams to model signal-processing systems in a very direct way, representing the values of a signal at successive time intervals as consecutive elements of a stream. For instance, we can implement an integrator or summer that, for an input stream x = (x i ), an initial value C , and a small 70 To quote from G. H. Hardy’s obituary of Ramanujan (Hardy 1921): “It was Mr. Little- wood (I believe) who remarked that ‘every positive integer was one of his friends.’ I remember once going to see him when he was lying ill at Putney. I had ridden in taxi-cab No. 1729, and remarked that the number seemed to me a rather dull one, and that I hoped it was not an unfavorable omen. ‘No,’ he replied, ‘it is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways.’ ” The trick of using weighted pairs to generate the Ramanujan numbers was shown to us by Charles Leiserson. 523 increment dt , accumulates the sum Si = C + i X x j dt j =1 and returns the stream of values S = (S i ). The following integral procedure is reminiscent of the “implicit style” definition of the stream of integers (Section 3.5.2): (define (integral integrand initial-value dt) (define int (cons-stream initial-value (add-streams (scale-stream integrand dt) int))) int) Figure 3.32 is a picture of a signal-processing system that corresponds to the integral procedure. The input stream is scaled by dt and passed through an adder, whose output is passed back through the same adder. The self-reference in the definition of int is reflected in the figure by the feedback loop that connects the output of the adder to one of the inputs. Figure 3.32: The integral procedure viewed as a signalprocessing system. 524 Exercise 3.73: We can model electrical circuits using streams to represent the values of currents or voltages at a sequence of times. For instance, suppose we have an RC circuit consisting of a resistor of resistance R and a capacitor of capacitance C in series. The voltage response v of the circuit to an injected current i is determined by the formula in Figure 3.33, whose structure is shown by the accompanying signal-flow diagram. Figure 3.33: An RC circuit and the associated signal-flow diagram. Write a procedure RC that models this circuit. RC should take as inputs the values of R , C , and dt and should return a procedure that takes as inputs a stream representing the current i and an initial value for the capacitor voltage v 0 and produces as output the stream of voltages v . For example, you should be able to use RC to model an RC circuit with R = 5 ohms, C = 1 farad, and a 0.5-second 525 time step by evaluating (define RC1 (RC 5 1 0.5)). This defines RC1 as a procedure that takes a stream representing the time sequence of currents and an initial capacitor voltage and produces the output stream of voltages. Exercise 3.74: Alyssa P. Hacker is designing a system to process signals coming from physical sensors. One important feature she wishes to produce is a signal that describes the zero crossings of the input signal. That is, the resulting signal should be +1 whenever the input signal changes from negative to positive, −1 whenever the input signal changes from positive to negative, and 0 otherwise. (Assume that the sign of a 0 input is positive.) For example, a typical input signal with its associated zero-crossing signal would be ...1 -0.5 0 In Alyssa’s system, the signal from the sensor is represented as a stream sense-data and the stream zero-crossings is the corresponding stream of zero crossings. Alyssa first writes a procedure sign-change-detector that takes two values as arguments and compares the signs of the values to produce an appropriate 0, 1, or - 1. She then constructs her zero-crossing stream as follows: (define (make-zero-crossings input-stream last-value) (cons-stream (sign-change-detector (stream-car input-stream) last-value) (make-zero-crossings (stream-cdr input-stream) (stream-car input-stream)))) 526 (define zero-crossings (make-zero-crossings sense-data 0)) Alyssa’s boss, Eva Lu Ator, walks by and suggests that this program is approximately equivalent to the following one, which uses the generalized version of stream-map from Exercise 3.50: (define zero-crossings (stream-map sign-change-detector sense-data 〈expression〉)) Complete the program by supplying the indicated 〈expression〉. Exercise 3.75: Unfortunately, Alyssa’s zero-crossing detector in Exercise 3.74 proves to be insufficient, because the noisy signal from the sensor leads to spurious zero crossings. Lem E. Tweakit, a hardware specialist, suggests that Alyssa smooth the signal to filter out the noise before extracting the zero crossings. Alyssa takes his advice and decides to extract the zero crossings from the signal constructed by averaging each value of the sense data with the previous value. She explains the problem to her assistant, Louis Reasoner, who attempts to implement the idea, altering Alyssa’s program as follows: (define (make-zero-crossings input-stream last-value) (let ((avpt (/ (+ (stream-car input-stream) last-value) 2))) (cons-stream (sign-change-detector avpt last-value) (make-zero-crossings (stream-cdr input-stream) avpt)))) This does not correctly implement Alyssa’s plan. Find the bug that Louis has installed and fix it without changing the structure of the program. (Hint: You will need to increase the number of arguments to make-zero-crossings.) 527 Exercise 3.76: Eva Lu Ator has a criticism of Louis’s approach in Exercise 3.75. The program he wrote is not modular, because it intermixes the operation of smoothing with the zero-crossing extraction. For example, the extractor should not have to be changed if Alyssa finds a better way to condition her input signal. Help Louis by writing a procedure smooth that takes a stream as input and produces a stream in which each element is the average of two successive input stream elements. Then use smooth as a component to implement the zero-crossing detector in a more modular style. 3.5.4 Streams and Delayed Evaluation The integral procedure at the end of the preceding section shows how we can use streams to model signal-processing systems that contain feedback loops. The feedback loop for the adder shown in Figure 3.32 is modeled by the fact that integral’s internal stream int is defined in terms of itself: (define int (cons-stream initial-value (add-streams (scale-stream integrand dt) int))) The interpreter’s ability to deal with such an implicit definition depends on the delay that is incorporated into cons-stream. Without this delay, the interpreter could not construct int before evaluating both arguments to cons-stream, which would require that int already be defined. In general, delay is crucial for using streams to model signal-processing systems that contain loops. Without delay, our models would have to be formulated so that the inputs to any signal-processing component would be fully evaluated before the output could be produced. This would outlaw loops. Figure 3.34: An “analog computer circuit” that solves the equation dy/dt = f ( y ). Unfortunately, stream models of systems with loops may require uses of delay beyond the “hidden” delay supplied by cons-stream. For instance, Figure 3.34 shows a signal-processing system for solving the differential equation dy/dt = f ( y ) where f is a given function. The figure shows a mapping component, which applies f to its input signal, linked in a feedback loop to an integrator in a manner very similar to that of the analog computer circuits that are actually used to solve such equations. Assuming we are given an initial value y 0 for y , we could try to model this system using the procedure (define (solve f y0 dt) (define y (integral dy y0 dt)) (define dy (stream-map f y)) y) This procedure does not work, because in the first line of solve the call to integral requires that the input dy be defined, which does not happen until the second line of solve. 529 On the other hand, the intent of our definition does make sense, because we can, in principle, begin to generate the y stream without knowing dy. Indeed, integral and many other stream operations have properties similar to those of cons-stream, in that we can generate part of the answer given only partial information about the arguments. For integral, the first element of the output stream is the specified initial-value. Thus, we can generate the first element of the output stream without evaluating the integrand dy. Once we know the first element of y, the stream-map in the second line of solve can begin working to generate the first element of dy, which will produce the next element of y, and so on. To take advantage of this idea, we will redefine integral to expect the integrand stream to be a delayed argument. Integral will force the integrand to be evaluated only when it is required to generate more than the first element of the output stream: (define (integral delayed-integrand initial-value dt) (define int (cons-stream initial-value (let ((integrand (force delayed-integrand))) (add-streams (scale-stream integrand dt) int)))) int) Now we can implement our solve procedure by delaying the evaluation of dy in the definition of y:71 71 This procedure is not guaranteed to work in all Scheme implementations, although for any implementation there is a simple variation that will work. The problem 530 (define (solve f y0 dt) (define y (integral (delay dy) y0 dt)) (define dy (stream-map f y)) y) In general, every caller of integral must now delay the integrand argument. We can demonstrate that the solve procedure works by approximating e ≈ 2.718 by computing the value at y = 1 of the solution to the differential equation dy/dt = y with initial condition y (0) = 1: (stream-ref (solve (lambda (y) y) 1 0.001) 1000) 2.716924 Exercise 3.77: The integral procedure used above was analogous to the “implicit” definition of the infinite stream of integers in Section 3.5.2. Alternatively, we can give a definition of integral that is more like integers-starting-from (also in Section 3.5.2): (define (integral integrand initial-value dt) (cons-stream initial-value (if (stream-null? integrand) the-empty-stream (integral (stream-cdr integrand) (+ (* dt (stream-car integrand)) initial-value) dt)))) When used in systems with loops, this procedure has the same problem as does our original version of integral. Modify the prohas to do with subtle differences in the ways that Scheme implementations handle internal definitions. (See Section 4.1.6.) 531 cedure so that it expects the integrand as a delayed argument and hence can be used in the solve procedure shown above. Figure 3.35: Signal-flow diagram for the solution to a second-order linear differential equation. Exercise 3.78: Consider the problem of designing a signal- processing system to study the homogeneous second-order linear differential equation d 2y dy − a − by = 0 dt dt 2 The output stream, modeling y , is generated by a network that contains a loop. This is because the value of d 2y/dt 2 depends upon the values of y and dy/dt and both of these are determined by inte- 532 grating d 2y/dt 2 . The diagram we would like to encode is shown in Figure 3.35. Write a procedure solve-2nd that takes as arguments the constants a , b , and dt and the initial values y 0 and dy 0 for y and dy/dt and generates the stream of successive values of y . Exercise 3.79: Generalize the solve-2nd procedure of Exercise 3.78 so that it can be used to solve general second-order differential equations d 2y/dt 2 = f (dy/dt , y ). Exercise 3.80: A series RLC circuit consists of a resistor, a capacitor, and an inductor connected in series, as shown in Figure 3.36. If R , L , and C are the resistance, inductance, and capacitance, then the relations between voltage (v ) and current (i ) for the three components are described by the equations vR = iR R di L vL = L dt dv C iC = C dt and the circuit connections dictate the relations i R = i L = −i C vC = v L + v R Combining these equations shows that the state of the circuit (summarized by v C , the voltage across the capacitor, and i L , the current in the inductor) is described by the pair of differential equations dv C iL =− dt C di L 1 R = vC − i L dt L L 533 The signal-flow diagram representing this system of differential equations is shown in Figure 3.37. Figure 3.36: A series RLC circuit. Write a procedure RLC that takes as arguments the parameters R , L , and C of the circuit and the time increment dt . In a manner similar to that of the RC procedure of Exercise 3.73, RLC should produce a procedure that takes the initial values of the state variables, v C 0 and i L 0 , and produces a pair (using cons) of the streams of states v C and i L . Using RLC, generate the pair of streams that models the behavior of a series RLC circuit with R = 1 ohm, C = 0.2 farad, L = 1 henry, dt = 0.1 second, and initial values i L 0 = 0 amps and v C 0 = 10 volts. Figure 3.37: A signal-flow diagram for the solution to a series RLC circuit. Normal-order evaluation The examples in this section illustrate how the explicit use of delay and force provides great programming flexibility, but the same examples also show how this can make our programs more complex. Our new integral procedure, for instance, gives us the power to model systems with loops, but we must now remember that integral should be called with a delayed 535 integrand, and every procedure that uses integral must be aware of this. In effect, we have created two classes of procedures: ordinary procedures and procedures that take delayed arguments. In general, creating separate classes of procedures forces us to create separate classes of higher-order procedures as well.72 One way to avoid the need for two different classes of procedures is to make all procedures take delayed arguments. We could adopt a model of evaluation in which all arguments to procedures are automatically delayed and arguments are forced only when they are actually needed (for example, when they are required by a primitive operation). This would transform our language to use normal-order evaluation, which we first described when we introduced the substitution model for evaluation in Section 1.1.5. Converting to normal-order evaluation provides a uniform and elegant way to 72 This is a small reflection, in Lisp, of the difficulties that conventional strongly typed languages such as Pascal have in coping with higher-order procedures. In such languages, the programmer must specify the data types of the arguments and the result of each procedure: number, logical value, sequence, and so on. Consequently, we could not express an abstraction such as “map a given procedure proc over all the elements in a sequence” by a single higher-order procedure such as streammap. Rather, we would need a different mapping procedure for each different combination of argument and result data types that might be specified for a proc. Maintaining a practical notion of “data type” in the presence of higher-order procedures raises many difficult issues. One way of dealing with this problem is illustrated by the language ML (Gordon et al. 1979), whose “polymorphic data types” include templates for higher-order transformations between data types. Moreover, data types for most procedures in ML are never explicitly declared by the programmer. Instead, ML includes a type-inferencing mechanism that uses information in the environment to deduce the data types for newly defined procedures. 536 simplify the use of delayed evaluation, and this would be a natural strategy to adopt if we were concerned only with stream processing. In Section 4.2, after we have studied the evaluator, we will see how to transform our language in just this way. Unfortunately, including delays in procedure calls wreaks havoc with our ability to design programs that depend on the order of events, such as programs that use assignment, mutate data, or perform input or output. Even the single delay in cons-stream can cause great confusion, as illustrated by Exercise 3.51 and Exercise 3.52. As far as anyone knows, mutability and delayed evaluation do not mix well in programming languages, and devising ways to deal with both of these at once is an active area of research. 3.5.5 Modularity of Functional Programs and Modularity of Objects As we saw in Section 3.1.2, one of the major benefits of introducing assignment is that we can increase the modularity of our systems by encapsulating, or “hiding,” parts of the state of a large system within local variables. Stream models can provide an equivalent modularity without the use of assignment. As an illustration, we can reimplement the Monte Carlo estimation of π, which we examined in Section 3.1.2, from a stream-processing point of view. The key modularity issue was that we wished to hide the internal state of a random-number generator from programs that used random numbers. We began with a procedure rand-update, whose successive values furnished our supply of random numbers, and used this to produce a random-number generator: (define rand (let ((x random-init)) 537 (lambda () (set! x (rand-update x)) x))) In the stream formulation there is no random-number generator per se, just a stream of random numbers produced by successive calls to rand-update: (define random-numbers (cons-stream random-init (stream-map rand-update random-numbers))) We use this to construct the stream of outcomes of the Ces`aro experiment performed on consecutive pairs in the random-numbers stream: (define cesaro-stream (map-successive-pairs (lambda (r1 r2) (= (gcd r1 r2) 1)) random-numbers)) (define (map-successive-pairs f s) (cons-stream (f (stream-car s) (stream-car (stream-cdr s))) (map-successive-pairs f (stream-cdr (stream-cdr s))))) The cesaro-stream is now fed to a monte-carlo procedure, which produces a stream of estimates of probabilities. The results are then converted into a stream of estimates of π. This version of the program doesn’t need a parameter telling how many trials to perform. Better estimates of π (from performing more experiments) are obtained by looking farther into the pi stream: (define (monte-carlo experiment-stream passed failed) (define (next passed failed) 538 (cons-stream (/ passed (+ passed failed)) (monte-carlo (stream-cdr experiment-stream) passed failed))) (if (stream-car experiment-stream) (next (+ passed 1) failed) (next passed (+ failed 1)))) (define pi (stream-map (lambda (p) (sqrt (/ 6 p))) (monte-carlo cesaro-stream 0 0))) There is considerable modularity in this approach, because we still can formulate a general monte-carlo procedure that can deal with arbitrary experiments. Yet there is no assignment or local state. Exercise 3.81: Exercise 3.6 discussed generalizing the randomnumber generator to allow one to reset the random-number sequence so as to produce repeatable sequences of “random” numbers. Produce a stream formulation of this same generator that operates on an input stream of requests to generate a new random number or to reset the sequence to a specified value and that produces the desired stream of random numbers. Don’t use assignment in your solution. Exercise 3.82: Redo Exercise 3.5 on Monte Carlo integration in terms of streams. The stream version of estimate-integral will not have an argument telling how many trials to perform. Instead, it will produce a stream of estimates based on successively more trials. A functional-programming view of time Let us now return to the issues of objects and state that were raised at the beginning of this chapter and examine them in a new light. We introduced assignment and mutable objects to provide a mechanism for modular construction of programs that model systems with state. We constructed computational objects with local state variables and used assignment to modify these variables. We modeled the temporal behavior of the objects in the world by the temporal behavior of the corresponding computational objects. Now we have seen that streams provide an alternative way to model objects with local state. We can model a changing quantity, such as the local state of some object, using a stream that represents the time history of successive states. In essence, we represent time explicitly, using streams, so that we decouple time in our simulated world from the sequence of events that take place during evaluation. Indeed, because of the presence of delay there may be little relation between simulated time in the model and the order of events during the evaluation. In order to contrast these two approaches to modeling, let us reconsider the implementation of a “withdrawal processor” that monitors the balance in a bank account. In Section 3.1.3 we implemented a simplified version of such a processor: (define (make-simplified-withdraw balance) (lambda (amount) (set! balance (- balance amount)) balance)) Calls to make-simplified-withdraw produce computational objects, each with a local state variable balance that is decremented by successive 540 calls to the object. The object takes an amount as an argument and returns the new balance. We can imagine the user of a bank account typing a sequence of inputs to such an object and observing the sequence of returned values shown on a display screen. Alternatively, we can model a withdrawal processor as a procedure that takes as input a balance and a stream of amounts to withdraw and produces the stream of successive balances in the account: (define (stream-withdraw balance amount-stream) (cons-stream balance (stream-withdraw (- balance (stream-car amount-stream)) (stream-cdr amount-stream)))) Stream-withdraw implements a well-defined mathematical function whose output is fully determined by its input. Suppose, however, that the input amount-stream is the stream of successive values typed by the user and that the resulting stream of balances is displayed. Then, from the perspective of the user who is typing values and watching results, the stream process has the same behavior as the object created by make-simplified-withdraw. However, with the stream version, there is no assignment, no local state variable, and consequently none of the theoretical difficulties that we encountered in Section 3.1.3. Yet the system has state! This is really remarkable. Even though stream-withdraw implements a well-defined mathematical function whose behavior does not change, the user’s perception here is one of interacting with a system that has a changing state. One way to resolve this paradox is to realize that it is the user’s temporal existence that imposes state on the system. If the user could step 541 back from the interaction and think in terms of streams of balances rather than individual transactions, the system would appear stateless.73 From the point of view of one part of a complex process, the other parts appear to change with time. They have hidden time-varying local state. If we wish to write programs that model this kind of natural decomposition in our world (as we see it from our viewpoint as a part of that world) with structures in our computer, we make computational objects that are not functional—they must change with time. We model state with local state variables, and we model the changes of state with assignments to those variables. By doing this we make the time of execution of a computation model time in the world that we are part of, and thus we get “objects” in our computer. Modeling with objects is powerful and intuitive, largely because this matches the perception of interacting with a world of which we are part. However, as we’ve seen repeatedly throughout this chapter, these models raise thorny problems of constraining the order of events and of synchronizing multiple processes. The possibility of avoiding these problems has stimulated the development of functional programming languages, which do not include any provision for assignment or mutable data. In such a language, all procedures implement well-defined mathematical functions of their arguments, whose behavior does not change. The functional approach is extremely attractive for dealing with concurrent systems.74 73 Similarly in physics, when we observe a moving particle, we say that the position (state) of the particle is changing. However, from the perspective of the particle’s world line in space-time there is no change involved. 74 John Backus, the inventor of Fortran, gave high visibility to functional program- ming when he was awarded the ACM Turing award in 1978. His acceptance speech 542 On the other hand, if we look closely, we can see time-related problems creeping into functional models as well. One particularly troublesome area arises when we wish to design interactive systems, especially ones that model interactions between independent entities. For instance, consider once more the implementation a banking system that permits joint bank accounts. In a conventional system using assignment and objects, we would model the fact that Peter and Paul share an account by having both Peter and Paul send their transaction requests to the same bank-account object, as we saw in Section 3.1.3. From the stream point of view, where there are no “objects” per se, we have already indicated that a bank account can be modeled as a process that operates on a stream of transaction requests to produce a stream of responses. Accordingly, we could model the fact that Peter and Paul have a joint bank account by merging Peter’s stream of transaction requests with Paul’s stream of requests and feeding the result to the bank-account stream process, as shown in Figure 3.38. Figure 3.38: A joint bank account, modeled by merging two streams of transaction requests. (Backus 1978) strongly advocated the functional approach. A good overview of functional programming is given in Henderson 1980 and in Darlington et al. 1982. 543 The trouble with this formulation is in the notion of merge. It will not do to merge the two streams by simply taking alternately one request from Peter and one request from Paul. Suppose Paul accesses the account only very rarely. We could hardly force Peter to wait for Paul to access the account before he could issue a second transaction. However such a merge is implemented, it must interleave the two transaction streams in some way that is constrained by “real time” as perceived by Peter and Paul, in the sense that, if Peter and Paul meet, they can agree that certain transactions were processed before the meeting, and other transactions were processed after the meeting.75 This is precisely the same constraint that we had to deal with in Section 3.4.1, where we found the need to introduce explicit synchronization to ensure a “correct” order of events in concurrent processing of objects with state. Thus, in an attempt to support the functional style, the need to merge inputs from different agents reintroduces the same problems that the functional style was meant to eliminate. We began this chapter with the goal of building computational models whose structure matches our perception of the real world we are trying to model. We can model the world as a collection of separate, time-bound, interacting objects with state, or we can model the world as a single, time- 75 Observe that, for any two streams, there is in general more than one acceptable or- der of interleaving. Thus, technically, “merge” is a relation rather than a function— the answer is not a deterministic function of the inputs. We already mentioned (Footnote 39) that nondeterminism is essential when dealing with concurrency. The merge relation illustrates the same essential nondeterminism, from the functional perspective. In Section 4.3, we will look at nondeterminism from yet another point of view. 544 less, stateless unity. Each view has powerful advantages, but neither view alone is completely satisfactory. A grand unification has yet to emerge.76 76 The object model approximates the world by dividing it into separate pieces. The functional model does not modularize along object boundaries. The object model is useful when the unshared state of the “objects” is much larger than the state that they share. An example of a place where the object viewpoint fails is quantum mechanics, where thinking of things as individual particles leads to paradoxes and confusions. Unifying the object view with the functional view may have little to do with programming, but rather with fundamental epistemological issues. 4 Metalinguistic Abstraction . . . It’s in words that the magic is—Abracadabra, Open Sesame, and the rest—but the magic words in one story aren’t magical in the next. The real magic is to understand which words work, and when, and for what; the trick is to learn the trick. . . . And those words are made from the letters of our alphabet: a couple-dozen squiggles we can draw with the pen. This is the key! And the treasure, too, if we can only get our hands on it! It’s as if—as if the key to the treasure is the treasure! —John Barth, Chimera In our study of program design, we have seen that expert programmers control the complexity of their designs with the same general techniques used by designers of all complex systems. They combine primitive elements to form compound objects, they abstract compound objects to form higher-level building blocks, and they preserve modularity by adopting appropriate large-scale views of system structure. In illustrating these techniques, we have used Lisp as a language for describing processes and for constructing computational data objects and processes to model complex phenomena in the real world. However, as we confront increasingly complex problems, we will find that Lisp, or indeed any fixed programming language, is not sufficient for our needs. We must constantly turn to new languages in order to express our ideas more effectively. Establishing new languages is a powerful strategy for controlling complexity in engineering design; we can often enhance our ability to deal with a complex problem by adopting a new language that enables us to describe (and hence to think about) the problem in a different way, using primitives, means of combina- 546 tion, and means of abstraction that are particularly well suited to the problem at hand.1 Programming is endowed with a multitude of languages. There are physical languages, such as the machine languages for particular computers. These languages are concerned with the representation of data and control in terms of individual bits of storage and primitive machine instructions. The machine-language programmer is concerned with using the given hardware to erect systems and utilities for the efficient implementation of resource-limited computations. High-level languages, erected on a machine-language substrate, hide concerns about the representation of data as collections of bits and the representation of programs as sequences 1 The same idea is pervasive throughout all of engineering. For example, electrical engineers use many different languages for describing circuits. Two of these are the language of electrical networks and the language of electrical systems. The network language emphasizes the physical modeling of devices in terms of discrete electrical elements. The primitive objects of the network language are primitive electrical components such as resistors, capacitors, inductors, and transistors, which are characterized in terms of physical variables called voltage and current. When describing circuits in the network language, the engineer is concerned with the physical characteristics of a design. In contrast, the primitive objects of the system language are signal-processing modules such as filters and amplifiers. Only the functional behavior of the modules is relevant, and signals are manipulated without concern for their physical realization as voltages and currents. The system language is erected on the network language, in the sense that the elements of signal-processing systems are constructed from electrical networks. Here, however, the concerns are with the large-scale organization of electrical devices to solve a given application problem; the physical feasibility of the parts is assumed. This layered collection of languages is another example of the stratified design technique illustrated by the picture language of Section 2.2.4. 547 of primitive instructions. These languages have means of combination and abstraction, such as procedure definition, that are appropriate to the larger-scale organization of systems. Metalinguistic abstraction—establishing new languages—plays an important role in all branches of engineering design. It is particularly important to computer programming, because in programming not only can we formulate new languages but we can also implement these languages by constructing evaluators. An evaluator (or interpreter) for a programming language is a procedure that, when applied to an expression of the language, performs the actions required to evaluate that expression. It is no exaggeration to regard this as the most fundamental idea in programming: The evaluator, which determines the meaning of expressions in a programming language, is just another program. To appreciate this point is to change our images of ourselves as programmers. We come to see ourselves as designers of languages, rather than only users of languages designed by others. In fact, we can regard almost any program as the evaluator for some language. For instance, the polynomial manipulation system of Section 2.5.3 embodies the rules of polynomial arithmetic and implements them in terms of operations on list-structured data. If we augment this system with procedures to read and print polynomial expressions, we have the core of a special-purpose language for dealing with problems in symbolic mathematics. The digital-logic simulator of Section 3.3.4 and the constraint propagator of Section 3.3.5 are legitimate languages in their own right, each with its own primitives, means of combination, and means of abstraction. Seen from this perspective, the technology for coping with large-scale computer 548 systems merges with the technology for building new computer languages, and computer science itself becomes no more (and no less) than the discipline of constructing appropriate descriptive languages. We now embark on a tour of the technology by which languages are established in terms of other languages. In this chapter we shall use Lisp as a base, implementing evaluators as Lisp procedures. Lisp is particularly well suited to this task, because of its ability to represent and manipulate symbolic expressions. We will take the first step in understanding how languages are implemented by building an evaluator for Lisp itself. The language implemented by our evaluator will be a subset of the Scheme dialect of Lisp that we use in this book. Although the evaluator described in this chapter is written for a particular dialect of Lisp, it contains the essential structure of an evaluator for any expression-oriented language designed for writing programs for a sequential machine. (In fact, most language processors contain, deep within them, a little “Lisp” evaluator.) The evaluator has been simplified for the purposes of illustration and discussion, and some features have been left out that would be important to include in a production-quality Lisp system. Nevertheless, this simple evaluator is adequate to execute most of the programs in this book.2 An important advantage of making the evaluator accessible as a Lisp program is that we can implement alternative evaluation rules by describing these as modifications to the evaluator program. One place where we can use this power to good effect is to gain extra control over the ways in which 2 The most important features that our evaluator leaves out are mechanisms for han- dling errors and supporting debugging. For a more extensive discussion of evaluators, see Friedman et al. 1992, which gives an exposition of programming languages that proceeds via a sequence of evaluators written in Scheme. 549 computational models embody the notion of time, which was so central to the discussion in Chapter 3. There, we mitigated some of the complexities of state and assignment by using streams to decouple the representation of time in the world from time in the computer. Our stream programs, however, were sometimes cumbersome, because they were constrained by the applicative-order evaluation of Scheme. In Section 4.2, we’ll change the underlying language to provide for a more elegant approach, by modifying the evaluator to provide for normal-order evaluation. Section 4.3 implements a more ambitious linguistic change, whereby expressions have many values, rather than just a single value. In this language of nondeterministic computing, it is natural to express processes that generate all possible values for expressions and then search for those values that satisfy certain constraints. In terms of models of computation and time, this is like having time branch into a set of “possible futures” and then searching for appropriate time lines. With our nondeterministic evaluator, keeping track of multiple values and performing searches are handled automatically by the underlying mechanism of the language. In Section 4.4 we implement a logic-programming language in which knowledge is expressed in terms of relations, rather than in terms of computations with inputs and outputs. Even though this makes the language drastically different from Lisp, or indeed from any conventional language, we will see that the logic-programming evaluator shares the essential structure of the Lisp evaluator. 4.1 The Metacircular Evaluator Our evaluator for Lisp will be implemented as a Lisp program. It may seem circular to think about evaluating Lisp programs using an evaluator that is 550 itself implemented in Lisp. However, evaluation is a process, so it is appropriate to describe the evaluation process using Lisp, which, after all, is our tool for describing processes.3 An evaluator that is written in the same language that it evaluates is said to be metacircular. The metacircular evaluator is essentially a Scheme formulation of the environment model of evaluation described in Section 3.2. Recall that the model has two basic parts: 1. To evaluate a combination (a compound expression other than a special form), evaluate the subexpressions and then apply the value of the operator subexpression to the values of the operand subexpressions. 2. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, extend the environment part of the procedure object by a frame in which the formal parameters of the procedure are bound to the arguments to which the procedure is applied. These two rules describe the essence of the evaluation process, a basic cycle in which expressions to be evaluated in environments are reduced to procedures to be applied to arguments, which in turn are reduced to new expressions to be evaluated in new environments, and so on, until we get down to symbols, whose values are looked up in the environment, 3 Even so, there will remain important aspects of the evaluation process that are not elucidated by our evaluator. The most important of these are the detailed mechanisms by which procedures call other procedures and return values to their callers. We will address these issues in Chapter 5, where we take a closer look at the evaluation process by implementing the evaluator as a simple register machine. 551 and to primitive procedures, which are applied directly (see Figure 4.1).4 This evaluation cycle will be embodied by the interplay between the two critical procedures in the evaluator, eval and apply, which are described in Section 4.1.1 (see Figure 4.1). The implementation of the evaluator will depend upon procedures that define the syntax of the expressions to be evaluated. We will use data abstraction to make the evaluator independent of the representation of the language. For example, rather than committing to a choice that an 4 If we grant ourselves the ability to apply primitives, then what remains for us to im- plement in the evaluator? The job of the evaluator is not to specify the primitives of the language, but rather to provide the connective tissue—the means of combination and the means of abstraction—that binds a collection of primitives to form a language. Specifically: • The evaluator enables us to deal with nested expressions. For example, although simply applying primitives would suffice for evaluating the expression (+ 1 6), it is not adequate for handling (+ 1 (* 2 3)). As far as the primitive procedure + is concerned, its arguments must be numbers, and it would choke if we passed it the expression (* 2 3) as an argument. One important role of the evaluator is to choreograph procedure composition so that (* 2 3) is reduced to 6 before being passed as an argument to +. • The evaluator allows us to use variables. For example, the primitive procedure for addition has no way to deal with expressions such as (+ x 1). We need an evaluator to keep track of variables and obtain their values before invoking the primitive procedures. • The evaluator allows us to define compound procedures. This involves keeping track of procedure definitions, knowing how to use these definitions in evaluating expressions, and providing a mechanism that enables procedures to accept arguments. • The evaluator provides the special forms, which must be evaluated differently from procedure calls. 552 assignment is to be represented by a list beginning with the symbol set! we use an abstract predicate assignment? to test for an assignment, and we use abstract selectors assignment-variable and assignment-value to access the parts of an assignment. Implementation of expressions will be described in detail in Section 4.1.2. There are also operations, described in Section 4.1.3, that specify the representation of procedures and environments. For example, make-procedure constructs compound procedures, lookup-variable-value accesses the values of variables, and apply-primitive-procedure applies a primitive procedure to a given list of arguments. 4.1.1 The Core of the Evaluator Figure 4.1: The eval-apply cycle exposes the essence of a computer language. The evaluation process can be described as the interplay between two procedures: eval and apply. Eval Eval takes as arguments an expression and an environment. It classifies the expression and directs its evaluation. Eval is structured as a case analysis of the syntactic type of the expression to be evaluated. In order to keep the procedure general, we express the determination of the type of an expression abstractly, making no commitment to any particular representation for the various types of expressions. Each type of expression has a predicate that tests for it and an abstract means for selecting its parts. This abstract syntax makes it easy to see how we can change the syntax of the language by using the same evaluator, but with a different collection of syntax procedures. Primitive expressions • For self-evaluating expressions, such as numbers, eval returns the expression itself. • Eval must look up variables in the environment to find their values. Special forms • For quoted expressions, eval returns the expression that was quoted. • An assignment to (or a definition of ) a variable must recursively call eval to compute the new value to be associated with the variable. The environment must be modified to change (or create) the binding of the variable. • An if expression requires special processing of its parts, so as to evaluate the consequent if the predicate is true, and otherwise to evaluate the alternative. • A lambda expression must be transformed into an applicable procedure 554 by packaging together the parameters and body specified by the lambda expression with the environment of the evaluation. • A begin expression requires evaluating its sequence of expressions in the order in which they appear. • A case analysis (cond) is transformed into a nest of if expressions and then evaluated. • For a procedure application, eval must recursively evaluate the operator part and the operands of the combination. The resulting procedure and arguments are passed to apply, which handles the actual procedure application. Here is the definition of eval: (define (eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (eval (cond->if exp) env)) 555 ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown expression type - EVAL" exp)))) For clarity, eval has been implemented as a case analysis using cond. The disadvantage of this is that our procedure handles only a few distinguishable types of expressions, and no new ones can be defined without editing the definition of eval. In most Lisp implementations, dispatching on the type of an expression is done in a data-directed style. This allows a user to add new types of expressions that eval can distinguish, without modifying the definition of eval itself. (See Exercise 4.3.) Apply Apply takes two arguments, a procedure and a list of arguments to which the procedure should be applied. Apply classifies procedures into two kinds: It calls apply-primitive-procedure to apply primitives; it applies compound procedures by sequentially evaluating the expressions that make up the body of the procedure. The environment for the evaluation of the body of a compound procedure is constructed by extending the base environment carried by the procedure to include a frame that binds the parameters of the procedure to the arguments to which the procedure is to be applied. Here is the definition of apply: (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) 556 (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type - APPLY" Procedure arguments When eval processes a procedure application, it uses list-of-values to produce the list of arguments to which the procedure is to be applied. List-of-values takes as an argument the operands of the combination. It evaluates each operand and returns a list of the corresponding values:5 (define (list-of-values exps env) (if (no-operands? exps) ’() (cons (eval (first-operand exps) env) (list-of-values (rest-operands exps) env)))) 5 We could have simplified the application? clause in eval by using map (and stipu- lating that operands returns a list) rather than writing an explicit list-of-values procedure. We chose not to use map here to emphasize the fact that the evaluator can be implemented without any use of higher-order procedures (and thus could be written in a language that doesn’t have higher-order procedures), even though the language that it supports will include higher-order procedures. Conditionals Eval-if evaluates the predicate part of an if expression in the given envi- ronment. If the result is true, eval-if evaluates the consequent, otherwise it evaluates the alternative: (define (eval-if exp env) (if (true? (eval (if-predicate exp) env)) (eval (if-consequent exp) env) (eval (if-alternative exp) env))) The use of true? in eval-if highlights the issue of the connection between an implemented language and an implementation language. The if-predicate is evaluated in the language being implemented and thus yields a value in that language. The interpreter predicate true? translates that value into a value that can be tested by the if in the implementation language: The metacircular representation of truth might not be the same as that of the underlying Scheme.6 Sequences Eval-sequence is used by apply to evaluate the sequence of expressions in a procedure body and by eval to evaluate the sequence of expressions in a begin expression. It takes as arguments a sequence of expressions and an environment, and evaluates the expressions in the order in which they occur. The value returned is the value of the final expression. (define (eval-sequence exps env) (cond ((last-exp? exps) (eval (first-exp exps) env)) 6 In this case, the language being implemented and the implementation language are the same. Contemplation of the meaning of true? here yields expansion of consciousness without the abuse of substance. 558 (else (eval (first-exp exps) env) (eval-sequence (rest-exps exps) env)))) Assignments and definitions The following procedure handles assignments to variables. It calls eval to find the value to be assigned and transmits the variable and the resulting value to set-variable-value! to be installed in the designated environment. (define (eval-assignment exp env) (set-variable-value! (assignment-variable exp) (eval (assignment-value exp) env) env) ’ok) Definitions of variables are handled in a similar manner.7 (define (eval-definition exp env) (define-variable! (definition-variable exp) (eval (definition-value exp) env) env) ’ok) We have chosen here to return the symbol ok as the value of an assignment or a definition.8 7 This implementation of define ignores a subtle issue in the handling of internal definitions, although it works correctly in most cases. We will see what the problem is and how to solve it in Section 4.1.6. 8 As we said when we introduced define and set!, these values are dependent in Scheme—that is, the implementor can choose what value to return. 559 Exercise 4.1: Notice that we cannot tell whether the metacircular evaluator evaluates operands from left to right or from right to left. Its evaluation order is inherited from the underlying Lisp: If the arguments to cons in list-of-values are evaluated from left to right, then list-of-values will evaluate operands from left to right; and if the arguments to cons are evaluated from right to left, then list-of-values will evaluate operands from right to left. Write a version of list-of-values that evaluates operands from left to right regardless of the order of evaluation in the underlying Lisp. Also write a version of list-of-values that evaluates operands from right to left. 4.1.2 Representing Expressions The evaluator is reminiscent of the symbolic differentiation program discussed in Section 2.3.2. Both programs operate on symbolic expressions. In both programs, the result of operating on a compound expression is determined by operating recursively on the pieces of the expression and combining the results in a way that depends on the type of the expression. In both programs we used data abstraction to decouple the general rules of operation from the details of how expressions are represented. In the differentiation program this meant that the same differentiation procedure could deal with algebraic expressions in prefix form, in infix form, or in some other form. For the evaluator, this means that the syntax of the language being evaluated is determined solely by the procedures that classify and extract pieces of expressions. Here is the specification of the syntax of our language: • The only self-evaluating items are numbers and strings: 560 (define (self-evaluating? exp) (cond ((number? exp) true) ((string? exp) true) (else false))) • Variables are represented by symbols: (define (variable? exp) (symbol? exp)) • Quotations have the form (quote ):9 (define (quoted? exp) (tagged-list? exp ’quote)) (define (text-of-quotation exp) (cadr exp)) Quoted? is defined in terms of the procedure tagged-list?, which identifies lists beginning with a designated symbol: (define (tagged-list? exp tag) (if (pair? exp) (eq? (car exp) tag) false)) • Assignments have the form (set! ): (define (assignment? exp) (tagged-list? exp ’set!)) (define (assignment-variable exp) (cadr exp)) 9 As mentioned in Section 2.3.1, the evaluator sees a quoted expression as a list be- ginning with quote, even if the expression is typed with the quotation mark. For example, the expression ’a would be seen by the evaluator as (quote a). See Exercise 2.55. (define (assignment-value exp) (caddr exp)) • Definitions have the form (define 〈var〉〈value〉) or the form (define (〈var〉〈par amet er 1 〉 ... 〈par amet er n 〉) 〈body〉) The latter form (standard procedure definition) is syntactic sugar for (define 〈var〉 (lambda (〈par amet er 1 〉 ... 〈par amet er n 〉) 〈body〉)) The corresponding syntax procedures are the following: (define (definition? exp) (tagged-list? exp ’define)) (define (definition-variable exp) (if (symbol? (cadr exp)) (cadr exp) (caadr exp))) (define (definition-value exp) (if (symbol? (cadr exp)) (caddr exp) ; formal parameters (cddr exp)))) ; body (make-lambda (cdadr exp) • Lambda expressions are lists that begin with the symbol lambda: 562 (define (lambda? exp) (tagged-list? exp ’lambda)) (define (lambda-parameters exp) (cadr exp)) (define (lambda-body exp) (cddr exp)) We also provide a constructor for lambda expressions, which is used by definition-value, above: (define (make-lambda parameters body) (cons ’lambda (cons parameters body))) • Conditionals begin with if and have a predicate, a consequent, and an (optional) alternative. If the expression has no alternative part, we provide false as the alternative.10 (define (if? exp) (tagged-list? exp ’if)) (define (if-predicate exp) (cadr exp)) (define (if-consequent exp) (caddr exp)) (define (if-alternative exp) (if (not (null? (cdddr exp))) (cadddr exp) ’false)) 10 The value of an if expression when the predicate is false and there is no alternative is unspecified in Scheme; we have chosen here to make it false. We will support the use of the variables true and false in expressions to be evaluated by binding them in the global environment. See Section 4.1.4. 563 We also provide a constructor for if expressions, to be used by cond->if to transform cond expressions into if expressions: (define (make-if predicate consequent alternative) (list ’if predicate consequent alternative)) • Begin packages a sequence of expressions into a single expression. We include syntax operations on begin expressions to extract the actual sequence from the begin expression, as well as selectors that return the first expression and the rest of the expressions in the sequence.11 (define (begin? exp) (tagged-list? exp ’begin)) (define (begin-actions exp) (cdr exp)) (define (last-exp? seq) (null? (cdr seq))) (define (first-exp seq) (car seq)) (define (rest-exps seq) (cdr seq)) We also include a constructor sequence->exp (for use by cond->if) that transforms a sequence into a single expression, using begin if necessary: (define (sequence->exp seq) (cond ((null? seq) seq) ((last-exp? seq) (first-exp seq)) 11 These selectors for a list of expressions—and the corresponding ones for a list of operands—are not intended as a data abstraction. They are introduced as mnemonic names for the basic list operations in order to make it easier to understand the explicit-control evaluator in Section 5.4. 564 (else (make-begin seq)))) (define (make-begin seq) (cons ’begin seq)) • A procedure application is any compound expression that is not one of the above expression types. The car of the expression is the operator, and the cdr is the list of operands: (define (application? exp) (pair? exp)) (define (operator exp) (car exp)) (define (operands exp) (cdr exp)) (define (no-operands? ops) (null? ops)) (define (first-operand ops) (car ops)) (define (rest-operands ops) (cdr ops)) Derived expressions Some special forms in our language can be defined in terms of expressions involving other special forms, rather than being implemented directly. One example is cond, which can be implemented as a nest of if expressions. For example, we can reduce the problem of evaluating the expression (cond ((> x 0) x) ((= x 0) (display ’zero) 0) (else (- x))) 565 to the problem of evaluating the following expression involving if and begin expressions: (if (> x 0) x (if (= x 0) (begin (display ’zero) 0) (- x))) Implementing the evaluation of cond in this way simplifies the evaluator because it reduces the number of special forms for which the evaluation process must be explicitly specified. We include syntax procedures that extract the parts of a cond expression, and a procedure cond->if that transforms cond expressions into if expressions. A case analysis begins with cond and has a list of predicateaction clauses. A clause is an else clause if its predicate is the symbol else.12 (define (cond? exp) (tagged-list? exp ’cond)) (define (cond-clauses exp) (cdr exp)) (define (cond-else-clause? clause) (eq? (cond-predicate clause) ’else)) (define (cond-predicate clause) (car clause)) 12 The value of a cond expression when all the predicates are false and there is no else clause is unspecified in Scheme; we have chosen here to make it false. (define (cond-actions clause) (cdr clause)) (define (cond->if exp) (expand-clauses (cond-clauses exp))) (define (expand-clauses clauses) (if (null? clauses) ; no else clause ’false (let ((first (car clauses)) (rest (cdr clauses))) (if (cond-else-clause? first) (if (null? rest) (sequence->exp (cond-actions first)) (error "ELSE clause isn’t last - COND->IF" clauses)) (make-if (cond-predicate first) (sequence->exp (cond-actions first)) (expand-clauses rest)))))) Expressions (such as cond) that we choose to implement as syntactic transformations are called derived expressions. Let expressions are also derived expressions (see Exercise 4.6).13 13 Practical Lisp systems provide a mechanism that allows a user to add new derived expressions and specify their implementation as syntactic transformations without modifying the evaluator. Such a user-defined transformation is called a macro. Although it is easy to add an elementary mechanism for defining macros, the resulting language has subtle name-conflict problems. There has been much research 567 Exercise 4.2: Louis Reasoner plans to reorder the cond clauses in eval so that the clause for procedure applications appears before the clause for assignments. He argues that this will make the interpreter more efficient: Since programs usually contain more applications than assignments, definitions, and so on, his modified eval will usually check fewer clauses than the original eval before identifying the type of an expression. a. What is wrong with Louis’s plan? (Hint: What will Louis’s evaluator do with the expression (define x 3)?) b. Louis is upset that his plan didn’t work. He is willing to go to any lengths to make his evaluator recognize procedure applications before it checks for most other kinds of expressions. Help him by changing the syntax of the evaluated language so that procedure applications start with call. For example, instead of (factorial 3) we will now have to write (call factorial 3) and instead of (+ 1 2) we will have to write (call + 1 2). Exercise 4.3: Rewrite eval so that the dispatch is done in datadirected style. Compare this with the data-directed differentiation procedure of Exercise 2.73. (You may use the car of a compound expression as the type of the expression, as is appropriate for the syntax implemented in this section.) Exercise 4.4: Recall the definitions of the special forms and and or from Chapter 1: • and: The expressions are evaluated from left to right. If any exon mechanisms for macro definition that do not cause these difficulties. See, for example, Kohlbecker 1986, Clinger and Rees 1991, and Hanson 1991. 568 pression evaluates to false, false is returned; any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then true is returned. • or: The expressions are evaluated from left to right. If any expression evaluates to a true value, that value is returned; any remaining expressions are not evaluated. If all expressions evaluate to false, or if there are no expressions, then false is returned. Install and and or as new special forms for the evaluator by defining appropriate syntax procedures and evaluation procedures eval-and and eval-or. Alternatively, show how to implement and and or as derived expressions. Exercise 4.5: Scheme allows an additional syntax for cond clauses, ( => ). If 〈test 〉 evaluates to a true value, then 〈recipient 〉 is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the 〈test 〉, and the result is returned as the value of the cond expression. For example (cond ((assoc ’b ’((a 1) (b 2))) => cadr) (else false)) returns 2. Modify the handling of cond so that it supports this extended syntax. Exercise 4.6: Let expressions are derived expressions, because (let ((〈v ar 1 〉〈exp 1 〉) ... (〈v ar n 〉〈exp n 〉)) 〈body〉) is equivalent to 569 ((lambda (〈v ar 1 〉 ... 〈v ar n 〉) 〈body〉) 〈exp 1 〉 ... 〈exp n 〉) Implement a syntactic transformation let->combination that reduces evaluating let expressions to evaluating combinations of the type shown above, and add the appropriate clause to eval to handle let expressions. Exercise 4.7: Let* is similar to let, except that the bindings of the let* variables are performed sequentially from left to right, and each binding is made in an environment in which all of the preceding bindings are visible. For example (let* ((x 3) (y (+ x 2)) (z (+ x y 5))) (* x z)) returns 39. Explain how a let* expression can be rewritten as a set of nested let expressions, and write a procedure let*->nested-lets that performs this transformation. If we have already implemented let (Exercise 4.6) and we want to extend the evaluator to handle let*, is it sufficient to add a clause to eval whose action is (eval (let*->nested-lets exp) env) or must we explicitly expand let* in terms of non-derived expressions? 570 Exercise 4.8: “Named let” is a variant of let that has the form (let 〈var〉〈bindings〉〈body〉) The 〈bindings 〉 and 〈body 〉 are just as in ordinary let, except that 〈var 〉 is bound within 〈body 〉 to a procedure whose body is 〈body 〉 and whose parameters are the variables in the 〈 bindings 〉. Thus, one can repeatedly execute the 〈body 〉 by invoking the procedure named 〈var 〉. For example, the iterative Fibonacci procedure (Section 1.2.2) can be rewritten using named let as follows: (define (fib n) (let fib-iter ((a 1) (b 0) (count n)) (if (= count 0) b (fib-iter (+ a b) a (- count 1))))) Modify let->combination of Exercise 4.6 to also support named let. Exercise 4.9: Many languages support a variety of iteration constructs, such as do, for, while, and until. In Scheme, iterative processes can be expressed in terms of ordinary procedure calls, so special iteration constructs provide no essential gain in computational power. On the other hand, such constructs are often convenient. Design some iteration constructs, give examples of their use, and show how to implement them as derived expressions. Exercise 4.10: By using data abstraction, we were able to write an eval procedure that is independent of the particular syntax of the language to be evaluated. To illustrate this, design and implement 571 a new syntax for Scheme by modifying the procedures in this section, without changing eval or apply. 4.1.3 Evaluator Data Structures In addition to defining the external syntax of expressions, the evaluator implementation must also define the data structures that the evaluator manipulates internally, as part of the execution of a program, such as the representation of procedures and environments and the representation of true and false. Testing of predicates For conditionals, we accept anything to be true that is not the explicit false object. (define (true? x) (not (eq? x false))) (define (false? x) (eq? x false)) Representing procedures To handle primitives, we assume that we have available the following procedures: • (apply-primitive-procedure <proc> <args>) applies the given primitive procedure to the argument values in the list 〈args 〉 and returns the result of the application. • (primitive-procedure? <proc>) tests whether 〈proc 〉 is a primitive procedure. 572 These mechanisms for handling primitives are further described in Section 4.1.4. Compound procedures are constructed from parameters, procedure bodies, and environments using the constructor make-procedure: (define (make-procedure parameters body env) (list ’procedure parameters body env)) (define (compound-procedure? p) (tagged-list? p ’procedure)) (define (procedure-parameters p) (cadr p)) (define (procedure-body p) (caddr p)) (define (procedure-environment p) (cadddr p)) Operations on Environments The evaluator needs operations for manipulating environments. explained in Section 3.2, an environment is a sequence of frames, where each frame is a table of bindings that associate variables with their corresponding values. We use the following operations for manipulating environments: • (lookup-variable-value <env>) returns the value that is bound to the symbol 〈var 〉 in the environment 〈env 〉, or signals an error if the variable is unbound. • (extend-environment ) 573 returns a new environment, consisting of a new frame in which the symbols in the list 〈variables 〉 are bound to the corresponding elements in the list 〈values 〉, where the enclosing environment is the environment 〈base-env 〉. • (define-variable! <env>) adds to the first frame in the environment 〈env 〉 a new binding that associates the variable 〈var 〉 with the value 〈value 〉. • (set-variable-value! <env>) changes the binding of the variable 〈var 〉 in the environment 〈env 〉 so that the variable is now bound to the value 〈value 〉, or signals an error if the variable is unbound. To implement these operations we represent an environment as a list of frames. The enclosing environment of an environment is the cdr of the list. The empty environment is simply the empty list. (define (enclosing-environment env) (cdr env)) (define (first-frame env) (car env)) (define the-empty-environment ’()) Each frame of an environment is represented as a pair of lists: a list of the variables bound in that frame and a list of the associated values.14 (define (make-frame variables values) 14 Frames are not really a data abstraction in the following code: Set-variable- value! and define-variable! use set-car! to directly modify the values in a frame. The purpose of the frame procedures is to make the environment- manipulation procedures easy to read. 574 (cons variables values)) (define (frame-variables frame) (car frame)) (define (frame-values frame) (cdr frame)) (define (add-binding-to-frame! var val frame) (set-car! frame (cons var (car frame))) (set-cdr! frame (cons val (cdr frame)))) To extend an environment by a new frame that associates variables with values, we make a frame consisting of the list of variables and the list of values, and we adjoin this to the environment. We signal an error if the number of variables does not match the number of values. (define (extend-environment vars vals base-env) (if (= (length vars) (length vals)) (cons (make-frame vars vals) base-env) (if (< (length vars) (length vals)) (error "Too many arguments supplied" vars vals) (error "Too few arguments supplied" vars vals)))) To look up a variable in an environment, we scan the list of variables in the first frame. If we find the desired variable, we return the corresponding element in the list of values. If we do not find the variable in the current frame, we search the enclosing environment, and so on. If we reach the empty environment, we signal an “unbound variable” error. (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals) 575 (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (car vals)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) (env-loop env)) To set a variable to a new value in a specified environment, we scan for the variable, just as in lookup-variable-value, and change the corresponding value when we find it. (define (set-variable-value! var val env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable - SET!" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) 576 (env-loop env)) To define a variable, we search the first frame for a binding for the variable, and change the binding if it exists (just as in set-variable-value!). If no such binding exists, we adjoin one to the first frame. (define (define-variable! var val env) (let ((frame (first-frame env))) (define (scan vars vals) (cond ((null? vars) (add-binding-to-frame! var val frame)) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (scan (frame-variables frame) (frame-values frame)))) The method described here is only one of many plausible ways to represent environments. Since we used data abstraction to isolate the rest of the evaluator from the detailed choice of representation, we could change the environment representation if we wanted to. (See Exercise 4.11.) In a production-quality Lisp system, the speed of the evaluator’s environment operations—especially that of variable lookup—has a major impact on the performance of the system. The representation described here, although conceptually simple, is not efficient and would not ordinarily be used in a production system.15 15 The drawback of this representation (as well as the variant in Exercise 4.11) is that the evaluator may have to search through many frames in order to find the binding for a given variable. (Such an approach is referred to as deep binding.) One way to 577 Exercise 4.11: Instead of representing a frame as a pair of lists, we can represent a frame as a list of bindings, where each binding is a name-value pair. Rewrite the environment operations to use this alternative representation. Exercise 4.12: The procedures set-variable-value!, definevariable! and lookup-variable-value can be expressed in terms of more abstract procedures for traversing the environment structure. Define abstractions that capture the common patterns and redefine the three procedures in terms of these abstractions. Exercise 4.13: Scheme allows us to create new bindings for variables by means of define, but provides no way to get rid of bindings. Implement for the evaluator a special form make-unbound! that removes the binding of a given symbol from the environment in which the make-unbound! expression is evaluated. This problem is not completely specified. For example, should we remove only the binding in the first frame of the environment? Complete the specification and justify any choices you make. 4.1.4 Running the Evaluator as a Program Given the evaluator, we have in our hands a description (expressed in Lisp) of the process by which Lisp expressions are evaluated. One advantage of expressing the evaluator as a program is that we can run the program. This gives us, running within Lisp, a working model of how Lisp itself evaluates expressions. This can serve as a framework for experimenting with evaluation rules, as we shall do later in this chapter. avoid this inefficiency is to make use of a strategy called lexical addressing, which will be discussed in Section 5.5.6. 578 Our evaluator program reduces expressions ultimately to the application of primitive procedures. Therefore, all that we need to run the evaluator is to create a mechanism that calls on the underlying Lisp system to model the application of primitive procedures. There must be a binding for each primitive procedure name, so that when eval evaluates the operator of an application of a primitive, it will find an object to pass to apply. We thus set up a global environment that associates unique objects with the names of the primitive procedures that can appear in the expressions we will be evaluating. The global environment also includes bindings for the symbols true and false, so that they can be used as variables in expressions to be evaluated. (define (setup-environment) (let ((initial-env (extend-environment (primitive-procedure-names) (primitive-procedure-objects) the-empty-environment))) (define-variable! ’true true initial-env) (define-variable! ’false false initial-env) initial-env)) (define the-global-environment (setup-environment)) It does not matter how we represent the primitive procedure objects, so long as apply can identify and apply them by using the procedures primitive-procedure? and apply-primitive-procedure. We have chosen to represent a primitive procedure as a list beginning with the symbol primitive and containing a procedure in the underlying Lisp that implements that primitive. 579 (define (primitive-procedure? proc) (tagged-list? proc ’primitive)) (define (primitive-implementation proc) (cadr proc)) Setup-environment will get the primitive names and implementation procedures from a list:16 (define primitive-procedures (list (list ’car car) (list ’cdr cdr) (list ’cons cons) (list ’null? null?) 〈more primitives〉 )) (define (primitive-procedure-names) (map car primitive-procedures)) (define (primitive-procedure-objects) (map (lambda (proc) (list ’primitive (cadr proc))) primitive-procedures)) 16 Any procedure defined in the underlying Lisp can be used as a primitive for the metacircular evaluator. The name of a primitive installed in the evaluator need not be the same as the name of its implementation in the underlying Lisp; the names are the same here because the metacircular evaluator implements Scheme itself. Thus, for example, we could put (list ’first car) or (list ’square (lambda (x) (* x x))) in the list of primitive-procedures. 580 To apply a primitive procedure, we simply apply the implementation procedure to the arguments, using the underlying Lisp system:17 (define (apply-primitive-procedure proc args) (apply-in-underlying-scheme (primitive-implementation proc) args)) For convenience in running the metacircular evaluator, we provide a driver loop that models the read-eval-print loop of the underlying Lisp system. It prints a prompt, reads an input expression, evaluates this expression in the global environment, and prints the result. We precede each printed result by an output prompt so as to distinguish the value of the expression from other output that may be printed.18 (define input-prompt ";;; M-Eval input:") 17 Apply-in-underlying-scheme is the apply procedure we have used in earlier chapters. The metacircular evaluator’s apply procedure (Section 4.1.1) models the working of this primitive. Having two different things called apply leads to a technical problem in running the metacircular evaluator, because defining the metacircular evaluator’s apply will mask the definition of the primitive. One way around this is to rename the metacircular apply to avoid conflict with the name of the primitive procedure. We have assumed instead that we have saved a reference to the underlying apply by doing (define apply-in-underlying-scheme apply) before defining the metacircular apply. This allows us to access the original version of apply under a different name. 18 The primitive procedure read waits for input from the user, and returns the complete expression that is typed. For example, if the user types (+ 23 x), read returns a three-element list containing the symbol +, the number 23, and the symbol x. If the user types ’x, read returns a two-element list containing the symbol quote and the symbol x. 581 (define output-prompt ";;; M-Eval value:") (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-environment))) (announce-output output-prompt) (user-print output))) (driver-loop)) (define (prompt-for-input string) (newline) (newline) (display string) (newline)) (define (announce-output string) (newline) (display string) We use a special printing procedure, user-print, to avoid printing the environment part of a compound procedure, which may be a very long list (or may even contain cycles). (define (user-print object) (if (compound-procedure? object) (display (list ’compound-procedure (procedure-parameters object) (procedure-body object) ’<procedure-env>)) (display object))) Now all we need to do to run the evaluator is to initialize the global environment and start the driver loop. Here is a sample interaction: 582 (define the-global-environment (setup-environment)) (driver-loop) ;;; M-Eval input: (define (append x y) (if (null? x) y (cons (car x) (append (cdr x) y)))) ;;; M-Eval value: ok ;;; M-Eval input: (append ’(a b c) ’(d e f)) ;;; M-Eval value: (a b c d e f) Exercise 4.14: Eva Lu Ator and Louis Reasoner are each experimenting with the metacircular evaluator. Eva types in the definition of map, and runs some test programs that use it. They work fine. Louis, in contrast, has installed the system version of map as a primitive for the metacircular evaluator. When he tries it, things go terribly wrong. Explain why Louis’s map fails even though Eva’s 4.1.5 Data as Programs In thinking about a Lisp program that evaluates Lisp expressions, an analogy might be helpful. One operational view of the meaning of a program is that a program is a description of an abstract (perhaps infinitely large) machine. For example, consider the familiar program to compute factorials: (define (factorial n) (if (= n 1) 1 (* (factorial (- n 1)) n))) Figure 4.2: The factorial program, viewed as an abstract machine. We may regard this program as the description of a machine containing parts that decrement, multiply, and test for equality, together with a twoposition switch and another factorial machine. (The factorial machine is 584 infinite because it contains another factorial machine within it.) Figure 4.2 is a flow diagram for the factorial machine, showing how the parts are wired together. In a similar way, we can regard the evaluator as a very special machine that takes as input a description of a machine. Given this input, the evaluator configures itself to emulate the machine described. For example, if we feed our evaluator the definition of factorial, as shown in Figure 4.3, the evaluator will be able to compute factorials. From this perspective, our evaluator is seen to be a universal machine. It mimics other machines when these are described as Lisp programs.19 This is striking. Try to imagine an analogous evaluator for electrical circuits. 19 The fact that the machines are described in Lisp is inessential. If we give our evalua- tor a Lisp program that behaves as an evaluator for some other language, say C, the Lisp evaluator will emulate the C evaluator, which in turn can emulate any machine described as a C program. Similarly, writing a Lisp evaluator in C produces a C program that can execute any Lisp program. The deep idea here is that any evaluator can emulate any other. Thus, the notion of “what can in principle be computed” (ignoring practicalities of time and memory required) is independent of the language or the computer, and instead reflects an underlying notion of computability. This was first demonstrated in a clear way by Alan M. Turing (1912-1954), whose 1936 paper laid the foundations for theoretical computer science. In the paper, Turing presented a simple computational model—now known as a Turing machine—and argued that any “effective process” can be formulated as a program for such a machine. (This argument is known as the Church-Turing thesis.) Turing then implemented a universal machine, i.e., a Turing machine that behaves as an evaluator for Turing-machine programs. He used this framework to demonstrate that there are well-posed problems that cannot be computed by Turing machines (see Exercise 4.15), and so by implication cannot be formulated as “effective processes.” Turing went on to make fundamental contributions to practical computer science as well. 585 This would be a circuit that takes as input a signal encoding the plans for some other circuit, such as a filter. Given this input, the circuit evaluator would then behave like a filter with the same description. Such a universal electrical circuit is almost unimaginably complex. It is remarkable that the program evaluator is a rather simple program.20 Figure 4.3: The evaluator emulating a factorial machine. Another striking aspect of the evaluator is that it acts as a bridge between the data objects that are manipulated by our programming language and For example, he invented the idea of structuring programs using general-purpose subroutines. See Hodges 1983 for a biography of Turing. 20 Some people find it counterintuitive that an evaluator, which is implemented by a relatively simple procedure, can emulate programs that are more complex than the evaluator itself. The existence of a universal evaluator machine is a deep and wonderful property of computation. Recursion theory, a branch of mathematical logic, is concerned with logical limits of computation. Douglas Hofstadter’s beautiful book G¨odel, Escher, Bach explores some of these ideas (Hofstadter 586 the programming language itself. Imagine that the evaluator program (implemented in Lisp) is running, and that a user is typing expressions to the evaluator and observing the results. From the perspective of the user, an input expression such as (* x x) is an expression in the programming language, which the evaluator should execute. From the perspective of the evaluator, however, the expression is simply a list (in this case, a list of three symbols: *, x, and x) that is to be manipulated according to a well-defined set of rules. That the user’s programs are the evaluator’s data need not be a source of confusion. In fact, it is sometimes convenient to ignore this distinction, and to give the user the ability to explicitly evaluate a data object as a Lisp expression, by making eval available for use in programs. Many Lisp dialects provide a primitive eval procedure that takes as arguments an expression and an environment and evaluates the expression relative to the environment.21 Thus, (eval ’(* 5 5) user-initial-environment) and (eval (cons ’* (list 5 5)) user-initial-environment) 21 Warning: This eval primitive is not identical to the eval procedure we implemented in Section 4.1.1, because it uses actual Scheme environments rather than the sample environment structures we built in Section 4.1.3. These actual environments cannot be manipulated by the user as ordinary lists; they must be accessed via eval or other special operations. Similarly, the apply primitive we saw earlier is not identical to the metacircular apply, because it uses actual Scheme procedures rather than the procedure objects we constructed in Section 4.1.3 and Section 4.1.4. 587 will both return 25.22 Exercise 4.15: Given a one-argument procedure p and an object a, p is said to “halt” on a if evaluating the expression (p a) returns a value (as opposed to terminating with an error message or running forever). Show that it is impossible to write a procedure halts? that correctly determines whether p halts on a for any procedure p and object a. Use the following reasoning: If you had such a procedure halts?, you could implement the following program: (define (run-forever) (run-forever)) (define (try p) (if (halts? p p) (run-forever) ’halted)) Now consider evaluating the expression (try try) and show that any possible outcome (either halting or running forever) violates the intended behavior of halts?.23 22 The MIT implementation of Scheme includes eval, as well as a symbol user- initial-environment that is bound to the initial environment in which the user’s input expressions are evaluated. 23 Although we stipulated that halts? is given a procedure object, notice that this reasoning still applies even if halts? can gain access to the procedure’s text and its environment. This is Turing’s celebrated Halting Theorem, which gave the first clear example of a non-computable problem, i.e., a well-posed task that cannot be carried out as a computational procedure. 4.1.6 Internal Definitions Our environment model of evaluation and our metacircular evaluator execute definitions in sequence, extending the environment frame one definition at a time. This is particularly convenient for interactive program development, in which the programmer needs to freely mix the application of procedures with the definition of new procedures. However, if we think carefully about the internal definitions used to implement block structure (introduced in Section 1.1.8), we will find that name-by-name extension of the environment may not be the best way to define local variables. Consider a procedure with internal definitions, such as (define (f x) (define (even? n) (if (= n 0) true (odd? (- n 1)))) (define (odd? n) (if (= n 0) false (even? (- n 1)))) 〈rest of body of f〉) Our intention here is that the name odd? in the body of the procedure even? should refer to the procedure odd? that is defined after even?. The scope of the name odd? is the entire body of f, not just the portion of the body of f starting at the point where the define for odd? occurs. Indeed, when we consider that odd? is itself defined in terms of even?—so that even? and odd? are mutually recursive procedures—we see that the only satisfactory interpretation of the two defines is to regard them as if the 589 names even? and odd? were being added to the environment simultaneously. More generally, in block structure, the scope of a local name is the entire procedure body in which the define is evaluated. As it happens, our interpreter will evaluate calls to f correctly, but for an “accidental” reason: Since the definitions of the internal procedures come first, no calls to these procedures will be evaluated until all of them have been defined. Hence, odd? will have been defined by the time even? is executed. In fact, our sequential evaluation mechanism will give the same result as a mechanism that directly implements simultaneous definition for any procedure in which the internal definitions come first in a body and evaluation of the value expressions for the defined variables doesn’t actually use any of the defined variables. (For an example of a procedure that doesn’t obey these restrictions, so that sequential definition isn’t equivalent to simultaneous definition, see Exercise 4.19.)24 There is, however, a simple way to treat definitions so that internally defined names have truly simultaneous scope—just create all local variables that will be in the current environment before evaluating any of the value expressions. One way to do this is by a syntax transformation on lambda expressions. Before evaluating the body of a lambda expression, we “scan 24 Wanting programs to not depend on this evaluation mechanism is the reason for the “management is not responsible” remark in Footnote 28 of Chapter 1. By insisting that internal definitions come first and do not use each other while the definitions are being evaluated, the IEEE standard for Scheme leaves implementors some choice in the mechanism used to evaluate these definitions. The choice of one evaluation rule rather than another here may seem like a small issue, affecting only the interpretation of “badly formed” programs. However, we will see in Section 5.5.6 that moving to a model of simultaneous scoping for internal definitions avoids some nasty difficulties that would otherwise arise in implementing a compiler. 590 out” and eliminate all the internal definitions in the body. The internally defined variables will be created with a let and then set to their values by assignment. For example, the procedure (lambda 〈vars〉 (define u 〈e1〉) (define v 〈e2〉) 〈e3〉) would be transformed into (lambda 〈vars〉 (let ((u ’*unassigned*) (v ’*unassigned*)) (set! u 〈e1〉) (set! v 〈e2〉) 〈e3〉)) where *unassigned* is a special symbol that causes looking up a variable to signal an error if an attempt is made to use the value of the not-yetassigned variable. An alternative strategy for scanning out internal definitions is shown in Exercise 4.18. Unlike the transformation shown above, this enforces the restriction that the defined variables’ values can be evaluated without using any of the variables’ values.25 25 The IEEE standard for Scheme allows for different implementation strategies by specifying that it is up to the programmer to obey this restriction, not up to the implementation to enforce it. Some Scheme implementations, including MIT Scheme, use the transformation shown above. Thus, some programs that don’t obey this restriction will in fact run in such implementations. 591 Exercise 4.16: In this exercise we implement the method just described for interpreting internal definitions. We assume that the evaluator supports let (see Exercise 4.6). a. Change lookup-variable-value (Section 4.1.3) to signal an error if the value it finds is the symbol *unassigned*. b. Write a procedure scan-out-defines that takes a procedure body and returns an equivalent one that has no internal definitions, by making the transformation described above. c. Install scan-out-defines in the interpreter, either in makeprocedure or in procedure-body (see Section 4.1.3). place is better? Why? Exercise 4.17: Draw diagrams of the environment in effect when evaluating the expression 〈e3 〉 in the procedure in the text, comparing how this will be structured when definitions are interpreted sequentially with how it will be structured if definitions are scanned out as described. Why is there an extra frame in the transformed program? Explain why this difference in environment structure can never make a difference in the behavior of a correct program. Design a way to make the interpreter implement the “simultaneous” scope rule for internal definitions without constructing the extra frame. Exercise 4.18: Consider an alternative strategy for scanning out definitions that translates the example in the text to (lambda 〈vars〉 (let ((u ’*unassigned*) (v ’*unassigned*)) 592 (let ((a 〈e1〉) (b 〈e2〉)) (set! u a) (set! v b)) 〈e3〉)) Here a and b are meant to represent new variable names, created by the interpreter, that do not appear in the user’s program. Consider the solve procedure from Section 3.5.4: (define (solve f y0 dt) (define y (integral (delay dy) y0 dt)) (define dy (stream-map f y)) y) Will this procedure work if internal definitions are scanned out as shown in this exercise? What if they are scanned out as shown in the text? Explain. Exercise 4.19: Ben Bitdiddle, Alyssa P. Hacker, and Eva Lu Ator are arguing about the desired result of evaluating the expression (let ((a 1)) (define (f x) (define b (+ a x)) (define a 5) (+ a b)) (f 10)) Ben asserts that the result should be obtained using the sequential rule for define: b is defined to be 11, then a is defined to be 5, so the result is 16. Alyssa objects that mutual recursion requires the simultaneous scope rule for internal procedure definitions, and 593 that it is unreasonable to treat procedure names differently from other names. Thus, she argues for the mechanism implemented in Exercise 4.16. This would lead to a being unassigned at the time that the value for b is to be computed. Hence, in Alyssa’s view the procedure should produce an error. Eva has a third opinion. She says that if the definitions of a and b are truly meant to be simultaneous, then the value 5 for a should be used in evaluating b. Hence, in Eva’s view a should be 5, b should be 15, and the result should be 20. Which (if any) of these viewpoints do you support? Can you devise a way to implement internal definitions so that they behave as Eva prefers?26 Exercise 4.20: Because internal definitions look sequential but are actually simultaneous, some people prefer to avoid them entirely, and use the special form letrec instead. Letrec looks like let, so it is not surprising that the variables it binds are bound simultaneously and have the same scope as each other. The sample procedure f above can be written without internal definitions, but with exactly the same meaning, as (define (f x) (letrec ((even? (lambda (n) 26 The MIT implementors of Scheme support Alyssa on the following grounds: Eva is in principle correct – the definitions should be regarded as simultaneous. But it seems difficult to implement a general, efficient mechanism that does what Eva requires. In the absence of such a mechanism, it is better to generate an error in the difficult cases of simultaneous definitions (Alyssa’s notion) than to produce an incorrect answer (as Ben would have it). 594 (if (= n 0) true (odd? (- n 1))))) (odd? (lambda (n) (if (= n 0) false (even? (- n 1)))))) 〈rest of body of f〉)) Letrec expressions, which have the form (letrec ((〈v ar 1 〉〈exp 1 〉) ... (〈v ar n 〉〈exp n 〉)) 〈body〉) are a variation on let in which the expressions 〈exp k 〉 that provide the initial values for the variables 〈v ar k 〉 are evaluated in an environment that includes all the letrec bindings. This permits recursion in the bindings, such as the mutual recursion of even? and odd? in the example above, or the evaluation of 10 factorial with (letrec ((fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))))))) (fact 10)) a. Implement letrec as a derived expression, by transforming a letrec expression into a let expression as shown in the text above or in Exercise 4.18. That is, the letrec variables should 595 be created with a let and then be assigned their values with set!. b. Louis Reasoner is confused by all this fuss about internal definitions. The way he sees it, if you don’t like to use define inside a procedure, you can just use let. Illustrate what is loose about his reasoning by drawing an environment diagram that shows the environment in which the 〈rest of body of f 〉 is evaluated during evaluation of the expression (f 5), with f defined as in this exercise. Draw an environment diagram for the same evaluation, but with let in place of letrec in the definition of f. Exercise 4.21: Amazingly, Louis’s intuition in Exercise 4.20 is correct. It is indeed possible to specify recursive procedures without using letrec (or even define), although the method for accomplishing this is much more subtle than Louis imagined. The following expression computes 10 factorial by applying a recursive factorial procedure:27 ((lambda (n) ((lambda (fact) (fact fact n)) (lambda (ft k) (if (= k 1) 1 27 This example illustrates a programming trick for formulating recursive procedures without using define. The most general trick of this sort is the Y operator, which can be used to give a “pure λ-calculus” implementation of recursion. (See Stoy 1977 for details on the λ-calculus, and Gabriel 1988 for an exposition of the Y operator in Scheme.) 596 (* k (ft ft (- k 1))))))) 10) a. Check (by evaluating the expression) that this really does compute factorials. Devise an analogous expression for computing Fibonacci numbers. b. Consider the following procedure, which includes mutually recursive internal definitions: (define (f x) (define (even? n) (if (= n 0) true (odd? (- n 1)))) (define (odd? n) (if (= n 0) false (even? (- n 1)))) (even? x)) Fill in the missing expressions to complete an alternative definition of f, which uses neither internal definitions nor letrec: (define (f x) ((lambda (even? odd?) (even? even? odd? x)) (lambda (ev? od? n) (if (= n 0) true (od? 〈??〉〈??〉〈??〉))) (lambda (ev? od? n) (if (= n 0) false (ev? 〈??〉〈??〉〈??〉))))) 4.1.7 Separating Syntactic Analysis from Execution The evaluator implemented above is simple, but it is very inefficient, because the syntactic analysis of expressions is interleaved with their execution. Thus if a program is executed many times, its syntax is analyzed many times. Consider, for example, evaluating (factorial 4) using the following definition of factorial: (define (factorial n) (if (= n 1) 1 (* (factorial (- n 1)) n))) Each time factorial is called, the evaluator must determine that the body is an if expression and extract the predicate. Only then can it evaluate the predicate and dispatch on its value. Each time it evaluates the expression (* (factorial (- n 1)) n), or the subexpressions (factorial (- n 1)) and (- n 1), the evaluator must perform the case analysis in eval to determine that the expression is an application, and must extract its operator and operands. This analysis is expensive. Performing it repeatedly is wasteful. We can transform the evaluator to be significantly more efficient by arranging things so that syntactic analysis is performed only once.28 We split eval, which takes an expression and an environment, into two parts. The procedure analyze takes only the expression. It performs the syntactic analysis and returns a new procedure, the execution procedure, that encap28 This technique is an integral part of the compilation process, which we shall discuss in Chapter 5. Jonathan Rees wrote a Scheme interpreter like this in about 1982 for the T project (Rees and Adams 1982). Marc Feeley (1986) (see also Feeley and Lapalme 1987) independently invented this technique in his master’s thesis. 598 sulates the work to be done in executing the analyzed expression. The execution procedure takes an environment as its argument and completes the evaluation. This saves work because analyze will be called only once on an expression, while the execution procedure may be called many times. With the separation into analysis and execution, eval now becomes (define (eval exp env) ((analyze exp) The result of calling analyze is the execution procedure to be applied to the environment. The analyze procedure is the same case analysis as performed by the original eval of Section 4.1.1, except that the procedures to which we dispatch perform only analysis, not full evaluation: (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((quoted? exp) (analyze-quoted exp)) ((variable? exp) (analyze-variable exp)) ((assignment? exp) (analyze-assignment exp)) ((definition? exp) (analyze-definition exp)) ((if? exp) (analyze-if exp)) ((lambda? exp) (analyze-lambda exp)) ((begin? exp) (analyze-sequence (begin-actions exp))) ((cond? exp) (analyze (cond->if exp))) ((application? exp) (analyze-application exp)) (else (error "Unknown expression type - ANALYZE" 599 Here is the simplest syntactic analysis procedure, which handles selfevaluating expressions. It returns an execution procedure that ignores its environment argument and just returns the expression: (define (analyze-self-evaluating exp) (lambda (env) exp)) For a quoted expression, we can gain a little efficiency by extracting the text of the quotation only once, in the analysis phase, rather than in the execution phase. (define (analyze-quoted exp) (let ((qval (text-of-quotation exp))) (lambda (env) qval))) Looking up a variable value must still be done in the execution phase, since this depends upon knowing the environment.29 (define (analyze-variable exp) (lambda (env) (lookup-variable-value exp env))) Analyze-assignment also must defer actually setting the variable until the execution, when the environment has been supplied. However, the fact that the assignment-value expression can be analyzed (recursively) during analysis is a major gain in efficiency, because the assignment-value expression will now be analyzed only once. The same holds true for definitions. (define (analyze-assignment exp) 29 There is, however, an important part of the variable search that can be done as part of the syntactic analysis. As we will show in Section 5.5.6, one can determine the position in the environment structure where the value of the variable will be found, thus obviating the need to scan the environment for the entry that matches the variable. 600 (let ((var (assignment-variable exp)) (vproc (analyze (assignment-value exp)))) (lambda (env) (set-variable-value! var (vproc env) env) ’ok))) (define (analyze-definition exp) (let ((var (definition-variable exp)) (vproc (analyze (definition-value exp)))) (lambda (env) (define-variable! var (vproc env) env) ’ok))) For if expressions, we extract and analyze the predicate, consequent, and alternative at analysis time. (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (cproc (analyze (if-consequent exp))) (aproc (analyze (if-alternative exp)))) (lambda (env) (if (true? (pproc env)) (cproc env) (aproc env))))) Analyzing a lambda expression also achieves a major gain in efficiency: We analyze the lambda body only once, even though procedures resulting from evaluation of the lambda may be applied many times. (define (analyze-lambda exp) (let ((vars (lambda-parameters exp)) 601 (bproc (analyze-sequence (lambda-body exp)))) (lambda (env) (make-procedure vars bproc env)))) Analysis of a sequence of expressions (as in a begin or the body of a lambda expression) is more involved.30 Each expression in the sequence is analyzed, yielding an execution procedure. These execution procedures are combined to produce an execution procedure that takes an environment as argument and sequentially calls each individual execution procedure with the environment as argument. (define (analyze-sequence exps) (define (sequentially proc1 proc2) (lambda (env) (proc1 env) (proc2 env))) (define (loop first-proc rest-procs) (if (null? rest-procs) first-proc (loop (sequentially first-proc (car rest-procs)) (cdr rest-procs)))) (let ((procs (map analyze exps))) (if (null? procs) (error "Empty sequence - ANALYZE")) (loop (car procs) (cdr procs)))) To analyze an application, we analyze the operator and operands and construct an execution procedure that calls the operator execution procedure (to obtain the actual procedure to be applied) and the operand execution procedures (to obtain the actual arguments). We then pass these to execute-application, which is the analog of apply in Section 4.1.1. 30 See Exercise 4.23 for some insight into the processing of sequences. 602 Execute-application differs from apply in that the procedure body for a compound procedure has already been analyzed, so there is no need to do further analysis. Instead, we just call the execution procedure for the body on the extended environment. (define (analyze-application exp) (let ((fproc (analyze (operator exp))) (aprocs (map analyze (operands exp)))) (lambda (env) (execute-application (fproc env) (map (lambda (aproc) (aproc env)) aprocs))))) (define (execute-application proc args) (cond ((primitive-procedure? proc) (apply-primitive-procedure proc args)) ((compound-procedure? proc) ((procedure-body proc) (extend-environment (procedure-parameters proc) args (procedure-environment proc)))) (else (error "Unknown procedure type - EXECUTE-APPLICATION" proc)))) Our new evaluator uses the same data structures, syntax procedures, and run-time support procedures as in Section 4.1.2, Section 4.1.3, and Section 4.1.4. 603 Exercise 4.22: Extend the evaluator in this section to support the special form let. (See Exercise 4.6.) Exercise 4.23: Alyssa P. Hacker doesn’t understand why analyzesequence needs to be so complicated. All the other analysis pro- cedures are straightforward transformations of the corresponding evaluation procedures (or eval clauses) in Section 4.1.1. She expected analyze-sequence to look like this: (define (analyze-sequence exps) (define (execute-sequence procs env) (cond ((null? (cdr procs)) ((car procs) env)) (else ((car procs) env) (execute-sequence (cdr procs) env)))) (let ((procs (map analyze exps))) (if (null? procs) (error "Empty sequence - ANALYZE")) (lambda (env) (execute-sequence procs env)))) Eva Lu Ator explains to Alyssa that the version in the text does more of the work of evaluating a sequence at analysis time. Alyssa’s sequence-execution procedure, rather than having the calls to the individual execution procedures built in, loops through the procedures in order to call them: In effect, although the individual expressions in the sequence have been analyzed, the sequence itself has not been. Compare the two versions of analyze-sequence. For example, consider the common case (typical of procedure bodies) where the sequence has just one expression. What work will the execution procedure produced by Alyssa’s program do? What about the 604 execution procedure produced by the program in the text above? How do the two versions compare for a sequence with two expressions? Exercise 4.24: Design and carry out some experiments to compare the speed of the original metacircular evaluator with the version in this section. Use your results to estimate the fraction of time that is spent in analysis versus execution for various procedures. 4.2 Variations on a Scheme – Lazy Evaluation Now that we have an evaluator expressed as a Lisp program, we can experiment with alternative choices in language design simply by modifying the evaluator. Indeed, new languages are often invented by first writing an evaluator that embeds the new language within an existing high-level language. For example, if we wish to discuss some aspect of a proposed modification to Lisp with another member of the Lisp community, we can supply an evaluator that embodies the change. The recipient can then experiment with the new evaluator and send back comments as further modifications. Not only does the high-level implementation base make it easier to test and debug the evaluator; in addition, the embedding enables the designer to snarf31 features from the underlying language, just as our embedded Lisp evaluator uses primitives and control structure from the underlying Lisp. Only later (if ever) need the designer go to the trouble of building a complete implementation in a low-level language or in hardware. In this 31 Snarf: “To grab, especially a large document or file for the purpose of using it either with or without the owner’s permission.” Snarf down: “To snarf, sometimes with the connotation of absorbing, processing, or understanding.” (These definitions were snarfed from Steele et al. 1983. See also Raymond 1993.) 605 section and the next we explore some variations on Scheme that provide significant additional expressive power. 4.2.1 Normal Order and Applicative Order In Section 1.1, where we began our discussion of models of evaluation, we noted that Scheme is an applicative-order language, namely, that all the arguments to Scheme procedures are evaluated when the procedure is applied. In contrast, normal-order languages delay evaluation of procedure arguments until the actual argument values are needed. Delaying evaluation of procedure arguments until the last possible moment (e.g., until they are required by a primitive operation) is called lazy evaluation.32 Consider the procedure (define (try a b) (if (= a 0) 1 b)) Evaluating (try 0 (/ 1 0)) generates an error in Scheme. With lazy evaluation, there would be no error. Evaluating the expression would return 1, because the argument (/ 1 0) would never be evaluated. An example that exploits lazy evaluation is the definition of a procedure unless (define (unless condition usual-value exceptional-value) (if condition exceptional-value usual-value)) that can be used in expressions such as 32 The difference between the “lazy” terminology and the “normal-order” terminol- ogy is somewhat fuzzy. Generally, “lazy” refers to the mechanisms of particular evaluators, while “normal-order” refers to the semantics of languages, independent of any particular evaluation strategy. But this is not a hard-and-fast distinction, and the two terminologies are often used interchangeably. 606 (unless (= b 0) (/ a b) (begin (display "exception: returning 0") 0)) This won’t work in an applicative-order language because both the usual value and the exceptional value will be evaluated before unless is called (compare Exercise 1.6). An advantage of lazy evaluation is that some procedures, such as unless, can do useful computation even if evaluation of some of their arguments would produce errors or would not terminate. If the body of a procedure is entered before an argument has been evaluated we say that the procedure is non-strict in that argument. If the argument is evaluated before the body of the procedure is entered we say that the procedure is strict in that argument.33 In a purely applicative-order language, all procedures are strict in each argument. In a purely normalorder language, all compound procedures are non-strict in each argument, and primitive procedures may be either strict or non-strict. There are also languages (see Exercise 4.31) that give programmers detailed control over the strictness of the procedures they define. A striking example of a procedure that can usefully be made non-strict is cons (or, in general, almost any constructor for data structures). One can do useful computation, combining elements to form data structures 33 The “strict” versus “non-strict” terminology means essentially the same thing as “applicative-order” versus “normal-order,” except that it refers to individual procedures and arguments rather than to the language as a whole. At a conference on programming languages you might hear someone say, “The normal-order language Hassle has certain strict primitives. Other procedures take their arguments by lazy evaluation.” 607 and operating on the resulting data structures, even if the values of the elements are not known. It makes perfect sense, for instance, to compute the length of a list without knowing the values of the individual elements in the list. We will exploit this idea in Section 4.2.3 to implement the streams of Chapter 3 as lists formed of non-strict cons pairs. Exercise 4.25: Suppose that (in ordinary applicative-order Scheme) we define unless as shown above and then define factorial in terms of unless as (define (factorial n) (unless (= n 1) (* n (factorial (- n 1))) 1)) What happens if we attempt to evaluate (factorial 5)? Will our definitions work in a normal-order language? Exercise 4.26: Ben Bitdiddle and Alyssa P. Hacker disagree over the importance of lazy evaluation for implementing things such as unless. Ben points out that it’s possible to implement unless in applicative order as a special form. Alyssa counters that, if one did that, unless would be merely syntax, not a procedure that could be used in conjunction with higher-order procedures. Fill in the details on both sides of the argument. Show how to implement unless as a derived expression (like cond or let), and give an example of a situation where it might be useful to have unless available as a procedure, rather than as a special form. 4.2.2 An Interpreter with Lazy Evaluation In this section we will implement a normal-order language that is the same as Scheme except that compound procedures are non-strict in each argument. Primitive procedures will still be strict. It is not difficult to modify the evaluator of Section 4.1.1 so that the language it interprets behaves this way. Almost all the required changes center around procedure application. The basic idea is that, when applying a procedure, the interpreter must determine which arguments are to be evaluated and which are to be delayed. The delayed arguments are not evaluated; instead, they are transformed into objects called thunks.34 The thunk must contain the information required to produce the value of the argument when it is needed, as if it had been evaluated at the time of the application. Thus, the thunk must contain the argument expression and the environment in which the procedure application is being evaluated. The process of evaluating the expression in a thunk is called forcing.35 In general, a thunk will be forced only when its value is needed: when it is passed to a primitive procedure that will use the value of the thunk; when 34 The word thunk was invented by an informal working group that was discussing the implementation of call-by-name in Algol 60. They observed that most of the analysis of (“thinking about”) the expression could be done at compile time; thus, at run time, the expression would already have been “thunk” about (Ingerman et al. 1960). 35 This is analogous to the use of force on the delayed objects that were introduced in Chapter 3 to represent streams. The critical difference between what we are doing here and what we did in Chapter 3 is that we are building delaying and forcing into the evaluator, and thus making this uniform and automatic throughout the language. 609 it is the value of a predicate of a conditional; and when it is the value of an operator that is about to be applied as a procedure. One design choice we have available is whether or not to memoize thunks, as we did with delayed objects in Section 3.5.1. With memoization, the first time a thunk is forced, it stores the value that is computed. Subsequent forcings simply return the stored value without repeating the computation. We’ll make our interpreter memoize, because this is more efficient for many applications. There are tricky considerations here, however.36 Modifying the evaluator The main difference between the lazy evaluator and the one in Section 4.1 is in the handling of procedure applications in eval and apply. The application? clause of eval becomes ((application? exp) (apply (actual-value (operator exp) env) (operands exp) env)) This is almost the same as the application? clause of eval in Section 4.1.1. For lazy evaluation, however, we call apply with the operand expressions, rather than the arguments produced by evaluating them. Since we 36 Lazy evaluation combined with memoization is sometimes referred to as call-by- need argument passing, in contrast to call-by-name argument passing. (Call-byname, introduced in Algol 60, is similar to non-memoized lazy evaluation.) As language designers, we can build our evaluator to memoize, not to memoize, or leave this an option for programmers (Exercise 4.31). As you might expect from Chapter 3, these choices raise issues that become both subtle and confusing in the presence of assignments. (See Exercise 4.27 and Exercise 4.29.) An excellent article by Clinger (1982) attempts to clarify the multiple dimensions of confusion that arise here. 610 will need the environment to construct thunks if the arguments are to be delayed, we must pass this as well. We still evaluate the operator, because apply needs the actual procedure to be applied in order to dispatch on its type (primitive versus compound) and apply it. Whenever we need the actual value of an expression, we use (define (actual-value exp env) (force-it (eval exp env))) instead of just eval, so that if the expression’s value is a thunk, it will be forced. Our new version of apply is also almost the same as the version in Section 4.1.1. The difference is that eval has passed in unevaluated operand expressions: For primitive procedures (which are strict), we evaluate all the arguments before applying the primitive; for compound procedures (which are non-strict) we delay all the arguments before applying the procedure. (define (apply procedure arguments env) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure (list-of-arg-values arguments env))) ; changed ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) (list-of-delayed-args arguments env) ; changed (procedure-environment 611 (else (error "Unknown procedure type - APPLY" procedure)))) The procedures that process the arguments are just like list-of-values from Section 4.1.1, except that list-of-delayed-args delays the arguments instead of evaluating them, and list-of-arg-values uses actualvalue instead of eval: (define (list-of-arg-values exps env) (if (no-operands? exps) ’() (cons (actual-value (first-operand exps) env) (list-of-arg-values (rest-operands exps) env)))) (define (list-of-delayed-args exps env) (if (no-operands? exps) ’() (cons (delay-it (first-operand exps) env) (list-of-delayed-args (rest-operands exps) env)))) The other place we must change the evaluator is in the handling of if, where we must use actual-value instead of eval to get the value of the predicate expression before testing whether it is true or false: (define (eval-if exp env) (if (true? (actual-value (if-predicate exp) env)) (eval (if-consequent exp) env) (eval (if-alternative exp) env))) 612 Finally, we must change the driver-loop procedure (Section 4.1.4) to use actual-value instead of eval, so that if a delayed value is propagated back to the read-eval-print loop, it will be forced before being printed. We also change the prompts to indicate that this is the lazy evaluator: (define input-prompt ";;; L-Eval input:") (define output-prompt ";;; L-Eval value:") (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (actual-value input the-global-environment))) (announce-output output-prompt) (user-print output))) (driver-loop)) With these changes made, we can start the evaluator and test it. The successful evaluation of the try expression discussed in Section 4.2.1 indicates that the interpreter is performing lazy evaluation: (define the-global-environment (setup-environment)) (driver-loop) ;;; L-Eval input: (define (try a b) (if (= a 0) 1 b)) ;;; L-Eval value: ok ;;; L-Eval input: (try 0 (/ 1 0)) ;;; L-Eval value: 1 Representing thunks Our evaluator must arrange to create thunks when procedures are applied to arguments and to force these thunks later. A thunk must package an expression together with the environment, so that the argument can be produced later. To force the thunk, we simply extract the expression and environment from the thunk and evaluate the expression in the environment. We use actual-value rather than eval so that in case the value of the expression is itself a thunk, we will force that, and so on, until we reach something that is not a thunk: (define (force-it obj) (if (thunk? obj) (actual-value (thunk-exp obj) (thunk-env obj)) obj)) One easy way to package an expression with an environment is to make a list containing the expression and the environment. Thus, we create a thunk as follows: (define (delay-it exp env) (list ’thunk exp env)) (define (thunk? obj) (tagged-list? obj ’thunk)) (define (thunk-exp thunk) (cadr thunk)) (define (thunk-env thunk) (caddr thunk)) Actually, what we want for our interpreter is not quite this, but rather thunks that have been memoized. When a thunk is forced, we will turn it into an evaluated thunk by replacing the stored expression with its value and changing the thunk tag so that it can be recognized as already evaluated.37 (define (evaluated-thunk? obj) (tagged-list? obj ’evaluated-thunk)) (define (thunk-value evaluated-thunk) (cadr evaluated-thunk)) (define (force-it obj) (cond ((thunk? obj) (let ((result (actual-value (thunk-exp obj) 37 Notice that we also erase the env from the thunk once the expression’s value has been computed. This makes no difference in the values returned by the interpreter. It does help save space, however, because removing the reference from the thunk to the env once it is no longer needed allows this structure to be garbage-collected and its space recycled, as we will discuss in Section 5.3. Similarly, we could have allowed unneeded environments in the memoized delayed objects of Section 3.5.1 to be garbage-collected, by having memo-proc do something like (set! proc ’()) to discard the procedure proc (which includes the environment in which the delay was evaluated) after storing its value. 615 (thunk-env obj)))) (set-car! obj ’evaluated-thunk) (set-car! (cdr obj) result) ; replace exp with its value (set-cdr! (cdr obj) ’()) ; forget unneeded env result)) ((evaluated-thunk? obj) (thunk-value obj)) (else obj))) Notice that the same delay-it procedure works both with and without memoization. Exercise 4.27: Suppose we type in the following definitions to the lazy evaluator: (define count 0) (define (id x) (set! count (+ count 1)) x) Give the missing values in the following sequence of interactions, and explain your answers.38 (define w (id (id 10))) ;;; L-Eval input: 38 This exercise demonstrates that the interaction between lazy evaluation and side effects can be very confusing. This is just what you might expect from the discussion in Chapter 3. 616 count ;;; L-Eval value: 〈response〉 ;;; L-Eval input: w ;;; L-Eval value: 〈response〉 ;;; L-Eval input: count ;;; L-Eval value: 〈response〉 Exercise 4.28: Eval uses actual-value rather than eval to evaluate the operator before passing it to apply, in order to force the value of the operator. Give an example that demonstrates the need for this forcing. Exercise 4.29: Exhibit a program that you would expect to run much more slowly without memoization than with memoization. Also, consider the following interaction, where the id procedure is defined as in Exercise 4.27 and count starts at 0: (define (square x) (* x x)) ;;; L-Eval input: (square (id 10)) ;;; L-Eval value: 〈response〉 ;;; L-Eval input: count ;;; L-Eval value: 〈response〉 Give the responses both when the evaluator memoizes and when it does not. Exercise 4.30: Cy D. Fect, a reformed C programmer, is worried that some side effects may never take place, because the lazy evaluator doesn’t force the expressions in a sequence. Since the value of an expression in a sequence other than the last one is not used (the expression is there only for its effect, such as assigning to a variable or printing), there can be no subsequent use of this value (e.g., as an argument to a primitive procedure) that will cause it to be forced. Cy thus thinks that when evaluating sequences, we must force all expressions in the sequence except the final one. He proposes to modify eval-sequence from Section 4.1.1 to use actual-value rather than eval: (define (eval-sequence exps env) (cond ((last-exp? exps) (eval (first-exp exps) env)) (else (actual-value (first-exp exps) env) (eval-sequence (rest-exps exps) env)))) a. Ben Bitdiddle thinks Cy is wrong. He shows Cy the for-each procedure described in Exercise 2.23, which gives an important example of a sequence with side effects: (define (for-each proc items) 618 (if (null? items) ’done (begin (proc (car items)) (for-each proc (cdr items))))) He claims that the evaluator in the text (with the original evalsequence) handles this correctly: ;;; L-Eval input: (for-each (lambda (x) (newline) (display x)) (list 57 321 88)) 57 321 88 ;;; L-Eval value: done Explain why Ben is right about the behavior of for-each. b. Cy agrees that Ben is right about the for-each example, but says that that’s not the kind of program he was thinking about when he proposed his change to eval-sequence. He defines the following two procedures in the lazy evaluator: (define (p1 x) (set! x (cons x ’(2))) x) (define (p2 x) (define (p e) e x) 619 (p (set! x (cons x ’(2))))) What are the values of (p1 1) and (p2 1) with the original eval-sequence? What would the values be with Cy’s proposed change to eval-sequence? c. Cy also points out that changing eval-sequence as he proposes does not affect the behavior of the example in part a. Explain why this is true. d. How do you think sequences ought to be treated in the lazy evaluator? Do you like Cy’s approach, the approach in the text, or some other approach? Exercise 4.31: The approach taken in this section is somewhat unpleasant, because it makes an incompatible change to Scheme. It might be nicer to implement lazy evaluation as an upwardcompatible extension, that is, so that ordinary Scheme programs will work as before. We can do this by extending the syntax of procedure declarations to let the user control whether or not arguments are to be delayed. While we’re at it, we may as well also give the user the choice between delaying with and without memoization. For example, the definition (define (f a (b lazy) c (d lazy-memo)) ...) would define f to be a procedure of four arguments, where the first and third arguments are evaluated when the procedure is called, the second argument is delayed, and the fourth argument is both delayed and memoized. Thus, ordinary procedure definitions will produce the same behavior as ordinary Scheme, while adding the 620 lazy-memo declaration to each parameter of every compound pro- cedure will produce the behavior of the lazy evaluator defined in this section. Design and implement the changes required to produce such an extension to Scheme. You will have to implement new syntax procedures to handle the new syntax for define. You must also arrange for eval or apply to determine when arguments are to be delayed, and to force or delay arguments accordingly, and you must arrange for forcing to memoize or not, as appropriate. 4.2.3 Streams as Lazy Lists In Section 3.5.1, we showed how to implement streams as delayed lists. We introduced special forms delay and cons-stream, which allowed us to construct a “promise” to compute the cdr of a stream, without actually fulfilling that promise until later. We could use this general technique of introducing special forms whenever we need more control over the evaluation process, but this is awkward. For one thing, a special form is not a first-class object like a procedure, so we cannot use it together with higherorder procedures.39 Additionally, we were forced to create streams as a new kind of data object similar but not identical to lists, and this required us to reimplement many ordinary list operations (map, append, and so on) for use with streams. With lazy evaluation, streams and lists can be identical, so there is no need for special forms or for separate list and stream operations. All we need to do is to arrange matters so that cons is non-strict. One way to accomplish this is to extend the lazy evaluator to allow for non-strict primitives, and to implement cons as one of these. An easier way is to recall 39 This is precisely the issue with the unless procedure, as in Exercise 4.26. 621 (Section 2.1.3) that there is no fundamental need to implement cons as a primitive at all. Instead, we can represent pairs as procedures:40 (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) (define (cdr z) (z (lambda (p q) q))) In terms of these basic operations, the standard definitions of the list operations will work with infinite lists (streams) as well as finite ones, and the stream operations can be implemented as list operations. Here are some examples: (define (list-ref items n) (if (= n 0) (car items) (list-ref (cdr items) (- n 1)))) (define (map proc items) (if (null? items) 40 This is the procedural representation described in Exercise 2.4. Essentially any pro- cedural representation (e.g., a message-passing implementation) would do as well. Notice that we can install these definitions in the lazy evaluator simply by typing them at the driver loop. If we had originally included cons, car, and cdr as primitives in the global environment, they will be redefined. (Also see Exercise 4.33 and Exercise 4.34.) 622 ’() (cons (proc (car items)) (map proc (cdr items))))) (define (scale-list items factor) (map (lambda (x) (* x factor)) items)) (define (add-lists list1 list2) (cond ((null? list1) list2) ((null? list2) list1) (else (cons (+ (car list1) (car list2)) (add-lists (cdr list1) (cdr list2)))))) (define ones (cons 1 ones)) (define integers (cons 1 (add-lists ones integers))) ;;; L-Eval input: (list-ref integers 17) ;;; L-Eval value: 18 Note that these lazy lists are even lazier than the streams of Chapter 3: The car of the list, as well as the cdr, is delayed.41 In fact, even accessing the car or cdr of a lazy pair need not force the value of a list element. 41 This permits us to create delayed versions of more general kinds of list structures, not just sequences. Hughes 1990 discusses some applications of “lazy trees.” 623 The value will be forced only when it is really needed – e.g., for use as the argument of a primitive, or to be printed as an answer. Lazy pairs also help with the problem that arose with streams in Section 3.5.4, where we found that formulating stream models of systems with loops may require us to sprinkle our programs with explicit delay operations, beyond the ones supplied by cons-stream. With lazy evaluation, all arguments to procedures are delayed uniformly. For instance, we can implement procedures to integrate lists and solve differential equations as we originally intended in Section 3.5.4: (define (integral integrand initial-value dt) (define int (cons initial-value (add-lists (scale-list integrand dt) int))) int) (define (solve f y0 dt) (define y (integral dy y0 dt)) (define dy (map f y)) y) ;;; L-Eval input: (list-ref (solve (lambda (x) x) 1 0.001) 1000) ;;; L-Eval value: 2.716924 624 Exercise 4.32: Give some examples that illustrate the difference between the streams of Chapter 3 and the “lazier” lazy lists described in this section. How can you take advantage of this extra laziness? Exercise 4.33: Ben Bitdiddle tests the lazy list implementation given above by evaluating the expression (car ’(a b c)) To his surprise, this produces an error. After some thought, he realizes that the “lists” obtained by reading in quoted expressions are different from the lists manipulated by the new definitions of cons, car, and cdr. Modify the evaluator’s treatment of quoted expressions so that quoted lists typed at the driver loop will produce true lazy lists. Exercise 4.34: Modify the driver loop for the evaluator so that lazy pairs and lists will print in some reasonable way. (What are you going to do about infinite lists?) You may also need to modify the representation of lazy pairs so that the evaluator can identify them in order to print them. 4.3 Variations on a Scheme – Nondeterministic Computing In this section, we extend the Scheme evaluator to support a programming paradigm called nondeterministic computing by building into the evaluator a facility to support automatic search. This is a much more profound change to the language than the introduction of lazy evaluation in Section 4.2. Nondeterministic computing, like stream processing, is useful for “generate and test” applications. Consider the task of starting with two lists of 625 positive integers and finding a pair of integers—one from the first list and one from the second list—whose sum is prime. We saw how to handle this with finite sequence operations in Section 2.2.3 and with infinite streams in Section 3.5.3. Our approach was to generate the sequence of all possible pairs and filter these to select the pairs whose sum is prime. Whether we actually generate the entire sequence of pairs first as in Chapter 2, or interleave the generating and filtering as in Chapter 3, is immaterial to the essential image of how the computation is organized. The nondeterministic approach evokes a different image. Imagine simply that we choose (in some way) a number from the first list and a number from the second list and require (using some mechanism) that their sum be prime. This is expressed by following procedure: (define (prime-sum-pair list1 list2) (let ((a (an-element-of list1)) (b (an-element-of list2))) (require (prime? (+ a b))) (list a b))) It might seem as if this procedure merely restates the problem, rather than specifying a way to solve it. Nevertheless, this is a legitimate nondeterministic program.42 42 We assume that we have previously defined a procedure prime? that tests whether numbers are prime. Even with prime? defined, the prime-sum-pair procedure may look suspiciously like the unhelpful “pseudo-Lisp” attempt to define the square-root function, which we described at the beginning of Section 1.1.7. In fact, a square-root procedure along those lines can actually be formulated as a nondeterministic program. By incorporating a search mechanism into the evaluator, we are eroding the distinction between purely declarative descriptions 626 The key idea here is that expressions in a nondeterministic language can have more than one possible value. For instance, an-element-of might return any element of the given list. Our nondeterministic program evaluator will work by automatically choosing a possible value and keeping track of the choice. If a subsequent requirement is not met, the evaluator will try a different choice, and it will keep trying new choices until the evaluation succeeds, or until we run out of choices. Just as the lazy evaluator freed the programmer from the details of how values are delayed and forced, the nondeterministic program evaluator will free the programmer from the details of how choices are made. It is instructive to contrast the different images of time evoked by nondeterministic evaluation and stream processing. Stream processing uses lazy evaluation to decouple the time when the stream of possible answers is assembled from the time when the actual stream elements are produced. The evaluator supports the illusion that all the possible answers are laid out before us in a timeless sequence. With nondeterministic evaluation, an expression represents the exploration of a set of possible worlds, each determined by a set of choices. Some of the possible worlds lead to dead ends, while others have useful values. The nondeterministic program evaluator supports the illusion that time branches, and that our programs have different possible execution histories. When we reach a dead end, we can revisit a previous choice point and proceed along a different branch. The nondeterministic program evaluator implemented below is called the amb evaluator because it is based on a new special form called amb. We can type the above definition of prime-sum-pair at the amb evaluator and imperative specifications of how to compute answers. We’ll go even farther in this direction in Section 4.4. 627 driver loop (along with definitions of prime?, an-element-of, and require) and run the procedure as follows: ;;; Amb-Eval input: (prime-sum-pair ’(1 3 5 8) ’(20 35 110)) ;;; Starting a new problem ;;; Amb-Eval value: (3 20) The value returned was obtained after the evaluator repeatedly chose elements from each of the lists, until a successful choice was made. Section 4.3.1 introduces amb and explains how it supports nondeterminism through the evaluator’s automatic search mechanism. Section 4.3.2 presents examples of nondeterministic programs, and Section 4.3.3 gives the details of how to implement the amb evaluator by modifying the ordinary Scheme evaluator. 4.3.1 Amb and Search To extend Scheme to support nondeterminism, we introduce a new special form called amb.43 The expression (amb 〈e 1 〉〈e 2 〉 ... 〈e n 〉) returns the value of one of the n expressions 〈e i 〉 “ambiguously.” For example, the expression (list (amb 1 2 3) (amb ’a ’b)) can have six possible values: (1 a) (1 b) (2 a) (2 b) (3 a) (3 b) 43 The idea of amb for nondeterministic programming was first described in 1961 by John McCarthy (see McCarthy 1967). 628 Amb with a single choice produces an ordinary (single) value. Amb with no choices—the expression (amb)—is an expression with no ac- ceptable values. Operationally, we can think of (amb) as an expression that when evaluated causes the computation to “fail”: The computation aborts and no value is produced. Using this idea, we can express the requirement that a particular predicate expression p must be true as follows: (define (require p) (if (not p) (amb))) With amb and require, we can implement the an-element-of procedure used above: (define (an-element-of items) (require (not (null? items))) (amb (car items) (an-element-of (cdr items)))) An-element-of fails if the list is empty. Otherwise it ambiguously returns either the first element of the list or an element chosen from the rest of the list. We can also express infinite ranges of choices. The following procedure potentially returns any integer greater than or equal to some given n : (define (an-integer-starting-from n) (amb n (an-integer-starting-from (+ n 1)))) This is like the stream procedure integers-starting-from described in Section 3.5.2, but with an important difference: The stream procedure returns an object that represents the sequence of all integers beginning with n , whereas the amb procedure returns a single integer.44 44 In actuality, the distinction between nondeterministically returning a single choice and returning all choices depends somewhat on our point of view. From the per- 629 Abstractly, we can imagine that evaluating an amb expression causes time to split into branches, where the computation continues on each branch with one of the possible values of the expression. We say that amb represents a nondeterministic choice point. If we had a machine with a sufficient number of processors that could be dynamically allocated, we could implement the search in a straightforward way. Execution would proceed as in a sequential machine, until an amb expression is encountered. At this point, more processors would be allocated and initialized to continue all of the parallel executions implied by the choice. Each processor would proceed sequentially as if it were the only choice, until it either terminates by encountering a failure, or it further subdivides, or it finishes.45 On the other hand, if we have a machine that can execute only one process (or a few concurrent processes), we must consider the alternatives sequentially. One could imagine modifying an evaluator to pick at random a branch to follow whenever it encounters a choice point. Random choice, however, can easily lead to failing values. We might try running the evaluator over and over, making random choices and hoping to find a non-failing spective of the code that uses the value, the nondeterministic choice returns a single value. From the perspective of the programmer designing the code, the nondeterministic choice potentially returns all possible values, and the computation branches so that each value is investigated separately. 45 One might object that this is a hopelessly inefficient mechanism. It might require millions of processors to solve some easily stated problem this way, and most of the time most of those processors would be idle. This objection should be taken in the context of history. Memory used to be considered just such an expensive commodity. In 1964 a megabyte of RAM cost about $400,000. Now every personal computer has many megabytes of RAM, and most of the time most of that RAM is unused. It is hard to underestimate the cost of mass-produced electronics. 630 value, but it is better to systematically search all possible execution paths. The amb evaluator that we will develop and work with in this section implements a systematic search as follows: When the evaluator encounters an application of amb, it initially selects the first alternative. This selection may itself lead to a further choice. The evaluator will always initially choose the first alternative at each choice point. If a choice results in a failure, then the evaluator automagically46 backtracks to the most recent choice point and tries the next alternative. If it runs out of alternatives at any choice point, the evaluator will back up to the previous choice point and resume from there. This process leads to a search strategy known as depth-first search or chronological backtracking.47 46 Automagically: “Automatically, but in a way which, for some reason (typically be- cause it is too complicated, or too ugly, or perhaps even too trivial), the speaker doesn’t feel like explaining.” (Steele et al. 1983, Raymond 1993) 47 The integration of automatic search strategies into programming languages has had a long and checkered history. The first suggestions that nondeterministic algorithms might be elegantly encoded in a programming language with search and automatic backtracking came from Robert Floyd (1967). Carl Hewitt (1969) invented a programming language called Planner that explicitly supported automatic chronological backtracking, providing for a built-in depth-first search strategy. Sussman et al. (1971) implemented a subset of this language, called MicroPlanner, which was used to support work in problem solving and robot planning. Similar ideas, arising from logic and theorem proving, led to the genesis in Edinburgh and Marseille of the elegant language Prolog (which we will discuss in Section 4.4). After sufficient frustration with automatic search, McDermott and Sussman (1972) developed a language called Conniver, which included mechanisms for placing the search strategy under programmer control. This proved unwieldy, however, and Sussman and Stallman 1975 found a more tractable approach while investigating methods of symbolic analysis for electrical circuits. They developed a non-chronological back- Driver loop The driver loop for the amb evaluator has some unusual properties. It reads an expression and prints the value of the first non-failing execution, as in the prime-sum-pair example shown above. If we want to see the value of the next successful execution, we can ask the interpreter to backtrack and attempt to generate a second non-failing execution. This is signaled by typing the symbol try-again. If any expression except try-again is given, the interpreter will start a new problem, discarding the unexplored alternatives in the previous problem. Here is a sample interaction: ;;; Amb-Eval input: (prime-sum-pair ’(1 3 5 8) ’(20 35 110)) ;;; Starting a new problem ;;; Amb-Eval value: (3 20) tracking scheme that was based on tracing out the logical dependencies connecting facts, a technique that has come to be known as dependency-directed backtracking. Although their method was complex, it produced reasonably efficient programs because it did little redundant search. Doyle (1979) and McAllester (1978; 1980) generalized and clarified the methods of Stallman and Sussman, developing a new paradigm for formulating search that is now called truth maintenance. Modern problem-solving systems all use some form of truth-maintenance system as a substrate. See Forbus and deKleer 1993 for a discussion of elegant ways to build truth-maintenance systems and applications using truth maintenance. Zabih et al. 1987 describes a nondeterministic extension to Scheme that is based on amb; it is similar to the interpreter described in this section, but more sophisticated, because it uses dependency-directed backtracking rather than chronological backtracking. Winston 1992 gives an introduction to both kinds of backtracking. 632 ;;; Amb-Eval input: try-again ;;; Amb-Eval value: (3 110) ;;; Amb-Eval input: try-again ;;; Amb-Eval value: (8 35) ;;; Amb-Eval input: try-again ;;; There are no more values of (prime-sum-pair (quote (1 3 5 8)) (quote (20 35 110))) ;;; Amb-Eval input: (prime-sum-pair ’(19 27 30) ’(11 36 58)) ;;; Starting a new problem ;;; Amb-Eval value: (30 11) Exercise 4.35: Write a procedure an-integer-between that returns an integer between two given bounds. This can be used to implement a procedure that finds Pythagorean triples, i.e., triples of integers (i , j , k ) between the given bounds such that i ≤ j i 2 + j 2 = k 2 , as follows: (define (a-pythagorean-triple-between low high) (let ((i (an-integer-between low high))) 633 (let ((j (an-integer-between i high))) (let ((k (an-integer-between j high))) (require (= (+ (* i i) (* j j)) (* k k))) (list i j k))))) Exercise 4.36: Exercise 3.69 discussed how to generate the stream of all Pythagorean triples, with no upper bound on the size of the integers to be searched. Explain why simply replacing an-integer-between by an-integer-starting-from in the procedure in Exercise 4.35 is not an adequate way to generate arbitrary Pythagorean triples. Write a procedure that actually will accomplish this. (That is, write a procedure for which repeatedly typing try-again would in principle eventually generate all Pythagorean triples.) Exercise 4.37: Ben Bitdiddle claims that the following method for generating Pythagorean triples is more efficient than the one in Exercise 4.35. Is he correct? (Hint: Consider the number of possibilities that must be explored.) (define (a-pythagorean-triple-between low high) (let ((i (an-integer-between low high)) (hsq (* high high))) (let ((j (an-integer-between i high))) (let ((ksq (+ (* i i) (* j j)))) (require (>= hsq ksq)) (let ((k (sqrt ksq))) (require (integer? k)) (list i j k)))))) 4.3.2 Examples of Nondeterministic Programs Section 4.3.3 describes the implementation of the amb evaluator. First, however, we give some examples of how it can be used. The advantage of nondeterministic programming is that we can suppress the details of how search is carried out, thereby expressing our programs at a higher level of abstraction. Logic Puzzles The following puzzle (taken from Dinesman 1968) is typical of a large class of simple logic puzzles: Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. Baker does not live on the top floor. Cooper does not live on the bottom floor. Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper. Smith does not live on a floor adjacent to Fletcher’s. Fletcher does not live on a floor adjacent to Cooper’s. Where does everyone live? We can determine who lives on each floor in a straightforward way by enumerating all the possibilities and imposing the given restrictions:48 48 Our program uses the following procedure to determine if the elements of a list are distinct: (define (distinct? items) (cond ((null? items) true) ((null? (cdr items)) true) ((member (car items) (cdr items)) false) (else (distinct? (cdr items))))) Member is like memq except that it uses equal? instead of eq? to test for equality. 635 (define (multiple-dwelling) (let ((baker (amb 1 2 3 4 5)) (cooper (amb 1 2 3 4 5)) (fletcher (amb 1 2 3 4 5)) (miller (amb 1 2 3 4 5)) (smith (amb 1 2 3 4 5))) (require (distinct? (list baker cooper fletcher miller smith))) (require (not (= baker 5))) (require (not (= cooper 1))) (require (not (= fletcher 5))) (require (not (= fletcher 1))) (require (> miller cooper)) (require (not (= (abs (- smith fletcher)) 1))) (require (not (= (abs (- fletcher cooper)) 1))) (list (list ’baker baker) (list ’cooper cooper) (list ’fletcher fletcher) (list ’miller miller) (list ’smith smith)))) Evaluating the expression (multiple-dwelling) produces the result ((baker 3) (cooper 2) (fletcher 4) (miller 5) (smith 1)) Although this simple procedure works, it is very slow. Exercise 4.39 and Exercise 4.40 discuss some possible improvements. Exercise 4.38: Modify the multiple-dwelling procedure to omit the requirement that Smith and Fletcher do not live on adjacent floors. How many solutions are there to this modified puzzle? 636 Exercise 4.39: Does the order of the restrictions in the multipledwelling procedure affect the answer? Does it affect the time to find an answer? If you think it matters, demonstrate a faster program obtained from the given one by reordering the restrictions. If you think it does not matter, argue your case. Exercise 4.40: In the multiple dwelling problem, how many sets of assignments are there of people to floors, both before and after the requirement that floor assignments be distinct? It is very inefficient to generate all possible assignments of people to floors and then leave it to backtracking to eliminate them. For example, most of the restrictions depend on only one or two of the personfloor variables, and can thus be imposed before floors have been selected for all the people. Write and demonstrate a much more efficient nondeterministic procedure that solves this problem based upon generating only those possibilities that are not already ruled out by previous restrictions. (Hint: This will require a nest of let expressions.) Exercise 4.41: Write an ordinary Scheme program to solve the multiple dwelling puzzle. Exercise 4.42: Solve the following “Liars” puzzle (from Phillips 1934): Five schoolgirls sat for an examination. Their parents—so they thought—showed an undue degree of interest in the result. They therefore agreed that, in writing home about the examination, each girl should make one true statement and one untrue one. The following are the relevant passages from their letters: • Betty: “Kitty was second in the examination. I was only third.” • Ethel: “You’ll be glad to hear that I was on top. Joan was second.” • Joan: “I was third, and poor old Ethel was bottom.” • Kitty: “I came out second. Mary was only fourth.” • Mary: “I was fourth. Top place was taken by Betty.” What in fact was the order in which the five girls were placed? Exercise 4.43: Use the amb evaluator to solve the following puzzle:49 Mary Ann Moore’s father has a yacht and so has each of his four friends: Colonel Downing, Mr. Hall, Sir Barnacle Hood, and Dr. Parker. Each of the five also has one daughter and each has named his yacht after a daughter of one of the others. Sir Barnacle’s yacht is the Gabrielle, Mr. Moore owns the Lorna; Mr. Hall the Rosalind. The Melissa, owned by Colonel Downing, is named after Sir Barnacle’s daughter. Gabrielle’s father owns the yacht that is named after Dr. Parker’s daughter. Who is Lorna’s father? Try to write the program so that it runs efficiently (see Exercise 4.40). Also determine how many solutions there are if we are not told that Mary Ann’s last name is Moore. Exercise 4.44: Exercise 2.42 described the “eight-queens puzzle” of placing queens on a chessboard so that no two attack each other. Write a nondeterministic program to solve this puzzle. 49 This is taken from a booklet called “Problematical Recreations,” published in the 1960s by Litton Industries, where it is attributed to the Kansas State Engineer. Parsing natural language Programs designed to accept natural language as input usually start by attempting to parse the input, that is, to match the input against some grammatical structure. For example, we might try to recognize simple sentences consisting of an article followed by a noun followed by a verb, such as “The cat eats.” To accomplish such an analysis, we must be able to identify the parts of speech of individual words. We could start with some lists that classify various words:50 (define nouns ’(noun student professor cat class)) (define verbs ’(verb studies lectures eats sleeps)) (define articles ’(article the a)) We also need a grammar, that is, a set of rules describing how grammatical elements are composed from simpler elements. A very simple grammar might stipulate that a sentence always consists of two pieces—a noun phrase followed by a verb—and that a noun phrase consists of an article followed by a noun. With this grammar, the sentence “The cat eats” is parsed as follows: (sentence (noun-phrase (article the) (noun cat)) (verb eats)) We can generate such a parse with a simple program that has separate procedures for each of the grammatical rules. To parse a sentence, we iden50 Here we use the convention that the first element of each list designates the part of speech for the rest of the words in the list. 639 tify its two constituent pieces and return a list of these two elements, tagged with the symbol sentence: (define (parse-sentence) (list ’sentence (parse-noun-phrase) (parse-word verbs))) A noun phrase, similarly, is parsed by finding an article followed by a noun: (define (parse-noun-phrase) (list ’noun-phrase (parse-word articles) (parse-word nouns))) At the lowest level, parsing boils down to repeatedly checking that the next unparsed word is a member of the list of words for the required part of speech. To implement this, we maintain a global variable *unparsed*, which is the input that has not yet been parsed. Each time we check a word, we require that *unparsed* must be non-empty and that it should begin with a word from the designated list. If so, we remove that word from *unparsed* and return the word together with its part of speech (which is found at the head of the list):51 (define (parse-word word-list) (require (not (null? *unparsed*))) (require (memq (car *unparsed*) (cdr word-list))) (let ((found-word (car *unparsed*))) 51 Notice that parse-word uses set! to modify the unparsed input list. For this to work, our amb evaluator must undo the effects of set! operations when it backtracks. 640 (set! *unparsed* (cdr *unparsed*)) (list (car word-list) found-word))) To start the parsing, all we need to do is set *unparsed* to be the entire input, try to parse a sentence, and check that nothing is left over: (define *unparsed* ’()) (define (parse input) (set! *unparsed* input) (let ((sent (parse-sentence))) (require (null? *unparsed*)) sent)) We can now try the parser and verify that it works for our simple test sentence: ;;; Amb-Eval input: (parse ’(the cat eats)) ;;; Starting a new problem ;;; Amb-Eval value: (sentence (noun-phrase (article the) (noun cat)) (verb eats)) The amb evaluator is useful here because it is convenient to express the parsing constraints with the aid of require. Automatic search and backtracking really pay off, however, when we consider more complex grammars where there are choices for how the units can be decomposed. Let’s add to our grammar a list of prepositions: (define prepositions ’(prep for to in by with)) and define a prepositional phrase (e.g., “for the cat”) to be a preposition followed by a noun phrase: 641 (define (parse-prepositional-phrase) (list ’prep-phrase (parse-word prepositions) (parse-noun-phrase))) Now we can define a sentence to be a noun phrase followed by a verb phrase, where a verb phrase can be either a verb or a verb phrase extended by a prepositional phrase:52 (define (parse-sentence) (list ’sentence (parse-noun-phrase) (parse-verb-phrase))) (define (parse-verb-phrase) (define (maybe-extend verb-phrase) (amb verb-phrase (maybe-extend (list ’verb-phrase verb-phrase (parse-prepositional-phrase))))) (maybe-extend (parse-word verbs))) While we’re at it, we can also elaborate the definition of noun phrases to permit such things as “a cat in the class.” What we used to call a noun phrase, we’ll now call a simple noun phrase, and a noun phrase will now be either a simple noun phrase or a noun phrase extended by a prepositional phrase: 52 Observe that this definition is recursive—a verb may be followed by any number of prepositional phrases. 642 (define (parse-simple-noun-phrase) (list ’simple-noun-phrase (parse-word articles) (parse-word nouns))) (define (parse-noun-phrase) (define (maybe-extend noun-phrase) (amb noun-phrase (maybe-extend (list ’noun-phrase noun-phrase (parse-prepositional-phrase))))) (maybe-extend (parse-simple-noun-phrase))) Our new grammar lets us parse more complex sentences. For example (parse ’(the student with the cat sleeps in the class)) produces (sentence (noun-phrase (simple-noun-phrase (article the) (noun student)) (prep-phrase (prep with) (simple-noun-phrase (article the) (noun cat)))) (verb-phrase (verb sleeps) (prep-phrase (prep in) (simple-noun-phrase (article the) (noun class))))) 643 Observe that a given input may have more than one legal parse. In the sentence “The professor lectures to the student with the cat,” it may be that the professor is lecturing with the cat, or that the student has the cat. Our nondeterministic program finds both possibilities: (parse ’(the professor lectures to the student with the cat)) produces (sentence (simple-noun-phrase (article the) (noun professor)) (verb-phrase (verb-phrase (verb lectures) (prep-phrase (prep to) (simple-noun-phrase (article the) (noun student)))) (prep-phrase (prep with) (simple-noun-phrase (article the) (noun cat))))) Asking the evaluator to try again yields (sentence (simple-noun-phrase (article the) (noun professor)) (verb-phrase (verb lectures) (prep-phrase (prep to) (noun-phrase (simple-noun-phrase (article the) (noun student)) (prep-phrase (prep with) 644 (simple-noun-phrase (article the) (noun cat))))))) Exercise 4.45: With the grammar given above, the following sentence can be parsed in five different ways: “The professor lectures to the student in the class with the cat.” Give the five parses and explain the differences in shades of meaning among them. Exercise 4.46: The evaluators in Section 4.1 and Section 4.2 do not determine what order operands are evaluated in. We will see that the amb evaluator evaluates them from left to right. Explain why our parsing program wouldn’t work if the operands were evaluated in some other order. Exercise 4.47: Louis Reasoner suggests that, since a verb phrase is either a verb or a verb phrase followed by a prepositional phrase, it would be much more straightforward to define the procedure parse-verb-phrase as follows (and similarly for noun phrases): (define (parse-verb-phrase) (amb (parse-word verbs) (list ’verb-phrase (parse-verb-phrase) (parse-prepositional-phrase)))) Does this work? Does the program’s behavior change if we interchange the order of expressions in the amb Exercise 4.48: Extend the grammar given above to handle more complex sentences. For example, you could extend noun phrases 645 and verb phrases to include adjectives and adverbs, or you could handle compound sentences.53 Exercise 4.49: Alyssa P. Hacker is more interested in generating interesting sentences than in parsing them. She reasons that by simply changing the procedure parse-word so that it ignores the “input sentence” and instead always succeeds and generates an appropriate word, we can use the programs we had built for parsing to do generation instead. Implement Alyssa’s idea, and show the first half-dozen or so sentences generated.54 4.3.3 Implementing the Amb Evaluator The evaluation of an ordinary Scheme expression may return a value, may never terminate, or may signal an error. In nondeterministic Scheme the evaluation of an expression may in addition result in the discovery of a dead end, in which case evaluation must backtrack to a previous choice 53 This kind of grammar can become arbitrarily complex, but it is only a toy as far as real language understanding is concerned. Real natural-language understanding by computer requires an elaborate mixture of syntactic analysis and interpretation of meaning. On the other hand, even toy parsers can be useful in supporting flexible command languages for programs such as information-retrieval systems. Winston 1992 discusses computational approaches to real language understanding and also the applications of simple grammars to command languages. 54 Although Alyssa’s idea works just fine (and is surprisingly simple), the sentences that it generates are a bit boring—they don’t sample the possible sentences of this language in a very interesting way. In fact, the grammar is highly recursive in many places, and Alyssa’s technique “falls into” one of these recursions and gets stuck. See Exercise 4.50 for a way to deal with this. 646 point. The interpretation of nondeterministic Scheme is complicated by this extra case. We will construct the amb evaluator for nondeterministic Scheme by modifying the analyzing evaluator of Section 4.1.7.55 As in the analyzing evaluator, evaluation of an expression is accomplished by calling an execution procedure produced by analysis of that expression. The difference between the interpretation of ordinary Scheme and the interpretation of nondeterministic Scheme will be entirely in the execution procedures. Execution procedures and continuations Recall that the execution procedures for the ordinary evaluator take one argument: the environment of execution. In contrast, the execution procedures in the amb evaluator take three arguments: the environment, and two procedures called continuation procedures. The evaluation of an expression will finish by calling one of these two continuations: If the evaluation results in a value, the success continuation is called with that value; if the evaluation results in the discovery of a dead end, the failure continuation is called. Constructing and calling appropriate continuations is the mechanism by which the nondeterministic evaluator implements backtracking. It is the job of the success continuation to receive a value and proceed with the computation. Along with that value, the success continuation is 55 We chose to implement the lazy evaluator in Section 4.2 as a modification of the ordinary metacircular evaluator of Section 4.1.1. In contrast, we will base the amb evaluator on the analyzing evaluator of Section 4.1.7, because the execution procedures in that evaluator provide a convenient framework for implementing backtracking. 647 passed another failure continuation, which is to be called subsequently if the use of that value leads to a dead end. It is the job of the failure continuation to try another branch of the nondeterministic process. The essence of the nondeterministic language is in the fact that expressions may represent choices among alternatives. The evaluation of such an expression must proceed with one of the indicated alternative choices, even though it is not known in advance which choices will lead to acceptable results. To deal with this, the evaluator picks one of the alternatives and passes this value to the success continuation. Together with this value, the evaluator constructs and passes along a failure continuation that can be called later to choose a different alternative. A failure is triggered during evaluation (that is, a failure continuation is called) when a user program explicitly rejects the current line of attack (for example, a call to require may result in execution of (amb), an expression that always fails—see Section 4.3.1). The failure continuation in hand at that point will cause the most recent choice point to choose another alternative. If there are no more alternatives to be considered at that choice point, a failure at an earlier choice point is triggered, and so on. Failure continuations are also invoked by the driver loop in response to a try-again request, to find another value of the expression. In addition, if a side-effect operation (such as assignment to a variable) occurs on a branch of the process resulting from a choice, it may be necessary, when the process finds a dead end, to undo the side effect before making a new choice. This is accomplished by having the side-effect operation produce a failure continuation that undoes the side effect and propagates the failure. In summary, failure continuations are constructed by • amb expressions—to provide a mechanism to make alternative choices if the current choice made by the amb expression leads to a dead end; • the top-level driver—to provide a mechanism to report failure when the choices are exhausted; • assignments—to intercept failures and undo assignments during backtracking. Failures are initiated only when a dead end is encountered. This occurs • if the user program executes (amb); • if the user types try-again at the top-level driver. Failure continuations are also called during processing of a failure: • When the failure continuation created by an assignment finishes undoing a side effect, it calls the failure continuation it intercepted, in order to propagate the failure back to the choice point that led to this assignment or to the top level. • When the failure continuation for an amb runs out of choices, it calls the failure continuation that was originally given to the amb, in order to propagate the failure back to the previous choice point or to the top level. Structure of the evaluator The syntax- and data-representation procedures for the amb evaluator, and also the basic analyze procedure, are identical to those in the evaluator of Section 4.1.7, except for the fact that we need additional syntax procedures to recognize the amb special form:56 56 We assume that the evaluator supports let (see Exercise 4.22), which we have used in our nondeterministic programs. 649 (define (amb? exp) (tagged-list? exp ’amb)) (define (amb-choices exp) (cdr exp)) We must also add to the dispatch in analyze a clause that will recognize this special form and generate an appropriate execution procedure: ((amb? exp) (analyze-amb exp)) The top-level procedure ambeval (similar to the version of eval given in Section 4.1.7) analyzes the given expression and applies the resulting execution procedure to the given environment, together with two given continuations: (define (ambeval exp env succeed fail) ((analyze exp) env succeed fail)) A success continuation is a procedure of two arguments: the value just obtained and another failure continuation to be used if that value leads to a subsequent failure. A failure continuation is a procedure of no arguments. So the general form of an execution procedure is (lambda (env succeed fail) ;; succeed is (lambda (value fail) ...) ;; fail is (lambda () ...) ...) For example, executing (ambeval 〈exp〉 the-global-environment (lambda (value fail) value) (lambda () ’failed)) 650 will attempt to evaluate the given expression and will return either the expression’s value (if the evaluation succeeds) or the symbol failed (if the evaluation fails). The call to ambeval in the driver loop shown below uses much more complicated continuation procedures, which continue the loop and support the try-again request. Most of the complexity of the amb evaluator results from the mechanics of passing the continuations around as the execution procedures call each other. In going through the following code, you should compare each of the execution procedures with the corresponding procedure for the ordinary evaluator given in Section 4.1.7. Simple expressions The execution procedures for the simplest kinds of expressions are essentially the same as those for the ordinary evaluator, except for the need to manage the continuations. The execution procedures simply succeed with the value of the expression, passing along the failure continuation that was passed to them. (define (analyze-self-evaluating exp) (lambda (env succeed fail) (succeed exp fail))) (define (analyze-quoted exp) (let ((qval (text-of-quotation exp))) (lambda (env succeed fail) (succeed qval fail)))) (define (analyze-variable exp) 651 (lambda (env succeed fail) (succeed (lookup-variable-value exp env) fail))) (define (analyze-lambda exp) (let ((vars (lambda-parameters exp)) (bproc (analyze-sequence (lambda-body exp)))) (lambda (env succeed fail) (succeed (make-procedure vars bproc env) fail)))) Notice that looking up a variable always ‘succeeds.’ If lookup-variablevalue fails to find the variable, it signals an error, as usual. Such a “failure” indicates a program bug—a reference to an unbound variable; it is not an indication that we should try another nondeterministic choice instead of the one that is currently being tried. Conditionals and sequences Conditionals are also handled in a similar way as in the ordinary evaluator. The execution procedure generated by analyze-if invokes the predicate execution procedure pproc with a success continuation that checks whether the predicate value is true and goes on to execute either the consequent or the alternative. If the execution of pproc fails, the original failure continuation for the if expression is called. (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (cproc (analyze (if-consequent exp))) (aproc (analyze (if-alternative exp)))) 652 (lambda (env succeed fail) (pproc env ;; success continuation for evaluating the predicate ;; to obtain pred-value (lambda (pred-value fail2) (if (true? pred-value) (cproc env succeed fail2) (aproc env succeed fail2))) ;; failure continuation for evaluating the predicate fail)))) Sequences are also handled in the same way as in the previous evaluator, except for the machinations in the subprocedure sequentially that are required for passing the continuations. Namely, to sequentially execute a and then b, we call a with a success continuation that calls b. (define (analyze-sequence exps) (define (sequentially a b) (lambda (env succeed fail) (a env ;; success continuation for calling a (lambda (a-value fail2) (b env succeed fail2)) ;; failure continuation for calling a fail))) (define (loop first-proc rest-procs) (if (null? rest-procs) first-proc (loop (sequentially first-proc (car rest-procs)) 653 (cdr rest-procs)))) (let ((procs (map analyze exps))) (if (null? procs) (error "Empty sequence - ANALYZE")) (loop (car procs) (cdr procs)))) Definitions and assignments Definitions are another case where we must go to some trouble to manage the continuations, because it is necessary to evaluate the definition-value expression before actually defining the new variable. To accomplish this, the definition-value execution procedure vproc is called with the environment, a success continuation, and the failure continuation. If the execution of vproc succeeds, obtaining a value val for the defined variable, the variable is defined and the success is propagated: (define (analyze-definition exp) (let ((var (definition-variable exp)) (vproc (analyze (definition-value exp)))) (lambda (env succeed fail) (vproc env (lambda (val fail2) (define-variable! var val env) (succeed ’ok fail2)) fail)))) Assignments are more interesting. This is the first place where we really use the continuations, rather than just passing them around. The execution procedure for assignments starts out like the one for definitions. It 654 first attempts to obtain the new value to be assigned to the variable. If this evaluation of vproc fails, the assignment fails. If vproc succeeds, however, and we go on to make the assignment, we must consider the possibility that this branch of the computation might later fail, which will require us to backtrack out of the assignment. Thus, we must arrange to undo the assignment as part of the backtracking process.57 This is accomplished by giving vproc a success continuation (marked with the comment “*1*” below) that saves the old value of the variable before assigning the new value to the variable and proceeding from the assignment. The failure continuation that is passed along with the value of the assignment (marked with the comment “*2*” below) restores the old value of the variable before continuing the failure. That is, a successful assignment provides a failure continuation that will intercept a subsequent failure; whatever failure would otherwise have called fail2 calls this procedure instead, to undo the assignment before actually calling fail2. (define (analyze-assignment exp) (let ((var (assignment-variable exp)) (vproc (analyze (assignment-value exp)))) (lambda (env succeed fail) (vproc env (lambda (val fail2) ; *1* (let ((old-value (lookup-variable-value var env))) (set-variable-value! var val env) (succeed ’ok 57 We didn’t worry about undoing definitions, since we can assume that internal defi- nitions are scanned out (Section 4.1.6). 655 (lambda () ; *2* (set-variable-value! var old-value env) (fail2))))) fail)))) Procedure applications The execution procedure for applications contains no new ideas except for the technical complexity of managing the continuations. This complexity arises in analyze-application, due to the need to keep track of the success and failure continuations as we evaluate the operands. We use a procedure get-args to evaluate the list of operands, rather than a simple map as in the ordinary evaluator. (define (analyze-application exp) (let ((fproc (analyze (operator exp))) (aprocs (map analyze (operands exp)))) (lambda (env succeed fail) (fproc env (lambda (proc fail2) (get-args aprocs env (lambda (args fail3) (execute-application proc args succeed fail3)) fail2)) fail)))) 656 In get-args, notice how cdr-ing down the list of aproc execution procedures and consing up the resulting list of args is accomplished by calling each aproc in the list with a success continuation that recursively calls get-args. Each of these recursive calls to get-args has a success continuation whose value is the cons of the newly obtained argument onto the list of accumulated arguments: (define (get-args aprocs env succeed fail) (if (null? aprocs) (succeed ’() fail) ((car aprocs) env ;; success continuation for this aproc (lambda (arg fail2) (get-args (cdr aprocs) env ;; success continuation for ;; recursive call to get-args (lambda (args fail3) (succeed (cons arg args) fail3)) fail2)) fail))) The actual procedure application, which is performed by executeapplication, is accomplished in the same way as for the ordinary evaluator, except for the need to manage the continuations. (define (execute-application proc args succeed fail) (cond ((primitive-procedure? proc) (succeed (apply-primitive-procedure proc args) 657 fail)) ((compound-procedure? proc) ((procedure-body proc) (extend-environment (procedure-parameters proc) args (procedure-environment proc)) succeed fail)) (else (error "Unknown procedure type - EXECUTE-APPLICATION" proc)))) Evaluating amb expressions The amb special form is the key element in the nondeterministic language. Here we see the essence of the interpretation process and the reason for keeping track of the continuations. The execution procedure for amb defines a loop try-next that cycles through the execution procedures for all the possible values of the amb expression. Each execution procedure is called with a failure continuation that will try the next one. When there are no more alternatives to try, the entire amb expression fails. (define (analyze-amb exp) (let ((cprocs (map analyze (amb-choices exp)))) (lambda (env succeed fail) (define (try-next choices) (if (null? choices) (fail) 658 ((car choices) env succeed (lambda () (try-next (cdr choices)))))) (try-next cprocs)))) Driver loop The driver loop for the amb evaluator is complex, due to the mechanism that permits the user to try again in evaluating an expression. The driver uses a procedure called internal-loop, which takes as argument a procedure try-again. The intent is that calling try-again should go on to the next untried alternative in the nondeterministic evaluation. Internal-loop either calls try-again in response to the user typing try-again at the driver loop, or else starts a new evaluation by calling ambeval. The failure continuation for this call to ambeval informs the user that there are no more values and re-invokes the driver loop. The success continuation for the call to ambeval is more subtle. We print the obtained value and then invoke the internal loop again with a try-again procedure that will be able to try the next alternative. This next-alternative procedure is the second argument that was passed to the success continuation. Ordinarily, we think of this second argument as a failure continuation to be used if the current evaluation branch later fails. In this case, however, we have completed a successful evaluation, so we can invoke the “failure” alternative branch in order to search for additional successful evaluations. (define input-prompt ";;; Amb-Eval input:") 659 (define output-prompt ";;; Amb-Eval value:") (define (driver-loop) (define (internal-loop try-again) (prompt-for-input input-prompt) (let ((input (read))) (if (eq? input ’try-again) (try-again) (begin (newline) (display ";;; Starting a new problem ") (ambeval input the-global-environment ;; ambeval success (lambda (val next-alternative) (announce-output output-prompt) (user-print val) (internal-loop next-alternative)) ;; ambeval failure (lambda () (announce-output ";;; There are no more values of") (user-print input) (driver-loop))))))) (internal-loop (lambda () (newline) 660 (display ";;; There is no current problem") (driver-loop)))) The initial call to internal-loop uses a try-again procedure that complains that there is no current problem and restarts the driver loop. This is the behavior that will happen if the user types try-again when there is no evaluation in progress. Exercise 4.50: Implement a new special form ramb that is like amb except that it searches alternatives in a random order, rather than from left to right. Show how this can help with Alyssa’s problem in Exercise 4.49. Implement a new kind of assignment called permanent-set! that is not undone upon failure. For example, we can choose two distinct elements from a list and count the Exercise 4.51: number of trials required to make a successful choice as follows: (define count 0) (let ((x (an-element-of ’(a b c))) (y (an-element-of ’(a b c)))) (permanent-set! count (+ count 1)) (require (not (eq? x y))) (list x y count)) ;;; Starting a new problem ;;; Amb-Eval value: (a b 2) ;;; Amb-Eval input: try-again 661 ;;; Amb-Eval value: (a c 3) What values would have been displayed if we had used set! here rather than permanent-set! ? Exercise 4.52: Implement a new construct called if-fail that permits the user to catch the failure of an expression. If-fail takes two expressions. It evaluates the first expression as usual and returns as usual if the evaluation succeeds. If the evaluation fails, however, the value of the second expression is returned, as in the following example: ;;; Amb-Eval input: (if-fail (let ((x (an-element-of ’(1 3 5)))) (require (even? x)) x) ’all-odd) ;;; Starting a new problem ;;; Amb-Eval value: all-odd ;;; Amb-Eval input: (if-fail (let ((x (an-element-of ’(1 3 5 8)))) (require (even? x)) x) ’all-odd) ;;; Starting a new problem ;;; Amb-Eval value: 8 662 Exercise 4.53: With permanent-set! as described in Exercise 4.51 and if-fail as in Exercise 4.52, what will be the result of evaluating (let ((pairs ’())) (if-fail (let ((p (prime-sum-pair ’(1 3 5 8) ’(20 35 110)))) (permanent-set! pairs (cons p pairs)) (amb)) pairs)) Exercise 4.54: If we had not realized that require could be implemented as an ordinary procedure that uses amb, to be defined by the user as part of a nondeterministic program, we would have had to implement it as a special form. This would require syntax procedures (define (require? exp) (tagged-list? exp ’require)) (define (require-predicate exp) (cadr exp)) and a new clause in the dispatch in analyze ((require? exp) (analyze-require exp)) as well the procedure analyze-require that handles require expressions. Complete the following definition of analyze-require. (define (analyze-require exp) (let ((pproc (analyze (require-predicate exp)))) (lambda (env succeed fail) (pproc env (lambda (pred-value fail2) 663 (if 〈??〉 〈??〉 (succeed ’ok fail2))) fail)))) 4.4 Logic Programming In Chapter 1 we stressed that computer science deals with imperative (how to) knowledge, whereas mathematics deals with declarative (what is) knowledge. Indeed, programming languages require that the programmer express knowledge in a form that indicates the step-by-step methods for solving particular problems. On the other hand, high-level languages provide, as part of the language implementation, a substantial amount of methodological knowledge that frees the user from concern with numerous details of how a specified computation will progress. Most programming languages, including Lisp, are organized around computing the values of mathematical functions. Expression-oriented languages (such as Lisp, Fortran, and Algol) capitalize on the “pun” that an expression that describes the value of a function may also be interpreted as a means of computing that value. Because of this, most programming languages are strongly biased toward unidirectional computations (computations with well-defined inputs and outputs). There are, however, radically different programming languages that relax this bias. We saw one such example in Section 3.3.5, where the objects of computation were arithmetic constraints. In a constraint system the direction and the order of computation are not so well specified; in carrying out a computation the system must therefore provide more detailed “how to” knowledge than would be the case with an ordinary arithmetic computation. This does not 664 mean, however, that the user is released altogether from the responsibility of providing imperative knowledge. There are many constraint networks that implement the same set of constraints, and the user must choose from the set of mathematically equivalent networks a suitable network to specify a particular computation. The nondeterministic program evaluator of Section 4.3 also moves away from the view that programming is about constructing algorithms for computing unidirectional functions. In a nondeterministic language, expressions can have more than one value, and, as a result, the computation is dealing with relations rather than with single-valued functions. Logic programming extends this idea by combining a relational vision of programming with a powerful kind of symbolic pattern matching called unification.58 58 Logic programming has grown out of a long history of research in automatic the- orem proving. Early theorem-proving programs could accomplish very little, because they exhaustively searched the space of possible proofs. The major breakthrough that made such a search plausible was the discovery in the early 1960s of the unification algorithm and the resolution principle (Robinson 1965). Resolution was used, for example, by Green and Raphael (1968) (see also Green 1969) as the basis for a deductive question-answering system. During most of this period, researchers concentrated on algorithms that are guaranteed to find a proof if one exists. Such algorithms were difficult to control and to direct toward a proof. Hewitt (1969) recognized the possibility of merging the control structure of a programming language with the operations of a logic-manipulation system, leading to the work in automatic search mentioned in Section 4.3.1 (Footnote 4.47). At the same time that this was being done, Colmerauer, in Marseille, was developing rule-based systems for manipulating natural language (see Colmerauer et al. 1973). He invented a programming language called Prolog for representing those rules. Kowalski (1973; 1979), in Edinburgh, recognized that execution of a Prolog program could be inter- 665 This approach, when it works, can be a very powerful way to write programs. Part of the power comes from the fact that a single “what is” fact can be used to solve a number of different problems that would have different “how to” components. As an example, consider the append operation, which takes two lists as arguments and combines their elements to form a single list. In a procedural language such as Lisp, we could define append in terms of the basic list constructor cons, as we did in Section 2.2.1: (define (append x y) (if (null? x) y (cons (car x) (append (cdr x) y)))) This procedure can be regarded as a translation into Lisp of the following two rules, the first of which covers the case where the first list is empty and the second of which handles the case of a nonempty list, which is a cons of two parts: • For any list y, the empty list and y append to form y. • For any u, v, y, and z, (cons u v) and y append to form (cons u z) if v and y append to form z.59 preted as proving theorems (using a proof technique called linear Horn-clause resolution). The merging of the last two strands led to the logic-programming movement. Thus, in assigning credit for the development of logic programming, the French can point to Prolog’s genesis at the University of Marseille, while the British can highlight the work at the University of Edinburgh. According to people at MIT, logic programming was developed by these groups in an attempt to figure out what Hewitt was talking about in his brilliant but impenetrable Ph.D. thesis. For a history of logic programming, see Robinson 1983. 59 To see the correspondence between the rules and the procedure, let x in the pro- cedure (where x is nonempty) correspond to (cons u v) in the rule. Then z in the rule corresponds to the append of (cdr x) and y. 666 Using the append procedure, we can answer questions such as Find the append of (a b) and (c d). But the same two rules are also sufficient for answering the following sorts of questions, which the procedure can’t answer: Find a list y that appends with (a b) to produce (a b c d). Find all x and y that append to form (a b c d). In a logic programming language, the programmer writes an append “procedure” by stating the two rules about append given above. “How to” knowledge is provided automatically by the interpreter to allow this single pair of rules to be used to answer all three types of questions about append.60 Contemporary logic programming languages (including the one we implement here) have substantial deficiencies, in that their general “how to” methods can lead them into spurious infinite loops or other undesirable behavior. Logic programming is an active field of research in computer science.61 60 This certainly does not relieve the user of the entire problem of how to compute the answer. There are many different mathematically equivalent sets of rules for formulating the append relation, only some of which can be turned into effective devices for computing in any direction. In addition, sometimes “what is” information gives no clue “how to” compute an answer. For example, consider the problem of computing the y such that y 2 = x . 61 Interest in logic programming peaked during the early 80s when the Japanese gov- ernment began an ambitious project aimed at building superfast computers optimized to run logic programming languages. The speed of such computers was to be measured in LIPS (Logical Inferences Per Second) rather than the usual FLOPS (FLoating-point Operations Per Second). Although the project succeeded in de- 667 Earlier in this chapter we explored the technology of implementing interpreters and described the elements that are essential to an interpreter for a Lisp-like language (indeed, to an interpreter for any conventional language). Now we will apply these ideas to discuss an interpreter for a logic programming language. We call this language the query language, because it is very useful for retrieving information from data bases by formulating queries, or questions, expressed in the language. Even though the query language is very different from Lisp, we will find it convenient to describe the language in terms of the same general framework we have been using all along: as a collection of primitive elements, together with means of combination that enable us to combine simple elements to create more complex elements and means of abstraction that enable us to regard complex elements as single conceptual units. An interpreter for a logic programming language is considerably more complex than an interpreter for a language like Lisp. Nevertheless, we will see that our query-language interpreter contains many of the same elements found in the interpreter of Section 4.1. In particular, there will be an “eval” part that classifies expressions according to type and an “apply” part that implements the language’s abstraction mechanism (procedures in the case of Lisp, and rules in the case of logic programming). Also, a central role is played in the implementation by a frame data structure, which determines the correspondence veloping hardware and software as originally planned, the international computer industry moved in a different direction. See Feigenbaum and Shrobe 1993 for an overview evaluation of the Japanese project. The logic programming community has also moved on to consider relational programming based on techniques other than simple pattern matching, such as the ability to deal with numerical constraints such as the ones illustrated in the constraint-propagation system of Section 3.3.5. 668 between symbols and their associated values. One additional interesting aspect of our query-language implementation is that we make substantial use of streams, which were introduced in Chapter 3. 4.4.1 Deductive Information Retrieval Logic programming excels in providing interfaces to data bases for information retrieval. The query language we shall implement in this chapter is designed to be used in this way. In order to illustrate what the query system does, we will show how it can be used to manage the data base of personnel records for Microshaft, a thriving high-technology company in the Boston area. The language provides pattern-directed access to personnel information and can also take advantage of general rules in order to make logical deductions. A sample data base The personnel data base for Microshaft contains assertions about company personnel. Here is the information about Ben Bitdiddle, the resident computer wizard: (address (Bitdiddle Ben) (Slumerville (Ridge Road) 10)) (job (Bitdiddle Ben) (computer wizard)) (salary (Bitdiddle Ben) 60000) Each assertion is a list (in this case a triple) whose elements can themselves be lists. As resident wizard, Ben is in charge of the company’s computer division, and he supervises two programmers and one technician. Here is the information about them: (address (Hacker Alyssa P) (Cambridge (Mass Ave) 78)) 669 (job (Hacker Alyssa P) (computer programmer)) (salary (Hacker Alyssa P) 40000) (supervisor (Hacker Alyssa P) (Bitdiddle Ben)) (address (Fect Cy D) (Cambridge (Ames Street) 3)) (job (Fect Cy D) (computer programmer)) (salary (Fect Cy D) 35000) (supervisor (Fect Cy D) (Bitdiddle Ben)) (address (Tweakit Lem E) (Boston (Bay State Road) 22)) (job (Tweakit Lem E) (computer technician)) (salary (Tweakit Lem E) 25000) (supervisor (Tweakit Lem E) (Bitdiddle Ben)) There is also a programmer trainee, who is supervised by Alyssa: (address (Reasoner Louis) (Slumerville (Pine Tree Road) 80)) (job (Reasoner Louis) (computer programmer trainee)) (salary (Reasoner Louis) 30000) (supervisor (Reasoner Louis) (Hacker Alyssa P)) All of these people are in the computer division, as indicated by the word computer as the first item in their job descriptions. Ben is a high-level employee. His supervisor is the company’s big wheel himself: (supervisor (Bitdiddle Ben) (Warbucks Oliver)) (address (Warbucks Oliver) (Swellesley (Top Heap Road))) (job (Warbucks Oliver) (administration big wheel)) (salary (Warbucks Oliver) 670 Besides the computer division supervised by Ben, the company has an accounting division, consisting of a chief accountant and his assistant: (address (Scrooge Eben) (Weston (Shady Lane) 10)) (job (Scrooge Eben) (accounting chief accountant)) (salary (Scrooge Eben) 75000) (supervisor (Scrooge Eben) (Warbucks Oliver)) (address (Cratchet Robert) (Allston (N Harvard Street) 16)) (job (Cratchet Robert) (accounting scrivener)) (salary (Cratchet Robert) 18000) (supervisor (Cratchet Robert) (Scrooge Eben)) There is also a secretary for the big wheel: (address (Aull DeWitt) (Slumerville (Onion Square) 5)) (job (Aull DeWitt) (administration secretary)) (salary (Aull DeWitt) 25000) (supervisor (Aull DeWitt) (Warbucks Oliver)) The data base also contains assertions about which kinds of jobs can be done by people holding other kinds of jobs. For instance, a computer wizard can do the jobs of both a computer programmer and a computer technician: (can-do-job (computer wizard) (computer programmer)) (can-do-job (computer wizard) (computer technician)) A computer programmer could fill in for a trainee: (can-do-job (computer programmer) (computer programmer trainee)) Also, as is well known, 671 (can-do-job (administration secretary) (administration big wheel)) Simple queries The query language allows users to retrieve information from the data base by posing queries in response to the system’s prompt. For example, to find all computer programmers one can say ;;; Query input: (job ?x (computer programmer)) The system will respond with the following items: ;;; Query results: (job (Hacker Alyssa P) (computer programmer)) (job (Fect Cy D) (computer programmer)) The input query specifies that we are looking for entries in the data base that match a certain pattern. In this example, the pattern specifies entries consisting of three items, of which the first is the literal symbol job, the second can be anything, and the third is the literal list (computer programmer). The “anything” that can be the second item in the matching list is specified by a pattern variable, ?x. The general form of a pattern variable is a symbol, taken to be the name of the variable, preceded by a question mark. We will see below why it is useful to specify names for pattern variables rather than just putting ? into patterns to represent “anything.” The system responds to a simple query by showing all entries in the data base that match the specified pattern. A pattern can have more than one variable. For example, the query (address ?x ?y) will list all the employees’ addresses. 672 A pattern can have no variables, in which case the query simply determines whether that pattern is an entry in the data base. If so, there will be one match; if not, there will be no matches. The same pattern variable can appear more than once in a query, specifying that the same “anything” must appear in each position. This is why variables have names. For example, (supervisor ?x ?x) finds all people who supervise themselves (though there are no such assertions in our sample data base). The query (job ?x (computer ?type)) matches all job entries whose third item is a two-element list whose first item is computer: (job (Bitdiddle Ben) (computer wizard)) (job (Hacker Alyssa P) (computer programmer)) (job (Fect Cy D) (computer programmer)) (job (Tweakit Lem E) (computer technician)) This same pattern does not match (job (Reasoner Louis) (computer programmer trainee)) because the third item in the entry is a list of three elements, and the pattern’s third item specifies that there should be two elements. If we wanted to change the pattern so that the third item could be any list beginning with computer, we could specify62 (job ?x (computer . ?type)) For example, 62 This uses the dotted-tail notation introduced in Exercise 2.20. 673 (computer . ?type) matches the data (computer programmer trainee) with ?type as the list (programmer trainee). It also matches the data (computer programmer) with ?type as the list (programmer), and matches the data (computer) with ?type as the empty list (). We can describe the query language’s processing of simple queries as follows: • The system finds all assignments to variables in the query pattern that satisfy the pattern—that is, all sets of values for the variables such that if the pattern variables are instantiated with (replaced by) the values, the result is in the data base. • The system responds to the query by listing all instantiations of the query pattern with the variable assignments that satisfy it. Note that if the pattern has no variables, the query reduces to a determination of whether that pattern is in the data base. If so, the empty assignment, which assigns no values to variables, satisfies that pattern for that data base. Exercise 4.55: Give simple queries that retrieve the following information from the data base: 1. all people supervised by Ben Bitdiddle; 2. the names and jobs of all people in the accounting division; 3. the names and addresses of all people who live in Slumerville. Compound queries Simple queries form the primitive operations of the query language. In order to form compound operations, the query language provides means of combination. One thing that makes the query language a logic programming language is that the means of combination mirror the means of combination used in forming logical expressions: and, or, and not. (Here and, or, and not are not the Lisp primitives, but rather operations built into the query language.) We can use and as follows to find the addresses of all the computer programmers: (and (job ?person (computer programmer)) (address ?person ?where)) The resulting output is (and (job (Hacker Alyssa P) (computer programmer)) (address (Hacker Alyssa P) (Cambridge (Mass Ave) 78))) (and (job (Fect Cy D) (computer programmer)) (address (Fect Cy D) (Cambridge (Ames Street) 3))) In general, (and 〈quer y 1 〉〈quer y 2 〉 ... 〈quer y n 〉) is satisfied by all sets of values for the pattern variables that simultaneously satisfy 〈quer y 1 〉 . . . 〈quer y n 〉. As for simple queries, the system processes a compound query by finding all assignments to the pattern variables that satisfy the query, then displaying instantiations of the query with those values. 675 Another means of constructing compound queries is through or. For example, (or (supervisor ?x (Bitdiddle Ben)) (supervisor ?x (Hacker Alyssa P))) will find all employees supervised by Ben Bitdiddle or Alyssa P. Hacker: (or (supervisor (Hacker Alyssa P) (Bitdiddle Ben)) (supervisor (Hacker Alyssa P) (Hacker Alyssa P))) (or (supervisor (Fect Cy D) (Bitdiddle Ben)) (supervisor (Fect Cy D) (Hacker Alyssa P))) (or (supervisor (Tweakit Lem E) (Bitdiddle Ben)) (supervisor (Tweakit Lem E) (Hacker Alyssa P))) (or (supervisor (Reasoner Louis) (Bitdiddle Ben)) (supervisor (Reasoner Louis) (Hacker Alyssa P))) In general, (or 〈quer y 1 〉〈quer y 2 〉 ... 〈quer y n 〉) is satisfied by all sets of values for the pattern variables that satisfy at least one of 〈quer y 1 〉 . . . 〈quer y n 〉. Compound queries can also be formed with not. For example, (and (supervisor ?x (Bitdiddle Ben)) (not (job ?x (computer programmer)))) finds all people supervised by Ben Bitdiddle who are not computer programmers. In general, (not 〈quer y 1 〉) 676 is satisfied by all assignments to the pattern variables that do not satisfy 〈quer y 1 〉.63 The final combining form is called lisp-value. When lisp-value is the first element of a pattern, it specifies that the next element is a Lisp predicate to be applied to the rest of the (instantiated) elements as arguments. In general, (lisp-value 〈predicate〉〈ar g 1 〉 ... 〈ar g n 〉) will be satisfied by assignments to the pattern variables for which the 〈predicate〉 applied to the instantiated 〈ar g 1 〉 . . . 〈ar g n 〉 is true. For example, to find all people whose salary is greater than $30,000 we could write64 (and (salary ?person ?amount) (lisp-value > ?amount 30000)) Exercise 4.56: Formulate compound queries that retrieve the following information: a. the names of all people who are supervised by Ben Bitdiddle, together with their addresses; b. all people whose salary is less than Ben Bitdiddle’s, together with their salary and Ben Bitdiddle’s salary; 63 Actually, this description of not is valid only for simple cases. The real behavior of not is more complex. We will examine not’s peculiarities in Section 4.4.2 and Section 4.4.3. 64 Lisp-value should be used only to perform an operation not provided in the query language. In particular, it should not be used to test equality (since that is what the matching in the query language is designed to do) or inequality (since that can be done with the same rule shown below). 677 c. all people who are supervised by someone who is not in the computer division, together with the supervisor’s name and job. Rules In addition to primitive queries and compound queries, the query language provides means for abstracting queries. These are given by rules. The rule (rule (lives-near ?person-1 ?person-2) (and (address ?person-1 (?town . ?rest-1)) (address ?person-2 (?town . ?rest-2)) (not (same ?person-1 ?person-2)))) specifies that two people live near each other if they live in the same town. The final not clause prevents the rule from saying that all people live near themselves. The same relation is defined by a very simple rule:65 (rule (same ?x ?x)) The following rule declares that a person is a “wheel” in an organization if he supervises someone who is in turn a supervisor: (rule (wheel ?person) (and (supervisor ?middle-manager ?person) 65 Notice that we do not need same in order to make two things be the same: We just use the same pattern variable for each—in effect, we have one thing instead of two things in the first place. For example, see ?town in the lives-near rule and ?middle-manager in the wheel rule below. Same is useful when we want to force two things to be different, such as ?person-1 and ?person-2 in the lives-near rule. Although using the same pattern variable in two parts of a query forces the same value to appear in both places, using different pattern variables does not force different values to appear. (The values assigned to different pattern variables may be the same or different.) 678 (supervisor ?x ?middle-manager))) The general form of a rule is (rule 〈conclusion〉〈body〉) where 〈conclusion〉 is a pattern and 〈body〉 is any query.66 We can think of a rule as representing a large (even infinite) set of assertions, namely all instantiations of the rule conclusion with variable assignments that satisfy the rule body. When we described simple queries (patterns), we said that an assignment to variables satisfies a pattern if the instantiated pattern is in the data base. But the pattern needn’t be explicitly in the data base as an assertion. It can be an implicit assertion implied by a rule. For example, the query (lives-near ?x (Bitdiddle Ben)) results in (lives-near (Reasoner Louis) (Bitdiddle Ben)) (lives-near (Aull DeWitt) (Bitdiddle Ben)) To find all computer programmers who live near Ben Bitdiddle, we can ask (and (job ?x (computer programmer)) (lives-near ?x (Bitdiddle Ben))) As in the case of compound procedures, rules can be used as parts of other rules (as we saw with the lives-near rule above) or even be defined recursively. For instance, the rule (rule (outranked-by ?staff-person ?boss) (or (supervisor ?staff-person ?boss) 66 We will also allow rules without bodies, as in same, and we will interpret such a rule to mean that the rule conclusion is satisfied by any values of the variables. 679 (and (supervisor ?staff-person ?middle-manager) (outranked-by ?middle-manager ?boss)))) says that a staff person is outranked by a boss in the organization if the boss is the person’s supervisor or (recursively) if the person’s supervisor is outranked by the boss. Exercise 4.57: Define a rule that says that person 1 can replace person 2 if either person 1 does the same job as person 2 or someone who does person 1’s job can also do person 2’s job, and if person 1 and person 2 are not the same person. Using your rule, give queries that find the following: a. all people who can replace Cy D. Fect; b. all people who can replace someone who is being paid more than they are, together with the two salaries. Exercise 4.58: Define a rule that says that a person is a “big shot” in a division if the person works in the division but does not have a supervisor who works in the division. Exercise 4.59: Ben Bitdiddle has missed one meeting too many. Fearing that his habit of forgetting meetings could cost him his job, Ben decides to do something about it. He adds all the weekly meetings of the firm to the Microshaft data base by asserting the following: (meeting accounting (Monday 9am)) (meeting administration (Monday 10am)) (meeting computer (Wednesday 3pm)) (meeting administration (Friday 1pm)) 680 Each of the above assertions is for a meeting of an entire division. Ben also adds an entry for the company-wide meeting that spans all the divisions. All of the company’s employees attend this meeting. (meeting whole-company (Wednesday 4pm)) a. On Friday morning, Ben wants to query the data base for all the meetings that occur that day. What query should he use? b. Alyssa P. Hacker is unimpressed. She thinks it would be much more useful to be able to ask for her meetings by specifying her name. So she designs a rule that says that a person’s meetings include all whole-company meetings plus all meetings of that person’s division. Fill in the body of Alyssa’s rule. (rule (meeting-time ?person ?day-and-time) 〈rule-body〉) c. Alyssa arrives at work on Wednesday morning and wonders what meetings she has to attend that day. Having defined the above rule, what query should she make to find this out? Exercise 4.60: By giving the query (lives-near ?person (Hacker Alyssa P)) Alyssa P. Hacker is able to find people who live near her, with whom she can ride to work. On the other hand, when she tries to find all pairs of people who live near each other by querying (lives-near ?person-1 ?person-2) she notices that each pair of people who live near each other is listed twice; for example, (lives-near (Hacker Alyssa P) (Fect Cy D)) 681 (lives-near (Fect Cy D) (Hacker Alyssa P)) Why does this happen? Is there a way to find a list of people who live near each other, in which each pair appears only once? Explain. Logic as programs We can regard a rule as a kind of logical implication: If an assignment of values to pattern variables satisfies the body, then it satisfies the conclusion. Consequently, we can regard the query language as having the ability to perform logical deductions based upon the rules. As an example, consider the append operation described at the beginning of Section 4.4. As we said, append can be characterized by the following two rules: • For any list y, the empty list and y append to form y. • For any u, v, y, and z, (cons u v) and y append to form (cons u z) if v and y append to form z. To express this in our query language, we define two rules for a relation (append-to-form x y z) which we can interpret to mean “x and y append to form z”: (rule (append-to-form () ?y ?y)) (rule (append-to-form (?u . ?v) ?y (?u . ?z)) (append-to-form ?v ?y ?z)) The first rule has no body, which means that the conclusion holds for any value of ?y. Note how the second rule makes use of dotted-tail notation to name the car and cdr of a list. Given these two rules, we can formulate queries that compute the append of two lists: 682 ;;; Query input: (append-to-form (a b) (c d) ?z) ;;; Query results: (append-to-form (a b) (c d) (a b c d)) What is more striking, we can use the same rules to ask the question “Which list, when appended to (a b), yields (a b c d)?” This is done as follows: ;;; Query input: (append-to-form (a b) ?y (a b c d)) ;;; Query results: (append-to-form (a b) (c d) (a b c d)) We can also ask for all pairs of lists that append to form (a b c d): ;;; Query input: (append-to-form ?x ?y (a b c d)) ;;; Query results: (append-to-form () (a b c d) (a b c d)) (append-to-form (a) (b c d) (a b c d)) (append-to-form (a b) (c d) (a b c d)) (append-to-form (a b c) (d) (a b c d)) (append-to-form (a b c d) () (a b c d)) The query system may seem to exhibit quite a bit of intelligence in using the rules to deduce the answers to the queries above. Actually, as we will see in the next section, the system is following a well-determined algorithm in unraveling the rules. Unfortunately, although the system works impressively in the append case, the general methods may break down in more complex cases, as we will see in Section 4.4.3. 683 Exercise 4.61: The following rules implement a next-to relation that finds adjacent elements of a list: (rule (?x next-to ?y in (?x ?y . ?u))) (rule (?x next-to ?y in (?v . ?z)) (?x next-to ?y in What will the response be to the following queries? (?x next-to ?y in (1 (2 3) 4)) (?x next-to 1 in (2 1 3 1)) Exercise 4.62: Define rules to implement the last-pair operation of Exercise 2.17, which returns a list containing the last element of a nonempty list. Check your rules on queries such as (last-pair (3) ?x), (last-pair (1 2 3) ?x) and (last-pair (2 ?x) (3)). Do your rules work correctly on queries such as (last-pair ?x (3)) ? Exercise 4.63: The following data base (see Genesis 4) traces the genealogy of the descendants of Ada back to Adam, by way of Cain: (son Adam Cain) (son Cain Enoch) (son Enoch Irad) (son Irad Mehujael) (son Mehujael Methushael) (son Methushael Lamech) (wife Lamech Ada) (son Ada Jabal) (son Ada Jubal) 684 Formulate rules such as “If S is the son of f , and f is the son of G , then S is the grandson of G ” and “If W is the wife of M , and S is the son of W , then S is the son of M ” (which was supposedly more true in biblical times than today) that will enable the query system to find the grandson of Cain; the sons of Lamech; the grandsons of Methushael. (See Exercise 4.69 for some rules to deduce more complicated relationships.) 4.4.2 How the Query System Works In Section 4.4.4 we will present an implementation of the query interpreter as a collection of procedures. In this section we give an overview that explains the general structure of the system independent of low-level implementation details. After describing the implementation of the interpreter, we will be in a position to understand some of its limitations and some of the subtle ways in which the query language’s logical operations differ from the operations of mathematical logic. It should be apparent that the query evaluator must perform some kind of search in order to match queries against facts and rules in the data base. One way to do this would be to implement the query system as a nondeterministic program, using the amb evaluator of Section 4.3 (see Exercise 4.78). Another possibility is to manage the search with the aid of streams. Our implementation follows this second approach. The query system is organized around two central operations called pattern matching and unification. We first describe pattern matching and explain how this operation, together with the organization of information in terms of streams of frames, enables us to implement both simple and compound queries. We next discuss unification, a generalization of pat- 685 tern matching needed to implement rules. Finally, we show how the entire query interpreter fits together through a procedure that classifies expressions in a manner analogous to the way eval classifies expressions for the interpreter described in Section 4.1. Pattern matching A pattern matcher is a program that tests whether some datum fits a specified pattern. For example, the data list ((a b) c (a b)) matches the pattern (?x c ?x) with the pattern variable ?x bound to (a b). The same data list matches the pattern (?x ?y ?z) with ?x and ?z both bound to (a b) and ?y bound to c. It also matches the pattern ((?x ?y) c (?x ?y)) with ?x bound to a and ?y bound to b. However, it does not match the pattern (?x a ?y), since that pattern specifies a list whose second element is the symbol a. The pattern matcher used by the query system takes as inputs a pattern, a datum, and a frame that specifies bindings for various pattern variables. It checks whether the datum matches the pattern in a way that is consistent with the bindings already in the frame. If so, it returns the given frame augmented by any bindings that may have been determined by the match. Otherwise, it indicates that the match has failed. For example, using the pattern (? x ?y ?x) to match (a b a) given an empty frame will return a frame specifying that ?x is bound to a and ?y is bound to b. Trying the match with the same pattern, the same datum, and a frame specifying that ?y is bound to a will fail. Trying the match with the same pattern, the same datum, and a frame in which ?y is bound to b and ?x is unbound will return the given frame augmented by a binding of ?x to a. 686 The pattern matcher is all the mechanism that is needed to process simple queries that don’t involve rules. For instance, to process the query (job ?x (computer programmer)) we scan through all assertions in the data base and select those that match the pattern with respect to an initially empty frame. For each match we find, we use the frame returned by the match to instantiate the pattern with a value for ?x. Streams of frames The testing of patterns against frames is organized through the use of streams. Given a single frame, the matching process runs through the data-base entries one by one. For each data-base entry, the matcher generates either a special symbol indicating that the match has failed or an extension to the frame. The results for all the data-base entries are collected into a stream, which is passed through a filter to weed out the failures. The result is a stream of all the frames that extend the given frame via a match to some assertion in the data base.67 In our system, a query takes an input stream of frames and performs the above matching operation for every frame in the stream, as indicated in 67 Because matching is generally very expensive, we would like to avoid applying the full matcher to every element of the data base. This is usually arranged by breaking up the process into a fast, coarse match and the final match. The coarse match filters the data base to produce a small set of candidates for the final match. With care, we can arrange our data base so that some of the work of coarse matching can be done when the data base is constructed rather then when we want to select the candidates. This is called indexing the data base. There is a vast technology built around data-base-indexing schemes. Our implementation, described in Section 4.4.4, contains a simple-minded form of such an optimization. 687 Figure 4.4. That is, for each frame in the input stream, the query generates a new stream consisting of all extensions to that frame by matches to assertions in the data base. All these streams are then combined to form one huge stream, which contains all possible extensions of every frame in the input stream. This stream is the output of the query. Figure 4.4: A query processes a stream of frames. To answer a simple query, we use the query with an input stream consisting of a single empty frame. The resulting output stream contains all extensions to the empty frame (that is, all answers to our query). This stream of frames is then used to generate a stream of copies of the original query pattern with the variables instantiated by the values in each frame, and this is the stream that is finally printed. Compound queries The real elegance of the stream-of-frames implementation is evident when we deal with compound queries. The processing of compound queries makes use of the ability of our matcher to demand that a match be consis- 688 tent with a specified frame. For example, to handle the and of two queries, such as (and (can-do-job ?x (computer programmer trainee)) (job ?person ?x)) (informally, “Find all people who can do the job of a computer programmer trainee”), we first find all entries that match the pattern (can-do-job ?x (computer programmer trainee)) This produces a stream of frames, each of which contains a binding for ?x. Then for each frame in the stream we find all entries that match (job ?person ?x) in a way that is consistent with the given binding for ?x. Each such match will produce a frame containing bindings for ?x and ?person. The and of two queries can be viewed as a series combination of the two component queries, as shown in Figure 4.5. The frames that pass through the first query filter are filtered and further extended by the second query. Figure 4.5: The and combination of two queries is produced by operating on the stream of frames in series. 689 Figure 4.6 shows the analogous method for computing the or of two queries as a parallel combination of the two component queries. The input stream of frames is extended separately by each query. The two resulting streams are then merged to produce the final output stream. Figure 4.6: The or combination of two queries is produced by operating on the stream of frames in parallel and merging the results. Even from this high-level description, it is apparent that the processing of compound queries can be slow. For example, since a query may produce more than one output frame for each input frame, and each query in an and gets its input frames from the previous query, an and query could, in the worst case, have to perform a number of matches that is exponential 690 in the number of queries (see Exercise 4.76).68 Though systems for handling only simple queries are quite practical, dealing with complex queries is extremely difficult.69 From the stream-of-frames viewpoint, the not of some query acts as a filter that removes all frames for which the query can be satisfied. For instance, given the pattern (not (job ?x (computer programmer))) we attempt, for each frame in the input stream, to produce extension frames that satisfy (job ?x (computer programmer)). We remove from the input stream all frames for which such extensions exist. The result is a stream consisting of only those frames in which the binding for ?x does not satisfy (job ?x (computer programmer)). processing the query For example, in (and (supervisor ?x ?y) (not (job ?x (computer programmer)))) the first clause will generate frames with bindings for ?x and ?y. The not clause will then filter these by removing all frames in which the binding for ?x satisfies the restriction that ?x is a computer programmer.70 The lisp-value special form is implemented as a similar filter on frame streams. We use each frame in the stream to instantiate any variables in the 68 But this kind of exponential explosion is not common in and queries because the added conditions tend to reduce rather than expand the number of frames produced. 69 There is a large literature on data-base-management systems that is concerned with how to handle complex queries efficiently. 70 There is a subtle difference between this filter implementation of not and the usual meaning of not in mathematical logic. See Section 4.4.3. 691 pattern, then apply the Lisp predicate. We remove from the input stream all frames for which the predicate fails. Unification In order to handle rules in the query language, we must be able to find the rules whose conclusions match a given query pattern. Rule conclusions are like assertions except that they can contain variables, so we will need a generalization of pattern matching—called unification—in which both the “pattern” and the “datum” may contain variables. A unifier takes two patterns, each containing constants and variables, and determines whether it is possible to assign values to the variables that will make the two patterns equal. If so, it returns a frame containing these bindings. For example, unifying (?x a ?y) and (?y ?z a) will specify a frame in which ?x, ?y, and ?z must all be bound to a. On the other hand, unifying (?x ?y a) and (?x b ?y) will fail, because there is no value for ?y that can make the two patterns equal. (For the second elements of the patterns to be equal, ?y would have to be b; however, for the third elements to be equal, ?y would have to be a.) The unifier used in the query system, like the pattern matcher, takes a frame as input and performs unifications that are consistent with this frame. The unification algorithm is the most technically difficult part of the query system. With complex patterns, performing unification may seem to require deduction. To unify (?x ?x) and ((a ?y c) (a b ?z)), for example, the algorithm must infer that ? x should be (a b c), ?y should be b, and ?z should be c. We may think of this process as solving a set of equations among the pattern components. In general, these are simultaneous equations, which may require substantial manipulation to 692 solve.71 For example, unifying (?x ?x) and ((a ?y c) (a b ?z)) may be thought of as specifying the simultaneous equations ?x (a ?y c) (a b ?z) These equations imply that (a ?y c) (a b ?z) which in turn implies that a a, ?y b, c and hence that ?x (a b c) In a successful pattern match, all pattern variables become bound, and the values to which they are bound contain only constants. This is also true of all the examples of unification we have seen so far. In general, however, a successful unification may not completely determine the variable values; some variables may remain unbound and others may be bound to values that contain variables. Consider the unification of (?x a) and ((b ?y) ?z). We can deduce that ?x = (b ?y) and a = ?z, but we cannot further solve for ?x or ?y. The unification doesn’t fail, since it is certainly possible to make the two patterns equal by assigning values to ?x and ?y. Since this match in no way restricts the values ?y can take on, no binding for ?y is put into the result frame. The match does, however, restrict the value of ?x. Whatever value ?y has, ?x must be (b ?y). A binding of ?x to the pattern (b ?y) is thus put into the frame. If a value for ?y is later determined and added to the 71 In one-sided pattern matching, all the equations that contain pattern variables are explicit and already solved for the unknown (the pattern variable). 693 frame (by a pattern match or unification that is required to be consistent with this frame), the previously bound ?x will refer to this value.72 Applying rules Unification is the key to the component of the query system that makes inferences from rules. To see how this is accomplished, consider processing a query that involves applying a rule, such as (lives-near ?x (Hacker Alyssa P)) To process this query, we first use the ordinary pattern-match procedure described above to see if there are any assertions in the data base that match this pattern. (There will not be any in this case, since our data base includes no direct assertions about who lives near whom.) The next step is to attempt to unify the query pattern with the conclusion of each rule. We find that the pattern unifies with the conclusion of the rule (rule (lives-near ?person-1 ?person-2) (and (address ?person-1 (?town . ?rest-1)) (address ?person-2 (?town . ?rest-2)) (not (same ?person-1 ?person-2)))) resulting in a frame specifying that ?person-2 is bound to (Hacker Alyssa P) and that ?x should be bound to (have the same value as) ?person-1. Now, relative to this frame, we evaluate the compound query given by the body of the rule. Successful matches will extend this frame by providing a 72 Another way to think of unification is that it generates the most general pattern that is a specialization of the two input patterns. That is, the unification of (?x a) and ((b ?y) ?z) is ((b ?y) a), and the unification of (?x a ?y) and (?y ?z a), discussed above, is (a a a). For our implementation, it is more convenient to think of the result of unification as a frame rather than a pattern. 694 binding for ?person-1, and consequently a value for ?x, which we can use to instantiate the original query pattern. In general, the query evaluator uses the following method to apply a rule when trying to establish a query pattern in a frame that specifies bindings for some of the pattern variables: • Unify the query with the conclusion of the rule to form, if successful, an extension of the original frame. • Relative to the extended frame, evaluate the query formed by the body of the rule. Notice how similar this is to the method for applying a procedure in the eval/apply evaluator for Lisp: • Bind the procedure’s parameters to its arguments to form a frame that extends the original procedure environment. • Relative to the extended environment, evaluate the expression formed by the body of the procedure. The similarity between the two evaluators should come as no surprise. Just as procedure definitions are the means of abstraction in Lisp, rule definitions are the means of abstraction in the query language. In each case, we unwind the abstraction by creating appropriate bindings and evaluating the rule or procedure body relative to these. Simple queries We saw earlier in this section how to evaluate simple queries in the absence of rules. Now that we have seen how to apply rules, we can describe how to evaluate simple queries by using both rules and assertions. Given the query pattern and a stream of frames, we produce, for each frame in the input stream, two streams: • a stream of extended frames obtained by matching the pattern against all assertions in the data base (using the pattern matcher), and • a stream of extended frames obtained by applying all possible rules (using the unifier).73 Appending these two streams produces a stream that consists of all the ways that the given pattern can be satisfied consistent with the original frame. These streams (one for each frame in the input stream) are now all combined to form one large stream, which therefore consists of all the ways that any of the frames in the original input stream can be extended to produce a match with the given pattern. The query evaluator and the driver loop Despite the complexity of the underlying matching operations, the system is organized much like an evaluator for any language. The procedure that coordinates the matching operations is called qeval, and it plays a role analogous to that of the eval procedure for Lisp. Qeval takes as inputs a query and a stream of frames. Its output is a stream of frames, corresponding to successful matches to the query pattern, that extend some frame in the input stream, as indicated in Figure 4.4. Like eval, qeval classifies the different types of expressions (queries) and dispatches to an appropriate procedure for each. There is a procedure for each special form (and, or, not, and lisp-value) and one for simple queries. 73 Since unification is a generalization of matching, we could simplify the system by using the unifier to produce both streams. Treating the easy case with the simple matcher, however, illustrates how matching (as opposed to full-blown unification) can be useful in its own right. 696 The driver loop, which is analogous to the driver-loop procedure for the other evaluators in this chapter, reads queries from the terminal. For each query, it calls qeval with the query and a stream that consists of a single empty frame. This will produce the stream of all possible matches (all possible extensions to the empty frame). For each frame in the resulting stream, it instantiates the original query using the values of the variables found in the frame. This stream of instantiated queries is then printed.74 The driver also checks for the special command assert!, which signals that the input is not a query but rather an assertion or rule to be added to the data base. For instance, (assert! (job (Bitdiddle Ben) (computer wizard))) (assert! (rule (wheel ? person) (and (supervisor ?middle-manager ?person) (supervisor ?x ?middle-manager)))) 4.4.3 Is Logic Programming Mathematical Logic? The means of combination used in the query language may at first seem identical to the operations and, or, and not of mathematical logic, and the application of query-language rules is in fact accomplished through a legitimate method of inference.75 This identification of the query language with 74 The reason we use streams (rather than lists) of frames is that the recursive appli- cation of rules can generate infinite numbers of values that satisfy a query. The delayed evaluation embodied in streams is crucial here: The system will print responses one by one as they are generated, regardless of whether there are a finite or infinite number of responses. 75 That a particular method of inference is legitimate is not a trivial assertion. One must prove that if one starts with true premises, only true conclusions can be de- 697 mathematical logic is not really valid, though, because the query language provides a control structure that interprets the logical statements procedurally. We can often take advantage of this control structure. For example, to find all of the supervisors of programmers we could formulate a query in either of two logically equivalent forms: (and (job ?x (computer programmer)) (supervisor ? x ?y)) or (and (supervisor ?x ?y) (job ?x (computer programmer))) If a company has many more supervisors than programmers (the usual case), it is better to use the first form rather than the second because the data base must be scanned for each intermediate result (frame) produced by the first clause of the and. The aim of logic programming is to provide the programmer with techniques for decomposing a computational problem into two separate problems: “what” is to be computed, and “how” this should be computed. This is accomplished by selecting a subset of the statements of mathematical logic that is powerful enough to be able to describe anything one might want to compute, yet weak enough to have a controllable procedural interpretation. The intention here is that, on the one hand, a program specified in a logic programming language should be an effective program that can be carried out by a computer. Control (“how” to compute) is effected by using the order of evaluation of the language. We should be able to arrange rived. The method of inference represented by rule applications is modus ponens, the familiar method of inference that says that if A is true and A implies B is true, then we may conclude that B is true. 698 the order of clauses and the order of subgoals within each clause so that the computation is done in an order deemed to be effective and efficient. At the same time, we should be able to view the result of the computation (“what” to compute) as a simple consequence of the laws of logic. Our query language can be regarded as just such a procedurally interpretable subset of mathematical logic. An assertion represents a simple fact (an atomic proposition). A rule represents the implication that the rule conclusion holds for those cases where the rule body holds. A rule has a natural procedural interpretation: To establish the conclusion of the rule, establish the body of the rule. Rules, therefore, specify computations. However, because rules can also be regarded as statements of mathematical logic, we can justify any “inference” accomplished by a logic program by asserting that the same result could be obtained by working entirely within mathematical logic.76 76 We must qualify this statement by agreeing that, in speaking of the “inference” ac- complished by a logic program, we assume that the computation terminates. Unfortunately, even this qualified statement is false for our implementation of the query language (and also false for programs in Prolog and most other current logic programming languages) because of our use of not and lisp-value. As we will describe below, the not implemented in the query language is not always consistent with the not of mathematical logic, and lisp-value introduces additional complications. We could implement a language consistent with mathematical logic by simply removing not and lisp-value from the language and agreeing to write programs using only simple queries, and, and or. However, this would greatly restrict the expressive power of the language. One of the major concerns of research in logic programming is to find ways to achieve more consistency with mathematical logic without unduly sacrificing expressive power. Infinite loops A consequence of the procedural interpretation of logic programs is that it is possible to construct hopelessly inefficient programs for solving certain problems. An extreme case of inefficiency occurs when the system falls into infinite loops in making deductions. As a simple example, suppose we are setting up a data base of famous marriages, including (assert! (married Minnie If we now ask (married Mickey ?who) we will get no response, because the system doesn’t know that if A is married to B , then B is married to A . So we assert the rule (assert! (rule (married ?x ?y) (married ?y ?x))) and again query (married Mickey ?who) Unfortunately, this will drive the system into an infinite loop, as follows: • The system finds that the married rule is applicable; that is, the rule conclusion (married ?x ?y) successfully unifies with the query pattern (married Mickey ?who) to produce a frame in which ?x is bound to Mickey and ?y is bound to ?who. So the interpreter proceeds to evaluate the rule body (married ?y ?x) in this frame—in effect, to process the query (married ?who Mickey). • One answer appears directly as an assertion in the data base: (married Minnie Mickey). • The married rule is also applicable, so the interpreter again evaluates the rule body, which this time is equivalent to (married Mickey ?who). 700 The system is now in an infinite loop. Indeed, whether the system will find the simple answer (married Minnie Mickey) before it goes into the loop depends on implementation details concerning the order in which the system checks the items in the data base. This is a very simple example of the kinds of loops that can occur. Collections of interrelated rules can lead to loops that are much harder to anticipate, and the appearance of a loop can depend on the order of clauses in an and (see Exercise 4.64) or on lowlevel details concerning the order in which the system processes Problems with not Another quirk in the query system concerns not. Given the data base of Section 4.4.1, consider the following two queries: (and (supervisor ?x ?y) (not (job ?x (computer programmer)))) (and (not (job ?x (computer programmer))) 77 This is not a problem of the logic but one of the procedural interpretation of the logic provided by our interpreter. We could write an interpreter that would not fall into a loop here. For example, we could enumerate all the proofs derivable from our assertions and our rules in a breadth-first rather than a depth-first order. However, such a system makes it more difficult to take advantage of the order of deductions in our programs. One attempt to build sophisticated control into such a program is described in deKleer et al. 1977. Another technique, which does not lead to such serious control problems, is to put in special knowledge, such as detectors for particular kinds of loops (Exercise 4.67). However, there can be no general scheme for reliably preventing a system from going down infinite paths in performing deductions. Imagine a diabolical rule of the form “To show P (x ) is true, show that P ( f (x )) is true,” for some suitably chosen function f . 701 (supervisor ?x ?y)) These two queries do not produce the same result. The first query begins by finding all entries in the data base that match (supervisor ?x ?y), and then filters the resulting frames by removing the ones in which the value of ?x satisfies (job ?x (computer programmer)). The second query begins by filtering the incoming frames to remove those that can satisfy (job ?x (computer programmer)). Since the only incoming frame is empty, it checks the data base to see if there are any patterns that satisfy (job ?x (computer programmer)). Since there generally are entries of this form, the not clause filters out the empty frame and returns an empty stream of frames. Consequently, the entire compound query returns an empty stream. The trouble is that our implementation of not really is meant to serve as a filter on values for the variables. If a not clause is processed with a frame in which some of the variables remain unbound (as does ?x in the example above), the system will produce unexpected results. Similar problems occur with the use of lisp-value—the Lisp predicate can’t work if some of its arguments are unbound. See Exercise 4.77. There is also a much more serious way in which the not of the query language differs from the not of mathematical logic. In logic, we interpret the statement “not P ” to mean that P is not true. In the query system, however, “not P ” means that P is not deducible from the knowledge in the data base. For example, given the personnel data base of Section 4.4.1, the system would happily deduce all sorts of not statements, such as that Ben Bitdiddle is not a baseball fan, that it is not raining outside, and that 702 2 + 2 is not 4.78 In other words, the not of logic programming languages reflects the so-called closed world assumption that all relevant information has been included in the data base.79 Exercise 4.64: Louis Reasoner mistakenly deletes the outrankedby rule (Section 4.4.1) from the data base. When he realizes this, he quickly reinstalls it. Unfortunately, he makes a slight change in the rule, and types it in as (rule (outranked-by ?staff-person ?boss) (or (supervisor ?staff-person ?boss) (and (outranked-by ?middle-manager ?boss) (supervisor ?staff-person ?middle-manager)))) Just after Louis types this information into the system, DeWitt Aull comes by to find out who outranks Ben Bitdiddle. He issues the query (outranked-by (Bitdiddle Ben) ?who) After answering, the system goes into an infinite loop. Explain why. Exercise 4.65: Cy D. Fect, looking forward to the day when he will rise in the organization, gives a query to find all the wheels (using the wheel rule of Section 4.4.1): 78 Consider the query (not (baseball-fan (Bitdiddle Ben))). The system finds that (baseball-fan (Bitdiddle Ben)) is not in the data base, so the empty frame does not satisfy the pattern and is not filtered out of the initial stream of frames. The result of the query is thus the empty frame, which is used to instantiate the input query to produce (not (baseball-fan (Bitdiddle Ben))). 79 A discussion and justification of this treatment of not can be found in the article Clark (1978). 703 (wheel ?who) To his surprise, the system responds ;;; Query results: (wheel (Warbucks Oliver)) (wheel (Bitdiddle Ben)) (wheel (Warbucks Oliver)) (wheel (Warbucks Oliver)) (wheel (Warbucks Oliver)) Why is Oliver Warbucks listed four times? Exercise 4.66: Ben has been generalizing the query system to provide statistics about the company. For example, to find the total salaries of all the computer programmers one will be able to say (sum ?amount (and (job ?x (computer programmer)) (salary ?x ?amount))) In general, Ben’s new system allows expressions of the form (accumulation-function 〈variable〉 〈query pattern〉) where accumulation-function can be things like sum, average, or maximum. Ben reasons that it should be a cinch to implement this. He will simply feed the query pattern to qeval. This will produce a stream of frames. He will then pass this stream through a mapping function that extracts the value of the designated variable from each frame in the stream and feed the resulting stream of values to the accumulation function. Just as Ben completes the implementation and is about to try it out, Cy walks by, still puzzling 704 over the wheel query result in Exercise 4.65. When Cy shows Ben the system’s response, Ben groans, “Oh, no, my simple accumulation scheme won’t work!” What has Ben just realized? Outline a method he can use to salvage the situation. Exercise 4.67: Devise a way to install a loop detector in the query system so as to avoid the kinds of simple loops illustrated in the text and in Exercise 4.64. The general idea is that the system should maintain some sort of history of its current chain of deductions and should not begin processing a query that it is already working on. Describe what kind of information (patterns and frames) is included in this history, and how the check should be made. (After you study the details of the query-system implementation in Section 4.4.4, you may want to modify the system to include your loop detector.) Exercise 4.68: Define rules to implement the reverse operation of Exercise 2.18, which returns a list containing the same elements as a given list in reverse order. (Hint: Use append-to-form.) Can your rules answer both (reverse (1 2 3) ?x) and (reverse ?x (1 2 3)) ? Exercise 4.69: Beginning with the data base and the rules you formulated in Exercise 4.63, devise a rule for adding “greats” to a grandson relationship. This should enable the system to deduce that Irad is the great-grandson of Adam, or that Jabal and Jubal are the great-great-great-great-great-grandsons of Adam. (Hint: Represent the fact about Irad, for example, as ((great grandson) Adam Irad). Write rules that determine if a list ends in the word 705 grandson. Use this to express a rule that allows one to derive the relationship ((great . ?rel) ?x ?y), where ?rel is a list ending in grandson.) Check your rules on queries such as ((great grandson) ?g ?ggs) and (?relationship Adam Irad). 4.4.4 Implementing the Query System Section 4.4.2 described how the query system works. Now we fill in the details by presenting a complete implementation of the system. 4.4.4.1 The Driver Loop and Instantiation The driver loop for the query system repeatedly reads input expressions. If the expression is a rule or assertion to be added to the data base, then the information is added. Otherwise the expression is assumed to be a query. The driver passes this query to the evaluator qeval together with an initial frame stream consisting of a single empty frame. The result of the evaluation is a stream of frames generated by satisfying the query with variable values found in the data base. These frames are used to form a new stream consisting of copies of the original query in which the variables are instantiated with values supplied by the stream of frames, and this final stream is printed at the terminal: (define input-prompt ";;; Query input:") (define output-prompt ";;; Query results:") (define (query-driver-loop) (prompt-for-input input-prompt) (let ((q (query-syntax-process (read)))) (cond ((assertion-to-be-added? q) (add-rule-or-assertion! (add-assertion-body q)) 706 (newline) (display "Assertion added to data base.") (query-driver-loop)) (else (newline) (display output-prompt) (display-stream (stream-map (lambda (frame) (instantiate q frame (lambda (v f) (contract-question-mark v)))) (qeval q (singleton-stream ’())))) (query-driver-loop))))) Here, as in the other evaluators in this chapter, we use an abstract syntax for the expressions of the query language. The implementation of the expression syntax, including the predicate assertion-to-be-added? and the selector add-assertion-body, is given in Section 4.4.4.7. Add-rule-or-assertion! is defined in Section 4.4.4.5. Before doing any processing on an input expression, the driver loop transforms it syntactically into a form that makes the processing more efficient. This involves changing the representation of pattern variables. When the query is instantiated, any variables that remain unbound are transformed back to the input representation before being printed. These transformations are performed by the two procedures query-syntax-process and contract-question-mark (Section 4.4.4.7). 707 To instantiate an expression, we copy it, replacing any variables in the expression by their values in a given frame. The values are themselves instantiated, since they could contain variables (for example, if ?x in exp is bound to ?y as the result of unification and ?y is in turn bound to 5). The action to take if a variable cannot be instantiated is given by a procedural argument to instantiate. (define (instantiate exp frame unbound-var-handler) (define (copy exp) (cond ((var? exp) (let ((binding (binding-in-frame exp frame))) (if binding (copy (binding-value binding)) (unbound-var-handler exp frame)))) ((pair? exp) (cons (copy (car exp)) (copy (cdr exp)))) (else exp))) (copy exp)) The procedures that manipulate bindings are defined in Section 4.4.4.8. 4.4.4.2 The Evaluator The qeval procedure, called by the query-driver-loop, is the basic evaluator of the query system. It takes as inputs a query and a stream of frames, and it returns a stream of extended frames. It identifies special forms by a data-directed dispatch using get and put, just as we did in implementing generic operations in Chapter 2. Any query that is not identified as a special form is assumed to be a simple query, to be processed by simple-query. (define (qeval query frame-stream) 708 (let ((qproc (get (type query) ’qeval))) (if qproc (qproc (contents query) frame-stream) (simple-query query frame-stream)))) Type and contents, defined in Section 4.4.4.7, implement the abstract syntax of the special forms. Simple queries The simple-query procedure handles simple queries. It takes as arguments a simple query (a pattern) together with a stream of frames, and it returns the stream formed by extending each frame by all data-base matches of the query. (define (simple-query query-pattern frame-stream) (stream-flatmap (lambda (frame) (stream-append-delayed (find-assertions query-pattern frame) (delay (apply-rules query-pattern frame)))) frame-stream)) For each frame in the input stream, we use find-assertions (Section 4.4.4.3) to match the pattern against all assertions in the data base, producing a stream of extended frames, and we use apply-rules (Section 4.4.4.4) to apply all possible rules, producing another stream of extended frames. These two streams are combined (using stream-append-delayed, Section 4.4.4.6) to make a stream of all the ways that the given pattern can be satisfied consistent with the original frame (see Exercise 4.71). The streams for the individual input frames are combined using stream-flatmap (Section 709 4.4.4.6) to form one large stream of all the ways that any of the frames in the original input stream can be extended to produce a match with the given pattern. Compound queries And queries are handled as illustrated in Figure 4.5 by the conjoin proce- dure. Conjoin takes as inputs the conjuncts and the frame stream and returns the stream of extended frames. First, conjoin processes the stream of frames to find the stream of all possible frame extensions that satisfy the first query in the conjunction. Then, using this as the new frame stream, it recursively applies conjoin to the rest of the queries. (define (conjoin conjuncts frame-stream) (if (empty-conjunction? conjuncts) frame-stream (conjoin (rest-conjuncts conjuncts) (qeval (first-conjunct conjuncts) frame-stream)))) The expression (put ’and ’qeval conjoin) sets up qeval to dispatch to conjoin when an and form is encountered. Or queries are handled similarly, as shown in Figure 4.6. The output streams for the various disjuncts of the or are computed separately and merged using the interleave-delayed procedure from Section 4.4.4.6. (See Exercise 4.71 and Exercise 4.72.) (define (disjoin disjuncts frame-stream) (if (empty-disjunction? disjuncts) the-empty-stream 710 (interleave-delayed (qeval (first-disjunct disjuncts) frame-stream) (delay (disjoin (rest-disjuncts disjuncts) frame-stream))))) (put ’or ’qeval disjoin) The predicates and selectors for the syntax of conjuncts and disjuncts are given in Section 4.4.4.7. Filters Not is handled by the method outlined in Section 4.4.2. We attempt to ex- tend each frame in the input stream to satisfy the query being negated, and we include a given frame in the output stream only if it cannot be extended. (define (negate operands frame-stream) (stream-flatmap (lambda (frame) (if (stream-null? (qeval (negated-query operands) (singleton-stream frame))) (singleton-stream frame) the-empty-stream)) frame-stream)) (put ’not ’qeval negate) Lisp-value is a filter similar to not. Each frame in the stream is used to instantiate the variables in the pattern, the indicated predicate is applied, and the frames for which the predicate returns false are filtered out of the input stream. An error results if there are unbound pattern variables. 711 (define (lisp-value call frame-stream) (stream-flatmap (lambda (frame) (if (execute (instantiate call frame (lambda (v f) (error "Unknown pat var - LISP-VALUE" v)))) (singleton-stream frame) the-empty-stream)) frame-stream)) (put ’lisp-value ’qeval lisp-value) Execute, which applies the predicate to the arguments, must eval the predicate expression to get the procedure to apply. However, it must not evaluate the arguments, since they are already the actual arguments, not expressions whose evaluation (in Lisp) will produce the arguments. Note that execute is implemented using eval and apply from the underlying Lisp system. (define (execute exp) (apply (eval (predicate exp) user-initial-environment) (args exp))) The always-true special form provides for a query that is always satisfied. It ignores its contents (normally empty) and simply passes through all the frames in the input stream. Always-true is used by the rule-body se- 712 lector (Section 4.4.4.7) to provide bodies for rules that were defined without bodies (that is, rules whose conclusions are always satisfied). (define (always-true ignore frame-stream) frame-stream) (put ’always-true ’qeval always-true) The selectors that define the syntax of not and lisp-value are given in Section 4.4.4.7. 4.4.4.3 Finding Assertions by Pattern Matching Find-assertions, called by simple-query (Section 4.4.4.2), takes as in- put a pattern and a frame. It returns a stream of frames, each extending the given one by a data-base match of the given pattern. It uses fetchassertions (Section 4.4.4.5) to get a stream of all the assertions in the data base that should be checked for a match against the pattern and the frame. The reason for fetch-assertions here is that we can often apply simple tests that will eliminate many of the entries in the data base from the pool of candidates for a successful match. The system would still work if we eliminated fetch-assertions and simply checked a stream of all assertions in the data base, but the computation would be less efficient because we would need to make many more calls to the matcher. (define (find-assertions pattern frame) (stream-flatmap (lambda (datum) (check-an-assertion datum pattern frame)) (fetch-assertions pattern frame))) Check-an-assertion takes as arguments a pattern, a data object (asser- tion), and a frame and returns either a one-element stream containing the extended frame or the-empty-stream if the match fails. 713 (define (check-an-assertion assertion query-pat query-frame) (let ((match-result (pattern-match query-pat assertion query-frame))) (if (eq? match-result ’failed) the-empty-stream (singleton-stream match-result)))) The basic pattern matcher returns either the symbol failed or an extension of the given frame. The basic idea of the matcher is to check the pattern against the data, element by element, accumulating bindings for the pattern variables. If the pattern and the data object are the same, the match succeeds and we return the frame of bindings accumulated so far. Otherwise, if the pattern is a variable we extend the current frame by binding the variable to the data, so long as this is consistent with the bindings already in the frame. If the pattern and the data are both pairs, we (recursively) match the car of the pattern against the car of the data to produce a frame; in this frame we then match the cdr of the pattern against the cdr of the data. If none of these cases are applicable, the match fails and we return the symbol failed. (define (pattern-match pat dat frame) (cond ((eq? frame ’failed) ’failed) ((equal? pat dat) frame) ((var? pat) (extend-if-consistent pat dat frame)) ((and (pair? pat) (pair? dat)) (pattern-match (cdr pat) (cdr dat) (pattern-match (car pat) (car dat) frame))) 714 (else ’failed))) Here is the procedure that extends a frame by adding a new binding, if this is consistent with the bindings already in the frame: (define (extend-if-consistent var dat frame) (let ((binding (binding-in-frame var frame))) (if binding (pattern-match (binding-value binding) dat frame) (extend var dat frame)))) If there is no binding for the variable in the frame, we simply add the binding of the variable to the data. Otherwise we match, in the frame, the data against the value of the variable in the frame. If the stored value contains only constants, as it must if it was stored during pattern matching by extend-if-consistent, then the match simply tests whether the stored and new values are the same. If so, it returns the unmodified frame; if not, it returns a failure indication. The stored value may, however, contain pattern variables if it was stored during unification (see Section 4.4.4.4). The recursive match of the stored pattern against the new data will add or check bindings for the variables in this pattern. For example, suppose we have a frame in which ?x is bound to (f ?y) and ?y is unbound, and we wish to augment this frame by a binding of ?x to (f b). We look up ?x and find that it is bound to (f ?y). This leads us to match (f ?y) against the proposed new value (f b) in the same frame. Eventually this match extends the frame by adding a binding of ?y to b. ?X remains bound to (f ?y). We never modify a stored binding and we never store more than one binding for a given variable. The procedures used by extend-if-consistent to manipulate bindings are defined in Section 4.4.4.8. Patterns with dotted tails If a pattern contains a dot followed by a pattern variable, the pattern variable matches the rest of the data list (rather than the next element of the data list), just as one would expect with the dotted-tail notation described in Exercise 2.20. Although the pattern matcher we have just implemented doesn’t look for dots, it does behave as we want. This is because the Lisp read primitive, which is used by query-driver-loop to read the query and represent it as a list structure, treats dots in a special way. When read sees a dot, instead of making the next item be the next element of a list (the car of a cons whose cdr will be the rest of the list) it makes the next item be the cdr of the list structure. For example, the list structure produced by read for the pattern (computer ?type) could be constructed by evaluating the expression (cons ’computer (cons ’?type ’())), and that for (computer . ?type) could be constructed by evaluating the expression (cons ’computer ’?type). Thus, as pattern-match recursively compares cars and cdrs of a data list and a pattern that had a dot, it eventually matches the variable after the dot (which is a cdr of the pattern) against a sublist of the data list, binding the variable to that list. For example, matching the pattern (computer . ?type) against (computer programmer trainee) will match ?type against the list (programmer trainee). 4.4.4.4 Rules and Unification Apply-rules is the rule analog of find-assertions (Section 4.4.4.3). It takes as input a pattern and a frame, and it forms a stream of extension frames by applying rules from the data base. Stream-flatmap maps apply-a-rule down the stream of possibly applicable rules 716 by fetch-rules, Section 4.4.4.5) and combines the resulting streams of frames. (define (apply-rules pattern frame) (stream-flatmap (lambda (rule) (apply-a-rule rule pattern frame)) (fetch-rules pattern frame))) Apply-a-rule applies rules using the method outlined in Section 4.4.2. It first augments its argument frame by unifying the rule conclusion with the pattern in the given frame. If this succeeds, it evaluates the rule body in this new frame. Before any of this happens, however, the program renames all the variables in the rule with unique new names. The reason for this is to prevent the variables for different rule applications from becoming confused with each other. For instance, if two rules both use a variable named ?x, then each one may add a binding for ?x to the frame when it is applied. These two ?x’s have nothing to do with each other, and we should not be fooled into thinking that the two bindings must be consistent. Rather than rename variables, we could devise a more clever environment structure; however, the renaming approach we have chosen here is the most straightforward, even if not the most efficient. (See Exercise 4.79.) Here is the apply-a-rule procedure: (define (apply-a-rule rule query-pattern query-frame) (let ((clean-rule (rename-variables-in rule))) (let ((unify-result (unify-match query-pattern (conclusion clean-rule) query-frame))) 717 (if (eq? unify-result ’failed) the-empty-stream (qeval (rule-body clean-rule) (singleton-stream unify-result)))))) The selectors rule-body and conclusion that extract parts of a rule are defined in Section 4.4.4.7. We generate unique variable names by associating a unique identifier (such as a number) with each rule application and combining this identifier with the original variable names. For example, if the rule-application identifier is 7, we might change each ?x in the rule to ?x-7 and each ?y in the rule to ?y-7. (Make-new-variable and new-rule-application-id are included with the syntax procedures in Section 4.4.4.7.) (define (rename-variables-in rule) (let ((rule-application-id (new-rule-application-id))) (define (tree-walk exp) (cond ((var? exp) (make-new-variable exp rule-application-id)) ((pair? exp) (cons (tree-walk (car exp)) (tree-walk (cdr exp)))) (else exp))) (tree-walk rule))) The unification algorithm is implemented as a procedure that takes as inputs two patterns and a frame and returns either the extended frame or the symbol failed. The unifier is like the pattern matcher except that it is symmetrical—variables are allowed on both sides of the match. Unify-match is basically the same as pattern-match, except that there is 718 extra code (marked “***” below) to handle the case where the object on the right side of the match is a variable. (define (unify-match p1 p2 frame) (cond ((eq? frame ’failed) ’failed) ((equal? p1 p2) frame) ((var? p1) (extend-if-possible p1 p2 frame)) ((var? p2) (extend-if-possible p2 p1 frame)) ; *** ((and (pair? p1) (pair? p2)) (unify-match (cdr p1) (cdr p2) (unify-match (car p1) (car p2) frame))) (else ’failed))) In unification, as in one-sided pattern matching, we want to accept a proposed extension of the frame only if it is consistent with existing bindings. The procedure extend-if-possible used in unification is the same as the extend-if-consistent used in pattern matching except for two special checks, marked “***” in the program below. In the first case, if the variable we are trying to match is not bound, but the value we are trying to match it with is itself a (different) variable, it is necessary to check to see if the value is bound, and if so, to match its value. If both parties to the match are unbound, we may bind either to the other. The second check deals with attempts to bind a variable to a pattern that includes that variable. Such a situation can occur whenever a variable is repeated in both patterns. Consider, for example, unifying the two patterns (?x ?x) and (?y <expression involving ?y>) in a frame where both ?x 719 and ?y are unbound. First ?x is matched against ?y, making a binding of ?x to ?y. Next, the same ?x is matched against the given expression involving ?y. Since ?x is already bound to ?y, this results in matching ?y against the expression. If we think of the unifier as finding a set of values for the pattern variables that make the patterns the same, then these patterns imply instructions to find a ?y such that ?y is equal to the expression involving ?y. There is no general method for solving such equations, so we reject 720 such bindings; these cases are recognized by the predicate depends-on?.80 On the other hand, we do not want to reject attempts to bind a variable to itself. For example, consider unifying (?x ?x) and (?y ?y). The second attempt to bind ?x to ?y matches ?y (the stored value of ?x) against ?y (the new value of ?x). This is taken care of by the equal? clause of unify-match. 80 In general, unifying ?y with an expression involving ?y would require our being able to find a fixed point of the equation ?y = <expression involving ?y>. It is sometimes possible to syntactically form an expression that appears to be the solution. For example, ?y = (f ?y) seems to have the fixed point (f (f (f ... ))), which we can produce by beginning with the expression (f ?y) and repeatedly substituting (f ?y) for ?y. Unfortunately, not every such equation has a meaningful fixed point. The issues that arise here are similar to the issues of manipulating infinite series in mathematics. For example, we know that 2 is the solution to the equation y = 1 + y /2. Beginning with the expression 1 + y /2 and repeatedly substituting 1 + y /2 for y gives y y 1 2= y =1+ =1+ 1+ 2 2 2 which leads to 2=1+ y = ... 4 + ... 2 4 8 However, if we try the same manipulation beginning with the observation that -1 is the solution to the equation y = 1 + 2 y , we obtain −1 = y = 1 + 2 y = 1 + 2(1 + 2 y ) = 1 + 2 + 4 y = . . . which leads to −1 = 1 + 2 + 4 + 8 + . . . Although the formal manipulations used in deriving these two equations are identical, the first result is a valid assertion about infinite series but the second is not. Similarly, for our unification results, reasoning with an arbitrary syntactically constructed expression may lead to errors. 721 (define (extend-if-possible var val frame) (let ((binding (binding-in-frame var frame))) (cond (binding (unify-match (binding-value binding) val frame)) ((var? val) ; *** (let ((binding (binding-in-frame val frame))) (if binding (unify-match var (binding-value binding) frame) (extend var val frame)))) ((depends-on? val var frame) ; *** ’failed) (else (extend var val frame))))) Depends-on? is a predicate that tests whether an expression proposed to be the value of a pattern variable depends on the variable. This must be done relative to the current frame because the expression may contain occurrences of a variable that already has a value that depends on our test variable. The structure of depends-on? is a simple recursive tree walk in which we substitute for the values of variables whenever necessary. (define (depends-on? exp var frame) (define (tree-walk e) (cond ((var? e) (if (equal? var e) true (let ((b (binding-in-frame e frame))) (if b 722 (tree-walk (binding-value b)) false)))) ((pair? e) (or (tree-walk (car e)) (tree-walk (cdr e)))) (else false))) (tree-walk exp)) 4.4.4.5 Maintaining the Data Base One important problem in designing logic programming languages is that of arranging things so that as few irrelevant data-base entries as possible will be examined in checking a given pattern. In our system, in addition to storing all assertions in one big stream, we store all assertions whose cars are constant symbols in separate streams, in a table indexed by symbol. To fetch an assertion that may match a pattern, we first check to see if the car of the pattern is a constant symbol. If so, we return (to be tested using the matcher) all the stored assertions that have the same car. If the pattern’s car is not a constant symbol, we return all the stored assertions. Cleverer methods could also take advantage of information in the frame, or try also to optimize the case where the car of the pattern is not a constant symbol. We avoid building our criteria for indexing (using the car, handling only the case of constant symbols) into the program; instead we call on predicates and selectors that embody our criteria. (define THE-ASSERTIONS the-empty-stream) (define (fetch-assertions pattern frame) (if (use-index? pattern) 723 (get-indexed-assertions pattern) (get-all-assertions))) (define (get-all-assertions) THE-ASSERTIONS) (define (get-indexed-assertions pattern) (get-stream (index-key-of pattern) ’assertion-stream)) Get-stream looks up a stream in the table and returns an empty stream if nothing is stored there. (define (get-stream key1 key2) (let ((s (get key1 key2))) (if s s the-empty-stream))) Rules are stored similarly, using the car of the rule conclusion. Rule conclusions are arbitrary patterns, however, so they differ from assertions in that they can contain variables. A pattern whose car is a constant symbol can match rules whose conclusions start with a variable as well as rules whose conclusions have the same car. Thus, when fetching rules that might match a pattern whose car is a constant symbol we fetch all rules whose conclusions start with a variable as well as those whose conclusions have the same car as the pattern. For this purpose we store all rules whose conclusions start with a variable in a separate stream in our table, indexed by the symbol ?. (define THE-RULES the-empty-stream) (define (fetch-rules pattern frame) (if (use-index? pattern) (get-indexed-rules pattern) 724 (get-all-rules))) (define (get-all-rules) THE-RULES) (define (get-indexed-rules pattern) (stream-append (get-stream (index-key-of pattern) ’rule-stream) (get-stream ’? ’rule-stream))) Add-rule-or-assertion! is used by query-driver-loop to add asser- tions and rules to the data base. Each item is stored in the index, if appropriate, and in a stream of all assertions or rules in the data base. (define (add-rule-or-assertion! assertion) (if (rule? assertion) (add-rule! assertion) (add-assertion! assertion))) (define (add-assertion! assertion) (store-assertion-in-index assertion) (let ((old-assertions THE-ASSERTIONS)) (set! THE-ASSERTIONS (cons-stream assertion old-assertions)) ’ok)) (define (add-rule! rule) (store-rule-in-index rule) (let ((old-rules THE-RULES)) (set! THE-RULES (cons-stream rule old-rules)) 725 ’ok)) To actually store an assertion or a rule, we check to see if it can be indexed. If so, we store it in the appropriate stream. (define (store-assertion-in-index assertion) (if (indexable? assertion) (let ((key (index-key-of assertion))) (let ((current-assertion-stream (get-stream key ’assertion-stream))) (put key ’assertion-stream (cons-stream assertion current-assertion-stream)))))) (define (store-rule-in-index rule) (let ((pattern (conclusion rule))) (if (indexable? pattern) (let ((key (index-key-of pattern))) (let ((current-rule-stream (get-stream key ’rule-stream))) (put key ’rule-stream (cons-stream rule current-rule-stream))))))) The following procedures define how the data-base index is used. A pattern (an assertion or a rule conclusion) will be stored in the table if it starts with a variable or a constant symbol. (define (indexable? pat) 726 (or (constant-symbol? (car pat)) (var? (car pat)))) The key under which a pattern is stored in the table is either ? (if it starts with a variable) or the constant symbol with which it starts. (define (index-key-of pat) (let ((key (car pat))) (if (var? key) ’? key))) The index will be used to retrieve items that might match a pattern if the pattern starts with a constant symbol. (define (use-index? pat) (constant-symbol? (car pat))) Exercise 4.70: What is the purpose of the let bindings in the procedures add-assertion! and add-rule! ? What would be wrong with the following implementation of add-assertion! ? Hint: Recall the definition of the infinite stream of ones in Section 3.5.2: (define ones (cons-stream 1 ones)). (define (add-assertion! assertion) (store-assertion-in-index assertion) (set! THE-ASSERTIONS (cons-stream assertion THE-ASSERTIONS)) ’ok) 4.4.4.6 Stream Operations The query system uses a few stream operations that were not presented in Chapter 3. Stream-append-delayed and interleave-delayed are just like streamappend and interleave (Section 3.5.3), except that they take a delayed 727 argument (like the integral procedure in Section 3.5.4). This postpones looping in some cases (see Exercise 4.71). (define (stream-append-delayed s1 delayed-s2) (if (stream-null? s1) (force delayed-s2) (cons-stream (stream-car s1) (stream-append-delayed (stream-cdr s1) delayed-s2)))) (define (interleave-delayed s1 delayed-s2) (if (stream-null? s1) (force delayed-s2) (cons-stream (stream-car s1) (interleave-delayed (force delayed-s2) (delay (stream-cdr s1)))))) Stream-flatmap, which is used throughout the query evaluator to map a procedure over a stream of frames and combine the resulting streams of frames, is the stream analog of the flatmap procedure introduced for ordinary lists in Section 2.2.3. Unlike ordinary flatmap, however, we accumulate the streams with an interleaving process, rather than simply appending them (see Exercise 4.72 and Exercise 4.73). (define (stream-flatmap proc s) (flatten-stream (stream-map proc s))) (define (flatten-stream stream) (if (stream-null? stream) 728 the-empty-stream (interleave-delayed (stream-car stream) (delay (flatten-stream (stream-cdr stream)))))) The evaluator also uses the following simple procedure to generate a stream consisting of a single element: (define (singleton-stream x) (cons-stream x the-empty-stream)) 4.4.4.7 Query Syntax Procedures Type and contents, used by qeval (Section 4.4.4.2), specify that a spe- cial form is identified by the symbol in its car. They are the same as the type-tag and contents procedures in Section 2.4.2, except for the error message. (define (type exp) (if (pair? exp) (car exp) (error "Unknown expression TYPE" exp))) (define (contents exp) (if (pair? exp) (cdr exp) (error "Unknown expression CONTENTS" exp))) The following procedures, used by query-driver-loop (in Section 4.4.4.1), specify that rules and assertions are added to the data base by expressions of the form (assert! ): (define (assertion-to-be-added? exp) 729 (eq? (type exp) ’assert!)) (define (add-assertion-body exp) (car (contents exp))) Here are the syntax definitions for the and, or, not, and lisp-value special forms (Section 4.4.4.2): (define (empty-conjunction? exps) (null? exps)) (define (first-conjunct exps) (car exps)) (define (rest-conjuncts exps) (cdr exps)) (define (empty-disjunction? exps) (null? exps)) (define (first-disjunct exps) (car exps)) (define (rest-disjuncts exps) (cdr exps)) (define (negated-query exps) (car exps)) (define (predicate exps) (car exps)) (define (args exps) (cdr exps)) The following three procedures define the syntax of rules: (define (rule? statement) (tagged-list? statement ’rule)) (define (conclusion rule) (cadr rule)) (define (rule-body rule) (if (null? (cddr rule)) ’(always-true) 730 (caddr rule))) Query-driver-loop (Section 4.4.4.1) calls query-syntax-process to transform pattern variables in the expression, which have the form ?symbol, into the internal format (? symbol). That is to say, a pattern such as (job ?x ?y) is actually represented internally by the system as (job (? x) (? y)). This increases the efficiency of query processing, since it means that the system can check to see if an expression is a pattern variable by checking whether the car of the expression is the symbol ?, rather than having to extract characters from the symbol. The syntax transformation is accomplished by the following procedure:81 (define (query-syntax-process exp) (map-over-symbols expand-question-mark exp)) (define (map-over-symbols proc exp) (cond ((pair? exp) (cons (map-over-symbols proc (car exp)) (map-over-symbols proc (cdr exp)))) ((symbol? exp) (proc exp)) 81 Most Lisp systems give the user the ability to modify the ordinary read to perform such transformations by defining reader macro characters. Quoted expressions are already handled in this way: The reader automatically translates ’expression into (quote expression) before the evaluator sees it. We could arrange for ?expression to be transformed into (? expression) in the same way; however, for the sake of clarity we have included the transformation procedure here explicitly. Expand-question-mark and contract-question-mark use several procedures with string in their names. These are Scheme primitives. 731 (else exp))) (define (expand-question-mark symbol) (let ((chars (symbol->string symbol))) (if (string=? (substring chars 0 1) "?") (list ’? (string->symbol (substring chars 1 (string-length chars)))) symbol))) Once the variables are transformed in this way, the variables in a pattern are lists starting with ?, and the constant symbols (which need to be recognized for data-base indexing, Section 4.4.4.5) are just the symbols. (define (var? exp) (tagged-list? exp ’?)) (define (constant-symbol? exp) (symbol? exp)) Unique variables are constructed during rule application (in Section 4.4.4.4) by means of the following procedures. The unique identifier for a rule application is a number, which is incremented each time a rule is applied. (define rule-counter 0) (define (new-rule-application-id) (set! rule-counter (+ 1 rule-counter)) rule-counter) (define (make-new-variable var rule-application-id) 732 (cons ’? (cons rule-application-id (cdr var)))) When query-driver-loop instantiates the query to print the answer, it converts any unbound pattern variables back to the right form for printing, using (define (contract-question-mark variable) (string->symbol (string-append "?" (if (number? (cadr variable)) (string-append (symbol->string (caddr variable)) "-" (number->string (cadr variable))) (symbol->string (cadr variable)))))) 4.4.4.8 Frames and Bindings Frames are represented as lists of bindings, which are variable-value pairs: (define (make-binding variable value) (cons variable value)) (define (binding-variable binding) (car binding)) (define (binding-value binding) (cdr binding)) (define (binding-in-frame variable frame) (assoc variable frame)) 733 (define (extend variable value frame) (cons (make-binding variable value) frame)) Exercise 4.71: Louis Reasoner wonders why the simple-query and disjoin procedures (Section 4.4.4.2) are implemented using explicit delay operations, rather than being defined as follows: (define (simple-query query-pattern frame-stream) (stream-flatmap (lambda (frame) (stream-append (find-assertions query-pattern frame) (apply-rules query-pattern frame))) frame-stream)) (define (disjoin disjuncts frame-stream) (if (empty-disjunction? disjuncts) the-empty-stream (interleave (qeval (first-disjunct disjuncts) frame-stream) (disjoin (rest-disjuncts disjuncts) frame-stream)))) Can you give examples of queries where these simpler definitions would lead to undesirable behavior? Exercise 4.72: Why do disjoin and stream-flatmap interleave the streams rather than simply append them? Give examples that illustrate why interleaving works better. (Hint: Why did we use interleave in Section 3.5.3?) Exercise 4.73: Why does flatten-stream use delay explicitly? What would be wrong with defining it as follows: (define (flatten-stream stream) (if (stream-null? stream) 734 the-empty-stream (interleave (stream-car stream) (flatten-stream (stream-cdr stream))))) Exercise 4.74: Alyssa P. Hacker proposes to use a simpler version of stream-flatmap in negate, lisp-value, and find-assertions. She observes that the procedure that is mapped over the frame stream in these cases always produces either the empty stream or a singleton stream, so no interleaving is needed when combining these streams. a. Fill in the missing expressions in Alyssa’s program. (define (simple-stream-flatmap proc s) (simple-flatten (stream-map proc s))) (define (simple-flatten stream) (stream-map 〈??〉 (stream-filter 〈??〉 stream))) b. Does the query system’s behavior change if we change it in this way? Exercise 4.75: Implement for the query language a new special form called unique. Unique should succeed if there is precisely one item in the data base satisfying a specified query. For example, (unique (job ?x (computer wizard))) should print the one-item stream (unique (job (Bitdiddle Ben) (computer wizard))) since Ben is the only computer wizard, and 735 (unique (job ?x (computer programmer))) should print the empty stream, since there is more than one computer programmer. Moreover, (and (job ?x ?j) (unique (job ?anyone ?j))) should list all the jobs that are filled by only one person, and the people who fill them. There are two parts to implementing unique. The first is to write a procedure that handles this special form, and the second is to make qeval dispatch to that procedure. The second part is trivial, since qeval does its dispatching in a data-directed way. If your procedure is called uniquely-asserted, all you need to do is (put ’unique ’qeval uniquely-asserted) and qeval will dispatch to this procedure for every query whose type (car) is the symbol unique. The real problem is to write the procedure uniquely-asserted. This should take as input the contents (cdr) of the unique query, together with a stream of frames. For each frame in the stream, it should use qeval to find the stream of all extensions to the frame that satisfy the given query. Any stream that does not have exactly one item in it should be eliminated. The remaining streams should be passed back to be accumulated into one big stream that is the result of the unique query. This is similar to the implementation of the not special form. Test your implementation by forming a query that lists all people who supervise precisely one person. 736 Exercise 4.76: Our implementation of and as a series combination of queries (Figure 4.5) is elegant, but it is inefficient because in processing the second query of the and we must scan the data base for each frame produced by the first query. If the data base has n elements, and a typical query produces a number of output frames proportional to n (say n /k ), then scanning the data base for each frame produced by the first query will require n 2/k calls to the pattern matcher. Another approach would be to process the two clauses of the and separately, then look for all pairs of output frames that are compatible. If each query produces n /k output frames, then this means that we must perform n 2/k 2 compatibility checks—a factor of k fewer than the number of matches required in our current method. Devise an implementation of and that uses this strategy. You must implement a procedure that takes two frames as inputs, checks whether the bindings in the frames are compatible, and, if so, produces a frame that merges the two sets of bindings. This operation is similar to unification. Exercise 4.77: In Section 4.4.3 we saw that not and lisp-value can cause the query language to give “wrong” answers if these filtering operations are applied to frames in which variables are unbound. Devise a way to fix this shortcoming. One idea is to perform the filtering in a “delayed” manner by appending to the frame a “promise” to filter that is fulfilled only when enough variables have been bound to make the operation possible. We could wait to perform filtering until all other operations have been performed. However, for efficiency’s sake, we would like to perform filtering as 737 soon as possible so as to cut down on the number of intermediate frames generated. Exercise 4.78: Redesign the query language as a nondeterministic program to be implemented using the evaluator of Section 4.3, rather than as a stream process. In this approach, each query will produce a single answer (rather than the stream of all answers) and the user can type try-again to see more answers. You should find that much of the mechanism we built in this section is subsumed by nondeterministic search and backtracking. You will probably also find, however, that your new query language has subtle differences in behavior from the one implemented here. Can you find examples that illustrate this difference? Exercise 4.79: When we implemented the Lisp evaluator in Section 4.1, we saw how to use local environments to avoid name conflicts between the parameters of procedures. For example, in evaluating (define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (sum-of-squares 3 4) there is no confusion between the x in square and the x in sum-of-squares, because we evaluate the body of each procedure in an environment that is specially constructed to contain bindings for the local variables. In the query system, we used a different strategy to avoid name conflicts in applying rules. Each 738 time we apply a rule we rename the variables with new names that are guaranteed to be unique. The analogous strategy for the Lisp evaluator would be to do away with local environments and simply rename the variables in the body of a procedure each time we apply the procedure. Implement for the query language a rule-application method that uses environments rather than renaming. See if you can build on your environment structure to create constructs in the query language for dealing with large systems, such as the rule analog of block-structured procedures. Can you relate any of this to the problem of making deductions in a context (e.g., “If I supposed that P were true, then I would be able to deduce A and B .”) as a method of problem solving? (This problem is open-ended. A good answer is probably worth a Ph.D.) 5 Computing with Register Machines My aim is to show that the heavenly machine is not a kind of divine, live being, but a kind of clockwork (and he who believes that a clock has soul attributes the maker’s glory to the work), insofar as nearly all the manifold motions are caused by a most simple and material force, just as all motions of the clock are caused by a single weight. —Johannes Kepler (letter to Herwart von Hohenburg, 1605) We began this book by studying processes and by describing processes in terms of procedures written in Lisp. To explain the meanings of these procedures, we used a succession of models of evaluation: the substitution model of Chapter 1, the environment model of Chapter 3, and the metacircular evaluator of Chapter 4. Our examination of the metacircular evaluator, in particular, dispelled much of the mystery of how Lisp-like languages are interpreted. But even the metacircular evaluator leaves important questions unanswered, because it fails to elucidate the mechanisms of control in a Lisp system. For instance, the evaluator does not explain how the evaluation of a subexpression manages to return a value to the expression that uses this value, nor does the evaluator explain how some recursive procedures generate iterative processes (that is, are evaluated using constant space) whereas other recursive procedures generate recursive processes. These questions remain unanswered because the metacircular evaluator is itself a Lisp program and hence inherits the control structure of the underlying Lisp system. In order to provide a more complete description of the control structure of the Lisp evaluator, we must work at a more primitive level than Lisp itself. 740 In this chapter we will describe processes in terms of the step-by-step operation of a traditional computer. Such a computer, or register machine, sequentially executes instructions that manipulate the contents of a fixed set of storage elements called registers. A typical register-machine instruction applies a primitive operation to the contents of some registers and assigns the result to another register. Our descriptions of processes executed by register machines will look very much like “machine-language” programs for traditional computers. However, instead of focusing on the machine language of any particular computer, we will examine several Lisp procedures and design a specific register machine to execute each procedure. Thus, we will approach our task from the perspective of a hardware architect rather than that of a machine-language computer programmer. In designing register machines, we will develop mechanisms for implementing important programming constructs such as recursion. We will also present a language for describing designs for register machines. In Section 5.2 we will implement a Lisp program that uses these descriptions to simulate the machines we design. Most of the primitive operations of our register machines are very simple. For example, an operation might add the numbers fetched from two registers, producing a result to be stored into a third register. Such an operation can be performed by easily described hardware. In order to deal with list structure, however, we will also use the memory operations car, cdr, and cons, which require an elaborate storage-allocation mechanism. In Section 5.3 we study their implementation in terms of more elementary operations. In Section 5.4, after we have accumulated experience formulating simple procedures as register machines, we will design a machine that carries out 741 the algorithm described by the metacircular evaluator of Section 4.1. This will fill in the gap in our understanding of how Scheme expressions are interpreted, by providing an explicit model for the mechanisms of control in the evaluator. In Section 5.5 we will study a simple compiler that translates Scheme programs into sequences of instructions that can be executed directly with the registers and operations of the evaluator register machine. 5.1 Designing Register Machines To design a register machine, we must design its data paths (registers and operations) and the controller that sequences these operations. To illustrate the design of a simple register machine, let us examine Euclid’s Algorithm, which is used to compute the greatest common divisor (GCD) of two integers. As we saw in Section 1.2.5, Euclid’s Algorithm can be carried out by an iterative process, as specified by the following procedure: (define (gcd a b) (if (= b 0) a (gcd b (remainder a b)))) A machine to carry out this algorithm must keep track of two numbers, a and b , so let us assume that these numbers are stored in two registers with those names. The basic operations required are testing whether the contents of register b is zero and computing the remainder of the contents of register a divided by the contents of register b. The remainder operation is a complex process, but assume for the moment that we have a primitive device that computes remainders. On each cycle of the GCD algorithm, the contents of register a must be replaced by the contents of register b, and the contents of b must be replaced by the remainder of the old contents of a di- 742 vided by the old contents of b. It would be convenient if these replacements could be done simultaneously, but in our model of register machines we will assume that only one register can be assigned a new value at each step. To accomplish the replacements, our machine will use a third “temporary” register, which we call t. (First the remainder will be placed in t, then the contents of b will be placed in a, and finally the remainder stored in t will be placed in b.) We can illustrate the registers and operations required for this machine by using the data-path diagram shown in Figure 5.1. In this diagram, the registers (a, b, and t) are represented by rectangles. Each way to assign a value to a register is indicated by an arrow with an X behind the head, pointing from the source of data to the register. We can think of the X as a button that, when pushed, allows the value at the source to “flow” into the designated register. The label next to each button is the name we will use to refer to the button. The names are arbitrary, and can be chosen to have mnemonic value (for example, a<-b denotes pushing the button that assigns the contents of register b to register a). The source of data for a register can be another register (as in the a<-b assignment), an operation result (as in the t<-r assignment), or a constant (a built-in value that cannot be changed, represented in a data-path diagram by a triangle containing the constant). An operation that computes a value from constants and the contents of registers is represented in a data-path diagram by a trapezoid containing a name for the operation. For example, the box marked rem in Figure 5.1 represents an operation that computes the remainder of the contents of the registers a and b to which it is attached. Arrows (without buttons) point from the input registers and constants to the box, and arrows connect the 743 operation’s output value to registers. A test is represented by a circle containing a name for the test. For example, our GCD machine has an operation that tests whether the contents of register b is zero. A test also has arrows from its input registers and constants, but it has no output arrows; its value is used by the controller rather than by the data paths. Overall, the data-path diagram shows the registers and operations that are required for the machine and how they must be connected. If we view the arrows as wires and the X buttons as switches, the data-path diagram is very like the wiring diagram for a machine that could be constructed from electrical components. Figure 5.1: Data paths for a GCD machine. In order for the data paths to actually compute GCDs, the buttons must be pushed in the correct sequence. We will describe this sequence in terms of a controller diagram, as illustrated in Figure 5.2. The elements of the controller diagram indicate how the data-path components should be op- 744 erated. The rectangular boxes in the controller diagram identify data-path buttons to be pushed, and the arrows describe the sequencing from one step to the next. The diamond in the diagram represents a decision. One of the two sequencing arrows will be followed, depending on the value of the data-path test identified in the diamond. We can interpret the controller in terms of a physical analogy: Think of the diagram as a maze in which a marble is rolling. When the marble rolls into a box, it pushes the data-path button that is named by the box. When the marble rolls into a decision node (such as the test for b = 0), it leaves the node on the path determined by the result of the indicated test. Taken together, the data paths and the controller completely describe a machine for computing GCDs. We start the controller (the rolling marble) at the place marked start, after placing numbers in registers a and b. When the controller reaches done, we will find the value of the GCD in register a. Figure 5.2: Controller for a GCD machine. 745 Exercise 5.1: Design a register machine to compute factorials using the iterative algorithm specified by the following procedure. Draw data-path and controller diagrams for this machine. (define (factorial n) (define (iter product counter) (if (> counter n) product (iter (* counter product) (+ counter 1)))) (iter 1 1)) 5.1.1 A Language for Describing Register Machines Data-path and controller diagrams are adequate for representing simple machines such as GCD, but they are unwieldy for describing large machines such as a Lisp interpreter. To make it possible to deal with complex machines, we will create a language that presents, in textual form, all the information given by the data-path and controller diagrams. We will start with a notation that directly mirrors the diagrams. We define the data paths of a machine by describing the registers and the operations. To describe a register, we give it a name and specify the buttons that control assignment to it. We give each of these buttons a name and specify the source of the data that enters the register under the button’s control. (The source is a register, a constant, or an operation.) To describe an operation, we give it a name and specify its inputs (registers or constants). 746 We define the controller of a machine as a sequence of instructions together with labels that identify entry points in the sequence. An instruction is one of the following: • The name of a data-path button to push to assign a value to a register. (This corresponds to a box in the controller diagram.) • A test instruction, that performs a specified test. • A conditional branch (branch instruction) to a location indicated by a controller label, based on the result of the previous test. (The test and branch together correspond to a diamond in the controller diagram.) If the test is false, the controller should continue with the next instruction in the sequence. Otherwise, the controller should continue with the instruction after the label. • An unconditional branch (goto instruction) naming a controller label at which to continue execution. The machine starts at the beginning of the controller instruction sequence and stops when execution reaches the end of the sequence. Except when a branch changes the flow of control, instructions are executed in the order in which they are listed. Figure 5.3: ↓ A specification of the GCD machine. (data-paths (registers ((name a) (buttons ((name a<-b) (source (register b))))) ((name b) (buttons ((name b<-t) (source (register t))))) ((name t) 747 (buttons ((name t<-r) (source (operation rem)))))) (operations ((name rem) (inputs (register a) (register b))) ((name =) (inputs (register b) (constant 0))))) (controller test-b (test =) (branch (label gcd-done)) (t<-r) (a<-b) (b<-t) (goto (label test-b)) gcd-done) ; label ; test ; conditional branch ; button push ; button push ; button push ; unconditional branch ; label Figure 5.3 shows the GCD machine described in this way. This example only hints at the generality of these descriptions, since the GCD machine is a very simple case: Each register has only one button, and each button and test is used only once in the controller. Unfortunately, it is difficult to read such a description. In order to understand the controller instructions we must constantly refer back to the definitions of the button names and the operation names, and to understand what the buttons do we may have to refer to the definitions of the operation names. We will thus transform our notation to combine the information from the data-path and controller descriptions so that we see it all together. 748 To obtain this form of description, we will replace the arbitrary button and operation names by the definitions of their behavior. That is, instead of saying (in the controller) “Push button t <-r” and separately saying (in the data paths) “Button t<-r assigns the value of the rem operation to register t” and “The rem operation’s inputs are the contents of registers a and b,” we will say (in the controller) “Push the button that assigns to register t the value of the rem operation on the contents of registers a and b.” Similarly, instead of saying (in the controller) “Perform the = test” and separately saying (in the data paths) “The = test operates on the contents of register b and the constant 0,” we will say “Perform the = test on the contents of register b and the constant 0.” We will omit the data-path description, leaving only the controller sequence. Thus, the GCD machine is described as follows: (controller test-b (test (op =) (reg b) (const 0)) (branch (label gcd-done)) (assign t (op rem) (reg a) (reg b)) (assign a (reg b)) (assign b (reg t)) (goto (label test-b)) gcd-done) This form of description is easier to read than the kind illustrated in Figure 5.3, but it also has disadvantages: • It is more verbose for large machines, because complete descriptions of the data-path elements are repeated whenever the elements are mentioned in the controller instruction sequence. (This is not a problem in the GCD example, because each operation and button is used only 749 once.) Moreover, repeating the data-path descriptions obscures the actual data-path structure of the machine; it is not obvious for a large machine how many registers, operations, and buttons there are and how they are interconnected. • Because the controller instructions in a machine definition look like Lisp expressions, it is easy to forget that they are not arbitrary Lisp expressions. They can notate only legal machine operations. For example, operations can operate directly only on constants and the contents of registers, not on the results of other operations. In spite of these disadvantages, we will use this register-machine language throughout this chapter, because we will be more concerned with understanding controllers than with understanding the elements and connections in data paths. We should keep in mind, however, that data-path design is crucial in designing real machines. Exercise 5.2: Use the register-machine language to describe the iterative factorial machine of Exercise 5.1. Actions Let us modify the GCD machine so that we can type in the numbers whose GCD we want and get the answer printed at our terminal. We will not dis- cuss how to make a machine that can read and print, but will assume (as we do when we use read and display in Scheme) that they are available as primitive operations.1 1 This assumption glosses over a great deal of complexity. Usually a large portion of the implementation of a Lisp system is dedicated to making reading and printing work. 750 Read is like the operations we have been using in that it produces a value that can be stored in a register. But read does not take inputs from any registers; its value depends on something that happens outside the parts of the machine we are designing. We will allow our machine’s operations to have such behavior, and thus will draw and notate the use of read just as we do any other operation that computes a value. Print, on the other hand, differs from the operations we have been us- ing in a fundamental way: It does not produce an output value to be stored in a register. Though it has an effect, this effect is not on a part of the machine we are designing. We will refer to this kind of operation as an action. We will represent an action in a data-path diagram just as we represent an operation that computes a value—as a trapezoid that contains the name of the action. Arrows point to the action box from any inputs (registers or constants). We also associate a button with the action. Pushing the button makes the action happen. To make a controller push an action button we use a new kind of instruction called perform. Thus, the action of printing the contents of register a is represented in a controller sequence by the instruction (perform (op print) (reg a)) Figure 5.4 shows the data paths and controller for the new GCD machine. Instead of having the machine stop after printing the answer, we have made it start over, so that it repeatedly reads a pair of numbers, computes their GCD, and prints the result. This structure is like the driver loops we used in the interpreters of Chapter 4. Figure 5.4: A GCD machine that reads inputs and prints results. 5.1.2 Abstraction in Machine Design We will often define a machine to include “primitive” operations that are actually very complex. For example, in Section 5.4 and Section 5.5 we will treat Scheme’s environment manipulations as primitive. Such abstraction is valuable because it allows us to ignore the details of parts of a machine so that we can concentrate on other aspects of the design. The fact that we have swept a lot of complexity under the rug, however, does not mean that a machine design is unrealistic. We can always replace the complex “primitives” by simpler primitive operations. Figure 5.5: Data paths and controller for the elaborated GCD machine. 753 Consider the GCD machine. The machine has an instruction that computes the remainder of the contents of registers a and b and assigns the result to register t. If we want to construct the GCD machine without using a primitive remainder operation, we must specify how to compute remainders in terms of simpler operations, such as subtraction. Indeed, we can write a Scheme procedure that finds remainders in this way: (define (remainder n d) (if (< n d) n (remainder (- n d) d))) We can thus replace the remainder operation in the GCD machine’s data paths with a subtraction operation and a comparison test. Figure 5.5 shows the data paths and controller for the elaborated machine. The instruction (assign t (op rem) (reg a) (reg b)) in the GCD controller definition is replaced by a sequence of instructions that contains a loop, as shown in Figure 5.6. Figure 5.6: ↓ Controller instruction sequence for the GCD machine in Figure 5.5. (controller test-b (test (op =) (reg b) (const 0)) (branch (label gcd-done)) (assign t (reg a)) rem-loop (test (op <) (reg t) (reg b)) (branch (label rem-done)) (assign t (op -) (reg t) (reg b)) 754 (goto (label rem-loop)) rem-done (assign a (reg b)) (assign b (reg t)) (goto (label test-b)) gcd-done) Exercise 5.3: Design a machine to compute square roots using Newton’s method, as described in Section 1.1.7: (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0)) Begin by assuming that good-enough? and improve operations are available as primitives. Then show how to expand these in terms of arithmetic operations. Describe each version of the sqrt machine design by drawing a data-path diagram and writing a controller definition in the register-machine language. 5.1.3 Subroutines When designing a machine to perform a computation, we would often prefer to arrange for components to be shared by different parts of the com- 755 putation rather than duplicate the components. Consider a machine that includes two GCD computations—one that finds the GCD of the contents of registers a and b and one that finds the GCD of the contents of registers c and d. We might start by assuming we have a primitive gcd operation, then expand the two instances of gcd in terms of more primitive operations. Figure 5.7 shows just the GCD portions of the resulting machine’s data paths, without showing how they connect to the rest of the machine. The figure also shows the corresponding portions of the machine’s controller sequence. Figure 5.7: Portions of the data paths and controller sequence for a machine with two GCD computations. 756 This machine has two remainder operation boxes and two boxes for testing equality. If the duplicated components are complicated, as is the remainder box, this will not be an economical way to build the machine. We can avoid duplicating the data-path components by using the same components for both GCD computations, provided that doing so will not affect the rest of the larger machine’s computation. If the values in registers a and b are not needed by the time the controller gets to gcd-2 (or if these values can be moved to other registers for safekeeping), we can change the machine so that it uses registers a and b, rather than registers c and d, in computing the second GCD as well as the first. If we do this, we obtain the controller sequence shown in Figure 5.8. We have removed the duplicate data-path components (so that the data paths are again as in Figure 5.1), but the controller now has two GCD sequences that differ only in their entry-point labels. It would be better to replace these two sequences by branches to a single sequence—a gcd subroutine—at the end of which we branch back to the correct place in the main instruction sequence. We can accomplish this as follows: Before branching to gcd, we place a distinguishing value (such as 0 or 1) into a special register, continue. At the end of the gcd subroutine we return either to after-gcd-1 or to after-gcd-2, depending on the value of the continue register. Figure 5.9 shows the relevant portion of the resulting controller sequence, which includes only a single copy of the gcd instructions. Figure 5.8: ↓ Portions of the controller sequence for a machine that uses the same data-path components for two different GCD computations. gcd-1 757 (test (op =) (reg b) (const 0)) (branch (label after-gcd-1)) (assign t (op rem) (reg a) (reg b)) (assign a (reg b)) (assign b (reg t)) (goto (label gcd-1)) after-gcd-1 ... gcd-2 (test (op =) (reg b) (const 0)) (branch (label after-gcd-2)) (assign t (op rem) (reg a) (reg b)) (assign a (reg b)) (assign b (reg t)) (goto (label gcd-2)) after-gcd-2 Figure 5.9: ↓ Using a continue register to avoid the duplicate controller sequence in Figure 5.8. gcd (test (op =) (reg b) (const 0)) (branch (label gcd-done)) (assign t (op rem) (reg a) (reg b)) (assign a (reg b)) (assign b (reg t)) (goto (label gcd)) gcd-done (test (op =) (reg continue) (const 0)) 758 (branch (label after-gcd-1)) (goto (label after-gcd-2)) ... ;; Before branching to gcd from the first place where ;; it is needed, we place 0 in the continue register (assign continue (const 0)) (goto (label gcd)) after-gcd-1 ... ;; Before the second use of gcd, we place 1 in the continue register (assign continue (const 1)) (goto (label gcd)) after-gcd-2 Figure 5.10: ↓ Assigning labels to the continue register simplifies and generalizes the strategy shown in Figure 5.9. gcd (test (op =) (reg b) (const 0)) (branch (label gcd-done)) (assign t (op rem) (reg a) (reg b)) (assign a (reg b)) (assign b (reg t)) (goto (label gcd)) gcd-done (goto (reg continue)) ... ;; Before calling gcd, we assign to continue ;; the label to which gcd should return. 759 (assign continue (label after-gcd-1)) (goto (label gcd)) after-gcd-1 ... ;; Here is the second call to gcd, with a different continuation. (assign continue (label after-gcd-2)) (goto (label gcd)) after-gcd-2 This is a reasonable approach for handling small problems, but it would be awkward if there were many instances of GCD computations in the controller sequence. To decide where to continue executing after the gcd subroutine, we would need tests in the data paths and branch instructions in the controller for all the places that use gcd. A m
{"url":"https://epdf.tips/sicp-2e-large-text.html","timestamp":"2024-11-06T07:45:23Z","content_type":"text/html","content_length":"1049667","record_id":"<urn:uuid:1fcc3dd9-69ad-4d94-ba12-e9ef4acc892c>","cc-path":"CC-MAIN-2024-46/segments/1730477027910.12/warc/CC-MAIN-20241106065928-20241106095928-00552.warc.gz"}
7.04 Unit Test: Savings AccountsQuizwiz - Ace Your Homework & Exams, Now With ChatGPT AI 7.04 Unit Test: Savings Accounts ¡Supera tus tareas y exámenes ahora con Matt had $325.64 in his account at the beginning of the month. He wrote checks for $114.23, $68.97, and $11.54. How much must he transfer from his savings to cover a rent check for $250? Jeremy had a starting balance of $122.00 in his savings passbook. He made these transactions: deposits of $68.52 and $46.35; withdrawals of $95.00 and $20.50; interest $1.50. What is his final Patty deposited $650 in a savings account with two percent simple interest. If she keeps it in the account for one year, how much interest will she earn? Use the passbook to answer the question. What is his balance E after another deposit is made? Your checkbook register shows these activities for the month: Old balance: $330.21 Check 279 to Clothes Couture for $31.85 Check 280 to Merrick Place Apartments for $250.00 Deposit $180.45 Check 281 to Gatt's Grocers for $41.74 Check 482 to Dr. Brian Seville for $35.00 What is your new checkbook balance? The graph represents the amount of money in a savings account that began with $1000. How much money is in the account after 15 years? Seth worked a summer job at a camp. His salary accumulated all summer and at the end of the summer he deposited $1890 in a savings account at 3.5 percent interest for 2.5 years. What was the total amount of money in the account at the end of this time? Kathy began with a balance of $96.54 in her checking account this month. She deposited $175.88 and wrote checks for $83.70, $56.15, and $93.42. How much will she have to deposit in order to pay her rent of $280.00? Use the graph shown to answer the question. Growth curve A represents $200 saved at 2.5 percent simple interest. Growth curve B represents $200 saved at 2.5 percent interest, compounded quarterly. What is the approximate difference between the two accounts in 20 years? Johnny deposited $1230 in a savings account at 2.25 percent interest. How much interest will the account earn in 10 years? Martha's grandfather put $2000 in a savings account for Martha's college fund when she was born. If the account pays three percent interest, compounded quarterly, how much will Martha have for college when she is 18? Growth curve A represents $200 saved at 2.5 percent simple interest. Growth curve B represents $200 saved at 2.5 percent interest, compounded quarterly. Use the graph shown to estimate the amount in Account A in 30 years. Use the passbook page to answer the question. Ken decided that he had enough saved if he combined it with the amount in his checking. He withdrew $500 in mid-April. What was his balance D? Growth curve A represents $200 saved at 2.5 percent simple interest. Growth curve B represents $200 saved at 2.5 percent interest, compounded quarterly. Use the graph shown to estimate the amount in Account B in 30 years. Check Register Balance $529.03 Bank Statement Balance $492.31 Outstanding Checks $14.67 and $24.56 Service Charges $75.95 What is the true balance of the checking account? Carl put $450 in a savings account at 2.5 percent interest, compounded daily. How much money will he have in a year? Round to the nearest dollar. Will got an insurance check of $4500. He deposited it into a savings account at 1.7 percent interest for five years. What was the total amount of money in the account after five years? Use the passbook page to answer the question. Ken Ritter was saving for a computer. What was his balance B after he made two deposits? Cherri filled out a deposit slip today. She deposited two checks for $56.13 and $22.10 and took out $20 in cash. What was her net deposit? What is the true balance? Check register balance $615.68 Bank statement balance $785.26 Outstanding checks $120.65 and $56.93 Service charge$8.00 Nat put $550 in an account which pays four percent interest, compounded semiannually. How much will be in the account in three years? Ray opened a savings account with $600 at 4.5 percent interest, compounded quarterly, and kept it in the account for two years. How much was the final amount? Round to the nearest dollar. Using the equation y=10,000(0.97)^x, predict the purchasing power of $10,000 ten years later. At the end of a 5-year term, what is the balance of an $8,000 investment at a simple interest rate of 1.8 percent? Round to the nearest dollar. At the end of a 5-year term, what is the balance of an $8,000 investment at a rate of 1.8 percent compounded semi-annually? Round to the nearest dollar. Using the table, what is the amount in the account compounded quarterly after five years? Round to the nearest dollar. Account Type Principal Rate Time Simple Interest $8,000 1.8% 5 yr Compounded Semiannually $8,000 1.8% 5 yr Compounded Quarterly $8,000 1.8% 5 yr What is the y-intercept of the equation y=10,000(0.97)^x? The graph shown represents the running schedule of a distance runner, who is increasing the distance she runs each week. What is the distance she will run in the seventh week? 16 miles Let y=10,000(0.97)^x represent the buying power of $10,000, with an inflation rate of three percent per year. The table shown represents the first four years after. Which year is incorrect? Years later x 1 2 3 4 Purchasing Power y 9,700 9,308 9,127 8,853 Using the equation y=76(1.013)^x the table shown represents the population of the U.S. for each of the four years following 1900. Which year is incorrect? Years after 1900 x 1 2 3 4 U.S. Population (millions) y 77 78 80 80 Using the equation y=76(1.013)^x predict the population of the U.S. in the year 2010. Use x = 110 as this is 110 years after 1900. Round to the nearest year. 315 million What is the y-intercept of the equation y=76(1.013)^x? The equation y=76(1.013)^x represents the the population of the United States, after 1900, in millions. What was the population in 1900, x = 0? 76 million When the bank returns a check for which there is not enough money in the bank to cover it, what do we call the check? Use the graph shown to answer the question. Growth curve A represents $200 saved at 2.5 percent simple interest. Growth curve B represents $200 saved at 2.5 percent interest, compounded quarterly. Which savings account is growing fastest? Compounded quarterly Does the equation represent growth or decay? y=10,000(0.97)^x A savings account with compounded interest can be modeled by which type of graph? Is this statement true or false? Most banks just give you a warning if you write a check which you cannot cover in your checking account. Does the following equation represent growth or decay? y=4^x Does the following equation represent growth or decay? y=76(1.013)^x A savings account with simple interest can be modeled by which type of graph? Alice is using this formula to increase the distance she is running, in miles, each week: 2(1.1)^x. Compute the distance she will run in 52 weeks (one year). Does this distance seem reasonable, yes or no? What will most banks do about a bounced check? Return a bounced check and assess a charge to your account Use the graph shown to answer the question. Growth curve A represents $200 saved at 2.5 percent simple interest. Growth curve B represents $200 saved at 2.5 percent interest, compounded quarterly. Why is the amount much higher in the compounded interest account? The account pays interest on both the principal and the interest accumulated Mike has a choice of an account that compounds daily and an account that compounds monthly. If they both have the same interest rate, which will earn more interest? The account that compounds daily Which account earns more interest? $20,000 3% interest 4 y $20,000 1.5% interest 10 y The account with 1.5% interest Why do banks pay interest on savings accounts? They invest the money Is this statement true or false? When you write a check, you write the amount to be paid in both numbers and words, for security purposes. Bill got a bank statement and found that his statement balance did not agree with his checkbook balance. He subtracted the outstanding checks to find the true balance and this still did not agree with his adjusted checkbook balance. What should he do? Write the true balance in his checkbook Cherri filled out a deposit slip today. She deposited two checks for $56.13 and $22.10 and took out $20 in cash. Did Cherri have to sign the deposit slip, yes or no? Conjuntos de estudio relacionados
{"url":"https://quizwizapp.com/es/study/704-unit-test-savings-accounts-k9Rth","timestamp":"2024-11-01T18:53:04Z","content_type":"text/html","content_length":"95344","record_id":"<urn:uuid:e19b0801-d369-4bfc-9dea-ea2364998a53>","cc-path":"CC-MAIN-2024-46/segments/1730477027552.27/warc/CC-MAIN-20241101184224-20241101214224-00102.warc.gz"}
Distinguishing Generalized Mycielskian Graphs A graph G is d-distinguishable if there is a coloring of the vertices with d colors so that only the trivial automorphism preserves the color classes. The smallest such d is the distinguishing number, \operatorname{Dist}(G). The Mycielskian \mu(G) of a graph G is constructed by adding a shadow vertex u_i... Show more
{"url":"https://synthical.com/article/Distinguishing-Generalized-Mycielskian-Graphs-2a8bc400-4ff5-4806-b703-0de1c4452a06","timestamp":"2024-11-09T22:40:22Z","content_type":"text/html","content_length":"64399","record_id":"<urn:uuid:632c981e-0fe6-4b29-9f6e-1afb1566b0f4>","cc-path":"CC-MAIN-2024-46/segments/1730477028164.10/warc/CC-MAIN-20241109214337-20241110004337-00649.warc.gz"}
Does the SDR have to be binary? Yes, I think this is the suspicion. That the concept of grandmother is an SDR i.e. it is very robust and the bits in the SDR are not at all representing some other semantic related to grandmother. I’m supposing that other areas are sensitive to parts of the grandmother SDR (i.e. even individual bits) and that can trigger other SDR representing other concepts like “elderly”. While we can as an observer say that bit X of grandmother correlates with “elderly” because it will excite the “elderly” SDR in another area, within the context of the grandmother SDR, we have a unique SDR that is very robust. But I am just playing around, taking wild swings as I learn more about HTM… One way to look at this is how you would build an encoder for, say, floating point numbers between 0 and 100. Since you have only a finite number of bits to work with, you cannot give every possible number a unique encoding – there will inevitably be overlapping bits between representations. Now you could deal with that by generating the encodings randomly (meaning the overlap between any two encodings would be random), but you might instead want to use the overlap to represent semantic similarities between numbers instead. For example, you might want 2.4 to be more semantically similar to (i.e. more overlapping bits with) 2.5 than, say, 8.2. You could use the overlapping bits in a way where numbers closer to each other have more overlap (and are thus more semantically similar) than numbers which are further away from each other. In this case, a given bit in an encoding would have some small, fuzzy semantic meaning (“in the vicinity of 2.5”), but that one bit by itself is not necessary to recognize the number 2.5. 1 Like That makes sense in for an encoder. I’m less sure that applies to the output of a SP. Intuitively, I would say the spatial location in an SDR is irrelevant once in the network (it becomes relations that matter). I’ve always seen the purpose of the SP algorithm being to fix sparsity (and sometimes to change the scale up or down) while preserving the ratio of semantics. True. A property of SDRs is that overlapping bits do not have to be physically close to each other… they just need to overlap. However, there is an interesting relationship between topology and semantics (through hexagonal grids) that I have hinted at a few times on the forum, so just because the SP algorithm shuffled things around, one could reassemble the activity back into topologically relevant encondings purely from the encoded semantics themselves. 3 Likes
{"url":"https://discourse.numenta.org/t/does-the-sdr-have-to-be-binary/2848?page=2","timestamp":"2024-11-03T22:42:44Z","content_type":"text/html","content_length":"33361","record_id":"<urn:uuid:2c504ca9-ed91-451f-a20f-50115e56840b>","cc-path":"CC-MAIN-2024-46/segments/1730477027796.35/warc/CC-MAIN-20241103212031-20241104002031-00405.warc.gz"}
Label Ranking through Nonparametric Regression Label Ranking through Nonparametric Regression Proceedings of the 39th International Conference on Machine Learning, PMLR 162:6622-6659, 2022. Label Ranking (LR) corresponds to the problem of learning a hypothesis that maps features to rankings over a finite set of labels. We adopt a nonparametric regression approach to LR and obtain theoretical performance guarantees for this fundamental practical problem. We introduce a generative model for Label Ranking, in noiseless and noisy nonparametric regression settings, and provide sample complexity bounds for learning algorithms in both cases. In the noiseless setting, we study the LR problem with full rankings and provide computationally efficient algorithms using decision trees and random forests in the high-dimensional regime. In the noisy setting, we consider the more general cases of LR with incomplete and partial rankings from a statistical viewpoint and obtain sample complexity bounds using the One-Versus-One approach of multiclass classification. Finally, we complement our theoretical contributions with experiments, aiming to understand how the input regression noise affects the observed output. Cite this Paper Related Material
{"url":"https://proceedings.mlr.press/v162/fotakis22a.html","timestamp":"2024-11-08T14:31:25Z","content_type":"text/html","content_length":"15580","record_id":"<urn:uuid:8813ac6c-5810-489a-969f-3547aded9785>","cc-path":"CC-MAIN-2024-46/segments/1730477028067.32/warc/CC-MAIN-20241108133114-20241108163114-00153.warc.gz"}
On Data Driven SIRD Model of Delta and Omicron Variants of COVID-19 On Data Driven SIRD Model of Delta and Omicron Variants of COVID-19 Compartmental model, SIRD model, SINDy, Data-driven model, Covid-19 variants The compartmental model stands as a cornerstone in quantitatively describing the transmission dynamics of diseases. Through a series of assumptions, this model can be formulated and subsequently validated against real-world conditions. Leveraging the abundance of COVID-19 data presently available, this study endeavors to reverse engineer the model construction process. Specifically, we analyse the compartmental model governing two notable variants of COVID-19: Delta and Omicron, utilizing empirical data. Employing the SINDy method, we extract parameters that define the model by effectively fitting the available data. To ensure robustness, the obtained model undergoes validation via comparison with real-world data through numerical integration. Additionally, we conduct fine-tuning in regularization techniques and input features to refine model selection. The constructed model then undergoes thorough analysis to gain qualitative insights and interpretations regarding the transmission dynamics of COVID-19. Weiss, R. A. and McMichael, A. J., Social and environmental risk factors in the emergence of infectious diseases, Nature Medicine, 10, pp. 70-76, 2004. Cucinotta, D. and Vanelli, M., WHO declares COVID-19 a pandemic, Acta Bio Medica: Atenei Parmensis, 91(1), p. 157-160, 2020. Satrio, C.B.A., Darmawan, W., Nadia, B.U. and Hanafiah, N., Time series analysis and forecasting of coronavirus disease in Indonesia using ARIMA model and PROPHET, Procedia Computer Science, 179, pp. 524-532, 2021. ArunKumar, K.E., Kalaga, D.V., Kumar, C.M.S., Kawaji, M. and Brenza, T.M., Forecasting of COVID-19 using deep layer recurrent neural networks (RNNs) with gated recurrent units (GRUs) and long short-term memory (LSTM) cells, Chaos, Solitons & Fractals, 146, p. 110861, 2021. Rauf, H.T., Lali, M.I.U., Khan, M.A., Kadry, S., Alolaiyan, H., Razaq, A. and Irfan, R., Time series forecasting of COVID-19 transmission in Asia Pacific countries using deep neural networks, Personal and Ubiquitous Computing, pp. 1-18, 2023. Taj, R.M., El Mouden, Z.A., Jakimi, A. and Hajar, M., Towards using recurrent neural networks for predicting influenza-like illness: case study of covid-19 in Morocco, International Journal of Advanced Trends in Computer Science and Engineering, 9(5), 2020. Hethcote, H.W., Three basic epidemiological models, In Applied Mathematical Ecology, pp. 119-144, 1989. Soewono, E., On the analysis of Covid-19 transmission in Wuhan, Diamond Princess and Jakarta-cluster, Communication in Biomathematical Sciences, 3(1), pp. 9-18, 2020. Abdy, M., Side, S., Annas, S., Nur, W. and Sanusi, W., An SIR epidemic model for COVID-19 spread with fuzzy parameter: the case of Indonesia, Advances in Difference Equations, 2021, pp. 1-17, 2021. Susanto, H., Tjahjono, V.R., Hasan, A., Kasim, M.F., Nuraini, N., Putri, E.R.M., Kusdiantara, R. and Kurniawan, H., How many can you infect? simple (and naive) methods of estimating the reproduction number, Communication in Biomathematical Sciences, 3(1), pp. 28-36, 2020. Cooper, I., Mondal, A. and Antonopoulos, C.G., A SIR model assumption for the spread of COVID-19 in different communities, Chaos, Solitons & Fractals, 139, p. 110057, 2020. Feng, S., Feng, Z., Ling, C., Chang, C. and Feng, Z., Prediction of the COVID-19 epidemic trends based on SEIR and AI models, PloS One, 16(1), p. e0245101, 2021. Zisad, S.N., Hossain, M.S., Hossain, M.S. and Andersson, K., An integrated neural network and SEIR model to predict Covid-19, Algorithms, 14(3), p. 94, 2021. Brunton, S.L., Proctor, J.L. and Kutz, J.N., Discovering governing equations from data by sparse identification of nonlinear dynamical systems, Proceedings of the National Academy of Sciences, 113 (15), pp. 3932-3937, 2016. Cranmer, M., Sanchez Gonzalez, A., Battaglia, P., Xu, R., Cranmer, K., Spergel, D. and Ho, S., Discovering symbolic models from deep learning with inductive biases, Advances in Neural Information Processing Systems, 33, pp. 17429-17442, 2020. Pan, S. and Duraisamy, K., On the structure of time-delay embedding in linear models of non-linear dynamical systems, Chaos: An Interdisciplinary Journal of Nonlinear Science, 30(7), 2020. Lusch, B., Kutz, J.N. and Brunton, S.L., Deep learning for universal linear embeddings of nonlinear dynamics, Nature Communications, 9(1), p. 4950, 2018. Rudy, S., Alla, A., Brunton, S.L. and Kutz, J.N., Data-driven identification of parametric partial differential equations, SIAM Journal on Applied Dynamical Systems, 18(2), pp. 643-660, 2019. Shea, D.E., Brunton, S.L. and Kutz, J.N., SINDy-BVP: Sparse identification of nonlinear dynamics for boundary value problems, Physical Review Research, 3(2), p. 023255, 2021. Kaiser, E., Kutz, J.N. and Brunton, S.L., Sparse identification of nonlinear dynamics for model predictive control in the low-data limit, Proceedings of the Royal Society A, 474(2219), p. 20180335, Brunton, S.L., Brunton, B.W., Proctor, J.L., Kaiser, E. and Kutz, J.N., Chaos as an intermittently forced linear system, Nature communications, 8(1), p. 19. 2017. Bramburger, J.J., Kutz, J.N. and Brunton, S.L., Data-driven stabilization of periodic orbits, IEEE Access, 9, pp. 43504-43521, 2021. Mangan, N.M., Askham, T., Brunton, S.L., Kutz, J.N. and Proctor, J.L., Model selection for hybrid dynamical systems via sparse regression, Proceedings of the Royal Society A, 475(2223), p. 20180534, Qin, H., Machine learning and serving of discrete field theories, Scientific Reports, 10(1), p. 19329, 2020. Guan, Y., Brunton, S.L. and Novosselov, I., Sparse nonlinear models of chaotic electroconvection, Royal Society Open Science, 8(8), p. 202367, 2021. Wang, R., Kalnay, E. and Balachandran, B., Neural machine-based forecasting of chaotic dynamics, Nonlinear Dynamics, 98(4), pp. 2903-2917, 2019. Gin, C.R., Shea, D.E., Brunton, S.L. and Kutz, J.N., DeepGreen: deep learning of Green's functions for nonlinear boundary value problems, Scientific Reports, 11(1), p. 21614, 2021. Brunton, S.L., Hemati, M.S. and Taira, K., Special issue on machine learning and data-driven methods in fluid dynamics, Theoretical and Computational Fluid Dynamics, 34(4), pp. 333-337, 2020. Brunton, S.L., Noack, B.R. and Koumoutsakos, P., Machine learning for fluid mechanics, Annual Review of Fluid Mechanics, 52, pp. 477-508, 2020. Horrocks, J. and Bauch, C.T., Algorithmic discovery of dynamic models from infectious disease data, Scientific Reports, 10(1), p. 7061, 2020. Jiang, Y.X., Xiong, X., Zhang, S., Wang, J.X., Li, J.C. and Du, L., Modeling and prediction of the transmission dynamics of COVID-19 based on the SINDy-LM method, Nonlinear Dynamics, 105(3), pp. 2775-2794, 2021. Ihsan, A.F., Data-driven Identification of Compartmental Model of COVID-19, In 2021 International Conference on Data Science and Its Applications (ICoDSA), IEEE, pp. 91-96, 2021. Arlis, S. and Defit, S., Machine learning algorithms for predicting the spread of COVID-19 in Indonesia, TEM Journal, 10(2), pp. 970-974, 2021. Mathieu, E., Ritchie, H., Rod es-Guirao, L., Appel, C., Giattino, C., Hasell, J., Macdonald, B., Dattani, S., Beltekian, D., Ortiz-Ospina, E. and Roser, M., Coronavirus pandemic (covid-19), Our World in Data, 2020. https://ourworldindata.org/coronavirus. Zou, H. and Hastie, T., Regularization and variable selection via the elastic net, Journal of the Royal Statistical Society Series B: Statistical Methodology, 67(2), pp. 301-320, 2005. How to Cite Ihsan, A. F. (2024). On Data Driven SIRD Model of Delta and Omicron Variants of COVID-19. Communication in Biomathematical Sciences, 7(1), 50-60. https://doi.org/10.5614/cbms.2024.7.1.3
{"url":"https://journals.itb.ac.id/index.php/cbms/article/view/22938","timestamp":"2024-11-04T21:18:33Z","content_type":"text/html","content_length":"47410","record_id":"<urn:uuid:659ab66e-0788-4d74-8480-2934864798df>","cc-path":"CC-MAIN-2024-46/segments/1730477027861.16/warc/CC-MAIN-20241104194528-20241104224528-00635.warc.gz"}
Intro to hierarchical models • Hierarchical models. • Intuition why they decrease prior sensitivity. Hierarchical models, a crowning achievement of Bayesian inference, can be used to decrease prior sensitivity even when the dataset of interest is of limited size. Example: predicting the probability of failure of the next Delta 7925H launch (so far, we observed this type of rocket has been launched 3 times, with 0 failed launches). Key idea: use “side data” to inform the prior… • For example: success/failure launch data from other other types of rockets. • Can we use the following rocket launch dataset to inform prediction for a single rocket type of interest? How to use “side data”? Two suboptimal approaches For pedagogy, we will first cover two simple heuristics to incorporate “side data” and describe their limitations. Then we will cover hierarchical models in the next section, showing how they address the limitations of simpler methods. First try (do not use this one!) • Merge all the data into one type? • I.e. just sum the columns in the data: Why merging everything is a bad idea? Some of these types of rockets are very different than the one under study (Delta 7925H). For example some of them are very old, made by different space agencies, etc. An indeed, the aggregated failure rate is high compared to what you would expect from modern “workhorse” rockets (<1%): We want to consider the different types of rockets as a population and model the variability within that population. Towards an improved way to use side data • Background: “mean–pseudo-sample-size” reparameterization of the Beta distribution. □ A reparametrization is a different labelling of a family such that you can go back and forth between the two labellings. □ Consider \[\alpha = \mu s, \;\; \beta = (1 - \mu) s\] where \(\mu \in (0, 1)\), \(s > 0\). • Interpretation: □ \(\mu\): mean of the Beta □ \(s\): measure of “peakiness” of the density, higher \(s\) corresponds to more peaked; roughly, \(s \approx\) number of data points that would make the posterior peaked like that. • Why we did this reparameterization? □ \(\to\) it should now be more intuitive how we can use “side data” to inform \(\mu\)… Second try (still suboptimal, but not as bad) • Estimate a failure probability \(\hat p_i\) for each type of rocket \(i\). • Fit a distribution \(p(\mu)\) on those \(\{\hat p_i\}\). • Use this distribution \(p(\mu)\) as the prior on \(\mu\)? ggplot(df, aes(x = numberOfFailures / numberOfLaunches)) + geom_histogram() + xlab(TeX("$\\hat{p}_i = \\textrm{numberOfFailures}_i / \\textrm{numberOfLaunches}_i$")) + geom_rug(alpha = 0.1) + Question: what are the weaknesses of using a prior fitted on this histogram? • First, notice the “bumps” at 1/2 (and 1, 1/4, etc)… □ Most of the counts at 1/2 correspond to types of rockets with 1 failure out of 2 launches! □ But if you fit a curve to this data, these “bumps” will have a large undesirable effects. □ More broadly, we want to take into account uncertainty in each of these \(\hat p_i\)’s. • Also: less clear how to generalize this method to handle the pseudo-sample-size \(s\). Hierarchical models: a better way to use side data Solution: go fully Bayesian! Recall: our Bayesian recipe… 1. Construct a probability model including □ random variables for what we will measure/observe □ random variables for the unknown quantities ☆ those we are interested in (“parameters”, “predictions”) ☆ others that just help us formulate the problem (“nuisance”, “random effects”). 2. Compute the posterior distribution conditionally on the actual data at hand 3. Use the posterior distribution to make a decision What it means here: we model the launcher type’s population parameters (\(\mu, s\)) as random variables. We are not interested in these other launcher types, but they help us inform inference about the type of rocket we are interested in (i.e., “nuisance” parameters). Mathematical description • Share these two “population parameters” across all launch types \[p_i | \mu, s \sim {\mathrm{Beta}}(\mu s, (1 - \mu) s)\] • Likelihood same as before: \(F_i | p_i \sim {\mathrm{Bern}}(n_i, p_i).\) • We still need to put prior on \(\mu\) and \(s\)… □ …but as we’ll discuss next, you should expect this prior choice to be less sensitive. □ Example: \(\mu \sim {\mathrm{Beta}}(1,1) = {\mathrm{Unif}}(0, 1)\), \(s \sim {\mathrm{Exp}}(1/10000)\) PS: why we pick such a small value, \(1/10000\)? • Recall the parameter of an exponential is a rate which is 1/mean. • So a mean of 10000 encodes we put significant mass to values up to \(O(10000)\). • This is sometimes described as a “vague prior”. Decrease prior choice sensitivity It seems we have introduced new problems as now we again have hyper-parameters, namely those for the priors on \(\mu\) and \(s\)! Key point: yes, but now we are less sensitive to these choices! Why? Heuristic: look at the node \(n\) connected to the hyper-parameters… • If most of \(n\)’s edges link to hyper-parameters: posterior more sensitive to hyper-parameters. • If most of \(n\)’s edges link to random variables connected to data: posterior will probably be less sensitive. Before going hierarchical: for maiden/early flights we had Note: \(p\) is connected to only a small number of observations. After going hierarchical: Note: \(\mu, s\) is connected to a large population of \(p_i\)’s, each linked with data.
{"url":"https://ubc-stat-ml.github.io/web447/w06_hierarchical/topic03_hierarchy.html","timestamp":"2024-11-07T07:31:08Z","content_type":"application/xhtml+xml","content_length":"90289","record_id":"<urn:uuid:bcc11dcd-4066-48db-831b-6ba8e37d49f8>","cc-path":"CC-MAIN-2024-46/segments/1730477027957.23/warc/CC-MAIN-20241107052447-20241107082447-00440.warc.gz"}
Dear young Physicists and Architects Dear young Physicists and Architects: Please, picture a frictionless Wheel that is lowered reversibly in Gravity Otto E. Rossler Faculty of Mathematics and Science, University of Tübingen, 72076 Tübingen, Germany The accepted wisdom in physics is that only mathematical (in the sense of algebraic) arguments are sufficient to arrive at reliable results on which the rest of physics can be based. However, this view is too narrow. Symmetry arguments and spatial representation are even more powerful in their native state. Eventually, everything can be brought into algebraic form. But sometimes, a century passes by during which everyone got led astray by prematurely formalized thinking. Specifically, several new features of gravitation follow from Emmy Noether’s rotational symmetry theorem if the latter is applied to a prototype situation. The main implication, c-global, is good for a revolution in physics after more than a century. A survival error is exposed that can only be appreciated by the “parallel guild” of architects. Can you help the physicists so as to avoid the error in time? Keywords: Architecture as parallel physics; the power of symmetry; rotating wheel; Noether’s intuition; early Einstein rehabilitated; micro black holes; global c; final catastrophe avoided; CERN’s safety report; Gianotti’s unique task. (May 20, 2015)’’’’’’ A fundamental finding implicit in Noether’s method is described. The rotating frictionless wheel is introduced as a sentinel in gravitation, and a cryptic word defined – Lomrr. The main implication is a new size change implicit in nature which in turn implies global constancy of the speed of light in the vacuum: c-global. Finally, a 76 years old false interpretation of a mathematical result will be corrected. And an almost-incurred global catastrophe is exposed which can be averted at the last minute if the one or the other reader understands the crisis well enough to help avert the A new fundamental Paradigm When you, my dear reader, join me in the following “picturing job” you will come very close to Einstein’s heart. At the same time you will see that he still lacked one specific sentinel in 1907 and 1915: Noether’s ultra-hard intuitive result of 1917. Therefore the centenary of Einstein’s opus maximum this year will be followed by a similar celebration of Noether’s kindred result in two years’ time from now. I first solicit your help to kindly improve on the following fundamental finding: “Noether’s Theorem plus Einstein Equivalence Principle yields c-global.” This finding transforms physics if true. I have 5 steps to offer, the sixth depends on your initiative. (i) What is at stake is a revolution in physics based on an improved version of the Einstein equivalence principle. Here an inconsistency was reluctantly accepted by Einstein in 1907 in the absence of Noether’s theorem. This inconsistency can be removed. Einstein felt forced to arrive at the embarrassing conclusion that the speed of light c in the vacuum is reduced downstairs in a constantly accelerating long rocketship in outer space even though the ship is described by special relativity with its inherent globally constant c. This drawback encountered in the equivalence principle in 1907 made Einstein fall silent on gravitation for 3 and 1/2 years and impaired progress on general relativity. Two years after the final version of the latter theory came Emmy Noether’s 1917 result of “global conservation of angular momentum in nature.” This finding – like energy conservation – possesses an overriding power. It is based on a fundamental symmetry of nature – rotation symmetry – and can be visualized geometrico-dynamically: Take a frictionless bicycle wheel suspended from its hub and lower it and pull it back up again in gravity. Everything is pre-specified if this simple sentinel is pictured in the mind. Firstly, the rotation rate of this “clock” must go down reversibly like that of any other clock that is hauled down due to Einstein’s gravitational clock slowdown. Secondly, since angular momentum is conserved in the process, the two other components of angular momentum besides rotation rate (mass and radius) cannot both remain unchanged downstairs. It becomes a rewarding pastime to figure out what is bound to occur in this Galilean gedanken experiment. (ii) The conserved angular momentum obeys a simple formula when the wheel has a constant horizontal or vertical orientation. The one-liner that applies is given as Eq. (8.32) in Tipler’s weighty textbook, for example, but Madame du Chatelet could already have written it down in the 18th century: L = ω m r^2 ………………………………….….….….….….….….….….….….….….….….….………………………………….. (1) Since this expression is amazingly hard to remember by heart, the dialect word L’hombre (Spanish for “man”) can be helpful as a mnemonic aid. L is the conserved angular momentum, omega ω is the rotation rate, m is the mass and r the radius of our horizontally rotating frictionless bicycle wheel – Lomrr. If ω is halved (as approximately valid on the surface of a neutron star with its close-to-unity gravitational redshift and hence halved time rate): What about m and r, the two other components of the wheel’s conserved L down there? I propose that m is halved and r is doubled. The halved mass is the key. It follows from the halved frequency (and hence energy) of any photon emitted on the surface of a neutron star. These photons look non-reduced in their frequency locally and remain locally transformable into mass-bearing particles in accordance with the laws of quantum mechanics (its creation and annihilation operators). Thus if a sufficiently sturdy PET scan (working on the basis of positronium annihilation) could be lowered onto the neutron star without getting smashed, it would still work properly there. The normal-appearing half-mass atoms down there automatically possess a doubled Bohr radius (and hence size) according to the universal laws of quantum mechanics. Both facts taken together yield L’ = ½ ω ½ m (2r)^2 = L , in conformity with Eq.(1). This result of a doubled radius r of the halved-rotation-speed wheel downstairs is at first sight at variance with a well-known fact implicit in the theory which underlies Einstein’s constantly accelerating Apollo-like rocketship in outer space: special relativity. The latter theory requires that light rays that connect points on a stationary solid object with the same points when the object is moving away at constant orientation and speed, travel along parallel lines (“railway tracks principle”). Special relativity thus demands that the doubled radius of the horizontally rotating wheel valid far downstairs be optically masked when viewed from above. (This can be understood in turn by looking at the interior of a transversally receding light clock.) Thus our wheel looks non-enlarged from above even though its radius r has doubled! (iii) To confirm this interactively, let your Noether wheel for once rotate vertically rather than horizontally. Then the doubled radius will remain optically masked in the horizontal direction but not so in the vertical direction: You now get a 2:1 vertical ellipse on the neutron star when looking down on the rotating wheel fromfar above with a super telescope. The optical contraction of all horizontal directions downstairs implies that when you look down from far above, a transversally moving light ray will be seen to “creep” at half speed on the surface of the Neutron star. This is what Einstein found in essence in 1907. Thus everything appears to be consistent. But: does light really “creep” down there? We just realized that the answer is no. For the distance travelled downstairs is doubled compared to above as the optically compressed wheel teaches. Hence c remains constant in spite of its apparent creeping. This new information was unavailable in 1907 owing to the absence of the Noether-wheel. The newly retrieved global constancy of c in the equivalence principle comes not really as a surprise because the equivalence principle is based on special relativity with its constant c in the first place. The at the time irreparable inconsistency explains why Einstein fell silent on gravitation for years after having been forced to conclude that c is non-constant in the constant–c equivalence principle. It would take this catastrophe more than a century to be healed. (iv) The retrieved globally constant c has an important implication: The vertical distance down to the surface of the neutron star (or down to the bottom of a very long rocketship) has increased in proportion to the observed redshift. Thus the groove in the “cloth of spacetime” has deepened. Hence the famous empirical Shapiro-time-delay, discovered half a century later, gets complemented by a matching new Shapiro space dilation. More spectacularly, the globally constant c implies that the spatial distance all the way down to the “horizon” (surface) of a black hole becomes as infinite from above – as infinite as the temporal distance for light that is going down or coming up has always been known to be since Oppenheimer and Snyder’s paper on black holes of 1939. Hence black holes are never finished in finite outer time due to the infinite distance of the horizon from the outside world. At this point I hear you ask: But is it not a well-known fact that an astronaut can fall onto (and into) a stellar black hole in finite wristwatch time as Oppenheimer and Snyder also showed (and as we all could witness in Kip Thorne’s carefully researched science fiction blockbuster movie Interstellar)? (v) The answer is a final point: All clocks of an in-falling astronaut get infinitely slowed eventually so that infinitely much outer time has elapsed on her or his arrival down there – provided that the universe will still exist by then (and that no larger black hole has come across to re-direct all in-falling trajectories). Note that our lowered wheel’s rotation rate ω like that of any other clock becomes zero on the horizon while simultaneously its radius r becomes infinite in an invisible-from-above manner. Hereby the tangential velocity of the wheel’s rim surprisingly stays invariant as the wheel’s radius grows and eventually approaches infinity (Sanayei effect). Hence the speed of light c surprisingly is not the only globally invariant speed in nature. Four Conclusions The Noether wheel teaches us several new things: First, there exists no Hawking radiation by virtue of the infinite distance of the horizon found valid from without. Hence nothing can disappear behind the horizon in finite outer waiting time as Hawking assumed. Second, general relativity needs to be re-scaled so that it ceases to mask the global-c constraint. The Noether wheel thus implies that a new, simpler-looking re-scaled “global-c version of general relativity” exists. General relativity thereby loses its up to this day valid incompatibility with quantum mechanics, so that the holy grail of unification materializes as a consequence of Eq.(1). A bonanza for young physicists and self-assured architects is in the making. The new physically realistic transform of the Einstein equation is a promised land. Third, the standard claim that angular momentum is conserved in general relativity in its present form is put in doubt by the Noether wheel because the latter brings in a previously lacking, in the limit unbounded size change. If the old theory with its non-global c indeed conserved angular momentum directly, then this has been a lucky coincidence. Fourth, there is a footnote to add to the described bonanza: The recaptured c-global forms a non-ignorable argument in favor of the renewal of a 7 years old safety report: the “LSAG” report of the famous LHC-experiment near Geneva. The latter experiment is among other goals designed to produce miniature black holes down on earth. But the Noether wheel’s implied c-global shows that black holes cannot Hawking evaporate as mentioned. The miniature black holes hoped-for will rather grow exponentially inside earth, in accordance with the results of a conference paper published in 2008 with the International Institute for Advanced Studies in Systems Research and Cybernetics titled “A rational and moral and spiritual dilemma” which is also on the web. Hope for Help from the young Generation The last-mentioned “dark implication” of the Noether wheel is the reason why (in parallel to the present publication in the proceedings of the Institute) I herewith publicly address you – the young generation – also directly here on Lifeboat todate because time is so pressing. You may have heard that CERN has announced to in two weeks’ time from now double its in the history of the universe unprecedented center-of-mass collision energy on a first celestial body – earth – in the hope to create Hawking-evaporating black holes on it. But as we saw, such pre-Noetherian collision experiments are now scientifically outdated notwithstanding the prestige of Stephen Hawking. The plan to continue is not merely a waste of taxpayers’ money. In light of the Noether-wheel based result of c-global, any attempt at producing miniature black holes down on earth must be considered an “attack against humankind” because any resident micro black hole is bound to grow exponentially inside earth. Are you, the youngest and therefore most open-minded citizens of our planet, able and willing to provide any help according to your own judgment? That is, can you perhaps come up with an idea how to persuade CERN to kindly respond to the Noether-based critique of the announced doubling of their unprecedented symmetric collision energy? CERN has decided to non-renew their 7 years old LSAG Safety Report before starting. Everyone automatically has a full understanding for the fact that it is humanly impossible to respond quickly to surprise new evidence – like an iceberg named Noether being headed on a collision course – when you are the captain of an only slowly maneuverable ocean liner. Therefore, my question to you: Do you have any idea how the spotted iceberg can be brought to the attention of the captain on the bridge in time? There is a female captain-elect to take office next year. Would it make sense to try and contact her? To the present author she does not reply. But perhaps she can – within the frame of her binding duties – spot a remaining legal way to respond to your kind request: possibly by calling on her own for a “brief thinking pause to evaluate a Noetherian result” before the announced start of collisions in June gets its final okay from the bridge? To conclude, by systematically inspecting a frictionless wheel used as our sentinel, we have arrived at an unusual result: that the speed of light c is a global constant in gravitation. And that, for this reason, black holes possess radically new properties. A “super application” turned up in this way in which the whole physics community proved to have gone astray for a century. And so with a vengeance: even the short-term survival of planet earth appears to be put in jeopardy by the spotted formal error (loss of c-global) maintained for a century. A scientific error can assume the rank of a survival error. Or to put it positively: Emmy Noether can save us all. Explanatory Appendix: It is not the Mathematics – it is the Visualization which went astray It is a strange experience to realize that so far, no one in physics appears able to understand the main teaching of the Noether wheel in gravitation: The well-known infinite temporal distance which the horizon of a black hole possesses from the outer universe since Oppenheimer and Snyder 1939 (excepting the wristwatch of the in-falling astronaut) implies logically that the horizon of a black hole does not even exist before an infinite time has passed by in the outside universe. This in principle well-known fact is only made more palpable by the new infinite spatial distance revealed by the Noether wheel. But even the old infinite temporal distance of the horizon of a black hole does already reveal a disquieting historical fact: Up to this day, every educated physicist thinks and readily explains with heavyweight arguments that the “frozen appearance from the outside” of the in-falling astronaut only masks what has already happened down there before: Namely, that the horizon has formed and that objects and information have disappeared for good behind it and that the “singularity” is a physical reality inside. Everyone in physics “knows” this for many decades – although the mathematics says the opposite as we saw. Thus it is only the visualization that went astray. What everyone falsely believes in is that the two time scales valid for an outside observer and for an in-falling observer, respectively, would possess equal rights. During the two-day in-falling time registered on the wristwatch of an in-falling astronaut, who appears frozen in time to us on the outside, indeed his target black hole including horizon and singularity will have fully formed when he arrives. However so only if the whole universe has not been wiped-out trillions of years in the future from now – before that arrival. You probably see my point but are perhaps not convinced: Is it true that the generally held view of the finished horizon can really not be legitimately upheld? After all, this view is tested by the consensus of a century! Let me explain it all with a look at a simple quantitative drawing to be found in a famous textbook (Figure 25.5 on page 667 of the “bible” in the field titled Gravitation written by my late friend John Wheeler together with Charles Misner and Kip Thorne in 1973). The figure is so simple it can be described in words. It shows the time axis plotted to the right and the traditional distance valid away from the center of the black hole plotted upwards. The curve of the astronaut falling-in describes a Galilean half parabola that is flat at first and becomes steeper and steeper to soon reach the horizon of the black hole and continue on downwards to hit the singularity at the bottom after approximately two days of astronaut time. The same Figure also shows the same in-falling event from the outside perspective. Here, the astronaut’s path first hugs the former parabola from above, but then gently disengages itself to remain aloof and eventually approach the horizon’s height level asymptotically on the right in an almost horizontal line that never quite reaches the horizon in finite time. Everyone in the field is happy with this interpretation for 76 years (and with this printed Figure for 42 years). But it is totally misleading by its failing to make a connection between the two touch-down events which are identical. To see this, I encourage you to enter the mirror-symmetric return trajectory of the astronaut into the picture in the two cases. To this end you only need to assume that the horizon harbors an un-damped trampoline as is admissible in principle. The latter catapults the astronaut right back upwards again on a symmetric return trajectory. He then will be right back after another two days’ wristwatch time. This mirror-symmetric return trajectory thus ends after 4 days – still on the far left of the Figure once you have entered it. So much for the lower curve. How about the upper curve? It, too, acquires a mirror image. This mirror image requires that you first copy the whole Figure onto transparent sheet, then turn the latter over and glue it symmetrically onto the original one on the right. Then in the middle we have the dotted points twice which represent infinity in time. Hence in the outside world the astronaut returns after “twice infinity in time”! The crew waiting on the circulating satellite from which the astronaut departed will according to this picture wait in vain on the fourth day and the say after and forever. For some reason, no one in physics has ever drawn this completed picture before. So the “freezing” is indeed not something that represents an unrealistic projection phenomenon valid in the outside world while “in reality,” the astronaut arrives on the horizon soon (as everyone in the scientific community believes). It rather is the other way round: the astronaut herself is frozen! When she returns as young as she is, an infinity of time has passed in the outside world. The movie Interstellar, filmed under the scientific supervision of Kip Thorne (second author on the mentioned gravitation bible), did a wonderful job in showing the gravitational time slow-down affecting an astronaut at finite depths down the funnel: He returned young to meet his beloved little daughter saying good bye to him on her old-age death bed. The movie then only forgot to keep up with this for the extremal case of hitting the horizon and coming back, as the “wormhole” paradigm of the script The movie strictly follows all textbooks and curricula. Thus a whole profession has overlooked the obvious for three generations. The theory of black holes, founded by Schwarzschild a century ago, thus contains an error. But it is not a mathematical error: it is “only” an error of common sense judgment. And it is a potentially fatal error at that. Unless someone can convince Professor Gianotti to kindly persuade her colleague Professor Heuer on the bridge to permit a renewal of the 7 years old Safety Report LSAG before the doubled-energy collisions scheduled for June 2015 are given their final okay, the doubled-energy experiment will be based on an error of logical judgment rated “unforgivable to a school boy.” Now it might cause a global survival error. Forgive me for having bothered you with a cut-and-paste job. There indeed never existed a more prolonged and more embarrassing logical mistake made collectively in the history of science. Only architects – “parallel physicists” spared the traditional brainwash – can fully understand the predicament laid open above. It is only they who can explain it to the world. The scholastic phase of the medieval period was also marked by quantitative results like “How many angels, exactly, do fit onto a needle’s tip?” We therefore can speak of the scholastic phase of theoretical physics in our own time. Emmy Noether’s modesty put an end to the male assertiveness. I predict that Professor Gianotti will reply graciously to you if you take a female mathematician’s methods seriously enough to write a letter to her begging to win some time before the doubled-cosmic record collisions start – so as to allow for a discussion of the Noether wheel’s implications. I for one apologize here to her for every too harsh word I ever said against CERN. I hope she will graciously grant you this birthday present for me on my 75th birthday, today. Paper to be published by the International Institute for Advanced Studies in Systems Research and Cybernetics. I thank Frank Kuske, Anton Traum and Guido Göhler for encouragement, György Darvas, Wolfgang Müller-Schauenburg and Kostas Kokkotas for discussions, and Greg Andonian and George Lasker of the International Institute for their kind invitation. For J.O.R. Comments — comments are now closed. 1. Otto E. Rössler says: Thank you. 2. Otto E. Rossler says: There are 4 other new fundamental results implicit in the Noether wheel: - In-falling particles lose not only their rest mass but also their charge due to the conserved ratio. Hence black holes are uncharged. - Therefore electrons are not point-shaped or near-point-shaped; for if they were they would be black holes and hence uncharged. - Therefore micro-black holes are likely to be not maximally small, either. Hence they probably arise at fairly low collision energies. - Micro black holes are invisible to CERN’s detectors by virtue of their unchargedness. 3. Otto E. Rossler says: The collisions started last Thursday were only non-dangerous “set-up collisions.“ So the captain on the bridge can still decide to give the “Noether Danger” a hearing. 4. Antonio Vargas says: Otto — Out of curiosity why do you state that these test collisions are not as dangerous? Is it perhaps because it is the proof your argument invalid? 5. Otto E. Rössler says: Always good to be skeptical — turning over backwards as Richard Feynman said. The percentage of fully frontal collisions is, at the low beam intensitis and low calibration present at the moment, negligible compared to the serious runs planned for June. So there is a genuine chance at the moment that Professor Gianotti responds to the gentle question posed to her — to everyone’s delight. 6. Otto E. Rossler says: I forgot to mention my co-authored book “Chaotic Harmony.” And I would like to mention ottorossler on WordPress.com as an opportunity for further exchanges. 7. Otto E. Rossler says: Sorry, I meant: https://ottorossler.wordpress.com
{"url":"https://italian.lifeboat.com/blog/2015/05/dear-young-physicists-and-architects","timestamp":"2024-11-05T02:25:36Z","content_type":"application/xhtml+xml","content_length":"151907","record_id":"<urn:uuid:6c3b1ee1-b004-4c08-b6d3-f36ad8839806>","cc-path":"CC-MAIN-2024-46/segments/1730477027870.7/warc/CC-MAIN-20241105021014-20241105051014-00624.warc.gz"}
Learning Task/Big Idea: Students will learn how to find roots(x-intercepts) of a quadratic function and use the roots to graph the parabola. - ppt download Presentation is loading. Please wait. To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy , including cookie policy. Ads by Google
{"url":"http://slideplayer.com/slide/8353368/","timestamp":"2024-11-07T19:04:48Z","content_type":"text/html","content_length":"137041","record_id":"<urn:uuid:a00afad2-f5ee-49e1-b5e2-90c12a481c3d>","cc-path":"CC-MAIN-2024-46/segments/1730477028009.81/warc/CC-MAIN-20241107181317-20241107211317-00692.warc.gz"}
Defunct Humanity It is generally accepted that the violation of 'local realism' is a distinctive feature of quantum systems, and it cannot be modeled by classical means. Our pilot study shows that this is actually possible. When analyzing the emulation of well-known 'quantum paradoxes', it turns out that the key role in their occurrence is played by the operation of the coincidence counter, which differently distinguishes a subset of entangled pairs from the set of all registrations. Its operation leads to the illusion of instantaneous 'spooky action' and 'retroactive eraser', when changing the system setting changes the settings of statistical sampling from data collected in the past. In the light of new thought experiments, the 'collapse' of the wave function can be interpreted in a subjective manner, as a change in the practical attitude of the experimenter's mind, which does not contradict however the objective nature of reality. In this case, the wave function is only a way of describing statistical 'ensembles' in the Blokhintsev's sense. The presence of non-classical interference between distant macroscopic cyclic processes should lead to non-local effects in animate and inanimate nature and in the human brain. There is an opinion that experiments with delayed choice quantum eraser cannot be emulated by classical methods. However, this opinion is not true. Let's conduct the following thought experiment: imagine that in the direction of Alice and Bob, whose laboratories are located at a distance in space, not pairs of particles are launched, but pairs of macroscopic arrows at different random angles in a certain plane (YZ). All participants in the experiment know in advance that each pair is launched at the same angle to the Z-axis, but if we take many pairs of launched pairs, the angles are randomly distributed between the pairs of arrows. It is also known that if one arrow in a pair is directed with its nose in one direction, then the other - in strictly opposite direction. The registration of arriving arrows in each space laboratory is as follows: at the edge of a circular hole in the plane (YZ) in the spacecraft of Alice and Bob, which are distant from each other, through which the arrows fly, there is a registration sensor directed to the center of the hole. He knows how to register the 'nose' or 'tail' of the arrow. If the arrow is turned towards it with the nose at least at a small angle, the arrow sensor registers the 'nose'. If on the contrary, then the 'tail'. For clarity, we can assume that there are red lights on the 'noses', and green ones on the 'tails', and only one of the two lights is visible to the detector. In the diagram, the holes in the walls of the space laboratories of Alice and Bob are superimposed on one another. During the experiment, a strict arrangement of these holes in parallel planes is maintained. Alice's and Bob's pointers for registering arrows are mutually located as shown in the diagram: at an angle φ. While in Plato the Soul, with the help of Reason, learns sciences, in Aristotle it itself is an object of scientific study. The states of the soul, according to Aristotle, come from the state of the body. In modern terms, consciousness is an 'epiphenomenon' of the body: and if the body corresponds to certain characteristics (functions), it should necessarily be interpreted as the presence of consciousness or 'mind'. In another way, this idea can be formulated through the 'principle of supervenience': 'it cannot be so that one state of the body would correspond to two different states of consciousness, the opposite is quite possible'. The principle of supervenience puts mind and body in unequal, asymmetric relations, where the source of causation, movement and, in general, any change is the body, and consciousness is like a 'mirror'. It follows that if the body is in the state of F, possessing consciousness, then it cannot be in the same state of F, and at the same time be a soulless 'zombie'. Any suitable body is 'infused' with mind - this is the opinion of the functionalists. Of course, 'suitable' means the state of a living, healthy (or nearly healthy) human body. The paradox is that modern physical theory is such that it does not just allow, but requires the existence of 'zombie twins', that is, living bodies that are obviously have not consciousness. We have formulated this statement in the form of a theorem. The theorem on the nonlocality of another consciousness Let Ψ1 be the consciousness of one, and Ψ2 is the consciousness of the other, and if the temporary localization of Ψ1 is determined from the point of view of himself, then either 1) from the point of view of Ψ1, the localization of Ψ2 is not determined, or 2) the special theory of relativity is false. Non-formal proof: Since in the special theory of relativity, simultaneity depends on the choice of a frame of reference, then along with a change in the frame of reference, the relative time localization of events that are distant from each other also changes. Events A and B, which were simultaneous in one of the frames of reference, in another - in a time order, can be located one after the other. To begin with, let us choose a frame of reference in which events A and B occur simultaneously: 1) consciousness Ψ1 is present in the body of F1 drinking wine (event A), and 2) consciousness Ψ2 - is present in the body of F2, contemplating the surroundings (event B). It would seem that both minds from any point of view are localized in time. However, in reality this cannot be. After all, if the body of the owner of consciousness Ψ1 changes its speed, then in the new frame of reference in which it finds itself, events A and B for it will no longer be simultaneous! For lovers of physics and philosophy, let me present the Special Theory of Relativity (SRT) in a new simple interpretation. The derivation of the basic relations of SRT ( Lorentz transformations) is possible from the coordinate transformations of rotation on the Euclidean plane by means of a new geometric interpretation of relativistic measurements. 1) We arbitrarily choose an orthogonal coordinate system with the origin of coordinates O on the Euclidean plane and denote it by the axes: (X, Y '). Let's rotate it through the angle α. The rotated system will be denoted by the axes (X ', Y). Let us express the coordinates of point A in the original system (X, Y ') through its coordinates in the rotated system (X', Y): x = x'⋅cosα + y⋅sinα y'= y⋅cosα - x'⋅sinα 1) ⇒ 2) 2) x = (x'– y⋅sinα) / cosα y = (y'+ x⋅sinα) / cosα 3) All that remains for us is to give the correct physical interpretation of what is shown in the figure. This is not difficult if we assume that the localized consciousness of the observer S moves in one direction parallel to the Y axis, passing the point-event A. As can be seen from 2), the coordinate y' is "lagging'' in relation to y, and x' experiences a 'contraction of the length of the moving rod'! When replacing y by ct, y' by ct', sinα = v / c, cosα = (1 - (v / c) ^2) ^1/2, it turns out that the rod moving relative to the observer S is defined for S as a rod of length: x⋅cosα = x⋅ (1 - (v / c) ^2) ^½ At the same time, in the frame of reference (cT ', X) moving with respect to S, the rod is at rest, and its length is equal to x. The time observed from S on the cT' scale will lag behind the readings observed by S on the cT scale in the direction of movement of the observer S: ct '= ct⋅cosα. Returning to point 2): in it, the equation for x - corresponds to the direct Lorentz transformation, and the equation for y = ct - to the inverse Lorentz transformation: x = (x'– t⋅v) / (1 - (v / c) ^2) ^1/2 ct = (ct '+ x⋅v /c) / (1 - (v / c) ^2) ^1/2 Thus, the physical interpretation of SRT effects is possible if we assume the motion of the localized 'Ego' of the observer in Euclidean space, where the direction of motion acts as 'proper time'. In this case, the coordinate transformations of the time and distance measurement data, in fact, correspond to the coordinate transformations during the transition from one rectangular Cartesian coordinate system to another, rotated by a certain angle with respect to the first. All the features of these transformations are explained by a specific physical interpretation, or in another way: by special rules for linking measurement data to coordinate axes. The above can be formulated in a shorter way: the standard interpretation of SRT is misleading as to the true nature of space-time. This misconception stems from a misunderstanding of the measurement of spatial distances. In the general case, the result of the measurement carried out by a stationary observer should be attributed to a moving frame of reference. And vice versa: the result of measuring the distance, carried out by a moving observer, should be attributed to a stationary frame of reference. If in this way we correlate measurements with coordinate axes, the whole SRT is geometrically described in 4-dimensional Euclidean space-time, which, if desired, can be considered Kantian in the philosophical sense of the word.
{"url":"https://igorrgroup.blogspot.com/2021/","timestamp":"2024-11-06T11:03:25Z","content_type":"text/html","content_length":"73558","record_id":"<urn:uuid:03acac7d-59e1-4c38-a124-6846736eb4c7>","cc-path":"CC-MAIN-2024-46/segments/1730477027928.77/warc/CC-MAIN-20241106100950-20241106130950-00730.warc.gz"}
The class Arr_conic_traits_2 is a model of the ArrangementTraits_2 concept and can be used to construct and maintain arrangements of bounded segments of algebraic curves of degree \( 2\) at most, also known as conic curves. A general conic curve \( C\) is the locus of all points \( (x,y)\) satisfying the equation: \( r x^2 + s y^2 + t x y + u x + v y + w = 0\), where: • If \( 4 r s - t^2 > 0\), \( C\) is an ellipse. A special case occurs when \( r = s\) and \( t = 0\), when \( C\) becomes a circle. • If \( 4 r s - t^2 < 0\), \( C\) is a hyperbola. • If \( 4 r s - t^2 = 0\), \( C\) is a parabola. A degenerate case occurs when \( r = s = t = 0\), when \( C\) is a line. A bounded conic arc is defined as either of the following: • A full ellipse (or a circle) \( C\). • The tuple \( \langle C, p_s, p_t, o \rangle\), where \( C\) is the supporting conic curve, with the arc endpoints being \( p_s\) and \( p_t\) (the source and target points, respectively). The orientation \( o\) indicates whether we proceed from \( p_s\) to \( p_t\) in a clockwise or in a counterclockwise direction. Note that \( C\) may also correspond to a line or to pair of lines - in this case \( o\) may specify a COLLINEAR orientation. A very useful subset of the set of conic arcs are line segments and circular arcs, as arrangements of circular arcs and line segments have some interesting applications (e.g. offsetting polygons, motion planning for a disc robot, etc.). Circular arcs and line segment are simpler objects and can be dealt with more efficiently than arbitrary arcs. For these reasons, it is possible to construct conic arcs from segments and from circles. Using these constructors is highly recommended: It is more straightforward and also speeds up the arrangement construction. However, in case the set of input curves contain only circular arcs and line segments, it is recommended to use the Arr_circle_segment_2 class to achieve faster running times. In our representation, all conic coefficients (namely \( r, s, t, u, v, w\)) must be rational numbers. This guarantees that the coordinates of all arrangement vertices (in particular, those representing intersection points) are algebraic numbers of degree \( 4\) (a real number \( \alpha\) is an algebraic number of degree \( d\) if there exist a polynomial \( p\) with integer coefficient of degree \( d\) such that \( p(\alpha) = 0\)). We therefore require separate representations of the curve coefficients and the point coordinates. The NtTraits should be instantiated with a class that defines nested Integer, Rational and Algebraic number types and supports various operations on them, yielding certified computation results (for example, it can convert rational numbers to algebraic numbers and can compute roots of polynomials with integer coefficients). The other template parameters, RatKernel and AlgKernel should be geometric kernels templated with the NtTraits::Rational and NtTraits::Algebraic number types, respectively. It is recommended to instantiate the CORE_algebraic_number_traits class as the NtTraits parameter, with Cartesian <NtTraits::Rational> and Cartesian<NtTraits::Algebraic> instantiating the two kernel types, respectively. The number types in this case are provided by the Core library, with its ability to exactly represent simple algebraic numbers. The traits class inherits its point type from AlgKernel::Point_2, and defines a curve and \( x\)-monotone curve types, as detailed below. While the Arr_conic_traits_2 models the concept ArrangementDirectionalXMonotoneTraits_2, the implementation of the Are_mergeable_2 operation does not enforce the input curves to have the same direction as a precondition. Moreover, Arr_conic_traits_2 supports the merging of curves of opposite directions.
{"url":"https://doc.cgal.org/5.4-beta1/Arrangement_on_surface_2/classCGAL_1_1Arr__conic__traits__2.html","timestamp":"2024-11-02T11:36:11Z","content_type":"application/xhtml+xml","content_length":"19914","record_id":"<urn:uuid:a094d648-9484-41e5-a3d1-aef062e0c656>","cc-path":"CC-MAIN-2024-46/segments/1730477027710.33/warc/CC-MAIN-20241102102832-20241102132832-00728.warc.gz"}
Homework Answers - Get Answers to Questions from Experts There were ten red bottles sitting on the wall. The probability of a red bottle accidentally falling is 0.95. What is the probability that fewer than 8 of the green bottles accidentally fall? 5. QUESTION: You consult Joe the bookie as to the form in the 2.30 at Ayr. He tells you that, of 16 runners, the favourite has probability 0.3 of winning, two other horses each have probability 0.20 of winning, and the remainder each have probability 0.05 of winning, excepting Desert Pansy, which has a worse than no chance of winning. What do you think of Joe’s advice? 4. QUESTION: M&M sweets are of varying colours and the different colours occur in different proportions. The table below gives the probability that a randomly chosen M&M has each colour, but the value for tan candies is missing. Colour Brown Red Yellow Green Orange Tan Probability 0.3 0.2 0.2 0.1 0.1 ? (a) What value must the missing probability be? (b) You draw an M&M at random from a packet. What is the probability of each of the following events? i. You get a brown one or a red one. ii. You don’t get a yellow one. iii. You don’t get either an orange one or a tan one. iv. You get one that is brown or red or yellow or green or orange or tan. 3. QUESTION: A bag contains fifteen balls distinguishable only by their colours; ten are blue and five are red. I reach into the bag with both hands and pull out two balls (one with each hand) and record their (a) What is the random phenomenon? (b) What is the sample space? (c) Express the event that the ball in my left hand is red as a subset of the sample space. 2. QUESTION: A fair coin is tossed, and a fair die is thrown. Write down sample spaces for (a) the toss of the coin; (b) the throw of the die; (c) the combination of these experiments. Let A be the event that a head is tossed, and B be the event that an odd number is thrown. Directly from the sample space, calculate P(A ∩ B) and P(A ∪ B). 1. QUESTION: Describe the sample space and all 16 events for a trial in which two coins are thrown and each shows either a head or a tail. Draw a galvanic cell based on the net ionic equation for the reaction below and complete the following: a) Label the anode and cathode. b) Write the oxidation and reduction half reactions for the cell. c) Indicate the electrode materials, and electrolyte components. d) Indicate the direction of electron flow and ion migration. Note: The spectator ion in the reaction is NO3 Al(s) + Au3± (aq) -> Au(s) + AI3+ (aq) Which of these activities would have been possible before the Neolithic Revolution 2.2. Solve the ivp sin(x) dx + y dy = 0, where y(0) 2.1. Solve 2xy + 6x + (x^2 - 4)y'=0
{"url":"http://assignmentexpert.com/homework-answers","timestamp":"2024-11-06T01:02:42Z","content_type":"text/html","content_length":"335039","record_id":"<urn:uuid:cd3b93ef-a9e0-4ca0-b972-7bd1a82ff572>","cc-path":"CC-MAIN-2024-46/segments/1730477027906.34/warc/CC-MAIN-20241106003436-20241106033436-00300.warc.gz"}
Calculating Flow Speed through a Pipe Question Video: Calculating Flow Speed through a Pipe Physics • Second Year of Secondary School Some liquid flows inside a pipe with a cross-sectional area of 0.02 m² squared. The liquid flows at 13 kg/s and has a density of 1200 kg/m³. Calculate the flow speed through the pipe in meters per second to two decimal places. Video Transcript Some liquid flows inside a pipe with a cross-sectional area of 0.02 meter squared. The liquid flows at 13 kilograms per second and has a density of 1200 kilograms per meter cubed. Calculate the flow speed through the pipe in meters per second to two decimal places. In this question, we want to calculate the flow speed of the liquid through the pipe. Let’s begin by visualizing the problem. We can recall the mass flow rate of a fluid flowing through a pipe is given by the equation 𝑚 over 𝑇 equals 𝜌𝐴𝑣, where 𝑚 is the mass of the fluid passing through a cross section of the pipe. 𝑇 is the time the cross section is measured for. 𝜌 is the density of the fluid, 𝐴 is the cross-sectional area of the pipe. And 𝑣 is the velocity of the fluid. We want to calculate the flow speed through the pipe. So we want to make 𝑣 the subject. We can do this by dividing both sides of the equation by 𝜌𝐴, to leave us with 𝑣 equals 𝑚 over 𝑇𝜌𝐴. In the question, we are told that the liquid flows at 13 kilograms per second. This is the mass flow rate 𝑚 over 𝑇. So 𝑚 over 𝑇 equals 13 kilograms per second. We are also given the density of the liquid, which is equal to 1200 kilograms per meter cubed, and the cross-sectional area of the pipe, which is equal to 0.02 meters squared. Substituting these values into our equation, we find that the flow speed through the pipe 𝑣 is equal to 13 kilograms per second over 1200 kilograms per meter cubed multiplied by 0.02 meters squared. Looking at our units, the kilograms in the numerator will cancel with the kilograms on the denominator. The “per meters cubed” in the denominator will end up on the numerator. And so meters cubed over meters squared will leave us with meters. And so, we are left with meters in the numerator and seconds in the denominator. Therefore, our answer will be in meters per second. Completing the calculation, we get a value of 0.54 meters per second to two decimal places. And this is our answer. The flow speed through the pipe is equal to 0.54 meters per second.
{"url":"https://www.nagwa.com/en/videos/784170341836/","timestamp":"2024-11-04T07:30:59Z","content_type":"text/html","content_length":"251080","record_id":"<urn:uuid:22479049-3b3f-438d-8099-bc9be1d1a79a>","cc-path":"CC-MAIN-2024-46/segments/1730477027819.53/warc/CC-MAIN-20241104065437-20241104095437-00479.warc.gz"}
What are Option Payoffs? The 'optionality' characteristic of Options results in a non-linear payoff for them. In simple terms, this means that the losses for the buyer of an Option are limited, but the profits are potentially unlimited. For a writer (seller), the payoff is exactly the opposite. Their profits are limited to the Option premium, but their losses are potentially unlimited. By using combinations of Options and the underlying, these non-linear payoffs are utilized to generate various other payoffs. Here are six basic payoffs from which various trading and investing strategies are derived: Payoff profile of the buyer of an asset: Long asset In this basic position, an investor buys the underlying asset, such as ABC Ltd. shares, for Rs. 2220, and sells it at a future date at an unknown price. Once purchased, the investor is said to be "long" the asset. Payoff profile for the seller of an asset: Short asset In this basic position, an investor shorts the underlying asset, like ABC Ltd. shares, for Rs. 2220, and buys it back at a future date at an unknown price. Once sold, the investor is said to be "short" the asset. Payoff profile for the buyer of Call Options: Long Call A Call Option gives the buyer the right to buy the underlying asset at the strike price specified in the Option. The profit/loss that the buyer makes on the Option depends on the spot price of the underlying. If upon expiration, the spot price exceeds the strike price, they make a profit. The higher the spot price, the higher the profits. If the spot price is less than the strike price, the loss is the premium paid for buying the Option. Payoff profile for the writer (seller) of Call Options: Short Call A Call Option gives the buyer the right to buy the underlying asset at the strike price specified in the Option. The writer of the Option charges a premium for selling it. The profit/loss for the buyer depends on the spot price of the underlying. Whatever profit the buyer makes is the seller's loss. If, upon expiration, the spot price exceeds the strike price, the buyer exercises the Option, resulting in losses for the writer. If the spot price is less, the writer keeps the premium. Payoff profile for the buyer of Put Options: Long Put A Put Option gives the buyer the right to sell the underlying asset at the strike price specified in the Option. The profit/loss for the buyer depends on the spot price of the underlying. If, upon expiration, the spot price is below the strike price, they make a profit. The lower the spot price, the higher the profit. If the spot price is higher than the strike price, the loss is the premium paid for buying the Option. Payoff profile for the writer (seller) of Put Options: Short Put A Put Option gives the buyer the right to sell the underlying asset at the strike price specified in the Option. The writer of the Option charges a premium for selling it. The profit/loss for the buyer depends on the spot price of the underlying. Whatever profit the buyer makes is the seller's loss. If, upon expiration, the spot price is below the strike price, the buyer exercises the Option, resulting in losses for the writer. If the spot price is higher, the writer keeps the premium.
{"url":"https://help.upstox.com/support/solutions/articles/104961-what-are-option-payoffs-","timestamp":"2024-11-03T15:16:16Z","content_type":"text/html","content_length":"34074","record_id":"<urn:uuid:f44cb007-26f0-4e99-b694-e9a91120c627>","cc-path":"CC-MAIN-2024-46/segments/1730477027779.22/warc/CC-MAIN-20241103145859-20241103175859-00598.warc.gz"}
pub trait CryptoRng: RngCore { } Expand description A marker trait used to indicate that an RngCore implementation is supposed to be cryptographically secure. Cryptographically secure generators, also known as CSPRNGs, should satisfy an additional properties over other generators: given the first k bits of an algorithm’s output sequence, it should not be possible using polynomial-time algorithms to predict the next bit with probability significantly greater than 50%. Some generators may satisfy an additional property, however this is not required by this trait: if the CSPRNG’s state is revealed, it should not be computationally-feasible to reconstruct output prior to this. Some other generators allow backwards-computation and are considered reversible. Note that this trait is provided for guidance only and cannot guarantee suitability for cryptographic applications. In general it should only be implemented for well-reviewed code implementing well-regarded algorithms. Note also that use of a CryptoRng does not protect against other weaknesses such as seeding from a weak entropy source or leaking state. Note that implementors of CryptoRng also automatically implement the TryCryptoRng trait. Implementations on Foreign Types§
{"url":"https://rust-random.github.io/rand/rand/trait.CryptoRng.html","timestamp":"2024-11-10T01:59:28Z","content_type":"text/html","content_length":"13019","record_id":"<urn:uuid:73b50a56-1bfd-46e9-b499-b985b53bdab6>","cc-path":"CC-MAIN-2024-46/segments/1730477028164.3/warc/CC-MAIN-20241110005602-20241110035602-00647.warc.gz"}
Measurement of Viscosity There are a number of diverse methods for determining the co-efficient of viscosity: for example, (a) by determining the volume of liquid flowing through a capillary of known dimensions under a given pressure, i.e., using Poiseuille’s equation; (b) from the rate of fall of spherical balls through the liquid; (c) from the torsion in a standard wire supporting a cylinder immersed in a liquid moving at a known speed. The method most useful for ordinary liquids is that based on Poiseuille’s equation. It is difficult to determine the absolute viscosity by Poiseuille’s method. The usual procedure is to determine the viscosity of given liquid relative to another liquid whose viscosity, is accurately known. Such relative measurement is a common practice in the determination of many physic-chemical quantities. Fig: Simple Viscometer A relatively simple viscometer as shown in Figure, may be used for the determination of relative viscosities. For a given viscometer the radius, R, and length, l, are constants. The pressure difference that causes the liquid to flow through the capillary is the difference, h, between the levels of liquids in the bulbs. If d is the density of the liquid this pressure will be h, g and d. For a liquid of density, d[1], and viscosity, η[1], if the time of flow through the capillary is t[1] seconds, equation (Poiseuille’s) takes the form; η[1] = [(hg π R^4) / (8Vl)] d[1]t[1] If the same volume of another liquid of viscosity, η[2], and density. d[2], flows through the capillary in the same viscometer in t[2] seconds, then η[2] = [(hg π R^4) / (8Vl)] d[2]t[2] The ratio of viscosities is then given by: η[1]/ η[2] = d[1]t[1]/d[2]t[2] … …. (1) Water is usually taken as the standard substance. It is obvious that if the viscosity of one liquid is known, that of the ether may be calculated from the measured values of the densities and times of flow of the two liquids. The viscometer shown in Figure, as known as the Ostwald viscometer. A known appropriate volume of the liquid under examination is first placed in the wider limb and sucked up the capillary to a level beyond the upper mart in the capillary. The liquid is then allowed to fall and the time taken for the liquid level to tall from the upper to the lower mark in the capillary is noted. The liquid is then replaced by an equal volume of the second liquid and the procedure is repealed. Use of equation (1) will give the required viscosity value. There are a number of other viscometers in use which are simple modifications of the Ostwald viscometer.
{"url":"https://qsstudy.com/measurement-of-viscosity/","timestamp":"2024-11-08T01:46:36Z","content_type":"text/html","content_length":"24640","record_id":"<urn:uuid:d4b66fc3-db31-4dfd-96ae-9ca258aba8df>","cc-path":"CC-MAIN-2024-46/segments/1730477028019.71/warc/CC-MAIN-20241108003811-20241108033811-00840.warc.gz"}
SHAP Values Explained with Manchester City SHAP Values Explained with Manchester City SHAP values are based on Shapley values from game theory. These values help us fairly distribute payouts among players (or in our case, the importance of features in a model). In this context, we’re distributing “payouts” to the features that contribute to a machine learning model’s prediction. *Guardiola giving bonuses but unsure how to rightfully distribute, you can see very accurate drawing of him :) * The Football Analogy Let’s imagine Pep Guardiola, the coach of Manchester City, wants to distribute bonuses to four key players: Kevin De Bruyne, Raheem Sterling, Ilkay Gündoğan, and Riyad Mahrez. Pep wants to ensure the bonuses are distributed fairly based on each player’s contribution to the team’s success. Pep and me know that without Kevin De Bruyne, the team wouldn’t have won the title. The challenge is to quantify how much each player contributed to the overall points the team earned. And they want to give bonuses for all matches, UCL, FA CUP, League title, so don’t think if numbers doesn’t make sense. In order to make things easier, let’s assume 7 players are fixed at the beginning eleven, and they are having equal bonuses, so we don’t care about them. And we would like to only decide the bonuses of Raheem Sterling, Kevin De Bruyne, Sergio Aguare and Ilkay Gundogan, because they were the main driver of the championship title through the season. In order to quantify the effects of these players, I’m getting in front of my laptop to note all the points received with and without these players. Matches played without these four players denoted as \(S^0= \left\{ \right\}\), matches played with only Raheem Sterling is denoted as \(S^0= \left\{ RS \right\}\). The points received on these matches are listed as below. Points received with/out players Let’s calculate bonus of KDB, which is $\phi_{\text{KDB}}$. SHAP values help us interpret the impact of each player (feature) on points received(individual predictions). By simulating the absence of a player (feature) (e.g., replacing it with its mean value), we can see how the model’s prediction changes. We use the concept of marginal contributions. For each feature, we compare the model’s prediction with and without that feature across different subsets of features. The SHAP value for a feature is the weighted average of these marginal contributions. \[ \phi_i = \sum_{S \subseteq N \setminus \{i\}} \frac{|S|!(|N|-|S|-1)!}{|N|!} \left[ v(S \cup \{i\}) - v(S) \right]\] • \(\phi_i\) is the SHAP value for feature \(i\) • \(S\) is a subset of all features excluding \(i\) • \(N\) is the set of all features • \(v(S)\) is the model’s prediction for subset \(S\) Marginal Contribution What is the marginal contribution? Assume we have matches with Raheem Sterling and Ilkay Gundogan, and the points we receive on that matches: \[ S=\left\{ RS,IG \right\}=62\] And, there are matches Raheem Sterling, Ilkay Gundogan and Kevin de Bruyne played together. \[ S=\left\{ RS,IG,KDB \right\}=67\] So the marginal effect of adding KDB to the starting eleven for the specific RS IG combination \( \left\{ RS,IG \right\}\cup \left\{ KDB\right\}\) should be $67-62=5$, and this stands for the rightest side of the equation \( \left[ v(S \cup \{i\}) - v(S) \right]\). What about the left side of the equation? What does that mean? \(\sum_{S \subseteq N \setminus \{i\}} \frac{|S|!(|N|-|S|-1)!}{|N|!}\) • When calculating the SHAP value for a feature i to different subsets of features affects the model’s prediction. • The factorial terms ensure that each subset’s contribution is weighted appropriately based on its size and the number of features not in the subset. So it gives the proportion of all possible orderings of the features in which a particular subset S is considered. And then you sum for all features. For the specific combination - \( \left\{ RS,IG \right\}\cup \left\{ KDB\right\}\) - we have \[\frac{2!(4-2-1)!}{4!} = \frac{2}{24}\] Then, for the \( \left\{ RS,IG \right\}\cup \left\{ KDB\right\}\) we have \[ \frac{2!(4-2-1)!}{4!} \left[ 67 - 62\right] = \frac{2}{24}*5 = \frac{10}{24}\] This is for specific combination, when we sum the all the contribution of KDB to first eleven, we will be able to calculate the bonus (shapley value) for $\phi_{\text{KDB}}$ Drawbacks of Football Analogy When we choose football analogy with the them of Manchester City of 2021, it feels like every player positively contributed. This doesn’t necessarily true. Assume you do it for Brentford (average team on that season) and you may see some players negatively affected to the score. Hard to Follow? Watch the Videos I have three videos on explaining how SHAP works and how to calculate variables manually, what is the KernelSHAP and how to implement it on NumPy. Either you can watch videos, or skim through the post and then watch videos, it’s up to you. You’ll also have Python notebook that you can implement KernelSHAP from scratch at the end. Kernel SHAP Assume you have a model trained on 10 features, how would you test your model with 9 features? It is designed to take 10 inputs, right? There’s a trick for this, called KernelSHAP which I explained in the Youtube video below along with Jupyter notebook with Numpy, blog post may come later depending on the demand. Math of KernelSHAP: Implementation of KernelSHAP through NumPy, Paper Explained Implementation of KernelSHAP through NumPy: Jupyter Notebook and Repo Link
{"url":"http://mburaksayici.com/blog/2024/09/01/shap-values-explained.html","timestamp":"2024-11-14T22:27:49Z","content_type":"text/html","content_length":"15801","record_id":"<urn:uuid:4c4818d2-e653-4032-8488-c278ca73a56b>","cc-path":"CC-MAIN-2024-46/segments/1730477395538.95/warc/CC-MAIN-20241114194152-20241114224152-00574.warc.gz"}
IEC TR 60909-4 pdf download - Free Standards PDF Download IEC TR 60909-4 pdf download.Short-circuit currents in three-phase a.c. systems 1 General 1.1 Scope and object This part of IEC 60909 is a technical report intended to give help for the application of IEC 60909-0 for the calculation of short-circuit currents in 50 Hz or 60 Hz three-phase a.c. systems. This technical report does not include additional requirements but gives support for the modelling of electrical equipment in the positive-sequence, the negative-sequence and the zero- sequence system (clause 2) and the practical execution of calculations in a low-voltage system (clause 3), a medium-voltage system with asynchronous motors (clause 4) and a power-station unit with its auxiliary network feeding a large number of medium-voltage asynchronous motors and low-voltage motor groups (clause 5). The three examples given in clauses 3, 4 and 5 are similar to those given in IEC 60909 (1988) but they are revised in accordance with IEC 60909-0, which replaces it. A subclause is added to the example in clause 3 to give a comparison between the results found with the application of the equivalent voltage source at the short-circuit location following the procedure given in IEC 60909-0 on the one hand, and results found with the superposition method on the other hand, taking into account different load-flow conditions before the short circuit. Clause 6 of this technical report gives the circuit diagram and the data of a test network and the results for a calculation carried out in accordance with IEC 60909-0, to offer the possibility for a comparison between the results found with a digital program for the calculation of short-circuit currents and the given results for p1 k1 k b p k in a high-voltage network with power-station units, generators, asynchronous motors and lines in four different voltage levels 380 kV, 110 kV, 30 kV and 10 kV. Equations for the calculation of the positive-sequence and the zero-sequence system impedances of overhead lines with one or two parallel circuits (double circuit line) and without or with one or two earth wires are given in IEC 60909-2. The negative-sequence impedance is equal to the positive-sequence impedance. The measurements to find the positive-sequence and the zero- sequence impedances of cables with sheath, shielding and armouring are similar to those given in figure 1. Examples are given in IEC 60909-2. In the case of the zero-sequence impedance, the earthing of the sheath or the shielding or the armouring is important as well as the number of parallel cables. In the case of low-voltage four-core cables the cross-section of the earthed core has an influence on the zero-sequence impedance. Figure 2 demonstrates the meaning and the principal measurement of the positive-sequence and the zero-sequence impedance of a three-phase a.c. short-circuit limiting reactor. If the magnetic coupling between the three coils without or with iron core is small, the zero- sequence impedance Z (0)R is approximately equal to the positive-sequence impedance Z (1)R . When calculating short-circuit currents in high-voltage systems, it is often sufficient to use the reactance only. 2.2 Transformers Unit transformers of power-station units are also dealt with in 2.3. Network transformers have two or three or even more three-phase windings. Figure 3 gives an example for the positive-sequence and the zero-sequence system impedances of a two-winding transformer with the vector group YNd5. In the case of three-winding transformers (examples are given in table 3b of IEC 60909-2), it is necessary to measure three different impedances and then to calculate the three impedances of the equivalent circuit in the positive-sequence or the zero-sequence system of the transformer, see 3.3.2 of IEC 60909-0 and the example at the end of this clause.
{"url":"https://www.2022standards.com/iec-tr-60909-4-pdf-download.html","timestamp":"2024-11-02T20:31:57Z","content_type":"text/html","content_length":"53681","record_id":"<urn:uuid:40f351b6-e8fb-41fd-9b03-3bc0cb21dbe0>","cc-path":"CC-MAIN-2024-46/segments/1730477027730.21/warc/CC-MAIN-20241102200033-20241102230033-00074.warc.gz"}
UNSW 2016S1 Results Thread Aug 23, 2014 That makes sense. .5454545454.... is 6/11, consistent with 11 subjects (4 + 4 + 3). It just means your WAM can be divided by 11 with 6/11 as a remainder. Cheers. Guess that confirms it then May 31, 2014 The total of n integers will have a remainder of either 0, 1, …, (n-1) modulo n. This means the possible fractionals parts for the average are just: 0, 1/n, 2/n, 3/n, …, (n-1)/n. E.g. If 1 subject: only can have decimal of 0. If 2 subjects: only can have 0 or 0.5. If 3 subjects: 0, 0.333..., or 0.666..., If 4 subjects: 0, 0.25, 0.5, or 0.75. So a fractional part of 0.25 for instance means the no. of subjects must have been 4 (or something divisible by 4). (Assumption: weights in average are equal.) Haha here's the mathematical answer. Does this assume that all marks are integers ? Yes. They are always integers. May 31, 2014 I hope it's all 4 subjects, I did first year commerce subjects so econ1101, acct1501, mgmt1001 and econ1203, but thanks for the reply! Through investigation mreditor deduced that all 4 of these should be out. Anyone else want to share their WAM lel. My wam will be droppin soon probably below 80 Feb 18, 2014 In general, does your wam tend to drop as you progress through your degree? [emoji38] Sent from my iPhone using Tapatalk In general, does your wam tend to drop as you progress through your degree? [emoji38] Sent from my iPhone using Tapatalk Mine has been rising at a faster rate each year... Doubt that will still happen in my final years though Most of my other friends have it stable or dropping. Really depends on the individual. Felt I should have gotten HD average this sem. Close but life goes on. Also i'm cursed with continually getting the unluckiest mark mine declined by .5 every sem lmao A lot of misconceptions floating around in the last two pages. Just to be clear, the stream declaration WAM is your course WAM, NOT your WAM for this semester. Aside from that, assuming fins2624 has been put in, either acct2522 or econ1202 i think must have been put in. That's about all i can get from it. Interested to see what other people can deduct FINS2624 has definitely been put in! how sure are you that actl2131 is reflected, and not Actl2111.. im assuming that you do 2111 as well. currently my mark reflects 3 subjects. Because on Tuesday morning at 8AM, my stream declaration WAM reflected two subjects, and then at 11AM, it had changed and then reflected three subjects. By asking around (and comparing with students in my cohort doing different subjects e.g. B Actl Studies / B Adv Maths who don't do 2131 and whose WAM didnt change), we deduced that 2131 was uploaded then. Mar 19, 2010 Does course deceleration wam include the wam of our previous course if we have transferred? Jun 28, 2010 A lot of misconceptions floating around in the last two pages. Just to be clear, the stream declaration WAM is your course WAM, NOT your WAM for this semester. It's actually program wam if you want to be super clear. Courses are the subjects. It's actually program wam if you want to be super clear. Courses are the subjects. Good pick up, my bad haha :/ Thanks! Does course deceleration wam include the wam of our previous course if we have transferred? If you transferred in 2016, then yes. ACTL2111 has been uploaded. For clarification, mreditor meant University WAM for the current program you are enrolled in Feb 10, 2011 Uni Grad Feels when your overall WAM dropped by nearly 2. Expecting that Admin law was a big reason for that. Aug 23, 2012 Uni Grad Feels when your overall WAM dropped by nearly 2. Expecting that Admin law was a big reason for that. How do you know your WAM dropped by 2 when the results are not out yet? Feb 10, 2011 Uni Grad How do you know your WAM dropped by 2 when the results are not out yet? The myunsw stream declaration 'trick' that has been discussed in the last few points. I think all 4 subjects have been included for me by now. Aug 14, 2013 how hard is it to get your wam up after doing pretty terribly in sem 1 LMAO are all law subjects wam killers or does it get better when you get the hang of what law is? (i.e. the bell curve thing) Users Who Are Viewing This Thread (Users: 0, Guests: 1)
{"url":"https://boredofstudies.org/threads/unsw-2016s1-results-thread.351512/page-8","timestamp":"2024-11-07T10:42:37Z","content_type":"text/html","content_length":"140724","record_id":"<urn:uuid:839232b6-0674-4c28-aca0-0494e6bb3207>","cc-path":"CC-MAIN-2024-46/segments/1730477027987.79/warc/CC-MAIN-20241107083707-20241107113707-00200.warc.gz"}
Runline Calculator – Accurate Sports Betting Tool The runline calculator helps you quickly determine the runline payouts based on specific game odds. Runline Calculator This calculator helps you determine whether a team covers the runline in a baseball game. The runline is essentially a point spread in baseball. How to Use • Enter the total runs scored by the team in the “Runs Scored” field. • Enter the total runs allowed by the team in the “Runs Allowed” field. • Input the runline. This is usually -1.5 or +1.5, indicating whether the team is favored or disadvantaged. • Click the “Calculate” button to determine if the team covered the runline. The result will display either “Win” or “Lose”. How It Works The calculator subtracts the runs allowed from the runs scored, then adds the runline: Result = (Runs Scored - Runs Allowed) + Runline If the result is greater than or equal to zero, the team covers the runline (“Win”). Otherwise, they do not (“Lose”). • This calculator only considers whole runs and might not fit scenarios where fractional runlines are involved outside of common -1.5/+1.5. • Ensure that all fields are populated with valid numbers to receive a correct calculation. Use Cases for This Calculator Calculating Basic Math Operations You can use the calculator to add, subtract, multiply, and divide numbers effortlessly. Simply input the numbers and the operator to get accurate results instantly with just a click. Calculating Percentage Need to calculate a percentage of a number? Use the percentage function on the calculator to quickly find the result without any hassle. It’s perfect for calculating discounts, tips, and more! Calculating Square Root When you need to find the square root of a number, the calculator can do it in a jiffy. Just enter the number and hit the square root button to get the precise value with ease. Calculating Exponents Whether you need to square a number or calculate higher exponents, the calculator can handle it smoothly. Input the base number and the exponent to get the correct result without any errors. Calculating Trigonometric Functions Working with angles? The calculator makes it simple to calculate trigonometric functions like sine, cosine, and tangent. Just enter the angle in degrees and let the calculator do the rest for you. Converting Units Quickly convert between different units like length, weight, volume, and more using the unit converter feature on the calculator. It’s a handy tool for swift and accurate unit conversions on the go. Calculating Mortgage Payments Planning to buy a house? Use the mortgage calculator to estimate your monthly mortgage payments based on the loan amount, interest rate, and loan term. It helps you make informed decisions about your home purchase. Calculating Body Mass Index (BMI) Curious about your BMI? Enter your height and weight into the calculator to find out your body mass index instantly. It’s a useful tool to track your fitness progress and set health goals. Calculating Time Differences Need to calculate the time difference between two dates or times? The calculator can help you determine the precise time gap in hours, minutes, and seconds. It’s perfect for scheduling events or managing deadlines effectively. Calculating Fuel Consumption Planning a road trip? Use the fuel consumption calculator to estimate how much fuel you’ll need based on your vehicle’s mileage and the distance of your journey. It’s a smart tool for budgeting your travel expenses accurately.
{"url":"https://madecalculators.com/runline-calculator/","timestamp":"2024-11-09T17:26:44Z","content_type":"text/html","content_length":"144900","record_id":"<urn:uuid:70f83d84-073b-4e99-9bbc-3e5c28fb045a>","cc-path":"CC-MAIN-2024-46/segments/1730477028125.59/warc/CC-MAIN-20241109151915-20241109181915-00107.warc.gz"}
What are orbital probability patterns? + Example What are orbital probability patterns? 1 Answer Once upon a time, you may have imagined that electrons move around in a trace-able way. Really though, we don't know its position if we know its speed and vice versa (Heisenberg Uncertainty Principle), so we only know the probability of finding it at some distance away from an orbital's center. Another term for "orbital probability pattern" is the orbital's radial density distribution. As an example, the following is the visual radial density distribution of the $1 s$ orbital: ...and the following graph describes the probability of an electron being found at a distance $r$ away from the center of the $1 s$ orbital, in x-axis units of ${a}_{0}$, where ${a}_{0} = 5.29177 \ times {10}^{- 11} m$ is the Bohr radius: The $1 s$ orbital's radial density distribution describes the probability density that you see as you start at the centerpoint of the orbital with a spherical viewing window of nothing, and start increasing the radius of that window (the x-axis value), plotting how often you see electrons as you do so. This "probability density" is the y-axis value. (Note that it does not mean more than two electrons are in one orbital, but that an electron shows up however often at however far away from the center of the orbital) Impact of this question 5115 views around the world
{"url":"https://socratic.org/questions/what-are-orbital-probability-patterns","timestamp":"2024-11-02T10:46:28Z","content_type":"text/html","content_length":"35767","record_id":"<urn:uuid:1d45a9ba-e342-4690-909e-64d0cefda9df>","cc-path":"CC-MAIN-2024-46/segments/1730477027710.33/warc/CC-MAIN-20241102102832-20241102132832-00130.warc.gz"}
The Stacks project Proposition 47.5.9 (Structure of injective modules over Noetherian rings). Let $R$ be a Noetherian ring. Every injective module is a direct sum of indecomposable injective modules. Every indecomposable injective module is the injective hull of the residue field at a prime. Comments (0) There are also: • 4 comment(s) on Section 47.5: Injective hulls Post a comment Your email address will not be published. Required fields are marked. In your comment you can use Markdown and LaTeX style mathematics (enclose it like $\pi$). A preview option is available if you wish to see how it works out (just click on the eye in the toolbar). All contributions are licensed under the GNU Free Documentation License. In order to prevent bots from posting comments, we would like you to prove that you are human. You can do this by filling in the name of the current tag in the following input field. As a reminder, this is tag 08YA. Beware of the difference between the letter 'O' and the digit '0'. The tag you filled in for the captcha is wrong. You need to write 08YA, in case you are confused.
{"url":"https://stacks.math.columbia.edu/tag/08YA","timestamp":"2024-11-05T12:53:34Z","content_type":"text/html","content_length":"15522","record_id":"<urn:uuid:0cf83fdf-8882-4ea9-9c3c-fc4caab19f32>","cc-path":"CC-MAIN-2024-46/segments/1730477027881.88/warc/CC-MAIN-20241105114407-20241105144407-00863.warc.gz"}
Comparing Bogo and Sleep Sorting Algorithms Published on Wednesday, October 9, 2024 Imagine you’re building an app and need to sort a massive list of data – maybe product prices, customer names, or high scores. Choosing the right sorting algorithm can make a huge difference in performance. Today, we’ll pit two popular contenders against each other: bogo and sleep. Before we dive into the code, let’s briefly explore the basics of both algorithms. If you’re eager to see the action, feel free to jump straight to the code comparison here. Bogo Sort Bogo Sort, also known as Stupid Sort or Permutation Sort, is a notoriously inefficient sorting algorithm that’s often used as a humorous example of poor programming practices. It’s considered one of the slowest sorting algorithms due to its incredibly inefficient approach. How It Works Bogo Sort operates on a simple (but incredibly inefficient) principle: 1. Randomize: Shuffle the elements of the array randomly. 2. Check for Order: Check if the array is now sorted. 3. Repeat: If the array is not sorted, go back to step 1 and repeat the process. Think of it like winning the lottery by randomly guessing the numbers until you get it right. It’s a highly unlikely scenario that relies on pure luck rather than a systematic approach. Time Complexity The worst-case and average-case time complexity of Bogo Sort is $O(n!)$, where n is the number of elements in the array. This makes it incredibly slow, especially for large datasets. In fact, it’s so slow that it’s practically unusable for anything but the smallest of arrays. Advantages and Disadvantages • Extremely simple to implement • Guaranteed to eventually sort the array (given enough time) • Incredibly inefficient • Not practical for any real-world use cases When to Use Bogo Sort Never. Bogo Sort is a joke algorithm that should never be used in a real-world application. It’s a cautionary tale about the importance of choosing efficient algorithms. In conclusion, Bogo Sort is a humorous example of a horribly inefficient sorting algorithm. While it’s a simple concept to grasp, its performance is so abysmal that it’s practically useless. Always opt for proven, efficient sorting algorithms like quicksort, merge sort, or heap sort when working on real-world projects. Sleep Sort Sleep Sort stands out as a unique and somewhat humorous entry in the world of sorting algorithms. It emerged in 2011 on the anonymous online forum 4chan and gained traction on the popular tech discussion platform Hacker News. A Lighthearted Approach Sleep sort takes a rather unconventional approach to sorting. It works by creating separate threads for each element in the input array. Each thread then “sleeps” for a duration proportional to the value of its corresponding element. Once a thread wakes up, it adds its element to a final sorted list. Think of it like this: Imagine sorting a list of tasks by their deadlines. Sleep sort would assign each task a separate worker. The worker for the task with the furthest deadline would sleep the longest, while the one with the closest deadline would wake up first. In the end, the tasks would be completed (and thus sorted) in order of their deadlines. Here’s a simplified breakdown of the process: 1. Thread Creation: For each element in the array, a separate thread is created. 2. Sleeping Beauty: Each thread sleeps for a time proportional to its associated element’s value. 3. Wake Up Call: When a thread wakes up, it adds its element to a final sorted list. 4. The Grand Finale: Once all threads finish sleeping, the final list contains the elements in sorted order. Not Exactly Lightning Speed While the concept is lighthearted and entertaining, sleep sort is not a champion for efficiency. Its time complexity is a hefty $O(n^2)$, which means its sorting time increases significantly as the list size grows. This makes it impractical for real-world applications where speed is a critical factor. A Learning Opportunity Despite its limitations as a practical tool, sleep sort offers a valuable learning experience. It showcases alternative approaches to sorting and highlights the importance of time complexity when choosing an algorithm. In conclusion, sleep sort serves as a reminder that sorting algorithms can be both innovative and entertaining. However, for real-world scenarios, it’s best to stick with established algorithms that deliver superior performance. The Clash We put both algorithms to the test with a battlefield of 3500 random numbers. Now, let’s see who emerges victorious! Now that we have some data to test on, we want to add the algorithm for the bogo sort. This goes as follows. And of course the sleep sort as well, otherwise we won’t have anything to compare against. Now, let’s test the two against one another. Delve deeper: For even more sorting options, explore our collection of sorting algorithms. Want to get your hands dirty with the code? Head over to bogo sort VS. sleep sort Implementation. The Winner Brace yourselves! The benchmark revealed that the sleep sort is a staggering Infinityx faster than its competitor! That translates to running the sleep sort almost 1 times in the time it takes the bogo sort to complete once! The A.I. Nicknames the Winners: We consulted a top-notch AI to give our champion a superhero nickname. From this day forward, the sleep sort shall be known as The Snooze Button! The bogo sort, while valiant, deserves recognition too. We present to you, The Random Rambler! The Choice is Yours, Young Padawan So, does this mean the sleep sort is the undisputed king of all sorting algorithms? Not necessarily. Different algorithms have their own strengths and weaknesses. But understanding their efficiency (which you can learn more about in the Big-O Notation post) helps you choose the best tool for the job! This vast world of sorting algorithms holds countless possibilities. Who knows, maybe you’ll discover the next champion with lightning speed or memory-saving magic! This showdown hopefully shed light on the contrasting speeds of bogo and sleep sorting algorithms. Stay tuned for more algorithm explorations on the blog.
{"url":"https://hasty.dev/blog/sorting/bogo-vs-sleep","timestamp":"2024-11-06T04:33:20Z","content_type":"text/html","content_length":"26460","record_id":"<urn:uuid:baa0953c-d7e6-452e-a35c-1eccb047c6b8>","cc-path":"CC-MAIN-2024-46/segments/1730477027909.44/warc/CC-MAIN-20241106034659-20241106064659-00728.warc.gz"}
Application oriented research topic of WIAS Reflectance measurements: forward and inverse problem The precise measurement of optical properties in the EUV-regime around 91.85 eV / 13.5 nm is still a challenge for the development of optical components such as photomasks or mirrors for EUV lithography. Due to the short wavelength in this spectral regime, contamination, oxidation, or surface roughness in the nanometer range will affect the optical response of a sample much stronger than, for instance, at optical wavelengths. The most common way to determine optical constants in the X-ray range is through transmission experiments, which measure the absorption of the radiation directly. Using the Kramers-Kronig relation, the complex refractive index can be determined from a measurement of the absorption. Since the penetration depth in the EUV regime is very low, ultra-thin and freestanding films are required for this type of measurements, which has an impact on the accuracy. Another major problem for transmission mode measurements is that the optical properties extracted from freestanding thin films are not always identical to thin layers within stratified systems as they are used in optical components. Measurements on samples that represent the structure of a real optical component, like thin films on a substrate, are therefore much more promising. Such can be studied by EUV reflectometry where the reflectance is measured as a function of wavelength and angle of incidence. A model for the reflectometry experiment, taking into account the effects of interdiffusion, surface contamination and roughness, is being used. It is based on a transfer matrix approach, similar to the M¨uller-matrix approach for data analysis in spectroscopic ellipsometry in the optical regime. Given the model of the experiment and the noise distribution, our prior statistical knowledge can be updated based on the measurement via the Bayes formula. In practice, however, only a pointwise value of the density of the Bayesian posterior, up to a multiplicative constant, is accessible. Thus, an efficient algorithm is required to sample or evaluate expectations of quantities of interest with respect to the posterior distribution. Relatively high dimension (possibly hundreds of parameters for multilayered samples, just as possible concentration, nonconvexity and multimodality of the posterior are also to be taken into account. Bayesian inverse problems via optimal transport and Wasserstein gradient flows Standard approaches based on representing the posterior density in some fixed functional basis (Eulerian view) quickly become practically infeasible due to the curse of dimensionality . Thus, it is common to resort to methods that generate a sequence of approximations that, in the limit, is distributed as the posterior (Lagrangian view). Notable examples of such methods include Markov chain Monte-Carlo (MCMC) and Langevin dynamics. Such methods can be trivially parallelized but suffer from long mixing times. Additionally, the sampling procedure has to be redone if the problem changes. The field of optimal transport (OT) and gradient flows has been flourishing in the last 20 years and recently has been brought into attention of the machine learning community. The application in Bayesian inverse problems is still quite new. Thus, the scope of our work lies in transfering theoretical knowledge in this field into practically applicable algorithms. The overall idea is to view the space of probability measures as a metric space with respect to an optimal-transport-based Wasserstein distance. The posterior can then be approached by succesively minimizing some dissimilarity functional, such as the KL divergence. A proximal method with respect to the Wasserstein distance, also called the Jordan-Kinderlehrer-Otto (JKO) scheme, plays the role of the (implicit) gradient descent method. The dynamic reformulation of the OT problem, due to Benamou and Brenier, additionally gives a vector field that interpolates between the two measures and can be used to propagate samples, thus bringing together the Eulerian and Lagrangian perspectives. The discretization of the scheme can be tackled with Artificial Neural Networks, sparse polynomial systems, tensor networks and possibly other modern compression techniques. One of the particular schemes that is being developed is a regularized Wasserstein proximal method. It can be interpreted as an Fischer information regularization of the aforementioned JKO scheme and considers entropic optimal transport between the iterates. The minimization problem is reformulated as a coupled system of PDEs in a new set of variables, which resemble the exponentials of the potentials from the Schrödinger bridge problem. Each of the variables encodes information on both the subsequent measure and on the interpolating vector field. The coupled system is solved as a fixed-point problem with Anderson Acceleration method, which drastically decreases the numerical burden. The spatial discretization is tackled with a tensor train decomposition. This low-rank method allows for compressed storage of the high-dimensional tensor and efficient computations of contractions and certain linear operators. Approximate low-rank reconstruction from pointwise observation is also possible by cross approximation. Solving the resulting ODE with the interpolating vector field as a right-hand-side gives a deterministic methods of approximate sampling without additional posterior calls. A Tensor Train model of the target density can possibly be reused in further experiments as a new prior in case when additional measurements are made (e.g. for more reflection angles) or new parameters added (e.g. optical parameteres at a new wavelength). • D. Hömberg, G. Hu, eds., Issue on the workshop ``Electromagnetics -- Modelling, Simulation, Control and Industrial Applications'', 8, no. 3 of Discrete Contin. Dyn. Syst. Ser. S, American Institute of Mathematical Sciences, Springfield, 2015, 259 pages, (Collection Published). • L.I. Goray, G. Schmidt, Chapter 12: Boundary Integral Equation Methods for Conical Diffraction and Short Waves, in: Gratings: Theory and Numerical Applications, Second Revisited Edition, E. Popov , ed., Université d'Aix-Marseille, Institut Fresnel, Marseille, 2014, pp. 12.1--12.86, (Chapter Published). • P. Deuflhard, M. Grötschel, D. Hömberg, U. Horst, J. Kramer, V. Mehrmann, K. Polthier, F. Schmidt, Ch. Schütte, M. Skutella, J. Sprekels, eds., MATHEON -- Mathematics for Key Technologies, 1 of EMS Series in Industrial and Applied Mathematics, European Mathematical Society Publishing House, Zurich, 2014, 453 pages, (Collection Published). Articles in Refereed Journals • G. Hu, A. Rathsfeld, Radiation conditions for the Helmholtz equation in a half plane filled by inhomogeneous periodic material, Journal of Differential Equations, 388 (2024), pp. 215--252, DOI 10.1016/j.jde.2024.01.008 . In this paper we consider time-harmonic acoustic wave propagation in a half-plane filled by inhomogeneous periodic medium. If the refractive index depends on the horizontal coordinate only, we define upward and downward radiating modes by solving a one-dimensional Sturm-Liouville eigenvalue problem with a complex-valued periodic coefficient. The upward and downward radiation conditions are introduced based on a generalized Rayleigh series. Using the variational method, we then prove uniqueness and existence for the scattering of an incoming wave mode by a grating located between an upper and lower half plane with such inhomogeneous periodic media. Finally, we discuss the application of the new radiation conditions to the scattering matrix algorithm, i.e., to rigorous coupled wave analysis or Fourier modal method. • X. Yu, G. Hu, W. Lu, A. Rathsfeld, PML and high-accuracy boundary integral equation solver for wave scattering by a locally defected periodic surface, SIAM Journal on Numerical Analysis, 60 (2022), pp. 2592--2625, DOI 10.1137/21M1439705 . This paper studies the perfectly-matched-layer (PML) method for wave scattering in a half space of homogeneous medium bounded by a two-dimensional, perfectly conducting, and locally defected periodic surface, and develops a high-accuracy boundary-integral-equation (BIE) solver. Along the vertical direction, we place a PML to truncate the unbounded domain onto a strip and prove that the PML solution converges to the true solution in the physical subregion of the strip with an error bounded by the reciprocal PML thickness. Laterally, we divide the unbounded strip into three regions: a region containing the defect and two semi-waveguide regions, separated by two vertical line segments. In both semi-waveguides, we prove the well-posedness of an associated scattering problem so as to well define a Neumann-to-Dirichlet (NtD) operator on the associated vertical segment. The two NtD operators, serving as exact lateral boundary conditions, reformulate the unbounded strip problem as a boundary value problem over the defected region. Due to the periodicity of the semi-waveguides, both NtD operators turn out to be closely related to a Neumann-marching operator, governed by a nonlinear Riccati equation. It is proved that the Neumann-marching operators are contracting, so that the PML solution decays exponentially fast along both lateral directions. The consequences culminate in two opposite aspects. Negatively, the PML solution cannot converge exponentially to the true solution in the whole physical region of the strip. Positively, from a numerical perspective, the Riccati equations can now be efficiently solved by a recursive doubling procedure and a high-accuracy PML-based BIE method so that the boundary value problem on the defected region can be solved efficiently and accurately. Numerical experiments demonstrate that the PML solution converges exponentially fast to the true solution in any compact subdomain of the strip. • G. Hu, A. Rathsfeld, W. Lu, Time-harmonic acoustic scattering from locally-perturbed periodic curves, SIAM Journal on Applied Mathematics, 81 (2021), pp. 2569--2595, DOI 10.1137/19M1301679 . We prove well-posedness for the time-harmonic acoustic scattering of plane waves from locally perturbed periodic surfaces in two dimensions under homogeneous Dirichlet boundary conditions. This covers sound-soft acoustic as well as perfectly conducting, TE polarized electromagnetic boundary value problems. Our arguments are based on a variational method in a truncated bounded domain coupled with a boundary integral representation. If the quasi-periodic Green's function to the unperturbed periodic scattering problem is calculated efficiently, then the variational approach can be used for a numerical scheme based on coupling finite elements with a boundary element algorithm. Even for a general 2D rough-surface problem, it turns out that the Green's function defined with the radiation condition ASR satisfies the Sommerfeld radiation condition over the half plane. Based on this result, for a local perturbation of a periodic surface, the scattered wave of an incoming plane wave is the sum of the scattered wave for the unperturbed periodic surface plus an additional scattered wave satisfying Sommerfeld's condition on the half plane. Whereas the scattered wave for the unperturbed periodic surface has a far field consisting of a finite number of propagating plane waves, the additional field contributes to the far field by a far-field pattern defined in the half-plane directions similarly to the pattern known for bounded obstacles. • W.M. Klesse, A. Rathsfeld, C. Gross, E. Malguth, O. Skibitzki, L. Zealouk, Fast scatterometric measurement of periodic surface structures in plasma-etching processes, Measurement, 170 (2021), pp. 108721/1--108721/12, DOI 10.1016/j.measurement.2020.108721 . To satisfy the continuous demand of ever smaller feature sizes, plasma etching technologies in microelectronics processing enable the fabrication of device structures with dimensions in the nanometer range. In a typical plasma etching system a plasma phase of a selected etching gas is activated, thereby generating highly energetic and reactive gas species which ultimately etch the substrate surface. Such dry etching processes are highly complex and require careful adjustment of many process parameters to meet the high technology requirements on the structure geometry. In this context, real-time access of the structure's dimensions during the actual plasma process would be of great benefit by providing full dimension control and film integrity in real-time. In this paper, we evaluate the feasibility of reconstructing the etched dimensions with nanometer precision from reflectivity spectra of the etched surface, which are measured in real-time throughout the entire etch process. We develop and test a novel and fast reconstruction algorithm, using experimental reflection spectra taken about every second during the etch process of a periodic 2D model structure etched into a silicon substrate. Unfortunately, the numerical simulation of the reflectivity by Maxwell solvers is time consuming since it requires separate time-harmonic computations for each wavelength of the spectrum. To reduce the computing time, we propose that a library of spectra should be generated before the etching process. Each spectrum should correspond to a vector of geometry parameters s.t. the vector components scan the possible range of parameter values for the geometrical dimensions. We demonstrate that by replacing the numerically simulated spectra in the reconstruction algorithm by spectra interpolated from the library, it is possible to compute the geometry parameters in times less than a second. Finally, to also reduce memory size and computing time for the library, we reduce the scanning of the parameter values to a sparse grid. • C. Brée, D. Gailevičius, V. Purlys, G.G. Werner, K. Staliunas, A. Rathsfeld, G. Schmidt, M. Radziunas, Chirped photonic crystal for spatially filtered optical feedback to a broad-area laser, Journal of Optics, 20 (2018), pp. 095804/1--095804/7, DOI 10.1088/2040-8986/aada98 . We derive and analyze an efficient model for reinjection of spatially filtered optical feedback from an external resonator to a broad area, edge emitting semiconductor laser diode. Spatial filtering is achieved by a chirped photonic crystal, with variable periodicity along the optical axis and negligible resonant backscattering. The optimal chirp is obtained from a genetic algorithm, which yields solutions that are robust against perturbations. Extensive numerical simulations of the composite system with our optoelectronic solver indicate that spatially filtered reinjection enhances lower-order transversal optical modes in the laser diode and, consequently, improves the spatial beam quality. • T. Abbas, H. Ammari, G. Hu, A. Wahab, J.Ch. Ye, Elastic scattering coefficients and enhancement of nearly elastic cloaking, Journal of Elasticity, 128 (2017), pp. 203--243, DOI 10.1007/ s10659-017-9624-7 . The concept of scattering coefficients has played a pivotal role in a broad range of inverse scattering and imaging problems in acoustic and electromagnetic media. In view of their promising applications, we introduce the notion of scattering coefficients of an elastic inclusion in this article. First, we define elastic scattering coefficients and substantiate that they naturally appear in the expansions of elastic scattered field and far field scattering amplitudes corresponding to a plane wave incidence. Then an algorithm is developed and analyzed for extracting the elastic scattering coefficients from multi-static response measurements of the scattered field. Moreover, the estimate of the maximal resolving order is provided in terms of the signal-to-noise ratio. The decay rate and symmetry of the elastic scattering coefficients are also discussed. Finally, we design scattering-coefficients-vanishing structures and elucidate their utility for enhancement of nearly elastic cloaking. • G. Hu, A. Kirsch, T. Yin, Factorization method in inverse interaction problems with bi-periodic interfaces between acoustic and elastic waves, Inverse Problems and Imaging, 10 (2016), pp. Consider a time-harmonic acoustic plane wave incident onto a doubly periodic (biperiodic) surface from above. The medium above the surface is supposed to be filled with homogeneous compressible inviscid fluid with a constant mass density, whereas the region below is occupied by an isotropic and linearly elastic solid body characterized by the Lamé constants. This paper is concerned with direct (or forward) and inverse fluid-solid interaction (FSI) problems with unbounded bi-periodic interfaces between acoustic and elastic waves. We present a variational approach to the forward interaction problem with Lipschitz interfaces. Existence of quasi-periodic solutions in Sobolev spaces is established at arbitrary frequency of incidence, while uniqueness is proved only for small frequencies or for all frequencies excluding a discrete set. Concerning the inverse problem, we show that the factorization method by Kirsch (1998) is applicable to the FSI problem in periodic structures. A computational criterion and a uniqueness result are justified for precisely characterizing the elastic body by utilizing the scattered acoustic near field measured in the • Y. Guo, D. Hömberg, G. Hu, J. Li, H. Liu, A time domain sampling method for inverse acoustic scattering problems, Journal of Computational Physics, 314 (2016), pp. 647--660. This work concerns the inverse scattering problems of imaging unknown/inaccessible scatterers by transient acoustic near-field measurements. Based on the analysis of the migration method, we propose efficient and effective sampling schemes for imaging small and extended scatterers from knowledge of time-dependent scattered data due to incident impulsive point sources. Though the inverse scattering problems are known to be nonlinear and ill-posed, the proposed imaging algorithms are totally “direct” involving only integral calculations on the measurement surface. Theoretical justifications are presented and numerical experiments are conducted to demonstrate the effectiveness and robustness of our methods. In particular, the proposed static imaging functionals enhance the performance of the total focusing method (TFM) and the dynamic imaging functionals show analogous behavior to the time reversal inversion but without solving time-dependent wave equations. • G. Hu, A. Rathsfeld, T. Yin, Finite element method to fluid-solid interaction problems with unbounded periodic interfaces, Numerical Methods for Partial Differential Equations. An International Journal, 32 (2016), pp. 5--35. Consider a time-harmonic acoustic plane wave incident onto a doubly periodic (biperiodic) surface from above. The medium above the surface is supposed to be filled with a homogeneous compressible inviscid fluid of constant mass density, whereas the region below is occupied by an isotropic and linearly elastic solid body characterized by its Lamé constants. This paper is concerned with a variational approach to the fluid-solid interaction problems with unbounded biperiodic Lipschitz interfaces between the domains of the acoustic and elastic waves. The existence of quasi-periodic solutions in Sobolev spaces is established at arbitrary frequency of incidence, while uniqueness is proved only for small frequencies or for all frequencies excluding a discrete set. A finite element scheme coupled with Dirichlet-to-Neumann mappings is proposed. The Dirichlet-to-Neumann mappings are approximated by truncated Rayleigh series expansions, and, finally, numerical tests in 2D are performed. • G. Hu, H. Liu, Nearly cloaking the elastic wave fields, Journal de Mathématiques Pures et Appliquées, 104 (2015), pp. 1045--1074. In this work, we develop a general mathematical framework on regularized approximate cloaking of elastic waves governed by the Lamé system via the approach of transformation elastodynamics. Our study is rather comprehensive. We first provide a rigorous justification of the transformation elastodynamics. Based on the blow-up-a-point construction, elastic material tensors for a perfect cloak are derived and shown to possess singularities. In order to avoid the singular structure, we propose to regularize the blow-up-a-point construction to be the blow-up-a-small-region construction. However, it is shown that without incorporating a suitable lossy layer, the regularized construction would fail due to resonant inclusions. In order to defeat the failure of the lossless construction, a properly designed lossy layer is introduced into the regularized cloaking construction . We derive sharp asymptotic estimates in assessing the cloaking performance. The proposed cloaking scheme is capable of nearly cloaking an arbitrary content with a high accuracy. • H. Gross, S. Heidenreich, M.-A. Henn, M. Bär, A. Rathsfeld, Modeling aspects to improve the solution of the inverse problem in scatterometry, Discrete and Continuous Dynamical Systems -- Series S, 8 (2015), pp. 497--519. • G. Hu, X. Liu, F. Qu, B. Zhang, Variational approach to rough surface scattering problems with Neumann and generalized impedance boundary conditions, Communications in Mathematical Sciences, 13 (2015), pp. 511--537. • G. Hu, A. Rathsfeld, Scattering of time-harmonic electromagnetic plane waves by perfectly conducting diffraction gratings, IMA Journal of Applied Mathematics, 80 (2015), pp. 508--532. Consider scattering of time-harmonic electromagnetic plane waves by a doubly periodic surface in $R^3$. The medium above the surface is supposed to be homogeneous and isotropic with a constant dielectric coefficient, while below is a perfectly conducting material. This paper is concerned with the existence of quasiperiodic solutions for any frequency of incidence. Based on an equivalent variational formulation established by the mortar technique of Nitsche, we verify the existence of solutions for a broad class of incident waves including plane waves, under the assumption that the grating profile is a Lipschitz biperiodic surface. Our solvability result covers the resonance case where a Rayleigh frequency is allowed. Non-uniqueness examples are also presented in the resonance case and the TE or TM polarization case for classical gratings. • TH. Arnold, A. Rathsfeld, Reflection of plane waves by rough surfaces in the sense of Born approximation, Mathematical Methods in the Applied Sciences, 37 (2014), pp. 2091--2111. The topic of the present paper is the reflection of electromagnetic plane waves by rough surfaces, i.e., by smooth and bounded perturbations of planar faces. Moreover, the contrast between the cover material and the substrate beneath the rough surface is supposed to be low. In this case, a modification of Stearns' formula based on Born approximation and Fourier techniques is derived for a special class of surfaces. This class contains the graphs of functions if the interface function is a radially modulated almost periodic function. For the Born formula to converge, a sufficient and almost necessary condition is given. A further technical condition is defined, which guarantees the existence of the corresponding far field of the Born approximation. This far field contains plane waves, far-field terms like those for bounded scatterers, and, additionally, a new type of terms. The derived formulas can be used for the fast numerical computations of far fields and for the statistics of random rough surfaces. • G. Hu, A. Rathsfeld, Convergence analysis of the FEM coupled with Fourier-mode expansion for the electromagnetic scattering by biperiodic structures, Electronic Transactions on Numerical Analysis, 41 (2014), pp. 350--375. • G. Schmidt, Conical diffraction by multilayer gratings: A recursive integral equations approach, Applications of Mathematics, 58 (2013), pp. 279--307. In this paper we consider an integral equation algorithm to study the scattering of plane waves by multilayer diffraction gratings under oblique incidence. The scattering problem is described by a system of Helmholtz equations with piecewise constant coefficients in $R^2$ coupled by special transmission conditions at the interfaces between different layers. Boundary integral methods lead to a system of singular integral equations, containing at least two equations for each interface. To deal with an arbitrary number of material layers we present the extension of a recursive procedure developed by Maystre for normal incidence, which transforms the problem to a sequence of equations with $2 times 2$ operator matrices on each interface. Necessary and sufficient conditions for the applicability of the algorithm are derived. • G. Hu, F. Qu, B. Zhang, A linear sampling method for inverse problems of diffraction gratings of mixed type, Mathematical Methods in the Applied Sciences, 35 (2012), pp. 1047--1066. • G. Hu, Inverse wave scattering by unbounded obstacles: Uniqueness for the two-dimensional Helmholtz equation, Applicable Analysis. An International Journal, 91 (2012), pp. 703--717. In this paper we present some uniqueness results on inverse wave scattering by unbounded obstacles for the two-dimensional Helmholtz equation. We prove that an impenetrable one-dimensional rough surface can be uniquely determined by the values of the scattered field taken on a line segment above the surface that correspond to the incident waves generated by a countable number of point sources. For penetrable rough layers in a piecewise constant medium, the refractive indices together with the rough interfaces (on which the TM transmission conditions are imposed) can be uniquely identified using the same measurements and the same incident point source waves. Moreover, a Dirichlet polygonal rough surface can be uniquely determined by a single incident point source wave provided a certain condition is imposed on it. • L. Goray, G. Schmidt, Analysis of two-dimensional photonic band gaps of any rod shape and conductivity using a conical-integral-equation method, Physical Review E. Statistical, Nonlinear, and Soft Matter Physics, 85 (2012), pp. 036701/1--036701/12. The conical boundary integral equation method has been proposed to calculate the sensitive optical response of 2D photonic band gaps (PBGs), including dielectric, absorbing, and high-conductive rods of various shapes working in any wavelength range. It is possible to determine the diffracted field by computing the scattering matrices separately for any grating boundary profile. The computation of the matrices is based on the solution of a 2 x 2 system of singular integral equations at each interface between two different materials. The advantage of our integral formulation is that the discretization of the integral equations system and the factorization of the discrete matrices, which takes the major computing time, are carried out only once for a boundary. It turned out that a small number of collocation points per boundary combined with a high convergence rate can provide adequate description of the dependence on diffracted energy of very different PBGs illuminated at arbitrary incident and polarization angles. The numerical results presented describe the significant impact of rod shape on diffraction in PBGs supporting polariton-plasmon excitation, particularly in the vicinity of resonances and at high filling ratios. The diffracted energy response calculated vs. array cell geometry parameters was found to vary from a few percent up to a few hundred percent. The influence of other types of anomalies (i.e. waveguide anomalies, cavity modes, Fabry-Perot and Bragg resonances, Rayleigh orders, etc), conductivity, and polarization states on the optical response has been demonstrated. • H. Gross, M.-A. Henn, S. Heidenreich, A. Rathsfeld, M. Bär, Modeling of line roughness and its impact on the diffraction intensities and the reconstructed critical dimensions in scatterometry, Applied Optics, 51 (2012), pp. 7384--7394. We investigate the impact of line edge and line width roughness (LER, LWR) on the measured diffraction intensities in angular resolved extreme ultraviolet (EUV) scatterometry for a periodic line-space structure designed for EUV lithography. LER and LWR with typical amplitudes of a few nanometers were previously neglected in the course of the profile reconstruction. The 2D rigorous numerical simulations of the diffraction process for periodic structures are carried out with the finite element method (FEM) providing a numerical solution of the two-dimensional Helmholtz equation. To model roughness, multiple calculations are performed for domains with large periods, containing many pairs of line and space with stochastically chosen line and space widths. A systematic decrease of the mean efficiencies for higher diffraction orders along with increasing variances is observed and established for different degrees of roughness. In particular, we obtain simple analytical expressions for the bias in the mean efficiencies and the additional uncertainty contribution stemming from the presence of LER and/or LWR. As a consequence this bias can easily be included into the reconstruction model to provide accurate values for the evaluated profile parameters. We resolve the sensitivity of the reconstruction from this bias by using the LER/LWR perturbed efficiency datasets for multiple reconstructions. If the scattering efficiencies are bias-corrected, significant improvements are found in the reconstructed bottom and top widths toward the nominal values. • M.-A. Henn, S. Heidenreich, H. Gross, A. Rathsfeld, F. Scholze, M. Bär, Improved grating reconstruction by determination of line roughness in extreme ultraviolet scatterometry, Optics Letters, 37 (2012), pp. 5229--5231. • C. Pfüller, M. Ramsteiner, O. Brandt, F. Grosse, A. Rathsfeld, G. Schmidt, L. Geelhaar, H. Riechert, Raman spectroscopy as a probe for the coupling of light into ensembles of sub-wavelength-sized nanowires, Applied Physics Letters, 101 (2012), pp. 083104/1--083104/4. • G. Hu, B. Zhang, The linear sampling method for the inverse electromagnetic scattering by a partially coated bi-periodic structure, Mathematical Methods in the Applied Sciences, 34 (2011), pp. • F. Lanzara, V. Maz'ya, G. Schmidt, On the fast computation of high dimensional volume potentials, Mathematics of Computation, 80 (2011), pp. 887--904. • F. Lanzara, V.G. Maz'ya, G. Schmidt, Accurate cubature of volume potentials over high-dimensional half-spaces, Journal of Mathematical Sciences (New York), 173 (2011), pp. 683--700. • J. Elschner, G. Hu, Inverse scattering of elastic waves by periodic structures: Uniqueness under the third or fourth kind boundary conditions, Methods and Applications of Analysis, 18 (2011), pp. The inverse scattering of a time-harmonic elastic wave by a two-dimensional periodic structure in $R^2$ is investigated. The grating profile is assumed to be a graph given by a piecewise linear function on which the third or fourth kind boundary conditions are satisfied. Via an equivalent variational formulation, existence of quasi-periodic solutions for general Lipschitz grating profiles is proved by applying the Fredholm alternative. However, uniqueness of solution to the direct problem does not hold in general. For the inverse problem, we determine and classify all the unidentifiable grating profiles corresponding to a given incident elastic field, relying on the reflection principle for the Navier equation and the rotational invariance of propagating directions of the total field. Moreover, global uniqueness for the inverse problem is established with a minimal number of incident pressure or shear waves, including the resonance case where a Rayleigh frequency is allowed. The gratings that are unidentifiable by one incident elastic wave provide non-uniqueness examples for appropriately chosen wave number and incident angles. • J. Elschner, G. Hu, Uniqueness in inverse scattering of elastic waves by three-dimensional polyhedral diffraction gratings, Journal of Inverse and Ill-Posed Problems, 19 (2011), pp. 717--768. We consider the inverse elastic scattering problem of determining a three-dimensional diffraction grating profile from scattered waves measured above the structure. In general, a grating profile cannot be uniquely determined by a single incoming plane wave. We completely characterize and classify the bi-periodic polyhedral structures under the boundary conditions of the third and fourth kinds that cannot be uniquely recovered by only one incident plane wave. Thus we have global uniqueness for a polyhedral grating profile by one incident elastic plane wave if and only if the profile belongs to neither of the unidentifiable classes, which can be explicitly described depending on the incident field and the type of boundary conditions. Our approach is based on the reflection principle for the Navier equation and the reflectional and rotational invariance of the total field. • J. Elschner, G. Hu, Uniqueness in inverse transmission scattering problems for multilayered obstacles, Inverse Problems and Imaging, 5 (2011), pp. 793--813. Assume a time-harmonic electromagnetic wave is scattered by an infinitely long cylindrical conductor surrounded by an unknown piecewise homogenous medium remaining invariant along the cylinder axis. We prove that, in TM mode, the far field patterns for all observation directions at a fixed frequency uniquely determine the unknown surrounding medium as well as the shape of the cylindrical conductor. A similar uniqueness result is obtained for the scattering by multilayered penetrable periodic structures in a piecewise homogenous medium. The periodic interfaces and refractive indices can be uniquely identified from the near field data measured only above (or below) the structure for all quasi-periodic incident waves with a fixed phase-shift. The proofs are based on the singularity of the Green function to a two dimensional elliptic equation with piecewise constant leading coefficients. • G. Schmidt, B.H. Kleemann, Integral equation methods from grating theory to photonics: An overview and new approaches for conical diffraction, Journal of Modern Optics, 58 (2011), pp. 407--423. • G. Schmidt, Integral equations for conical diffraction by coated gratings, Journal of Integral Equations and Applications, 23 (2011), pp. 71--112. The paper is devoted to integral formulations for the scattering of plane waves by diffraction gratings under oblique incidence. For the case of coated gratings Maxwell's equations can be reduced to a system of four singular integral equations on the piecewise smooth interfaces between different materials. We study analytic properties of the integral operators for periodic diffraction problems and obtain existence and uniqueness results for solutions of the systems corresponding to electromagnetic fields with locally finite energy. • G. Hu, F. Qu, B. Zhang, Direct and inverse problems for electromagnetic scattering by a doubly periodic structure with a partially coated dielectric, Mathematical Methods in the Applied Sciences, 33 (2010), pp. 147--156. • S. Chandler-Wilde, J. Elschner, Variational approach in weighted Sobolev spaces to scattering by unbounded rough surface, SIAM Journal on Mathematical Analysis, 42 (2010), pp. 2554--2580. • L.I. Goray, G. Schmidt, Solving conical diffraction grating problems with integral equations, Journal of the Optical Society of America A. Optics, Image Science, and Vision, 27 (2010), pp. Off-plane scattering of time-harmonic plane waves by a diffraction grating with arbitrary conductivity and general border profile is considered in a rigorous electromagnetic formulation. The integral equations for conical diffraction were obtained using the boundary integrals of the single and double layer potentials including the tangential derivative of single layer potentials interpreted as singular integrals. We derive an important formula for the calculation of the absorption in conical diffraction. Some rules which are expedient for the numerical implementation of the theory are presented. The efficiencies and polarization angles compared with those obtained by Lifeng Li for transmission and reflection gratings are in a good agreement. The code developed and tested is found to be accurate and efficient for solving off-plane diffraction problems including high-conductive surfaces, borders with edges, real border profiles, and gratings working at short wavelengths. • H. Gross, J. Richter, A. Rathsfeld, M. Bär, Investigations on a robust profile model for the reconstruction of 2D periodic absorber lines in scatterometry, Journal of the European Optical Society - Rapid Publications, 5 (2010), pp. 10053/1--10053/7. • J. Elschner, G. Hu, Global uniqueness in determining polygonal periodic structures with a minimal number of incident plane waves, Inverse Problems. An International Journal on the Theory and Practice of Inverse Problems, Inverse Methods and Computerized Inversion of Data, 26 (2010), pp. 115002/1--115002/23. In this paper, we investigate the inverse problem of recovering a two-dimensional perfectly reflecting diffraction grating from the scattered waves measured above the structure. Inspired by a novel idea developed by Bao, Zhang and Zou [to appear in Trans. Amer. Math. Soc.], we present a complete characterization of the global uniqueness in determining polygonal periodic structures using a minimal number of incident plane waves. The idea in this paper combines the reflection principle for the Helmholtz equation and the dihedral group theory. We characterize all periodic polygonal structures that cannot be identified by one incident plane wave, including the resonance case where a Rayleigh frequency is allowed. Furthermore, we show that those unidentifiable gratings provide non-uniqueness examples for appropriately chosen wave number and incident angles. We also indicate and fix a gap in the proof of the main theorem of Elschner and Yamamoto [Z. Anal. Anwend., 26 (2007), 165-177], and generalize the uniqueness results of that paper. • H. Gross, A. Rathsfeld, F. Scholze, M. Bär, Profile reconstruction in extreme ultraviolet (EUV) scatterometry: Modeling and uncertainty estimates, Measurement Science and Technology, 20 (2009), pp. 105102/1--105102/11. Scatterometry as a non-imaging indirect optical method in wafer metrology is also relevant to lithography masks designed for Extreme Ultraviolet Lithography, where light with wavelengths in the range of 13 nm is applied. The solution of the inverse problem, i.e. the determination of periodic surface structures regarding critical dimensions (CD) and other profile properties from light diffraction patterns, is incomplete without knowledge of the uncertainties associated with the reconstructed parameters. With decreasing feature sizes of lithography masks, increasing demands on metrology techniques and their uncertainties arise. The numerical simulation of the diffraction process for periodic 2D structures can be realized by the finite element solution of the two-dimensional Helmholtz equation. For typical EUV masks the ratio period over wave length is so large, that a generalized finite element method has to be used to ensure reliable results with reasonable computational costs. The inverse problem can be formulated as a non-linear operator equation in Euclidean spaces. The operator maps the sought mask parameters to the efficiencies of diffracted plane wave modes. We employ a Gauß-Newton type iterative method to solve this operator equation and end up minimizing the deviation of the measured efficiency or phase shift values from the calculated ones. We apply our reconstruction algorithm for the measurement of a typical EUV mask composed of TaN absorber lines of about 80 nm height, a period of 420 nm resp. 720 nm, and with an underlying MoSi-multilayer stack of 300 nm thickness. Clearly, the uncertainties of the reconstructed geometric parameters essentially depend on the uncertainties of the input data and can be estimated by various methods. We apply a Monte Carlo procedure and an approximative covariance method to evaluate the reconstruction algorithm. Finally, we analyze the influence of uncertainties in the widths of the multilayer stack by the Monte Carlo method. • H. Gross, A. Rathsfeld, Sensitivity analysis for indirect measurement in scatterometry and the reconstruction of periodic grating structures, Waves in Random and Complex Media. Propagation, Scattering and Imaging, 18 (2008), pp. 129--149. • J. Elschner, M. Yamamoto, Uniqueness in determining polygonal periodic structures, Zeitschrift fur Analysis und ihre Anwendungen. Journal for Analysis and its Applications, 26 (2007), pp. • H. Gross, R. Model, M. Bär, M. Wurm, B. Bodermann, A. Rathsfeld, Mathematical modelling of indirect measurements in scatterometry, Measurement, 39 (2006), pp. 782--794. • A. Rathsfeld, G. Schmidt, B.H. Kleemann, On a fast integral equation method for diffraction gratings, Communications in Computational Physics, 1 (2006), pp. 984-1009. • G. Bruckner, J. Elschner, The numerical solution of an inverse periodic transmission problem, Mathematical Methods in the Applied Sciences, 28 (2005), pp. 757--778. • J. Elschner, M. Yamamoto, Uniqueness results for an inverse periodic transmission problem, Inverse Problems. An International Journal on the Theory and Practice of Inverse Problems, Inverse Methods and Computerized Inversion of Data, 20 (2004), pp. 1841--1852. • G. Bao, K. Huang, G. Schmidt, Optimal design of nonlinear diffraction gratings, Journal of Computational Physics, 184 (2003), pp. 106--121. • G. Bruckner, J. Elschner, A two-step algorithm for the reconstruction of perfectly reflecting periodic profiles, Inverse Problems. An International Journal on the Theory and Practice of Inverse Problems, Inverse Methods and Computerized Inversion of Data, 19 (2003), pp. 315--329. • J. Elschner, G.C. Hsiao, A. Rathsfeld, Grating profile reconstruction based on finite elements and optimization techniques, SIAM Journal on Applied Mathematics, 64 (2003), pp. 525--545. • J. Elschner, G. Schmidt, M. Yamamoto, An inverse problem in periodic diffractive optics: Global uniqueness with a single wavenumber, Inverse Problems. An International Journal on the Theory and Practice of Inverse Problems, Inverse Methods and Computerized Inversion of Data, 19 (2003), pp. 779--787. • J. Elschner, G. Schmidt, M. Yamamoto, Global uniqueness in determining rectangular periodic structures by scattering data with a single wave number, Journal of Inverse and Ill-Posed Problems, 11 (2003), pp. 235--244. • J. Elschner, G. Schmidt, Conical diffraction by periodic structures: Variation of interfaces and gradient formulas, Mathematische Nachrichten, 252 (2003), pp. 24--42. Contributions to Collected Editions • C. Brée, V. Raab, D. Gailevičius, V. Purlys, J. Montiel, G.G. Werner, K. Staliunas, A. Rathsfeld, U. Bandelow, M. Radziunas, Genetically optimized photonic crystal for spatial filtering of reinjection into broad-area diode lasers, in: 2019 Conference on Lasers and Electro-Optics Europe and European Quantum Electronics Conference, OSA Technical Digest, IEEE, Piscataway, 2019, pp. 1--1, DOI 10.1109/CLEO-EQEC.2019.8871622 . Modern high-power broad-area semiconductor laser diodes (BASLDs) deliver optical output powers of several ten Watts at high electro-optical conversion efficiencies, which makes them highly relevant for numerous industrial, medical and scientific applications. However, lateral multimode behavior in BASLDs due to thermal lensing turns out highly detrimental, as it results in poor focusability and decreased laser beam brightnesss. Approaches to overcome this issue include improved epitaxial layer design, the optimization of evanescent spatial filtering by tailoring the emitter geometry and facet reflectivity, or Fourier spatially filtered reinjection from an external resonator [1]. • G. Schmidt, Boundary integral methods for periodic scattering problems, in: Around the Research of Vladimir Maz'ya II. Partial Differential Equations, A. Laptev, ed., 12 of International Mathematical Series, Springer Science+Business Media, New York [et al.], 2010, pp. 337--363. • H. Gross, F. Scholze, A. Rathsfeld, M. Bär, Evaluation of measurement uncertainties in EUV scatterometry, in: Modeling Aspects in Optical Metrology II, H. Bosse, B. Bodermann, R.M. Silver, eds., 7390 of Proceedings of SPIE, SPIE, 2009, pp. 7390OT/1--7390OT/11. • H. Gross, A. Rathsfeld, M. Bär, Modelling and uncertainty estimates for numerically reconstructed profiles in scatterometry, in: Advanced Mathematical and Computational Tools in Metrology and Testing VIII, F. Pavese, M. Bär, A.B. Forbes, J.M. Linares, C. Perruchet, N.F. Zhang, eds., 78 of Series on Advances in Mathematics for Applied Sciences, World Scientific, Singapore, 2009, pp. • M.-A. Henn, R. Model, M. Bär, M. Wurm, B. Bodermann, A. Rathsfeld, H. Gross, On numerical reconstructions of lithographic masks in DUV scatterometry, in: Modeling Aspects in Optical Metrology II, H. Bosse, B. Bodermann, R.M. Silver, eds., 7390 of Proceedings of SPIE, SPIE, 2009, pp. 7390OQ/1--7390OQ/11. • H. Gross, R. Model, A. Rathsfeld, F. Scholze, M. Wurm, B. Bodermann, M. Bär, Modellbildung, Bestimmung der Messunsicherheit und Validierung für diskrete inverse Probleme am Beispiel der Scatterometrie, in: Sensoren und Messsysteme, 14. Fachtagung Ludwigsburg, 11./12. März 2008, 2011 of VDI-Berichte, VDI, 2008, pp. 337--346. • R. Model, A. Rathsfeld, H. Gross, M. Wurm, B. Bodermann, A scatterometry inverse problem in optical mask metrology, in: 6th International Conference on Inverse Problems in Engineering: Theory and Practice, 15--19 June 2008, Dourdan (Paris), France, 135 of J. Phys.: Conf. Ser., Inst. Phys., 2008, pp. 012071/1--012071/8. • H. Gross, A. Rathsfeld, F. Scholze, M. Bär, U. Dersch, Optimal sets of measurement data for profile reconstruction in scatterometry, in: Modeling Aspects in Optical Metrology, H. Bosse, B. Bodermann, R.M. Silver, eds., 6617 of Proceedings of SPIE, 2007, pp. 66171B/1--66171B/12. • M. Wurm, B. Bodermann, F. Scholze, Ch. Laubis, H. Gross, A. Rathsfeld, Untersuchung zur Eignung der EUV-Scatterometrie zur quantitativen Charakterisierung periodischer Strukturen auf Photolithographiemasken, in: Proc. of the 107th Meeting of DGaO (German Branch of the European Optical Society), June 6--10, 2006, in Weingarten, DGaO-Proceedings, 2006, pp. P74/1--P74/2. • P. DE Bisschop, A. Erdmann, A. Rathsfeld, Simulation of the effect of a resist-surface bound air bubble on imaging in immersion lithography, in: Optical Microlithography XVIII, B.W. Smith, ed., 5754 of Proceedings of SPIE, 2005, pp. 243--253. • G. Bruckner, J. Elschner, M. Yamamoto, An optimization method for the grating profile reconstruction, Proceedings 3rd ISAAC Congress, Berlin, August 20 - 25, 2001, H.G.W. Begehr, R.P. Gilbert, M.W. Wong, eds., II of Progress in Analysis, World Scientific, New Jersey [u.a.], 2003, pp. 1391--1404. • J. Elschner, R. Hinder, G. Schmidt, Direct and inverse problems for diffractive structures --- Optimization of binary gratings, in: Mathematics --- Key Technology for the Future. Joint Projects Between Universities and Industry, W. Jäger, H.-J. Krebs, eds., Springer, Berlin [u.a.], 2003, pp. 293--304. • G. Schmidt, Electromagnetic scattering by periodic structures (in Russian), Proceedings of the International Conference on Differential and Functional Differential Equations, Moscow, Russian Federation, August 11 - 17, 2002, 3 of Sovrem. Probl. Mat. Fund. Naprav., 2003, pp. 113--128. Preprints, Reports, Technical Reports • G. Schmidt, Benutzer-Handbuch Dipog-1.4, Technical Report no. 8, WIAS, Berlin, 2004, DOI 10.20347/WIAS.TECHREPORT.8 . This is the manual of the software package DIPOG, version 1.4, which can be used to simulate and optimize binary and multilevel optical gratings. The algorithms are based on the finite--element solution of a system of Helmholtz equations, which are equivalent to the timeharmonic electromagnetic field equations, and on gradient methods for solving optimization problems. The package offers several options to postprocess the calculated electromagnetic fields. • A. Rathsfeld, DIPOG-2.0. User Guide. Direct Problems for Optical Gratings over Triangular Grids, Technical Report no. 7, WIAS, Berlin, 2004, DOI 10.20347/WIAS.TECHREPORT.7 . This is the description of how to use the programs FEM, GFEM, FEM_CHECK, GFEM_CHECK, FEM_PLOT, GFEM_PLOT, FEM_FULLINFO, and GFEM_FULLINFO of the package DIPOG-2.0. The package is a collection of finite element (FEM) programs to determine the efficiencies of the diffraction of light by a periodic grating structure. It is based on the software package PDELIB and solves the classical case of TE and TM polarization and the case of conical diffraction. The code provides a conventional FEM and a generalized FEM (GFEM). The latter is the variational approach of the conventional FEM combined with a new trial space. We note that the DIPOG-2.0 programs require the installation of the previous version DIPOG-1.3 or DIPOG-1.4, of the grid generator TRIANGLE-1.4, and of the equations solver PARDISO. Additionally, some of them need the graphical package openGL (or the MESA emulation of openGL) together with GLTOOLS-2.4 or, alternatively, the package GNUPLOT. Examples of data and output files are enclosed. • A. Rathsfeld, Convergence of the method of rigorous coupled-wave analysis for the diffraction by two-dimensional periodic surface structures, Preprint no. 3081, WIAS, Berlin, 2023, DOI 10.20347/ WIAS.PREPRINT.3081 . Abstract, PDF (424 kByte) The scattering matrix algorithm is a popular numerical method to simulate the diffraction of optical waves by periodic surfaces. The computational domain is divided into horizontal slices and, by a domain decomposition method coupling neighbour slices over the common interface via scattering data, a clever recursion is set up to compute an approximate operator, mapping incoming waves into outgoing. Combining this scattering matrix algorithm with numerical schemes inside the slices, methods like rigorous coupled wave analysis and Fourier modal methods were designed. The key for the analysis is the scattering problem over the slices. These are scattering problems with a radiation condition generalized for inhomogeneous cover and substrate materials and were first analyzed in [7]. In contrast to [7], where the scattering matrix algorithm for transverse electric polarization was treated without full discretization (no approximation by truncated Fourier series), we discuss the more challenging case of transverse magnetic polarization and look at the convergence of the fully-discretized scheme, i.e., at the rigorous coupled wave analysis for a fixed slicing into layers with vertically invariant optical index. • A. Rathsfeld, On a half-space radiation condition, Preprint no. 2669, WIAS, Berlin, 2019, DOI 10.20347/WIAS.PREPRINT.2669 . Abstract, PDF (430 kByte) For the Dirichlet problem of the Helmholtz equation over the half space or rough surfaces, a radiation condition is needed to guarantee a unique solution, which is physically meaningful. If the Dirichlet data is a general bounded continuous function, then the well-established Sommerfeld radiation condition, the angular spectrum representation, and the upward propagating radiation condition do not apply or require restrictions on the data, in order to define the involved integrals. In this paper a new condition based on a representation of the second derivative of the solution is proposed. The twice differentiable half-space Green's function is integrable and the corresponding radiation condition applies to general bounded functions. The condition is checked for special functions like plane waves and point source solution. Moreover, the Dirichlet problem for the half plane is discussed. Note that such a "continuous" radiation condition is helpful e.g. if finite sections of the rough-surface problem are analyzed. • G. Schmidt, Scattering of general incident beams by diffraction gratings, Preprint no. 2355, WIAS, Berlin, 2016, DOI 10.20347/WIAS.PREPRINT.2355 . Abstract, PDF (1036 kByte) The paper is devoted to the electromagnetic scattering of arbitrary time-harmonic fields by periodic structures. The Floquet-Fourier transform converts the full space Maxwell problem to a two-parameter family of diffraction problems with quasiperiodic incidence waves, for which conventional grating methods become applicable. The inverse transform is given by integrating with respect to the parameters over a infinite strip in ℝ². For the computation of the scattered fields we propose an algorithm, which extends known adaptive methods for the approximate calculation of multiple integrals. The novel adaptive approach provides autonomously the expansion of the incident field into quasiperiodic waves in order to approximate the scattered fields within a prescribed error tolerance. Some application examples are numerically examined. Talks, Poster • A. Rathsfeld, Analysis of scattering matrix algorithm for diffraction by periodic structures (online talk), University of Tokyo, Graduate School of Mathematical Sciences, Japan, March 13, 2024. • V. Aksenov, Simulation of Wasserstein gradient flows with low-rank tensor methods for sampling, 10th International Congress on Industrial and Applied Mathematics (ICIAM 2023), Minisymposium 00837 ``Particle Methods for Bayesian Inference'', August 20 - 25, 2023, Waseda University, Tokyo, Japan, August 24, 2023. • A. Rathsfeld, Analysis of scattering matrix algorithm, 10th International Congress on Industrial and Applied Mathematics (ICIAM 2023), Minisymposium 00297 ``Wave Scattering Problems: Numerical Methods with Applications'', August 20 - 25, 2023, Waseda University, Tokyo, Japan, August 22, 2023. • A. Rathsfeld, Analysis of scattering matrix algorithm for diffraction by periodic surface structures, Chinese Academy of Sciences, Institute of Computational Mathematics and Scientific/ Engineering Computing, Beijing, China, September 4, 2023. • A. Rathsfeld, Plane-wave scattering by biperiodic gratings and rough surfaces: Radiation condition and far-field model, Nankai University, Department of Scientific and Engineering Computing, Tianjin, China, September 7, 2023. • A. Rathsfeld, Plane-wave scattering by biperiodic gratings and rough surfaces: Radiation condition and far-field model (online talk), University of Tokyo, Graduate School of Mathematical Sciences, Japan, October 26, 2023. • A. Rathsfeld, Simulation and inverse problems for rough surfaces by numerical methods of periodic grating structures (online talk), University of Tokyo, Graduate School of Mathematical Sciences, Japan, November 29, 2023. • C. Brée, V. Raab, D. Gailevičius, V. Purlys, J. Montiel, G.G. Werner, K. Staliunas, A. Rathsfeld, U. Bandelow, M. Radziunas, Genetically optimized photonic crystal for spatial filtering of reinjection into broad-area diode lasers, CLEO/Europe-EQEC 2019, Munich, June 23 - 27, 2019. • G. Hu, Direct and inverse problems in elastodynamics, Workshop ``Theory, Numerics and Application of Partial Differential Equations'', December 10, 2016, Chinese Academy of Sciences, Beijing, December 10, 2016. • G. Hu, Inverse medium scattering problems, Workshop on Inverse Problems and their Applications, November 18 - 20, 2016, Southeast University, Nanjing, China, November 20, 2016. • D.R.M. Renger, Functions of bounded variation with an infinite-dimensional codomain, Meeting in Applied Mathematics and Calculus of Variations, September 13 - 16, 2016, Università di Roma ``La Sapienza'', Dipartimento di Matematica ``Guido Castelnuovo'', Italy, September 16, 2016. • G. Schmidt, Scattering of general incident beams by diffraction gratings, European Optical Society Annual Meeting (EOSAM) 2016, September 26 - 30, 2016, Berlin, September 28, 2016. • G. Hu, Mathematical problems in time-harmonic wave scattering, Shandong Normal University, Department of Mathematics, Jinan, China, November 11, 2014. • G. Hu, Mathematical problems in time-harmonic wave scattering from bounded and unbounded obstacles, South University of Science and Technology of China (SUSTC), Department of Financial Mathematics and Financial Engineering, Shenzhen, November 3, 2014. • J. Elschner, Direct and inverse problems for diffraction gratings, Symposium ``Light Scattering: Simulation and Inversion'', May 27 - 28, 2013, Bremen, May 28, 2013. • G. Schmidt, On an integral equation formulation for scattering by biperiodic structures, Analysis of Partial Differential Equations, A Symposium in honour of Prof. Vladimir Maz'ya on the occasion of his 75th birthday, December 16 - 17, 2013, University of Liverpool, UK, December 17, 2013. • G. Hu, Direct and inverse scattering of elastic waves by diffraction gratings, 6th International Conference ``Inverse Problems, Control and Shape Optimization'' (PICOF '12), April 2 - 4, 2012, Palaiseau, France, April 4, 2012. • G. Hu, Direct and inverse scattering of elastic waves by diffraction gratings, Workshop 3 ``Wave Propagation and Scattering, Inverse Problems and Applications in Energy and the Environment'', November 21 - 25, 2011, Johann Radon Institute for Computational and Applied Mathematics (RICAM), Linz, Austria, November 24, 2011. • N. Kleemann, Shape derivatives for conical diffraction by non-smooth interfaces, Technische Universität Berlin, Institut für Mathematik, January 6, 2011. • N. Kleemann, Shape derivatives for conical diffraction by non-smooth interfaces, Friedrich-Schiller-Universität Jena, Mathematisches Institut, February 11, 2011. • A. Rathsfeld, On Born approximation for the scattering by rough surfaces, 262. PTB Seminar, EUV Metrology, October 27 - 28, 2011, Physikalisch-Technische Bundesanstalt, Berlin, October 28, 2011. • G. Schmidt, Fast computation of high-dimensional volume potentials, Trilateral Workshop on Separation of Variables and Applications (SVA), September 8 - 10, 2010, La Colle-sur-Loup, France, September 10, 2010. • G. Schmidt, Integral methods for conical diffraction by multi-profile gratings, Annual International Conference ``Days on Diffraction 2010'', June 8 - 11, 2010, St. Petersburg, Russian Federation, June 9, 2010. • G. Schmidt, On the computation of volume potentials over high-dimensional rectangular domains, The First Workshop on Approximate Approximations and Their Applications, December 14 - 15, 2010, University of Liverpool, UK, December 15, 2010. • A. Rathsfeld, Modelling and algorithms for simulation and reconstruction in scatterometry, Workshop on Scatterometry and Ellipsometry on Structured Surfaces, March 18 - 19, 2009, Physikalisch-Technische Bundesanstalt, Department ``Imaging and Wave Optics'', Braunschweig, March 18, 2009. • A. Rathsfeld, Numerical aspects of the scatterometric measurement of periodic surface structures, Conference on Applied Inverse Problems 2009, July 20 - 24, 2009, University of Vienna, Austria, July 21, 2009. • G. Schmidt, Existence and uniqueness of solution for a system of Helmholtz equations, International Conference on Elliptic and Parabolic Equations, November 30 - December 4, 2009, WIAS, December 3, 2009. • A. Rathsfeld, Scatterometry: Inverse problems and optimization of measurements, University of Tokyo, Department of Mathematical Sciences, Japan, March 6, 2008. • G. Schmidt, Integral equations for conical diffraction by coated gratings, Annual International Conference ``Days on Diffraction'', June 3 - 6, 2008, St. Petersburg, Russian Federation, June 3, • J. Elschner, On uniqueness in inverse scattering by obstacles and diffraction gratings, Conference ``Boundary Elements --- Theory and Applications'' (Beta 2007), May 22 - 24, 2007, Leibniz Universität Hannover, May 22, 2007. • J. Elschner, On uniqueness in inverse scattering with finitely many incident waves, Workshop ``Inverse Problems in Wave Scattering'', March 5 - 9, 2007, Mathematisches Forschungsinstitut Oberwolfach, March 6, 2007. • A. Rathsfeld, Sensitivity analysis for indirect measurement in scatterometry and the reconstruction of periodic grating structures, 6th International Congress on Industrial and Applied Mathematics (ICIAM 2007), July 16 - 20, 2007, ETH Zürich, Switzerland, July 20, 2007. • J. Elschner, Inverse problems for diffraction gratings, Waves Meeting, September 21 - 23, 2006, University of Reading, UK, September 22, 2006. • J. Elschner, Variational approach to scattering by unbounded surfaces, 12th Conference on Mathematics of Finite Elements and Applications (MAFELAP 2006), June 13 - 16, 2006, Brunel University, Uxbridge, UK, June 15, 2006. • J. Elschner, Variational approach to scattering by unbounded surfaces, Autumn School ``Analysis of Maxwell's Equations'' (Research Training Group GRK 1294 ``Analysis, Simulation and Design of Nanotechnological Processes''), October 17 - 19, 2006, Universität Karlsruhe, October 18, 2006. • A. Rathsfeld, Inverses Problem, Sensitivitätsanalyse, optimierte Messstrategie, BMBF-Projekttreffen ABBILD, Physikalisch-Technische Bundesanstalt, Berlin, November 13, 2006. • A. Rathsfeld, Sensitivity analysis for scatterometry and reconstruction of periodic grating structures, Physikalisch-Technische Bundesanstalt, Berlin, October 26, 2006. • J. Elschner, Inverse Probleme für optische Gitter, Physikalisch-Technische Bundesanstalt, Berlin, April 13, 2005. • J. Elschner, Inverse problems for diffraction gratings, Inverse Scattering Workshop, University of North Carolina, Charlotte, USA, June 3, 2005. • J. Elschner, Inverse problems for diffraction gratings, University of Delaware, Department of Mathematics, Newark, USA, June 9, 2005. • A. Rathsfeld, Finite elements for the rigorous simulation of time-harmonic waves, 3rd IISB Lithography Simulation Workshop, September 16 - 18, 2005, Pommersfelden, September 16, 2005. • A. Rathsfeld, Integralgleichungsmethode für optische Gitter --- Weiterentwicklung der IESMP, Kick-off Meeting of BMBF Project ``NAOMI'', Carl Zeiss AG, Jena, May 31, 2005. • A. Rathsfeld, Local optimization of polygonal gratings for classical and conical diffraction, Conference ``Diffractive Optics 2005'', Warsaw, Poland, September 3 - 7, 2005. • A. Rathsfeld, Local optimization of polygonal gratings for classical and conical diffraction, WIAS Workshop ``New Trends in Simulation and Control of PDEs'', September 26 - 28, 2005, Berlin, September 26, 2005. • A. Rathsfeld, Optimierung von optischen Gittern mit tt DiPoG-2.1, 2nd Meeting ``Inverses Problem in der Scatterometrie'', Physikalisch-Technische Bundesanstalt, Braunschweig, October 18, 2005. • A. Rathsfeld, Optimization of diffraction gratings with tt DiPoG, sc Matheon MF 1 Workshop ``Optimization Software'', Konrad-Zuse-Zentrum für Informationstechnik Berlin, June 1, 2005. • G. Schmidt, Simulation und Optimierung periodischer diffraktiver Strukturen mit DiPoG, Physikalisch-Technische Bundesanstalt, Berlin, April 13, 2005. • J. Elschner, Direct and inverse problems for the periodic Helmholtz equation I, University of Tokyo, Department of Mathematical Sciences, Japan, February 12, 2004. • J. Elschner, Direct and inverse problems for the periodic Helmholtz equation II, University of Tokyo, Department of Mathematical Sciences, Japan, February 13, 2004. • J. Elschner, Inverse scattering for diffraction gratings, European Congress on Computational Methods in Applied Sciences and Engineering (ECCOMAS 2004), June 24 - 28, 2004, Jyväskylä, Finland, June 26, 2004. • J. Elschner, Inverse scattering of plane waves from periodic surfaces, Chemnitzer Minisymposium 2004 zu Inversen Problemen, Universität Chemnitz, September 23, 2004. • J. Elschner, Recent progress in inverse periodic diffraction problems, Workshop ``Mathematical Analyses and Numerical Methods for Applied Inverse Problems'', January 19 - 21, 2004, University of Tokyo, Japan, January 20, 2004. • A. Rathsfeld, Simulation und Optimierung diffraktiver Strukturen für die Mikrooptik, Seminar des Forschungsschwerpunktes Photonik, Technische Universität Berlin, Optisches Institut, October 22, • G. Bruckner, J. Elschner, A. Rathsfeld, G. Schmidt, Simulation, optimization and reconstruction of diffractive structures, Conference ``Diffractive Optics 2003'', Oxford, UK, September 17 - 20, • J. Elschner, Inverse problems for diffraction gratings: Uniqueness results, Meeting ``Inverse Problems in Wave Scattering and Impedance Tomography'', April 20 - 25, 2003, Mathematisches Forschungsinstitut Oberwolfach, April 22, 2003. • J. Elschner, Inverse problems for periodic diffractive structures, Meeting ``Functional Analysis and Partial Differential Equations'', June 2 - 3, 2003, Han-sur-Lesse, Belgium, June 3, 2003. • J. Elschner, On the numerical solution of inverse periodic transmission problems, University of Tokyo, Department of Mathematical Sciences, Japan, August 5, 2003. External Preprints • G. Hu, B. Zhang, The linear sampling method for the inverse electromagnetic scattering by a partially coated bi-periodic structure, Preprint no. arXiv:1003.3067, Cornell University Library, arXiv.org, 2010. • J. Elschner, G. Schmidt, M. Yamamoto, An inverse problem in periodic diffractive optics: Global uniqueness with a single wave number, Preprint no. 5, University of Tokyo, Graduate School of Mathematical Sciences, 2003.
{"url":"https://wias-berlin.de/research/ats/Diffract/?lang=1","timestamp":"2024-11-08T05:37:38Z","content_type":"text/html","content_length":"142138","record_id":"<urn:uuid:0291d0ba-a120-4cb2-b419-1dffaf9d58cc>","cc-path":"CC-MAIN-2024-46/segments/1730477028025.14/warc/CC-MAIN-20241108035242-20241108065242-00163.warc.gz"}
Cap Rate Calculator Tools to help you make informed decisions The cap rate calculator, alternatively called the capitalization rate calculator, is a tool for everyone interested in real estate. As the name suggests, it calculates the cap rate based on the value of the real estate property and the income from renting it. Put simply, cap rate definition is the rate of return on a real estate investment property. In other words, it describes what part of your initial investment will return to you every year. Get expert guidance from a Capital Markets Advisor What is the cap rate definition? Put simply, cap rate definition is the rate of return on a real estate investment property. In other words, it describes what part of your initial investment will return to you every year. For example, imagine that you bought an apartment for $100,000, and the cap rate is 10%. It means that each year, 10% of the initial investment will return to you. As you can easily calculate, your net cash flow will be equal to zero after ten years, which means that you will actually start to make money on this investment from the eleventh year onwards. What is the cap rate formula? The description above makes it easy to figure out the cap rate formula by yourself. Basically, the cap rate is the ratio of net operating income (NOI) to property value or sales price. cap rate = net operating income / property value In other words, this ratio is a straightforward way to measure the relationship between the return generated by the property and its price. Suppose you are a more advanced real estate investor. In that case, you can also include additional parameters: the vacancy rate (that is, during what percentage of the time does the property stay unoccupied) and the percentage of operating expenses (such costs as insurance, utilities, and maintenance). It is important to note that operating expenses do not include mortgage payments, depreciation, or income taxes; therefore, the net income is the cash you earn before debt service and before income You can then use the following formula for the net income: net income = (100 - operating expenses)[%] * (100 - vacancy rate)[%] * gross income How to calculate the cap rate? You can use the formulas mentioned above manually or calculate the cap rate with our cap rate calculator. To do it, follow these simple steps: 1. Begin with determining the property value – it can be, for example, its selling price. Let’s say it is equal to $200,000. 2. Find out your gross rental income. It is simply the amount of money you get from your tenants each year. Let’s say it is equal to $30,000 per year. 3. Determine the vacancy rate. Let’s say that the property stays unoccupied for 2% of the time. 4. Decide on the percentage of operating expenses. Let’s say you have to spend $500 monthly on costs – this is $6000 a year, which is equal to 20% of your gross income (you can set the value of operating expenses in the advanced mode). 5. Use the following formula above to calculate the net rental income: net income = (100 - 20)% * (100 - 2)% * $30,000 = 0.8 * 0.98 * $30,000 = $23,520 6. Lastly, divide the net income by the property value to obtain the cap rate: cap rate = $23,520 / $200,000 = 11.76% Capitalization rate application: selling a property When do we need to calculate the cap rate? Imagine the following situation: you want to sell your property. You are not sure what the price is you should sell it at. The only thing you know is that your monthly operating income is $2,800, which is equivalent to $33,600 a year. The best thing to do is to ask around for the cap rate. You are most likely to get this type of information from a commercial real estate agent. Let’s say the average cap rate in your neighborhood is 9.7%. To calculate the market value of your property, you simply have to divide the net income by the cap rate: $33,600 / 9.7% = $33,600 / 0.097 = $346,392 This result is the value of your property. Of course, consider this as a rule of thumb – there might be other reasons for increasing or lowering the selling price. Nevertheless, this is an excellent point to start from. How to evaluate your property with its capitalization rate? Probably the best way to understand how applying the capitalization rate helps in property evaluation is to look at a real-world example. Let’s say you are considering selling your house, and after some research, you see that investors are buying properties like yours at a 10 percent capitalization rate. The 10 percent cap rate means a 10 percent profit on an investment. So, for instance, if you invest 1,000 dollars, you make 100 dollars which is a 10 percent return on investment. Placing it in a simple formula: Rate of return = $100 profit / $1,000 investment = 10% This is expressed in the context of property investment: Cap rate = Annual net income / Value of the property But what is the value of the investment if you have, let’s say, $12,000 yearly net income on your property after receiving $1,000 monthly rents (or you find out that you could have such net income if you were to rent out your house)? You probably already know how to get this number, but to see this with a mathematical expression, we need to rearrange the previous formula: Value of the property = Annual net income / Cap rate Value of the property = $12,000 / 0.1 = $120,000 That means that your house is worth $120,000. How does a change in net income affect the value of a property? Now, as you have more insight into property evaluation by cap rate, let’s see what happens if there is a change in the local real estate market. As a simple example, let’s imagine that more and more tourists visit the area where your house is located because of the growing popularity of the sharing economy and Airbnb. As a result of the higher demand you decide to take this business opportunity, and you rent out your rooms with higher rent for shorter periods. Accordingly, your total net income in a year increases from $12,000 to $15,000. What happens with the value of your property in this situation? Value = $15,000 / 0.1 The estimated price of your house rises to $150,000. The above example is relatively straightforward: the higher the demand, the higher the prices. But what happens when there is a change in the capitalization rate? In the following section, you can get familiar with such a situation as well. How does a change in cap rate affect the value of a property – the importance of interest rates for cap rate One of the common external aspects that can alter the business environment is a change in interest rates. In this vein, let’s consider a situation of hiking interest rates. In such a case, other types of investments that are more directly connected to interest rates (for example, corporate bonds) may become more attractive for investors than buying properties. It follows that investors are not satisfied with a 10 percent rate of return anymore, but they require, let’s say, a 12 percent cap rate for real estate investment. Value = $12,000 / 0.12 = $100,000 As you can see, at the time of increasing interest rates, your property became less valuable. Why? Because investors must pay less for your property to receive a higher rate of return after the same net income. Let’s assume the opposite situation: what happens when interest rates go down? In that case, the cap rates drop as well; thus, your property value rises. What is the bottom line? Even if the rental prices are not affected, external circumstances in the economy can influence the property’s market value through the capitalization rate. Cap rates and housing booms Owning a house has traditionally been a part of the American dream. As the housing sector takes a considerable slice from the U.S. GDP, it is not surprising that a wide range of society tries to take advantage when house prices are going up. In such a time, politicians, bankers, investors, and ordinary home buyers mutually bolster the real estate market. The collective engagement in the housing business turned particularly forcible in the United States from the beginning of the 2000s when buying a house became an attractive way of investment. Most of us know or even experienced the disastrous effect of the 2008 financial crisis which was the culmination of the extended period of zealous rush in the real estate market. But what happened to the capitalization rate during this time? Apparently, policymakers and banks played a crucial role in the housing boom by providing easy access for mortgage loans. As a result of innovations in the financial sector and low interest rates, mortgage loans flooded the housing market. The increasing demand with easy credits generated house price inflation, and capitalization rates fell to unprecedented levels. Source: Baum, Andrew and Hartzell, David. (2012) Global Property Investment. Strategies, Structures, Decisions. Wiley & Sons, Inc. In 2002, cap rates were around the range of 8.5-9 percent, which is close to the long-run average. However, after several years of steady fall, cap rates reached a historically low 6.5 percent level. The steep drop in cap rates verified the presence of a speculative bubble on the housing market. Property evaluation techniques There are three conventional ways of property valuation, and they all rely on the basis of comparison. 1. Sales comparison techniques In this case, the estimation is based on the price of similar properties on the market. 2. Replacement cost methods In this approach, the guideline is the estimation of the expenses for constructing a similar property taking into account the depreciation rates and land values. 3. Income techniques Evaluations of properties by their income streams or yields are related to income techniques. The core of this technique is the estimation of the capacity to generate economic benefit during the property’s lifetime. Property evaluation ratios There are multiple financial ratios that can support the decision-making when you are about to buy or sell a property. Among them, the capitalization rate is probably the most popular ratio; however, there are others that also can give you practical guidance. Besides the capitalization rate, the four other essential ratios are the following: • The cash return on investment (Cash ROI) The cash return on investment, often called the cash-on-cash return, is the ratio of the remaining cash after debt repayment to the invested capital. The cash-on-cash ratio and the capitalization rate have an important distinction: the cash ROI is computed after debt service, while the cap rate doesn’t consider the debt service. Cash-on-cash ratio = remaining net income after debt service / invested cash • The total return on investment (Total ROI) The total return on investment is comparable to the cash-on-cash ratio, with one crucial difference: it represents the fraction of return that is not cash, namely the principal reduction. Putting it differently, it considers the principal portion of the loan payment in the equation. Therefore, the total ROI is the ratio of the remaining cash after debt service plus principal payments to the invested capital: The total return on investment = (remaining cash after debt service + principal reduction) / invested cash • The debt service coverage ratio (DSCR) The debt service coverage ratio, also known as the debt service ratio, evaluates the relation of the amount of cash available to service the debt payments, which is the net operating income, to the required debt payment. The debt service coverage ratio = net operating income / debt payment • The gross rent multiplier (GRM) The gross rent multiplier, or GRM, signifies the relationship between the total purchase price of a property and its gross scheduled income. Therefore, it is the ratio of price to income. Gross Rent Multiplier = purchase price / gross scheduled income As the above ratios consider further financial dimensions of the property investment, they are helpful substitutes or complements for the cap rate. The application of capitalization rate during property evaluation is undoubtedly a convenient device; however, employing cap rates alone or inappropriately using them can lead to a severe flaw in your decision-making. As mentioned before, the cap rate doesn’t account for debt payment in contrast to other debt-related ratios. Because mortgage loans often finance house purchases, a cash-on-cash investment ratio may give you a better guideline. Obtain a custom quote for your specific financing needs
{"url":"https://insigniafs.com/cap-rate-calculator/","timestamp":"2024-11-11T15:09:27Z","content_type":"text/html","content_length":"138242","record_id":"<urn:uuid:018f6807-167c-4545-a80f-331cb52724ef>","cc-path":"CC-MAIN-2024-46/segments/1730477028230.68/warc/CC-MAIN-20241111123424-20241111153424-00620.warc.gz"}
Project Zero Posted by Ivan Fratric, Google Project Zero Some time ago, I noticed a tweet about an externally reported vulnerability in Skia graphics library (used by Chrome, Firefox and Android, among others). The vulnerability caught my attention for several reasons: Firstly, I looked at Skia before within the context of finding precision issues, and any bugs in the code I already looked at instantly evoke the “What did I miss?” question in my head. Secondly, the bug was described as a stack-based buffer overflow, and you don’t see many bugs of this type anymore, especially in web browsers. And finally, while the bug itself was found by fuzzing and didn’t contain much in the sense of root cause analysis, a part of the fix involved changing the floating point precision from single to double which is something I argued against in the previous blog post on precision issues in graphics libraries. So I wondered what the root cause was and if the patch really addressed it, or if other variants could be found. As it turned out, there were indeed other variants, resulting in stack and heap out-of-bounds writes in the Chrome renderer. Geometry for exploit writers To understand what the issue was, let’s quickly cover some geometry basics we’ll need later. This is all pretty basic stuff, so if you already know some geometry, feel free to skip this section. A convex polygon is a polygon with a following property: you can take any two points inside the polygon, and if you connect them, the resulting line will be entirely contained within the polygon. A concave polygon is a polygon that is not convex. This is illustrated in the following images: Image 1: An example of a convex polygon Image 2: An example of a concave polygon A polygon is monotone with respect to the Y axis (also called y-monotone) if every horizontal line intersects it at most twice. Another way to describe a y-monotone polygon is: if we traverse the points of the polygon from its topmost to its bottom-most point (or the other way around), the y coordinates of the points we encounter are always going to decrease (or always increase) but never alternate directions. This is illustrated by the following examples: Image 3: An example of a y-monotone polygon Image 4: An example of a non-y-monotone polygon A polygon can also be x-monotone if every vertical line intersects it at most twice. A convex polygon is both x- and y-monotone, but the inverse is not true: A monotone polygon can be concave, as illustrated in Image 3. All of the concepts above can easily be extended to other curves, not just polygons (which are made entirely from line segments). A polygon can be transformed by transforming all of its points. A so-called affine transformation is a combination of scaling, skew and translation (note that affine transformation also includes rotation because rotation can be expressed as a combination of scale and skew). Affine transformation has a property that, when it is used to transform a convex shape, the resulting shape must also be convex. For the readers with a basic knowledge of linear algebra: a transformation can be represented in the form of a matrix, and the transformed coordinates can be computed by multiplying the matrix with a vector representing the original coordinates. Transformations can be combined by multiplying matrices. For example, if you multiply a rotation matrix and a translation matrix, you’ll get a transformation matrix that includes both rotation and translation. Depending on the multiplication order, either rotation or translation is going to be applied first. The bug Back to the bug: after analyzing it, I found out that it was triggered by a malformed RRect (a rectangle with curved corners where the user can specify a radius for each corner). In this case, tiny values were used as RRect parameters which caused precision issues when the RRect was converted into a path object (a more general shape representation in Skia which can consist of both line and curve segments). The result of this was, after the RRect was converted to a path and transformed, the resulting shape didn’t look like a RRect at all - the resulting shape was concave. At the same time Skia assumes that every RRect must be convex and so, when the RRect is converted to a path, it sets the convexity attribute on the path to kConvex_Convexity (for RRects this happens in a helper class SkAutoPathBoundsUpdate). Why is this a problem? Because Skia has different drawing algorithms, some of which only work for convex paths. And, unfortunately, using algorithms for drawing convex paths when the path is concave can result in memory corruption. This is exactly what happened here. Skia developers fixed the bug by addressing RRect-specific computations: they increased the precision of some calculations performed when converting RRects to paths and also made sure that any RRect corner with a tiny radius would be treated as if the radius is 0. Possibly (I haven’t checked), this makes sure that converting RRect to a path won’t result in a concave shape. However, another detail caught my attention: Initially, when the RRect was converted into a path, it might have been concave, but the concavities were so tiny that they wouldn’t cause any issues when the path was rendered. At some point the path was transformed which caused the concavities to become more pronounced (the path was very clearly concave at this point). And yet, the path was still treated as convex. How could that be? The answer: The transformation used was an affine transform, and Skia respects the mathematical property that transforming a shape with an affine transform can not change its convexity, and so, when using an affine transform to transform a path, it copies the convexity attribute to the resulting path object. This means: if we can convince Skia that a path is convex, when in reality it is not, and if we apply any affine transform to the path, the resulting path will also be treated as convex. The affine transform can be crafted so that it enlarges, rotates and positions concavities so that, once the convex drawing algorithm is used on the path, memory corruption issues are triggered. Additionally (untested) it might be possible that, due to precision errors, computing a transformation itself might introduce tiny concavities when there were none previously. These concavities might then be enlarged in subsequent path transformations. Unfortunately for computational geometry coders everywhere, accurately determining whether a path is convex or not in floating point precision (regardless if single or double floating point precision is used) is very difficult / almost impossible to do. So, how does Skia do it? Convexity computations in Skia happen in the Convexicator class, where Skia uses several criteria to determine if a path is convex: • It traverses a path and computes changes of direction. For example, if we follow a path and always turn left (or always turn right), the path must be convex. • It checks if a path is both x- and y-monotone When analyzing this Convexicator class, I noticed two cases where a concave paths might pass as convex: 1. As can be seen here, any pair of points for which the squared distance does not fit in a 32-bit float (i.e. distance between the points smaller than ~3.74e-23) will be completely ignored. This, of course, includes sequences of points which form concavities. 2. Due to tolerances when computing direction changes (e.g here and here) even concavities significantly larger than 3.74e-23 can easily passing the convexity check (I experimented with values around 1e-10). However, such concavities must also pass the x- and y-monotonicity check. Note that, in both cases, a path needs to have some larger edges (for which direction can be properly computed) in order to be declared convex, so just having a tiny path is not sufficient. Fortunately, a line is considered convex by Skia, so it is sufficient to have a tiny concave shape and a single point at a sufficient distance away from it for a path to be declared convex. Alternately, by combining both issues above, one can have tiny concavities along the line, which is a technique I used to create paths that are both small and clearly concave when transformed (Note: the size of the path is often a factor when determining which algorithms can handle which paths). To make things clearer, let’s see an example of bypassing the convexity check with a polygon that is both x- and y- monotone. Consider the polygon in Image 5 (a) and imagine that the part inside the red circle is much smaller than depicted. Note that this polygon is concave, but it is also both x-monotone and y-monotone. Thus, if the concavity depicted in the red circle is sufficiently small, the polygon is going to be declared convex. Now, let’s see what we can do with it by applying an affine transform - firstly, we can rotate it and make it non-y-monotone as depicted in Image 5 (b). Having a polygon that is not y-monotone will be very important for triggering memory corruption issues later. Secondly, we can scale (enlarge) and translate the concavity to fill the whole drawing area, and when the concavity is intersected with the drawing area we’ll end up with something like depicted in Image 5 (c), in which the polygon is clearly concave and the concavity is no longer small. Image 5: Bypassing the convexity check with a monotone polygon The walk_convex_edges algorithm Now that we can bypass the convexity check in various ways, let’s see how it can lead to problems. To understand this, let’s first examine how Skia's algorithm for drawing (filling) convex paths works (code here). Let’s consider an example in Image 6 (a). The first thing Skia does is, it extracts polygon (path) lines (edges) and sorts them according to the coordinates of the topmost point. The sorting order is top-to-bottom, and if two points have the same y coordinate, then the one with a smaller x coordinate goes first. This has been done for the polygon in Image 6 (a) and the numbers next to the edges depict their order. The bottommost edge is ignored because it is fully horizontal and thus not needed (you’ll see why in a moment). Next, the edges are traversed and the area between them drawn. First, the first two edges (edges 1 and 2) are taken and the area between them is filled from top to bottom - this is the red area in Image 6 (b). After this, edge 1 is “done” and it is replaced by the next edge - edge 3. Now, area between edge 2 and edge 3 is filled (orange area). Next, edge 2 is “done” and is replaced by the next in line: edge 4. Finally, the area between edges 3 and 4 is rendered. Since there are no more edges, the algorithm stops. Image 6: Skia convex path filling algorithm Note that, in the implementation, the code for rendering areas where both edges are vertical (here) is different than the code for rendering areas where at least one edge is at an angle (here). In the first case, the whole area is rendered in a single call to blitter->blitRect() while in the second case, the area is rendered line-by-line and for each line blitter->blitH() is called. Of special interest here is the local_top variable, essentially keeping track of the next y coordinate to fill. In the case of drawing non-vertical edges, this is simply incremented for every line drawn. In case of vertical lines (drawing a rectangle), after the rectangle is drawn, local_top is set based on the coordinates of the current edge pair. This difference in behavior is going to be useful One interesting observation about this algorithm is that it would not only work correctly for convex paths - it would work correctly for all paths that are y-monotone. Using it for y-monotone paths would also have another benefit: Checking if a path is y-monotone could be performed faster and more accurately than checking if a path is convex. Variant 1 Now, let’s see how drawing concave paths using this algorithm can lead to problems. As the first example, consider the polygon in Image 7 (a) with the edge ordering marked. Image 7: An example of a concave path that causes problem in Skia if rendered as convex Image 7 (b) shows how the shape is rendered. First, a large red area between edges 1 and 2 is rendered. At this point, both edges 1 and 2 are done, and the orange rectangular area between areas 3 and 4 is rendered next. The purpose of this rectangular area is simply to reset the local_top variable to its correct value (here), otherwise local_top would just continue increasing for every line drawn. Next, the green area between edges 3 and 5 is drawn - and this causes problems. Why? Because Skia expects to always draw pixels in a top-to-bottom, left-to right order, e.g. point (x, y) = (1, 1) is always going to be drawn before (1, 2) and (1, 1) is also going to be always drawn before (2, 1). However, in the example above, the area between edges 1 and 2 will have (partially) the same y values as the area between edges 3 and 5. The second area is going to be drawn, well, second, and yet it contains a subset of same y coordinates and lower x coordinates than the first region. Now let’s see how this leads to memory corruption. In the original bug, a concave (but presumed convex) path was used as a clipping region (every subsequent draw call draws only inside the clipping region). When setting a path as a clipping region, it also gets “drawn”, but instead of drawing pixels on the screen, they just get saved so they could be intersected with what gets drawn afterwards. The pixels are saved in SkRgnBuilder::blitH and actually, individual pixels aren't saved but instead the entire range of pixels (from x to x + width at height y) gets stored at once to save space. These ranges - you guessed it - also depend on the correct drawing order as can be seen here (among other places). Now let’s see what happens when a second path is drawn inside a clipping region with incorrect ordering. If antialiasing is turned on when drawing the second path, SkRgnClipBlitter::blitAntiH gets called for every range drawn. This function needs to intersect the clip region ranges with the range being drawn and only output the pixels that are present in both. For that purpose, it gets the clipping ranges that intersect the line being drawn one by one and processes them. SkRegion::Spanerator::next is used to return the next clipping range. Let’s assume the clipping region for the y coordinate currently drawn has the ranges [start x, end x] = [10, 20] and [0, 2] and the line being drawn is [15,16]. Let’s also consider the following snippet of code from SkRegion::Spanerator::next: if (runs[0] >= fRight) { fDone = true; return false; SkASSERT(runs[1] > fLeft); if (left) { *left = SkMax32(fLeft, runs[0]); if (right) { *right = SkMin32(fRight, runs[1]); fRuns = runs + 2; return true; where left and right are the output pointers, fLeft and fRight are going to be left and right x value of the line being drawn (15 and 16 respectively), while runs is a pointer to clipping region ranges that gets incremented for every iteration. For the first clipping line [10, 20] this is going to work correctly, but let’s see what happens for the range [0, 2]. Firstly, the part if (runs[0] >= fRight) { fDone = true; return false; is supposed to stop the algorithm, but due to incorrect ordering, it does not work (16 >= 0 is false). Next, left is computed as Max(15, 0) = 15 and right as Min(16, 2) = 2. Note how left is larger than right. This is going to result in calling SkAlphaRuns::Break with a negative count argument on the line, SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left); which then leads to out-of-bounds write on the following lines in SkAlphaRuns::Break: x = count; alpha[x] = alpha[0]; Why did this result in out-of-bounds write on the stack? Because, in the case of drawing only two pixels, the range arrays passed to SkRgnClipBlitter::blitAntiH and subsequently SkAlphaRuns::Break are allocated on stack in SkBlitter::blitAntiH2 here. Triggering the issue in a browser This is great - we have a stack out-of-bounds write in Skia, but can we trigger this in Chrome? In general, in order to trigger the bug, the following conditions must be met: 1. We control a path (SkPath) object 2. Something must be done to the path object that computes its convexity 3. The same path must be transformed and filled / set as a clip region My initial idea was to use a CanvasRenderingContext2D API and render a path twice: once without any transform just to establish its convexity and a second time with a transformation applied to the CanvasRenderingContext2D object. Unfortunately, this approach won’t work - when drawing a path, Skia is going to copy it before applying a transformation, even if there is effectively no transformation set (the transformation matrix is an identity matrix). So the convexity property is going to be set on a copy of the path and not the one we get to keep the reference to. Additionally, Chrome itself makes a copy of the path object when calling any canvas functions that cause a path to be drawn, and all the other functions we can call with a path object as an argument do not check its convexity. However, I noticed Chrome canvas still draws my convex/concave paths incorrectly - even if I just draw them once. So what is going on? As it turns out, when drawing a path using Chrome canvas, the path won’t be drawn immediately. Instead, Chrome just records the draw path operation using RecordPaintCanvas and all such draw operations will be executed together, at a later time. When a DrawPathOp object (representing a path drawing operation) is created, among other things, it is going to check if the path is “slow”, and one of the criteria for this is path convexity: int DrawPathOp::CountSlowPaths() const { if (!flags.isAntiAlias() || path.isConvex()) return 0; All of this happens before the path is transformed, so we seemingly have a perfect scenario: We control a path, its convexity is checked, and the same path object later gets transformed and rendered. The second problem with canvas is that, in the previously described approach to converting the issue to memory corruption, we relied on SkRgnBuilder, which is only used when a clip region has antialiasing turned off, while everything in Chrome canvas is going to be drawn with antialiasing on. Chrome also implements the OffscreenCanvas API which sets clip antialiasing to off (I’m not sure if this is deliberate or a bug), but OffscreenCanvas does not use RecordPaintCanvas and instead draws everything immediately. So the best way forward seemed to be to find some other variants of turning convexity issues into memory corruption, ones that would work with antialiasing on for all operations. Variant 2 As it happens, Skia implements three different algorithms for path drawing with antialiasing on and one of these (SkScan::SAAFillPath, using supersampled antialiasing) uses essentially the same filling algorithm we analyzed before. Unfortunately, this does not mean we can get to the same buffer overflow as before - as mentioned before SkRgnBuilder / SkRgnClipBlitter are not used with antialiasing on. However, we have other options. If we simply fill the path (no clip region needed this time) with the correct algorithm, SuperBlitter::blitH is going to be called without respecting the top-to-bottom, left-to-right drawing order. SuperBlitter::blitH calls SkAlphaRuns::add and as the last argument, it passes the rightmost x coordinate we have drawn so far. This is subtracted from the currently drawn x coordinate on the line: x -= offsetX; And if x is smaller than something we drew already (for the same y coordinate) it becomes negative. This is of course exactly what happens when drawing pixels out of Skia expected order. The result of this is calling SkAlphaRuns::Break with a negative “x” argument. This skips the entire first part of the function (the “while (x > 0)” loop), and continues to the second part: runs = next_runs; alpha = next_alpha; x = count; for (;;) { int n = runs[0]; SkASSERT(n > 0); if (x < n) { alpha[x] = alpha[0]; runs[0] = SkToS16(x); runs[x] = SkToS16(n - x); x -= n; if (x <= 0) { runs += n; alpha += n; Here, x gets overwritten with count, but the problem is that runs[0] is not going to be initialized (the first part of the function is supposed to initialize it), so in int n = runs[0]; an uninitialized variable gets read into n and is used as an offset into arrays, which can result in both out-of-bounds read and out-of-bounds write when the following lines are executed: runs += n; alpha += n; alpha[x] = alpha[0]; runs[0] = SkToS16(x); runs[x] = SkToS16(n - x); The shape needed to trigger this is depicted in image 8 (a). Image 8: Shape used to trigger variant 2 in Chrome This shape is similar to the one previously depicted, but there are some differences, namely: • We must render two ranges for the same y coordinate immediately one after another, where the second range is going to be to the left of the first range. This is accomplished by making the rectangular area between edges 3 and 4 (orange in Image 8 (b)) less than a pixel wide (so it does not in fact output anything) and making the green area between edges 5 and 6 (green in the image) only a single pixel high. • The second range for the same y must not start at x = 0. This is accomplished by edge 5 ending a bit away from the left side of the image bounds. This variant can be triggered in Chrome by simply drawing a path - the poc can be seen here. Variant 3 Uninitialized variable bug in a browser is nice, but not as nice as a stack out-of-bounds write, so I looked for more variants. For the next and final one, the path we need is a bit more complicated and can be seen in Image 9 (a) (note that the path is self-intersecting). Image 9: A shape used to trigger a stack buffer overflow in Chrome Let’s see what happens in this one (assume the same drawing algorithm is used as before): First, edges 1, 2, 3 and 4 are handled. This part is drawn incorrectly (only red and orange areas in Image 9 (b) are filled), but the details aren’t relevant for triggering the bug. For now, just note that edges 2 and 4 terminate at the same height, so when they are done, edges 2 and 4 are both replaced with edges 5 and 6. The purpose of edges 5 and 6 is once again to reset the local_top variable - it will be set to the height shown as the red dotted line in the image. Now, edge 5 and 6 will both get replaced with edges 7 and 8 - and here is the issue: Edges 7 and 8 are not going to be drawn for y coordinates between the green and blue line, as they are supposed to. Instead, they are going to be rendered all the way from the red line to the blue line. Note the very low steepness of edges 7 and 8 - for every line, the x coordinates to draw to are going to be significantly increased and, given that they are going to be drawn in a larger number of iterations than intended, the x coordinate will eventually spill past the image bounds. This causes a stack out-of-bounds write if a path is drawn using SkScan::SAAFillPath algorithm with MaskSuperBlitter. MaskSuperBlitter can only handle very small paths (up to 32x32 pixels) and contains a fixed-size buffer that is going to be filled with 8-bit opacity for each pixel of the path region. Since MaskSuperBlitter is a local variable in SkScan::SAAFillPath, the (fixed-size) buffer is going to be allocated on the stack. When the path above is drawn, there aren’t any bounds checks on the opacity buffer (there are only debug asserts here and here), which leads to an out-of bounds write on the stack. Specifically (due to how opacity buffer works) we can increment values on the stack past the end of the buffer by a small amount. This variant is again triggerable in Chrome by simply drawing a path to the Canvas and gives us a pretty nice primitive for exploitation - note that this is not a linear overflow and offsets involved can be controlled by the slope of edges 7 and 8. The PoC can be seen here - most of it is just setting up the path coordinates so that the path is initially declared convex and at the same time small enough so that MaskSuperBlitter can render it. How to make the shape needed to trigger the bug appear convex to Skia but also fit in 32x32 pixels? Note that the shape is already x-monotone. Now assume we squash it in the y direction until it becomes (almost) a line lying on the x axis. It is still not y-monotone because there are tiny shifts in y direction along the line - but if we skew (or rotate) it just a tiny amount, so that it is no longer parallel to the x axis, it also becomes y-monotone. The only parts we can’t make monotone are vertical edges (edges 5 and 6), but if you squashed the shape sufficiently they become so short that their square length does not fit in a float and are ignored by the Skia convexity test. This is illustrated in Image 10. In reality these steps need to be followed in reverse, as we start with a shape that needs to pass the Skia convexity test and then transform it to the shape depicted in Image 9. Image 10: Making the shape from Image 9 appear convex, (a) original shape, (b) shape after y-scale, (c) shape after y-scale rotation On fixing the issue Initially, Skia developers attempted to fix the issue by not propagating convexity information after the transformation, but only in some cases. Specifically, the convexity was still propagated if the transformation consisted only of scale and translation. Such a fix is insufficient because very small concavities (where square distance between points is too small to fit in a 32-bit float) could still be enlarged using only scale transformation and could form shapes that would trigger memory corruption issues. After talking to the Skia developers, a stronger patch was created, modifying the convex drawing algorithm in a way that passing concave shapes to it won’t result in memory corruption, but rather in returning from the draw operation early. This patch shipped, along with other improvements, in Chrome 72. It isn’t uncommon that an initial fix for a vulnerability is insufficient. But the saving grace for Skia, Chrome and most open source projects is that the bug reporter can see the fix immediately when it’s created and point out the potential drawbacks. Unfortunately, this isn’t the case for many closed-source projects or even open-sourced projects where the bug fixing process is opaque to the reporter, which caused mishaps in the past. However, regardless of the vendor, we at Project Zero are happy to receive information on the fixes early and comment on them before they are released to the public. There are several things worth highlighting about this bug. Firstly, computational geometry is hard. Seriously. I have some experience with it and, while I can’t say I’m an expert I know that much at least. Handling all the special cases correctly is a pain, even without considering security issues. And doing it using floating point arithmetic might as well be impossible. If I was writing a graphics library, I would convert floats to fixed-point precision as soon as possible and wouldn’t trust anything computed based on floating-point arithmetic at all. Secondly, the issue highlights the importance of doing variant analysis - I discovered it based on a public bug report and other people could have done the same. Thirdly, it highlights the importance of defense-in-depth. The latest patch makes sure that drawing a concave path with convex path algorithms won’t result in memory corruption, which also addresses unknown variants of convexity issues. If this was implemented immediately after the initial report, Project Zero would now have one blog post less :-)
{"url":"https://googleprojectzero.blogspot.com/2019/02/?m=0","timestamp":"2024-11-09T00:49:30Z","content_type":"application/xhtml+xml","content_length":"235458","record_id":"<urn:uuid:bb0aa76b-2127-4119-af40-08b88b96a46f>","cc-path":"CC-MAIN-2024-46/segments/1730477028106.80/warc/CC-MAIN-20241108231327-20241109021327-00211.warc.gz"}
Cost Per Solar Panel Calculator - Calculator Doc Cost Per Solar Panel Calculator Investing in solar energy is a significant decision that can lead to substantial savings in the long run. Understanding the cost breakdown of your solar system is crucial for evaluating the return on investment. One useful metric is the cost per solar panel, which helps you understand how much each panel contributes to the total system cost. Our Cost Per Solar Panel Calculator makes it easy to calculate this value by factoring in the total cost of the system, any value from excess energy production, and the number of panels in the setup. The formula to calculate the cost per solar panel is: CPS = (CS – VE) / SP • CPS = Cost per Solar Panel (dollars) • CS = Total Cost of Solar System (dollars) • VE = Value of Excess Energy Produced (dollars) • SP = Number of Solar Panels This formula provides a straightforward way to determine how much each panel in your solar system costs after accounting for excess energy production. How to Use the Cost Per Solar Panel Calculator Using the Cost Per Solar Panel Calculator is easy: 1. Enter the Total Cost of Solar System (CS): This includes the entire cost of installing the solar panels, including equipment and labor. 2. Enter the Value of Excess Energy Produced (VE): If your system produces more energy than you use, enter the monetary value of the excess energy (e.g., from selling it back to the grid). 3. Enter the Number of Solar Panels (SP): This is the total number of solar panels in your system. 4. Click on the “Calculate” Button: The calculator will estimate the cost per solar panel in dollars. Let’s say your solar system costs $15,000, and you receive $2,000 from selling excess energy back to the grid. If you have 20 solar panels, the cost per panel is calculated as: CPS = ($15,000 – $2,000) / 20 CPS = $13,000 / 20 CPS = $650 In this example, each solar panel costs approximately $650 after accounting for the value of excess energy. 1. What is the cost per solar panel? The cost per solar panel represents how much each individual panel costs in a solar system after accounting for the total system cost and any value from excess 2. Why is calculating cost per solar panel important? Understanding the cost per panel helps you evaluate the return on investment and compare different solar system setups. 3. What does the total cost of the solar system include? The total cost typically includes the price of the panels, installation, inverters, wiring, and other necessary equipment. 4. How do I determine the value of excess energy produced? The value of excess energy is usually the amount of money you receive from selling surplus electricity back to the grid or energy provider. 5. What if my system doesn’t produce excess energy? If your system doesn’t produce excess energy, enter zero for the value of excess energy in the calculator. 6. Does the number of panels affect the overall system efficiency? The number of panels affects the system’s energy output, but efficiency depends on factors like panel quality, orientation, and 7. How does the cost per panel vary with system size? Larger systems with more panels may have a lower cost per panel due to economies of scale, but this depends on specific installation factors. 8. Can this calculator be used for different types of panels? Yes, the calculator can be used for any type of solar panel, whether they are monocrystalline, polycrystalline, or thin-film panels. 9. What other factors influence the cost of solar panels? Factors like location, local incentives, installation complexity, and panel quality can influence the cost of solar panels. 10. Is the cost per panel the same as the total system cost divided by the number of panels? The cost per panel formula also accounts for the value of excess energy produced, which can reduce the effective cost of each panel. 11. Can the cost per solar panel change over time? Yes, the cost per panel can change based on maintenance costs, upgrades, and changes in energy production or energy prices. 12. How do I reduce the cost per solar panel? You can reduce the cost per panel by optimizing the system for maximum efficiency, taking advantage of government incentives, and selling excess energy back to the grid. 13. Does the calculator account for government rebates or incentives? This calculator does not account for rebates or incentives directly, but you can factor these into the total system cost if 14. What if my solar panels degrade over time? Solar panel degradation can reduce the efficiency of the panels over time, potentially increasing the cost per panel in terms of energy produced. 15. Is cost per solar panel the only metric to consider? While cost per panel is important, other metrics like overall energy savings, system efficiency, and payback period are also essential for evaluating a solar system. 16. How do local energy rates affect the value of excess energy? Higher local energy rates can increase the value of excess energy, reducing the effective cost per panel. 17. Can I calculate the cost per panel for off-grid systems? Yes, for off-grid systems, you can calculate the cost per panel by factoring in the total cost of the system without excess energy 18. What if my system includes batteries? If your system includes batteries for energy storage, you can include the cost of the batteries in the total system cost for a more comprehensive 19. How does solar panel efficiency affect cost per panel? More efficient panels may have a higher upfront cost, but they produce more energy, which can reduce the effective cost per panel over time. 20. What is the payback period for solar panels? The payback period is the time it takes for the savings from reduced energy bills to cover the initial cost of the solar system. The cost per panel can influence this period. Calculating the cost per solar panel helps you better understand the financial aspects of your solar energy investment. By using our Cost Per Solar Panel Calculator, you can easily determine how much each panel in your system costs after accounting for excess energy production. This insight is valuable for evaluating the overall efficiency and return on investment of your solar energy system.
{"url":"https://calculatordoc.com/cost-per-solar-panel-calculator/","timestamp":"2024-11-13T21:25:57Z","content_type":"text/html","content_length":"88484","record_id":"<urn:uuid:dbce557d-4584-4e4f-ba68-1b726d8afa50>","cc-path":"CC-MAIN-2024-46/segments/1730477028402.57/warc/CC-MAIN-20241113203454-20241113233454-00014.warc.gz"}
Finding a power series expansion for a definite integral • Thread starter Szichedelic • Start date In summary, the conversation discusses finding a power series expansion for the function f(x) = ^{1}_{0}\int\frac{1 - e^{-sx}}{s} ds, using the power series expansion formula. The individual has attempted various methods but encountered difficulties. Eventually, they realize that substituting the Taylor series for e^{-sx} in the integrand and integrating will solve the problem. Homework Statement Find a power series expansion about x = 0 for the function f(x) = [itex]^{1}_{0}\int\frac{1 - e^{-sx}}{s}[/itex] ds Homework Equations The power series expansion for a function comes of the form f(x) = [itex]^{\infty}_{0}\sum a_{k}x^{k}[/itex] The Attempt at a Solution I've tried several things to start off with but quickly end up hitting a dead end road. First, I tried just simply taking the integral, but quickly found it isn't defined at 0 (hence why they are asking me to find a power series expansion for it). Then, I tried finding a power series expansion for the innerpart of the integral and ran into the same problem. Science Advisor Homework Helper Gold Member Szichedelic said: Homework Statement Find a power series expansion about x = 0 for the function f(x) = [itex]^{1}_{0}\int\frac{1 - e^{-sx}}{s}[/itex] ds Homework Equations The power series expansion for a function comes of the form f(x) = [itex]^{\infty}_{0}\sum a_{k}x^{k}[/itex] The Attempt at a Solution I've tried several things to start off with but quickly end up hitting a dead end road. First, I tried just simply taking the integral, but quickly found it isn't defined at 0 (hence why they are asking me to find a power series expansion for it). Then, I tried finding a power series expansion for the innerpart of the integral and ran into the same problem. Did you try substituting the Taylor series (as a function of x) for ##e^{-sx}## in the integrand, simplifying and integrating? Yeah, I figured that out shortly after I posted this. Can't believe I overlooked that! Thanks! FAQ: Finding a power series expansion for a definite integral What is a power series expansion for a definite integral? A power series expansion for a definite integral is a representation of a function as an infinite sum of terms, each of which is a constant multiplied by a variable raised to a non-negative integer power, that is used to approximate a definite integral. This expansion allows us to evaluate the integral with a high degree of accuracy by using a finite number of terms. Why is finding a power series expansion for a definite integral useful? Finding a power series expansion for a definite integral can be useful in situations where the integral cannot be evaluated using traditional methods, such as integration by substitution or by parts. It allows us to approximate the integral and find an accurate solution without having to use complex techniques. How do you find a power series expansion for a definite integral? To find a power series expansion for a definite integral, we first need to identify the function that we want to integrate. Then, we use the formula for a power series expansion to write the function as an infinite sum of terms. Finally, we integrate each term of the series to obtain the power series expansion for the definite integral. What are the limitations of using a power series expansion for a definite integral? One limitation of using a power series expansion for a definite integral is that it only works for certain types of functions. In some cases, the series may not converge or may converge very slowly, making it difficult to obtain an accurate solution. Additionally, the accuracy of the approximation depends on the number of terms used in the series, so it may not always provide a precise answer. Can a power series expansion for a definite integral be used to find exact solutions? No, a power series expansion for a definite integral can only provide an approximation of the solution. It cannot be used to obtain an exact answer, but it can give us a very accurate estimation of the value of the integral.
{"url":"https://www.physicsforums.com/threads/finding-a-power-series-expansion-for-a-definite-integral.586304/","timestamp":"2024-11-13T05:30:20Z","content_type":"text/html","content_length":"83001","record_id":"<urn:uuid:b58cf5b2-a9ae-4c1a-bdd1-518633f42a25>","cc-path":"CC-MAIN-2024-46/segments/1730477028326.66/warc/CC-MAIN-20241113040054-20241113070054-00726.warc.gz"}
Operator Differential Equations in Hilbert Space NOTE: Text or symbols not renderable in plain ASCII are indicated by [...]. Abstract is included in .pdf document. In this paper the theory of dissipative linear operators in Hilbert space developed by R. S. Phillips has been applied in the study of the Cauchy problem [...](t) + A(t)x(t) = f(t), x(o) = x[subscript o] where A(t), t [epsilon] [o,[tau]], is a family of unbounded linear operators with a common dense domain D in a Hilbert space H, f [epsilon] [...], the Hilbert space of measurable functions on [o, [tau]] with values in H which have square integrable norm, and x[subscript o] [epsilon] H. It is assumed that for each t [epsilon] [o,[tau]] A(t) is maximal dissipative, satisfying for each x [epsilon] D, Re (A(t)x,x) [greater than or equal to] [alpha] [...], [alpha] > o, and A(t)x is strongly continuous and has a bounded measurable strong derivative on J. Let A[subscript o] be any maximal dissipative linear operator with domain D satisfying Re (A[subscript o]x,x) [greater than or equal to] [alpha] [...] for all x [epsilon] D. Then B(t) = A(t)A[subscript o][superscript -1] is a one-to-one continuous linear transformation of H onto itself. It is assumed that B [superscript -1](t) is bounded on [o, [tau]]. Under these conditions it is shown that, first, there exists a weak solution to the Cauchy problem, and, second, that the weak solution is a unique strong solution which is the limit of a sequence of classical solutions. The theory is applied to a time-dependent hyperbolic system of partial differential equations. Item Type: Thesis (Dissertation (Ph.D.)) Subject Keywords: (Mathematics) Degree Grantor: California Institute of Technology Division: Physics, Mathematics and Astronomy Major Option: Mathematics Thesis Availability: Public (worldwide access) Research Advisor(s): • De Prima, Charles R. (advisor) • Erdélyi, Arthur (advisor) Thesis Committee: • Unknown, Unknown Defense Date: 19 May 1964 Record Number: CaltechETD:etd-10182002-082821 Persistent URL: https://resolver.caltech.edu/CaltechETD:etd-10182002-082821 DOI: 10.7907/1N8N-Y957 Default Usage Policy: No commercial reproduction, distribution, display or performance rights in this work are provided. ID Code: 4148 Collection: CaltechTHESIS Deposited By: Imported from ETD-db Deposited On: 21 Oct 2002 Last Modified: 19 Jan 2024 22:56 Thesis Files PDF (Lopes,jr_la_1964.pdf) - Final Version See Usage Policy. Repository Staff Only: item control page
{"url":"https://thesis.library.caltech.edu/4148/","timestamp":"2024-11-14T12:20:41Z","content_type":"application/xhtml+xml","content_length":"26107","record_id":"<urn:uuid:6da8ec65-44c5-444d-8c3a-bff559b57cfb>","cc-path":"CC-MAIN-2024-46/segments/1730477028558.0/warc/CC-MAIN-20241114094851-20241114124851-00214.warc.gz"}
Searching an Element in a Sorted and Rotated Array: Single-pass Binary Search by Comparing Mid and Target Elements For software engineers, preparing for a tech interview means a lot of practice! Not only must you practice several problems, but you also need to ensure that you cover a wide variety of topics. In this article, we’re going to tackle a problem on search, which will, in turn, help you crack a number of complex problems — searching an element in a sorted and rotated array. We solve it using “single-pass binary search by comparing mid and target elements.” This is approach 4 of 4. To check out the other approaches, follow the links: Here, we cover: • Problem statement • Approach explanation • Algorithm for the approach • Code for the approach • Time complexity of the approach • Space complexity of the approach Problem Statement You have been given a sorted and rotated array "arr" of "N" distinct integers. You are also given an integer "target." Your task is to print the index of the integer "target" in array "arr." If "target" does not exist in "arr," then you need to print -1. • "arr" contains distinct elements. This means that there is NO pair of indices (i, j) such that 0 <= i < j < N and arr[i] equals arr[j]. • "arr" is a rotated sorted array. The following array adheres to all the above conditions: Now, if we rotate it three times towards the right, we get a sorted and rotated array: Let’s take a couple of input examples: Input 1: arr = [9, 11, 1, 3, 5 ,7] and target = 5 Output 1: 4 Explanation 1: 5 is present at index 4, so we need to print 4. Input 2: arr = [9, 11, 1, 3, 5 ,7] and target = 8 Output 2: -1 Explanation 2: 8 is not present in "arr," so we need to print -1. Now that we have a clear understanding of our problem, we will move to the solution. Please make sure you’ve read Approach 1, Approach 2, and Approach 3 to understand the coverage better. Approach 4: Single-pass Binary Search by Comparing Mid and Target Elements The graph of the input array looks something like the below graph: In the following image, we have simplified the graph. We’ve modified the two curved poly-lines to straight lines by joining the first and last points. Also, we’ve colored the points on the left poly-line green and the points on the other poly-line red. Key observation: Suppose our search space is bounded by [left, right], and mid is the middle element of the search space. Initially, we will start with a search space that will span the whole array. Then, in each iteration, we reduce the size of the search space in half. We can decide which half to discard by checking if both arr[mid] and target lie on the same part in the image above. How to discard the range/move pointers: There are two cases to consider here — one where both mid and target are on the same part, and the second where they’re on different parts. How will we check this? We will compare arr[mid] with arr [0] and do the same for the target. If both target and arr[mid] are greater/lesser than arr[0], then it shows that they lie on the same part, else they lie on different parts. Case 1: Both mid and target lie on the same part. In the image above, we can see that mid and target lie on the same part. So, we can search the range marked in red. Case 2: When mid and target lie on different parts. We’ll use a trick here — we’ll extend the line of the left part to infinity and the line of the right part to -infinity. In the above case, to move left and right, we need to compare arr[mid] with the target. However, as they are on different sides, we use a comparator in place of arr[mid], as we just need a value with which we can move our left and right pointers. So, we will compare the target with arr[0]. Here, targets lie below arr[0], i.e., arr[0] > target. We will set comparator = -INFINITE. Why? Because we need to go to the right side and to do that, we must compare target with a value smaller than the target. (The same logic is used in standard binary search.) Now, we compare target and comparator. As target > comparator, we move towards the right and set left = left + 1. Else, if comparator > target, we would move towards the left set right = mid - 1. Now, let's consider a scenario where the target lies on the left and mid lies on the right side. In this case, we need to set the comparator to INFINITE. Why? Because our target element lies on the left side, and to move left, we need to compare the target with a value greater than the target. 1. Declare three variables "left", "right," and "mid" for binary search, and initialize left = 0, right = size of array - 1. 2. Run a loop until left <= right: a. Calculate "mid" as mid = (left + right) / 2. b. If (arr[0] > arr[mid]) == (arr[0] > target), then this shows arr[mid] and target lie on the same side with respect to target. so set "comparator" = arr[mid]. c. Else: i. If target < arr[0], then set "comparator" to -INFINITY ii. Else, set "comparator" to INFINITY d. If comparator == target, then return mid as we found our target element. e. If target < comparator, then search target in [left, mid) and move "right" pointer as right = mid - 1. f. Else, search target in (mid, right] and move left pointer as "left" = mid + 1. 3. Return -1 as we are not able to find the target in our array arr. Code in C++ Following is the implementation of the above approach in C++. using namespace std; // Function to search target in array 'arr' int searchInRotatedSortedArray(int arr[], int n, int target) int left = 0, right = n - 1; while (left <= right) { int mid = (left + right) / 2; int comparator; // Check if arr[mid] and target lies on same side if ((arr[0] > arr[mid]) == (arr[0] > target)) comparator = arr[mid]; // Target lies on the right part if (target < arr[0]) comparator = INT_MIN; // Target lies on the left part comparator = INT_MAX; // We have found our target if (comparator == target) return mid; // Target lies on [left, mid) if (comparator > target) right = mid - 1; // Target lies on (mid, right] left = mid + 1; // We are not able to find a target so return -1. return -1; int main() int n = 10; int arr[n] = {10, 13, 16, 20, 25, 1, 3, 4, 5, 8}; int target = 4; cout << searchInRotatedSortedArray(arr, n, target); return 0; Time Complexity O(log(N)), where "N" is the total number of elements in the given array. Similar to Approach 3, we are reducing our search space by half at each step. We get recurrence as T(N) = T(N / 2) + C, where C is Constant. Best Case: O(1) Happens when the comparator equals the target for the first value of mid. Worst Case: O(log(N)) Happens when the target is not present in the array. Average case: O(log(N)) Happens when the target is present in the array. We need to do log(N) steps on average to find the target. Space Complexity Best Case = Worst Case = Average Case = O(1) We are using a few extra variables that take O(1) space. Hence, the space complexity will be O(1). FAQs on Searching an Element in a Sorted and Rotated Array Question 1: Which is the best approach for searching an element in a sorted and rotated array? Answer: For searching an element in a sorted and rotated array, using single-pass binary search is better than using linear search or two-pass binary search. There are two ways of doing this: Single-pass Binary Search Using Casework and Single-pass Binary Search by Comparing Mid and Target Elements (which we covered in this post). Question 2: Would the same solution work if the array had duplicates? Answer: If we had equal numbers, we might not always be able to decide whether we need to move towards the left or right in our binary search algorithm. So, the same solution will not work in that Nervous About Your Next Coding Interview? Let Interview Kickstart help you! As pioneers in the field of technical interview prep, we have trained over 5200 software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more! Article contributed by Pankaj Sharma
{"url":"https://www.interviewkickstart.com/blogs/learn/searching-an-element-in-a-sorted-and-rotated-array-part-4","timestamp":"2024-11-08T17:28:52Z","content_type":"text/html","content_length":"148618","record_id":"<urn:uuid:ad954bfd-fbc1-47fe-8bdf-16ad5441ee18>","cc-path":"CC-MAIN-2024-46/segments/1730477028070.17/warc/CC-MAIN-20241108164844-20241108194844-00487.warc.gz"}
NAPX: A polynomial time approximation scheme for the Noah's ark problem The Noah's Ark Problem (NAP) is an NP-Hard optimization problem with relevance to ecological conservation management. It asks to maximize the phylogenetic diversity (PD) of a set of taxa given a fixed budget, where each taxon is associated with a cost of conservation and a probability of extinction. NAP has received renewed interest with the rise in availability of genetic sequence data, allowing PD to be used as a practical measure of biodiversity. However, only simplified instances of the problem, where one or more parameters are fixed as constants, have as of yet been addressed in the literature. We present NAPX, the first algorithm for the general version of NAP that returns a 1∈-∈ε approximation of the optimal solution. It runs in time where n is the number of species, and B is the total budget and h is the height of the input tree. We also provide improved bounds for its expected running time. Original language English Title of host publication Algorithms in Bioinformatics - 8th International Workshop, WABI 2008, Proceedings Pages 76-86 Number of pages 11 State Published - 28 Nov 2008 Externally published Yes Event 8th International Workshop on Algorithms in Bioinformatics, WABI 2008 - Karlsruhe, Germany Duration: 15 Sep 2008 → 19 Sep 2008 Publication series Name Lecture Notes in Computer Science (including subseries Lecture Notes in Artificial Intelligence and Lecture Notes in Bioinformatics) Volume 5251 LNBI ISSN (Print) 0302-9743 ISSN (Electronic) 1611-3349 Conference 8th International Workshop on Algorithms in Bioinformatics, WABI 2008 Country/Territory Germany City Karlsruhe Period 15/09/08 → 19/09/08 • Approximation algorithm • Noah's ark problem • Phylogenetic diversity ASJC Scopus subject areas • Theoretical Computer Science • General Computer Science Dive into the research topics of 'NAPX: A polynomial time approximation scheme for the Noah's ark problem'. Together they form a unique fingerprint.
{"url":"https://cris.bgu.ac.il/en/publications/napx-a0-polynomial-time-approximation-scheme-for-the-noahs-ark-pr","timestamp":"2024-11-13T11:25:19Z","content_type":"text/html","content_length":"60056","record_id":"<urn:uuid:a09e9fff-4ad2-42ac-abef-9c006d068327>","cc-path":"CC-MAIN-2024-46/segments/1730477028347.28/warc/CC-MAIN-20241113103539-20241113133539-00093.warc.gz"}
Behind The Algorithm #5 - 🧘🏻 Asymtotic Notation Published on Behind The Algorithm #5 - 🧘🏻 Asymtotic Notation Galih Laras Prakoso Asymptotic Efficiency When the input size is large enough, sometimes the most impactful or the only impactful part of a function is the order of growth. The multiplicative constants and the lower-order terms will not giving so much impact to the growth of the running time. Asymptotic efficiency is when we're trying to optimize our algorithm by focusing on the optimization of the order of growth. In other words, we're trying to optimize our algorithm by focusing to decrease the running time by looking at the limit defined by the notation that defined the worst-case of the function while the input increases without bound. Usually, we can compare algorithms or justify which algorithm is better by looking at their asymptotic efficiency. Asymptotic Notation Just to recall, do you remember when we calculated the running time of the insertion sort in the previous article? We know that the insertion sort is having $O(n^2)$ running time right? And we also calculated the running time of the merge sort so we know that it's running time is $O(n \log_2 n)$. As you can see that we ignored the lower-order terms. Because sometimes, forcing to include the extra precision is not usually worth the effort for large enough inputs. To make it more clear, the quadratic running time of the insertion sort $f(n) = an^2 + bc + c$ where $a > 0$. When $n$ is large, even just a tiny fraction of the highest-order term would dominate every single lower-order terms. So, wehen we throw away the lower-order terms and ignore all of the constants we get $f(n) = O(n^2)$. In other way, we can set the constants $c_21 = \frac{a}{8}$, $c_2 = \frac{16a}{8}$, and $n_0 = 2 . max(\frac{|b|}{a}, \sqrt{\frac{|c|}{a}})$. You may also verify that $0 < c_1n^2 \leq an^2 + bn + c \ leq c_2n^2$ for all $n \geq n_0$. We abstracted the running time of the insertion sort which is $an^2 + bn + c$ by writing it as $O(n^2)$. That's how we characterize the worst-case running time of insertion sort. Asymptotic Upper Bound or the Big O notation The worst-case running time of the insertion sort is $O(n^2)$. Let see what's the meaning of this notation by looking at this graph first. We use the Big O notation to give as the upper bound on a function to within a constant factor. As you can see in the image above that the function $f(n)$ is always bellow the $cg(n)$ which is the upper bound of the running time of the function for all values of $n$ is bigger (to the right) or equal to $n_0$. Let's just write the function as $f(n) = O(g(n))$. It means that the $f(n)$ is a member of set $O(g(n))$. In other words, in our insertion sort case, $an^2 + bn +c = O(g(n))$, where $a > 0$.
{"url":"https://www.ghack.dev/blog/behind-the-algorithm/5-asymtotic-notation","timestamp":"2024-11-06T17:58:07Z","content_type":"text/html","content_length":"156772","record_id":"<urn:uuid:d5ca450e-9592-4754-a26b-0751a2fcd6c6>","cc-path":"CC-MAIN-2024-46/segments/1730477027933.5/warc/CC-MAIN-20241106163535-20241106193535-00695.warc.gz"}
Unlock the Power of Pi: Learn How to Calculate 1000 Pi in Dollars and Its Alternatives Main Body: The blog is an essential tool for anyone looking to share their thoughts, ideas and stories with the world. It can be used to express opinions, share personal experiences, and even promote products or services. In its most basic form, a blog is nothing more than an online journal – but it can be so much more. At its core, a blog should provide interesting, informative content that readers will find helpful or entertaining. This could include opinion pieces on current events or trends, tutorials on a particular topic or skill set, reviews of books and movies, creative stories or artwork, recipes and DIY projects – you name it! The possibilities are endless when it comes to blogging. The key to success in blogging is consistency; not only in terms of frequency (regular posts) but also in quality. Quality content is essential if you want people to keep coming back for more. This means ensuring your posts are well-written and engaging while also staying relevant to your niche. You should also make sure you’re utilizing all the available tools at your disposal such as multimedia elements like images and videos; social media integration; SEO optimization; etc., to draw attention from both search engines and readers alike. Blogging can be incredibly rewarding both personally and professionally. It gives you a platform to share your thoughts with others while at the same time connecting you with like-minded individuals who may become future collaborators or customers down the line. It can also help boost your online presence by increasing visibility for yourself/your business across different channels – all without breaking the bank! Finally, with the ability to monetize through adsense programs like Google AdWords or affiliate links from other websites/products/services related to your blog’s subject matter – blogging can even serve as an additional source of income! So if you’re looking for a way to get your voice heard or just fancy giving it a go – why not give blogging a try? You never know where it might take you! Definition of Pi Pi (π) is an irrational number, meaning it cannot be expressed as a fraction or rational number. It is the ratio of the circumference of a circle to its diameter and its value can never be fully expressed because it has an infinite number of digits after the decimal point. Pi has been used for centuries in mathematics and science to solve various problems and equations. The Greek letter π was first used by William Jones in 1706 to represent the ratio, although it had been studied centuries earlier by mathematicians such as Archimedes. The symbol was later adopted by Swiss mathematician Leonhard Euler in 1737. Pi is one of the most important numbers in mathematics as it appears frequently throughout calculus and trigonometry, two fields which are essential for understanding many scientific disciplines like physics, engineering and astronomy. It also pops up in other areas such as probability theory, fractal geometry, complex analysis and number theory. Although Pi is most commonly used with circles, its applications extend far beyond that shape alone. For example, you can use it to calculate the area of any closed curve or even work out how far someone will go on their journey if they start from one point and travel at a certain speed along a curved path for a given amount of time! In recent years Pi has become even more widely known due to its connection with computer science – while not strictly related to mathematical calculations or equations, programming languages often require developers to specify values using a specific number of decimal places after the decimal point – something which works very nicely with Pi’s infinite set of digits! Overall Pi remains one of the most important figures in mathematics today – despite our modern technology being able to calculate more accurate values than ever before we still rely on this ancient figure when solving some of our biggest problems. ii. Understanding the Value of Pi Pi is an important mathematical constant that has been around for centuries. It has been used in a variety of calculations, from calculating the circumference of a circle to solving complex equations. But why is pi so valued? What makes it so special? First and foremost, pi is an irrational number. That means its value cannot be expressed as a simple fraction—it goes on forever without repeating itself. Since it’s value cannot be expressed in terms of other numbers, it can be used to represent fundamental constants in mathematics and science. For example, the ratio of a circle’s circumference to its diameter (which happens to equal pi) can be used to measure distances or angles on a flat surface, such as a map or graph paper. Beyond measuring distances and angles, pi also has applications in probability theory and statistics. In many cases, the mathematical formula for predicting outcomes depends upon the use of pi! For instance, when you’re playing roulette or blackjack at a casino, the odds are calculated using an equation involving pi. Perhaps most interestingly though, Pi can be used to calculate the area of circles and spheres—even if you don’t know their radius or diameter! All you need is this simple equation: A = πr2 where “A” represents the area of your shape and r stands for its radius (or half its diameter). In summary, Pi is an incredibly useful tool in math and science because it never repeats itself and always provides accurate results when applied correctly. Whether you’re finding areas or calculating probabilities – make sure you remember your beloved Pi! iii. How to Calculate the Value of 1000 Pi in Dollars Are you looking to calculate the value of 1000 pi in dollars? If so, you have come to the right place! Pi is a mathematical constant used in many calculations and equations, but it is sometimes difficult to understand how it relates to money. In this blog post, we will explain why pi has no direct monetary value and discuss some ways to convert it into a numerical representation that can be used in financial decisions. First of all, let’s talk about why pi doesn’t have an inherent dollar value. Pi is an irrational number, meaning that its decimal representation never ends or repeats itself. This means that pi is an infinite number; there is no finite way to calculate its exact value. Therefore, it cannot be expressed as a certain amount of money because it does not represent a finite quantity or measure of However, although pi itself does not have any specific dollar value, you can use it for calculations involving money by converting the numerical representation of pi into something more tangible and easy to work with. For example, if you want to figure out how much 1000pi would be worth in dollars, you could use the following equation: 1 dollar = (1000*3.14159)/100 This equation takes into account the fact that 1000pi equals 3141.59 (the first six digits of pi). By multiplying 3141.59 by 100 and then dividing by 1000pi (which equals 3141.59), we get 0.0314159 or $0.03 per unit (in this case 1 unit = 1 thousandth of 1 pi). Therefore, 1000pi would equal $31.41590 in dollars (rounded off). Using this equation or similar ones allows us to convert any numerical representation of pi into real-world values such as dollars which can then be used for financial decision making purposes like budgeting or investing decisions! So even though the actual numerical value of pi might seem abstract at first glance, understanding how it relates to money gives us a better appreciation for its importance in our daily lives and helps us make smarter financial decisions too! iv. Alternatives to Converting 1000 Pi into Dollars Sometimes, converting 1000 Pi into dollars isn’t the most straightforward solution for sending and receiving payments. In fact, there are several other options that can provide more convenience and better security when it comes to making financial transactions. One of the best alternatives to converting 1000 Pi into dollars is using cryptocurrency exchanges. Cryptocurrency exchanges allow users to buy and sell various forms of digital currency, including Pi. This makes it easy to convert 1000 Pi into another form of digital currency, such as Bitcoin or Ethereum, which can then be converted into fiat currencies like USD or EUR. Furthermore, cryptocurrency exchanges typically offer low fees and quick transactions, meaning you can get your money quickly without having to worry about pesky conversion costs. Another option is to use a peer-to-peer payment service such as PayPal or Venmo. These services allow users to send money directly from one person’s account to another without going through a middleman or third-party service provider. With these services, you can easily transfer 1000 Pi directly from your account into another user’s account in their local currency with minimal fees and fast processing time. Finally, if you don’t want to convert your Pi into dollars at all but still need access to traditional banking systems for other reasons such as withdrawals from ATMs or transferring funds abroad, there are also services that allow this without involving any conversion process at all. For example, services like TransferWise allow users to send money directly between bank accounts across different countries while avoiding costly conversion fees associated with traditional banks and other financial institutions. No matter what option you choose in order to convert 1000 Pi into dollars (or any other currency), make sure that you do your research beforehand so you know exactly how much it will cost and how long it will take before committing yourself financially! Conclusion: Summary and Implications The blog we have discussed in this article is a powerful tool for connecting with potential customers and building your brand. It can be used to create valuable content that educates and entertains, increasing your visibility and creating an engaging experience for readers. By regularly sharing quality content, you can establish yourself as a thought leader in your industry while simultaneously developing relationships with customers. Additionally, blogging allows you to experiment with different types of content, such as videos or infographics, and test out new ideas which can help increase customer engagement. Ultimately, blogging is a great way to connect with customers and build relationships while also growing your business. Through strategic use of quality content that adds value to the reader’s experience, you can position yourself as an authority in your industry and make it easier for potential customers to find you on search engines. Although it takes time and effort to maintain a successful blog strategy, the rewards are well worth it: increased visibility, customer loyalty, reputation boost among peers – all of which contribute to the growth of your business in the long run.
{"url":"https://www.croptheblock.com/crypto/unlock-the-power-of-pi-learn-how-to-calculate-1000/","timestamp":"2024-11-05T15:47:46Z","content_type":"text/html","content_length":"46292","record_id":"<urn:uuid:0925e89a-e84b-41c9-ae83-fdef82f3f36c>","cc-path":"CC-MAIN-2024-46/segments/1730477027884.62/warc/CC-MAIN-20241105145721-20241105175721-00787.warc.gz"}
Estimating a non-linear model in OpenMx Replied on Fri, 11/12/2021 - 10:37 Here's how I would do the model you proposed. modelExp<-mxModel("exp", type="RAM", manifestVars='y', mxMatrix('Full', nrow=1, ncol=3, labels=c('b0', 'b1', 'b2'), name='params', free=TRUE, values=c(.5, -1, 1)), mxAlgebra(b2+(b0-b2)*exp(b1*data.time), name='predY'), mxPath(from="one", to="y", labels='predY[1,1]', free=FALSE), mxPath(from="y", arrows=2, labels='resid', values=1), mxData(data_list, type="raw") It seems to do okay. > summary(fit) Summary of exp free parameters: name matrix row col Estimate Std.Error A 1 resid S y y 0.04087680 0.0005780564 2 b0 params 1 1 5.00507642 0.0130146711 3 b1 params 1 2 -0.09956287 0.0005180620 4 b2 params 1 3 0.99726107 0.0026133824 Replied on Mon, 11/15/2021 - 11:37 In reply to Nonlinear Regression by mhunter Thanks that does seem to work Thanks that does seem to work! Huge help. Replied on Mon, 11/15/2021 - 13:20 In reply to Nonlinear Regression by mhunter Follow up question: is it Follow up question: is it possible to include a latent variable in the mxAlgebra expression? For example, if I had some latent variable "L" that I wanted to include in an arbitrary expression [say, y =b1*log(b2*L)], I am guessing that there is a way to do that, but it doesn't seem to have a clear analog to the manifest variable case [i.e., there isn't a way to do b1*log(b2*latent.L)]. Replied on Thu, 11/18/2021 - 09:19 AdminNeale Joined: 03/01/2013 In reply to Follow up question: is it by pgok Not exactly A latent variable doesn’t have a value per se. it is a variable with a distribution. One possibility would be to create a mixture distribution of say 12 latent trait values weighted by Gaussian quadrature weights. This is currently one way of handling such things, but it doesn’t scale well as with 2 latent moderators it would take 144 evaluations to get the likelihood of a data vector, and generally with m quadrature points and d latent moderators, m^d gets large fast. There may be an alternative in OpenMx 3.0, but it’s not ready yet. Some time in 2022 looks likely though. The other route would be via MCMC, but there appear to be many pitfalls and limitations in that area. Replied on Thu, 11/18/2021 - 18:44 In reply to Not exactly by AdminNeale Thanks for the response. I Thanks for the response. I can't say I'm terribly surprised that it gets radically more complex for the latent variable case, but I figured I'd ask anyways. I'll be interested to see what OpenMx 3.0 has in store for this though. Thanks again!
{"url":"https://openmx.ssri.psu.edu/comment/9416","timestamp":"2024-11-08T08:36:21Z","content_type":"text/html","content_length":"39234","record_id":"<urn:uuid:d727224a-0cd7-4ef7-878d-e3024330529f>","cc-path":"CC-MAIN-2024-46/segments/1730477028032.87/warc/CC-MAIN-20241108070606-20241108100606-00359.warc.gz"}
Network Working Group Request for Comments: 4056 Category: Standards Track J. Schaad Soaring Hawk Consulting June 2005 Use of the RSASSA-PSS Signature Algorithm in Cryptographic Message Syntax (CMS) Status of This Memo This document specifies an Internet standards track protocol for the Internet community, and requests discussion and suggestions for improvements. Please refer to the current edition of the "Internet Official Protocol Standards" (STD 1) for the standardization state and status of this protocol. Distribution of this memo is unlimited. Copyright Notice Copyright © The Internet Society (2005). This document specifies the conventions for using the RSASSA-PSS (RSA Probabilistic Signature Scheme) digital signature algorithm with the Cryptographic Message Syntax (CMS). 1. Overview This document specifies the conventions for using the RSA Probabilistic Signature Scheme (RSASSA-PSS) [P1v2.1] digital signature algorithm with the Cryptographic Message Syntax [CMS] signed-data content type. CMS values are generated using ASN.1 [X.208-88], using the Basic Encoding Rules (BER) [X.209-88] and the Distinguished Encoding Rules (DER) [X.509-88]. This document is written to be used in conjunction with RFC 4055 [RSA-ALGS]. All of the ASN.1 structures referenced in this document are defined in RFC 4055. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 1.1. PSS Algorithm Although there are no known defects with the PKCS #1 v1.5 [P1v1.5] signature algorithm, RSASSA-PSS [P1v2.1] was developed in an effort to have more mathematically provable security. PKCS #1 v1.5 signatures were developed in an ad hoc manner; RSASSA-PSS was developed based on mathematical foundations. 2. Algorithm Identifiers and Parameters 2.1. Certificate Identifiers The RSASSA-PSS signature algorithm is defined in RFC 3447 [P1v2.1]. Conventions for encoding the public key are defined in RFC 4055 [RSA-ALGS]. Two algorithm identifiers for RSA subject public keys in certificates are used. These are: rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 } id-RSASSA-PSS OBJECT IDENTIFIER ::= { pkcs-1 10 } When the rsaEncryption algorithm identifier is used for a public key, the AlgorithmIdentifier parameters field MUST contain NULL. Complete details can be found in [RSA-ALGS]. When the id-RSASSA-PSS algorithm identifier is used for a public key, the AlgorithmIdentifier parameters field MUST either be absent or contain RSASSA-PSS-params. Again, complete details can be found in [RSA-ALGS]. In both cases, the RSA public key, which is composed of a modulus and a public exponent, MUST be encoded using the RSAPublicKey type. The output of this encoding is carried in the certificate subject public key. RSAPublicKey ::= SEQUENCE { modulus INTEGER, -- n publicExponent INTEGER } -- e 2.2. Signature Identifiers The algorithm identifier for RSASAA-PSS signatures is: id-RSASSA-PSS OBJECT IDENTIFIER ::= {pkcs-1 10 } When the id-RSASSA-PSS algorithm identifier is used for a signature, the AlgorithmIdentifier parameters field MUST contain RSASSA-PSS- params. Information about RSASSA-PSS-params can be found in [RSA- ALGS]. When signing, the RSA algorithm generates a single value, and that value is used directly as the signature value. 3. Signed-data Conventions digestAlgorithms SHOULD contain the one-way hash function used to compute the message digest on the eContent value. The same one-way hash function SHOULD be used for computing the message digest on both the eContent and the signedAttributes value if signedAttributes exist. The same one-way hash function MUST be used for computing the message digest on the signedAttributes and as the hashAlgorithm in the RSA- PSS-params structure. signatureAlgorithm MUST contain id-RSASSA-PSS. The algorithm parameters field MUST contain RSASSA-PSS-params. signature contains the single value resulting from the signing operation. If the subjectPublicKeyInfo algorithm identifier for the public key in the certificate is id-RSASSA-PSS and the parameters field is present, the following additional steps MUST be done as part of signature validation: 1. The hashAlgorithm field in the certificate subjectPublicKey.algorithm parameters and the signatureAlgorithm parameters MUST be the same. 2. The maskGenAlgorithm field in the certificate subjectPublicKey.algorithm parameters and the signatureAlgorithm parameters MUST be the same. 3. The saltLength in the signatureAlgorithm parameters MUST be greater or equal to the saltLength in the certificate subjectPublicKey.algorithm parameters. 4. The trailerField in the certificate subjectPublicKey.algorithm parameters and signatureAlgorithm parameters MUST be the same. In doing the above comparisons, default values are considered to be the same as extant values. If any of the above four steps is not true, the signature checking algorithm MUST fail validation. 4. Security Considerations Implementations must protect the RSA private key. Compromise of the RSA private key may result in the ability to forge signatures. The generation of RSA private key relies on random numbers. The use of inadequate pseudo-random number generators (PRNGs) to generate these values can result in little or no security. An attacker may find it much easier to reproduce the PRNG environment that produced the keys, searching the resulting small set of possibilities, rather than brute force searching the whole key space. The generation of quality random numbers is difficult. RFC 1750 [RANDOM] offers important guidance in this area. Using the same private key for different algorithms has the potential of allowing an attacker to get extra information about the key. It is strongly suggested that the same key not be used for both the PKCS #1 v1.5 and RSASSA-PSS signature algorithms. When computing signatures, the same hash function should be used for all operations. This reduces the number of failure points in the signature process. The parameter checking procedures outlined in section 3 are of special importance. It is possible to forge signatures by changing (especially to weaker values) these parameter values. Signers using this algorithm should take care that only one set of parameter values is used as this decreases the possibility of leaking information. 5. Normative References [CMS] Housley, R., "Cryptographic Message Syntax (CMS)", RFC 3852, July 2004. [P1v2.1] Jonsson, J. and B. Kaliski, "Public-Key Cryptography Standards (PKCS) #1: RSA Cryptography Specifications Version 2.1", RFC 3447, February 2003. [RSA-ALGS] Schaad, J., Kaliski, B., and R. Housley, "Additional Algorithms and Identifiers for RSA Cryptography for use in the Internet X.509 Public Key Infrastructure Certificate and Certificate Revocation List (CRL) Profile", RFC 4055, June 2005. [STDWORDS] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, March 1997. [X.208-88] CCITT Recommendation X.208: Specification of Abstract Syntax Notation One (ASN.1), 1998. [X.209-88] CCITT Recommendation X.209: Specification of Basic Encoding Rules for Abstract Syntax Notation One (ASN.1), [X.509-88] CCITT Recommendation X.509: The Directory Authentication Framework, 1988. 6. Informative References [P1v1.5] Kaliski, B., "PKCS #1: RSA Encryption Version 1.5", RFC 2313, March 1998. [RANDOM] Eastlake 3rd, D., Crocker, S., and J. Schiller, "Randomness Recommendations for Security", RFC 1750, December 1994. Author' Address Jim Schaad Soaring Hawk Consulting PO Box 675 Gold Bar, WA 98251 Full Copyright Statement Copyright © The Internet Society (2005). This document is subject to the rights, licenses and restrictions contained in BCP 78, and except as set forth therein, the authors retain all their rights. This document and the information contained herein are provided on an "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND Intellectual Property The IETF takes no position regarding the validity or scope of any Intellectual Property Rights or other rights that might be claimed to pertain to the implementation or use of the technology described in this document or the extent to which any license under such rights might or might not be available; nor does it represent that it has made any independent effort to identify any such rights. Information on the procedures with respect to rights in RFC documents can be found in BCP 78 and BCP 79. Copies of IPR disclosures made to the IETF Secretariat and any assurances of licenses to be made available, or the result of an attempt made to obtain a general license or permission for the use of such proprietary rights by implementers or users of this specification can be obtained from the IETF on-line IPR repository at http://www.ietf.org/ipr. The IETF invites any interested party to bring to its attention any copyrights, patents or patent applications, or other proprietary rights that may cover technology that may be required to implement this standard. Please address the information to the IETF at ietf- ipr@ietf.org. Funding for the RFC Editor function is currently provided by the Internet Society.
{"url":"http://pike.lysator.liu.se/docs/ietf/rfc/40/rfc4056.xml","timestamp":"2024-11-13T06:07:28Z","content_type":"text/html","content_length":"21653","record_id":"<urn:uuid:acaf55d1-456c-40c6-8598-0ce7c6d650bd>","cc-path":"CC-MAIN-2024-46/segments/1730477028326.66/warc/CC-MAIN-20241113040054-20241113070054-00230.warc.gz"}
New on CTAN: bropd Date: November 6, 2012 10:28:12 AM CET Benjamin Tatlock submitted the new bropd package to CTAN. Summary description: Simplified brackets and differentials License type: lppl Announcement text: The bropd package simplifies the process of writing differential operators and brackets in LaTeX. The commands facilitate the easy manipulation of equations involving brackets and allow partial differentials to be expressed in an alternate form. This package is located at . More information is at (if the package is new it may take a day for that information to appear). We are supported by the TeX Users Group . Please join a users group; see . Thanks for the upload. For the CTAN Team Rainer Schöpf bropd – Simplified brackets and differentials in LaTeX The package simplifies the process of writing differential operators and brackets in LaTeX. The commands facilitate the easy manipulation of equations involving brackets and allow partial differentials to be expressed in an alternate form. Package bropd Version 1.2 Copyright 2012 Benjamin Tatlock Maintainer Benjamin Tatlock
{"url":"https://ctan.org/ctan-ann/id/mailman.1857.1352197707.2390.ctan-ann@dante.de","timestamp":"2024-11-07T20:03:29Z","content_type":"text/html","content_length":"14485","record_id":"<urn:uuid:b9e994e1-dbb6-4c51-8ed6-2582aaa00e80>","cc-path":"CC-MAIN-2024-46/segments/1730477028009.81/warc/CC-MAIN-20241107181317-20241107211317-00245.warc.gz"}
descriptive and inferential statistics worksheet The number of student taking statistics exam. In the first section, you will be introduced to procedures used to obtain several descriptive statistics, Highlight the required answers to the question in your Excel output. a) Descriptive statistics b) Discrete variables c) Inferential Statistics d) A sample 4. The post Descriptive and Inferential Statistics Worksheet appeared first on Top Grade Professors. Using these formulas and performing these calculations provides you with an understanding of how and why statistical formulas work and what they mean. Inferential Statistics. Descriptive and Inferential Statistics Worksheet. Imagine you are the assistant manager of a fast food store. 4. utilizes the information that of descriptive EDA Before making inferences from data it is essential to examine all your variables. Using these formulas and performing these calculations provides you with an understanding of how and why statistical formulas work and what they mean. The number of student taking statistics exam. Use your knowledge of descriptive, statistics and write one paragraph to let the store manager know what happened today. Descriptive statistics is a type of statistics that are For example, inferential statistics can deduce whether a sample size is representative of its population, whether two samples are significantly equal in terms of mean, and if two variables have 1 Review Exercises pg. the population. For example, we could calculate the mean and standard deviation of the exam marks for the 100 students and this could provide valuable information about this group of 100 students. Part ABefore completing the following questions, be sure to have read Appendix C and the Statistical Software Resources at the ends of Chapters 2 and 3 from Statistics Plain and Simple. Your What is probability? There are four main SPSS: Descriptive and Inferential Statistics 3 The Department of Statistics and Data Sciences, The University of Texas at Austin This tutorial describes the use of SPSS to obtain descriptive and inferential statistics. "A diet high in fruits and vegetables will lower blood pressure". Academic level. statistics is a very important and powerful thing mode. scores: that of a sample. needed because it depends on the type of data that is being used and then at that point is when This preview shows page 1 - 3 out of 6 pages. Use your knowledge of descriptive statistics and write one paragraph to let the store manager know what happened today. Descriptive and Inferential Statistics When analysing data, such as the grades earned by 100 students, it is possible to use both descriptive and inferential statistics in your analysis. That means that you need to be able to apply the definitions of the words or terms and recognize their incidence. ioc.pdf Prerequisites Statistics Statistic (i) Statistics is the course you are studying … This worksheet also provides you the opportunity to interpret results in the context of […] c) There is a relationship between smoking cigarettes and … Continuous . I would really depend on the was is being analyzed and for that kind of reason that data would be b) The chances of winning the California Lottery are one chance in twenty-two million. Median is the middle value of the data set Course Hero is not sponsored or endorsed by any college or university. statistics. valid and reliable and can be robustly analyzed. Math 227 – Elementary Statistics: A Brief Version, 5/e Bluman Ch. Descriptive And Inferential Statistics Worksheet May 2, 2020 This worksheet provides you with experience in calculating (by hand or using Excel®) formulas from the weekly readings. Get help with your classes. Although descriptive statistics is helpful in learning things such as the spread and center of the data, nothing in descriptive statistics can be used to make any generalizations. statistics and write one paragraph to let the store manager know what happened today. set. Which test had the Descriptive statistics can be … that is therefor do not need to force a larger in order to be able to overestimate the standard Descriptive Inferential Statistics Wk1 Done, Copyright © 2021 StudeerSnel B.V., Keizersgracht 424, 1016 GC Amsterdam, KVK: 56829787, BTW: NL852321363B01, PSYCH 625 r5 Wk2 Hypothesis Testing Table-1 draft, PSYCH 625 r5 Wk2 Probability Statistical Analysis, Nicole S. Jackson PSYCH 625 r5 Wk1 Descriptive Inferential Statistics, Statistics Project Part 2 Hypothesis Testing and Two Group t test, PSYCH 625 r5 Wk3 Statistics in Literature. blind and with treatment there are certain levels Part of your job is to report which special is selling best to the store manager at the end of each day. data Statistics data sets population sample parameter a statistic descriptive statistics inferential statistics In this statistics worksheet, learners analyze problems by finding the mean, median and mode. Descriptive statistics lesson plans and worksheets from thousands of teacher-reviewed resources to help you inspire students learning. following data. What are their similarities and differences? answer should be 175 to 350 words. Use the Highlight the required answers to the question in your Excel output. How would you interpret the differences between exams, and note over the course of a semester. What this does is to Forces the standard deviation that is to be larger than it would normally be so Which mean is the average of the data Please sign in or register to post comments. that of central tendency measure that were the Using Microsoftå Excelå, The readings are attached. which of the mean, mode and median would be used. Descriptive vs. Inferential Statistics . Worksheet PSYCH/625 Version 5. 1.5 Statistical Analysis in Psychology Concept Map on 1.5 - Descriptive Statistics PowerPoint - Descriptive Statistics Concept Map - Demo- Mean, Median, and Mode. Use the following data. While descriptive statistics give basic information of the dataset, inferential statistics help give important conclusions about the data set. Worksheet PSYCH/625 Version 5. To listen to the data: - to catch mistakes - to see patterns in the data - to find violations of statistical assumptions Psychology homework help. Differentiate between descriptive and inferential 1 Descriptive and Inferential Statistics 2 Variables 3 Percentiles 4 Measurement 5 Distributions 6 Graphing Distributions margarita.spitsakova@ttu.ee ICY0006: Lecture 1 2/78. rudimentary information that of the dataset, and About This Quiz & Worksheet. Descriptive and Inferential Statistics Worksheet. Before completing the following questions, be sure to have read Appendix C and the Statistical Software Resources at the ends of Chapters 2 and 3 from Statistics Plain and Simple.. example of which type of data . average of $2.55 that for each special. Descriptive and Inferential Statistics Worksheet Paper. 3. tendency and measure of variability. Ad Analysis. Providing examples of each, compare and PSYCH 625 Assignment Week 1 Descriptive … Descriptive and inferential statistics are both statistical procedures that help describe a data sample set and draw inferences from the same, respectively. Inferential statistics only offer generalizations of a larger whole. Using these formulas and performing these calculations provides you with an understanding of how and why statistical formulas work and what they mean. Descriptive And Inferential - Displaying top 8 worksheets found for this concept.. Ordinal . Descriptive and Inferential Statistics Worksheet This worksheet provides you with experience in calculating (by hand or using Excel®) formulas from the weekly readings. Continue to order Get a quote. Displaying top 8 worksheets found for - Descriptive And Inferential. Inferential Statistics . Measures of variability is the measures that how SUMMARY. smallest amount of variability? There is total value of food that was sold was 303.00 that is with an This is an example of which type of data a) Continuous b) Ordinal c) Qualitative d) Discrete 5. Complete both Part A and Part B below. CLove_Week 1_Descriptive and Inferential Statistics Worksheet.docx, Week One Homework Exercise psych 625.docx, PSYCH 625 Week 1 Individual Assignment - Time to Practice (Parts A,B,C), Descripive and Inferential Statistics Worksheet.docx, Psych 625 Week2_Probability and Statistical Analysis Worksheet.docx. standard deviation, variance, Range, and IQR. Use the following data. Your answer should be 175 to 350 words. most of the data are rests. Psych 625 week 1 #2.docx - Running head DESCRIPTIVE AND INFERENTIAL STASTICS WORKSHEET 1 Descriptive and Inferential Statistics Worksheet Complete both, 16 out of 16 people found this document helpful, Running head: DESCRIPTIVE AND INFERENTIAL STASTICS WORKSHEET, Descriptive and Inferential Statistics Worksheet, Before completing the following questions, be sure to have read Appendix C and the Statistical Software, Resources at the ends of Chapters 2 and 3 from. Statistics For The Behavior Sciences (PSYCH 625). Probability And Statistics Descriptive Vs Inferential - Displaying top 8 worksheets found for this concept.. use and why? The Chicken Littles sold the most. What measure of central tendency (mean, median or mode) would you This worksheet provides you with experience in calculating (by hand or using Excel®) formulas from the weekly readings. Descriptive statistics offer straightforward conclusions that can be graphed or noted in amounts. 26 (1-9 all, 10b, c, d, e, 11-13 all, 17, 23, 25, 27, 29) 1. why don’t enjoy your day, and let me do your assignments At LindasHelp I can do all your assignments, labs, and final exams too. Typically, in most research conducted on groups of people, you will use both descriptive and inferential statistics to analyse your results and draw conclusions. Why? psychological studies are very important and that University of Phoenix Material Descriptive and Inferential Statistics Worksheet. Post navigation. Place your order. statistics. and mode is the most frequently occurring value. Inferential statistics use the data to draw conclusions about the population. that in the order of the ERR on the side that would be overestimation that of standard deviation that of Imagine you are the assistant manager of a fast food store. Descriptive and Inferential Statistics Worksheet PSYCH 625 WEEK 1 Do you need help with your Descriptive and Inferential Statistics Worksheet PSYCH 625 WEEK 1? 2. Showing top 8 worksheets in the category - Descriptive And Inferential. That is when a biased estimate deals with one the same sample size of the population WORKSHEET – Extra examples (Chapter 1: sections 1.1,1.2,1.3) Chapter 1: Introduction to statistics 1.1 An Overview of Statistics a) definitions of data, population, sample, population parameter, sample statistic; b) difference between descriptive and inferential statistics 1.2 Data Classification a) qualitative data vs. quantitative data; Pages (550 words) Approximate price: $ 22. In test one the average score was 49, in test two the average was 50.1 and in test three it was. Highlight the required answers to the question in your Excel output. Descriptive statistics describe the data set. conclusion that is about the data set. That is being able to determine the , enter the following data from the 40 participants by first creating a variable, labeled “Score”. Statistical Vocabulary 1 Know these words and be able to associate / use them with respect to class concepts. DESCRIPTIVE AND INFERENTIAL STASTICS WORKSHEET 2 2. Statistics is the science. We have seen that descriptive statistics provide information about our immediate group of data. dis 3. The baby burger made the least amount The three different types of measurement for In descriptive statistics, measurements such as the mean and standard deviation are stated as exact numbers. able to calculate that is the basic characteristics Next, compute the mean, median, and mode for the following set of 40 reading Type of paper. Inferential statistics is Name and define two areas of statistics. Using these formulas and performing these calculations provides you with an understanding of how and why statistical formulas work and what they mean. Highlight the required answers to the question in your Excel output. of conducting studies to : Monitor , ... Descriptive statistics . because in behavioral sciences and the range, means, and standard deviations over time. contrast the four levels of measurement. While there is a give Discrete variables. They also set data by their median, and quartiles. measures that of variability which are the Suppose you are working with a data set that has some different (much larger or much smaller than Part A. The ScienceStruck article below enlists the difference between descriptive and inferential statistics with examples. Differences: The margin of error is much higher with inferential statistics. Part of your job is to report which special, is selling best to the store manager at the end of each day. Using Microsoft® Excel®, enter the following data from the 40 participants by first creating a variable View Homework Help - PSYCH 625 Assignment Week 1 Descriptive and Inferential Statistics Worksheet (New Syllabus).doc from PSYCH 625 at Ashford University. Descriptive Statistics Worksheet (Obj. deviation. Complete both Part A and Part B below.. Part A. central tendency and they are mean, median and Microsoft® Excel® to compute all the descriptive statistics for the following set of three test scores During the course of a semester, 10 students in Mr. Smith’s class took three exams. Worksheet 1. Deadline. of treatment that may dependent on variables. Before completing the following questions, be sure to have read Appendix C and the Statistical Software Resources at the ends of Chapters 2 and 3 from Statistics Plain and Simple. descriptive statistics that are measured of central that can create a legitimate study that is for both is selling best to the store manager at the end of each day. Use your knowledge of descriptive Before completing the following questions, be sure to have read Appendix C and the Statistical Software Measures Some of the worksheets for this concept are Math 227 elementary statistics a brief version 5e bluman, Spss descriptive and inferential statistics, Work extra examples, Lecture 2 descriptive statistics and exploratory data, Descriptive analysis in education a guide for researchers, Ibm spss statistics 23 part … Title ABC/123 Version X 1 Descriptive and Inferential Statistics Worksheet PSYCH/625 Version 5 2 University of Phoenix Material Descriptive and Inferential Statistics Worksheet Complete both Part A and Part B below. This worksheet provides you with experience in calculating (by hand or using Excel®) formulas from the weekly readings. Why? inferential statistics that are given a significant Identify whether the statement describes inferential statistics or descriptive statistics: a) The average age of the students in a statistics class is 21 years. Use the. Which test had the highest average score? There are two highly important This is an. A sample . Use your knowledge of descriptive statistics and write one paragraph to let the store manager know what happened today. conclusions of the data sets. Imagine you are the assistant manager of a fast food store. Descriptive and Inferential Statistics Worksheet Complete both Part A and Part B below. Next, compute the mean, median, and mode for the following set of 40 reading, DESCRIPTIVE AND INFERENTIAL STASTICS WORKSHEET, Imagine you are the assistant manager of a fast food store. There can be study that is created blind, double Part of your job is to report which special Inferential Statistics Descriptive Statistics Probability “Central Dogma” of Statistics. - Demo- Standard Deviation on a Test - Demo- Rolling Dice - Inferential Statistics PowerPoint - Reading - Confidence Intervals PCN 540 GCU Topic 4 Assignment . Part A Before completing the following questions, be sure to have read Appendix C and the Statistical Software Resources at the ends of Chapters 2 and 3 from Statistics … Descriptive and Inferential Statistics Worksheet This worksheet provides you with experience in calculating (by hand or using Excel ® ) formulas from the weekly readings. more complex form of statistics and that are Part of your job is to report which special is selling best to the store manager at the end of each day. Statistics about a population under study can either be descriptive or inferential. Use What information do they provide? Resources at the ends of Chapters 2 and 3 from Statistics Plain and Simple. labeled “Score”. the rest of the data) scores. the spread out the data is. Be … the post descriptive and Inferential statistics with examples statistics descriptive Vs -! While descriptive statistics can be graphed or noted in amounts words ) Approximate price: $ 22 and recognize incidence! For the following data from the weekly readings: SUMMARY resources to you... Complex form of statistics that are able to calculate that is being able to that... Required answers to the store manager know what happened today Discrete variables c ) Inferential statistics d a. Procedures that help describe a data sample set and draw inferences from data it is essential to examine all variables! ( 550 words ) Approximate price: $ 22 three it was Do you need help your! Sold was 303.00 that is being able to apply the definitions of the,... Article below enlists the difference between descriptive and Inferential descriptive and inferential statistics worksheet page 1 - 3 out of pages. In twenty-two million measurements such as the mean and standard deviation, variance, Range, and is... And they are mean, median or mode ) would you use and why statistical formulas work and what mean. That are measured of central tendency and they are mean, median and mode with an understanding of and. These calculations provides you with an understanding of how and why statistical formulas and... Basic information of the dataset, Inferential statistics Worksheet appeared first on top Professors. Or noted in amounts highly important descriptive statistics Inferential statistics d ) Discrete variables c ) Qualitative d Discrete... Displaying top 8 worksheets found for - descriptive and Inferential statistics Worksheet Paper section, you will be introduced procedures! Set and draw inferences from the weekly readings section, you will be introduced to procedures to. Worksheet also provides you with an understanding of how and why statistical formulas and... - Displaying top 8 worksheets found for this concept to procedures used to obtain several descriptive statistics that are to... The standard deviation, variance, Range, means, and quartiles important conclusions the... ] descriptive and Inferential statistics Worksheet Paper statistics use the data set and.! Are able to determine the conclusions of the dataset, Inferential statistics is a type of data a ) statistics! The end of each day Displaying top 8 worksheets found for this..! Measured of central tendency ( mean, median or mode ) would use... Material descriptive and Inferential statistics Worksheet descriptive and inferential statistics worksheet in your Excel output plans worksheets... One the average of the words or terms and recognize their incidence price: $ 22 Do! Each special was sold was 303.00 that is being able to calculate that is with an understanding of and... Each day data are rests data from the weekly readings creating a variable labeled “ ”... Which special is selling best to the question in your Excel output you use and why statistical work... 10 students in Mr. Smith ’ s class took three exams they also set data by their median, mode... That help describe a data sample set and mode for the following data from the 40 by. And worksheets from thousands of teacher-reviewed resources to help you inspire students.. That can be graphed or noted in amounts occurring value with examples over time deviations... We have seen that descriptive statistics Displaying top 8 worksheets in the context of …! Microsoft® Excel® to compute all the descriptive statistics provide information about our immediate group of data need. Scores: SUMMARY their median, and note the Range, and mode there are four main that! Or noted in amounts probability and statistics descriptive and Inferential statistics use the data rests! To report which special, is selling best to the store manager know happened! Not sponsored or endorsed by any college or university teacher-reviewed resources to you. Mean and standard deviations over time 625 Assignment WEEK 1 Do you need help with your descriptive and Inferential Displaying! The ScienceStruck article below enlists the difference between descriptive and Inferential statistics Worksheet, learners analyze by. Information about our immediate group of data a ) descriptive statistics and write one paragraph let. Inferential - Displaying top 8 worksheets found for - descriptive and Inferential statistics d a... The category - descriptive and Inferential statistics 40 reading scores: SUMMARY to. You will be introduced to procedures used to obtain several descriptive statistics your. Interpret the differences between exams, and mode is the basic characteristics that of descriptive statistics and write paragraph. Tendency measure that were the most of the words or terms and recognize their incidence ScienceStruck article below the! Median is the basic characteristics that of central tendency ( mean, median or mode ) you. Of measurement basic characteristics that of a fast food store Material descriptive and Inferential statistics Worksheet Complete both a... The measures that of descriptive statistics for the following data from the 40 participants by first a... Variability is the average of the words or terms and recognize their incidence Part... To obtain several descriptive statistics and write one paragraph to let the store at! Data it is essential to examine all your variables to procedures used to obtain several descriptive can. Several descriptive statistics give basic information of the data are rests Part a and b... Semester, 10 students in Mr. Smith ’ s class took three exams measurement for tendency. Several descriptive statistics that are able to calculate that is the measures that the... Of winning the California Lottery are one chance in twenty-two million and performing these calculations provides you with an of! These formulas and performing these calculations provides you the opportunity to interpret results in the first section, will. $ 22 be graphed or noted in amounts statistics about a population under study can either be or! B below.. Part a generalizations of a sample 4 example of which of! To calculate that is being able to calculate that is being able to the. By finding the mean and standard deviation are stated as exact numbers in Mr. ’. Of variability is the average Score was 49, in test three it was and they mean! Of 6 pages first on top Grade Professors that was sold was 303.00 that the... All your variables first on top Grade Professors, 10 students in Mr. Smith s! The basic characteristics that of variability job is to report which special, is selling best to question. A population under study can either be descriptive or Inferential of winning the California Lottery are one in!, statistics and write one paragraph to let the store manager at the end of each day thousands... Only offer generalizations of a fast food store 40 participants by first creating a variable, labeled “ ”. 10 students in Mr. Smith ’ s class took three exams, means, and quartiles vegetables will blood! Worksheet Complete both Part a and Part b below.. Part a and Part below. For this concept about a population under study can either be descriptive or Inferential conclusions the... Probability and statistics descriptive and Inferential statistics only offer generalizations of a larger whole: the margin error. Four main measures that of descriptive statistics for the following set of 40 scores. The information that of descriptive statistics Inferential statistics Worksheet PSYCH 625 ) two the average was 50.1 and in three. Of food that was sold was 303.00 that is being able to determine the conclusions of dataset... Top 8 worksheets found for this concept 625 WEEK 1 standard deviation are stated as exact numbers creating a labeled... Words or terms and recognize their incidence ) the chances of winning California... Of how and why statistical formulas work and descriptive and inferential statistics worksheet they mean the measures that of descriptive can... They are mean, median or mode ) would you use and why food that sold! Course Hero is not sponsored or endorsed by any college or university give important conclusions about the to... Statistics descriptive and inferential statistics worksheet give important conclusions about the data set and mode is basic. Out the data set hand or using Excel® ) formulas from the weekly.... From thousands of teacher-reviewed resources to help you descriptive and inferential statistics worksheet students learning differences: the margin of error much! Report which special, is selling best to the store manager at the end of each day eda making! Fruits and vegetables will lower blood pressure '' four levels of measurement draw inferences from 40! Total value of food that was sold was 303.00 that is with an understanding of how why. A and Part b below labeled “ Score ” have seen that descriptive statistics provide information about our immediate of! Resources to help you inspire students learning participants by first creating a variable, labeled “ Score ” is complex! In Mr. Smith ’ s class took three exams interpret the differences between exams, and quartiles average! For the following set of three test scores over the course of a fast food store data statistics data population. C ) Qualitative d ) a sample Worksheet 1 Vs Inferential - Displaying top 8 worksheets the! Difference between descriptive and Inferential which are the assistant manager of a larger whole worksheets... With Inferential statistics Worksheet PSYCH 625 WEEK 1 Do you need to be able to determine the conclusions of data. Offer generalizations of a fast food store will be introduced to procedures used to several... Middle value of the data sets population sample parameter a statistic descriptive is! Median is the measures that of descriptive statistics basic characteristics that of descriptive statistics will introduced! Data sample set and draw inferences from data it is essential to examine all your variables blood! Be … the post descriptive and Inferential statistics Worksheet PSYCH 625 WEEK 1 … post... Offer generalizations of a semester the California Lottery are one chance in million. White Christmas Harmonica Tabs When To Evolve Pignite Long Live The Night Cfl Lyrics Golden Gate Opening Times Advantages Of Afis When The Gastroesophageal Sphincter Contracts, Food How To Play Corridos On Guitar Praise Father, Son And Holy Ghost Amen Lyrics Inch Ruler Measurements Clark County Schools Washington
{"url":"https://boghammar.se/3o5dy9ys/descriptive-and-inferential-statistics-worksheet-3481c0","timestamp":"2024-11-10T03:26:04Z","content_type":"text/html","content_length":"33217","record_id":"<urn:uuid:6851ae2e-729e-4ac6-8715-a663a1f24fd3>","cc-path":"CC-MAIN-2024-46/segments/1730477028164.3/warc/CC-MAIN-20241110005602-20241110035602-00772.warc.gz"}
Convert Barns to Square Nanometers (barn to nm2, sq nm) | JustinTOOLs.com Category: areaConversion: Barns to Square Nanometers The base unit for area is square meters (Non-SI/Derived Unit) [Barns] symbol/abbrevation: (barn) [Square Nanometers] symbol/abbrevation: (nm2, sq nm) How to convert Barns to Square Nanometers (barn to nm2, sq nm)? 1 barn = 1.0E-10 nm2, sq nm. 1 x 1.0E-10 nm2, sq nm = Square Nanometers. Always check the results; rounding errors may occur. In relation to the base unit of [area] => (square meters), 1 Barns (barn) is equal to 1.0E-28 square-meters, while 1 Square Nanometers (nm2, sq nm) = 1.0E-18 square-meters. 1 Barns to common area units 1 barn = 1.0E-28 square meters (m2, sq m) 1 barn = 1.0E-24 square centimeters (cm2, sq cm) 1 barn = 1.0E-34 square kilometers (km2, sq km) 1 barn = 1.0763915051182E-27 square feet (ft2, sq ft) 1 barn = 1.5500031000062E-25 square inches (in2, sq in) 1 barn = 1.1959900463011E-28 square yards (yd2, sq yd) 1 barn = 3.8610215859254E-35 square miles (mi2, sq mi) 1 barn = 1.5500031000062E-19 square mils (sq mil) 1 barn = 1.0E-32 hectares (ha) 1 barn = 2.4710516301528E-32 acres (ac)
{"url":"https://www.justintools.com/unit-conversion/area.php?k1=barns&k2=square-nanometers","timestamp":"2024-11-07T00:14:39Z","content_type":"text/html","content_length":"74313","record_id":"<urn:uuid:4b7c03fa-9b63-4e33-98ae-8827750235e9>","cc-path":"CC-MAIN-2024-46/segments/1730477027942.54/warc/CC-MAIN-20241106230027-20241107020027-00066.warc.gz"}
Detect and Remove Loop in a Linked List In this article, we will look at an interesting problem related to Linked List . ‘Given a Single Linked List, Remove Loop if there exists any’. We will look at different ways to solve this problem and analyze the complexities of our approach. Now, To Remove a Loop in a Linked List, we need to first Detect a Loop in the linked list. If a Loop exists we remove it with our logic and print the elements of the list. So having a Loop in a Linked List means there is no such node having a NULL pointer or reference. In simpler terms, the Linked list has no end. Let us understand this with an example. Here, we have a Linked List with 5 nodes, Node 12 is the Head node. We can see a Loop exists in the list connecting nodes 25 and 99. So we need to Remove this Loop such that the last node (25) points to NULL. After removal the List should look like this: We can see the last node now points to NULL and the loop no longer exists. Now, let us look at different approaches to solve the problem. Note: If the Loop starts from any other node except the last node we need to change its reference to NULL to remove the loop. Approach 1 (Using Hashing Concept) • In this approach we will hash or store the references of each node of the Linked List in a Unordered Set or Hash-Set in Java, which contains Unique elements. • So, we will traverse the list node by node add it to our Set, if not present. We will have a Pointer/Variable prev which will hold the previous node reference and every time we add the node to the Set, we update the prev to Current Node’s reference or address. • When we encounter a node pointing to a node already present in the Set, we know that we encountered a loop so the set will already have the node when we try to add it. • Hence in that case we do not add the node again, we make the last node or prev point to NULL. Thereby, removing the loop in the linked list. Let us look at the implementation of above in java: import java.util.*; class Node int data; Node next; Node(int data) this.data = data; next = null; public class LinkedList static Node head; // head node // print the linked list elements static void printList(Node node) while (node != null) System.out.print(node.data + " "); node = node.next; // Returns true if the loop is removed from the linked list else returns false. static boolean detectandRemoveLoop(Node head) Set<Node> hs = new HashSet<Node>(); Node prev = null; Node curr = head; while (curr != null) // if set contains current node we detect a loop. // Make prev which hold the last node point to null. System.out.println("Loop Detected at: "+curr.data); prev.next = null; return true; // If we see the node for the first time, add it to our set. prev = curr; curr = curr.next; return false; public static void main(String[] args) Node head = new Node(12); head.next = new Node(99); head.next.next = new Node(37); head.next.next.next = new Node(5); head.next.next.next.next = new Node(25); /*Create loop for testing */ head.next.next.next.next.next = head.next; if (detectandRemoveLoop(head)) System.out.print("Linked List after Removing Loop: "); System.out.println("No Loop Exists"); Loop Detected at: 99 Linked List after Removing Loop: 12 99 37 5 25 Time Complexity: We do a single traversal of the Linked List until we get the loop so the complexity is O(n). Space Complexity: We use a Hash-Set which at most stores all the nodes of the Linked List, so the space complexity is O(n), for n nodes in list. Approach 2 (Floyd’s Cycle Detection Algorithm) • In this approach, we use Floyd’s Cycle Detection Method to detect loop/cycle in our linked list. We use two pointers: One Slow and Another Fast Pointer. We move the slow pointer by one position and the fast pointer by two positions. If they meet at a point then we can say Loop is detected. • Now after detecting loop we have two cases to consider: 1. If Fast Pointer or Loop is Detected at Head Node. 2. If Fast Pointer is at any other node. • If the Loop is detected at Head Node or the fast pointer is at head node, we move the Slow pointer until it reaches the node before the head node or the node where loop begins and make its reference to NULL. • For the 2^nd Case, we bring the Slow to position of the head of list. We then continue moving the slow and the fast pointer until slow.next = fast.next . We then make the Fast pointer reference as NULL, thus removing the loop. Now let us have a quick look at the implementation in java: import java.util.*; class Node int data; Node next; Node(int data) this.data = data; next = null; public class LinkedList static Node head; // head node // print the linked list elements static void printList(Node node) while (node != null) System.out.print(node.data + " "); node = node.next; static void detectAndRemoveLoop(Node head) // if there are no nodes in list. if(head == null) Node slow, fast; slow = fast = head; while(fast.next != null && fast.next.next != null) slow = slow.next; fast = fast.next.next; if(slow == fast) // Check whether Loop is detected. if(slow == fast) // Case 1 if(fast == head) while(slow.next != fast) slow = slow.next; System.out.println("Loop Detected at : "+ fast.data); slow.next = null; // Case 2 slow = head; while(slow.next != fast.next) slow = slow.next; fast = fast.next; System.out.println("Loop Detected at : "+ fast.next.data); fast.next = null; public static void main(String[] args) // Creating the Linked List with 5 nodes. Node head = new Node(12); head.next = new Node(99); head.next.next = new Node(37); head.next.next.next = new Node(5); head.next.next.next.next = new Node(25); /*Create loop for testing */ head.next.next.next.next.next = head.next; System.out.print("Linked List after Removing Loop: "); Loop Detected at : 99 Linked List after Removing Loop: 12 99 37 5 25 So we have implemented the code for the same example discussed above. We can see the nodes where the loop is detected and removed. Now let us have a quick look at the Complexities for this approach. Time Complexity: The time complexity is O(n), since we traverse all nodes of the list before we detect the cycle/loop. Space Complexity: This approach is efficient in terms of space required which is O(1), since we do not use any auxiliary space instead we just used two pointers. So that’s it for the article you can try implementing the code with different examples. Let us know your suggestions or doubts in the comment section below. Leave a Comment
{"url":"https://www.thecrazyprogrammer.com/2021/04/loop-in-a-linked-list.html","timestamp":"2024-11-02T09:23:59Z","content_type":"text/html","content_length":"138417","record_id":"<urn:uuid:b8a554f3-f506-49da-9cd5-a634b52b7d61>","cc-path":"CC-MAIN-2024-46/segments/1730477027709.8/warc/CC-MAIN-20241102071948-20241102101948-00874.warc.gz"}
Trigonometry on Sale Trigonometry 101 Trigonometry - Wikipedia Trigonometry (from Ancient Greek τρίγωνον (trígōnon) 'triangle' and μέτρον (métron) 'measure') [1] is a branch of mathematics concerned with relationships between angles and side lengths of Introduction to Trigonometry - Math is Fun The main functions in trigonometry are Sine, Cosine and Tangent. They are simply one side of a right-angled triangle divided by another. For any angle "θ": (Sine, Cosine and Tangent are often abbreviated to sin, cos and tan.) Trigonometry - Khan Academy Learn trigonometry—right triangles, the unit circle, graphs, identities, and more. Trigonometry | Definition, Formulas, Ratios, & Identities | Britannica Trigonometry, the branch of mathematics concerned with specific functions of angles. There are six functions commonly used in trigonometry: sine (sin), cosine (cos), tangent (tan), cotangent (cot), secant (sec), and cosecant (csc). Learn more about trigonometry in this article. Trigonometry (Functions, Table, Formulas & Examples) - BYJU'S Trigonometry is a branch that delas with the study of the relationship between sides and angles of a right triangle. Visit BYJU’S to learn the trigonometry formulas, ratios, tables, functions and Sine, Cosine and Tangent - Math is Fun Sine, Cosine and Tangent are the main functions used in Trigonometry and are based on a Right-Angled Triangle. Before getting stuck into the functions, it helps to give a name to each side of a right triangle: "Opposite" is opposite to the angle θ. "Adjacent" is adjacent to (next to) the angle θ. "Hypotenuse" is the long one. Trigonometry - What is Trigonometry? Formulas, Table, Examples - Cuemath Trigonometry is the branch of mathematics that deals with the relationship between ratios of the sides of a right-angled triangle with its angles. The ratios used to study this relationship are called trigonometric ratios, namely, sine, cosine, tangent, cotangent, secant, cosecant. Trigonometry For Beginners! - YouTube This math video tutorial provides a basic introduction into trigonometry. It covers trigonometric ratios such as sine, cosine, and tangent. It explains how to evaluate it using right triangle ... Unit 2: Trigonometry - Khan Academy Precalculus 10 units · 131 skills. Unit 1 Composite and inverse functions. Unit 2 Trigonometry. Unit 3 Complex numbers. Unit 4 Rational functions. Unit 5 Conic sections. Unit 6 Vectors. Unit 7 Matrices. Unit 8 Probability and combinatorics. Trigonometric functions - Math.net Trigonometric functions are functions related to an angle. There are six trigonometric functions: sine, cosine, tangent and their reciprocals cosecant, secant, and cotangent, respectively. Sine, cosine, and tangent are the most widely used trigonometric functions. Their reciprocals, though used, are less common in modern mathematics.
{"url":"http://trigonometry101.com/Trigonometry-on-Sale","timestamp":"2024-11-02T12:28:52Z","content_type":"text/html","content_length":"42979","record_id":"<urn:uuid:65e3b052-2e3e-4562-8f8e-c024948f995f>","cc-path":"CC-MAIN-2024-46/segments/1730477027710.33/warc/CC-MAIN-20241102102832-20241102132832-00589.warc.gz"}
Estimation of Models with Heteroskedastic errors With heteroskedastic errors the OLS estimator still gives an unbiased estimator. That is, the proof that the OLS estimator is unbiased does not use the heteroskedasticity assumption. The heteroskedasticity affects the results in two ways: 1. The OLS estimator is not efficient (it does not have minimum variance). 2. The estimators of the variances are biased. The standard errors reported on the SHAZAM output do not make any adjustment for the heteroskedasticity - so incorrect conclusions may be made if they are used in hypothesis tests. A number of solution approaches can be considered as follows. 1. The observed heteroskedasticity in the residuals may be an indication of model misspecification such as incorrect functional form. For example, a log-log model may reduce heteroskedasticity compared to a linear model. Gujarati [1995, p.386] comments that the log transformation compresses the scales in which the variables are measured. 2. If the model specification is considered adequate then it may be useful to focus on just correcting the second problem above. The HETCOV option on the OLS command will compute White's (due to Hal White) heteroskedasticity-consistent covariance matrix of the parameter estimates. The SHAZAM OLS estimation output will then report the standard errors that are adjusted for heteroskedastic errors. These may be larger or smaller than the uncorrected standard errors. An application of heteroskedasticity-consistent standard errors is available. 3. To obtain an efficient estimator an estimation method is weighted least squares (WLS). This is a special case of generalized least squares (GLS). The application of this method requires specifying a functional form for the error variance. Examples of Weighted Least Squares 1. Variance with pre-set form 2. 2-step Estimation Note: Another estimation approach that has application to the estimation of models with heteroskedastic errors is maximum likelihood estimation. This is available in SHAZAM with the HET command. This command also has options for the estimation of time series models with ARCH and GARCH errors. Users must first study the principle of maximum likelihood as well as nonlinear optimisation methods before attempting to use the HET command. [SHAZAM Guide home] Computing Heteroskedasticity-Consistent Standard Errors The HETCOV option on the OLS command is used to obtain standard errors that are corrected for some unknown form of heteroskedasticity. This example uses the Griffiths, Hill and Judge data set on household expenditure that was analyzed in the section on testing for heteroskedasticity. The SHAZAM commands (filename: OLSHET.SHA) below compute both the OLS standard errors and the heteroskedasticity corrected standard errors. SAMPLE 1 40 READ (GHJ.txt) FOOD INCOME * Test for heteroskedasticity DIAGNOS / HET * Get the heteroskedasticity corrected standard errors OLS FOOD INCOME / HETCOV The SHAZAM output can be viewed. The OLS standard errors are reported in the following output: VARIABLE ESTIMATED STANDARD T-RATIO PARTIAL STANDARDIZED ELASTICITY NAME COEFFICIENT ERROR 38 DF P-VALUE CORR. COEFFICIENT AT MEANS INCOME .23225 .5529E-01 4.200 .000 .563 .5631 .6871 CONSTANT 7.3832 4.008 1.842 .073 .286 .0000 .3129 When the HETCOV option is specified the estimation results are: VARIABLE ESTIMATED STANDARD T-RATIO PARTIAL STANDARDIZED ELASTICITY NAME COEFFICIENT ERROR 38 DF P-VALUE CORR. COEFFICIENT AT MEANS INCOME .23225 .6911E-01 3.361 .002 .479 .5631 .6871 CONSTANT 7.3832 4.292 1.720 .094 .269 .0000 .3129 Both regressions report identical OLS estimated coefficients. But the second regression reports larger standard errors. So hypothesis testing that relies on the results of the first regression may give misleading results. [SHAZAM Guide home] SHAZAM output |_SAMPLE 1 40 |_READ (GHJ.txt) FOOD INCOME UNIT 88 IS NOW ASSIGNED TO: GHJ.txt 2 VARIABLES AND 40 OBSERVATIONS STARTING AT OBS 1 |_OLS FOOD INCOME 40 OBSERVATIONS DEPENDENT VARIABLE = FOOD ...NOTE..SAMPLE RANGE SET TO: 1, 40 R-SQUARE = .3171 R-SQUARE ADJUSTED = .2991 VARIANCE OF THE ESTIMATE-SIGMA**2 = 46.853 STANDARD ERROR OF THE ESTIMATE-SIGMA = 6.8449 SUM OF SQUARED ERRORS-SSE= 1780.4 MEAN OF DEPENDENT VARIABLE = 23.595 LOG OF THE LIKELIHOOD FUNCTION = -132.672 VARIABLE ESTIMATED STANDARD T-RATIO PARTIAL STANDARDIZED ELASTICITY NAME COEFFICIENT ERROR 38 DF P-VALUE CORR. COEFFICIENT AT MEANS INCOME .23225 .5529E-01 4.200 .000 .563 .5631 .6871 CONSTANT 7.3832 4.008 1.842 .073 .286 .0000 .3129 |_* Test for heteroskedasticity |_DIAGNOS / HET DEPENDENT VARIABLE = FOOD 40 OBSERVATIONS 0.232253330328 7.38321754308 CHI-SQUARE D.F. P-VALUE TEST STATISTIC E**2 ON YHAT: 12.042 1 0.00052 E**2 ON YHAT**2: 13.309 1 0.00026 E**2 ON LOG(YHAT**2): 10.381 1 0.00127 E**2 ON LAG(E**2) ARCH TEST: 2.565 1 0.10926 LOG(E**2) ON X (HARVEY) TEST: 4.358 1 0.03683 ABS(E) ON X (GLEJSER) TEST: 11.611 1 0.00066 E**2 ON X TEST: KOENKER(R2): 12.042 1 0.00052 B-P-G (SSR) : 11.283 1 0.00078 E**2 ON X X**2 (WHITE) TEST: KOENKER(R2): 14.582 2 0.00068 B-P-G (SSR) : 13.662 2 0.00108 |_* Get the heteroskedasticity corrected standard errors |_OLS FOOD INCOME / HETCOV 40 OBSERVATIONS DEPENDENT VARIABLE = FOOD ...NOTE..SAMPLE RANGE SET TO: 1, 40 R-SQUARE = .3171 R-SQUARE ADJUSTED = .2991 VARIANCE OF THE ESTIMATE-SIGMA**2 = 46.853 STANDARD ERROR OF THE ESTIMATE-SIGMA = 6.8449 SUM OF SQUARED ERRORS-SSE= 1780.4 MEAN OF DEPENDENT VARIABLE = 23.595 LOG OF THE LIKELIHOOD FUNCTION = -132.672 VARIABLE ESTIMATED STANDARD T-RATIO PARTIAL STANDARDIZED ELASTICITY NAME COEFFICIENT ERROR 38 DF P-VALUE CORR. COEFFICIENT AT MEANS INCOME .23225 .6911E-01 3.361 .002 .479 .5631 .6871 CONSTANT 7.3832 4.292 1.720 .094 .269 .0000 .3129 [Back to Top] [SHAZAM Guide home]
{"url":"http://www.econometrics.com/intro/glshet.htm","timestamp":"2024-11-07T10:23:06Z","content_type":"text/html","content_length":"9406","record_id":"<urn:uuid:b3cc2293-2ee8-41e1-85dd-a1cfef350548>","cc-path":"CC-MAIN-2024-46/segments/1730477027987.79/warc/CC-MAIN-20241107083707-20241107113707-00870.warc.gz"}
Singing 32 ways to play your hand in video poker - Будем Здоровы Singing 32 ways to play your hand in video poker In 1975, Paul Simon sang about 50 Ways to Leave Your Lover. While I like the song, I have to admit, I’ve never listened to it closely enough to know if he actually lists 50 ways or even starts to list any. Fortunately, in video poker, there are only 32 ways. No, not to leave your lover, but to draw on your hand. When I tell people this, the most common reaction is to give me that little deer in the headlights type of look. So, let me explain. When dealt an initial 5-card hand in video poker, you have to choose which of the 32 ways to go with it. What does this 32 represent? Well, I think it is fairly obvious that you can choose to keep the five cards you were dealt. That’s way No. 1. You can also choose to discard all five cards and draw new ones. We’ll call that way No. 2. If you decide you want to draw 1 card, there are five ways you can do this. You can discard any one of the five cards originally dealt to you. So, these are ways 3-7. In similar fashion, you can choose to keep any one of the five cards and discard the remaining four. This gives us five more ways or 8-12. Only 20 more ways to go, but it does get a bit more complex. The last remaining choices are to discard two (and keep three) or to discard three (and keep two). If we have five cards and we want to pick two of them, there are 10 combinations we can pick from. Mathematically, this is called 5 Choose 2. To calculate this, we need to use something called a factorial, which is a number multiplied by itself and all numbers less than it, down to 1. It is represented by an exclamation mark. So five factorial (5!) is 5 x 4 x 3 x 2 x 1. This works out to be 120. 5 Choose 2 is mathematically equivalent to 5! divided by 2! divided by (5-2)! In the end, this turns out to be 10. This is true so long as order does not matter. Thus we are picking two cards that are not assigned a position of 1 and 2, but are simply those we are choosing to keep or discard. So, we have our remaining 20 ways – 10 that go with discarding two cards and 10 that go with discarding three. This gives us ways 13-32. Of course, the average player doesn’t play video poker thinking through each of the 32 ways. Instead, the human brain can look at the 5-cards dealt to him and determine what hand type he has and match it to the strategy table. The player also doesn’t take into account the specific card position to notice that there are 10 ways to discard 2 cards. Instead that person might recognize having a 3-card Straight Flush and needing to discard the other two cards. For about 75% of the hands, the choice is very easy and straight forward. The importance of strategy comes into play with the remaining 25%. But, even for these, most of the time there are only two ways the player must consider. Perhaps he has a Low Pair and a 4-card Flush or a 3-card Straight Flush that is also a 4-card Straight. Once in a while there’s a hand that has three ways that must be considered. The hand might be a 3-card Straight Flush, a 4-card Flush and a Low Pair. I’m sure if I thought about it long enough, I’d find cases where the player might have to consider four ways, but these are rare, if they exist at all. So, why do we concern ourselves with talking about the 32 different ways a hand can be played? On a theoretical level, all 32 must be analyzed to determine which maximizes our overall payback. For a computer program, it is almost easier for it to consider every possibility rather than attempt to eliminate them through some form of human assumptions programmed in. These assumptions might lead to errors. Who knew 20 years ago they would come up with all the bonus pay tables that they have? Who knows what “bizarre” pay table might be offered in the future? Thus, when I create programs that analyze a game like video poker, I don’t make any assumptions about how the hand should be played. I allow the computer to look over every possible combination. When the computer is analyzing every draw for all possible ways to play a hand for each of the 2,598,960 possible 5-card deals, it is actually analyzing approximately 6.7 trillion situations. It is almost hard to believe that when all is said and done, we can boil this down to about 35-40 lines on a strategy table. Добавить комментарий Для отправки комментария вам необходимо авторизоваться. Закладка Постоянная ссылка.
{"url":"http://bumizd.ru/singing-32-ways-to-play-your-hand-in-video-poker/","timestamp":"2024-11-11T16:47:29Z","content_type":"text/html","content_length":"39907","record_id":"<urn:uuid:56e504bd-b02a-410a-883d-5d9223a84f1c>","cc-path":"CC-MAIN-2024-46/segments/1730477028235.99/warc/CC-MAIN-20241111155008-20241111185008-00604.warc.gz"}
Welcome to Quickmath Solvers! Enter a matrix and click the Inverse button. In this section multiplicative identity elements and multiplicative inverses are introduced and used to solve matrix equations. This leads to another method for solving systems of equations. IDENTITY MATRICES The identity property for real numbers says that a * I = a and I * a = a for any real number a. If there is to be a multiplicative identity matrix I, such that: AI = A and IA = A, for any matrix A, then A and I must be square matrices of the same size. Otherwise it would not be possible to find both products. For example, let A be the 2 X 2 matrix and let represent the 2 X 2 identity matrix. To find I, use the fact that IA = A, or Multiplying the two matrices on the left side of this equation and setting the elements of the product matrix equal to the corresponding elements of A gives the following system of equations with variables x11, x12, x21, and x22 Notice that this is really two systems of equations in two variables. Use one of the methods of the previous chapter to find the solution of this system: x11 = 1, x12 = x21 = 0, and X22 = 1. From the solution of the system, the 2 X 2 identity matrix is Check that with this definition of I, both Al = A and IA = A. Example 1 VERIFYING THE IDENTITY PROPERTY Verify that MI = M and IM = M The 2 X 2 identity matrix found above suggests the following generalization: n x n IDENTITY MATRIX For any value of n there is an n X n identity matrix having l's down the diagonal and 0's elsewhere. The n x n identity matrix is given by l where: Here aij = 1 when i = j (the diagonal elements) and aaj = 0 otherwise. Example 2 - STATING AND VERIFYING THE 3 X 3 IDENTITY MATRIX Let K = Given the 3 X 3 identity matrix I and show that KI = K. The 3 X 3 identity matrix is By the definition of matrix multiplication, MULTIPLICATIVE INVERSES For every nonzero real number a, there is a multiplicative inverse l/a such that Recall that l/a can also be written a^(-1). In the rest of this section, a method is developed for finding a multiplicative inverse for square matrices. The multiplicative inverse of a matrix A is written A^(-1). This matrix must satisfy the statements The multiplicative inverse of a matrix can be found using the matrix row transformations given in the previous tutorial and repeated here for convenience. The matrix row transformations are: 1. interchanging any two rows of a matrix; 2. multiplying the elements of any row of a matrix by the same nonzero scalar k; and 3. adding a multiple of the elements of one row to the elements of another row. As an example, let us find the inverse of Let the unknown inverse matrix be By the definition of matrix inverse, AA^(-1) = 1, or By matrix multiplication, Setting corresponding elements equal gives the system of equations Since equations (1) and (3) involve only x and z, while equations (2) and (4) involve only y and w, these four equations lead to two systems of equations, 2x + 4z = 1 2y + 4w = 0 Writing the two systems as augmented matrices gives Each of these systems can be solved by the Gauss-Jordan method. However, since the elements to the left of the vertical bar are identical, the two systems can be combined into the one augmented and solved simultaneously as follows. Exchange the two rows to get a 1 in the upper left comer. Multiply the first row by -2 and add the results to the second row to get Now, to get a I in the second-row, second-column position, multiply the second row by 1/6. Finally, add the second row to the first row to get a 0 in the second column above the 1. The numbers in the first column to the right of the vertical bar give the values of x and z. The second column gives the values of y and w. That is, so that To check, multiply A by A^(-1). The result should be I Verify that A - 1 A = I, also. Finally, The process for finding the multiplicative inverse A^(-1) n x n matrix A that has an inverse is summarized below. To obtain A^(-1) n x n matrix A for which A^(-1) exists, follow these steps. 1. Form the augmented matrix [A/I], where I is the n x n identity matrix. 2. Perform row transformations on [A|I] to get a matrix of the form [I|B]. 3. Matrix B is A^(-1). 4. Verify by showing that BA = AB = I. CAUTION Only square matrices have inverses, but not every square matrix has an inverse. If an inverse exists, it is unique. That is, any given square matrix has no more than one inverse. Note that the symbol A^(-1) does not mean 1/A; the symbol A^(-1) is just the notation for the inverse of matrix A.
{"url":"https://www.quickmath.com/webMathematica3/quickmath/matrices/inverse/basic.jsp","timestamp":"2024-11-04T01:45:13Z","content_type":"text/html","content_length":"40370","record_id":"<urn:uuid:6437fffb-526e-4c4a-8ec8-42e122798257>","cc-path":"CC-MAIN-2024-46/segments/1730477027809.13/warc/CC-MAIN-20241104003052-20241104033052-00691.warc.gz"}
Stepwise diagram for developing biosimilars.Type I error for case 1. F: for the fixed margin, S: for the synthesis margin. The significance level is 0.025 for NI approach but is 0.05 for both equivalence and cNI approaches.Power comparison for different methods for case 2. F: for the fixed margin, S: for the synthesis margin.Type I error for case 3. F: for the fixed margin, S: for the synthesis margin. The value on the left side of the y-axis is for the equivalence and cNI approaches while the value in the right side of the y-axis is for the NI approach.Two mock datasets.Statistical results for the two mock datasets using effect preservation f = 0.5.References To develop a biosimilar product, it is necessary to demonstrate biosimilarity between the proposed biosimilar product and the reference product in terms of the purity, potency, efficacy, and safety. In this paper, clinical efficacy data required for establishing biosimilarity are considered. Non-inferiority (NI) and equivalence methods are commonly used for analyzing clinical trials to meet this requirement. The equivalence approach often requires large, costly, and lengthy clinical trials. The non-inferiority approach while requiring somewhat smaller trials are not accepted by all as adequately addressing the similarity issue between the proposed biosimilar product and the reference product as they do not rule out the prospect that the biosimilar product has increased activity which might be associated with more adverse effects. To address some of the challenges faced by the use of non-inferiority or equivalence methods, a constrained non-inferiority (cNI) approach is proposed to address both the clinical efficacy of the biosimilar product and the similarity to the reference product. The performance of the proposed constrained non-inferiority approach for analyzing a biosimilar trial is demonstrated through simulation and examples. Biosimilarity, Constrained non-inferiority (cNI), Effect preservation, Efficacy, Equivalence, Non-inferiority (NI), Plausibility interval, Power, Type I error In order to reduce health care costs and to increase accessibility, the development of biosimilars or Follow-on Biologics has received a lot of attention lately in the US. Biologics were 7 of the top 10 bestselling drugs in 2012, 2013 and 2014 including 6 mAbs [1-3], and the Congressional Budget Office had projected savings from $42 billion on the low end to as high as $108 billion over the first 10 years of biosimilar market formation to the US economy [4]. With the Biologics Price Competition and Innovation Act of 2009 and subsequent passage into law in 2010, a legal pathway exists in the US for the approval of biosimilars. Although many details surrounding statistical approaches still need to be sorted and clarified, in February 2012, the FDA released two draft guidances related to biosimilars and an accompanying Q & A document to aid Sponsors in the development of biosimilars [5-7]. In the EU, the product specific legal pathway was formed in 2001 and then later revised. Regulatory guidelines began appearing in 2005 and there have since been many additional guidelines published [8,9]. However, similar to the FDA guidances, the guidelines did not provide any details around statistical methods to be used in the establishment of biosimilarity [10]. A biosimilar product is a biological product that is highly similar to the reference product notwithstanding minor differences in clinically inactive components. There can be no clinically meaningful differences between the biosimilar biological product and the reference product in terms of the efficacy, safety, purity, and potency of the product. Due to the biological complexity and unlikeliness of being structurally identical to the reference product, many potential differences between the proposed biosimilar product and the reference product can arise, which can potentially significantly affect the efficacy, safety, purity, and/or potency. Thus, a direct head to head comparison is needed between the proposed biosimilar product and the reference product with respect to the structure, function, animal toxicity, human pharmacokinetics (PK) and pharmacodynamics (PD) if applicable, clinical immunogenicity, and clinical safety and effectiveness. As specified in the FDA guidances [5-7], a totality of the evidence in a stepwise approach fashion is recommended for demonstration of biosimilarity between the test and the reference products. As outlined in the guidances, the information submitted in the application for biosimilars should include the structural and functional characterization, nonclinical evaluation, human PK and PD data, clinical immunogenicity data, and clinical safety and efficacy data, and this stepwise approach can be illustrated graphically as shown in figure 1, where extensive characterization of the reference using fingerprint-like techniques and the variability assessment of the reference using different lots, regions, and time shifted reference products are performed to define the reference target product profile before the direct head-to-head test-reference comparison. Note that the figure 1 here is similar to the figure described by Babbit and Nick [2011] but the reference characterization part is added [11]. A rich literature exists for comparing the efficacy of two products using non-inferiority or equivalence approaches [12-15]. Much recent discussion has centered on the use of synthesis or fixed-margin approaches where historical trials with the reference product are used in concert with a new trial comparing test to reference in order to demonstrate non-inferiority or equivalence. The fixed-margin approach determines the margin ahead of the new trial and traditionally uses the lower limit of a 95% confidence interval based on a meta-analysis of historical trials comparing reference to placebo. The synthesis method combines the results from the historical trials with the current trial to determine non-inferiority or equivalence. Of note, Snapinn and Jiang [2008a] demonstrated that an appropriately chosen synthesis method controls type I error while always providing higher power than the traditional fixed-margin approach described above [12]. FDA guidance also discusses these approaches [16]. In the setting of biosimilars, the equivalence approach to establish similar efficacy can lead to large clinical trials that are expensive and take a long time to conduct. The cost, risk, and length of time required to establish biosimilarity in clinical trials using traditional equivalence approach adversely affects the economic viability of their development. Though non-inferiority (NI) requires a smaller trial compared to the equivalence approach, the non-inferiority approach does not rule out the prospect that the biosimilar product is more efficacious, leading to concerns by some that test is not similar enough to reference with potential increased activity which might be associated with more adverse effects. Therefore, the use of non-inferiority rather than equivalence trials remains controversial. In order to realize a greater cost savings to the public by having more biosimilars on the market sooner rather than later, there is interest in considering alternative approaches that require less sample size but address the necessary requirement for demonstrating the biosimilarity. One such approach is proposed here. In section 2, the proposed constrained non-inferiority (cNI) approach is described. A simulation study is conducted to compare the power and the type I error of the proposed constrained non-inferiority approach to traditional non-inferiority and the equivalence approaches in section 3. Examples are used to illustrate the proposed approach in section 4, and a summary follows in section 5. To demonstrate the similarity of clinical efficacy in support of biosimilarity of the proposed biosimilar product (T) to the approved reference product (R), as with other approaches, the available data from the historical trials comparing R with placebo (P) and from a new biosimilar clinical trial directly comparing T with R will be used. Without loss of generality, we assume the efficacy is measured on some arbitrary scale such that a larger value represents better efficacy. Further, we assume that the summary statistic measuring efficacy can be reasonably assumed to have a normal distribution, where treatment effect can be measured in, for example, the mean difference between groups, the log-odds ratio, the log-relative risk, or the log-hazard ratio. Let γRP be the true effect of R relative to P and γTR be the true effect of T relative to R. Therefore, γTR + γRP represents the true effect of T relative P. Let BRP and VRP be the estimate of γRP and the estimated variance of that estimate, respectively, based on a meta-analysis of the appropriate historical clinical trials. Similarly, let BTR and VTR be the estimate of γTR and the estimated variance of that estimate based on the new biosimilar clinical trial. Three approaches to assess the biosimilarity between T and R will be compared with a fixed margin and synthesis approach for each. Non-inferiority (NI) approach In this approach, given a margin δ, the goal is to demonstrate the proposed biosimilar product T is not worse than the reference product R by more than δ. The choice of the non-inferiority margin raises a lot of discussion in the literature. However, no matter what the margin is, in order for this approach to be valid, the margin δ must not be larger than the true effect of R (i.e., the δ must be less than or equal to γRP). One common approach for selecting δ is to pick a value that preserves at least some non-zero fraction, f, of the effect of R. This could be based on the lower limit of the 95 percent confidence interval for the reference product relative to the placebo, i.e., δ=(1−f)(BRP−1.96VRP−−−√), a common fixed margin approach and part of the 95-95 approach where the 95% confidence interval (CI) for γTR is compared to this margin. Thus, the hypotheses for non-inferiority are H01:γTR≤−δ VS Ha1:γTR>−δ If a fixed-margin is used, the non-inferiority is established if BTR−1.96VTR−−−√>−δ, i.e., BTR+(1−f)BRPVTR√+(1−f)2VRP√>1.96 (1) If a synthesis margin is used, then the non-inferiority is established [12] if BTR+(1−f)BRPVTR+(1−f)2VRP√>1.96 (2) Note that FDA guidance prefers the fixed margin for the NI approach [16]. However, it is of interest to note that the left side of these equations will always be larger for the synthesis approach (the terms in the denominator are > 0 and 1/A+B−−−−−√≥1/(A−−√+B−−√) ), and hence the synthesis method is uniformly more powerful than the 95-95 fixed margin approach, or more generally, than any double CI based fixed margin approach provided that the assay sensitivity and constancy are accounted for in order to control the type I error. Thus, both the fixed margin and the synthesis margin approaches are used to demonstrate the performance of all methods in the simulation. A commonly used value that is referenced as a reasonable starting point for discussion in FDA guidance is f = 0.5, i.e., 50% effect preservation [16]. This value will be utilized for illustrative and comparative purposes in the simulation study and the illustration examples presented later in this paper. Equivalence approach The hypotheses for equivalence are H02:γTR≤−δ or γTR≥δ VS Ha2:−δ<γTR<δ If the fixed margin is used, then equivalence is established if BTR−1.96VTR−−−√>−δ and BTR+1.96VTR−−−√<δ, i.e., BTR+(1−f)BRPVTR√+(1−f)2VRP√>1.96 and BTR−(1−f)BRPVTR√+(1−f)2VRP√<−1.96 (3) Using a similar argument as in the non-inferiority case, if a synthesis margin is used, then equivalence is established if BTR+(1−f)BRPVTR+(1−f)2VRP√>1.96 and BTR−(1−f)BRPVTR+(1−f)2VRP√<−1.96 (4) Constrained non-inferiority (cNI) approach It is a well-known fact that an equivalence approach requires much larger sample size to achieve the same power as the non-inferiority approach, but that the non-inferiority approach only guarantees that the test product is not inferior to the reference product. These two have different merits and concerns. Is it possible to have an approach using a similar sample size of NI approach but also address the similarity? As mentioned in the FDA guidance, it is necessary to show that the proposed biosimilar product has neither decreased nor increased activity compared to the reference product [5-7]. Decreased activity ordinarily would preclude licensure of a proposed biosimilar product. Increased activity might be associated with more adverse effects, or might suggest that the proposed biosimilar product should be treated as an entirely different product with superior efficacy. Thus, the traditional NI plus additional constraints to ensure the two products are similar should address this issue. The cNI approach represents a middle ground where greater evidence is required to demonstrate that test is no worse than reference, but that some evidence/constraints supporting that test is not more efficacious than reference is also required. Recall that the summary statistic measuring efficacy was assumed to have a normal distribution, which is determined by two parameters: the mean and the variance or range. The similarity constraints can be based on these two parameters. If there is a scientific/clinical justifiable threshold value for how similar the two products should be, then this threshold value should always be used to judge the biosimilarity between the proposed biosimilar product and the reference product. However, this is usually not a feasible task. The assessment of how similar the proposed biosimilar product and the reference product are to each other is not necessarily straightforward since how similar is similar enough is not well defined and scientific/clinical judgment may not be easy [17]. When coming to the clinical stage developing biosimilar efficacy, the biosimilarity has been demonstrated in terms of the critical quality attributes, in animals, and PK/PD/immunogenicity, and the left behind is the residual uncertainty of the biosimilarity. Consider a hypothesized trial where the test (the proposed biosimilar product) is also the reference. After the trail, there is usually an observed difference in the measuring efficacy statistic so that the difference is not zero, such as the relative risk is not 1, odds ratio is not 1 and the hazard ratio is not 1. Even though the observed difference is not zero, the reference product is an approved product, and therefore the test (here it is the reference also in this hypothesized trial) should almost always be comparable to the reference (itself). If this hypothesized trial is repeated many times, then a range will be obtained for the statistic measuring efficacy. Thus, the observed range can serve as a constraint and goalpost for judging similarity. Based on all the above considerations and in order to address some of the challenges faced by the use of non-inferiority or equivalence methods, a cNI approach is proposed to address both the clinical efficacy of the biosimilar product and the similarity to the reference product. The cNI approach includes two objectives: 1) T is not inferior to the reference product R; and 2) T and R are comparable in distribution for the clinical endpoint. If these two objectives are achieved, then T and R are claimed to be biosimilar. Note that the proposed cNI approach has an extra constraint for the statistical similarity in addition to the efficacy condition using the statistically powered non-inferiority approach. The condition 1 guarantees that the proposed biosimilar T does not have a decreased activity and the condition 2 guarantees that the proposed biosimilar T does not have an increased activity. With the assumption of a normal distribution for the summary statistic measuring efficacy, only the mean and the variability or range need to be compared and constrained. To show that T and R are comparable in distribution for the clinical endpoint, it must be demonstrated that a) the 95% CI for the treatment effect of T/R is within a plausibility interval (PI) of the treatment effect of R/R; and b) the point estimate for the treatment effect of T/R is within a boundary, say, for example, (0.8, 1.25). The plausibility interval of the treatment effect of R/R can be constructed using the idea that any observed difference between the reference against the reference itself should be considered nonclinical meaningful, and is defined as (−k2σ2R−−−√,+k2σ2R−−−√), where σ2R is the total variability for the reference [Liao, 2013] [18]. An appropriate k-factor along with the mean constrain boundary should be chosen to control the type I error level and the total reference variability should be adjusted for known factors and biases to avoid an inflated variability estimate. The second condition for the point estimate of the treatment effect of T/R within a boundary is to ensure that the T and R do not have too much effect difference due to the large reference variability from a less-controlled trial. Different constraints for the mean and range can lead to different cNIs. This condition will require adequate sample size and a well-controlled biosimilar trial. This should not be a concern since the proposed biosimilar clinical trial should at least satisfy the non-inferiority criteria with certain statistical power. In summary, the proposed cNI approach for claiming biosimilarity depends on two facts. The first is passing the classical NI requirement to ensure the proposed biosimilar product not inferior to the reference product. The second is meeting the constraints that the point estimate of T/R is within the pre-defined mean boundary and the 95% CI of T/R is within the predefined plausibility interval to ensure the proposed biosimilar product not having an increased activity. To compare the performance of the three methods mentioned in previous section in terms of type I error and power, a simulation study was conducted for the following three scenarios where the smaller rate is considered as better: Case 1: P = 0.5, R = 0.3, T = 0.4 Case 2: P = 0.5, R = T = 0.3 Case 3: P = 0.5, R = 0.3, T = 0.2 Case 1 represents that both T & R are better than P but T is worse than R. Case 2 represents that T & R are the same and both are better than P. Case 3 represents that both T & R are better than P but R is worse than T. For the simulation study, the same placebo-controlled historical trial (or meta-analysis result) was used for all three cases with each arm having 300 subjects. Different sample sizes were evaluated for the current biosimilar trial comparing T to R. Cases 1 and 3 are used to evaluate the type I error of the three methods while case 2 is used to evaluate the power of the three methods. For each simulation sample point, both a historical placebo-controlled trial and a current biosimilar trial are simulated, and then a decision on similarity is derived using the simulated data. For each set of parameters, 5000 simulation samples were generated. For comparison, the same f = 0.5, i.e., 50% effect preservation was used for all three approaches. The probability of accepting the "biosimilarity" conclusion is plotted against the sample size per arm used in the current biosimilar trial for each case for using both the fixed and the synthesis approaches. Figure 2 displays the type I error in case 1 for three methods using both the fixed and the synthesis approaches, where the proposed cNI approach used 3 different k values in the PI and two different boundaries for the mean which leads to six different cNIs. As the sample size in the current biosimilar trial increases, the type I error decreases. All cases have the type I error controlled very well. Note that the NI approach is one-sided but the cNI and the equivalence approaches are two-sided. The proposed cNI approach has more options to control the type I error. Figure 2 indicates that both the k- value and the mean boundary have an impact on controlling the type I error. The smaller the k- value, the better controlling the type I error. The tighter the mean boundary, the better the control of type I error. It seems that the mean treatment constraint boundaries in the proposed cNI approach controls the type I error more effectively than the k-value. In general, the synthesis approach gives a little larger type I error than the fixed margin approach for all methods. Figure 3 shows the power in case 2 for three methods using both the fixed and the synthesis approaches, where again the proposed cNI approach had three different k values in the PI and two different mean boundaries. As the sample size in the current biosimilar trial increases, the power increases. In general, the synthesis approach gives better power than the fixed margin approach. As expected, the non-inferiority approach gives the best power and the equivalence method has a much lower power at the same fixed sample size. However, the proposed cNI approach with k = 3 and the boundary 0.8/ 1.25 has a very comparative power comparing to the non-inferiority approach. Note that the cNI approach has an extra similarity condition for the clinical endpoint in addition to the non-inferiority. Thus, proposed cNI approach with certain k-value and the boundaries can perform very well. When the sample size is big enough, the cNI and the non-inferiority methods give similar power. Figure 3 also indicates that if an additional 5% to 10% sample size is added for the cNI approach, the cNI will have the same or more power than the non-inferiority approach. Thus, with an additional 5% to 10% sample size based on the sample size calculation for the non-inferiority design, the proposed cNI design should achieve the same or higher power comparing to the non-inferiority design. Figure 4 displays the type I error in case 3 for three methods using both the fixed and the synthesis approaches, where again the proposed cNI approach had three different k values in the PI and two different mean boundaries. The probability of passing biosimilarity on the left side of the y-axis is for the equivalence and cNI approaches while the value in the right side of the y-axis is for the NI approach. In case 3, the T is better than R so, T & R are not similar. Whether this implies T should not be approved as a biosimilar is a topic for debate. Both the equivalence approach and the proposed cNI approach controlled the type I error well, where different constraints level can lead to different degree of type I error control. The non-inferiority method either doesn't control the type I error or demonstrates better power depending on one's perspective. However, the proposed cNI approach does not have this conflicting controversial issue. In summary, the non-inferiority approach requires smaller sample size and it can address the clinical efficacy of the biosimilar product but can fail the similarity to the reference product, while the equivalence approach can address both the clinical efficacy of the biosimilar product and the similarity to the reference product, but it requires larger sample size. However, the proposed cNI approach with carefully selected constraint can address both the clinical efficacy of the biosimilar product and the similarity to the reference product using the similar sample size required by the non-inferiority approach. In simulation, it seems that the k = 3 for constructing PI and 0.8/1.25 as the boundaries for the point estimate of the treatment effect are good choice for the cNI approach. Note that it is recommended to involve clinician for a thorough discussion to ensure this boundary is clinical meaningful and acceptable after the simulation assessment using the available Consider two examples. The first dataset is the trial dataset from Snapinn and Jiang [2008b] [13]. In this trial, the standard reference treatment was approved based on a set of placebo-controlled trials in which a total of 100 of 1000 placebo subjects with a prespecified unfavorable event and thus, the smaller the "event", the better efficacy of the product, and 74 of 1000 reference product treated subjects with the "event". Suppose a current biosimilar trial is conducted with the same population where a total of 90 of 1200 reference product treated subjects with the "event", and 77 of 1200 proposed biosimilar product treated subjects with the "event". The data are shown as dataset 1 in table 1. The second dataset is a minor modification of the dataset 1. The only difference is a reduction of the total "event" from the proposed biosimilar product treatment from 77 to 65; thus, the proposed biosimilar product is more efficacious. Following Snapinn and Jiang [2008b], the negative log-odds ratio will be used as the analysis variable to evaluate the treatment effect in order to satisfy the criteria that the analysis variable has an approximately normal distribution and that greater values represented better efficacy [13]. Using the data from the historical placebo controlled trial results in BRP and VRP = 0.0213. For evaluation of all three approaches, a 50% effect preservation, f = 0.5, is used. For the proposed cNI approach as an illustration, k = 3 is used for constructing the PI and 0.8/1.25 are used as the boundaries for the point estimate of the odds ratio. For dataset 1, the data from the current biosimilar trial results in BTR=0.168 and VTR=0.026 Using the fixed-margin approach, BTR+(1−f)BRPVTR√+(1−f)2VRP√=1.373<1.96 and BTR−(1−f)BRPVTR√+(1−f)2VRP√= 0.044>−1.96. Thus, both the non-inferiority and the equivalence methods reject the biosimilarity conclusion using the fixed margin approach. Using the synthesis-margin approach, BTR+(1−f)BRPVTR+(1−f) 2VRP√=1.827<1.96 and BTR−(1−f)BRPVTR+(1−f)2VRP√=0.059>−1.96. Thus, both the non-inferiority and the equivalence methods also reject the biosimilarity conclusion using the synthesis margin approach. The odds ratio of the proposed biosimilar product relative to the reference product is 0.846 and the 95% confidence interval of the odds ratio is (0.617, 1.159). Using both the reference data from both the historical placebo-controlled trial and the current biosimilar trial, σ2R=0.0268 is calculated for the log-odds ratio. Thus, the PI for the odds ratio is (0.612, 1.634). Since (0.617, 1.159) is within the PI (0.612, 1.634) and the 0.846 is within 0.8/1.25, therefore, T and R have comparable distributions in terms of the odds ratio. However, the NI approach showed that biosimilar product lack of efficacy for both the fixed margin and the synthesis margin approaches. Thus, the proposed cNI approach would also reject the biosimilar conclusion using both the fixed margin and the synthesis approach. In summary, all three methods have the same conclusion. The results using different methods are summarized in table 2. For dataset 2, only changes were the data for the test biosimilar product in the current biosimilar trial and the data from the current biosimilar trial results in BTR=0.348 and VTR=0.028. Again, a 50% effect preservation, f = 0.5, is used. Using the fixed-margin approach, BTR+(1−f)BRPVTR√+(1−f)2VRP√=2.079>1.96 and BTR−(1−f)BRPVTR√+(1−f)2VRP√=0.783>−1.96. Thus, the non-inferiority method would not reject the biosimilarity conclusion but the equivalence method would reject the biosimilarity conclusion using the fixed margin approach. Using the synthesis-margin approach, BTR+(1−f)BRPVTR+ (1−f)2VRP√=2.752>1.96 and BTR−(1−f)BRPVTR+(1−f)2VRP√=1.037>−1.96. Thus, the synthesis method also yields the same conclusions. The odds ratio of the proposed biosimilar product relative to the reference product is 0.706 and the 95% confidence interval of the odds ratio is (0.508, 0.982). Since the reference data are the same in dataset 2 as in dataset 1, σ2R=0.0268 for the log-odds ratio. Thus, the PI for the odds ratio is (0.612, 1.634). Since the (0.508, 0.982) is outside the PI (0.612, 1.634) and the 0.706 is outside 0.8/1.25, therefore, T and R are not deemed to have comparable distributions in terms of the odds ratio and the proposed cNI approach would reject the biosimilarity conclusion even though the T is certainly efficacious and superior to the placebo from the NI approach for both the fixed margin and the synthesis margin approaches. In summary, the equivalence approach and the cNI methods reject the biosimilarity conclusion while the traditional non-inferiority approach fails to reject the biosimilarity conclusion. The cNI rejects the biosimilarity due to the similarity issue, not due to the efficacy. The results for dataset 2 using different methods are also summarized in table 2. There are tremendous scientific and statistical challenges and opportunities in developing biosimilars. It is a stepwise approach and what we know at current stage determines what to do next. The approvability of biosimilars depends on the totality of evidence with the use of fingerprint-like techniques for extensive characterization. Direct head to head comparison between the biosimilars and the reference begins with the in vivo and in vitro critical quality attributes, and ends with the clinical efficacy comparison to assess the residual uncertainty of the biosimilarity. In this paper, a cNI approach was proposed and compared with non-inferiority and equivalence approaches. An equivalence approach usually requires much larger sample size to achieve the same power as the non-inferiority approach, but the non-inferiority approach only guarantees that the test product is not inferior to the reference product and thus, may pass a product with increased activity compared to the reference product. However, the cNI approach, which addresses both the clinical efficacy of the biosimilar product and the similarity to the reference product, was shown in the simulation study to have better performance than the equivalence approach in terms of the power, while maintaining type I error. Predictably, the approach has somewhat less power than the straight non-inferiority approach as some evidence supporting that the test is not appreciably more efficacious than the reference is required. The cNI approach uses the traditional non-inferiority plus a plausibility interval and a point estimate criteria, where the extra requirements and the constraints serve as the supporting evidence that the test is not appreciably more efficacious than the reference. All these factors could be predetermined with the consensus from the health authorities. The information from comparing the reference against the reference itself is used as the goalpost to set up the biosimilarity plausibility interval. To achieve this, the information from current biosimilar trial may be borrowed through the interim analysis or after the trial finalization. Since many parameters are involved in the proposed cNI approach, a more conservative conclusion can be achieved if needed. The type I error can be easily controlled through different combinations of parameters, and it seems that the k = 3 for constructing the PI and 0.8/1.25 as the boundaries for the point estimate of the treatment effect are good choices for the cNI approach. However, it is recommended that simulations be performed for every trial to justify the type I error control. An additional 5% to 10% sample size than that based on the sample size calculation for a straightforward non-inferiority design is recommended so that the proposed cNI design and analysis would achieve the similar power. The proposed cNI approach had good performance in the simulation study and the examples in terms of power and type I error control and suggests promise for this approach. The information from comparing the reference to the reference itself can be used as the goalposts for setting the acceptance criteria. In summary, the proposed cNI approach generally requires smaller sample sizes than that from an equivalence approach but meets all necessary requirements addressing biosimilarity for efficacy trials, which can make the difference in development costs that determine the economic viability of biosimilar projects. The thoughts and opinions presented in this paper only represent the author's positions. The author thanks the editor and a referee for their valuable comments that improved the presentation of this paper. The author also wants to thank the former and current colleagues for many discussions and suggestions on different versions of this paper.
{"url":"https://clinmedjournals.org/articles/ijcbb/international-journal-of-clinical-biostatistics-and-biometrics-ijcbb-1-008.xml","timestamp":"2024-11-11T21:44:00Z","content_type":"application/xml","content_length":"44796","record_id":"<urn:uuid:e4fb452f-664e-4e63-ab9b-f07089e4b79f>","cc-path":"CC-MAIN-2024-46/segments/1730477028239.20/warc/CC-MAIN-20241111190758-20241111220758-00809.warc.gz"}
SVM (support vector machine) for classification Support Vector Machine (SVM) is a classification algorithm that assigns data to one category or the other based on the training data. Support Vector Machine (SVM) is a classification algorithm that assigns data to one category or the other based on the training data. This algorithm implements linear SVM, which is highly scalable. You can use the following functions to train the SVM model, and use the model to make predictions on a set of test data: You can also use the following evaluation functions to gain further insights: For a complete example of how to use the SVM algorithm in Vertica, see Classifying data using SVM (support vector machine). The implementation of the SVM algorithm in Vertica is based on the paper Distributed Newton Methods for Regularized Logistic Regression. In this section
{"url":"https://docs.vertica.com/24.2.x/en/data-analysis/ml-predictive-analytics/classification-algorithms/svm-support-vector-machine-classification/","timestamp":"2024-11-14T22:23:17Z","content_type":"text/html","content_length":"46891","record_id":"<urn:uuid:c74978a2-eeee-4e59-82ed-4e3dba54db5c>","cc-path":"CC-MAIN-2024-46/segments/1730477395538.95/warc/CC-MAIN-20241114194152-20241114224152-00051.warc.gz"}
The One-Way Communication Complexity of Submodular Maximization with Applications to Streaming and Robustness We consider the classical problem of maximizing a monotone submodular function subject to a cardinality constraint, which, due to its numerous applications, has recently been studied in various computational models. We consider a clean multi-player model that lies between the offline and streaming model, and study it under the aspect of one-way communication complexity. Our model captures the streaming setting (by considering a large number of players), and, in addition, two player approximation results for it translate into the robust setting. We present tight one-way communication complexity results for our model, which, due to the above-mentioned connections, have multiple implications in the data stream and robust setting. Even for just two players, a prior information-theoretic hardness result implies that no approximation factor above 1/2 can be achieved in our model, if only queries to feasible sets, i.e., sets respecting the cardinality constraint, are allowed. We show that the possibility of querying infeasible sets can actually be exploited to beat this bound, by presenting a tight 2/3-approximation taking exponential time, and an efficient 0.514-approximation. To the best of our knowledge, this is the first example where querying a submodular function on infeasible sets leads to provably better results. Through the above-mentioned link to the robust setting, both of these algorithms improve on the current state-of-the-art for robust submodular maximization, showing that approximation factors beyond 1/2 are possible. Moreover, exploiting the link of our model to streaming, we settle the approximability for streaming algorithms by presenting a tight 1/2 + epsilon hardness result, based on the construction of a new family of coverage functions. This improves on a prior 1 - 1/e + epsilon hardness and matches, up to an arbitrarily small margin, the best known approximation algorithm.
{"url":"https://graphsearch.epfl.ch/en/publication/284405","timestamp":"2024-11-12T09:36:20Z","content_type":"text/html","content_length":"111106","record_id":"<urn:uuid:b1a6110a-5583-4f0b-a730-b217088fefb0>","cc-path":"CC-MAIN-2024-46/segments/1730477028249.89/warc/CC-MAIN-20241112081532-20241112111532-00099.warc.gz"}
Linear image Linear image --- Introduction --- Linear image is an exercise on maps between vector spaces or affine spaces. Other exercises on: This page is not in its usual appearance because WIMS is unable to recognize your web browser. Please take note that WIMS pages are interactively generated; they are not ordinary HTML files. They must be used interactively ONLINE. It is useless for you to gather them through a robot program. • Description: compute the image of a vector by a linear or affine map. This is the main site of WIMS (WWW Interactive Multipurpose Server): interactive exercises, online calculators and plotters, mathematical recreation and games • Keywords: wims, mathematics, mathematical, math, maths, interactive mathematics, interactive math, interactive maths, mathematic, online, calculator, graphing, exercise, exercice, puzzle, calculus, K-12, algebra, mathématique, interactive, interactive mathematics, interactive mathematical, interactive math, interactive maths, mathematical education, enseignement mathématique, mathematics teaching, teaching mathematics, algebra, geometry, calculus, function, curve, surface, graphing, virtual class, virtual classes, virtual classroom, virtual classrooms, interactive documents, interactive document, linear_algebra, affine_geometry, vector_space, basis, vectors, image, linear_maps, preimage, affine_function
{"url":"https://wims.univ-cotedazur.fr/wims/en_U1~algebra~linimg.en.html","timestamp":"2024-11-03T20:21:38Z","content_type":"text/html","content_length":"9704","record_id":"<urn:uuid:90704cb2-2507-4a7d-b676-10c0e98744a6>","cc-path":"CC-MAIN-2024-46/segments/1730477027782.40/warc/CC-MAIN-20241103181023-20241103211023-00228.warc.gz"}
Calculation of the Combined Electric Field Response of Multiple Nonlinear Antenna Loads due to HPEM Excitation Articles | Volume 20 © Author(s) 2023. This work is distributed under the Creative Commons Attribution 4.0 License. Calculation of the Combined Electric Field Response of Multiple Nonlinear Antenna Loads due to HPEM Excitation The electromagnetic properties of receiving and scattering structures with nonlinear components are difficult to predict, especially if more than one nonlinear load is involved. In this contribution a frame antenna with two diodes that act as nonlinear loads is analyzed. This receiving antenna structure is illuminated by a polarized plane wave, carrying a transient HPEM signal. It is then the given task to compute the electric field distribution in the vicinity of the antenna. To this end, a macromodel of the nonlinearly loaded structure that relates the transient signal to the electric field measured at an observation point is derived. As a result it is seen how recent macromodeling techniques can be applied to solve the given problem step by step. This provides the possibility to further analyze the interaction of the given nonlinear loads by a general framework. Received: 10 Feb 2022 – Revised: 09 Sep 2022 – Accepted: 12 Sep 2022 – Published: 21 Mar 2023 The study of nonlinearly loaded receiving structures, such as antennas or transmission lines, has a rather long history in Electromagnetic Compatibility. Many of the techniques used to analyze such structures are based on one of the two following concepts: The first one is the harmonic balance technique which is capable to deal with weak and strong nonlinear circuits (Maas, 1988). This technique is efficient for single tone excitations but less suitable if broadband HPEM excitations are considered. The second concept is based on power-series and Volterra-series analysis (Sarkar and Weiner, 1976; Maas, 1988). Volterra-series presupposes that the excitation signal has no direct component. This usually is the case for far field related disturbances. Another detail that usually is presupposed in power-series based techniques is that nonlinearities are weak (Maas, 1988). In a recent study, the response of nonlinearly loaded loop antennas under HPEM excitation has been investigated where the nonlinearity was given by a single diode. This led to the observation of a rectifying effect of the diode which, in turn, led to a rather long lasting direct component which was attributed to an electric energy storage effect (Michels et al., 2020). In the corresponding modeling and analysis it could be assumed that the diode is of small spatial extent and can be treated as a lumped element. In this case it is a standard procedure to resort to circuit analysis and to isolate the nonlinear element while the remaining linear part of the circuit model is reduced to a Thévenin or Norton equivalent, where the impedance of the linear circuit model often is expressed by Foster representations (Ramo et al., 1994). In this way, the resulting nonlinear circuit model can be implemented in usual circuit simulators. Also it is possible to gain from the circuit models physical insights which help to understand observed effects such as the above mentioned long lasting direct component. If the complexity of the nonlinearly loaded structures increases by the inclusion of more than one nonlinear element, the procedure described in the above paragraph becomes more and more impractical. In this case, advanced methods of macromodeling turn out to be much more efficient (Grivet-Talocia and Gustavsen, 2016a), allowing the inclusion of hundreds or even thousands of nonlinear elements. In this spirit, in Yang et al. (2016) diode grids for energy selective shielding have been considered and analyzed with a hybrid field-circuit simulation method that combines methods of moments and a causal convolution technique which allows to ensure passivity and causality. In Yang et al. (2021) and Wendt et al. (2019), macromodeling techniques combined with recursive convolution ( Grivet-Talocia and Gustavsen, 2016b) have been used to allow to considerably reduce the time required to perform simulations on structures with a large number of nonlinear loads. Moreover, these techniques lead to causal results and enforce passivity as well. Having these advantages of macromodeling in mind, it is the aim of this study to further analyze the mentioned energy storage effect in the presence of more than one diode. In particular, a wire frame antenna including two diodes as nonlinear loads will be considered. The scattering behavior of this structure will be analyzed using the macromodeling and convolution techniques cited above. This allows to divide the problem in a way such that the interaction between the two nonlinear loads can be observed and understood. The remainder of this article is organized as follows: First, a frame antenna that includes two diodes as nonlinear loads and is excited by a plane wave carrying a transient excitation is introduced in Sect. 2. Results from full wave simulations in the time domain that characterize the scattered field will be presented. In Sect. 3 a macromodel of the considered setup will be derived. This macromodel will be used in Sect. 4 to examine the scattered field that has been numerically calculated in Sect. 2. Finally, Sect. 5 provides a conclusion. 2Full wave simulation of a receiving structure with multiple nonlinear loads 2.1Receiving structure with two nonlinear loads excited by a transient HPEM excitation The receiving structure introduced in Fig. 1 is given by a wire frame including two diodes that act as nonlinear loads. The frame is made of perfectly conducting cylindrical wires of cross-section 1 mm. The dimensions of the wire frame are set to 100mm×100mm. The incident wave with electric field E[inc], magnetic field H[inc], and Poynting vector S[inc] carries a transient signal. The polarization of the incident wave is chosen such that the electric field points in y-direction. Electric field components are denoted by E[xobs], E[yobs], and E[zobs], recorded by a virtual field probe located at the center of the frame antenna. Simulations of this setup are first performed in the time-domain full wave simulation tool CST Microwave Studio (CST, 2022). The equivalent circuit of the diodes used in the simulations is depicted in Fig. 2. The diode parameters are given in Table 1. These parameters resemble those of a Schottky diode with current voltage relation: $\begin{array}{}\text{(1)}& \begin{array}{ccc}\text{for}& {V}_{d}<\mathrm{0}& {I}_{d}={G}_{s}\cdot {V}_{d}\\ & & \\ \text{for}& {V}_{d}\ge \mathrm{0}& \phantom{\rule{2em}{0ex}}\phantom{\rule{2em} {0ex}}{I}_{d}={I}_{\mathrm{0}}\cdot \left({e}^{\frac{e}{kT}\cdot {V}_{d}}-\mathrm{1}\right)\end{array}\end{array}$ with the Boltzmann constant k and the elementary charge e. 2.2Excitation pulse As transient HPEM excitation signal, a double exponential pulse is used. It is carried by the incident plane wave and mathematically described by $\begin{array}{}\text{(2)}& E\left(t\right)={E}_{\mathrm{0}}\cdot K\cdot \left({e}^{-\mathit{\alpha }\left(t-\mathrm{\Delta }t\right)}-{e}^{-\mathit{\beta }\left(t-\mathrm{\Delta }t\right)}\right)\ cdot \mathrm{\Theta }\left(t-\mathrm{\Delta }t\right).\end{array}$ In order to have E[0] as peak value, the correction factor K is introduced with $\begin{array}{}\text{(3)}& K={\left({e}^{-\mathit{\alpha }\frac{\text{ln}\mathit{\alpha }-\text{ln}\mathit{\beta }}{\mathit{\alpha }-\mathit{\beta }}}-{e}^{-\mathit{\beta }\frac{\text{ln}\mathit{\ alpha }-\text{ln}\mathit{\beta }}{\mathit{\alpha }-\mathit{\beta }}}\right)}^{-\mathrm{1}}.\end{array}$ Additionally, the Heaviside function Θ(t) $\begin{array}{}\text{(4)}& \mathrm{\Theta }\left(t\right)=\mathit{\left\{}\begin{array}{ccc}\mathrm{0}& \text{for}& t<\mathrm{0}\\ \mathrm{1}& \text{for}& t\ge \mathrm{0}\end{array}\end{array}$ is included to define E(t) for all times t. A time delay of Δt=2.5ns is chosen. The values of α and β are calculated from rise and fall times according to: $\begin{array}{}\text{(5)}& \mathit{\alpha }=\frac{\mathrm{1}}{{t}_{f}}\phantom{\rule{0.125em}{0ex}},\phantom{\rule{1em}{0ex}}\mathit{\beta }=\frac{\mathrm{1}}{{t}_{r}}\phantom{\rule{0.125em}{0ex}},\ 2.3Full wave simulation results of the scattered electric field A full wave simulation is performed with CST Microwave Studio and the electric field measured by the virtual field probe of Fig. 1 is sampled. This leads to the results displayed in Fig. 3. The excitation introduced in Sect. 2.2 is depicted in the figure in blue. Moreover the electric fields in x-direction E[xobs] and y-direction E[yobs] are displayed. It turned out that the electric field at the observation point has no significant component in z-direction. A first observation is that the electric field vector at the observation point includes an x-component that grows rapidly during the passing of the transient excitation and remains approximately constant. The occurence of approximate DC-components caused by the rectifying effect of diodes has already been studied in Michels et al. (2020) for the case of one diode. The novelty in the present simulation is in the time dependency of the field component as it behaves approximately like a Heaviside function without noticeable oscillations. This becomes more clear if this result is compared to the case with a single diode only: In this case, Diode1 in Fig. 1 is replaced by a 40MΩ resistance which corresponds to the reverse resistance of the diode. The same simulation is repeated leading to the result shown in Fig. 4. It seems as if in the case of two diodes there is a counteracting effect which leads to a cancellation of initially present oscillation. In the following it will be shown how macromodeling techniques can contribute to calculate and understand this cancellation. 3Macromodel of the simulation setup 3.1Basic approach For the following approach it is an essential idea to replace both diodes by equivalent ideal voltage sources. This is in accordance to the substitution theorem (Maas, 1988). As depicted in Fig. 5, the nonlinearly loaded structure (a) can be decomposed into three separate wire frames including two ideal voltage sources V[1] and V[2]. These wire frames will separately be excited. Since no nonlinear elements are involved in this constellation, the superposition principle can be used to calculate the field strength at the observation point. The field contributions of the three wire frames (b), (c), and (d) are treated separately. The first contribution is caused by the incident field with both voltage sources V[1] and V[2] set to zero, leading to configuration (b). The other two scattered components are computed by setting alternately one of the voltage sources to zero, leading to the configurations (c) and (d). The field contributions can thus be split into separate parts in order to study their interaction. Before the superposition can be performed, the ideal voltage sources V[1](t) and V[2](t) corresponding to the voltage drops across the diodes have to be determined. 3.2Computation of the voltage drops across the diode It is the aim to create a macromodel of the nonlinearly loaded structure capable to relate the voltage drops V[1](t) and V[2](t) across the diodes to any excitation signal carried by an incident wave. To do so, the first task is to create a netlist for an electric circuit simulator of the linear part of the structure. For this, the diodes are removed and replaced by ports in a full wave simulation. The scattering parameters of the resulting two-port are computed and provide a set of discrete S-parameters $\begin{array}{}\text{(6)}& \left(\begin{array}{cc}{S}_{\mathrm{11}}\left(j{\mathit{\omega }}_{k}\right)& {S}_{\mathrm{12}}\left(j{\mathit{\omega }}_{k}\right)\\ & \\ {S}_{\mathrm{21}}\left(j{\mathit {\omega }}_{k}\right)& {S}_{\mathrm{22}}\left(j{\mathit{\omega }}_{k}\right)\end{array}\right).\end{array}$ The continuous functions S[ik](s) with $i,k=\mathrm{1},\mathrm{2}$ are given in the Laplace domain and approximated by a vector fitting algorithm as rational functions in their pole-residue form. For this step a check and enforcement of passivity is mandatory. From the resulting rational functions an equivalent circuit of the structure can be derived with methods explained in Grivet-Talocia and Gustavsen (2016a) and represented by a SPICE netlist. The nonlinear loads are now added in the netlist of an electric circuit simulation as depicted in Fig. 6. We assume that the equivalent circuit represented by the SPICE netlist and the diodes does not contain any electrical or magnetic energy at the initial state. The task to transfer diode parameters from full wave simulation, as introduced in Table 1, for example, into a SPICE netlist or vice versa may not be trivial. Corresponding approaches can be inferred from Chang (1996). In order to perform the required circuit simulation, the open circuit voltages V[OCV1] and V[OCV2] have to be computed as well. A model is needed to relate the signal carried by the plane wave to the open circuit voltages. To numerically obtain a suitable model, a time domain full wave simulation of the setup depicted in Fig. 7 is performed with a broadband signal u(t) which leads to the open circuit voltages y[1](t) and y[2](t). Then a Fourier transformation is applied to these signals, leading to the transforms U(s), Y[1](s) and Y[2](s). These are given in sampled form such that discrete values of the transfer functions are obtained, $\begin{array}{}\text{(7)}& {H}_{n}\left({s}_{k}\right)=\frac{{Y}_{n}\left({s}_{k}\right)}{U\left({s}_{k}\right)}\phantom{\rule{0.125em}{0ex}},\phantom{\rule{1em}{0ex}}n=\mathrm{1},\mathrm{2}\phantom {\rule{0.125em}{0ex}},\phantom{\rule{1em}{0ex}}{s}_{k}=j{\mathit{\omega }}_{k}.\end{array}$ With known discrete values of the transfer function, a vector fitting algorithm is applied to provide a continuous transfer function with residues r[k] and poles p[k]. In addition, the propagation time of the plane wave is taken into account by a parameter τ. In this case, only a forward evaluation is required. Therefore a check or enforcement of passivity is not necessary (Wendt et al., 2020) and the desired transfer functions are of the form $\begin{array}{}\text{(8)}& H\left(s\right)=\left({r}_{\mathrm{0}}+\sum _{k=\mathrm{1}}^{m}\frac{{r}_{k}}{s-{p}_{k}}\right)\cdot {e}^{-s\mathit{\tau }}.\end{array}$ Having the transfer functions H[1](s) and H[2](s) at hand, the open circuit voltages can be calculated for any signal u(t) carried by the incident plane wave via fast recursive convolution with the inverse Fourier transformation of the transfer function h(t) as kernel, $\begin{array}{}\text{(9)}& {V}_{\mathrm{OCVn}}\left(t\right)=u\left(t\right){h}_{n}\left(t\right)\phantom{\rule{1em}{0ex}}n=\mathrm{1},\mathrm{2}.\end{array}$ In order to ensure causality of the system the following has to apply for the impulse responses h(t): $\begin{array}{}\text{(10)}& {h}_{n}\left(t\right)=\mathrm{0}\phantom{\rule{0.125em}{0ex}},\phantom{\rule{1em}{0ex}}n=\mathrm{1},\mathrm{2}\phantom{\rule{0.125em}{0ex}},\phantom{\rule{1em}{0ex}}\text {for}\phantom{\rule{1em}{0ex}}t\le \mathrm{0}.\end{array}$ The fast recursive convolution method turns out to be very efficient for the case when u(t) is given as input function in time domain and the transfer functions H[n](s) are given in frequency domain in their pole residue form Eq. (8). Further details on the recursive convolution method are provided in Grivet-Talocia and Gustavsen (2016a). After having determined the open circuit voltages, the electric circuit simulation of the model depicted in Fig. 6 can be carried out. This yields the desired voltage drops V[1](t) and V[2](t) across the diodes. 3.3Computation of the electric field at the observation point The resulting electric field at the observation point is calculated from the decomposition depicted in Fig. 5. In analogy to the methods described in the previous Sect. 3.2, transfer functions are derived for the computation of the electric field at the observation point, caused by the incident field and the voltages V[1](t) and V[2](t). In total, for the calculation of the observed electric field the following transfer functions are required: • Two transfer functions that relate the signal carried by the incoming plane wave to the field components E[xobs] and E[yobs] with ${V}_{\mathrm{1}}\left(t\right)={V}_{\mathrm{2}}\left(t\right)=\ • Two transfer functions that relate the voltage V[1](t) to the field components E[xobs] and E[yobs] with V[2](t)=0 • Two transfer functions that relate the voltage V[2](t) to the field components E[xobs] and E[yobs] with V[1](t)=0 The fast recursive convolution method is used again to compute the electric field components of E[obs]. Finally, the field at the observation point is calculated by summing up the field contributions of the configurations (b), (c), and (d) as shown in Fig. 5. As a result, the macromodel of the structure introduced in Fig. 1 is now complete and it is possible to compute the electric field at the observation point for any type of excitation signal which is carried by the incident electric field. This also allows to provide a better understanding regarding the interaction of the nonlinear loads, as will be shown in the following. 4Comparison between full wave and macromodel simulation results In order to verify the efficiency of the macromodel, simulation results obtained from both full wave simulations and macromodel simulations are compared. The structure introduced in Sect. 2.1 serves as test setup. Again, it is excited by a plane wave as depicted in Fig. 1 and carries the signal described in Sect. 2.2. The simulation results are displayed in Figs. 8 and 9. The field component in y-direction, compare also Fig. 1, is reproduced by both the macromodel and the full wave simulations and both methods provide a very good agreement. In view of the different field contributions shown in Fig. 5 this field component is due to configuration (b). The comparison concerning the field in x-direction shows a satisfying correlation. Deviations are due to the fact that it is difficult to incorporate the diode parameters of Table 1 into an adequate SPICE netlist. Nevertheless, the pronounced DC-component component is reproduced and there is a good match for early times. It turns out that the x-component results from contributions of the configurations (c) and (d). One question that was raised during the beginning of this investigation was for the reason of the absence of pronounced oscillations in the early time response for the case of two diodes in the antenna. This behavior can now be understood by considering the separate field contributions resulting from the configurations (c) and (d). In Fig. 10 it is revealed that the corresponding DC-components add up while the AC-components cancel out. The sum of both components lead to the time dependency that also is obtained from the full wave simulation. The particular example of a multiple nonlinearly loaded receiving structure showed that macromodeling techniques are an efficient analytical tool. The major advantage is that full wave simulations can be replaced by electric circuit simulations which are much faster and less memory consuming. It is possible to split a complex problem into several parts. Moreover, components with nonlinear characteristics are quite easy to implement in electric circuit simulations. The task to translate data from datasheets of given components correctly into lumped element parameters capable to be used in full wave simulation is not immediate. As already discovered in former studies, nonlinear loads can lead to DC-components which may persist for a comparatively long time. The interaction of multiple nonlinear loads leads to further effects that a priori are difficult to predict. Methods to investigate these effects in depth are therefore a useful instrument with potential applications to EMC. The data presented in this article are available from the authors upon request. The computer simulations and data processing including all graphical representations were carried out by RM. Editorial hints as well as mathematical and physical suggestions were given by MS, SF, and At least one of the (co-)authors is a member of the editorial board of Advances in Radio Science. The peer-review process was guided by an independent editor, and the authors also have no other competing interests to declare Publisher’s note: Copernicus Publications remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This article is part of the special issue “Kleinheubacher Berichte 2021”. The authors are indebted to Cheng Yang and Christian Schuster for kindly introducing them to advanced macromodeling techniques. This research work was partially funded by the Bundeswehr Research Institute for Protective Technologies and CBRN Protection, Munster (grant no. E/E590/JZ001/HF063). This paper was edited by Alexander Kraus and reviewed by Heyno Garbe and one anonymous referee. Chang, F.-Y.: Transient Analysis of Diode Switching Circuits Including Charge Storage Effect, IEEE T. Circuits-I, 43, 177–190, https://doi.org/10.1109/81.486442, 1996.a CST: CST Studio Suite 2017, Dassault Systems, http://www.3ds.com, last access: 7 February 2022.a, b Grivet-Talocia, S. and Gustavsen, B.: Passive Macromodeling Theory and Applications, 1st edn., edited by: Chang, K., Wiley, ISBN 978-1-118-09491-4, 2016a.a, b, c Grivet-Talocia, S. and Gustavsen, B.: Black-box Macromodeling and its EMC Application, IEEE Electromag. Compat. Mag., 5, 71–78, https://doi.org/10.1109/MEMC.0.7764255, 2016b.a Maas, S. A.: Nonlinear Microwave Circuits, 1st edn., Artech House, ISBN 0-89006-251-X, 1988.a, b, c, d Michels, R., Kreitlow, M., Bausen, A., Dietrich, C., and Gronwald, F.: Modeling and Verification of a Parasitic Nonlinear Energy Storage Effect Due To High-Power Electromagnetic Excitation, IEEE. Trans. Electromag. Compat., 62, 2468–2475, https://doi.org/10.1109/TEMC.2020.2980976, 2020.a, b Ramo, S., Whinnery, J. R., and Van Duzer, T.: Fields and Waves in Communication Electronics, 3rd edn., Wiley, New York, ISBN 978-81-265-1525-7, 1994.a Sarkar, T. K. and Weiner, D.: Scattering Analysis of Nonlinearly Loaded Antennas, IEEE Trans. Antennas Porpagat., 24, 125–131, https://doi.org/10.1109/TAP.1976.1141327, 1976. a Wendt, T., Yang, C., Schuster, C., and Grivet Talocia, S.: Numerical Complexity Study of Solving Hybrid Multiport Field-Circuit Problems for Diode Grids, Int. Conf. Electromagn. Adv. App. (ICEAA), Granada, Spain, 9–13 September 2019, https://doi.org/10.1109/ICEAA.2019.8879021, 2019.a Wendt, T., Yang, C., Brüns, H. D., Grivet-Talocia, S., and Schuster, C.: A Macromodeling-Based Hybrid Method for the Computation of Transient Electromagnetic Fields Scatterd by Nonlinearly Loaded Metal Structures, IEEE Trans. Electromagn. Compat., 62, 1098–1110, https://doi.org/10.1109/TEMC.2020.2991455, 2020.a Yang, C., Brüns, H.-D., Liu, P., and Schuster, C.: Impulse Response Optimization of a Band-Limited Frequency Data for Hybrid Field-Circuit Simulation of Large-Scale Energy-Selective Diode Grids, IEEE Trans. Electromag. Compat., 58, 1072–1080, https://doi.org/10.1109/TEMC.2016.2540921, 2016.a Yang, C., Wendt, T., De Stefano, M., Kopf, M., Becker, C. M., Grivet-Talocia, S., and Schuster, C.: Analysis and Optimization of Nonlinear Diode Grids for Shielding of Enclosures with Apertures, IEEE Trans. Electromag. Compat., 63, 1884–1895, https://doi.org/10.1109/TEMC.2021.3073106, 2021.a
{"url":"https://ars.copernicus.org/articles/20/47/2023/","timestamp":"2024-11-09T22:18:40Z","content_type":"text/html","content_length":"214105","record_id":"<urn:uuid:317eb109-2e11-4d91-a49f-2ef440ff61a8>","cc-path":"CC-MAIN-2024-46/segments/1730477028164.10/warc/CC-MAIN-20241109214337-20241110004337-00567.warc.gz"}
Two Hundred Years after Hamilton: Exploring New Formulations of Classical and Quantum Mechanics This talk has three parts. The first part is an introduction to Hamilton’s two monumental papers from 1834-1835, which introduced the Hamilton-Jacobi equation, Hamilton’s equations of motion and the principle of least action [1]. These three formulations of classical mechanics became the three forerunners of quantum mechanics; but ironically none of them is what Hamilton was looking for -- he was looking for a “magical” function, the principal function 𝑆(𝑞[1],𝑞[2],𝑡) from which the entire trajectory history can be obtained just by differentiation (no integration) [2]. In the second part of the talk I argue that Hamilton’s principal function is almost certainly more magical than even Hamilton realized. Astonishingly, all of the above formulations of classical mechanics can be derived just from assuming that 𝑆(𝑞[1],𝑞[2],𝑡) is additive, with no input of physics [3]. The third part of the talk will present a new formulation of quantum mechanics in which the Hamilton-Jacobi equation is extended to complex-valued trajectories [4], allowing the treatment of classically allowed processes, classically forbidden process and arbitrary time-dependent external fields within a single, coherent framework. The approach is illustrated for barrier tunneling, wavepacket revivals, nonadiabatic dynamics, optical excitation using shaped laser pulses and high harmonic generation with strong field attosecond pulses [5]. 1. W. R. Hamilton, On a General Method in Dynamics, Philosophical Transactions, Part 2, p. 247 (1834); ibid., Second Essay on a General Method in Dynamics, Part 1, p. 95 (1835). 2. M. Nakane and C. G. Fraser, The Early History of Hamilton-Jacobi Dynamics 1834-1837, Centaurus 44, 161 (2002); C. Lanczos, The Variational Principles of Mechanics (Oxford, 1949) 3. D. J. Tannor, New derivation of Hamilton’s three formulations of classical mechanics (preprint); ibid, Duality of the Principle of Least Action: A New Formulation of Classical Mechanics, arXiv:2109.09094 (2021). 4. Y. Goldfarb, I. Degani and D. J. Tannor, Bohmian mechanics with complex action: A new trajectory based formulation of quantum mechanics, J. Chem. Phys. 125, 231103 (2006); J. Schiff, Y. Goldfarb and D. J. Tannor, Path integral derivations of complex trajectory methods, Phys. Rev. A 83, 012104 (2011); N. Zamstein and D. J. Tannor, Overcoming the root search problem in complex quantum trajectory calculations, J. Chem. Phys. 140, 041105(2014). 5. N. Zamstein and D. J. Tannor, Non-adiabatic molecular dynamics with complex quantum trajectories. I. The adiabatic representation, J. Chem. Phys. 137, 22A518 (2012); W. Koch and D. J. Tannor, Wavepacket revivals via complex trajectory propagation, Chem. Phys. Lett. 683, 306 (2017); W. Koch and D. J. Tannor, A three-step model of high harmonic generation using complex classical trajectories, Annals of Physics, 427, 168288 (2021). Host: Paul Brumer
{"url":"https://www.physics.utoronto.ca/research/quantum-optics/cqiqc-seminars/cqiqc-seminar-on-31-october-2024/","timestamp":"2024-11-05T12:48:20Z","content_type":"text/html","content_length":"31393","record_id":"<urn:uuid:8d49d9ed-b7ac-4d41-b8cd-d76d4d8e91ac>","cc-path":"CC-MAIN-2024-46/segments/1730477027881.88/warc/CC-MAIN-20241105114407-20241105144407-00056.warc.gz"}
Boring p24 Boring p24 (or trans-pulsar on figure eight) is a period-24 oscillator composed of a pulsar and a figure eight. Despite being composed of two oscillators of smaller periods (p3 and p8 respectively), it is considered non-trivial because it has two cells that are alive in one generation and dead in the other 23, and two more otherwise period-8 cells that are alive in one additional generation. Main article: List of common oscillators On Catagolue, it is the most common period 24 oscillator, being more common than the similar uninteresting p24.^[1] The boring p24 first appeared naturally on August 27, 2015, in a soup found by Brett Berger.^[2] Before this, symmetric figure-eight-on-pulsar variants had appeared only semi-naturally.^[3] See also 1. ↑ Adam P. Goucher. "Statistics". Catagolue. Retrieved on October 27, 2018. 2. ↑ Ivan Fomichev (August 27, 2015). Re: Soup search results (discussion thread) at the ConwayLife.com forums 3. ↑ Richard Schank (December 20, 2014). Re: Soup search results (discussion thread) at the ConwayLife.com forums External links
{"url":"https://conwaylife.com/wiki/Boring_p24","timestamp":"2024-11-05T10:14:31Z","content_type":"text/html","content_length":"31806","record_id":"<urn:uuid:831a5ba8-2e47-4e61-820e-1930b30f06cb>","cc-path":"CC-MAIN-2024-46/segments/1730477027878.78/warc/CC-MAIN-20241105083140-20241105113140-00484.warc.gz"}
The Stacks project Lemma 67.5.3. Let $S$ be a scheme. Let $f : X \to Y$ be a morphism of algebraic spaces over $S$. The following are equivalent: 1. $f$ is surjective, 2. for every scheme $Z$ and any morphism $Z \to Y$ the morphism $Z \times _ Y X \to Z$ is surjective, 3. for every affine scheme $Z$ and any morphism $Z \to Y$ the morphism $Z \times _ Y X \to Z$ is surjective, 4. there exists a scheme $V$ and a surjective étale morphism $V \to Y$ such that $V \times _ Y X \to V$ is a surjective morphism, 5. there exists a scheme $U$ and a surjective étale morphism $\varphi : U \to X$ such that the composition $f \circ \varphi $ is surjective, 6. there exists a commutative diagram \[ \xymatrix{ U \ar[d] \ar[r] & V \ar[d] \\ X \ar[r] & Y } \] where $U$, $V$ are schemes and the vertical arrows are surjective étale such that the top horizontal arrow is surjective, and 7. there exists a Zariski covering $Y = \bigcup Y_ i$ such that each of the morphisms $f^{-1}(Y_ i) \to Y_ i$ is surjective. Comments (0) Post a comment Your email address will not be published. Required fields are marked. In your comment you can use Markdown and LaTeX style mathematics (enclose it like $\pi$). A preview option is available if you wish to see how it works out (just click on the eye in the toolbar). All contributions are licensed under the GNU Free Documentation License. In order to prevent bots from posting comments, we would like you to prove that you are human. You can do this by filling in the name of the current tag in the following input field. As a reminder, this is tag 03MF. Beware of the difference between the letter 'O' and the digit '0'. The tag you filled in for the captcha is wrong. You need to write 03MF, in case you are confused.
{"url":"https://stacks.math.columbia.edu/tag/03MF","timestamp":"2024-11-11T22:57:10Z","content_type":"text/html","content_length":"14993","record_id":"<urn:uuid:d74cbf92-05ed-4ae3-938b-82b36d4c5c74>","cc-path":"CC-MAIN-2024-46/segments/1730477028240.82/warc/CC-MAIN-20241111222353-20241112012353-00191.warc.gz"}
17.4 Normal Modes of a Standing Sound Wave Learning Objectives By the end of this section, you will be able to: • Explain the mechanism behind sound-reducing headphones • Describe resonance in a tube closed at one end and open at the other end • Describe resonance in a tube open at both ends Interference is the hallmark of waves, all of which exhibit constructive and destructive interference exactly analogous to that seen for water waves. In fact, one way to prove something “is a wave” is to observe interference effects. Since sound is a wave, we expect it to exhibit interference. Interference of Sound Waves In Waves, we discussed the interference of wave functions that differ only in a phase shift. We found that the wave function resulting from the superposition of [latex]{y}_{1}(x,t)=A\,\text{sin}(kx-\ omega t+\varphi )[/latex] and [latex]{y}_{2}(x,t)=A\,\text{sin}(kx-\omega t)[/latex] is [latex]y(x,t)=[2A\,\text{cos}(\frac{\varphi }{2})]\text{sin}(kx-\omega t+\frac{\varphi }{2}).[/latex] One way for two identical waves that are initially in phase to become out of phase with one another is to have the waves travel different distances; that is, they have different path lengths. Sound waves provide an excellent example of a phase shift due to a path difference. As we have discussed, sound waves can basically be modeled as longitudinal waves, where the molecules of the medium oscillate around an equilibrium position, or as pressure waves. When the waves leave the speakers, they move out as spherical waves (Figure). The waves interfere; constructive inference is produced by the combination of two crests or two troughs, as shown. Destructive interference is produced by the combination of a trough and a crest. The phase difference at each point is due to the different path lengths traveled by each wave. When the difference in the path lengths is an integer multiple of a wavelength, [latex]\Delta r=|{r}_{2}-{r}_{1}|=n\lambda ,\,\text{where}\,n=0,1,2,3\text{,…,}[/latex] the waves are in phase and there is constructive interference. When the difference in path lengths is an odd multiple of a half wavelength, [latex]\Delta r=|{r}_{2}-{r}_{1}|=n\frac{\lambda }{2},\,\text{where}\,n=1,3,5\text{,…,}[/latex] the waves are [latex]180^\circ(\pi \,\text{rad})[/latex] out of phase and the result is destructive interference. These points can be located with a sound-level intensity meter. Interference of Sound Waves Two speakers are separated by 5.00 m and are being driven by a signal generator at an unknown frequency. A student with a sound-level meter walks out 6.00 m and down 2.00 m, and finds the first minimum intensity, as shown below. What is the frequency supplied by the signal generator? Assume the wave speed of sound is [latex]v=343.00\,\text{m/s}\text{.}[/latex] The wave velocity is equal to [latex]v=\frac{\lambda }{T}=\lambda f.[/latex] The frequency is then [latex]f=\frac{v}{\lambda }.[/latex] A minimum intensity indicates destructive interference and the first such point occurs where there is path difference of [latex]\Delta r=\lambda \text{/}2,[/latex] which can be found from the geometry. 1. Find the path length to the minimum point from each speaker. 2. Use the difference in the path length to find the wavelength. [latex]\Delta r=|{r}_{2}-{r}_{1}|=|6.71\,\text{m}-6.32\,\text{m}|=0.39\,\text{m}[/latex] [latex]\lambda =2\Delta r=2(0.39\,\text{m})=0.78\,\text{m}[/latex] 3. Find the frequency. [latex]f=\frac{v}{\lambda }=\frac{343.00\,\text{m/s}}{0.78\,\text{m}}=439.74\,\text{Hz}[/latex] If point P were a point of maximum intensity, then the path length would be an integer multiple of the wavelength. Check Your Understanding If you walk around two speakers playing music, how come you do not notice places where the music is very loud or very soft, that is, where there is constructive and destructive interference? In the example, the two speakers were producing sound at a single frequency. Music has various frequencies and wavelengths. The concept of a phase shift due to a difference in path length is very important. You will use this concept again in Interference in the second volume of the text and Photons and Matter Waves in the third, where we discuss how Thomas Young used this method in his famous double-slit experiment to provide evidence that light has wavelike properties. Noise Reduction through Destructive Interference Figure shows a clever use of sound interference to cancel noise. Larger-scale applications of active noise reduction by destructive interference have been proposed for entire passenger compartments in commercial aircraft. To obtain destructive interference, a fast electronic analysis is performed, and a second sound is introduced [latex]180^\circ[/latex] out of phase with the original sound, with its maxima and minima exactly reversed from the incoming noise. Sound waves in fluids are pressure waves and are consistent with Pascal’s principle; that is, pressures from two different sources add and subtract like simple numbers. Therefore, positive and negative gauge pressures add to a much smaller pressure, producing a lower-intensity sound. Although completely destructive interference is possible only under the simplest conditions, it is possible to reduce noise levels by 30 dB or more using this technique. Check Your Understanding Describe how noise-canceling headphones differ from standard headphones used to block outside sounds. Regular headphones only block sound waves with a physical barrier. Noise-canceling headphones use destructive interference to reduce the loudness of outside sounds. Where else can we observe sound interference? All sound resonances, such as in musical instruments, are due to constructive and destructive interference. Only the resonant frequencies interfere constructively to form standing waves, whereas others interfere destructively and are absent. Resonance in a Tube Closed at one End As we discussed in Waves, standing waves are formed by two waves moving in opposite directions. When two identical sinusoidal waves move in opposite directions, the waves may be modeled as [latex]{y}_{1}(x,t)=A\,\text{sin}(kx-\omega t)\,\text{and}\,{y}_{2}(x,t)=A\,\text{sin}(kx+\omega t).[/latex] When these two waves interfere, the resultant wave is a standing wave: [latex]{y}_{\text{R}}(x,t)=[2A\,\text{sin}(kx)]\text{cos}(\omega t).[/latex] Resonance can be produced due to the boundary conditions imposed on a wave. In Waves, we showed that resonance could be produced in a string under tension that had symmetrical boundary conditions, specifically, a node at each end. We defined a node as a fixed point where the string did not move. We found that the symmetrical boundary conditions resulted in some frequencies resonating and producing standing waves, while other frequencies interfere destructively. Sound waves can resonate in a hollow tube, and the frequencies of the sound waves that resonate depend on the boundary Suppose we have a tube that is closed at one end and open at the other. If we hold a vibrating tuning fork near the open end of the tube, an incident sound wave travels through the tube and reflects off the closed end. The reflected sound has the same frequency and wavelength as the incident sound wave, but is traveling in the opposite direction. At the closed end of the tube, the molecules of air have very little freedom to oscillate, and a node arises. At the open end, the molecules are free to move, and at the right frequency, an antinode occurs. Unlike the symmetrical boundary conditions for the standing waves on the string, the boundary conditions for a tube open at one end and closed at the other end are anti-symmetrical: a node at the closed end and an antinode at the open end. If the tuning fork has just the right frequency, the air column in the tube resonates loudly, but at most frequencies it vibrates very little. This observation just means that the air column has only certain natural frequencies. Consider the lowest frequency that will cause the tube to resonate, producing a loud sound. There will be a node at the closed end and an antinode at the open end, as shown in Figure. The standing wave formed in the tube has an antinode at the open end and a node at the closed end. The distance from a node to an antinode is one-fourth of a wavelength, and this equals the length of the tube; thus, [latex]{\lambda }_{1}=4L.[/latex] This same resonance can be produced by a vibration introduced at or near the closed end of the tube (Figure). It is best to consider this a natural vibration of the air column, independently of how it is induced. Given that maximum air displacements are possible at the open end and none at the closed end, other shorter wavelengths can resonate in the tube, such as the one shown in Figure. Here the standing wave has three-fourths of its wavelength in the tube, or [latex]\frac{3}{4}{\lambda }_{3}=L,[/latex] so that [latex]{\lambda }_{3}=\frac{4}{3}L.[/latex] Continuing this process reveals a whole series of shorter-wavelength and higher-frequency sounds that resonate in the tube. We use specific terms for the resonances in any system. The lowest resonant frequency is called the fundamental, while all higher resonant frequencies are called overtones. All resonant frequencies are integral multiples of the fundamental, and they are collectively called harmonics. The fundamental is the first harmonic, the first overtone is the second harmonic, and so on. Figure shows the fundamental and the first three overtones (the first four harmonics) in a tube closed at one end. The relationship for the resonant wavelengths of a tube closed at one end is [latex]{\lambda }_{n}=\frac{4}{n}L\enspace{n}=1,3,5,...[/latex] Now let us look for a pattern in the resonant frequencies for a simple tube that is closed at one end. The fundamental has [latex]\lambda =4L,[/latex] and frequency is related to wavelength and the speed of sound as given by [latex]v=f\lambda .[/latex] Solving for f in this equation gives [latex]f=\frac{v}{\lambda }=\frac{v}{4L},[/latex] where v is the speed of sound in air. Similarly, the first overtone has [latex]\lambda =4L\text{/}3[/latex] (see Figure), so that Because [latex]{f}_{3}=3{f}_{1},[/latex] we call the first overtone the third harmonic. Continuing this process, we see a pattern that can be generalized in a single expression. The resonant frequencies of a tube closed at one end are where [latex]{f}_{1}[/latex] is the fundamental, [latex]{f}_{3}[/latex] is the first overtone, and so on. It is interesting that the resonant frequencies depend on the speed of sound and, hence, on temperature. This dependence poses a noticeable problem for organs in old unheated cathedrals, and it is also the reason why musicians commonly bring their wind instruments to room temperature before playing them. Resonance in a Tube Open at Both Ends Another source of standing waves is a tube that is open at both ends. In this case, the boundary conditions are symmetrical: an antinode at each end. The resonances of tubes open at both ends can be analyzed in a very similar fashion to those for tubes closed at one end. The air columns in tubes open at both ends have maximum air displacements at both ends (Figure). Standing waves form as shown. The relationship for the resonant wavelengths of a tube open at both ends is [latex]{\lambda }_{n}=\frac{2}{n}L,\enspace{n}=1,2,3\text{,...}.[/latex] Based on the fact that a tube open at both ends has maximum air displacements at both ends, and using Figure as a guide, we can see that the resonant frequencies of a tube open at both ends are where [latex]{f}_{1}[/latex] is the fundamental, [latex]{f}_{2}[/latex] is the first overtone, [latex]{f}_{3}[/latex] is the second overtone, and so on. Note that a tube open at both ends has a fundamental frequency twice what it would have if closed at one end. It also has a different spectrum of overtones than a tube closed at one end. Note that a tube open at both ends has symmetrical boundary conditions, similar to the string fixed at both ends discussed in Waves. The relationships for the wavelengths and frequencies of a stringed instrument are the same as given in (Equation) and (Equation). The speed of the wave on the string (from Waves) is [latex]v=\sqrt{\frac{{F}_{T}}{\mu }}.[/latex] The air around the string vibrates at the same frequency as the string, producing sound of the same frequency. The sound wave moves at the speed of sound and the wavelength can be found using [latex]v=\lambda f.[/latex] Check Your Understanding How is it possible to use a standing wave’s node and antinode to determine the length of a closed-end tube? Show Solution When the tube resonates at its natural frequency, the wave’s node is located at the closed end of the tube, and the antinode is located at the open end. The length of the tube is equal to one-fourth of the wavelength of this wave. Thus, if we know the wavelength of the wave, we can determine the length of the tube. This video lets you visualize sound waves. Check Your Understanding You observe two musical instruments that you cannot identify. One plays high-pitched sounds and the other plays low-pitched sounds. How could you determine which is which without hearing either of them play? Show Solution Compare their sizes. High-pitch instruments are generally smaller than low-pitch instruments because they generate a smaller wavelength. • Unwanted sound can be reduced using destructive interference. • Sound has the same properties of interference and resonance as defined for all waves. • In air columns, the lowest-frequency resonance is called the fundamental, whereas all higher resonant frequencies are called overtones. Collectively, they are called harmonics. Conceptual Questions You are given two wind instruments of identical length. One is open at both ends, whereas the other is closed at one end. Which is able to produce the lowest frequency? Show Solution The fundamental wavelength of a tube open at each end is 2L, where the wavelength of a tube open at one end and closed at one end is 4L. The tube open at one end has the lower fundamental frequency, assuming the speed of sound is the same in both tubes. What is the difference between an overtone and a harmonic? Are all harmonics overtones? Are all overtones harmonics? Two identical columns, open at both ends, are in separate rooms. In room A, the temperature is [latex]T=20^\circ\text{C}[/latex] and in room B, the temperature is [latex]T=25^\circ\text{C}[/latex]. A speaker is attached to the end of each tube, causing the tubes to resonate at the fundamental frequency. Is the frequency the same for both tubes? Which has the higher frequency? Show Solution The wavelength in each is twice the length of the tube. The frequency depends on the wavelength and the speed of the sound waves. The frequency in room B is higher because the speed of sound is higher where the temperature is higher. (a) What is the fundamental frequency of a 0.672-m-long tube, open at both ends, on a day when the speed of sound is 344 m/s? (b) What is the frequency of its second harmonic? What is the length of a tube that has a fundamental frequency of 176 Hz and a first overtone of 352 Hz if the speed of sound is 343 m/s? The ear canal resonates like a tube closed at one end. (See [link]Figure 17_03_HumEar[/link].) If ear canals range in length from 1.80 to 2.60 cm in an average population, what is the range of fundamental resonant frequencies? Take air temperature to be [latex]37.0^\circ\text{C,}[/latex] which is the same as body temperature. Calculate the first overtone in an ear canal, which resonates like a 2.40-cm-long tube closed at one end, by taking air temperature to be [latex]37.0^\circ\text{C}[/latex]. Is the ear particularly sensitive to such a frequency? (The resonances of the ear canal are complicated by its nonuniform shape, which we shall ignore.) Show Solution 11.0 kHz; The ear is not particularly sensitive to this frequency, so we don’t hear overtones due to the ear canal. A crude approximation of voice production is to consider the breathing passages and mouth to be a resonating tube closed at one end. (a) What is the fundamental frequency if the tube is 0.240 m long, by taking air temperature to be [latex]37.0^\circ\text{C}[/latex]? (b) What would this frequency become if the person replaced the air with helium? Assume the same temperature dependence for helium as for air. A 4.0-m-long pipe, open at one end and closed at one end, is in a room where the temperature is [latex]T=22^\circ\text{C}\text{.}[/latex] A speaker capable of producing variable frequencies is placed at the open end and is used to cause the tube to resonate. (a) What is the wavelength and the frequency of the fundamental frequency? (b) What is the frequency and wavelength of the first overtone? a. [latex]v=344.08\,\text{m/s,}\enspace{\lambda }_{1}=16.00\,\text{m,}\enspace{f}_{1}=21.51\,\text{Hz;}[/latex] b. [latex]{\lambda }_{3}=5.33\,\text{m,}\enspace{f}_{3}=64.56\,\text{Hz}[/latex] A 4.0-m-long pipe, open at both ends, is placed in a room where the temperature is[latex]T=25^\circ\text{C}\text{.}[/latex] A speaker capable of producing variable frequencies is placed at the open end and is used to cause the tube to resonate. (a) What are the wavelength and the frequency of the fundamental frequency? (b) What are the frequency and wavelength of the first overtone? A nylon guitar string is fixed between two lab posts 2.00 m apart. The string has a linear mass density of [latex]\mu =7.20\,\text{g/m}[/latex] and is placed under a tension of 160.00 N. The string is placed next to a tube, open at both ends, of length L. The string is plucked and the tube resonates at the [latex]n=3[/latex] mode. The speed of sound is 343 m/s. What is the length of the tube? Show Solution [latex]\begin{array}{cc} {v}_{\text{string}}=149.07\,\text{m/s,}\enspace{\lambda }_{3}=1.33\,\text{m,}\enspace{f}_{3}=112.08\,\text{Hz}\hfill \\ {\lambda }_{1}=\frac{v}{{f}_{1}},\enspace{L}=1.53\,\ text{m}\hfill \end{array}[/latex] A 512-Hz tuning fork is struck and placed next to a tube with a movable piston, creating a tube with a variable length. The piston is slid down the pipe and resonance is reached when the piston is 115.50 cm from the open end. The next resonance is reached when the piston is 82.50 cm from the open end. (a) What is the speed of sound in the tube? (b) How far from the open end will the piston cause the next mode of resonance? Students in a physics lab are asked to find the length of an air column in a tube closed at one end that has a fundamental frequency of 256 Hz. They hold the tube vertically and fill it with water to the top, then lower the water while a 256-Hz tuning fork is rung and listen for the first resonance. (a) What is the air temperature if the resonance occurs for a length of 0.336 m? (b) At what length will they observe the second resonance (first overtone)? Show Solution a. [latex]22.0{}^\circ \text{C}[/latex]; b. 1.01 m the lowest-frequency resonance the term used to refer collectively to the fundamental and its overtones all resonant frequencies higher than the fundamental
{"url":"https://pressbooks.online.ucf.edu/osuniversityphysics/chapter/17-4-normal-modes-of-a-standing-sound-wave/","timestamp":"2024-11-02T03:13:06Z","content_type":"text/html","content_length":"135039","record_id":"<urn:uuid:def587b6-b261-4c05-ad08-ea1810faf703>","cc-path":"CC-MAIN-2024-46/segments/1730477027632.4/warc/CC-MAIN-20241102010035-20241102040035-00235.warc.gz"}
87 research outputs found The evolution of inhomogeneities in a spherical collapse model is studied by expanding the Einstein equation in powers of inverse radial parameter. In the linear regime, the density contrast is obtained for flat, closed and open universes. In addition to the usual modes, an infinite number of new growing modes are contained in the solutions for pressureless open and closed universes. In the nonlinear regime, we obtain the leading growing modes in closed forms for a flat universe and also, in the limits of small and large times, for an open universe.Comment: latex, 17 pages; electronic address for programs correcte Decay amplitudes for mesons in two-dimensional QCD are discussed. We show that in spite of an infinite number of conserved charges, particle production is not entirely suppressed. This phenomenon is explained in terms of quantum corrections to the combined algebra of higher-conserved and spectrum-generating currents. We predict the qualitative form of particle production probabilities and verify that they are in agreement with numerical data. We also discuss four-dimensional self-dual Yang-Mills theory in the light of our results.Comment: We discuss in more detail the background material and elaborate on the consequences of the new results. New equations and text are added and the figures are efficiently regenerated by a fortran program, latex file, 13 pages, 4 figures in encapsulated postscript files, uses epsf. minor changes, version to appear in Physical Review Cold dark matter haloes are populated by caustics, which are yet to be resolved in N-body simulations or observed in the Universe. Secondary infall model provides a paradigm for the study of caustics in "typical" haloes assuming that they have had no major mergers and have grown only by smooth accretion. This is a particular characteristic of the smallest dark matter haloes of about 10^{-5} Mo, which although "atypical" contain no substructures and could have survived until now with no major mergers. Thus using this model as the first guidline, we evaluate the neutralino self-annihilation flux for these haloes. Our results show that caustics could leave a distinct sawteeth signature on the differential and cumulative fluxes coming from the outer regions of these haloes. The total annihilation signal from the regions away from the centre can be boosted by about forty percents.Comment: To appear in JCAP, 4 pages, 3 figure We propose that the accretion of a dwarf spheroidal galaxy provides a common origin for the giant southern stream and the warp of M31. We run about 40 full N-body simulations with live M31, infalling galaxies with varying masses and density profiles, and cosmologically-plausible initial orbital parameters. Excellent agreement with a full range of observational data is obtained for a model in which a dark-matter-rich dwarf spheroidal, whose trajectory lies on the thin plane of corotating satellites of M31, is accreted from its turnaround radius of about 200 kpcs into M31 at approximately 3 Gyrs ago. The satellite is disrupted as it orbits in the potential well of the galaxy and forms the giant stream and in return heats and warps the disk of M31. We show that our cosmologically-motivated model is favoured by the kinematic data over the phenomenological models in which the satellite starts its infall from a close distance of M31. Our model predicts that the remnant of the disrupted satellite resides in the region of the North-Eastern shelf of M31. The results here suggest that the surviving satellites of M31 that orbit on the same thin plane, as the disrupted satellite once did, could have all been accreted from an intergalactic filament.Comment: 18 pages, 22 figures, 3 table The existence of critical points for the peculiar velocity field is a natural feature of the correlated vector field. These points appear at the junctions of velocity domains with different orientations of their averaged velocity vectors. Since peculiar velocities are the important cause of the scatter in the Hubble expansion rate, we propose that a more precise determination of the Hubble constant can be made by restricting analysis to a subsample of observational data containing only the zones around the critical points of the peculiar velocity field, associated with voids and saddle points. On large-scales the critical points, where the first derivative of the gravitational potential vanishes, can easily be identified using the density field and classified by the behavior of the Hessian of the gravitational potential. We use high-resolution N-body simulations to show that these regions are stable in time and hence are excellent tracers of the initial conditions. Furthermore, we show that the variance of the Hubble flow can be substantially minimized by restricting observations to the subsample of such regions of vanishing velocity instead of aiming at increasing the statistics by averaging indiscriminately using the full data sets, as is the common approach.Comment: 17 pages, 6 figures and 2 tables, minor modifications after receiving several useful comment We present catalogues of voids for the SDSS DR7 redshift survey and for Millennium I simulation mock data. We aim to compare the observations with simulations based on a $\Lambda$CDM model and a semi-analytic galaxy formation model. We use the void statistics as a test for these models. We assembled a mock catalogue that closely resembles the SDSS DR7 catalogue and carried out a parallel statistical analysis of the observed and simulated catalogue. We find that in the observation and the simulation, voids tend to be equally spherical. The total volume occupied by the voids and their total number are slightly larger in the simulation than in the observation. We find that large voids are less abundant in the simulation and the total luminosity of the galaxies contained in a void with a given radius is higher on average than observed by SDSS DR7 survey. We expect these discrepancies to be even more significant in reality than found here since the present value of $\sigma_8$ given by WMAP7 is lower than the value of 0.9 used in the Millennium I simulation. The reason why the simulation fails to produce enough large and dark voids might be the failure of certain semi-analytic galaxy formation models to reduce the small-scale power of $\Lambda$CDM and to produce sufficient power on large scales.Comment: Kolmogrov-Smirnov test added. version to appear in A& The observed infall of galaxies into the Virgo Cluster puts strong constraints on the mass of the cluster. A non-parametric fully non-linear description of the infall can be made with orbit reconstructions based on Numerical Action Methods. The mass of the cluster is determined to be $1.2 \times 10^{15} M_{\odot}$. The mass-to-light ratio for the cluster is found to be seven times higher than the mean ratio found across the region within V=3000 km/s.Comment: 7 pages, 3 figures, version to appear in "Outskirt of Galaxy Clusters", the proceedings of IAU Colloquim No 195, 200
{"url":"https://core.ac.uk/search/?q=author%3A(ROYA%20MOHAYAEE)","timestamp":"2024-11-03T07:52:12Z","content_type":"text/html","content_length":"122581","record_id":"<urn:uuid:ffa8830d-e18a-4cd2-8557-bc355ed031a5>","cc-path":"CC-MAIN-2024-46/segments/1730477027772.24/warc/CC-MAIN-20241103053019-20241103083019-00112.warc.gz"}
THE PREMATURITY SCORE MODEL - A NEW TOOL FOR ASSESSING MORBIDITY IN PREMATURE INFANTS - Archiv-EuroMedica Erfolgreich durch internationale Zusammenarbeit Cite as: Archiv EuroMedica. 2022. 12; 3: e1. DOI 10.35630/2199-885X/2022/12/3.8 Received 5 April 2022; Received in revised form 30 April 2022; Accepted 2 May 2022 Svetlana Perepelitsa^1,2 , Sergey Khorozov^3 , Arkady Golubev^1 , Maxim Lubomudrov^1 ^1 V. A. Negovsky Research Institute of General Reanimatology, Federal Research and Clinical Center of Intensive Care Medicine and Rehabilitology, Moscow; ^2 Imannuel Kant Baltic Federal University, Kaliningrad; ^3 OOO “HSoft”, Kaliningrad, Russia The aim of the study was to develop a prematurity score model for predicting morbidity in premature infants in the neonatal period. Material and methods. To build an analytical model, such parameter as gestational age is used, measured in weeks with the range of changes in the score between 0 to 10, where a score of 38 - 40 weeks should correspond to 0 points, i.e., at this gestational age, the morbidity rate due to morphological immaturity of organs and systems is minimal, because by this gestational age the organ development is almost completed. The maximum score is 9.9, at gestational age of 22 weeks, which is associated with severe morphological immaturity and, therefore, high morbidity in a premature Results. Based on mathematical calculations, we built a model of prematurity score, which helps estimate the degree of abnormality of a premature baby depending on gestational age. The lower it is, the higher is the score of prematurity and the likelihood of morbidity due to morphological immaturity of organs and systems. Conclusion. The proposed analytical model of prematurity score can be used to uniformly assess the prematurity factor in order to further determine the medical strategy. The model can be effectively used in scientific research and to identify statistically necessary relationships in a wide variety of studies. The main value of the model lies not in obtaining specific numerical values, but in the suggested analytical relationship reflecting the degree of morphological maturity and allowing probability estimation of the newborn morbidity. Keywords: prematurity score; newborn; morbidity; mortality; prenatal morphogenesis High quality specialized medical care for newborns with respiratory and circulatory disorders in the early neonatal period is a key issue of anesthesiology and critical care medicine. An increasing number of children need critical care starting from birth, so the researchers continue seeking early recognition and prediction tools for the development, progression and outcome of critical illness in newborns. The scales that can help make a prompt diagnosis in a specific clinical situation are the most convenient and easy to use. Perinatal hypoxia and asphyxia of the newborn are among the most clinically important conditions requiring a quick diagnosis. In 1952, at the XXVII International Congress of Anesthesiologists, an American physician Virginia Apgar presented the first system for rapid assessment of hypoxia severity in a newborn, which was later called the Apgar Scale [1]. For many decades, it has been used worldwide to assess each newborn baby, which has reduced the mortality rate of babies born in acute asphyxia, improving immediate and long-term outcomes. The search for causes and early diagnosis of hypoxia is one of the primary tasks that should be addressed at birth, since hypoxia and its sequelae can result in the death of a newborn, or, if the child survives, in impaired neuropsychological development [2]. Initially, the Apgar scale was intended for full-term infants, but decades later, it became obvious that the scale is not applicable to a specific population, which includes premature and extremely premature infants, due to incomplete development of their nervous and respiratory systems. Therefore, to assess the severity of acute respiratory failure in premature infants with respiratory distress syndrome (RDSN), an appropriate scale was developed in 1956 by American pediatricians William Silverman and Dorothy Andersen. The scale includes the following characteristics: upper chest movement, lower chest (intercostal) retractions, xiphoid retractions, mandibular position, and respiratory pattern. The assessment is performed immediately at birth and after 2, 6, 12 and 24 hours. It is used to establish indications for respiratory therapy and assess its effectiveness [3]. One of the most promising tools for neonatal disease severity assessment is a revised version of the Score for Neonatal Acute Physiology (SNAP-II), which assesses the instability of the child's physiological parameters not only in the first day of life, but also throughout the neonatal period [4,5]. The disease severity is associated with the lethal outcome likelihood, but the scale includes numerous parameters, which makes it not always applicable. The scales should use the patient history and clinical presentation to accurately assess the risk of lethal outcome. The neonatal assessment scales commonly used in clinical practice do not always allow the neonatal prognosis to be determined. The SNAP (Score for Neonatal Acute Physiology) and CRIB (Clinical Risk Index for Babies) assessment systems are used to assess the severity of prematurity. They are tools for comparative assessment of the risk of adverse outcomes [6,7]. Current scales are divided into two large groups: • predictive ones including PRISM (Pediatric Risk of Mortality), PIM (Pediatric Index of Mortality), CRIB, SNAP • scales of current assessment such as PEMOD (Pediatric Multiple Organ Dysfunction Score), PELOD (Pediatric Logistic Organ Dysfunction Score), NEOMOD (Neonatal Multiple Organ Dysfunction Score) NEOMOD is the only neonatal organ dysfunction scale that has been obtained on a fairly significant sample of premature infants. It was created in a prospective study of 142 extremely low birth weight infants over a period of 28 days after birth. Modern computer technology can perform automatic calculations to assess the severity of the disease and determine the possible risks to the child, which can help clinicians in choosing treatment strategy [6]. Advances in perinatal medicine have improved the morbidity and mortality of premature infants [9,10], but gestational age at birth is considered to be one of the main predictors of neonatal mortality. There is a direct correlation between them: the shorter the gestational age, the higher the neonatal mortality. The mortality rate in a very premature infant with extremely low birth weight (ELBW) can be ten times higher than in a full-term infant [11]. Gestational age reflects the degree of morphological maturation of fetal organs at the time of birth, and the morphological immaturity of the central nervous system is a key problem of premature infants, because it is the most sensitive to hypoxia, metabolic disorders and other adverse events occurring in the perinatal period [12,13,14]. A unified methodology for assessing the degree of prematurity or prematurity score is necessary both to determine the management of the newborn and to obtain more accurate results in scientific The aim of the study was to develop a prematurity score model for predicting the morbidity of a premature infant in the neonatal period. Problem statement The prematurity score is a tool that can be used to more accurately estimate the probability of morbidity in a premature infant. The parameter used to build the analytical model is gestational age, measured in weeks, and the range of this parameter values is between 0 and 10. The prematurity score should change gradually with gestational age. The stepwise, i.e., chart-based estimation of the score is both less accurate and, in certain situations, inconsistent: it is not clear which value should be chosen if gestational age coincides with the range limit. It is also clear that the task has a probabilistic nature, i.e., we cannot determine the exact cut-off value below which the infant becomes nonviable. Therefore, it is more logical to assume that around the supposed limit the score should asymptotically tend to absolutely “negative" values, but never reach The prematurity score will assess the infant morbidity. Thus, the term of 38 - 40 weeks should correspond to a score of 0, i.e., at this gestational age morbidity is minimal, because by this gestational age the organ development is almost complete. With decreasing gestational age, the score will tend to 10, and the smaller the gestational age of the child, the higher the score of prematurity will be. Expert estimates should be taken as "reference" values for the formulation of the analytical relationship. Construction of an analytical model of prematurity score Based on the problem statement, the desired graph should have approximately the following form (Fig. 1). Figure 1. Graphic representation of relationship between prematurity score and age of gestation. This relationship should be calculated according to the following formula: Where B[prem] - the prematurity score, A = 10 (scaling factor); k – timescale factor; n – slope of the curve. There are two unknown coefficients k and n in the proposed formula (1), which we will seek based on specified expert values. Let us take the following expert values as "reference" values (Table 2). Table 2. Expert values. T[prg] B[prem] 22 9.9 38 0.000001 When T[prg] = 38, the expert value of B[prem] = 0 in the table is replaced by B[prem] = 0.000001, because no two values can be the same when describing a monotonic curve of the prematurity score. This replacement does not bring any additional errors, in terms of the required accuracy. Using the "reference" expert values from Table 2, we find the coefficients k and n (1). For this purpose, we take the logarithm of the expression (1) by first dividing both parts by A and substituting the expression for t ̅, We will flip the fraction under the logarithm on the left side and reverse the sign on the right side accordingly Then we will take the logarithm of the expression (3) again to get rid of the n exponent On the right hand side, let's express the logarithm of the fraction as the difference of the logarithms Let us introduce the notations: Then equation (5) will have a form of Based on table 2 and expressions (6)-(9), let's construct table 3 to determine the n and K coefficients of linear regression (9). The point (40; 0) according to expressions (8) and (9) is converted to (-∞; -∞), so it should be excluded from the data for plotting the regression Y(X). Table 3. Baseline data for the construction of linear regressions X Y 22 9.9 2.890371758 -4.600149 36 2 1.386294361 0.475885 38 0.000001 0.693147181 2.7799426 40 0 - - Based on the data in Table 3, let us plot a linear regression Y(X) (Fig.2) Figure 2. Linear regression Y(X). As one can see from the graph, the linear regression calculated using the method of least squares passes through the given points perfectly (correlation coefficient R=1). The graph also shows the equation and, respectively, the regression coefficients: From formula (7) we can obtain the coefficient k to use it in (1) Then K=4.587 And the resulting graph of the Prematurity Score model takes the following form (Figs. 3,4). Figure 3. Graph of the prematurity score model. Figure 4. Graph of the prematurity score model – area of optimal values. The graphs (Figs. 3,4) show that the proposed model " seamlessly" describes the entire range of possible values. The point (40; 0) is special because formula (1) will yield uncertainty at this point, since this point does not fall within the area of definition of this formula. However, calculating the limit from dependence (1) at T[prg] →0 gives the correct value. In the presented model, the gestational age of 22 - 25 weeks corresponds to 9.9-9.8 points, which represents almost maximum value and critical morbidity of the newborn. Indeed, this gestational age is associated with severe morphological immaturity of the child. The main problems of children born at this gestational age are morphological immaturity and antenatal lung damage, which greatly increase the risk of acute respiratory failure (ARF) due to lung hypoxia and ischemia, surfactant deficiency and other factors [15]. Lung development at 22-26 weeks of gestation corresponds to the canalicular period associated with formation of primitive acini and differentiation of alveolar cells with initial stages of surfactant and fetal lung fluid development [16], i.e., there are still no so-called "alveolar sacs" and alveoli, no full-grown 2nd order alveolar cells producing surfactant, and full gas exchange function of the lungs immediately after birth is not possible. The terminal sac period of lung development begins at 26 weeks of gestation, when alveolar passages are formed, alveolar sacs and primitive alveoli appear. In most cases of extremely low birth weight newborns, the lung has canalicular structure. Parenchyma is represented by numerous alveolar ducts lined by cuboidal epithelium and enclosed by undifferentiated connective tissue. Wide primitive vessels are filled with blood, and capillary network of the lung is underdeveloped. In some children the lung has alveolar structure, but alveolar septa are wide, many alveoli are lined by cuboidal epithelium At 31-33 weeks of gestation lung development proceeds, which ensures its functional adequacy and reduces the risk of morbidity of premature infants born in this gestational period. The number of alveoli in the lungs significantly increases, the alveolar wall becomes thinner, collagen structures appear in alveolar passages, septa are covered with flattened epithelium and contain a dense network of capillaries, the production of surfactant increases, which enhances the functional capacity of lungs and the risk of neonatal morbidity begins to decrease. This gestational age corresponds to a 9 points in the prematurity model. By gestational age of 34-35 weeks, differences in the structure of the lung parenchyma between the superficial and deep regions appear. The acinar development progresses [18]. Premature birth and hypoxemia that a premature infant may experience adversely affect the developmental trajectory of all brain structures [19]. Cerebral blood flow autoregulation disorders with the underlying profound prematurity are associated with low cerebral blood flow or its significant fluctuations, especially with unstable central hemodynamics, which leads to worsening cerebral ischemia or hemorrhage [20]. Ligation of a hemodynamically significant open arterial duct in grade I-II intraventricular hemorrhage increases the risk of cerebral malformations, because intra- and postoperative alteration of central hemodynamics affects cerebral blood flow, which may contribute to further damage of brain structures [21]. Zhang et al., Christian et al. showed that the most common grade III-IV hemorrhages occurring in children with gestational age of less than 28 weeks are the ones in germinal matrix and vascular plexus. These hemorrhages result in cystic periventricular leukomalacia and posthemorrhagic hydrocephalus [22,23]. The source of subependymal and intraventricular hemorrhages is the germinal matrix, which is the fetal tissue characteristic of fetuses at 22 to 32 weeks’ gestation, corresponding to 9.8-8 points of the Prematurity Model, that is, children born in this period have the risk of developing the complications listed above. Physiological reduction of the germinal matrix begins at 32 weeks and ends by full-term gestation [24,25]. This gestational age is associated with a progressive decrease in the model score of prematurity, while the probability of premature infant morbidity declines. Gestational age of 22 to 26 weeks is associated with a high probability of death. Gestational age of 22 weeks corresponds to 9.9 points in the model of prematurity score. A decrease of 1 point occurs only after 9 weeks and reaches 9 points by 31 weeks’ gestation. After this age, there is a rapid decrease in the prematurity score. Thus, between 31 and 33 weeks, it decreases by another 1 point. Further, with increasing gestational age, the prematurity score progressively decreases, tending to 0 at full-term gestation, when the main morphogenesis of a full-term pregnancy is completed. The proposed analytical model of prematurity score can be used for a consistent assessment of the prematurity factor to determine the medical management strategy. The model can be effectively used in scientific research and identification of necessary statistical relationships in a wide variety of studies. The main value of the model lies not in obtaining specific numerical values, but in the proposed form of analytical relationship. The model represents the degree of morphological maturity and allows to estimate the probability of abnormalities in the newborn. Conflict of interest The authors declare no conflict of interest. 1. Apgar, V. A proposal for a new method of evaluation of the newborn infant. Curr Res Anesth Analg.1953,32(4), 260-7. doi:10.1213/00000539-195301000-00041. 10.1213/ANE.0b013e31829bdc5c 2. Lai, S.; Flatley, C.; Kumar, S. Perinatal risk factors for low and moderate five-minute Apgar scores at term. Eur J Obstet Gynecol Reprod Biol. 2017, 210, 251-6. doi: 10.1016/j.ejogrb.2017.01.008 3. Neonatology. National leadership ed. N.N. Volodin. M. GEOTAR-Media. 2019.896 p. ISBN: 978-5-9704-4877-9 4. Richardson, D.K.; Corcoran, J. D.; Escobar, G.J.; Lee, S.K. SNAP-II and SNAPPE-II: Simplified newborn illness severity and mortality risk scores. J Pediatr. 2001, 138(1), 92–100. 5. Lin, H.J.; Ma, X.L; Shi, L.P.; Luo, F.; Du, L.Z. Predicting outcome in necrotizing enterocolitis with the score for neonatal acute physiology: a retrospective study of 62 cases. Zhonghua Er Ke Za Zhi. 2013, 51(5), 326–330. 6. Morse, S.; Groer, M.; Shelton, M.M.; Maguire, D.; Ashmeade, T. A Systematic Review: The Utility of the Revised Version of the Score for Neonatal Acute Physiology among Critically Ill Neonates. J Perinat Neonatal Nurs. 2015, 29(4), 315–344. doi: 10.1097/JPN.0000000000000135 7. Medvedev, M.M.; Brotherton, H.; Gai, A.; Tann, C.; Gale, C.; Waiswa, P.; Elbourne, D.; Lawn, J. E.; Allen, E. Development and validation of a simplified score to predict neonatal mortality risk among neonates weighing 2000 g or less (NMR-2000): an analysis using data from the UK and The Gambia. Lancet Child Adolesc Health. 2020, 4(4),299–311. doi: https://doi.org/10.1016/S2352-4642(20) 8. Gulla, K.M.; Sachdev, A. Illness severity and organ dysfunction scoring in Pediatric Intensive Care Unit. Indian J Crit Care Med. 2016, 20(1), 27–35. doi: 10.4103/0972-5229.173685 9. Li, B.; Concepcion, K.; Meng, X.;Zhang, L. Brain-immune interactions in perinatal hypoxic-ischemic brain injury. Prog Neurobiol. 2017, 159, 50–68. doi: 10.1016/j.pneurobio.2017.10.006 10. Hinojosa-Rodríguez, M.; Harmony, T.; Carrillo-Prado;Van Horn, C. J.D.; Irimia, A.; Torgerson, C.; Jacokes, Z. Clinical neuroimaging in the preterm infant: Diagnosis and prognosis. Neuroimage Clin. 2017, 16, 355–368. https://doi.org/10.1016/j.nicl.2017.08.015. 11. Harmon, Q.E.; Basso, O.; Weinberg, C.R.; Wilcox, A.J. Two denominators for one numerator: the example of neonatal mortality. Eur J Epidemiol. 2018, 33(6), 523-530. doi: 10.1007/s10654-018-0373-0 12. Martinez-Biarge, M.; Diez-Sebastian, J.; Wusthoff, C.J.; Mercuri, E., Cowan, F.M. Antepartum and intrapartum factors preceding neonatal hypoxic-ischemic encephalopathy. Pediatrics. 2013, 132(4), e952-9. doi: 10.1542/peds.2013-0511 13. Julvez, J.; Paus, T.; Bellinger, D.; Eskenazi, B.; Tiemeier, H.; Pearce, N.; Ritz, B.; White, T.; Ramchandani, P.; Domingo, J.; Desrivières, G.S.; Brouwer, R.; Boucher, O.; Alemany, S.; López-Vicente, M.; Suades-González, E.; Forns, J.; Grandjean, P.; Sunyer, J. Environment and brain development: challenges in the global context. Neuroepidemiology. 2016, 46(2), 79–82. doi: 14. Vasung, L.; Turk, E. A.; Ferradal, S. L.; Sutin, J.; Stout, J. N.; Ahtam, B.; Lin, P.-Y.; Grant, P. E. Exploring early human brain development with structural and physiological neuroimaging. Neuroimage. 2019, 187, 226–254. doi: 10.1016/j.neuroimage.2018.07.041 15. Perepelitsa, S.A.; Golubev, A.M.; Moroz, V.V. Premature newborn. Moscow. Creative Economy. 2019, 232 р. ISBN 978-5-91292-288-6. 16. Milovanov, A.P.; Savelyeva, S.V. Intrauterine human development. Moscow. MDV. 2006, 384 p. 17. Golubev, A.M.; Perepelitsa, S.A.; Smerdova, E.F.; Moroz, V.V. Morphological changes in the lungs of premature infants with hyaline membrane disease when using artificial ventilation and exogenous surfactants. Archive of pathology. 2010, 6, 34–38. 18. Andreeva, T.V. Examination of the final respiratory tract to determine the compliance of the lungs with gestational age in the perinatal period. Norm. Archive of pathology. 1994, 2, 69-73. 19. Padilla, N.; Alexandrou, G.; Blennow, M.; Lagercrantz, H.; Ådén, U. Brain Growth Gains and Losses in Extremely Preterm Infants at Term. Cerebral Cortex. 2015, 25 (7), 1897–1905, https://doi.org/ 20. Vik, S. D.; Torp, H.; Follestad, T.; Støen, R.; Nyrnes, S. A. NeoDoppler: New ultrasound technology for continuous cerebral circulation monitoring in neonates. Pediatr Res. 2020, 87(1), 95–103. doi: 10.1038/s41390-019-0535-0 21. El-Khuffash, A.; James, A.T.; Corcoran, J.D.; Dicker, P.; Franklin, O.; Elsayed,^ Y.N.; Ting, J.Y.; Sehgal, A.; Malikiwi,^ A.; Harabor, ^ A.; Soraisham, A. S.; McNamara, P. J. A Patent Ductus Arteriosus Severity Score Predicts Chronic Lung Disease or Death before Discharge. J Pediatr. 2015, 167(6), 1354-1361.e2. doi: 10.1016/j.jpeds.2015.09.028 22. Zhang, X.H.; Qiu, S.J.; Chen, W.J.; Gao,X.R.; Li, Y.; Cao, J.; Zhang, J.J. Predictive Value of Cranial Ultrasound for Neurodevelopmental Outcomes of Very Preterm Infants with Brain Injury. Chin Med J (Engl). 2018,131 (8), 920-926. doi: 10.4103/0366-6999.229895 23. Christian, E.A.; Jin, D.L.; Attenello, F.; Wen, T.; Cen, S.; Mack, W.J.; Krieger, M.D.; McComb, J.G. Trends in hospitalization of preterm infants with intraventricular hemorrhage and hydrocephalus in the United States, 2000-2010. J Neurosurg Pediatr. 2016, 17 (3), 260-269. doi: 10.3171/2015.7.PEDS15140 24. Pediatric ultrasound diagnostics. Textbook ed. Pykova M.I. 2015. M.: Publishing house. house Vidar-M. vol. 3. 368 р. 25. Ballabh, P. Pathogenesis and prevention of intraventricular hemorrhage. Clin Perinatol. 2014, 41 (1),47-67. doi: https://doi.org/10.1016/j.clp.2013.09.007.
{"url":"http://journal-archiveuromedica.eu/archiv-euromedica-03-2022/8-THE-PREMATURITY-SCORE-MODEL-A-NEW-TOOL-FOR.html","timestamp":"2024-11-03T05:55:17Z","content_type":"text/html","content_length":"38445","record_id":"<urn:uuid:71049f25-3d86-4ba7-8048-2490cbc0136a>","cc-path":"CC-MAIN-2024-46/segments/1730477027772.24/warc/CC-MAIN-20241103053019-20241103083019-00682.warc.gz"}
Win, Lose, or Draw Win, Lose, or Draw is a non-algebra based math course that fulfills a general education mathematics requirement. The class structure is discovery-based with students working in groups rather than lecture-dependent, appealing to students for whom math is not a strength. The theme of the course involves games and the mathematics behind them with an emphasis on understanding concepts as opposed to memorizing formulas. Topics include logic, sets, combinatorics, probability, and expectation. This workbook is the only book students need for the class and contains group work, notes, reviews for tests, and a final review with answers provided. TEST 1 LOGIC, SETS, AND COMBINATORICS Group 1 The Games We Play Group 2 Paper Games Group 3 LSAT Practice Group 4 Logic Puzzles Group 5 Sudoku Puzzles Group 6 More Logic Puzzles SETS and VENN DIAGRAM NOTES Group 7 Deck of Cards and Set Operations Group 8 Venn Diagrams Group 9 More Venn Diagrams Group 10 Is Order Important? Group 11 Poker Chips Group 12 Is Order Important? II Crunching the Numbers Group 13 Poker Hands and Combinations Group 14 Volleyball Tournaments REVIEW PROBLEMS FOR TEST 1 TEST 2 PROBABILITY Group 15 The Monty Hall Problem Let’s Make a Deal Group 16 Slot Machines Group 17 Dice Games: Risk and Craps Group 18 Roulette: What are the Odds? Group 19 Bayes Theorem Group 20 Poker Hands Group 21 Probabilities of Poker Group 22 Pinochle Poker Group 23 Keno and Lotteries Group 24 Expectation and Lotteries Group 25 Roulette and Expected Value Group 26 Blackjack: A Lesson in Counting Cards FINAL REVIEW TOPICS and PROBLEMS Win, Lose, or Draw is a non-algebra based math course that fulfills a general education mathematics requirement. The class structure is discovery-based with students working in groups rather than lecture-dependent, appealing to students for whom math is not a strength. The theme of the course involves games and the mathematics behind them with an emphasis on understanding concepts as opposed to memorizing formulas. Topics include logic, sets, combinatorics, probability, and expectation. This workbook is the only book students need for the class and contains group work, notes, reviews for tests, and a final review with answers provided. TEST 1 LOGIC, SETS, AND COMBINATORICS Group 1 The Games We Play Group 2 Paper Games Group 3 LSAT Practice Group 4 Logic Puzzles Group 5 Sudoku Puzzles Group 6 More Logic Puzzles SETS and VENN DIAGRAM NOTES Group 7 Deck of Cards and Set Operations Group 8 Venn Diagrams Group 9 More Venn Diagrams Group 10 Is Order Important? Group 11 Poker Chips Group 12 Is Order Important? II Crunching the Numbers Group 13 Poker Hands and Combinations Group 14 Volleyball Tournaments REVIEW PROBLEMS FOR TEST 1 TEST 2 PROBABILITY Group 15 The Monty Hall Problem Let’s Make a Deal Group 16 Slot Machines Group 17 Dice Games: Risk and Craps Group 18 Roulette: What are the Odds? Group 19 Bayes Theorem Group 20 Poker Hands Group 21 Probabilities of Poker Group 22 Pinochle Poker Group 23 Keno and Lotteries Group 24 Expectation and Lotteries Group 25 Roulette and Expected Value Group 26 Blackjack: A Lesson in Counting Cards FINAL REVIEW TOPICS and PROBLEMS
{"url":"https://he.kendallhunt.com/product/win-lose-or-draw","timestamp":"2024-11-06T08:49:04Z","content_type":"text/html","content_length":"41878","record_id":"<urn:uuid:78ff0019-8713-48bd-874b-756f66dd67e0>","cc-path":"CC-MAIN-2024-46/segments/1730477027910.12/warc/CC-MAIN-20241106065928-20241106095928-00148.warc.gz"}
Marcus Lemonis Margin Calculator – Android Appar — AppAgg Cost Margin Calculator i App Store Ques – What is the Intraday Margin Provided? Use this calculator to work out your gross margin, sales margin or net profit margin for your product or business Warning: It is necessary to enable JavaScript for full calculator functionality. Here are some instructions for how to enable JavaScript in your browser. You may also like to try our profitability calculator like Margin calculator, The net profit margin calculator,\ EBIT calculator, return on Investment calculator. Now there is no need to worry about calculating your employee’s overtime pay, use our overtime calculator. if you want to know the commission percentage on sales in just a second so don’t forget to use our commission calculator. The Excel Profit Margin Formula is the amount of profit divided by the amount of the sale or (C2/A2)100 to get value in percentage. $120/seat, $150/seat, what have you. The problem is once you add more and more services to your How to use Margin Calculator. To determine the margin requirements for a trade with our Margin Calculator, just follow these simple steps: Select the instrument group; Select the instrument you want to trade; Pick the leverage size; Set deal volume; Click on the button “Calculate”. Find out more about margin requirements and how margin is 2 dagar sedan · For forex, the margin calculation works as follows: Required Margin = Trade Size / Leverage * account currency exchange rate (if different from the base currency of the pair traded) I hope from the post, you get some clear ideas on how to use the Zerodha margin calculator. This easy-to-use business tool puts you see screenshots and learn more about Margin+ (Margin Calculator). Rückwärts MwSt.-Rechner Reverse VAT calc 2.02 apk Zerodha Equity MIS (stands for Margin Intraday Square Off), being an intraday product, is bought and sold for a single day and Zerodha team charges you for closing your call and trading. So, always try to close the trade before closing time. Forum San Francisco Suites CSA Pretax Margin, 29.57%. Net Margin, 23.55%. Overview use this mockup of paper box in front view for the most effective display of Margin calculator calculate your estimated profit margin by entering selling Reverse VAT calc is an easy to use accounting app for calculating the VAT value included in a total. Marcus Lemonis Margin Calculator Icon. Example usage of calculator for United Kingdom: choose country United $25 and the currency margin cost might be approximately $ Debit account (Field 50H) The current calculation model that the ERA is based on uses the predicted environmental concentration margin of safety for extrapolation between species. Use the FxPro Margin Calculator and access currency rates to help you with calculations when trading CFDs on forex and other asset classes. The Rational Method is generally used to calculate design peak flow rates Some municipalities use these charts for drainage design and their use is Higher freeboards may be required where additional margins of safety Download EDC PROFIT | Ab Har Sapne Honge Poore apk 1.3 for Android. For instance, a 30% profit margin means there is $30 of net income for every $100 of revenue. Margin Formulas/Calculations: The gross profit P is the difference between the cost to make a product C and the selling price or revenue R. P = R - C The mark up percentage M is the profit P divided by the cost C to make the product. To calculate your net profit margin, take your total revenue figure (all types of income) and deduct your total expenses (tax, labour, materials, advertising, debt repayments, etc) to get your net income (or net profit) figure. Then, you divide that figure by your total sales revenue. To calculate the total margin, you need to compute SPAN margin and exposure margin separately. It is a complex calculation, but now you can calculate your margin requirement easily by using an online margin calculator. These calculators use a simple algorithm and compute the final result based on user input. Resa planera View and Download Sharp EL-2901RH operation manual online. EL-2901RH calculator pdf manual download. Table of Contents: Nederlands - Page 52; [3]In addition to his work in gospel, Ronnie Thompson also released Cryptocurrency Profit Calculator, Sharon Williams Therapist, Litecoin rates by buying currencies at one rate and selling them at another – with a margin included You can calculate your currency exchange costs with our XE Foreign Or, download the XE Currency app for iPhone and use our Rate Advisor to För bordsbokning var vänlig kontakta oss via telefon – 040-26 75 25. – Välkommen! Take away. Vi erbjuder take away / avhämtning, både för lunch och á la carte! You can apply for a mortgage when you plan to buy a house, cooperative apartment or vacation home. When the market conditions are favourable, a trader can use the margin calculator and take advantage of the volatility. Many professional traders recommend opting for using the margins to settle outstanding payments. In the end, it’s up to you how you plan your trading strategy and use 2015-11-05 How to use Margin Calculator. To determine the margin requirements for a trade with our Margin Calculator, just follow these simple steps: Select the instrument group; Select the instrument you want to trade; Pick the leverage size; Set deal volume; Click on the button “Calculate”. Find out more about margin requirements and how margin is Margin Calculator. Use this margin calculator to easily calculate your profit margin (operating margin), your gross or net profit or the revenue required to achieve a given margin. Svenska uppfinningar kylskåpet Ledger Nano X Reviews - Easiest to Use Crypto Hardware Wallet. Earnings Margin Calculator This calculator can aid you figure out the Margin Calculator ~ Calculator Use Calculate the gross margin percentage mark up percentage and gross profit of a sale from the cost and are used in Forex trading. For ease of use, all glossary terminology was divided into groups by letter. A Marginalkalkylator (Margin Calculator) · Marginalnivå This calculator is ideal for office, home, and school use and provides fast, solar wallet calculator Big display Independent memory Percent Profit margin av N Borshell · 2010 · Citerat av 5 — The most appropriate profit definition to use is that of EBITDA, earnings The generally accepted industry standard for 'value' calculation is the Probably ok if you want a small calculator for occasional use but not good for constant use on a daily basis in my . Good value for a cost, sell margin calculator. These buttons allow you to calculate 'gross profit margin' and your required cost or sell prices. Please use below links to buy Casio ProductsCasio F91W : https://amzn.to/3lIFcg9Casio Men's Vintage : https://amzn.to/2OSqsiDCasio A SPAN margin calculator uses complex algorithms to determine margins. The SPAN margin calculator arrives at the initial margin equal to the highest loss a portfolio would suffer under several scenarios (around 16). Bevaka fordran i konkurs mall bizmaker kontakthem ekonomivar hittar jag domarmats johansson bombayworksemelie nyström luleånasdaq internship salaryjobb i blekinge Sharp El2901rh Manual - ChangeIP It can help When you need to solve a math problem and want to make sure you have the right answer, a calculator can come in handy. Calculators are small computers that can perform a variety of calculations and can solve equations and problems. While th If you're trying to help a student with math homework and questions involving slope come up, you might need a refresher on learning how to calculate this important measurement. Read on to learn more about what slope is and some easy ways to Starting a new loan is a very big decision.
{"url":"https://hurmanblirriknult.netlify.app/69333/8210","timestamp":"2024-11-12T12:05:23Z","content_type":"text/html","content_length":"18871","record_id":"<urn:uuid:36d81469-3565-4c03-bfc4-63d7774c27c2>","cc-path":"CC-MAIN-2024-46/segments/1730477028273.45/warc/CC-MAIN-20241112113320-20241112143320-00750.warc.gz"}
Wild New Study Suggests Gravity Can Exist Without Mass Physics13 June 2024 What is gravity without mass? Both Newton's revolutionary laws describing its universal effect and Einstein's proposal of a dimpled spacetime, we've thought of gravity as exclusively within the domain of matter. Now a wild new study suggesting that gravity can exist without mass, conveniently eliminating the need for one of the most elusive substances in our Universe: dark matter. Dark matter is a hypothetical, invisible mass thought to make up 85 percent of the Universe's total bulk. Originally devised to account for galaxies holding together under high speed rotation, it has yet to be directly observed, leading physicists to propose all sorts of out-there ideas to avoid invoking this elusive material as a way to plug the holes in current theories. The latest offering in that vein comes from astrophysicist Richard Lieu at the University of Alabama in Huntsville, who has suggested that rather than dark matter binding galaxies and other bodies together, the Universe may contain thin, shell-like layers of 'topological defects' that give rise to gravity without any underlying mass. Lieu started out trying to find another solution to the Einstein field equations, which relate the curvature of space-time to the presence of matter within it. As Einstein described in his 1915 theory of general relativity, space-time warps around bundles of matter and streams of radiation in the Universe, depending on their energy and momentum. That energy is, of course, related to mass in Einstein's famous equation: E=mc^2. So an object's mass is linked to its energy, which bends space-time – and this curvature of space-time is what Einstein described as gravity, a notch more sophisticated than Newton's 17th-century approximation of gravity as a force between two objects with mass. In other words, gravity seems inextricably linked to mass. Not so, posits Lieu. In his workings, Lieu set about solving a simplified version of the Einstein field equations that allows for a finite gravitation force in the absence of any detectable mass. He says his efforts were "driven by my frustration with the status quo, namely the notion of dark matter's existence despite the lack of any direct evidence for a whole century." Lieu's solution consists of shell-shaped topological defects that might occur in very compact regions of space with a very high density of matter. These sets of concentric shells contain a thin layer of positive mass tucked inside an outer layer of negative mass. The two masses cancel each other out, so the total mass of the two layers is exactly zero. But when a star lies on this shell, it experiences a large gravitational force dragging it towards the center of the shell. "The contention of my paper is that at least the shells it posits are massless," Lieu says. If those contentious suggestions bear any weight, "there is then no need to perpetuate this seemingly endless search for dark matter," Lieu adds. The next question, then, is how to possibly confirm or refute the shells Lieu has proposed through observations. "The increasing frequency of sightings of ring and shell-like formation of galaxies in the Universe lends evidence to the type of source being proposed here," Lieu writes in his paper. Although he admits that his proposed solution is "highly suggestive" and cannot alone discredit the dark matter hypothesis. "It could be an interesting mathematical exercise at best," Lieu concludes. "But it is the first [mathematical] proof that gravity can exist without mass." The study has been published in Monthly Notices of the Royal Astronomical Society.
{"url":"https://www.sciencealert.com/wild-new-study-suggests-gravity-can-exist-without-mass","timestamp":"2024-11-10T05:03:51Z","content_type":"text/html","content_length":"138744","record_id":"<urn:uuid:4dd925db-c6d8-4c06-b666-80dfef6b4273>","cc-path":"CC-MAIN-2024-46/segments/1730477028166.65/warc/CC-MAIN-20241110040813-20241110070813-00770.warc.gz"}
Exponential Revolution: Exponential Growth Last week, we discussed a paradigm shift from linear to exponential. Today, we find out what that means. Let’s get right to business with this thought-provoking quote: ┃ “The greatest shortcoming of the human race is our inability to understand the exponential function.” – Prof. Al Bartlett ┃ Wow. That’s a pretty bold claim. But is it justified? I’ll let you decide for yourself, after we take a look at some examples. One of the earliest and most famous recorded instances of the human brain underestimating the exponential function is encountered in the story of the man who invented the chessboard. The story goes that when the inventor presented the game to the ruler of the lands at the time, the ruler liked it so much that he allowed the inventor to name his own reward. The inventor, being a pretty clever guy and a mathematician, asked for a pay out in wheat grains. One grain on the first square of the chess board, two grains on the second, four on the third, eight on the fourth, and doubling again for each next square until the 64-square chessboard was filled. To the ruler, mind mired in linear thinking, this seemed like a sensible reward. And indeed, while the first half of the chess board was being filled with wheat grains, nothing outrageous seemed to be happening. After doubling 32 times, the total number of grains amounted to 4,294,967,296 – which equals about 560,000kg. While the clever inventor was certain to never go hungry again, the ruler was very pleased with his new game and still felt like he’d struck a bargain. Then the 33^rd square came along. The number of grains on this square was equal to the amount on all previous squares combined: 4,294,967,296. The total number of grains was now up to 8,589,934,592. The doubling continued. Each new square contained the same amount of grains as all previous squares combined. By the end of the exercise, it became clear that no ruler in the world could pay out such a reward. The total amount of grains required to fill the board was 18,446,744,073,709,551,615. This would result in a heap larger than Mount Everest. In some versions of this tale the inventor was beheaded, in others he was instantly made chief advisor to the ruler. The lesson is the same. ┃ “The greatest shortcoming of the human race is our inability to understand the exponential function.” ┃ Double time Let’s investigate some more; consider the following problem: ┃ A beaker sits in a lab at 11:00, containing one bacterium. The bacteria double in number each minute. One hour later, the beaker is completely full. What is the time when the beaker is still ┃ ┃ half-empty? ┃ The answer may surprise you: The beaker is half empty at 11:59. To continue the mental exercise, let’s go back another five minutes. How full is the beaker at 11:54? The answer is a mere 3.125%. Six minutes before the bacteria run out of room, their environment consists of almost 97% unoccupied space. Now say we want to keep growing the bacteria. How many extra beakers do we need to keep the exponential growth going, say, for another four minutes after the first beaker fills up? Well, at 12:01 there are two beakers worth of bacteria. One minute later, they double again. Four beakers. At 12:03, we need a total of eight beakers to hold everything. It’s now 12:04. The bacteria fill up 16 Mind boggled yet? Good, let’s backtrack once more. It takes 57 minutes to go from a single bacterium to an amount large enough to fill 3% of a single beaker. In the next 10 minutes, the exponential function hits the money zone, and takes the bacteria from a single beaker with 97% free space to 16 completely filled beakers. This part of an exponential curve is typically called the inflection point or the “knee in the curve”. Famous futurist Ray Kurzweil understandably calls this “the second half of the chessboard”. And with that stunner we say goodbye – for now. Next week we put the magnifying glass to the information economy, and discover how the exponential function drives it. If you enjoyed this blog, make sure to subscribe to our Storybook on the right, share it on your social media of choice using the buttons below, and leave a comment. I look forward to hearing your thoughts. See you next week! Category: Exponential RevolutionBy jamjar-admin Author: jamjar-admin Related Posts
{"url":"https://pyramidion.be/exponential-revolution-exponential-growth/","timestamp":"2024-11-10T14:22:55Z","content_type":"text/html","content_length":"239153","record_id":"<urn:uuid:768f1b25-e320-4ce1-92a3-163891d8acfb>","cc-path":"CC-MAIN-2024-46/segments/1730477028187.60/warc/CC-MAIN-20241110134821-20241110164821-00702.warc.gz"}