
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  (c)  William Stein
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass{beamer}
\usepackage{beamerthemesplit}
\usepackage{pgfpages}

%\pgfpagesuselayout{4 on 1}[letterpaper,landscape, border shrink=5mm]
%\setbeamercolor{background canvas}{bg=black!0}

\usepackage{graphicx}
\newcommand{\page}[1]{\frame{#1}}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{graphicx} 
\usepackage{pstricks} 
\usepackage{color}
\definecolor{dbluecolor}{rgb}{0,0,0.6}
\definecolor{dredcolor}{rgb}{.5,0.0,0.0}
\definecolor{dgreencolor}{rgb}{0,0.4,0}
\definecolor{blue}{rgb}{.02, .02, .908}
\newcommand{\dred}{\color{dredcolor}\bf}
\newcommand{\dblue}{\color{dbluecolor}\bf}
\newcommand{\dgreen}{\color{dgreencolor}\bf}
%\newcommand{\dred}{\bf}
%\newcommand{\dblue}{\bf}
%\newcommand{\dgreen}{\bf}

\usepackage{xspace}  % to allow for text macros that don't eat space 
\newcommand{\SAGE}{{\color{blue}\sf SAGE}\xspace }
\newcommand{\sage}{\SAGE}

\newcommand{\graph}[1]{\includegraphics[width=0.45\textwidth]{#1}}

\title{How SAGE Works: Interfaces}
\author{William Stein (University of Washington)}
\date{May 11, 2006: UW SAGE Seminar\vspace{2ex}\\
\includegraphics[width=1.5in]{icon-5}}
\bibliographystyle{amsalpha}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\defn}[1]{{\em #1}}
% ---- SHA ----
\DeclareFontEncoding{OT2}{}{} % to enable usage of cyrillic fonts
  \newcommand{\textcyr}[1]{%
    {\fontencoding{OT2}\fontfamily{wncyr}\fontseries{m}\fontshape{n}%
     \selectfont #1}}
\newcommand{\Sha}{{\mbox{\textcyr{Sh}}}}
\DeclareMathOperator{\rank}{rank}
\DeclareMathOperator{\Vis}{Vis}
\DeclareMathOperator{\HH}{H}
\DeclareMathOperator{\Ker}{Ker}
\DeclareMathOperator{\ord}{ord}
\renewcommand{\H}{\HH}
\newcommand{\hra}{\hookrightarrow}
\newcommand{\isom}{\cong}
\newcommand{\ds}{\displaystyle}
\renewcommand{\t}[1]{\begin{center}{\large{\dblue #1}}\end{center}}


\begin{document}

\page{
\maketitle
}


\begin{frame}
\begin{abstract}
  This talk will be aimed at people who want to understand the
  architecture of \SAGE.  Today I will explain some of the details 
  about how \SAGE interfaces with computer algebra systems and
  how people interface with \sage.
\begin{enumerate}
   \item How \sage{} {\dblue interfaces} with other computer algebra systems.
   \item How \sage{} {\dblue interfaces} with users via their web-browsers.
\end{enumerate}
\end{abstract}
\end{frame}

\begin{frame}
\t{Part I: How \sage{} {\dblue interfaces} with other computer algebra systems.}
\end{frame}

\begin{frame}
\t{Class Hierarchy}
\includegraphics[width=1.1\textwidth]{classdia}

(examples)
\end{frame}

\begin{frame}
\t{Pexpect and Pseudotty's}
\begin{itemize}
\item {\dblue Pseudotty:} A device which appears to an application program
as an ordinary terminal but which is {\em in fact} connected to a
different process.  Pseudo-ttys have a slave half and a control half.

\item {\dblue Pexpect:} {\em makes Python a better tool for controlling
  other applications.}  ({\tt pexpect.sourceforge.net})

  Pexpect is a pure Python module for spawning child applications;
  controlling them; and responding to expected patterns in their
  output.  Pexpect works like Don Libes' Expect. Pexpect allows your
  script to spawn a child application and control it as if a human
  were typing commands.
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\t{Using pexpect: The Basic Idea}
\begin{verbatim}
sage: import pexpect
sage: p = pexpect.spawn('gp --emacs --fast --quiet')
sage: p.maxread = 100000   # crucial for speed!!!
sage: p.expect('\\? ')
0
sage: p.sendline('factor(2006)')
13
sage: p.expect('\\? ')
0
sage: p.before
'factor(2006)\r\n\r\n[2 1]\r\n\r\n[17 1]\r\n\r\n
 [59 1]\r\n\r\n'
sage: p.close()
\end{verbatim}
\end{frame}

\begin{frame}
\t{Difficulties}
\begin{enumerate}
\item Getting subprocesses (and their children!) to quit when you
quit \sage{}.
\item I/O Prompts: make very obscure or embed control codes.
\item Control-C: How to break out of a computation. 
\item Large I/O: use files.
\item Remote servers: Use the network -- will greatly improve 
         this summer (Yi Qiang). 
\item Readline (weird formatting in server); often there are
debug modes, e.g., in Maple; or emacs modes.
\item Support for direct interaction -- coding sprint at Sage Days 1.
\item Unknown number of return values -- Magma.
\end{enumerate}
\end{frame}

\begin{frame}
\t{How to Create a New Interface}
\begin{enumerate}
\item Decide which of the following programs
is most similar to your program: 
gap, gnuplot, magma, maxima, genus2reduction, gp, mwrank, ecm,
kash, maple, octave, singular, gfan, macaulay2, mathematica
\item Copy {\tt sage/interfaces/similarsys.py} and modify
as appropriate.  Use the {\tt log=} option to interface
constructor for debuging. 
\item Figure out what the true, false, equality, etc., symbols
are in your system and code those into the interface.
\item Provide special functionality for tab completion (listing
all defined functions), help on functions, etc. 
\end{enumerate}

\end{frame}

\begin{frame}
\t{How to Improve an Existing Interface}
Let $C$ be a computer algebra system, e.g., Maple,
Macaulay2, etc.
\begin{enumerate}
\item Systematically work through a standard
tutorial for $C$ but using the SAGE interface; 
record anything that is difficult, impossible,
or {\em unnatural} to do.  Add functionality
to interface to remedy these problems. 
\item Try the interface I and if I.[tab] doesn't
give all functions in the system, figure out how
to get them and tell me!
\item Write conversion functions between SAGE objects
and objects in $C$, e.g., matrices.  We need far
more of these!
\end{enumerate}
\end{frame}

\begin{frame}
\t{Part II: Web Browser Interfaces to SAGE}
\begin{enumerate}
\item Simple cgi-bin script interface
\item SAJAX -- an AJAX interface to SAGE
\item SAGE Web Notebook -- brand new local web-server interface
\end{enumerate}
\end{frame}


\begin{frame}
\t{1. Simple cgi-bin script interface}
This is at
{\tt http://modular.math.washington.edu/calc/},
but people have copied it for Kash, MAGMA, etc. 

\begin{enumerate}
\item Started so Keith Conrad's student at UCONN could use
PARI easily for their number theory class.
\item Uses apache2 and some simple Python cgi-bin scripts.
\item Provided way to ``try out MAGMA'' (and variant is
now available at MAGMA website.)
\end{enumerate}
\end{frame}

\begin{frame}[fragile]
\begin{verbatim}
Date: Mon, 19 Dec 2005 16:54:09 -0800
From: "John Cannon" <john@maths.usyd.edu.au>
William,
 
This is to formally advise you that your permission to 
run a general-purpose calculator based on Magma ends on 
Dec 31, 2005. [...]
 
Note that this does NOT affect the use of Magma in your 
modular forms site.  Nor is it likely that we would 
withdraw permission for this use of Magma provided that 
Magma is properly acknowledged (as it has been in the 
past).  In fact we are encouraging the use of Magma as 
part of the backend in similar servers.

Please confirm receipt of this letter.
Wishing you a happy Christmas,
John
\end{verbatim}
\end{frame}


\begin{frame}
\t{2. SAJAX: AJAX interface to SAGE}
Aaron Klem found an AJAX terminal application at
{\tt http://antony.lesuisse.org/qweb/trac/wiki/AjaxTerm}.
It's a stand-alone webserver written with Python combined with
a javascript page.

I modified it to make
  {\tt http://modular.math.washington.edu:8389/}

\begin{enumerate}
\item No authentication -- need chroot.  The AJAX term web
server runs as a normal process.   When a user connects, 
a chroot'd SAGE session starts set to timeout after an 
hour.   
\item Multiple connections at once are allowed. 
\item {\em Rumored} to be unusably slow, though I've
not experienced this (i.e., it's great if the server
is in ``on campus''). 
\item No graphics.  No scrollback.  Cut and paste
is very difficult.
\item Tab completion, history, etc., work well.
\item Hard to setup (because of chroot jail).
\end{enumerate}
\end{frame}

\begin{frame}
\t{3. The SAGE Web Notebook}
Alex Clemesha and I wrote it from scratch as part of SAGE
(on Monday).
It uses that Python has (as part of the standard library!)
a web server built in. 
\begin{itemize}
\item Completely standalone.
\item Python for webserver; some Javascript in the web page. 
\item Not done!  But I already like using it more than command line!
\item Saves all I/O to a disk file whenever possible (if you gave
  optional name='file' argument).
\item Multiple people connect to a single session at
  once.
\item May become the standard SAGE interface; you start a local server
  on your computer (e.g., via Cygwin or VMware under Windows), connect
  to a local web address, and use SAGE.  Or connect to a university
  server and use SAGE; same interface!
\item Completely avoids need to create GUI for SAGE.
\end{itemize}
\end{frame}


\begin{frame}[fragile]
\t{SAGE Web Notebook: TODO List}
{\tiny\begin{verbatim}
   [] Add authentication 
   [] The "move to the current input box" javascript *only* works
      with firefox (not opera, not konqueror); also this should
      just keep the page position where it is rather than move it.
      Moving to a more AJAX-ish model would alternatively fix this, maybe.
   [] A. Clemesha: shrink/expand input/output blocks
   [] A. Clemesha: When hit shift-enter the next text box should be made
      into focus.
   [] Add plain text annotation that is not evaluated
      between blocks (maybe in html?)
      E.g., just make ctrl-enter on a block by HTML-it.
   [] Ability to interrupt running calculations directly
      from the web interface (no console access)
   [] Nice animation while a computation is proceeding.
   [] Some way to show output as it is computed.
   [] Option to delete blocks
   [] Make block expand if enter a lot of text into it.
   [] Evaluate the entire worksheet
   [] Theme-able / skin-able
   [] Embedded graphics from plots;
       also embed png from latexing of math objects (so they look nice).
   [] Downloading and access to exact log of IO to client SAGE process
   [] Save session objects as to log objects so don't have to re-eval?
   [] The entire page is resent/updated every time you hit shift-enter;
      using 'AJAX' this flicker/lag could be completely eliminated.
   [] When pressing shift-enter a line feed is inserted temporarily
      into the inbox, which is unnerving.

Try it and send me some ideas!
\end{verbatim}
}
\end{frame}

\end{document}

