Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Finally, implement for #lists: `TypeList`, `List`, `ConsList`, `Head`, `Tail`, `IsEmpty`
- **Lists**
- ```stella
- extend with #lists;
- Copy
- ```
- A list is a homogenous collection of elements of an arbitrary length. Its type is written as `[Type]`, and it is constructed using either the `cons` expression which accepts the head and the tail to be linked, or using the `[expr1, expr2, ...]` syntax. The first element of the list can be accessed using `List::head`, the remaining elements (as a list) using `List::tail`, and you can check if it's empty using `List::isempty`.
- ```stella
- language core;
- extend with #lists;
- extend with #multiparameter-functions;
- fn nonempty(list : [Nat]) -> Bool {
- return if List::isempty(list) then true else false
- }
- fn first_or_default(list : [Nat], default : Nat) -> Nat {
- return if nonempty(list) then List::head(list) else default
- }
- fn main(default : Nat) -> Nat {
- return first_or_default([], default)
- }
- ```
- ```
- // File generated by the BNF Converter (bnfc 2.9.5).
- package org.syntax.stella.Absyn;
- public class TypeList extends Type {
- public final Type type_;
- public int line_num, col_num, offset;
- public TypeList(Type p1) { type_ = p1; }
- public R accept(org.syntax.stella.Absyn.Type.Visitor v, A arg) { return v.visit(this, arg); }
- public boolean equals(java.lang.Object o) {
- if (this == o) return true;
- if (o instanceof org.syntax.stella.Absyn.TypeList) {
- org.syntax.stella.Absyn.TypeList x = (org.syntax.stella.Absyn.TypeList)o;
- return this.type_.equals(x.type_);
- }
- return false;
- }
- public int hashCode() {
- return this.type_.hashCode();
- }
- }
- ```
- ```
- // File generated by the BNF Converter (bnfc 2.9.5).
- package org.syntax.stella.Absyn;
- public class List extends Expr {
- public final ListExpr listexpr_;
- public int line_num, col_num, offset;
- public List(ListExpr p1) { listexpr_ = p1; }
- public R accept(org.syntax.stella.Absyn.Expr.Visitor v, A arg) { return v.visit(this, arg); }
- public boolean equals(java.lang.Object o) {
- if (this == o) return true;
- if (o instanceof org.syntax.stella.Absyn.List) {
- org.syntax.stella.Absyn.List x = (org.syntax.stella.Absyn.List)o;
- return this.listexpr_.equals(x.listexpr_);
- }
- return false;
- }
- public int hashCode() {
- return this.listexpr_.hashCode();
- }
- }
- ```
- ```
- // File generated by the BNF Converter (bnfc 2.9.5).
- package org.syntax.stella.Absyn;
- public class ConsList extends Expr {
- public final Expr expr_1, expr_2;
- public int line_num, col_num, offset;
- public ConsList(Expr p1, Expr p2) { expr_1 = p1; expr_2 = p2; }
- public R accept(org.syntax.stella.Absyn.Expr.Visitor v, A arg) { return v.visit(this, arg); }
- public boolean equals(java.lang.Object o) {
- if (this == o) return true;
- if (o instanceof org.syntax.stella.Absyn.ConsList) {
- org.syntax.stella.Absyn.ConsList x = (org.syntax.stella.Absyn.ConsList)o;
- return this.expr_1.equals(x.expr_1) && this.expr_2.equals(x.expr_2);
- }
- return false;
- }
- public int hashCode() {
- return 37*(this.expr_1.hashCode())+this.expr_2.hashCode();
- }
- }
- ```
- ```
- // File generated by the BNF Converter (bnfc 2.9.5).
- package org.syntax.stella.Absyn;
- public class Head extends Expr {
- public final Expr expr_;
- public int line_num, col_num, offset;
- public Head(Expr p1) { expr_ = p1; }
- public R accept(org.syntax.stella.Absyn.Expr.Visitor v, A arg) { return v.visit(this, arg); }
- public boolean equals(java.lang.Object o) {
- if (this == o) return true;
- if (o instanceof org.syntax.stella.Absyn.Head) {
- org.syntax.stella.Absyn.Head x = (org.syntax.stella.Absyn.Head)o;
- return this.expr_.equals(x.expr_);
- }
- return false;
- }
- public int hashCode() {
- return this.expr_.hashCode();
- }
- }
- ```
- ```
- // File generated by the BNF Converter (bnfc 2.9.5).
- package org.syntax.stella.Absyn;
- public class Tail extends Expr {
- public final Expr expr_;
- public int line_num, col_num, offset;
- public Tail(Expr p1) { expr_ = p1; }
- public R accept(org.syntax.stella.Absyn.Expr.Visitor v, A arg) { return v.visit(this, arg); }
- public boolean equals(java.lang.Object o) {
- if (this == o) return true;
- if (o instanceof org.syntax.stella.Absyn.Tail) {
- org.syntax.stella.Absyn.Tail x = (org.syntax.stella.Absyn.Tail)o;
- return this.expr_.equals(x.expr_);
- }
- return false;
- }
- public int hashCode() {
- return this.expr_.hashCode();
- }
- }
- ```
- ```
- // File generated by the BNF Converter (bnfc 2.9.5).
- package org.syntax.stella.Absyn;
- public class IsEmpty extends Expr {
- public final Expr expr_;
- public int line_num, col_num, offset;
- public IsEmpty(Expr p1) { expr_ = p1; }
- public R accept(org.syntax.stella.Absyn.Expr.Visitor v, A arg) { return v.visit(this, arg); }
- public boolean equals(java.lang.Object o) {
- if (this == o) return true;
- if (o instanceof org.syntax.stella.Absyn.IsEmpty) {
- org.syntax.stella.Absyn.IsEmpty x = (org.syntax.stella.Absyn.IsEmpty)o;
- return this.expr_.equals(x.expr_);
- }
- return false;
- }
- public int hashCode() {
- return this.expr_.hashCode();
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement