
file:
    { declaration } EOF

declaration:
    classspec
  | adtspec
  | groundsignature
  | typedef
  | typeabbrev
  | termdef

typedef:
    GROUNDTYPE identifier [ parameterlist ]

typeabbrev:
    TYPE identifier EQUAL type

termdef:
    GROUNDTERM idorinfix COLON type

classspec:
    BEGIN identifier [ parameterlist ] COLON finalorloose CLASSSPEC { importing } { classsection } END identifier

finalorloose:
    (* EMPTY *)
  | FINAL

parameterlist:
    OBRACKET parameters { COMMA parameters } CBRACKET

parameters:
    identifier { COMMA identifier } COLON [ variance ] TYPE

variance:
    POS
  | NEG
  | MIXED
  | OPAREN numberorquestion COMMA numberorquestion CPAREN

numberorquestion:
    QUESTIONMARK
  | VALUE

classsection:
    inheritsection
  | [ visibility ] attributesection [ SEMICOLON ]
  | [ visibility ] methodsection [ SEMICOLON ]
  | definitionsection
  | classconstructorsection [ SEMICOLON ]
  | assertionsection
  | creationsection
  | requestsection [ SEMICOLON ]

visibility:
    PUBLIC
  | PRIVATE

inheritsection:
    INHERIT FROM ancestor { COMMA ancestor }

ancestor:
    identifier [ argumentlist ] [ RENAMING renaming { AND renaming } ]

renaming:
    identifier AS identifier

attributesection:
    ATTRIBUTE member { SEMICOLON member }

methodsection:
    METHOD member { SEMICOLON member }

member:
    identifier COLON type ARROW type

definitionsection:
    DEFINING member formula SEMICOLON { member formula SEMICOLON }

classconstructorsection:
    CONSTRUCTOR classconstructor { SEMICOLON classconstructor }

classconstructor:
    identifier COLON type
  | identifier COLON type ARROW type

assertionsection:
    ASSERTION { importing } [ assertionselfvar ] { assertionvarlist } identifier COLON formula SEMICOLON { identifier COLON formula SEMICOLON }

assertionselfvar:
    SELFVAR identifier COLON SELF

assertionvarlist:
    VAR vardeclaration { SEMICOLON vardeclaration }

creationsection:
    CREATION { importing } { assertionvarlist } identifier COLON formula SEMICOLON { identifier COLON formula SEMICOLON }

namedformula:
    identifier COLON formula [ SEMICOLON ]

requestsection:
    REQUEST request { SEMICOLON request }

request:
    identifier COLON type

formula:
    FORALL OPAREN vardeclaration { COMMA vardeclaration } CPAREN colonordot formula
  | EXISTS OPAREN vardeclaration { COMMA vardeclaration } CPAREN colonordot formula
  | LAMBDA OPAREN vardeclaration { COMMA vardeclaration } CPAREN colonordot formula
  | LET binding { semicolonorcomma binding } [ semicolonorcomma ] IN formula
  | formula IFF formula
  | formula IMPLIES formula
  | formula OR formula
  | formula AND formula
  | IF formula THEN formula ELSE formula
  | NOT formula
  | formula infix_operator formula
  | ALWAYS formula FOR [ identifier [ argumentlist ] DOUBLECOLON ] methodlist
  | EVENTUALLY formula FOR [ identifier [ argumentlist ] DOUBLECOLON ] methodlist
  | CASES formula OF caselist [ semicolonorcomma ] ENDCASES
  | formula WITH OBRACKET update { COMMA update } CBRACKET
  | formula DOT qualifiedid
  | formula formula
  | TRUE
  | FALSE
  | PROJ_N
  | VALUE
  | qualifiedid
  | OPAREN formula COLON type CPAREN
  | OPAREN formula { COMMA formula } CPAREN

colonordot:
    COLON
  | DOT

vardeclaration:
    identifier { COMMA identifier } COLON type

methodlist:
    OBRACE identifier { COMMA identifier } CBRACE

qualifiedid:
    idorinfix
  | identifier [ argumentlist ] DOUBLECOLON idorinfix

idorinfix:
    OPAREN infix_operator CPAREN
  | identifier

binding:
    identifier [ COLON type ] EQUAL formula

semicolonorcomma:
    COMMA
  | SEMICOLON

caselist:
    pattern COLON formula { semicolonorcomma pattern COLON formula }

pattern:
    identifier [ OPAREN identifier { COMMA identifier } CPAREN ]

update:
    formula ASSIGN formula

adtspec:
    BEGIN identifier [ parameterlist ] COLON ADT { adtsection } END identifier

adtsection:
    adtconstructorlist [ SEMICOLON ]

adtconstructorlist:
    CONSTRUCTOR adtconstructor { SEMICOLON adtconstructor }

adtconstructor:
    identifier [ adtaccessors ] COLON type
  | identifier [ adtaccessors ] COLON type ARROW type

adtaccessors:
    OPAREN identifier { COMMA identifier } CPAREN

groundsignature:
    BEGIN identifier [ parameterlist ] COLON GROUNDSIGNATURE { importing } { signaturesection } END identifier

signaturesection:
    signaturetype
  | signaturesymbolsection [ SEMICOLON ]

signaturetype:
    TYPE identifier

signaturesymbolsection:
    CONSTANT signaturesymbol { SEMICOLON signaturesymbol }

signaturesymbol:
    idorinfix COLON type

type:
    SELF
  | CARRIER
  | BOOL
  | OBRACKET type { COMMA type } ARROW type CBRACKET
  | OBRACKET type { COMMA type } CBRACKET
  | qualifiedid
  | identifier argumentlist

argumentlist:
    OBRACKET type { COMMA type } CBRACKET

importing:
    IMPORTING theory

theory:
    identifier
  | identifier argumentlist

