\documentclass{article}

\usepackage{verbatim}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{array}
\usepackage{fullpage}

\author{B.~Barras}
\title{Syntax of Coq V8}

%% Le _ est un caractère normal
\catcode`\_=13
\let\subscr=_
\def_{\ifmmode\sb\else\subscr\fi}

\def\bfbar{\ensuremath{|\hskip -0.22em{}|\hskip -0.24em{}|}}
\def\TERMbar{\bfbar}
\def\TERMbarbar{\bfbar\bfbar}
\def\notv{\text{_}}
\def\infx#1{\notv#1\notv}

%% Macros pour les grammaires
\def\GR#1{\text{\large(}#1\text{\large)}}
\def\NT#1{\langle\textit{#1}\rangle}
\def\NTL#1#2{\langle\textit{#1}\rangle_{#2}}
\def\TERM#1{{\bf\textrm{\bf #1}}}
%\def\TERM#1{{\bf\textsf{#1}}}
\def\KWD#1{\TERM{#1}}
\def\ETERM#1{\TERM{#1}}
\def\CHAR#1{\TERM{#1}}

\def\STAR#1{#1*}
\def\STARGR#1{\GR{#1}*}
\def\PLUS#1{#1+}
\def\PLUSGR#1{\GR{#1}+}
\def\OPT#1{#1?}
\def\OPTGR#1{\GR{#1}?}
%% Tableaux de definition de non-terminaux
\newenvironment{cadre}
        {\begin{array}{|c|}\hline\\}
        {\\\\\hline\end{array}}
\newenvironment{rulebox}
        {$$\begin{cadre}\begin{array}{r@{~}c@{~}l@{}l@{}r}}
        {\end{array}\end{cadre}$$}
\def\DEFNT#1{\NT{#1} & ::= &}
\def\EXTNT#1{\NT{#1} & ::= & ... \\&|&}
\def\RNAME#1{(\textsc{#1})}
\def\SEPDEF{\\\\}
\def\nlsep{\\&|&}
\def\nlcont{\\&&}
\newenvironment{rules}
        {\begin{center}\begin{rulebox}}
        {\end{rulebox}\end{center}}

\begin{document}

\maketitle

\section{Meta notations used in this document}

Non-terminals are printed between angle brackets (e.g. $\NT{non-terminal}$) and
terminal symbols are printed in bold font (e.g. $\ETERM{terminal}$). Lexemes
are displayed as non-terminals.

The usual operators on regular expressions:
\begin{center}
\begin{tabular}{l|l}
\hfil notation & \hfil  meaning \\
\hline
$\STAR{regexp}$ & repeat $regexp$ 0 or more times \\
$\PLUS{regexp}$ & repeat $regexp$ 1 or more times \\
$\OPT{regexp}$ & $regexp$ is optional \\
$regexp_1~\mid~regexp_2$ & alternative
\end{tabular}
\end{center}

Parenthesis are used to group regexps. Beware to distinguish this operator
$\GR{~}$ from the terminals $\ETERM{( )}$, and $\mid$ from terminal
\TERMbar.

Rules are optionaly annotated in the right margin with:
\begin{itemize}
\item a precedence and associativity (L for left, R for right and N for no associativity), indicating how to solve conflicts;
  lower levels are tighter;
\item a rule name.
\end{itemize}
In order to solve some conflicts, a non-terminal may be invoked with a
precedence (notation: $\NTL{entry}{prec}$), meaning that rules with higher
precedence do not apply.

\section{Lexical conventions}

Lexical categories are:
\begin{rules}
\DEFNT{ident}
     \STARGR{\NT{letter}\mid\CHAR{_}}
     \STARGR{\NT{letter}\mid \NT{digit} \mid \CHAR{'} \mid \CHAR{_}}
\SEPDEF
\DEFNT{field}       \CHAR{.}\NT{ident}
\SEPDEF
\DEFNT{meta-ident}  \CHAR{?}\NT{ident}
\SEPDEF
\DEFNT{num}         \PLUS{\NT{digit}}
\SEPDEF
\DEFNT{int}         \NT{num} \mid \CHAR{-}\NT{num}
\SEPDEF
\DEFNT{digit}       \CHAR{0}-\CHAR{9}
\SEPDEF
\DEFNT{letter}      \CHAR{a}-\CHAR{z}\mid\CHAR{A}-\CHAR{Z}
                    \mid\NT{unicode-letter}

\SEPDEF
\DEFNT{string}      \CHAR{"}~\STARGR{\CHAR{""}\mid\NT{unicode-char-but-"}}~\CHAR{"}
\end{rules}

Reserved identifiers for the core syntax are:
\begin{quote}
\KWD{as},
\KWD{cofix},
\KWD{else},
\KWD{end},
\KWD{fix},
\KWD{for},
\KWD{forall},
\KWD{fun},
\KWD{if},
\KWD{in},
\KWD{let},
\KWD{match},
\KWD{Prop},
\KWD{return},
\KWD{Set},
\KWD{then},
\KWD{Type},
\KWD{with}
\end{quote}

Symbols used in the core syntax:
$$ \KWD{(}
~~ \KWD{)}
~~ \KWD{\{}
~~ \KWD{\}}
~~ \KWD{:}
~~ \KWD{,}
~~ \Rightarrow
~~ \rightarrow
~~ \KWD{:=}
~~ \KWD{_}
~~ \TERMbar
~~ \KWD{@}
~~ \KWD{\%}
~~ \KWD{.(}
$$

Note that \TERM{struct} is not a reserved identifier.

\section{Syntax of terms}

\subsection{Core syntax}

The main entry point of the term grammar is $\NTL{constr}{9}$. When no
conflict can appear, $\NTL{constr}{200}$ is also used as entry point.

\begin{rules}
\DEFNT{constr}
       \NT{binder-constr}                             &200R~~ &\RNAME{binders}
\nlsep \NT{constr}~\KWD{:}~\NT{constr}                &100R &\RNAME{cast}
\nlsep \NT{constr}~\KWD{:}~\NT{binder-constr}         &100R &\RNAME{cast'}
\nlsep \NT{constr}~\KWD{$\rightarrow$}~\NT{constr}    &80R  &\RNAME{arrow}
\nlsep \NT{constr}~\KWD{$\rightarrow$}~\NT{binder-constr} &80R &\RNAME{arrow'}
\nlsep \NT{constr}~\PLUS{\NT{appl-arg}}               &10L  &\RNAME{apply}
\nlsep \KWD{@}~\NT{reference}~\STAR{\NTL{constr}{9}}  &10L  &\RNAME{expl-apply}
\nlsep \NT{constr}~\KWD{.(}
       ~\NT{reference}~\STAR{\NT{appl-arg}}~\TERM{)}  &1L & \RNAME{proj}
\nlsep \NT{constr}~\KWD{.(}~\TERM{@}
       ~\NT{reference}~\STAR{\NTL{constr}{9}}~\TERM{)} &1L & \RNAME{expl-proj}
\nlsep \NT{constr} ~ \KWD{\%} ~ \NT{ident}            &1L   &\RNAME{scope-chg}
\nlsep \NT{atomic-constr}                             &0
\nlsep \NT{match-expr}                                &0
\nlsep \KWD{(}~\NT{constr}~\KWD{)}                    &0
\SEPDEF
\DEFNT{binder-constr}
       \KWD{forall}~\NT{binder-list}~\KWD{,}~\NTL{constr}{200}
          &&\RNAME{prod}
\nlsep \KWD{fun} ~\NT{binder-list} ~\KWD{$\Rightarrow$}~\NTL{constr}{200}
          &&\RNAME{lambda}
\nlsep \NT{fix-expr}
\nlsep \KWD{let}~\NT{ident-with-params} ~\KWD{:=}~\NTL{constr}{200}
       ~\KWD{in}~\NTL{constr}{200}  &&\RNAME{let}
\nlsep \KWD{let}~\NT{single-fix} ~\KWD{in}~\NTL{constr}{200}
          &&\RNAME{rec-let}
\nlsep \KWD{let}~\KWD{(}~\OPT{\NT{let-pattern}}~\KWD{)}~\OPT{\NT{return-type}}
       ~\KWD{:=}~\NTL{constr}{200}~\KWD{in}~\NTL{constr}{200}
          &&\RNAME{let-case}
\nlsep \KWD{if}~\NT{if-item}
       ~\KWD{then}~\NTL{constr}{200}~\KWD{else}~\NTL{constr}{200}
          &&\RNAME{if-case}
\SEPDEF
\DEFNT{appl-arg}
       \KWD{(}~\NT{ident}~\!\KWD{:=}~\NTL{constr}{200}~\KWD{)}
          &&\RNAME{impl-arg}
\nlsep \KWD{(}~\NT{num}~\!\KWD{:=}~\NTL{constr}{200}~\KWD{)}
          &&\RNAME{impl-arg}
\nlsep \NTL{constr}{9}
\SEPDEF
\DEFNT{atomic-constr}
       \NT{reference}     && \RNAME{variables}
\nlsep \NT{sort}          && \RNAME{CIC-sort}
\nlsep \NT{num}           && \RNAME{number}
\nlsep \KWD{_}            && \RNAME{hole}
\nlsep \NT{meta-ident}    && \RNAME{meta/evar}
\end{rules}

\begin{rules}
\DEFNT{ident-with-params}
       \NT{ident}~\STAR{\NT{binder-let}}~\NT{type-cstr}
\SEPDEF
\DEFNT{binder-list}
       \NT{binder}~\STAR{\NT{binder-let}}
\nlsep \PLUS{\NT{name}}~\KWD{:}~\NT{constr}
\SEPDEF
\DEFNT{binder}
       \NT{name}      &&\RNAME{infer}
\nlsep \KWD{(}~\PLUS{\NT{name}}~\KWD{:}~\NT{constr}
       ~\KWD{)}      &&\RNAME{binder}
\SEPDEF
\DEFNT{binder-let}
       \NT{binder}
\nlsep \KWD{(}~\NT{name}~\NT{type-cstr}~\KWD{:=}~\NT{constr}~\KWD{)}
\SEPDEF
\DEFNT{let-pattern}
       \NT{name}
\nlsep \NT{name} ~\KWD{,} ~\NT{let-pattern}
\SEPDEF
\DEFNT{type-cstr}
       \OPTGR{\KWD{:}~\NT{constr}}
\SEPDEF
\DEFNT{reference}
       \NT{ident}                   && \RNAME{short-ident}
\nlsep \NT{ident}~\PLUS{\NT{field}} && \RNAME{qualid}
\SEPDEF
\DEFNT{sort}
       \KWD{Prop} ~\mid~ \KWD{Set} ~\mid~ \KWD{Type}
\SEPDEF
\DEFNT{name}
       \NT{ident} ~\mid~ \KWD{_}
\end{rules}

\begin{rules}
\DEFNT{fix-expr}
       \NT{single-fix}
\nlsep \NT{single-fix}~\PLUSGR{\KWD{with}~\NT{fix-decl}}
      ~\KWD{for}~\NT{ident}
\SEPDEF
\DEFNT{single-fix}
       \NT{fix-kw}~\NT{fix-decl}
\SEPDEF
\DEFNT{fix-kw} \KWD{fix} ~\mid~ \KWD{cofix}
\SEPDEF
\DEFNT{fix-decl}
       \NT{ident}~\STAR{\NT{binder-let}}~\OPT{\NT{annot}}~\NT{type-cstr}
       ~\KWD{:=}~\NTL{constr}{200}
\SEPDEF
\DEFNT{annot}
       \KWD{\{}~\TERM{struct}~\NT{ident}~\KWD{\}}
\end{rules}

\begin{rules}
\DEFNT{match-expr}
       \KWD{match}~\NT{match-items}~\OPT{\NT{return-type}}~\KWD{with}
  ~\OPT{\TERMbar}~\OPT{\NT{branches}}~\KWD{end}        &&\RNAME{match}
\SEPDEF
\DEFNT{match-items}
       \NT{match-item} ~\KWD{,} ~\NT{match-items}
\nlsep \NT{match-item}
\SEPDEF
\DEFNT{match-item}
       \NTL{constr}{100}~\OPTGR{\KWD{as}~\NT{name}}
       ~\OPTGR{\KWD{in}~\NTL{constr}{100}}
\SEPDEF
\DEFNT{return-type}
       \KWD{return}~\NTL{constr}{100}
\SEPDEF
\DEFNT{if-item}
       \NT{constr}~\OPTGR{\OPTGR{\KWD{as}~\NT{name}}~\NT{return-type}}
\SEPDEF
\DEFNT{branches}
       \NT{eqn}~\TERMbar~\NT{branches}
\nlsep \NT{eqn}
\SEPDEF
\DEFNT{eqn}
       \NT{pattern} ~\STARGR{\KWD{,}~\NT{pattern}}
       ~\KWD{$\Rightarrow$}~\NT{constr}
\SEPDEF
\DEFNT{pattern}
       \NT{reference}~\PLUS{\NT{pattern}}  &1L~~ & \RNAME{constructor}
\nlsep \NT{pattern}~\KWD{as}~\NT{ident}    &1L & \RNAME{alias}
\nlsep \NT{pattern}~\KWD{\%}~\NT{ident}    &1L & \RNAME{scope-change}
\nlsep \NT{reference}                      &0  & \RNAME{pattern-var}
\nlsep \KWD{_}                             &0  & \RNAME{hole}
\nlsep \NT{num}                            &0
\nlsep \KWD{(}~\NT{tuple-pattern}~\KWD{)}
\SEPDEF
\DEFNT{tuple-pattern}
       \NT{pattern}
\nlsep \NT{tuple-pattern}~\KWD{,}~\NT{pattern} && \RNAME{pair}
\end{rules}

\subsection{Notations of the prelude (logic and basic arithmetic)}

Reserved notations:

$$
\begin{array}{l|c}
\text{Symbol} & \text{precedence} \\
\hline
\infx{,}             & 250L \\
\KWD{IF}~\notv~\KWD{then}~\notv~\KWD{else}~\notv
                     & 200R \\
\infx{:}             & 100R \\
\infx{\leftrightarrow} & 95N \\
\infx{\rightarrow}   & 90R \\
\infx{\vee}          & 85R \\
\infx{\wedge}        & 80R \\
\tilde{}\notv        & 75R \\
\begin{array}[c]{@{}l@{}}
 \infx{=}\quad \infx{=}\KWD{$:>$}\notv \quad \infx{=}=\notv
 \quad \infx{\neq} \quad  \infx{\neq}\KWD{$:>$}\notv \\
 \infx{<}\quad\infx{>} \quad \infx{\leq}\quad\infx{\geq}
 \quad \infx{<}<\notv \quad \infx{<}\leq\notv
 \quad \infx{\leq}<\notv \quad \infx{\leq}\leq\notv
\end{array}          & 70N \\
\infx{+}\quad\infx{-}\quad -\notv   & 50L \\
\infx{*}\quad\infx{/}\quad /\notv   & 40L \\
\end{array}
$$

Existential quantifiers follows the \KWD{forall} notation (with same
precedence 200), but only one quantified variable is allowed.

\begin{rules}
\EXTNT{binder-constr}
       \NT{quantifier-kwd}~\NT{name}~\NT{type-cstr}~\KWD{,}~\NTL{constr}{200} \\
\SEPDEF
\DEFNT{quantifier-kwd}
       \TERM{exists} && \RNAME{ex}
\nlsep \TERM{exists2} && \RNAME{ex2}
\end{rules}

$$
\begin{array}{l|c|l}
\text{Symbol} & \text{precedence} \\
\hline
\notv+\{\notv\}                            & 50 & \RNAME{sumor} \\
\{\notv:\notv~|~\notv\}                    & 0 & \RNAME{sig} \\
\{\notv:\notv~|~\notv \& \notv \}          & 0 & \RNAME{sig2} \\
\{\notv:\notv~\&~\notv \}                  & 0 & \RNAME{sigS} \\
\{\notv:\notv~\&~\notv \& \notv \}         & 0 & \RNAME{sigS2} \\
\{\notv\}+\{\notv\}                        & 0 & \RNAME{sumbool} \\
\end{array}
$$

%% Strange: nat + {x:nat|x=x} * nat  == ( + ) *

\section{Grammar of tactics}

\def\tacconstr{\NTL{constr}{9}}
\def\taclconstr{\NTL{constr}{200}}

Additional symbols are:
$$ \TERM{'}
~~ \KWD{;}
~~ \TERM{()}
~~ \TERMbarbar
~~ \TERM{$\vdash$}
~~ \TERM{[}
~~ \TERM{]}
~~ \TERM{$\leftarrow$}
$$
Additional reserved keywords are:
$$ \KWD{at}
~~ \TERM{using}
$$

\subsection{Basic tactics}

\begin{rules}
\DEFNT{simple-tactic}
       \TERM{intros}~\TERM{until}~\NT{quantified-hyp}
\nlsep \TERM{intros}~\NT{intro-patterns}
\nlsep \TERM{intro}~\OPT{\NT{ident}}~\OPTGR{\TERM{after}~\NT{ident}}
%%
\nlsep \TERM{assumption}
\nlsep \TERM{exact}~\tacconstr
%%
\nlsep \TERM{apply}~\NT{constr-with-bindings}
\nlsep \TERM{elim}~\NT{constr-with-bindings}~\OPT{\NT{eliminator}}
\nlsep \TERM{elimtype}~\tacconstr
\nlsep \TERM{case}~\NT{constr-with-bindings}
\nlsep \TERM{casetype}~\tacconstr
\nlsep \KWD{fix}~\OPT{\NT{ident}}~\NT{num}
\nlsep \KWD{fix}~\NT{ident}~\NT{num}~\KWD{with}~\PLUS{\NT{fix-spec}}
\nlsep \KWD{cofix}~\OPT{\NT{ident}}
\nlsep \KWD{cofix}~\NT{ident}~\PLUS{\NT{fix-spec}}
%%
\nlsep \TERM{cut}~\tacconstr
\nlsep \TERM{assert}~\tacconstr
\nlsep \TERM{assert}~
  \TERM{(}~\NT{ident}~\KWD{:}~\taclconstr~\TERM{)}
\nlsep \TERM{assert}~
  \TERM{(}~\NT{ident}~\KWD{:=}~\taclconstr~\TERM{)}
\nlsep \TERM{pose}~\tacconstr
\nlsep \TERM{pose}~
  \TERM{(}~\NT{ident}~\KWD{:=}~\taclconstr~\TERM{)}
\nlsep \TERM{generalize}~\PLUS{\tacconstr}
\nlsep \TERM{generalize}~\TERM{dependent}~\tacconstr
\nlsep \TERM{set}~\tacconstr~\OPT{\NT{clause}}
\nlsep \TERM{set}~
  \TERM{(}~\NT{ident}~\KWD{:=}~\taclconstr~\TERM{)}~\OPT{\NT{clause}}
\nlsep \TERM{instantiate}~
  \TERM{(}~\NT{num}~\TERM{:=}~\taclconstr~\TERM{)}~\OPT{\NT{clause}}
%%
\nlsep \TERM{specialize}~\OPT{\NT{num}}~\NT{constr-with-bindings}
\nlsep \TERM{lapply}~\tacconstr
%%
\nlsep \TERM{simple}~\TERM{induction}~\NT{quantified-hyp}
\nlsep \TERM{induction}~\NT{induction-arg}~\OPT{\NT{with-names}}
       ~\OPT{\NT{eliminator}}
\nlsep \TERM{double}~\TERM{induction}~\NT{quantified-hyp}~\NT{quantified-hyp}
\nlsep \TERM{simple}~\TERM{destruct}~\NT{quantified-hyp}
\nlsep \TERM{destruct}~\NT{induction-arg}~\OPT{\NT{with-names}}
       ~\OPT{\NT{eliminator}}
\nlsep \TERM{decompose}~\TERM{record}~\tacconstr
\nlsep \TERM{decompose}~\TERM{sum}~\tacconstr
\nlsep \TERM{decompose}~\TERM{[}~\PLUS{\NT{reference}}~\TERM{]}
       ~\tacconstr
%%
\nlsep ...
\end{rules}

\begin{rules}
\EXTNT{simple-tactic}
       \TERM{trivial}~\OPT{\NT{hint-bases}}
\nlsep \TERM{auto}~\OPT{\NT{num}}~\OPT{\NT{hint-bases}}
%%
%%\nlsep \TERM{autotdb}~\OPT{\NT{num}}
%%\nlsep \TERM{cdhyp}~\NT{ident}
%%\nlsep \TERM{dhyp}~\NT{ident}
%%\nlsep \TERM{dconcl}
%%\nlsep \TERM{superauto}~\NT{auto-args}
\nlsep \TERM{auto}~\OPT{\NT{num}}~\TERM{decomp}~\OPT{\NT{num}}
%%
\nlsep \TERM{clear}~\PLUS{\NT{ident}}
\nlsep \TERM{clearbody}~\PLUS{\NT{ident}}
\nlsep \TERM{move}~\NT{ident}~\TERM{after}~\NT{ident}
\nlsep \TERM{rename}~\NT{ident}~\TERM{into}~\NT{ident}
%%
\nlsep \TERM{left}~\OPT{\NT{with-binding-list}}
\nlsep \TERM{right}~\OPT{\NT{with-binding-list}}
\nlsep \TERM{split}~\OPT{\NT{with-binding-list}}
\nlsep \TERM{exists}~\OPT{\NT{binding-list}}
\nlsep \TERM{constructor}~\NT{num}~\OPT{\NT{with-binding-list}}
\nlsep \TERM{constructor}~\OPT{\NT{tactic}}
%%
\nlsep \TERM{reflexivity}
\nlsep \TERM{symmetry}~\OPTGR{\KWD{in}~\NT{ident}}
\nlsep \TERM{transitivity}~\tacconstr
%%
\nlsep \NT{inversion-kwd}~\NT{quantified-hyp}~\OPT{\NT{with-names}}~\OPT{\NT{clause}}
\nlsep \TERM{dependent}~\NT{inversion-kwd}~\NT{quantified-hyp}
  ~\OPT{\NT{with-names}}~\OPTGR{\KWD{with}~\tacconstr}
\nlsep \TERM{inversion}~\NT{quantified-hyp}~\TERM{using}~\tacconstr~\OPT{\NT{clause}}
%%
\nlsep \NT{red-expr}~\OPT{\NT{clause}}
\nlsep \TERM{change}~\NT{conversion}~\OPT{\NT{clause}}
\SEPDEF
\DEFNT{red-expr}
       \TERM{red} ~\mid~ \TERM{hnf} ~\mid~ \TERM{compute}
\nlsep \TERM{simpl}~\OPT{\NT{pattern-occ}}
\nlsep \TERM{cbv}~\PLUS{\NT{red-flag}}
\nlsep \TERM{lazy}~\PLUS{\NT{red-flag}}
\nlsep \TERM{unfold}~\NT{unfold-occ}~\STARGR{\KWD{,}~\NT{unfold-occ}}
\nlsep \TERM{fold}~\PLUS{\tacconstr}
\nlsep \TERM{pattern}~\NT{pattern-occ}~\STARGR{\KWD{,}~\NT{pattern-occ}}
\SEPDEF
\DEFNT{conversion}
       \NT{pattern-occ}~\KWD{with}~\tacconstr
\nlsep \tacconstr
\SEPDEF
\DEFNT{inversion-kwd}
       \TERM{inversion} ~\mid~ \TERM{invesion_clear} ~\mid~
       \TERM{simple}~\TERM{inversion}
\end{rules}

Conflicts exists between integers and constrs.

\begin{rules}
\DEFNT{quantified-hyp}
       \NT{int}~\mid~\NT{ident}
\SEPDEF
\DEFNT{induction-arg}
       \NT{int}~\mid~\tacconstr
\SEPDEF
\DEFNT{fix-spec}
       \KWD{(}~\NT{ident}~\STAR{\NT{binder}}~\OPT{\NT{annot}}
       ~\KWD{:}~\taclconstr~\KWD{)}
\SEPDEF
\DEFNT{intro-patterns}
       \STAR{\NT{intro-pattern}}
\SEPDEF
\DEFNT{intro-pattern}
       \NT{name}
\nlsep \TERM{[}~\NT{intro-patterns}~\STARGR{\TERMbar~\NT{intro-patterns}}
       ~\TERM{]}
\nlsep \KWD{(}~\NT{intro-pattern}~\STARGR{\KWD{,}~\NT{intro-pattern}}
       ~\KWD{)}
\SEPDEF
\DEFNT{with-names}
%       \KWD{as}~\TERM{[}~\STAR{\NT{ident}}~\STARGR{\TERMbar~\STAR{\NT{ident}}}
%       ~\TERM{]}
       \KWD{as}~\NT{intro-pattern}
\SEPDEF
\DEFNT{eliminator}
       \TERM{using}~\NT{constr-with-bindings}
\SEPDEF
\DEFNT{constr-with-bindings}
       % dangling ``with'' of ``fix'' can conflict with ``with''
       \tacconstr~\OPT{\NT{with-binding-list}}
\SEPDEF
\DEFNT{with-binding-list}
       \KWD{with}~\NT{binding-list}
\SEPDEF
\DEFNT{binding-list}
       \PLUS{\tacconstr}
\nlsep \PLUS{\NT{simple-binding}}
\SEPDEF
\DEFNT{simple-binding}
       \KWD{(}~\NT{quantified-hyp}~\KWD{:=}~\taclconstr~\KWD{)}
\SEPDEF
\DEFNT{red-flag}
       \TERM{beta} ~\mid~ \TERM{iota} ~\mid~ \TERM{zeta}
       ~\mid~ \TERM{delta} ~\mid~
       \TERM{delta}~\OPT{\TERM{-}}~\TERM{[}~\PLUS{\NT{reference}}~\TERM{]}
\SEPDEF
\DEFNT{clause}
       \KWD{in}~\TERM{*}
\nlsep \KWD{in}~\TERM{*}~\KWD{$\vdash$}~\OPT{\NT{concl-occ}}
\nlsep \KWD{in}~\OPT{\NT{hyp-ident-list}} ~\KWD{$\vdash$} ~\OPT{\NT{concl-occ}}
\nlsep \KWD{in}~\OPT{\NT{hyp-ident-list}}
\SEPDEF
\DEFNT{hyp-ident-list}
       \NT{hyp-ident}
\nlsep \NT{hyp-ident}~\KWD{,}~\NT{hyp-ident-list}
\SEPDEF
\DEFNT{hyp-ident}
       \NT{ident}
\nlsep \KWD{(}~\TERM{type}~\TERM{of}~\NT{ident}~\KWD{)}
\nlsep \KWD{(}~\TERM{value}~\TERM{of}~\NT{ident}~\KWD{)}
\SEPDEF
\DEFNT{concl-occ}
       \TERM{*} ~\NT{occurrences}
\SEPDEF
\DEFNT{pattern-occ}
       \tacconstr ~\NT{occurrences}
\SEPDEF
\DEFNT{unfold-occ}
       \NT{reference}~\NT{occurrences}
\SEPDEF
\DEFNT{occurrences}
       ~\OPTGR{\KWD{at}~\PLUS{\NT{int}}}
\SEPDEF
\DEFNT{hint-bases}
       \KWD{with}~\TERM{*}
\nlsep \KWD{with}~\PLUS{\NT{ident}}
\SEPDEF
\DEFNT{auto-args}
       \OPT{\NT{num}}~\OPTGR{\TERM{adding}~\TERM{[}~\PLUS{\NT{reference}}
       ~\TERM{]}}~\OPT{\TERM{destructuring}}~\OPTGR{\TERM{using}~\TERM{tdb}}
\end{rules}

\subsection{Ltac}

%% Currently, there are conflicts with keyword \KWD{in}: in the following,
%% has the keyword to be associated to \KWD{let} or to tactic \TERM{simpl} ?
%% \begin{center}
%% \texttt{let x := simpl in ...}
%% \end{center}

\begin{rules}
\DEFNT{tactic}
       \NT{tactic} ~\KWD{;} ~\NT{tactic}  &5 &\RNAME{Then}
\nlsep \NT{tactic} ~\KWD{;}~\TERM{[} ~\OPT{\NT{tactic-seq}} ~\TERM{]}
         &5 &\RNAME{Then-seq}
%%
\nlsep \TERM{try} ~\NT{tactic}     &3R &\RNAME{Try}
\nlsep \TERM{do} ~\NT{int-or-var} ~\NT{tactic}
\nlsep \TERM{repeat} ~\NT{tactic}
\nlsep \TERM{progress} ~\NT{tactic}
\nlsep \TERM{info} ~\NT{tactic}
\nlsep \TERM{abstract}~\NTL{tactic}{2}~\OPTGR{\TERM{using}~\NT{ident}}
%%
\nlsep \NT{tactic} ~\TERMbarbar ~\NT{tactic} &2R &\RNAME{Orelse}
%%
\nlsep \KWD{fun} ~\PLUS{\NT{name}} ~\KWD{$\Rightarrow$}
       ~\NT{tactic}  &1 &\RNAME{Fun-tac}
\nlsep \KWD{let} ~\NT{let-clauses} ~\KWD{in} ~\NT{tactic}
\nlsep \KWD{let} ~\TERM{rec} ~\NT{rec-clauses} ~\KWD{in} ~\NT{tactic}
\nlsep \KWD{match}~\OPT{\TERM{reverse}}~\TERM{goal}~\KWD{with}
       ~\OPT{\TERMbar}~\OPT{\NT{match-goal-rules}} ~\KWD{end}
\nlsep \KWD{match} ~\NT{tactic} ~\KWD{with}
       ~\OPT{\TERMbar}~\OPT{\NT{match-rules}} ~\KWD{end}
\nlsep \TERM{first}~\TERM{[} ~\NT{tactic-seq} ~\TERM{]}
\nlsep \TERM{solve}~\TERM{[} ~\NT{tactic-seq} ~\TERM{]}
\nlsep \TERM{idtac}
\nlsep \TERM{fail} ~\OPT{\NT{num}} ~\OPT{\NT{string}}
\nlsep \TERM{constr}~\KWD{:}~\tacconstr
\nlsep \TERM{ipattern}~\KWD{:}~\NT{intro-pattern}
\nlsep \NT{term-ltac}
\nlsep \NT{reference}~\STAR{\NT{tactic-arg}}  &&\RNAME{call-tactic}
\nlsep \NT{simple-tactic}
%%
\nlsep \NT{tactic-atom}  &0 &\RNAME{atomic}
\nlsep \KWD{(} ~\NT{tactic} ~\KWD{)}
\SEPDEF
\DEFNT{tactic-arg}
       \TERM{ltac}~\KWD{:}~\NTL{tactic}{0}
\nlsep \TERM{ipattern}~\KWD{:}~\NT{intro-pattern}
\nlsep \NT{term-ltac}
\nlsep \NT{tactic-atom}
\nlsep \tacconstr
\SEPDEF
\DEFNT{term-ltac}
       \TERM{fresh} ~\OPT{\NT{string}}
\nlsep \TERM{context} ~\NT{ident} ~\TERM{[} ~\taclconstr ~\TERM{]}
\nlsep \TERM{eval} ~\NT{red-expr} ~\KWD{in} ~\tacconstr
\nlsep \TERM{type} ~\tacconstr
\SEPDEF
\DEFNT{tactic-atom}
       \NT{reference}
\nlsep \TERM{()}
\SEPDEF
\DEFNT{tactic-seq}
       \NT{tactic} ~\TERMbar ~\NT{tactic-seq}
\nlsep \NT{tactic}
\end{rules}

\begin{rules}
\DEFNT{let-clauses}
       \NT{let-clause} ~\STARGR{\KWD{with}~\NT{let-clause}}
\SEPDEF
\DEFNT{let-clause}
       \NT{ident} ~\STAR{\NT{name}} ~\KWD{:=} ~\NT{tactic}
\SEPDEF
\DEFNT{rec-clauses}
       \NT{rec-clause} ~\KWD{with} ~\NT{rec-clauses}
\nlsep \NT{rec-clause}
\SEPDEF
\DEFNT{rec-clause}
       \NT{ident} ~\PLUS{\NT{name}} ~\KWD{:=} ~\NT{tactic}
\SEPDEF
\DEFNT{match-goal-rules}
       \NT{match-goal-rule}
\nlsep \NT{match-goal-rule} ~\TERMbar ~\NT{match-goal-rules}
\SEPDEF
\DEFNT{match-goal-rule}
       \NT{match-hyps-list} ~\TERM{$\vdash$} ~\NT{match-pattern}
       ~\KWD{$\Rightarrow$} ~\NT{tactic}
\nlsep \KWD{[}~\NT{match-hyps-list} ~\TERM{$\vdash$} ~\NT{match-pattern}
       ~\KWD{]}~\KWD{$\Rightarrow$} ~\NT{tactic}
\nlsep \KWD{_} ~\KWD{$\Rightarrow$} ~\NT{tactic}
\SEPDEF
\DEFNT{match-hyps-list}
       \NT{match-hyps} ~\KWD{,} ~\NT{match-hyps-list}
\nlsep \NT{match-hyps}
\SEPDEF
\DEFNT{match-hyps}
       \NT{name} ~\KWD{:} ~\NT{match-pattern}
\SEPDEF
\DEFNT{match-rules}
       \NT{match-rule}
\nlsep \NT{match-rule} ~\TERMbar ~\NT{match-rules}
\SEPDEF
\DEFNT{match-rule}
       \NT{match-pattern} ~\KWD{$\Rightarrow$} ~\NT{tactic}
\nlsep \KWD{_} ~\KWD{$\Rightarrow$} ~\NT{tactic}
\SEPDEF
\DEFNT{match-pattern}
       \TERM{context}~\OPT{\NT{ident}}
       ~\TERM{[} ~\NT{constr-pattern} ~\TERM{]}  &&\RNAME{subterm}
\nlsep \NT{constr-pattern}
\SEPDEF
\DEFNT{constr-pattern}
       \tacconstr
\end{rules}

\subsection{Other tactics}

\begin{rules}
\EXTNT{simple-tactic}
       \TERM{rewrite} ~\NT{orient} ~\NT{constr-with-bindings}
         ~\OPTGR{\KWD{in}~\NT{ident}}
\nlsep \TERM{replace} ~\tacconstr ~\KWD{with} ~\tacconstr
         ~\OPTGR{\KWD{in}~\NT{ident}}
\nlsep \TERM{replace} ~\OPT{\NT{orient}} ~\tacconstr
         ~\OPTGR{\KWD{in}~\NT{ident}}
\nlsep \TERM{symplify_eq} ~\OPT{\NT{quantified-hyp}}
\nlsep \TERM{discriminate} ~\OPT{\NT{quantified-hyp}}
\nlsep \TERM{injection} ~\OPT{\NT{quantified-hyp}}
\nlsep \TERM{conditional}~\NT{tactic}~\TERM{rewrite}~\NT{orient}
         ~\NT{constr-with-bindings}~\OPTGR{\KWD{in}~\NT{ident}}
\nlsep \TERM{dependent}~\TERM{rewrite}~\NT{orient}~\NT{ident}
\nlsep \TERM{cutrewrite}~\NT{orient}~\tacconstr
         ~\OPTGR{\KWD{in}~\NT{ident}}
\nlsep \TERM{absurd} ~\tacconstr
\nlsep \TERM{contradiction}
\nlsep \TERM{autorewrite}~\NT{hint-bases}~\OPTGR{\KWD{using}~\NT{tactic}}
\nlsep \TERM{refine}~\tacconstr
\nlsep \TERM{setoid_replace} ~\tacconstr ~\KWD{with} ~\tacconstr
\nlsep \TERM{setoid_rewrite} ~\NT{orient} ~\tacconstr
\nlsep \TERM{subst} ~\STAR{\NT{ident}}
%% eqdecide.ml4
\nlsep \TERM{decide}~\TERM{equality} ~\OPTGR{\tacconstr~\tacconstr}
\nlsep \TERM{compare}~\tacconstr~\tacconstr
%% eauto
\nlsep \TERM{eexact}~\tacconstr
\nlsep \TERM{eapply}~\NT{constr-with-bindings}
\nlsep \TERM{prolog}~\TERM{[}~\STAR{\tacconstr}~\TERM{]}
         ~\NT{quantified-hyp}
\nlsep \TERM{eauto}~\OPT{\NT{quantified-hyp}}~\OPT{\NT{quantified-hyp}}
         ~\NT{hint-bases}
\nlsep \TERM{eautod}~\OPT{\NT{quantified-hyp}}~\OPT{\NT{quantified-hyp}}
         ~\NT{hint-bases}
%% tauto
\nlsep \TERM{tauto}
\nlsep \TERM{simplif}
\nlsep \TERM{intuition}~\OPT{\NTL{tactic}{0}}
\nlsep \TERM{linearintuition}~\OPT{\NT{num}}
%% plugins/cc
\nlsep \TERM{cc}
%% plugins/field
\nlsep \TERM{field}~\STAR{\tacconstr}
%% plugins/firstorder
\nlsep \TERM{ground}~\OPT{\NTL{tactic}{0}}
\nlsep \TERM{ground}~\OPT{\NTL{tactic}{0}}~\KWD{with}~\PLUS{\NT{reference}}
\nlsep \TERM{ground}~\OPT{\NTL{tactic}{0}}~\KWD{using}~\PLUS{\NT{ident}}
%%\nlsep \TERM{gtauto}
\nlsep \TERM{gintuition}~\OPT{\NTL{tactic}{0}}
%% plugins/fourier
\nlsep \TERM{fourierZ}
%% plugins/funind
\nlsep \TERM{functional}~\TERM{induction}~\tacconstr~\PLUS{\tacconstr}
%% plugins/jprover
\nlsep \TERM{jp}~\OPT{\NT{num}}
%% plugins/omega
\nlsep \TERM{omega}
%% plugins/ring
\nlsep \TERM{quote}~\NT{ident}~\OPTGR{\KWD{[}~\PLUS{\NT{ident}}~\KWD{]}}
\nlsep \TERM{ring}~\STAR{\tacconstr}
%% plugins/romega
\nlsep \TERM{romega}
\SEPDEF
\DEFNT{orient}
       \KWD{$\rightarrow$}~\mid~\KWD{$\leftarrow$}
\end{rules}

\section{Grammar of commands}

New symbols:
$$ \TERM{.}
~~ \TERM{..}
~~ \TERM{\tt >->}
~~ \TERM{:$>$}
~~ \TERM{$<$:}
$$

New keyword:
$$ \KWD{where}
$$

\subsection{Classification of commands}

\begin{rules}
\DEFNT{vernac}
       \TERM{Time}~\NT{vernac}  &2~~ &\RNAME{Timing}
%%
\nlsep \NT{gallina}~\TERM{.}    &1
\nlsep \NT{command}~\TERM{.}
\nlsep \NT{syntax}~\TERM{.}
\nlsep \TERM{[}~\PLUS{\NT{vernac}}~\TERM{]}~\TERM{.}
%%
\nlsep \OPTGR{\NT{num}~\KWD{:}}~\NT{subgoal-command}~\TERM{.} ~~~&0
\SEPDEF
\DEFNT{subgoal-command}
       \NT{check-command}
\nlsep %\OPT{\TERM{By}}~
   \NT{tactic}~\OPT{\KWD{..}}
\end{rules}

\subsection{Gallina and extensions}

\begin{rules}
\DEFNT{gallina}
       \NT{thm-token}~\NT{ident}~\STAR{\NT{binder-let}}~\KWD{:}~\NT{constr}
\nlsep \NT{def-token}~\NT{ident}~\NT{def-body}
\nlsep \NT{assum-token}~\NT{assum-list}
\nlsep \NT{finite-token}~\NT{inductive-definition}
      ~\STARGR{\KWD{with}~\NT{inductive-definition}}
\nlsep \TERM{Fixpoint}~\NT{fix-decl}~\STARGR{\KWD{with}~\NT{fix-decl}}
\nlsep \TERM{CoFixpoint}~\NT{fix-decl}~\STARGR{\KWD{with}~\NT{fix-decl}}
\nlsep \TERM{Scheme}~\NT{scheme}~\STARGR{\KWD{with}~\NT{scheme}}
%% Extension: record
\nlsep \NT{record-tok}~\OPT{\TERM{$>$}}~\NT{ident}~\STAR{\NT{binder-let}}
       ~\KWD{:}~\NT{constr}~\KWD{:=}
       ~\OPT{\NT{ident}}~\KWD{\{}~\NT{field-list}~\KWD{\}}
\nlsep \TERM{Ltac}~\NT{ltac-def}~\STARGR{~\TERM{with}~\NT{ltac-def}}
\end{rules}

\begin{rules}
\DEFNT{thm-token}
   \TERM{Theorem} ~\mid~ \TERM{Lemma} ~\mid~ \TERM{Fact} ~\mid~ \TERM{Remark}
\SEPDEF
\DEFNT{def-token}
   \TERM{Definition} ~\mid~ \TERM{Let} ~\mid~
   \OPT{\TERM{Local}}~\TERM{SubClass}
\SEPDEF
\DEFNT{assum-token}
    \TERM{Hypothesis} ~\mid~ \TERM{Variable} ~\mid~ \TERM{Axiom} ~\mid~
    \TERM{Parameter}
\SEPDEF
\DEFNT{finite-token}
    \TERM{Inductive} ~\mid~ \TERM{CoInductive}
\SEPDEF
\DEFNT{record-tok}
    \TERM{Record} ~\mid~ \TERM{Structure}
\end{rules}

\begin{rules}
\DEFNT{def-body}
       \STAR{\NT{binder-let}}~\NT{type-cstr}~\KWD{:=}
       ~\OPT{\NT{reduce}}~\NT{constr}
\nlsep \STAR{\NT{binder-let}}~\KWD{:}~\NT{constr}
\SEPDEF
\DEFNT{reduce}
       \TERM{Eval}~\NT{red-expr}~\KWD{in}
\SEPDEF
\DEFNT{ltac-def}
       \NT{ident}~\STAR{\NT{name}}~\KWD{:=}~\NT{tactic}
\SEPDEF
\DEFNT{rec-definition}
       \NT{fix-decl}~\OPT{\NT{decl-notation}}
\SEPDEF
\DEFNT{inductive-definition}
       \OPT{\NT{string}}~\NT{ident}~\STAR{\NT{binder-let}}~\KWD{:}
       ~\NT{constr}~\KWD{:=}
       ~\OPT{\TERMbar}~\OPT{\NT{constructor-list}}
       ~\OPT{\NT{decl-notation}}
\SEPDEF
\DEFNT{constructor-list}
       \NT{constructor}~\TERMbar~\NT{constructor-list}
\nlsep \NT{constructor}
\SEPDEF
\DEFNT{constructor}
       \NT{ident}~\STAR{\NT{binder-let}}\OPTGR{\NT{coerce-kwd}~\NT{constr}}
\SEPDEF
\DEFNT{decl-notation}
       \TERM{where}~\NT{string}~\TERM{:=}~\NT{constr}
\SEPDEF
\DEFNT{field-list}
       \NT{field}~\KWD{;}~\NT{field-list}
\nlsep \NT{field}
\SEPDEF
\DEFNT{field}
       \NT{ident}~\OPTGR{\NT{coerce-kwd}~\NT{constr}}
\nlsep \NT{ident}~\NT{type-cstr-coe}~\KWD{:=}~\NT{constr}
\SEPDEF
\DEFNT{assum-list}
       \PLUS{\GR{\KWD{(}~\NT{simple-assum-coe}~\KWD{)}}}
\nlsep \NT{simple-assum-coe}
\SEPDEF
\DEFNT{simple-assum-coe}
       \PLUS{\NT{ident}}~\NT{coerce-kwd}~\NT{constr}
\SEPDEF
\DEFNT{coerce-kwd} \TERM{:$>$} ~\mid~ \KWD{:}
\SEPDEF
\DEFNT{type-cstr-coe} \OPTGR{\NT{coerce-kwd}~\NT{constr}}
\SEPDEF
\DEFNT{scheme}
       \NT{ident}~\KWD{:=}~\NT{dep-scheme}~\KWD{for}~\NT{reference}
       ~\TERM{Sort}~\NT{sort}
\SEPDEF
\DEFNT{dep-scheme}
       \TERM{Induction}~\mid~\TERM{Minimality}
\end{rules}

\subsection{Modules and sections}

\begin{rules}
\DEFNT{gallina}
       \TERM{Module}~\NT{ident}~\STAR{\NT{mbinder}}~\OPT{\NT{of-mod-type}}
       ~\OPTGR{\KWD{:=}~\NT{mod-expr}}
\nlsep \TERM{Module}~\KWD{Type}~\NT{ident}~\STAR{\NT{mbinder}}
       ~\OPTGR{\KWD{:=}~\NT{mod-type}}
\nlsep \TERM{Declare}~\TERM{Module}~\NT{ident}~\STAR{\NT{mbinder}}
       ~\OPT{\NT{of-mod-type}}
       ~\OPTGR{\KWD{:=}~\NT{mod-expr}}
\nlsep \TERM{Section}~\NT{ident}
\nlsep \TERM{Chapter}~\NT{ident}
\nlsep \TERM{End}~\NT{ident}
%%
\nlsep \TERM{Require}~\OPT{\NT{export-token}}~\OPT{\NT{specif-token}}
       ~\PLUS{\NT{reference}}
\nlsep \TERM{Require}~\OPT{\NT{export-token}}~\OPT{\NT{specif-token}}
       ~\NT{string}
\nlsep \TERM{Import}~\PLUS{\NT{reference}}
\nlsep \TERM{Export}~\PLUS{\NT{reference}}
\SEPDEF
\DEFNT{export-token}
       \TERM{Import} ~\mid~ \TERM{Export}
\SEPDEF
\DEFNT{specif-token}
       \TERM{Implementation} ~\mid~ \TERM{Specification}
\SEPDEF
\DEFNT{mod-expr}
       \NT{reference}
\nlsep \NT{mod-expr}~\NT{mod-expr} & L
\nlsep \KWD{(}~\NT{mod-expr}~\KWD{)}
\SEPDEF
\DEFNT{mod-type}
       \NT{reference}
\nlsep \NT{mod-type}~\KWD{with}~\NT{with-declaration}
\SEPDEF
\DEFNT{with-declaration}
                                                          %on forcera les ( )
                                                          %si exceptionnellemt
                                                          %un fixpoint ici
       \TERM{Definition}~\NT{ident}~\KWD{:=}~\NTL{constr}{} %{100}
\nlsep \TERM{Module}~\NT{ident}~\KWD{:=}~\NT{reference}
\SEPDEF
\DEFNT{of-mod-type}
       \KWD{:}~\NT{mod-type}
\nlsep \TERM{$<$:}~\NT{mod-type}
\SEPDEF
\DEFNT{mbinder}
       \KWD{(}~\PLUS{\NT{ident}}~\KWD{:}~\NT{mod-type}~\KWD{)}
\end{rules}

\begin{rules}
\DEFNT{gallina}
       \TERM{Transparent}~\PLUS{\NT{reference}}
\nlsep \TERM{Opaque}~\PLUS{\NT{reference}}
\nlsep \TERM{Canonical}~\TERM{Structure}~\NT{reference}~\OPT{\NT{def-body}}
\nlsep \TERM{Coercion}~\OPT{\TERM{Local}}~\NT{reference}~\NT{def-body}
\nlsep \TERM{Coercion}~\OPT{\TERM{Local}}~\NT{reference}~\KWD{:}
       ~\NT{class-rawexpr}~\TERM{$>->$}~\NT{class-rawexpr}
\nlsep \TERM{Identity}~\TERM{Coercion}~\OPT{\TERM{Local}}~\NT{ident}~\KWD{:}
       ~\NT{class-rawexpr}~\TERM{$>->$}~\NT{class-rawexpr}
\nlsep \TERM{Implicit}~\TERM{Arguments}~\NT{reference}~\TERM{[}~\STAR{\NT{num}}~\TERM{]}
\nlsep \TERM{Implicit}~\TERM{Arguments}~\NT{reference}
\nlsep \TERM{Implicit}~\KWD{Type}~\PLUS{\NT{ident}}~\KWD{:}~\NT{constr}
\SEPDEF
\DEFNT{command}
       \TERM{Comments}~\STAR{\NT{comment}}
\nlsep \TERM{Pwd}
\nlsep \TERM{Cd}~\OPT{\NT{string}}
\nlsep \TERM{Drop} ~\mid~ \TERM{ProtectedLoop} ~\mid~\TERM{Quit}
%%
\nlsep \TERM{Load}~\OPT{\TERM{Verbose}}~\NT{ident}
\nlsep \TERM{Load}~\OPT{\TERM{Verbose}}~\NT{string}
\nlsep \TERM{Declare}~\TERM{ML}~\TERM{Module}~\PLUS{\NT{string}}
\nlsep \TERM{Locate}~\NT{locatable}
\nlsep \TERM{Add}~\OPT{\TERM{Rec}}~\TERM{LoadPath}~\NT{string}~\OPT{\NT{as-dirpath}}
\nlsep \TERM{Remove}~\TERM{LoadPath}~\NT{string}
\nlsep \TERM{Add}~\OPT{\TERM{Rec}}~\TERM{ML}~\TERM{Path}~\NT{string}
%%
\nlsep \KWD{Type}~\NT{constr}
\nlsep \TERM{Print}~\NT{printable}
\nlsep \TERM{Print}~\NT{reference}
\nlsep \TERM{Inspect}~\NT{num}
\nlsep \TERM{About}~\NT{reference}
%%
\nlsep \TERM{Search}~\NT{reference}~\OPT{\NT{in-out-modules}}
\nlsep \TERM{SearchPattern}~\NT{constr-pattern}~\OPT{\NT{in-out-modules}}
\nlsep \TERM{SearchRewrite}~\NT{constr-pattern}~\OPT{\NT{in-out-modules}}
\nlsep \TERM{SearchAbout}~\NT{reference}~\OPT{\NT{in-out-modules}}
\nlsep \TERM{SearchAbout}~\TERM{[}~\STAR{\NT{ref-or-string}}~\TERM{]}\OPT{\NT{in-out-modules}}
\nlsep \KWD{Set}~\NT{ident}~\OPT{\NT{opt-value}}
\nlsep \TERM{Unset}~\NT{ident}
\nlsep \KWD{Set}~\NT{ident}~\NT{ident}~\OPT{\NT{opt-value}}
\nlsep \KWD{Set}~\NT{ident}~\NT{ident}~\PLUS{\NT{opt-ref-value}}
\nlsep \TERM{Unset}~\NT{ident}~\NT{ident}~\STAR{\NT{opt-ref-value}}
%%
\nlsep \TERM{Print}~\TERM{Table}~\NT{ident}~\NT{ident}
\nlsep \TERM{Print}~\TERM{Table}~\NT{ident}
\nlsep \TERM{Add}~\NT{ident}~\OPT{\NT{ident}}~\PLUS{\NT{opt-ref-value}}
%%
\nlsep \TERM{Test}~\NT{ident}~\OPT{\NT{ident}}~\STAR{\NT{opt-ref-value}}
%%
\nlsep \TERM{Remove}~\NT{ident}~\OPT{\NT{ident}}~\PLUS{\NT{opt-ref-value}}
\SEPDEF
\DEFNT{check-command}
       \TERM{Eval}~\NT{red-expr}~\KWD{in}~\NT{constr}
\nlsep \TERM{Check}~\NT{constr}
\SEPDEF
\DEFNT{ref-or-string}
       \NT{reference}
\nlsep \NT{string}
\end{rules}

\begin{rules}
\DEFNT{printable}
       \TERM{Term}~\NT{reference}
\nlsep \TERM{All}
\nlsep \TERM{Section}~\NT{reference}
\nlsep \TERM{Grammar}~\NT{ident}
\nlsep \TERM{LoadPath}
\nlsep \TERM{Module}~\OPT{\KWD{Type}}~\NT{reference}
\nlsep \TERM{Modules}
\nlsep \TERM{ML}~\TERM{Path}
\nlsep \TERM{ML}~\TERM{Modules}
\nlsep \TERM{Graph}
\nlsep \TERM{Classes}
\nlsep \TERM{Coercions}
\nlsep \TERM{Coercion}~\TERM{Paths}~\NT{class-rawexpr}~\NT{class-rawexpr}
\nlsep \TERM{Tables}
% \nlsep \TERM{Proof}~\NT{reference}  % Obsolete, useful in V6.3 ??
\nlsep \TERM{Hint}~\OPT{\NT{reference}}
\nlsep \TERM{Hint}~\TERM{*}
\nlsep \TERM{HintDb}~\NT{ident}
\nlsep \TERM{Scopes}
\nlsep \TERM{Scope}~\NT{ident}
\nlsep \TERM{Visibility}~\OPT{\NT{ident}}
\nlsep \TERM{Implicit}~\NT{reference}
\SEPDEF
\DEFNT{class-rawexpr}
       \TERM{Funclass}~\mid~\TERM{Sortclass}~\mid~\NT{reference}
\SEPDEF
\DEFNT{locatable}
       \NT{reference}
\nlsep \TERM{File}~\NT{string}
\nlsep \TERM{Library}~\NT{reference}
\nlsep \NT{string}
\SEPDEF
\DEFNT{opt-value}
       \NT{ident} ~\mid~ \NT{string}
\SEPDEF
\DEFNT{opt-ref-value}
       \NT{reference} ~\mid~ \NT{string}
\SEPDEF
\DEFNT{as-dirpath}
       \KWD{as}~\NT{reference}
\SEPDEF
\DEFNT{in-out-modules}
       \TERM{inside}~\PLUS{\NT{reference}}
\nlsep \TERM{outside}~\PLUS{\NT{reference}}
\SEPDEF
\DEFNT{comment}
       \NT{constr}
\nlsep \NT{string}
\end{rules}

\subsection{Other commands}

%% TODO: min/maj pas a jour
\begin{rules}
\EXTNT{command}
       \TERM{Debug}~\TERM{On}
\nlsep \TERM{Debug}~\TERM{Off}
%% TODO: vernac
\nlsep \TERM{Add}~\TERM{setoid}~\tacconstr~\tacconstr~\tacconstr
\nlsep \TERM{Add}~\TERM{morphism}~\tacconstr~\KWD{:}~\NT{ident}
\nlsep \TERM{Derive}~\TERM{inversion_clear}
         ~\OPT{\NT{num}}~\NT{ident}~\NT{ident}
\nlsep \TERM{Derive}~\TERM{inversion_clear}
         ~\NT{ident}~\KWD{with}~\tacconstr~\OPTGR{\TERM{Sort}~\NT{sort}}
\nlsep \TERM{Derive}~\TERM{inversion}
         ~\OPT{\NT{num}}~\NT{ident}~\NT{ident}
\nlsep \TERM{Derive}~\TERM{inversion}
         ~\NT{ident}~\KWD{with}~\tacconstr~\OPTGR{\TERM{Sort}~\NT{sort}}
\nlsep \TERM{Derive}~\TERM{dependent}~\TERM{inversion_clear}
         ~\NT{ident}~\KWD{with}~\tacconstr~\OPTGR{\TERM{Sort}~\NT{sort}}
\nlsep \TERM{Derive}~\TERM{dependent}~\TERM{inversion}
         ~\NT{ident}~\KWD{with}~\tacconstr~\OPTGR{\TERM{Sort}~\NT{sort}}
%% Correctness: obsolete ?
%\nlsep Correctness
%\nlsep Global Variable
%% TODO: extraction
\nlsep Extraction ...
%% field
\nlsep \TERM{Add}~\TERM{Field}~\tacconstr~\tacconstr~\tacconstr
         ~\tacconstr~\tacconstr~\tacconstr
\nlcont~~~~\tacconstr~\tacconstr~\OPT{\NT{minus-div}}
%% funind
\nlsep \TERM{Functional}~\TERM{Scheme}~\NT{ident}~\KWD{:=}
         ~\TERM{Induction}~\KWD{for}~\tacconstr
         ~\OPTGR{\KWD{with}~\PLUS{\tacconstr}}
%% ring
\nlsep \TERM{Add}~\TERM{Ring}~\tacconstr~\tacconstr~\tacconstr
         ~\tacconstr~\tacconstr~\tacconstr
\nlcont~~~~\tacconstr~\tacconstr~\KWD{[}~\PLUS{\tacconstr}~\KWD{]}
\nlsep \TERM{Add}~\TERM{Semi}~\TERM{Ring}~\tacconstr~\tacconstr~\tacconstr
         ~\tacconstr~\tacconstr~\tacconstr
\nlcont~~~~\tacconstr~\KWD{[}~\PLUS{\tacconstr}~\KWD{]}
\nlsep \TERM{Add}~\TERM{Abstract}~\TERM{Ring}~\tacconstr~\tacconstr~\tacconstr
         ~\tacconstr~\tacconstr~\tacconstr
\nlcont~~~~\tacconstr~\tacconstr
\nlsep \TERM{Add}~\TERM{Abstract}~\TERM{Semi}~\TERM{Ring}~\tacconstr
         ~\tacconstr~\tacconstr~\tacconstr~\tacconstr~\tacconstr
\nlcont~~~~\tacconstr
\nlsep \TERM{Add}~\TERM{Setoid}~\TERM{Ring}~\tacconstr~\tacconstr~\tacconstr
         ~\tacconstr~\tacconstr~\tacconstr
\nlcont~~~~\tacconstr~\tacconstr~\tacconstr~\tacconstr~\tacconstr~\tacconstr
         ~\tacconstr~\KWD{[}~\PLUS{\tacconstr}~\KWD{]}
\nlsep \TERM{Add}~\TERM{Setoid}~\TERM{Semi}~\TERM{Ring}~\tacconstr~\tacconstr
         ~\tacconstr~\tacconstr~\tacconstr~\tacconstr
\nlcont~~~~\tacconstr~\tacconstr~\tacconstr~\tacconstr~\tacconstr
         ~\KWD{[}~\PLUS{tacconstr}~\KWD{]}
\SEPDEF
\DEFNT{minus-div}
       \KWD{with}~\NT{minus-arg}~\NT{div-arg}
\nlsep \KWD{with}~\NT{div-arg}~\NT{minus-arg}
\SEPDEF
\DEFNT{minus-arg}
       \TERM{minus}~\KWD{:=}~\tacconstr
\SEPDEF
\DEFNT{div-arg}
       \TERM{div}~\KWD{:=}~\tacconstr
\end{rules}

\begin{rules}
\EXTNT{command}
       \TERM{Write}~\TERM{State}~\NT{ident}
\nlsep \TERM{Write}~\TERM{State}~\NT{string}
\nlsep \TERM{Restore}~\TERM{State}~\NT{ident}
\nlsep \TERM{Restore}~\TERM{State}~\NT{string}
\nlsep \TERM{Reset}~\NT{ident}
\nlsep \TERM{Reset}~\TERM{Initial}
\nlsep \TERM{Back}~\OPT{\NT{num}}
\end{rules}

\subsection{Proof-editing commands}

\begin{rules}
\EXTNT{command}
       \TERM{Goal}~\NT{constr}
\nlsep \TERM{Proof}~\OPT{\NT{constr}}
\nlsep \TERM{Proof}~\KWD{with}~\NT{tactic}
\nlsep \TERM{Abort}~\OPT{\TERM{All}}
\nlsep \TERM{Abort}~\NT{ident}
\nlsep \TERM{Existential}~\NT{num}~\KWD{:=}~\NT{constr-body}
\nlsep \TERM{Qed}
\nlsep \TERM{Save}~\OPTGR{\NT{thm-token}~\NT{ident}}
\nlsep \TERM{Defined}~\OPT{\NT{ident}}
\nlsep \TERM{Suspend}
\nlsep \TERM{Resume}~\OPT{\NT{ident}}
\nlsep \TERM{Restart}
\nlsep \TERM{Undo}~\OPT{\NT{num}}
\nlsep \TERM{Focus}~\OPT{\NT{num}}
\nlsep \TERM{Unfocus}
\nlsep \TERM{Show}~\OPT{\NT{num}}
\nlsep \TERM{Show}~\TERM{Implicit}~\TERM{Arguments}~\OPT{\NT{num}}
\nlsep \TERM{Show}~\TERM{Node}
\nlsep \TERM{Show}~\TERM{Script}
\nlsep \TERM{Show}~\TERM{Existentials}
\nlsep \TERM{Show}~\TERM{Tree}
\nlsep \TERM{Show}~\TERM{Conjecture}
\nlsep \TERM{Show}~\TERM{Proof}
\nlsep \TERM{Show}~\TERM{Intro}
\nlsep \TERM{Show}~\TERM{Intros}
%% Correctness: obsolete ?
%%\nlsep \TERM{Show}~\TERM{Programs}
\nlsep \TERM{Hint}~\OPT{\TERM{Local}}~\NT{hint}~\OPT{\NT{inbases}}
%% PrintConstr not documented
\end{rules}

\begin{rules}
\DEFNT{constr-body}
       \NT{type-cstr}~\KWD{:=}~\NT{constr}
\SEPDEF
\DEFNT{hint}
       \TERM{Resolve}~\PLUS{\NTL{constr}{9}}
\nlsep \TERM{Immediate}~\PLUS{\NTL{constr}{9}}
\nlsep \TERM{Unfold}~\PLUS{\NT{reference}}
\nlsep \TERM{Constructors}~\PLUS{\NT{reference}}
\nlsep \TERM{Extern}~\NT{num}~\NT{constr}~\KWD{$\Rightarrow$}~\NT{tactic}
\nlsep \TERM{Destruct}~\NT{ident}~\KWD{:=}~\NT{num}~\NT{destruct-loc}
       ~\NT{constr}~\KWD{$\Rightarrow$}~\NT{tactic}
\nlsep \TERM{Rewrite}~\NT{orient}~\PLUS{\NTL{constr}{9}}
       ~\OPTGR{\KWD{using}~\NT{tactic}}
\SEPDEF
\DEFNT{inbases}
       \KWD{:}~\PLUS{\NT{ident}}
\SEPDEF
\DEFNT{destruct-loc}
       \TERM{Conclusion}
\nlsep \OPT{\TERM{Discardable}}~\TERM{Hypothesis}
\end{rules}

\subsection{Syntax extensions}

\begin{rules}
\DEFNT{syntax}
       \TERM{Open}~\TERM{Scope}~\NT{ident}
\nlsep \TERM{Close}~\TERM{Scope}~\NT{ident}
\nlsep \TERM{Delimit}~\TERM{Scope}~\NT{ident}~\KWD{with}~\NT{ident}
\nlsep \TERM{Bind}~\TERM{Scope}~\NT{ident}~\KWD{with}~\PLUS{\NT{class-rawexpr}}
\nlsep \TERM{Arguments}~\TERM{Scope}~\NT{reference}
       ~\TERM{[}~\PLUS{\NT{name}}~\TERM{]}
\nlsep \TERM{Infix}~\OPT{\TERM{Local}} %%% ~\NT{prec}~\OPT{\NT{num}}
       ~\NT{string}~\KWD{:=}~\NT{reference}~\OPT{\NT{modifiers}}
       ~\OPT{\NT{in-scope}}
\nlsep \TERM{Notation}~\OPT{\TERM{Local}}~\NT{string}~\KWD{:=}~\NT{constr}
       ~\OPT{\NT{modifiers}}~\OPT{\NT{in-scope}}
\nlsep \TERM{Notation}~\OPT{\TERM{Local}}~\NT{ident}~\KWD{:=}~\NT{constr}
       ~\OPT{\KWD{(}\TERM{only~\TERM{parsing}\KWD{)}}}
\nlsep \TERM{Reserved}~\TERM{Notation}~\OPT{\TERM{Local}}~\NT{string}
       ~\OPT{\NT{modifiers}}
\nlsep \TERM{Tactic}~\TERM{Notation}~\NT{string}~\STAR{\NT{tac-production}}
       ~\KWD{:=}~\NT{tactic}
\SEPDEF
\DEFNT{modifiers}
       \KWD{(}~\NT{mod-list}~\KWD{)}
\SEPDEF
\DEFNT{mod-list}
       \NT{modifier}
\nlsep \NT{modifier}~\KWD{,}~\NT{mod-list}
\SEPDEF
\DEFNT{modifier}
       \NT{ident}~\KWD{at}~\NT{num}
\nlsep \NT{ident}~\STARGR{\KWD{,}~\NT{ident}}~\KWD{at}~\NT{num}
\nlsep \KWD{at}~\TERM{next}~\TERM{level}
\nlsep \KWD{at}~\TERM{level}~\NT{num}
\nlsep \TERM{left}~\TERM{associativity}
\nlsep \TERM{right}~\TERM{associativity}
\nlsep \TERM{no}~\TERM{associativity}
\nlsep \NT{ident}~\NT{syntax-entry}
\nlsep \TERM{only}~\TERM{parsing}
\nlsep \TERM{format}~\NT{string}
\SEPDEF
\DEFNT{in-scope}
       \KWD{:}~\NT{ident}
\SEPDEF
\DEFNT{syntax-entry}
       \TERM{ident}~\mid~\TERM{global}~\mid~\TERM{bigint}
\SEPDEF
\DEFNT{tac-production}
       \NT{string}
\nlsep \NT{ident}~\TERM{(}~\NT{ident}~\TERM{)}
%%% \SEPDEF
%%% \DEFNT{prec}
%%%       \TERM{LeftA}~\mid~\TERM{RightA}~\mid~\TERM{NonA}
\end{rules}

\end{document}


% Created 2018-12-12 Mi 22:53
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\author{Xinrui Hu}
\date{\today}
\title{2018-12-12-From-Statistics-To-Lack-of-fit-test}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 25.3.2 (Org mode 8.2.10)}}
\begin{document}

\maketitle
\tableofcontents

\section{Background}
\label{sec-1}
According to Alan Turing, a system, which probably looks pretty complicate, can be described with simple mathmatical equation. In machine learning domain, the models can be usually interpreted with equations, and the parameters of "equations" is what we want to learn. Usually, the parameter we finally get is an estimator of the real value. But how do we describe how good the estimator is?

\section{Bias and Variance}
\label{sec-2}

Let's start with the \textbf{Estimator.}

The parameter is a quantity of a real model e.g. the mean or the variance of random variable, the parameter of a probability distribution, etc. An estimator, according to \href{https://en.wikipedia.org/wiki/Estimator}{Wikipedia} is a rule for calculating an estimate of parameter based on observed data. Because the estimator is calculated by a function of observed data, so the estimator is itself also random variable. (This concept is also the main idea of Bayesian Model in machine learning).

Suppose we want to know a paramter $\theta$, which can be estimated by a sample set $D_{S}$ drawn from $X$. $\hat{\theta}_S(X)$ is denoted as the estimator of $\theta$ based on observed data of random variable $X$. Let's denote $x$ as a particular obseved data $X=x$. Sampling is an approximation of the real world. So it could happen that for some reasons the sample set doesn't represent the real issuse very well. There are some important quantified properties which are helpful before we dive deeper.

\subsection{Sampling Deviation}
\label{sec-2-1}
For a given sample $X=x$, the sampling deviation is denoted as:
\begin{equation}
d(x) = \hat{\theta}_S(x)-E(\hat{\theta}_S(X)) = \hat{\theta}_S(x) - E(\hat{\theta})
\end{equation}
where $E(\hat{\theta})$ is the expected value of the estimator. As mentioned, the estimator is itself a random variable.
\subsection{Estimator Bias}
\label{sec-2-2}
We randomly itteratively sample several times. Each time we give an estimate of $\theta$. The bias then can be denoted as:
\begin{equation}
Bias(\hat{\theta}) = E(\hat{\theta})-\theta = E(\hat(\theta)-\theta) =E(error)
\end{equation}
The distance btw. expected value of $\theta$ and the real theta being estimated. Bias can describe how far the average of estimates be off-target.
\subsection{Estimator Variance}
\label{sec-2-3}
The expected value of the squared sampling deviation:
\begin{equation}
Var(\hat{\theta}) = E(\sqrt((\hat{\theta} - E(\hat{\theta}))))
\end{equation}
It describes the scatterness or clusterness of the estimates.

\subsection{Mean Square Error}
\label{sec-2-4}
The expected value of the squared error:
\begin{equation}
MSE(\hat{\theta}) = E[\sqrt((\hat{\theta}(X)-\theta))]
\end{equation}

It is proven that $MSE=\sqrt(Bias) + Var$

\section{*Estimator Variance}
\label{sec-3}
measures how “scatter” our estimator is to sampling, e.g. if we observe the stock price every 100ms instead of every 10ms would the estimator change a lot?

In statistics and machine learning, the \textbf{bias–variance tradeoff} is the property of a set of predictive models whereby models with a lower bias in parameter estimation have a higher variance of the parameter estimates across samples, and vice versa. Ideally, we hope to simultaneously minimize these two sources of error. However, the all-known delimma is that we can't get the "whole" data at all. In oder to optimal the performance we have to consider the trade-off btw. bias and variance. Firsty, what do they actually mean? According to \href{https://en.wikipedia.org/wiki/Bias\%E2\%80\%93variance_tradeoff}{Wikipedia}:

\begin{quote}
\begin{itemize}
\item \textbf{Bias} is an error from erroneous assumptions in the learning algorithm. High bias can cause an algorithm to miss the relevant relations between features and target outputs (underfitting).
\item \textbf{Variance} is an error from sensitivity to small fluctuations in the training set. High variance can cause an algorithm to model the random noise in the training data, rather than the intended outputs(overfitting)
\end{itemize}
\end{quote}
% Emacs 25.3.2 (Org mode 8.2.10)
\end{document}


\documentclass[letterpaper,12pt]{article}
\usepackage{epsfig,latexsym,amsmath,amssymb,epic,eepic,psfrag,subfigure,float,euscript,array}
\usepackage[latin1]{inputenc}
\usepackage{tikz,pgf,pgfplots}
\usepackage[margin=2.5cm]{geometry}
\usepackage[amssymb]{SIunits}
\usepackage{url}
\newenvironment{exercise}[1][Uppgift]{\begin{trivlist} \item[\hskip
    \labelsep {\stepcounter{exerctr}\bfseries #1
      \arabic{exerctr}}]}{\end{trivlist}\vspace{10mm}}

\newcounter{exerctr}
\newcounter{abcctr}[exerctr]

\newcommand{\abc}{\noindent\vspace{1mm}\\ {\bf
    \stepcounter{abcctr}(\alph{abcctr})\ }}
\newcommand{\bbm}{\begin{bmatrix}}
\newcommand{\ebm}{\end{bmatrix}}
\newcommand{\point}[1]{\hfill {\bf (#1p)}\\ \vspace{-5mm}}
\newcommand{\ctrb}{\EuScript{S}}
\newcommand{\Lap}{\mathcal{L}}
\newcommand{\obsv}{\EuScript{O}}
\newcommand{\realdel}[1]{\text{Re}\left\{#1\right\}}
\newcommand{\imagdel}{\text{Im}}
\newcommand{\bC}{\mathbb{C}}
\newcommand{\bR}{\mathbb{R}}
\newcommand{\bmpv}{\begin{minipage}[t]}
\newcommand{\bmps}{\begin{minipage}[t]{45mm}}
\newcommand{\bmpm}{\begin{minipage}[t]{90mm}}
\newcommand{\bmpl}{\begin{minipage}[t]{\textwidth}}
\newcommand{\emp}{\end{minipage}}
\newcommand*{\zethree}{\big(z - \mexp{-3h}\big)}
\newcommand*{\mexp}[1]{\ensuremath{\mathrm{e}^{#1}}}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\addtolength{\topmargin}{-1cm}
\textheight 23.5cm
%\oddsidemargin 0.61cm
%\evensidemargin 0.61cm

\title{Control Computarizado final exam (29\%)}
\author{Kjartan Halvorsen}

\begin{document}

\maketitle

\begin{description}
\item[Time] 2017-11-13 07:00 - 2017-11-14 17:30
\item[Permitted aids] Whatever books, notes, web pages, videos you can find. \textbf{Except} help from another living person. For questions about the exam I can be contacted at \url{kjartan@itesm.mx} or if acute by WhatsApp (556219 4048).
\end{description}

All answers should be readable and well motivated (if nothing else is written). Hand in (on paper) at the time of the class Tuesday November 14 at 17:30.

\begin{center}
{\Large Good luck!} \\
\end{center}

%-----------------------------------------------------------------

\subsubsection*{An inverted pendulum}

\noindent
\begin{minipage}{0.6\linewidth}
The cart-pendulum system is a common example of an unstable system. If we are not concerned with the position of the cart, but only with the angle of the pendulum, then the system can be simplified and written as
\begin{align}
\frac{d}{dt} \bbm \theta\\\dot{\theta} \ebm &= \bbm \dot{\theta}\\\frac{mgl}{J} \sin\theta + \frac{1}{J}\cos\theta u \ebm, \qquad y = \theta,
 \end{align}
where $J$ is the moment of inertia of the pendulum.
\end{minipage}
\begin{minipage}{0.3\linewidth}
\begin{center}
\includegraphics[width=\linewidth]{cart-pendulum.png}\\
{\tiny From the textbook}
\end{center}
\end{minipage}

\subsection*{Problem 1}
%-----------------------------
% Discretization
%-----------------------------

\subsubsection*{(a)}
Show that linearizing the model about the vertical equilibrium state ($\sin\theta \approx \theta$, $\cos\theta \approx 1$) gives the linear state-space system
\begin{equation}
\begin{aligned}
\frac{d}{dt}  \bbm \theta\\\dot{\theta} \ebm &= \bbm 0 & 1\\ \frac{mgl}{J} & 0\ebm \bbm \theta\\\dot{\theta} \ebm + \bbm 0\\\frac{1}{J} \ebm u\\
y &= \bbm 1 & 0 \ebm \bbm \theta\\\dot{\theta} \ebm.
\end{aligned}
\label{eq:sscont}
\end{equation}

\subsubsection*{(b)}
Show that the linear state space model corresponds to the transfer function
\begin{equation}
G(s) = C \big( sI- A\big)^{-1} B = \frac{ 1/J }{s^2 - \frac{mgl}{J}},
\end{equation}
with poles in \(\pm \omega_0\) where \(\omega_0 = \sqrt{\frac{mgl}{J}}\).

\subsubsection*{(c)}
Here, and in the rest of the exam let $J=1$. Show that the zero-order-hold discretization of the state space model \eqref{eq:sscont} gives the discrete-time state space model
\begin{equation}
\begin{aligned}
x(kh+h) &= \underbrace{\bbm \cosh(\omega_0h) & \frac{1}{\omega_0}\sinh(\omega_0h)\\
                            \omega_0\sinh(\omega_0h) & \cosh(\omega_0h) \ebm}_{\Phi} x(kh)
       + \underbrace{\bbm \frac{1}{\omega_0^2}(\cosh(\omega_0h)-1)\\ \
                               \frac{1}{\omega_0}\sinh(\omega_0h) \ebm}_{\Gamma} u(kh)\\
y(kh) &= \underbrace{\bbm 1 & 0 \ebm}_{C} x(kh).
\end{aligned}
\end{equation}

\subsection*{Problem 2}
%-----------------------------
% Feedback design
%-----------------------------

The open-loop discrete-time system has poles in
\[ z = \mexp{\omega_0h} \quad \text{and} \quad z = \mexp{-\omega_0h}.\]
We want to design a controller that gives a closed-loop system with both poles in $z = \mexp{-\omega_0h}$.

\begin{enumerate}
\item Let $\omega_0=1$, choose a reasonable sampling period $h$ (motivate!) and write your state space system with numerical values.
\item Calculate the state feedback gain $L$ in
\[ u(kh) = -l_1 x(kh) - l_2 x(kh) = -L x(kh)\]
so that the closed loop systems has the desired poles.
\item Verify your calculations using matlab. If you don't have matlab on your computer you can use the online-service \url{https://octave-online.net/} which includes a control toolbox. At least, you should calculate the eigenvalued of the closed loop system by \verb!eig(Phi-Gamma*L)!. You can also try to use the functions \verb!acker! or \verb!place!.
\item If the state is not available, we must use an observer to estimate the state. Suggest a reasonable choice of observer poles (numerical values) in the z-plane.
\end{enumerate}

\subsection*{Problem 3}
%-----------------------------
% RST
%-----------------------------
For certain values of $\omega_0$ and $h$ we obtain the following discrete-time model of the inverted pendulum
\[ H(z) = \frac{B(z)}{A(z)} = \frac{0.02(z+1)}{z^2 - 2.04z + 1}. \]
Instead of using state feedback with observer, consider using RST control as in the figure below.

\begin{center}
\begin{tikzpicture}[
    node distance=2cm, block/.style={rectangle, draw, minimum height=15mm, minimum width=20mm}, sumnode/.style={circle, draw, inner sep=1pt}]

  \node[coordinate] (input) {};
  \node[block, right of=input] (TR) {$F_f(z)=\frac{T(z)}{R(z)}$};
  \node[sumnode, right of=TR] (sum) {$\sum$};
  %\node[block,right of=sum, node distance=30mm] (plant) {$H(z)=\frac{B(z)}{A(z)}$};
  \node[block,right of=sum, node distance=30mm] (plant) {$H(z)=\frac{B(z)}{A(z)}$};
  \node[sumnode, right of=plant] (sumdist) {$\sum$};
  \node[coordinate, above of=sumdist, node distance=15mm] (dist) {};
  \node[coordinate, right of=sumdist, node distance=15mm] (measure) {};
  \node[coordinate, right of=measure, node distance=10mm] (output) {};
  \node[sumnode,below of=measure, node distance=25mm] (sumnoise) {$\sum$};
  \node[coordinate, right of=sumnoise, node distance=15mm] (noise) {};
  %\node[block,left of=sumnoise, node distance=20mm] (delay) {$\frac{1}{z^2}$};
  \node[block,left of=sumnoise, node distance=30mm] (SR) {$F_b(z) = \frac{S(z)}{R(z)}$};

  \draw[->] (input) -- node[above] {$y_{ref}$} (TR);
  \draw[->] (TR) -- node[above] {} (sum);
  \draw[->] (sum) -- node[above] {$u$} (plant);
  \draw[->] (plant) -- (sumdist);
  \draw[->] (dist) -- node[at start, right] {$v$} (sumdist);
  \draw[->] (sumdist) -- node[at end, above] {$y$} (output);
  \draw[->] (measure) -- (sumnoise);
  \draw[->] (noise) -- node[at start, above] {$n$} (sumnoise);
  \draw[->] (sumnoise) -- (SR);
  %\draw[->] (delay) -- (SR);
  \draw[->] (SR) -| (sum) node[left, pos=0.96] {$-$};
\end{tikzpicture}
\end{center}

We desire a closed-loop system from the reference signal \(y_{ref}\) to the output \(y\)  with pulse-transfer function
\[H_c(z) = \frac{0.1643(z+1)}{z^2 - 1.637z + 0.6703} \]
The S- and R- polynomials have been found from the Diophantine equation
\[ A(z)R(z) + B(z)S(z) = (z^2 - 1.637z + 0.6703)(z - 0.67)\]
to be
\begin{equation*}
\begin{aligned}
  R(z) &= z - 0.368\\
  S(z) &= 4.98z - 4.076
\end{aligned}
\end{equation*}

Determine the feedforward  (prefilter) part of the controller \(F_f(z) = \frac{T(z)}{R(z)}\) so that the desired closed-loop pulse-transfer function is obtained.

%\end{document}

\section*{Solutions}

\subsection*{Problem 1}
\subsubsection*{(a)}
With the small-angle approximations we get the system of first-order differential equations
\begin{equation*}
\begin{aligned}
\frac{d}{dt}  \bbm \theta\\\dot{\theta} \ebm &= \bbm \dot{\theta}\\ \frac{mgl}{J}\theta + \frac{1}{J}u \ebm, \qquad y = \theta
\end{aligned}
\end{equation*}
which written on matrix-vector form is
\begin{equation*}
\begin{aligned}
\frac{d}{dt}  \bbm \theta\\\dot{\theta} \ebm &= \bbm 0 & 1\\ \frac{mgl}{J} & 0\ebm \bbm \theta\\\dot{\theta} \ebm + \bbm 0\\\frac{1}{J} \ebm u\\
y &= \bbm 1 & 0 \ebm \bbm \theta\\\dot{\theta} \ebm.
\end{aligned}
\end{equation*}

\subsubsection*{(b)}
The expression
\begin{equation*}
G(s) = C \big( sI- A\big)^{-1} B
\end{equation*}
for how to go from a state-space model to a transfer function is obtained by taking the Laplace-transform of the state-space model (this part is not needed to show). Note that
\[ (sI-A)^{-1} = \bbm s & -1\\-\frac{mgl}{J} & s\ebm^{-1} = \frac{1}{s^2 - \frac{mgl}{J}} \bbm s & 1\\\frac{mgl}{J} & s\ebm. \]
Inserting into the expression we get
\begin{equation*}
\begin{aligned}
G(s) = C \big( sI- A\big)^{-1} B = \frac{1}{s^2 - \frac{mgl}{J}}  \bbm 1 & 0 \ebm \bbm s & 1\\\frac{mgl}{J} & s\ebm \bbm 0\\\frac{1}{J}\ebm = \frac{ 1/J }{s^2 - \frac{mgl}{J}}.
\end{aligned}
\end{equation*}
The poles are the roots of the characteristic polynomial \(s^2 - \frac{mgl}{J}\), which clearly are
\(\pm \omega_0\) where \(\omega_0 = \sqrt{\frac{mgl}{J}}\).

\subsubsection*{(c)}

The discrete-time state-space model is given by
\begin{equation*}
\begin{aligned}
  x(kh+h) &= \Phi(h) x(kh+h) + \Gamma(h) u(kh)\\
  y(kh) &= C x(kh),
\end{aligned}
\end{equation*}
where
\[ \Phi(h) = \mexp{Ah}, \qquad \Gamma(h) = \int_0^h \mexp{A\tau} d\tau.\]

We get
\begin{equation*}
\begin{aligned}
  \Phi(h) &= \mexp{Ah} = \Lap^{-1} \{ (sI-A)^{-1}\} =  \Lap^{-1} \{ \bbm s & -1\\ -\omega_0^2 & s\ebm^{-1} \}\\
  &= \Lap^{-1} \{ \frac{1}{s^2 - \omega_0^2} \bbm s & 1\\\omega_0^2 & s\ebm \}
   = \bbm \Lap^{-1} \frac{s}{s^2 - \omega_0^2} & \frac{1}{s^2 - \omega_0^2}\\\frac{\omega_0^2}{s^2 - \omega_0^2} & \frac{s}{s^2 - \omega_0^2}\ebm\\
   &= \bbm \cosh(\omega_0 h) & \frac{1}{\omega_0} \sinh(\omega_0 h)\\\omega_0 \sinh(\omega_0 h) & \cosh(\omega_0 h) \ebm.
 \end{aligned}
\end{equation*}
\begin{equation*}
\begin{aligned}
  \Gamma(h) &= \int_0^h \mexp{A\tau} d\tau = \int_0^h \bbm \frac{1}{\omega_0} \sinh(\omega\tau)\\ \cosh(\omega_0 \tau) \ebm d\tau
  = \bbm  \frac{1}{\omega_0^2} \cosh(\omega_0\tau) \Big|_0^h\\[3mm]  \frac{1}{\omega_0}\sinh(\omega_0\tau) \Big|_0^h\ebm \\
  &= \bbm \frac{1}{\omega_0^2}\big( \cosh(\omega_0 h) - 1 \big)\\ \frac{1}{\omega_0} \sinh(\omega_0h) \ebm.
\end{aligned}
\end{equation*}

\subsection*{Problem 2}

\begin{enumerate}
\item The sampling period should be related to the speed of the closed-loop system, since we have given the desired closed-loop poles. In continuous-time this is a second order critically-damped system with double-pole in $s=-\omega_0=-1$. The step-response of such a system is given by \[ y(t) = 1 - (1+t)\mexp{-t}\]
  \begin{center}
    \begin{tikzpicture}
      \begin{axis}[
        width=0.6\linewidth,
        height = 5cm,
        ]
        \addplot+[no marks, blue!80, domain=-1:5, samples=400] { (x>0)*(1 - exp(-x)*(1+x))};
      \end{axis}
    \end{tikzpicture}
  \end{center}
  The rise-time is about 4 seconds, and using the rule-of-thumb that the number of sampling perdiods should be 4-10, we may for instance choose $h=0.5$. This gives the state-space model
  \begin{equation*}
    \begin{aligned}
      x(k+1) &= \bbm 1.13 & 0.52\\0.52 & 1.13 \ebm x(k) + \bbm 0.13\\0.52 \ebm u(k)\\
      y(k) &= \bbm 1 & 0 \ebm x(k).
    \end{aligned}
  \end{equation*}
\item Calculate the state feedback gain $L$ in
  \[ u(kh) = -l_1 x(kh) - l_2 x(kh) = -L x(kh)\]
  so that the closed loop systems has the desired poles. With the negative state feedback we get a closed-loop system with characteristic polynomial given by
  \begin{equation*}
    \begin{aligned}
      \det \big(zI - (\Phi - \Gamma L)\big) &= \det \left( \bbm z & 0\\ 0 & z\ebm - \left(\bbm 1.13 & 0.52\\0.52 & 1.13 \ebm - \bbm 0.13 l_1 & 0.13 l_2\\ 0.52 l_1 & 0.52 l_2 \ebm\right)\right)\\
      &= \det \bbm z-1.13+0.13l_1 & -0.52+0.13l_2\\ -0.52 + 0.52l_1 & z-1.13+0.52l_2 \ebm\\
      &= (z-1.13+0.13l_1)(z-1.13+0.52l_2) - (-0.52 + 0.52l_1)(-0.52+0.13l_2)\\
      &= z^2 + (0.13l_1 + 0.52l_2 - 2.26)z + 0.1235l_1 - 0.52l_2 + 1.0065.
    \end{aligned}
  \end{equation*}
  Setting each coefficient equal to those of the desired characteristic polynomial \((z-\mexp{-0.5})^2 = (z-0.607)^2 = z^2 - 1.21z + 0.368\) gives the system of equations
  \begin{equation*}
    \begin{aligned}
      \bbm 0.13 & 0.52\\0.1235 & -0.52 \ebm \bbm l_1\\l_2 \ebm = \bbm -1.21 + 2.26\\0.368-1.0065 \ebm
    \end{aligned}
  \end{equation*}
  with solution
  \[ l1 =  1.61 \qquad l2= 1.61 \]
\item Verify your calculations using matlab.
  \begin{verbatim}
omega0 = 1;
h = 0.5;
Phi = [cosh(omega0*h), 1/omega0*sinh(omega0*h)
    omega0*sinh(omega0*h), cosh(omega0*h)]
Gamma = [1/omega0^2*(cosh(omega0*h) - 1); 1/omega0*sinh(omega0*h)]
L = [1.61, 1.61];
eig(Phi-Gamma*L)

ans =

   0.606530659712637
   0.604280024872918
\end{verbatim}
\item The observer poles should be at least twice as fast as the closed-loop poles. Twice as fast means two poles in \(z = \mexp{-2\omega_0 h} = \mexp{-1} \approx 0.368\).
\end{enumerate}

\subsection*{Problem 3}

We only need to determine the polynomial \(T(z)\). The closed-loop transfer function from the reference signal to the output is, according to the block-diagram and using Mason's rule
\begin{equation*}
  \begin{aligned}
    H_c(z) &= \frac{ \frac{T}{R} \frac{B}{A} }{1 + \frac{S}{R} \frac{B}{A}} = \frac{ TB}{AR + BS} = \frac{T(z)B(z)}{A_{cl}(z)}\\
    &= \frac{T(z)0.02(z+1)}{(z^2 - 1.637z + 0.6703)(z-0.67)},
  \end{aligned}
\end{equation*}
which should be equal to the desired closed-loop transfer function
\[H_c(z) = \frac{0.1643(z+1)}{z^2 - 1.637z + 0.6703}. \]
This gives
\[ \frac{T(z)0.02(z+1)}{(z^2 - 1.637z + 0.6703)(z-0.67)} = \frac{0.1643(z+1)}{z^2 - 1.637z + 0.6703}. \]
Solving for \(T(z)\) gives
\[ T(z) = \frac{0.1643}{0.02}(z-0.67).\]

You would arrive at the same result if you identified \((z-0.67)\) as the observer polynomial, and followed the usual route of setting \(T(z) = t_0A_o(z)\) and solving for \(t_0\) so that the closed-loop system have unit static gain.

\end{document}


\documentclass[12pt,letterpaper]{article}
\usepackage{amsmath}
\usepackage{gensymb}
\usepackage{enumitem}
\usepackage{graphicx}
\usepackage[margin=1in]{geometry}
\setlength{\parindent}{0pt}
\title{Physics Club Practice Test 2}
\usepackage{titlesec}
\usepackage[export]{adjustbox}
\titlespacing{\paragraph}{0pt}{0pt}{1em}
% \setenumerate[2]{label={\alph*.}}
\usepackage{fancyhdr}
\fancypagestyle{firstpage}
{
	\renewcommand{\headrulewidth}{0pt}
	\rhead{Version 1.0}
	\cfoot{\thepage}
}
\thispagestyle{firstpage}
\begin{document}
\section*{Physics Club: $F=ma$ Practice Test 2}
Assume the acceleration due to gravity near the surface of the Earth $g = 10$ m/s$^2$.
\smallskip

Correct answers will be awarded one point; incorrect answers will result in a deduction of 1/4 point. There is no penalty for leaving an answer blank.
\smallskip

You may use a scientific calculator. Its memory must be cleared of data and programs.

\begin{enumerate}

\item
Consider a dripping faucet, where the faucet is 15 cm above the sink. The time between drops is such that when one drop hits the sink, one is in the air, and another is about to drop. At what height above the sink will the drop in the air be right as a drop hits the sink?
\begin{enumerate}
\item Between 2 and 4 cm
\item Between 4 and 6 cm
\item Between 6 and 8 cm
\item Between 8 and 10 cm
\item Between 10 and 12 cm
\end{enumerate}

\item
A cannonball is launched with initial velocity of magnitude $v_0$ over a horizontal surface. At what minimal angle $\theta_\text{min}$ above the horizontal should the cannonball be launched so that it rises to a height $H$ which is larger than one half of the horizontal distance $R$ that it will travel when it returns to the ground?
\begin{enumerate}
\item $\theta_\text{min} = 76\degree$
\item $\theta_\text{min} = 70\degree$
\item $\theta_\text{min} = 64\degree$
\item $\theta_\text{min} = 45\degree$
\item There is no such angle $\theta_\text{min}$, as $R>2H$ for all range problems.
\end{enumerate}

\item
An equilateral triangle is sitting on an inclined plane. Friction is too high for it to slide under any circumstance, but if the plane is sloped enough it can ``topple'' down the hill. What angle incline is necessary for it to start toppling?
\begin{enumerate}
\item 30 degrees
\item 45 degrees
\item 60 degrees
\item It will topple at any angle more than zero
\item It can never topple if it cannot slide
\end{enumerate}

\item
A particle at rest explodes into three particles of equal mass in the absence of external forces. Two particles emerge with velocities of the same magnitude $v$ but form an angle of $60\degree$. What is the speed of the third particle?
\begin{enumerate}
\item $v$
\item $\sqrt{3}v$
\item $2v$
\item $2\sqrt{3}v$
\item The third particle can have a range of different speeds.
\end{enumerate}

\item
A 12 kg block moving east at 3 m/s collides head on with a 6 kg block that is moving west at 2 m/s. The two blocks move together after the collision. What is the loss in kinetic energy in this collision?
\begin{enumerate}
\item 36 J
\item 50 J
\item 60 J
\item 72 J
\item 96 J
\end{enumerate}
\end{enumerate}

\textbf{The following information is used for questions X and Y.}\\
Two cannons are arranged vertically, with the lower cannon pointing upward (toward the upper cannon) and the upper cannon pointing downward (toward the lower cannon), 200 m above the lower cannon. Simultaneously, they both fire. The muzzle velocity of the lower cannon is 35 m/s and the muzzle velocity of the upper cannon is 55 m/s.

\begin{enumerate}[resume]
\item
How long after the cannons fire do the projectiles collide?
\begin{enumerate}
\item 2.2 s
\item 2.5 s
\item 3.6 s
\item 6.7 s
\item 8.0 s
\end{enumerate}

\item
How far beneath the top cannon do the projectiles collide?
\begin{enumerate}
\item 31 m
\item 67 m
\item 110 m
\item 150 m
\item 170 m
\end{enumerate}

\item
A block of mass $m = 4.0$ kg is moving on a horizontal surface toward a massless spring with spring constant $k = 80.0$ N/m. The coefficient of kinetic friction between the block and the surface is $\mu_k = 0.50$. The block has speed of 2.0 m/s when it first comes into contact with the spring. How far will the spring be compressed?
\begin{enumerate}
\item 0.19 m
\item 0.24 m
\item 0.32 m
\item 0.45 m
\item 0.61 m
\end{enumerate}

\item
A uniform spherical planet has radius $R$ and the acceleration due to gravity at its surface is $g$. What is the escape velocity of a particle from the planet's surface?
\begin{enumerate}
\item $\frac{1}{2}\sqrt{gR}$
\item $\sqrt{gR}$
\item $\sqrt{2gR}$
\item $2\sqrt{gR}$
\item The escape velocity cannot be expressed in terms of $g$ and $R$ alone.
\end{enumerate}

\item
Four objects are placed at rest at the top of an inclined plane and allowed to roll without slipping to the bottom in absence of rolling resistance and air resistance.\\
Object A is a solid brass ball of diameter $d$.\\
Object B is a solid brass ball of diameter $2d$.\\
Object C is a hollow brass sphere of diameter $d$.\\
Object D is a solid aluminum ball of diameter $d$. (Aluminum is less dense than brass.)\\
The balls are placed so that their centers of mass all travel the same distance. In each case, the time of motion $T$ is measured. Which of the following statements if correct?
\begin{enumerate}
\item $T_\text{B} > T_\text{C} > T_\text{A} = T_\text{D}$
\item $T_\text{A} = T_\text{B} > T_\text{C} = T_\text{D}$
\item $T_\text{B} > T_\text{A} = T_\text{C} = T_\text{D}$
\item $T_\text{A} = T_\text{B} = T_\text{C} = T_\text{D}$
\item $T_\text{C} > T_\text{A} = T_\text{B} = T_\text{D}$
\end{enumerate}

\item
As shown, Lily is using the rope through a fixed pulley to move a box with constant speed $v$. The kinetic friction coefficient between the box and the ground is $\mu <1$; assume that the fixed pulley is massless and there is no friction between the rope and the fixed pulley. Then, while the box is moving, which of the following statements is correct?
\begin{enumerate}
\item The magnitude of the force on the rope is constant.
\item The magnitude of friction between the ground and the box is increasing.
\item The magnitude of the normal force of the ground on the box is decreasing.
\item The pressure of the box on the ground is increasing.
\item The pressure of the box on the ground is constant.
\end{enumerate}

\item
A rigid hoop can rotate about the center. Two massless strings are attached to the hoop, one at A, the other at B. These strings are tied together at the center of the hoop at O, and a weight G is suspended from that point. The strings have a fixed length, regardless of the tension, and the weight G is only supported by the strings. Originally OA is horizontal and OB forms an angle $\theta_0 < 90\degree$ with the horizontal as shown in the diagram.\\

Now, the outer hoop will start to slowly rotate $90\degree$ clockwise until OA will become vertical, while keeping the angle between the strings constant and keeping the object static. Which of the following statements about the tensions $T_1$ and $T_2$ in the two strings is correct?
\begin{enumerate}
\item $T_1$ always decreases.
\item $T_1$ always increases.
\item $T_2$ always decreases.
\item $T_2$ always increases.
\item $T_2$ first increases then decreases.
\end{enumerate}

\item
Shown below is a graph of the $x$ component of force versus position for a 2.0 kg cart constrained to move in one dimension on the $x$ axis. At $x=0$ the cart has a velocity of $-3.0$ m/s (in the negative direction). Which of the following is closest to the maximum speed of the cart?
\begin{enumerate}
\item 5.1 m/s
\item 4.2 m/s
\item 4.0 m/s
\item 3.0 m/s
\item 2.5 m/s
\end{enumerate}

\item
A uniform cylinder of radius $a$ originally has a weight of 80 N. After an off-axis cylinder hole at $2a/5$ was drilled through it, it weighs 54 N. The axes of the two cylinders are parallel and their centers are at the same height.\\

A force $T$ is applied to the top of the cylinder horizontally. In order to keep the cylinder at rest, the magnitude of the force $T$ is closest to
\begin{enumerate}
\item 3 N
\item 5 N
\item 10 N
\item 25 N
\item 35 N
\end{enumerate}

\item
A car of mass $m$ has an engine that provides a constant power output $P$. Assuming no frictional loss of energy, what is the maximum constant speed $v_\text{max}$ that this car can drive up a long incline that makes an angle $\theta$ with the horizontal?
\begin{enumerate}
\item $\displaystyle v_\text{max} = \frac{1}{\sin\theta}\sqrt\frac{2P}{mg}$
\item $\displaystyle v_\text{max} = \frac{P^2\sin\theta}{mg}$
\item $\displaystyle v_\text{max} = \frac{P}{mg\sin\theta}$
\item There is no maximum constant speed.
\item The maximum constant speed depends on the length of the incline.
\end{enumerate}

\item
Inside a cart that is accelerating horizontally at acceleration $a$, there is a block of mass $M$ connected to two light springs of force constants $k_1$ and $k_2$. The block can move without friction horizontally. Find the vibration frequency of the block.
\begin{enumerate}
\item $\displaystyle \frac{1}{2\pi}\sqrt{\frac{k_1+k_2}{M}+a}$
\item $\displaystyle \frac{1}{2\pi}\sqrt{\frac{k_1+k_2}{M}}$
\item $\displaystyle \frac{1}{2\pi}\sqrt{\frac{k_1k_2}{(k_1+k_2)M}}$
\item $\displaystyle \frac{1}{2\pi}\sqrt{\frac{k_1k_2}{(k_1+k_2)M}+a}$
\item $\displaystyle \frac{1}{2\pi}\sqrt{\frac{|k_1-k_2}{M}}$
\end{enumerate}

\item
Shown below is a log-log plot for the data collected of amplitude and period of oscillation for a certain non-linear oscillator.\\

According to the data, the relationship between the period $T$ and the amplitude $A$ is best given by
\begin{enumerate}
\item $T = 100A^3$
\item $T = 1000A^2$
\item $T = 1000A$
\item $T = 2A + 3$
\item The period is independent of the amplitude for oscillating systems.
\end{enumerate}

\item
A mass hangs from the ceiling of a box by an ideal spring. With the box held fixed, the mass is given an initial velocity and oscillates with purely vertical motion. When the mass reaches the lowest point of its motion, the box is released and allowed to fall. To an observer inside the box, which of the following statements is true? Ignore air resistance.
\begin{enumerate}
\item The amplitude of the oscillation decreases.
\item The period of the oscillation increases.
\item The maximum speed reached by the mass increases.
\item The height, measured from the bottom, at which the mass reaches its maximum speed decreases.
\item The maximum height reached by the mass decreases.
\end{enumerate}

\item
A 1500 watt motor is used to pump water a vertical height of 2.0 meters out of a flooded basement through a cylindrical pipe. The water is ejected through the end of the pipe at a speed of 5.0 m/s. Ignoring friction and assuming that all of the energy of the motor goes to the water, which of the following is closest to the radius of the pipe? The density of water is 1000 kg/m$^3$.
\begin{enumerate}
\item 1/3 cm
\item 1 cm
\item 5 cm
\item 10 cm
\item 30 cm
\end{enumerate}

\item
A container of water is sitting on a scale. Originally, the scale reads $M_1 = 45$ kg. A block of wood is suspended from a second scale; originally the scale reads $M_2 = 12$ kg. The density of wood is 0.60 g/cm$^3$; the density of the water is 1.00 g/cm$^3$. The block of wood is lowered into the water until half of the block is beneath the surface. What is the resulting reading on the scales?
\begin{enumerate}
\item $M_1 = 55$ kg and $M_2 = 6$ kg
\item $M_1 = 55$ kg and $M_2 = 2$ kg
\item $M_1 = 45$ kg and $M_2 = 2$ kg
\item $M_1 = 45$ kg and $M_2 = 6$ kg
\item $M_1 = 45$ kg and $M_2 = 10$ kg
\end{enumerate}

\item
A spring system is set up as follows: a platform with a weight of 10 N is on top of two identical springs, each with spring constant 75 N/m. On top of the platform is a third spring with spring constant 75 N/m. If a ball with a weight of 5.0 N is then fastened to the top of the third spring and then slowly lowered, by how much does the height of the spring system change?
\begin{enumerate}
\item 0.033 m
\item 0.067 m
\item 0.100 m
\item 0.133 m
\item 0.600 m
\end{enumerate}

\item
The softest audible sound has an intensity of $I_0 = 10^{-12}$ W/m$^2$. In terms of fundamental units of kilograms, meters, and seconds, this is equivalent to
\begin{enumerate}
\item $I_0 = 10^{-12}$ kg/s
\item $I_0 = 10^{-12}$ kg$^2\,\cdot\,$m/s
\item $I_0 = 10^{-12}$ kg$^2\,\cdot\,$m/s$^2$
\item $I_0 = 10^{-12}$ kg$\,\cdot\,$m$^{-1}\,\cdot\,$s$^{-3}$
\item $I_0 = 10^{-12}$ kg/s$^3$
\end{enumerate}

\item
Which of the following sets of equipment cannot be used to measure the local value of the acceleration due to gravity ($g$)?
\begin{enumerate}
\item A spring scale (which reads in force units) and a known mass
\item A rod of known length, an unknown mass, and a stopwatch
\item A launcher which launches projectiles at a known speed, a projectile of known mass, and meter stick.
\item An inclined plane of known inclination, several carts of different known masses, and a stopwatch
\item A motor with a known output power, a known mass, a piece of string of unknown length, and a stopwatch
\end{enumerate}

\item
Three point masses $m$ are attached together by identical springs. When placed at rest on a horizontal surface the masses form a triangle with side length $l$. When the assembly is rotated about its center at angular velocity $\omega$, the masses form a triangle with side ength $2l$. What is the spring constant $k$ of the sprngs?
\begin{enumerate}
\item $2m\omega^2$
\item $\displaystyle \frac{2}{\sqrt{3}}m\omega^2$
\item $\displaystyle \frac{2}{3}m\omega^2$
\item $\displaystyle \frac{1}{\sqrt{3}}m\omega^2$
\item $\displaystyle \frac{1}{3}m\omega^2$
\end{enumerate}

\item
Consider the two orbits around the Sun shown below. Orbit P is circular with radius R, orbit Q is elliptical such that the farthest point b is between $2R$ and $3R$, and the nearest point a is between $R/3$ and $R/2$. Consider the magnitudes of the velocity of the circular orbit $v_\text{c}$, the velocity of the comet in the elliptical orbit at the farther point $v_\text{b}$, and the velocity of the comet in the elliptical orbit at the nearest point $v_\text{a}$. Which of the following rankings is correct?
\begin{enumerate}
\item $v_\text{b} > v_\text{c} > 2v_\text{a}$
\item $2v_\text{c} > v_\text{b} > v_\text{a}$
\item $10v_\text{b} > v_\text{a} > v_\text{c}$
\item $v_\text{c} > v_\text{a} > 4v_\text{b}$
\item $2v_\text{a} > \sqrt{2}v_\text{b} > v_\text{c}$
\end{enumerate}
%---------------

% \item
% Starting from rest at time $t = 0$, a car moves in a straight line with an acceleration given by the accompanying graph. What is the speed of the car at $t = 2$ s?

% \begin{tabular}{l r}

% \begin{minipage}{0.575\textwidth}
% \begin{enumerate}
% \item 1.0 m/s
% \item 2.0 m/s
% \item 8.0 m/s
% \item 10.5 m/s
% \item 12.5 m/s
% \end{enumerate}
% \end{minipage} &
% \begin{minipage}{0.4\textwidth}
% \includegraphics[width=0.65\textwidth,center]{agraph.png}
% \end{minipage}
% \end{tabular}
% \end{enumerate}

% \textbf{The following information is used for questions 2 and 3.}\\
% A flare is dropped from a plane flying over level ground at a velocity of 70 m/s in the horizontal direction. At the instant the flare is released, the plane begins to accelerate horizontally at 1.75 m/s$^2$. The flare takes 4.5 s to reach the ground.

% \begin{enumerate}[resume]
% \item
% Relative to a spot directly under the flare at release, the flare lands
% \begin{enumerate}
% \item directly on the spot.
% \item 333 m in front of the spot.
% \item 274 m in front of the spot.
% \item 280 m in front of the spot.
% \item 315 m in front of the spot.
% \end{enumerate}

% \vfill
% \newpage

% \item
% Which solid vector in the accompanying figures best represents the acceleration of the pendulum mass at the intermediate point in its swing indicated?

% \includegraphics[width=0.9\textwidth,center]{swing.png}

\end{enumerate}

\end{document}


%%
%% Automatically generated file from DocOnce source
%% (https://github.com/hplgit/doconce/)
%%
%%
% #ifdef PTEX2TEX_EXPLANATION
%%
%% The file follows the ptex2tex extended LaTeX format, see
%% ptex2tex: http://code.google.com/p/ptex2tex/
%%
%% Run
%%      ptex2tex myfile
%% or
%%      doconce ptex2tex myfile
%%
%% to turn myfile.p.tex into an ordinary LaTeX file myfile.tex.
%% (The ptex2tex program: http://code.google.com/p/ptex2tex)
%% Many preprocess options can be added to ptex2tex or doconce ptex2tex
%%
%%      ptex2tex -DMINTED myfile
%%      doconce ptex2tex myfile envir=minted
%%
%% ptex2tex will typeset code environments according to a global or local
%% .ptex2tex.cfg configure file. doconce ptex2tex will typeset code
%% according to options on the command line (just type doconce ptex2tex to
%% see examples). If doconce ptex2tex has envir=minted, it enables the
%% minted style without needing -DMINTED.
% #endif

% #define PREAMBLE

% #ifdef PREAMBLE
%-------------------- begin preamble ----------------------

\documentclass[%
oneside,                 % oneside: electronic viewing, twoside: printing
final,                   % draft: marks overfull hboxes, figures with paths
10pt]{article}

\listfiles               %  print all files needed to compile this document

\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb}
\usepackage[table]{xcolor}
\usepackage{bm,ltablex,microtype}

\usepackage[pdftex]{graphicx}

\usepackage{ptex2tex}
% #ifdef MINTED
\usepackage{minted}
\usemintedstyle{default}
% #endif
\usepackage{fancyvrb}

\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}

\usepackage{lmodern}         % Latin Modern fonts derived from Computer Modern

% Hyperlinks in PDF:
\definecolor{linkcolor}{rgb}{0,0,0.4}
\usepackage{hyperref}
\hypersetup{
    breaklinks=true,
    colorlinks=true,
    linkcolor=linkcolor,
    urlcolor=linkcolor,
    citecolor=black,
    filecolor=black,
    %filecolor=blue,
    pdfmenubar=true,
    pdftoolbar=true,
    bookmarksdepth=3   % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC
    }
%\hyperbaseurl{}   % hyperlinks are relative to this root

\setcounter{tocdepth}{2}  % levels in table of contents

% prevent orhpans and widows
\clubpenalty = 10000
\widowpenalty = 10000

% --- end of standard preamble for documents ---

% insert custom LaTeX commands...

\raggedbottom
\makeindex
\usepackage[totoc]{idxlayout}   % for index in the toc
\usepackage[nottoc]{tocbibind}  % for references/bibliography in the toc

%-------------------- end preamble ----------------------

\begin{document}

% matching end for #ifdef PREAMBLE
% #endif

\newcommand{\exercisesection}[1]{\subsection*{#1}}

% ------------------- main content ----------------------

% ----------------- title -------------------------

\thispagestyle{empty}

\begin{center}
{\LARGE\bf
\begin{spacing}{1.25}
Second week
\end{spacing}
}
\end{center}

% ----------------- author(s) -------------------------

\begin{center}
{\bf Data Analysis and Machine Learning${}^{}$} \\ [0mm]
\end{center}

\begin{center}
% List of all institutions:
\end{center}

% ----------------- end author(s) -------------------------

% --- begin date ---
\begin{center}
May 29, 2020
\end{center}
% --- end date ---

\vspace{1cm}

\subsection{Regression examples, from linear regression, via decision trees and various forests to neural networks}

The main aim of this project is to study some specific
regression problems, starting with the regression algorithms studied
in homework set 3 (exercise 2 in particular).
We will include decision trees, random forests and eventually boosting
methods  and neural network with \textbf{tensorflow} (feel free however to write your own code).

The case we encounter here
is  the so-called Ising model for  our training data and we will
focus on supervised training. We will follow closely the recent
article of \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv
1803.08823}. This article stands
out as an excellent review on machine learning (ML) algorithms.
The added benefit is that each figure and
model presented in \href{{https://physics.bu.edu/~pankajm/MLnotebooks.html}}{this article is accompanied by its jupyter
notebook}. This
means that we can start using these and compare with our own results.

You can also look up the \href{{https://compphysics.github.io/MachineLearningMSU-FRIB2020/doc/pub/Regression/html/Regression.html}}{Regression slides for a discussion of the Ising model} (scroll down to the end).

Alternatively, you can replace the Ising thorughout the exercises with the nuclear binding energies. The choice is yours. Or if you have other data sets suitable for regression, feel free to use those.

What follows here is however a discussion of the Ising model. The nuclear binding energies were discussed during the lectures.

With the abovementioned configurations we will determine, using first
various regression methods, the value of the coupling constant for the
energy of the one-dimensional Ising model.
We will mainly use \textbf{scikit-learn} or \textbf{tensorflow} or other Python packages such as \textbf{keras} or other.

Feel free to use the notebooks to benchmark your code.

\paragraph{Part a): Producing the data for the one-dimensional Ising model.}
The model we will employ in our studies is the so-called \href{{https://en.wikipedia.org/wiki/Ising_model}}{Ising
model}.  Together with
models like the \href{{https://en.wikipedia.org/wiki/Potts_model}}{Potts
model} and similar
so-called lattice models, the Ising model has been widely studied in
mathematics (in statistics in particular), physics, \href{{https://journals.aps.org/pre/abstract/10.1103/PhysRevE.93.062402}}{life
science},
chemistry and even in the \href{{https://www.springer.com/gp/book/9781461420316}}{social sciences in order to model social
behavior}. It is a
simple binary value system where the variables of the model (spins often in
physics) can take two values only, for example $\pm 1$ or $0$ and $1$.
The system exhibits a phase transition in two or higher dimensions and
the first person to find the analytical expressions for various
expectation values was the Norwegian chemist \href{{https://en.wikipedia.org/wiki/Lars_Onsager}}{Lars
Onsager} (Nobel prize in
chemistry) after a tour de force mathematics exercise.

In our discussions here we will stay with a physicist's approach and
call the variables for spin. You could replace this with any other
type of binary variables, ranging from a two political parties to blue
and red spheres.  In its simplest form we define the energy of the
system as

\begin{equation*}
  E=-J\sum_{<kl>}^{N}s_ks_l,
\end{equation*}
with  $s_k=\pm 1$, $N$ is the total number of spins,
$J$ is a coupling constant expressing the strength of the interaction
between neighboring spins.

The symbol $<kl>$ indicates that we sum over nearest
neighbors only.
Notice that for $J>0$ it is energetically favorable for neighboring spins
to be aligned. This feature leads to, at low enough temperatures,
a cooperative phenomenon called spontaneous magnetization. That is,
through interactions between nearest neighbors, a given magnetic
moment can influence the alignment of spins  that are separated
from the given spin by a macroscopic distance. These long range correlations
between spins are associated with a long-range order in which
the lattice has a net magnetization in the absence of a magnetic field.

We start by considering the one-dimensional Ising model with nearest neighbor interactions. This model does not exhibit any phase transition.

Consider the 1D Ising model with nearest-neighbor interactions

\begin{equation*}
  E[\hat{s}]=-J\sum_{j=1}^{N}s_{j}s_{j+1},
\end{equation*}

on a chain of length $N$ with so-called periodic boundary conditions and $S_j=\pm 1$ Ising spin variables.
In one dimension, this model has no phase transition at finite temperature.

In the Python code below we generate, with a coupling coefficient set to $J=1$, a large number of spin configurations say $10000$ as shown in the code below.
It means that our data will be a set of $i=1\ldots n$ points of the form
$\{(E[\boldsymbol{s}^i],\boldsymbol{s}^i)\}$.
Our task is to find the value of $J$ from the data set using linear regression.

Here is the Python code you need to generate the training data, see
also the \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html}}{notebook of Mehta et
al}.

\bpycod
import numpy as np
import scipy.sparse as sp
np.random.seed(12)

import warnings
#Comment this to turn on warnings
warnings.filterwarnings('ignore')

### define Ising model aprams
# system size
L=40

# create 10000 random Ising states
states=np.random.choice([-1, 1], size=(10000,L))

def ising_energies(states,L):
    """
    This function calculates the energies of the states in the nn Ising Hamiltonian
    """
    J=np.zeros((L,L),)
    for i in range(L):
        J[i,(i+1)%L]-=1.0
    # compute energies
    E = np.einsum('...i,ij,...j->...',states,J,states)

    return E
# calculate Ising energies
energies=ising_energies(states,L)
\epycod

We can now recast the problem as a linear regression model using our codes from homework set 3 (exercise 2 in particular).
The way we are going to build our model mimicks the way we could think of finding say the gravitional constant for the graviational force between two planets.
In the absence of any prior knowledge, one sensible choice is the all-to-all Ising model

\[
E_\mathrm{model}[\boldsymbol{s}^i] = - \sum_{j=1}^N \sum_{k=1}^N J_{j,k}s_{j}^is_{k}^i.
\]

Here $i$ represents a particular spin configuration (one of the possible $n$ configurations we generated with the code above).

This model is uniquely defined by the non-local coupling strengths $J_{jk}$ which we want to learn.
The model is linear in $\mathbf{J}$ which makes it possible to use linear regression.

To apply linear regression, we recast this model in the form
\[
E_\mathrm{model}^i \equiv \mathbf{X}^i \cdot \mathbf{J},
\]

where the vectors $\mathbf{X}^i$ represent all two-body interactions
$\{s_{j}^is_{k}^i \}_{j,k=1}^N$, and the index $i$ runs over the
samples in the data set.  To make the analogy complete, we can also
represent the dot product by a single index $p = \{j,k\}$,
i.e.~$\mathbf{X}^i \cdot \mathbf{J}=X^i_pJ_p$.  Note that the
regression model does not include the minus sign, so we expect to
learn negative $J$'s.

\paragraph{Part b): Estimating the coupling constant of the one-dimensional Ising model using linear regression.}
We start with the one-dimensional Ising model and use the data we have
generated with $J=1$ in the previous point.

\textbf{Use linear regression,
Lasso and Ridge regression as done in homework 3}.
Make an analysis of the guessed coupling constant as function of the hyperparameters $\lambda$.
You can compare your
results with those of \href{{https://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB_CVI-linreg_ising.html}}{Mehta
et al.}.
Make sure it is the 1D data which is used.

Discuss the methods and how they perform in computing the coupling
constant $J$ and include a bias-variance analysis using the bootstrap resampling method.
Discuss also the mean squared error and
the $R2$ score as measures to assess your model.

Give a critical analysis of your results.

You can replace the Ising model data with the nuclear binding energy data.

\paragraph{Part c): Random forests and boosting.}
Repeat the above analysis but using random forests and boosting (XGboost or normal gradient boosting). You can use the functions
in \textbf{scikit-learn} for random forests and gradient boosting. For \textbf{XGBoost} you need to install it separately. You can still use other functionality in \textbf{scikit-Learn}.

\paragraph{Part d): Regression analysis of the one-dimensional Ising model using neural networks.}
Your aim now is to use either \textbf{scikit-learn} or \textbf{tensorflow} in order to set up a neural network to find the optimal weights and biases.

Train your network and compare the results with those from your linear regression code and random forests/boosting methods.

You can test your results against a similar code using \textbf{scikit-learn} or \textbf{tensorflow/keras}.

A useful reference on the back progagation algorithm is \href{{http://neuralnetworksanddeeplearning.com/}}{Nielsen's book}. It is an excellent read.

Finally, give a critical analysis of your results with pros and cons of the various methods.

\subsection{Checklist for handling data, Machine Learning cheat sheet}

This is a short cheat sheet for doing machine learning
experiments. It'll discuss data exploration the modelling pipeline and
model validation in short with links to external resources for
reference

\paragraph{Data Exploration.}
When modelling with machine learning it's easy to just present your
data to a model, while this has the odd chance of working more likely
this will give you a very weak model.

The first step of the machine learning process is then to look at your
data, and your chosen representation of the data.

\paragraph{Visualization.}
Depending on your data, if it's sequential, real-continious or image
based your data should be visualized to get a reference for the
distribution of data. In particular for representations of continious
or discreet variables visualizing ditributions can be hugely helpful
to determining the style of normalization to apply. In short: look at
your data.

\paragraph{Normalizing.}
After inspecting your data distribution(s) you should consider what
standardization techniques to apply. In particular you need to think
about if you need the covariance matrix of your data to be unchanged
under the normalization. Mean centering doesn't change your covariance
matrix, standardization of the variance does.

Also consider if your features are on the same scale. Your model might
be in trouble if one feature is on the order $~10^0$ and a second is
on the order $~10^3$.

\paragraph{Prototyping.}
It's likely that the first version of your chosen model, or even that
the model type or representation might provide unsuitable for the
problem at hand. A fundamental part of the process then is exploring
representations, and model types that might be suitable. The \texttt{keras}
API and \texttt{scikit-learn} off-the-shelf algorithms are fantastic for this
purpose.

Remember to keep your model assumptions in mind
The following links are useful \href{{https://sebastianraschka.com/Articles/2014_about_feature_scaling.html}}{\nolinkurl{https://sebastianraschka.com/Articles/2014_about_feature_scaling.html}} and  \href{{https://medium.com/greyatom/why-how-and-when-to-scale-your-features-4b30ab09db5e}}{\nolinkurl{https://medium.com/greyatom/why-how-and-when-to-scale-your-features-4b30ab09db5e}}

\paragraph{Model Validation.}
For a regression problem, supervised classification or unsupervised
classification with a set of ground truth labels it is absolutely
necessary to split your data in disjoint sets for training and testing
prior to any processing. The processing variables should be
established from the training set and applied to test and
training. This is to estimate if your normalization is sane for
"unseen" data.

\paragraph{Hyperparameter tuning.}
For almost all interesting problems the computational cost of running
a gridsearch to find the optimal configuration for the hyperparameters
is not feasible, both because it is expensive, but the loss-surface
might be glassy excepting one very narrow spike your grid doesn't
hit. There are many sophisticated tools for the job, but for most
cases the tool for the job has shown to be simple random search. Run N
experiments with random hyperparameter configurations and pick the
best performing on the validation set.

\paragraph{Performance estimation.}
For estimating your models performance the tool depends on your
application. For regression the $R^2$ coefficient (explained variance)
is commonly used, and for linear regression models you usually want an
estimate of coefficient significance (\texttt{scipy statsmodels} is a python
package good for this). For estimating significance and impact you
should have a clear image of the degree of multi-colinearity in your
data (does your design matrix have full rank?). For non-linear models
of 1d vectors (not sequence or image models) estimating feature
importance (the impact of removing one feature on model
performance). For image data and convolutional nets visualizing the
max activation of the filters is a good way to estimate what your
model is doing.

For classification there usually is a tradeoff between number of true
and false positives. The f1-score (or depending on your model you
might favor precision over recall or vice versa which would need an
adjusted f-score) is a good single number-measure. Plotting the ROC
(reciever-operator characteristic) curve and estimating it's area
under the curve (analogous to accuracy). These curves are plotted
per-class basis, and then you can average over them to produce an
aggregate performance.

You should also use some statsitical measure for model
validation. $k$-fold cross validation is recommended for most cases.

In essence: what the goal of all this is to be able to estimate the
generalization performance. The training set is used to estimate the
optimal parameters and hyperparameters as well as the distribution of
performance under these. For each hyperparameter search you use the
test set to make a final estimation of your generalization
performance, going back and adjusting parameters based on test
performance strongly reduces your certainty of generalization and is
strongly discouraged.

Scikit-learn has many off-the shelf measures for model performance and validation, with examples.  The following blogs on this may be of interest
\href{{https://towardsdatascience.com/hyper-parameter-tuning-techniques-in-deep-learning-4dad592c63c8}}{\nolinkurl{https://towardsdatascience.com/hyper-parameter-tuning-techniques-in-deep-learning-4dad592c63c8}} and
\href{{https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc_crossval.html#sphx-glr-auto-examples-model-selection-plot-roc-crossval-py}}{\nolinkurl{https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc_crossval.html\#sphx-glr-auto-examples-model-selection-plot-roc-crossval-py}}

\subsection{Background literature}

\begin{enumerate}
\item The text of Michael Nielsen is highly recommended, see \href{{http://neuralnetworksanddeeplearning.com/}}{Nielsen's book}. It is an excellent read.

\item The textbook of \href{{https://www.springer.com/gp/book/9780387848570}}{Trevor Hastie, Robert Tibshirani, Jerome H. Friedman, The Elements of Statistical Learning, Springer}, chapters 3 and 7 are the most relevant ones for the analysis here.

\item \href{{https://arxiv.org/abs/1803.08823}}{Mehta et al, arXiv 1803.08823}, \emph{A high-bias, low-variance introduction to Machine Learning for physicists}, ArXiv:1803.08823.
\end{enumerate}

\noindent
If you wish to read more about the Ising model and statistical physics here are three suggestions.

\begin{enumerate}
\item \href{{http://www.worldscientific.com/worldscibooks/10.1142/5660}}{M. Plischke and B. Bergersen}, \emph{Equilibrium Statistical Physics}, World Scientific, see chapters 5 and 6.

\item \href{{http://www.cambridge.org/no/academic/subjects/physics/computational-science-and-modelling/guide-monte-carlo-simulations-statistical-physics-4th-edition?format=HB}}{D. P. Landau and K. Binder}, \emph{A Guide to Monte Carlo Simulations in Statistical Physics}, Cambridge, see chapters 2,3 and 4.

\item \href{{https://global.oup.com/academic/product/monte-carlo-methods-in-statistical-physics-9780198517979?cc=no&lang=en&}}{M. E. J. Newman and T. Barkema}, \emph{Monte Carlo Methods in Statistical Physics}, Oxford, see chapters 3 and 4.
\end{enumerate}

\noindent

% ------------------- end of main content ---------------

% #ifdef PREAMBLE
\end{document}
% #endif


\documentclass{article}
\input{mydefs}

\usepackage{fullpage}

\usepackage[colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}

\title{Python Exercises for Ghayoung and Beth}
\author{Sunghee}

\begin{document}
\maketitle

\section{Basic exercises}

\begin{description}
\item[Exercise 01]
\href{https://www.practicepython.org/exercise/2014/01/29/01-character-input.html}{Character Input}

\item[Exercise 02]
\href{https://www.practicepython.org/exercise/2014/02/05/02-odd-or-even.html}{Odd or Even}

\item[Exercise 03]
\href{https://www.practicepython.org/exercise/2014/02/26/04-divisors.html}{Divisors}

\item[Exercise 04]
Create a program that asks the user for a number and then prints out all prime numbers that is less than that number.
(Hint: Think of the definition of a prime number. You can use the piece of code you developed for one of the above problems.
You will need to use nested for-loops. Ask your instructor if you don't know what nested for-loops are.)

\item[Exercise 04.1]
Implement a function which takes two numbers, adds them, and returns the result.

\item[Exercise 04.2]
Implement a function which takes one integer and returns {\tt True} if it is an even number,
and return {\tt False} otherwise.

\item[Exercise 04.3]
Implement a function which takes one integer and prints all its divisors.

\item[Exercise 04.4]
Implement a function named \verb+is_prime+ which takes one integer and returns {\tt True} if it is a prime number,
and return {\tt False} otherwise.

\item[Exercise 04.5]
Create a program that asks the user for a number and then prints out all prime numbers that is less than that number
using a function you implemented above.

\item[Exercise 05 (Optional)]
Make the prime number printing program more efficient following the steps below.

\begin{enumerate}
\item Make \verb+is_prime+ faster by cutting down the size of the \verb+for+-loop into half,
\ie, by changing the \verb+for+-loop as follows:
\begin{verbatim}
    for divisor in range(2, int(number/2) + 1):
        ...
\end{verbatim}

\item Make \verb+is_prime+ faster by cutting down the size of the \verb+for+-loop even further
as follows.
\begin{verbatim}
    import math

    for divisor in range(2, int(math.sqrt(number)) + 1):
        ...
\end{verbatim}

Can you explain why this works?

\item (Advanced) Make \verb+is_prime+ faster by only checking whether the number is divided by primer numbers less than or equal to
the square root of the number.

\end{enumerate}
\end{description}

\section{Intermediate-level exercises}

\begin{enumerate}
\item
\href{https://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html}{List Overlap}

\item
\href{https://www.practicepython.org/exercise/2014/03/12/06-string-lists.html}{String Lists}

\end{enumerate}

\end{document}