20-CS-4003-001 Organization of Programming Languages Fall 2018

Lambda calculus, Type theory, Formal semantics, Program analysis

    Previous     Next     All lectures        Code    

Successor Producer, Times Consumer

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;

// Stream declaration 
class Stream {
   public Stream () { isNull = true; }
   public boolean isNull;
   public BigInteger first;
   public Stream rest () { return null; }

// Inputs: an integer n and a Stream s of numbers
// Output: the Stream of all tokens of s multiplied by n
class Times extends Stream {
   Stream s1;
   BigInteger multiplier;

   public Times (BigInteger n, Stream s) {
      isNull = s.isNull;
      s1 = s;
      multiplier = n;
      if (!isNull) first = n.multiply(s.first);

   public Times rest() { 
      return new Times (multiplier, s1.rest()); 

// successor declaration 
class Successor extends Stream {

   public Successor (BigInteger num) { 
      first = num;
      isNull = false; 

   public Successor rest () {
      BigInteger one = new BigInteger("1");
      return new Successor (first.add(one));

class Stream_2Frame 
   extends JFrame implements ActionListener {
   JTextArea text;
   JButton go;
   Stream s;

   public Stream_2Frame () {
      setLayout(new BorderLayout());
      setBackground(new Color(255,255,223));
          new JScrollPane(text = new JTextArea()));
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.setBackground(new Color(255,255,223));
      p.add(go = new JButton("Next"));
      add("South", p);
          new JLabel("Successor times 3",JLabel.LEFT));
      s = new Times(new BigInteger("3"),
                    new Successor(new BigInteger("1")));

   public void actionPerformed (ActionEvent evt) {
      int p = text.getDocument().getLength();
      s = s.rest();

public class stream_2 
   extends Applet implements ActionListener {
   JButton go;

   public void init () {
      setLayout(new BorderLayout());
      setBackground(new Color(255,255,223));
      add("Center", go = new JButton("Applet"));

   public void actionPerformed (ActionEvent evt) {
      Stream_2Frame sf = new Stream_2Frame();
 -  A second stream, called Times, is introduced and connects with a Successor stream. Its purpose is to multiply all numbers in the Successor stream by a value that is given to its constructor. In this example, the connection of a Times stream that multiplies by 3 to a Successor stream and becomes the stream s as shown by the red code lines. Walking through s is done as in the previous slide.