Tehee
This commit is contained in:
30
3/assig.org
30
3/assig.org
@ -3,10 +3,8 @@
|
||||
#+latex_header: \usepackage{parskip}
|
||||
|
||||
* Supporting files
|
||||
|
||||
~This~ time you can make use of a [[http://bnfc.digitalgrammars.com/][BNFC]] specification ([[https://chalmers.instructure.com/courses/36941/file_contents/course%20files/chi/Chi.cf][~Chi.cf~]]) of a variant of the concrete syntax of $\chi$. This grammar gives specific rules for variable and constructor names, and differs from the concrete syntax presented in the lectures in some ways:
|
||||
# TODO: Fix '
|
||||
- Variables and constructors are sequences of letters (~a~ to ~z~ and ~A~ to ~Z~), digits (~0~ to ~9~), underscores (~_~), dashes (~-~) and primes ('), and variables have to start with lower-case letters or underscores, whereas constructors have to start with upper-case letters.
|
||||
This time you can make use of a [[http://bnfc.digitalgrammars.com/][BNFC]] specification ([[https://chalmers.instructure.com/courses/36941/file_contents/course%20files/chi/Chi.cf][~Chi.cf~]]) of a variant of the concrete syntax of $\chi$. This grammar gives specific rules for variable and constructor names, and differs from the concrete syntax presented in the lectures in some ways:
|
||||
- Variables and constructors are sequences of letters (~a~ to ~z~ and ~A~ to ~Z~), digits (~0~ to ~9~), underscores (~_~), dashes (~-~) and primes (='=), and variables have to start with lower-case letters or underscores, whereas constructors have to start with upper-case letters.
|
||||
- Lambdas are written using backslash characters (~\~) and arrows are written using the string ~->~.
|
||||
- End-of-line comments of the form ~-- ...~ are allowed, as are (non-nested) comments of the form ~{- ... -}~.
|
||||
- There can be a final comma after a non-empty list of constructor arguments, and there can be a final semicolon after a non-empty list of branches.
|
||||
@ -61,10 +59,14 @@ Consider the $\chi$ term /t/ with concrete syntax $C (\lambda z.z)$:
|
||||
|
||||
In the abstract syntax the constructor ~C~ should be represented by $\underline{C} \in \text{Const}$, and the variable ~z~ by $\underline{z} \in \text{Var}$. When giving the $\chi$ representation of the term you should use (the representation of) the number 0 to represent $\underline{C}$, and 1 to represent $\underline{z}$.
|
||||
*** Answer
|
||||
The abstract syntax is the following: $\text{const}\ \underline{C}\ (\text{cons}\ (\text{lambda}\ \underline{z}\ (\text{var}\ \underline{z}))\ \text{nil})$.
|
||||
The abstract syntax is the following:
|
||||
\[ \text{const}\ \underline{C}\ (\text{cons}\ (\text{lambda}\ \underline{z}\ (\text{var}\ \underline{z}))\ \text{nil}) \]
|
||||
|
||||
The concrete syntax of the standard representation is the following: $Const(\ulcorner \underline{C} \urcorner, Cons(Lambda(\ulcorner \underline{z} \urcorner, Cons (Var(\ulcorner \underline{z} \urcorner), Nil())), Nil())$
|
||||
# TODO: check parenthesis above
|
||||
The concrete syntax of the standard representation is the following:
|
||||
\[ \text{Const}(\ulcorner \underline{C} \urcorner, \text{Cons}(\text{Lambda}(\ulcorner \underline{z} \urcorner, \text{Cons} (\text{Var}(\ulcorner \underline{z} \urcorner), \text{Nil}())), \text{Nil}())) \]
|
||||
|
||||
The abstract syntax of the standard representation, given that $\underline{z}$ and $\underline{C}$ corresponds to zero:
|
||||
\[ \text{Const}(\text{Zero}(), \text{Cons}(\text{Lambda}(\text{Zero}(), \text{Cons} (\text{Var}(\text{Zero}()), \text{Nil}())), \text{Nil}())) \]
|
||||
|
||||
** (2p) [BN]
|
||||
|
||||
@ -82,6 +84,7 @@ Test your implementation. Here are some test cases that must work:
|
||||
| ~z~ | $C(\lambda z . z)$ | $\text{case}\ z\ \text{of}\ \{ C(z) \rightarrow z \}$ | $\text{case}\ C(\lambda z. z) \ \text{of}\ \{ C(z) \rightarrow z \}$ |
|
||||
|
||||
*** Answer
|
||||
See Assignment.hs
|
||||
|
||||
** (1p)
|
||||
Implement multiplication of natural numbers in $\chi$, using the representation of natural numbers given in the $\chi$ specification.
|
||||
@ -89,6 +92,19 @@ Implement multiplication of natural numbers in $\chi$, using the representation
|
||||
Hint: If you want to make use of addition in the implementation of multiplication, then you can define multiplication using a free variable ~add~, and use the substitution operation from the previous exercise to substitute a complete (closed) implementation of addition for this variable.
|
||||
|
||||
*** Answer
|
||||
#+begin_src chi
|
||||
\m. rec mult = \n. case n of
|
||||
{ Zero() -> Zero()
|
||||
; Succ(n) -> add m (mult n)
|
||||
}
|
||||
#+end_src
|
||||
where we substitute ~add~ for the following:
|
||||
#+begin_src chi
|
||||
\m. rec add = \n. case n of
|
||||
{ Zero() -> m
|
||||
; Succ(n) -> Suc(add n)
|
||||
}
|
||||
#+end_src
|
||||
|
||||
** (2p) [BN]
|
||||
Implement an interpreter for $\chi$. The interpreter should be a partial function from closed terms to values, and should implement the operational semantics of $\chi$.
|
||||
|
||||
Reference in New Issue
Block a user