CafeOBJ. CafeOBJ. Starting CafeOBJ. Wolfgang Schreiner 1. A Quick Overview. 2.

Similar documents
Computing Fundamentals 2 Introduction to CafeOBJ

Built-in Module BOOL. Lecture Note 01a

Algebraic specification and verification with CafeOBJ Part 1 Introduction

the Common Algebraic Specification Language

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

The Prototype Verification System PVS

Verifying the Correctness of Compile Imperative Programming Language. Supervisor: Professor Futatsugi Koki of Information, Master Course

RSL Reference Manual

CS422 - Programming Language Design

The Maude 2.0 System. M. Clavel F. Durán S. Eker P. Lincoln. N. Martí-Oliet J. Meseguer C. Talcott

Overloading, Type Classes, and Algebraic Datatypes

Haskell 98 in short! CPSC 449 Principles of Programming Languages

BOBJ: A Quickstart for Software Engineers

The Programming Language Core

Data types for mcrl2

Languages with Contexts II: An Applicative Language

Contents. Chapter 1 SPECIFYING SYNTAX 1

Logical Methods in... using Haskell Getting Started

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Specifying and Verifying Programs (Part 2)

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Data-Oriented System Development

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

A Small Interpreted Language

Behavioral Verification of Distributed Concurrent Systems with BOBJ

Haskell Overview III (3A) Young Won Lim 10/4/16

Programming Language Concepts: Lecture 14

THE AGDA STANDARD LIBRARY

Analysis of the Suzuki-Kasami Algorithm with the Maude Model Checker

Flang typechecker Due: February 27, 2015

i217 Functional Programming 7. Multisets

Reasoning About Programs Panagiotis Manolios

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Unification in Maude. Steven Eker

UNIT 3

Realistic Program Visualization in CafePie

Functional Programming. Pure Functional Programming

Program analysis parameterized by the semantics in Maude

ISR 2014 Strategies. Hélène KIRCHNER Inria. August Topics, Objectives, Contents. Hélène KIRCHNER Inria ISR 2014 Strategies 1 / 48

Typed Racket: Racket with Static Types

Maude 2.0 Manual. Version 1.0. Manuel Clavel Francisco Durán Steven Eker Patrick Lincoln Narciso Martí-Oliet José Meseguer Carolyn Talcott

Pierce Ch. 3, 8, 11, 15. Type Systems

Evolutionary Search in Machine Learning. Lutz Hamel Dept. of Computer Science & Statistics University of Rhode Island

CSCE 314 Programming Languages

Data Types The ML Type System

Dorel LUCANU. University Al.I.Cuza of Iaşi Department of Computer Science Berthelot Iaşi, Romania

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Section 17.1

CSCC24 Functional Programming Scheme Part 2

Introduction to Programming Using Java (98-388)

modulo associativity, commutativity, and/or identity. With its module database and its ability to incorporate Lisp code, this provides a very exible a

Expressions and Assignment

CA Compiler Construction

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Design

Programming Language Concepts, CS2104 Lecture 7

Semantic Algebras. Semantic Algebras

4.3 Lists of natural numbers Binary trees with natural numbers Specications with equat

Advanced features of Functional Programming (Haskell)

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Proving Theorems with Athena

CSE 3302 Programming Languages Lecture 8: Functional Programming

Functional Logic Programming Language Curry

Methods. Methods. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Section 17.2

Maude Manual (Version 2.5) Manuel Clavel Francisco Durán Steven Eker Patrick Lincoln Narciso Martí-Oliet José Meseguer Carolyn Talcott

G Programming Languages - Fall 2012

IA014: Advanced Functional Programming

Programming Languages Third Edition

Syntax and Grammars 1 / 21

CMSC 330: Organization of Programming Languages

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CS 457/557: Functional Languages

Denotational Semantics. Domain Theory

Functional Programming in Haskell Part I : Basics

CS 320: Concepts of Programming Languages

Basic Elements of Computer Algebra in MIZAR

Programming Languages

Using an LALR(1) Parser Generator

3.1 IMP: A Simple Imperative Language

Principles of Programming Languages 2017W, Functional Programming

A Michael Jackson presentation. CSE503: Software Engineering. The following slides are from his keynote at ICSE 1995

Lecture 19: Functions, Types and Data Structures in Haskell

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

JML Class Specifications The Java Modeling Language (Part 2) A Java Class

Lexical Analysis. Introduction

Lecture 2: Big-Step Semantics

Programming in Omega Part 1. Tim Sheard Portland State University

INTRODUCTION TO FUNCTIONAL PROGRAMMING

SMPL - A Simplified Modeling Language for Mathematical Programming

Relational Databases

TEMPO RELEASE NOTES I O A. Recent Fixes (v0.1.4) 3. Known Issues (v0.1.4) 4. Language changes (v0.1.4) 4. v0.1.4 (BETA) 12/14/2006

Introduction to Scheme

The Java Modeling Language (Part 2)

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CSC Discrete Math I, Spring Sets

Compiler Theory. (Semantic Analysis and Run-Time Environments)

B.6 Types and Overloading

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Transcription:

CafeOBJ Wolfgang Schreiner Wolfgang.Schreiner@risc.uni-linz.ac.at 1. A Quick Overview Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria http://www.risc.uni-linz.ac.at 2. More Details 3. More Advanced Features Wolfgang Schreiner http://www.risc.uni-linz.ac.at 1/28 Wolfgang Schreiner http://www.risc.uni-linz.ac.at 2/28 CafeOBJ Starting CafeOBJ CafeOBJ: an algebraic specification language/system. 1995 : Japan Institute of Advanced Science and Technology (JAIST). Kokichi Futatsugi (JAIST), Razvan Diaconescu (IMAR institute, Romania), et al. A member of the OBJ language family. Since 1970s: Josef Goguen (Univ. of California at San Diego), et al. From http://www.cse.ucsd.edu/users/goguen/sys/obj.html: The OBJ languages are broad spectrum algebraic programming and specification languages, based on order sorted equational logic, possibly enriched with other logics (such as rewriting logic, hidden equational logic, or first order logic), and providing the powerful module system of parameterized programming. Current Version (2008): 1.4.8 Open source implementation in Common Lisp. Binaries for Linux, Windows, Mac OS provided. http://www.ldl.jaist.ac.jp/cafeobj Wolfgang Schreiner http://www.risc.uni-linz.ac.at 3/28 > cafeobj -- loading standard prelude ; Loading /usr3/cafeobj-1.4/prelude/std.bin CafeOBJ> -- CafeOBJ system Version 1.4.7b5(PigNose0.99) -- built: 2007 Jan 27 Sat 23:48:16 GMT prelude file: std.bin *** 2008 Jan 22 Tue 7:27:38 GMT Type? for help *** -- Containing PigNose Extensions -- --- built on International Allegro CL Enterprise Edition 8.0 [Linux (x86)] (Jan 28, 2007 8:48) Wolfgang Schreiner http://www.risc.uni-linz.ac.at 4/28

Defining Tight Modules Predefined Modules module! STACK Introduce a tight module : a named specification with initial (executable) semantics. protecting (NAT) Import another specification preserving its model. signature [ sorts ] opns axioms var vars equns Pattern matching on the left hand side of each equation. Note the period after each equation (preceded by a blank)! Executable specifications. -- stack of natural numbers module! STACK protecting (NAT) signature [ Stack ] op empty : -> Stack op push : Nat Stack -> Stack op top : Stack -> Nat op pop : Stack -> Stack axioms var N : Nat var S : Stack eq top(push(n, S)) = N. eq pop(push(n, S)) = S. CafeOBJ provides a library of predefined modules. Some modules are automatically imported. BOOL: sort Bool, ops. true, false, not, and, or, xor, implies. Other modules require explicit import. NAT: sort Nat, number literals, operations 0, s, 1, +, *, <, <=,... INT: sort Int, literals and operations as for NAT extended by -. RAT: sort Rat, literals and operations as for INT extended by /. CHARACTER: sort Character with various operations. STRING: sort String with various operations.... See subdirectory lib of CafeOBJ installation for module names, use command show Module for viewing module contents. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 5/28 Wolfgang Schreiner http://www.risc.uni-linz.ac.at 6/28 Showing Module Contents Reading Modules from Files CafeOBJ> show NAT ; Loading /usr3/cafeobj-1.4/lib/nat.bin sys:mod! NAT principal-sort Nat signature op s _ : Nat -> NzNat demod pred _ >= _ : Nat Nat demod pred _ > _ : Nat Nat demod pred _ <= _ : Nat Nat demod pred _ < _ : Nat Nat demod op _ * _ : Nat Nat -> Nat assoc comm idr: 1 demod r-assoc op _ + _ : Nat Nat -> Nat assoc comm idr: 0 demod r-assoc op sd : Nat Nat -> Nat comm demod op _ quo _ : Nat NzNat -> Nat demod op _ rem _ : Nat NzNat -> Nat demod l-assoc pred _ divides _ : NzNat Nat demod op p _ : NzNat -> Nat demod Wolfgang Schreiner http://www.risc.uni-linz.ac.at 7/28 CafeOBJ> input Stack.cobj processing input : /usr2/schreine//examples/stack.cobj -- defining module! STACK -- reading in file : nat ; Loading /usr3/cafeobj-1.4/lib/nat.bin -- defining module! NAT -- reading in file : nznat ; Loading /usr3/cafeobj-1.4/lib/nznat.bin -- defining module! NZNAT.._.._* done. -- done reading in file: nznat _.._.* done. -- done reading in file: nat.._..* done. CafeOBJ> show STACK module! STACK Command input reads file with module definitions. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 8/28

Evaluating Terms Tracing Evaluations CafeOBJ> open STACK -- opening module STACK.. done. %STACK> reduce top(pop(push(2, push(1, empty)))). -- reduce in %STACK : top(pop(push(2,push(1,empty)))) 1 : NzNat (0.000 sec for parse, 2 rewrites(0.000 sec), 2 matches) %STACK> reduce top(pop(push(1, empty))). -- reduce in %STACK : top(pop(push(1,empty))) top(empty) : Nat (0.000 sec for parse, 1 rewrites(0.000 sec), 2 matches) %STACK> close CafeOBJ> Commands open/close enter/leave the context of a module; command reduce evaluates terms (note the period preceded by a blank). %STACK> set trace on %STACK> reduce top(pop(push(2, push(1, empty)))). -- reduce in %STACK : top(pop(push(2,push(1,empty)))) 1>[1] rule: eq pop(push(n:nat,s:stack)) = S N:Nat -> 2, S:Stack -> push(1,empty) 1<[1] pop(push(2,push(1,empty))) --> push(1,empty) 1>[2] rule: eq top(push(n:nat,s:stack)) = N N:Nat -> 1, S:Stack -> empty 1<[2] top(push(1,empty)) --> 1 1 : NzNat (0.000 sec for parse, 2 rewrites(0.000 sec), 2 matches) Command set trace on shows rules applied in the reduction. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 9/28 Wolfgang Schreiner http://www.risc.uni-linz.ac.at 10/28 Tracing Evaluations (Contd) %STACK> set trace whole on %STACK> reduce top(pop(push(2, push(1, empty)))). -- reduce in %STACK : top(pop(push(2,push(1,empty)))) 1>[1] rule: eq pop(push(n:nat,s:stack)) = S N:Nat -> 2, S:Stack -> push(1,empty) 1<[1] pop(push(2,push(1,empty))) --> push(1,empty) [1]: top(pop(push(2,push(1,empty)))) ---> top(push(1,empty)) 1>[2] rule: eq top(push(n:nat,s:stack)) = N N:Nat -> 1, S:Stack -> empty 1<[2] top(push(1,empty)) --> 1 [2]: top(push(1,empty)) ---> 1 1 : NzNat (0.000 sec for parse, 2 rewrites(0.010 sec), 2 matches) 1. A Quick Overview 2. More Details 3. More Advanced Features Wolfgang Schreiner http://www.risc.uni-linz.ac.at 11/28 Wolfgang Schreiner http://www.risc.uni-linz.ac.at 12/28

Identifiers Modules Almost arbitrary strings may denote names of sorts and operators. Identifier x-value Difference term x - value Identifier x+value Sum term x + value Identifier x*value Sum term x * value Always use blanks around infix/mixfix operators. Every module introduces a name space. Only entities declared in a module can be directly referenced within the module. By their unqualified name. Entities of other modules can be referenced by qualified names. name.module : entity name in module. Other modules may be imported. Remote entities become visible. Can be referenced like local entities. Ambiguities can be resolved by qualification with module name. Imported modules are not duplicated. Multiple imports of a module share the same model. -- comments module! name imports signature sorts operators axioms equations Wolfgang Schreiner http://www.risc.uni-linz.ac.at 13/28 Wolfgang Schreiner http://www.risc.uni-linz.ac.at 14/28 Sorts Operators A signature may introduce one or more sorts. [ sort1 sort2 ] Sequence of sort names separated by blanks. Note the blanks after [ and before ]. Sorts may be partially ordered: [ Nat < Int < Rat, Int < Float ] Subsort < Supersort Sort order is interpreted as set inclusion. The type checker considers values of the subsort also as values of the supersort. The use of subsorts may simplify specifications considerably. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 15/28 A signature may introduce operators (operations/constants). op name : argument sorts -> result sort op name : -> result sort Note the blanks around the tokens : and ->. Operators may be declared as infix/mixfix by the use of. op _+_ : Nat Nat -> Nat op _<_ : Nat Nat -> Bool op if_then_else_fi: Bool Nat Nat -> Nat 2 + 3, 2 < 3, if N < M then N else M fi Multiple operators may be declared with the same arity. op (_+_) (_*_) : Nat Nat -> Nat Operator names may be overloaded. op _+_ : Nat Nat -> Nat -- addition op _+_ : Set Set -> Set -- union Wolfgang Schreiner http://www.risc.uni-linz.ac.at 16/28

Predicates Axioms Predicates are operators with target sort Bool op name : argument sorts -> Bool pred name : argument sorts pred can be used as a shorthand for predicate declarations. pred _<_ : Nat Nat The (in)equality predicate is implicitly defined on each sort. pred _==_ : S S pred _=/=_ : S S Equality is defined in terms of evaluation. (t == t ) = true iff t and t evaluate to a common term. Works correctly iff term rewriting system is Noetherian and confluent. CafeOBJ considers predicates just as normal operators. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 17/28 Axioms declare variables and (conditional) equations. var name : sort vars name1 name2 : sort eq term = term. ceq term = term if boolean-term. Syntax pitfalls: Note the blanks around the tokens : and =. Note the period. preceded by a blank. Equations may be labeled: var N : Nat eq [ right-id ] : N+0 = N Labels are printed in reduction traces. Equations of arbitrary shape are allowed but only especially constrained equations are used as reduction rules (to be discussed later). Wolfgang Schreiner http://www.risc.uni-linz.ac.at 18/28 Example Context Variables module! GCD protecting (INT) signature op gcd : Int Int -> Int axioms vars N M : Int eq gcd(n, 0) = N. eq gcd(0, M) = M. ceq gcd(n, M) = gcd(n - M, M) if N >= M and M > 0. ceq gcd(n, M) = gcd(n, M - N) if M >= N and N > 0. %GCD> reduce gcd(15,12). -- reduce in %GCD : gcd(15,12) 3 : NzNat (0.000 sec for parse, 45 rewrites(0.010 sec), 95 matches) Wolfgang Schreiner http://www.risc.uni-linz.ac.at 19/28 CafeOBJ> open GCD -- opening module GCD.. done. %GCD> let a = 15. -- setting let variable "a" to : 15 : NzNat %GCD> let b = 12. -- setting let variable "b" to : 12 : NzNat %GCD> show let [bindings] b = 12 a = 15 %GCD> reduce gcd(a,b). -- reduce in %GCD : gcd(15,12) 3 : NzNat (0.000 sec for parse, 45 rewrites(0.000 sec), 95 matches) Command let to bind variables in current module context. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 20/28

Local Bindings Operator Attributes Unfortunately CafeOBJ does not support local bindings in a term. Abstract specification: f (x, y) = let z = x x in x + y z CafeOBJ: eq f(x, y) = f0(x, y, x * x) eq f0(x, y, z) = x*y*z Use auxiliary operators as a substitute for local bindings There is a shorthand notation for some special axioms. op name : argument sorts -> result sort attributes Example: op + : S S -> S assoc comm idem id:n Predicate == considers these operation attributes. assoc(iative): x + (y + z) = (x + y) + z comm(utative): x + y = y + x idem(potent): x + x = x id:n: x + n = x Constructor attribute constr: Unused (treated as comment) by CafeOBJ. op nil : -> List constr op < : List List -> List constr Wolfgang Schreiner http://www.risc.uni-linz.ac.at 21/28 Wolfgang Schreiner http://www.risc.uni-linz.ac.at 22/28 Evaluating Terms Showing Rules A tight module defines a term rewriting system. (Conditional) equations define (conditional) rewrite rules. eq t = t. defines t t. eq t = t if b. defines t t with condition b. Also the rewrite rules of the imported modules are included. Rewrite rules of module BOOL are always included. Equations must satisfy two constraints to become rewrite rules. 1. Every variable on the righthand side of the equation (or in the condition) must occur on the left-hand side. 2. The lefthand side must not be a single variable. The term rewriting system is not necessarily Noetherian and confluent (i.e. reductions need not terminate, different reduction strategies may give different results). Wolfgang Schreiner http://www.risc.uni-linz.ac.at 23/28 CafeOBJ> open STACK -- opening module STACK.. done. %STACK> show rules -- rewrite rules in module : %STACK 1 : eq top(push(n,s)) = N 2 : eq pop(push(n,s)) = S %STACK> show all rules -- rewrite rules in module : %STACK 1 : eq top(push(n,s)) = N 2 : eq pop(push(n,s)) = S 3 : eq [:BDEMOD] : sd(m:nat,n:nat) = #! (abs (- m n)) 4 : eq [:BDEMOD] : M:Nat + N:Nat = #! (+ m n) 5 : eq [:BDEMOD] : N:Nat * 0 = 0 6 : eq [:BDEMOD] : M:Nat quo NN:NzNat = #! (truncate m nn) 7 : eq [:BDEMOD] : M:Nat rem NN:NzNat = #! (rem m nn) Commands show rules and show all rules. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 24/28

Evaluation Strategy CafeOBJ supports various evaluation strategies. Default strategy: when evaluating a term f (,a i,), first evaluate every a i, for which there is a rewrite rule f (..., t i,...)... with a non-variable term t i in the position of a i. then evaluate the whole term f (...). Alternative strategy may be specified by attribute strat: (ints) ints is a list of integers denoting argument positions. Positive number denotes eager evaluation on corresponding argument. Negative (or missing) number denotes lazy evaluation on argument. 0 denotes evaluation of the the whole term. op if_then_else_fi : Bool Int Int -> Int strat: (1 0) op _+_ : Int Int -> Int strat: (1 2 0) op cons : Elem List -> List strat: (0) The chosen strategy may affect the result/termination of the evaluation. 1. A Quick Overview 2. More Details 3. More Advanced Features Wolfgang Schreiner http://www.risc.uni-linz.ac.at 25/28 Wolfgang Schreiner http://www.risc.uni-linz.ac.at 26/28 More Advanced Features Parameterized Modules Further features of CafeOBJ. Term rewriting commands. CafeOBJ may be used for term rewriting/induction proofs (see chapters 9 and 10 of the manual). Behavioral operators and behavioral equations. Modeling object methods: an operator may have a special argument describing an object whose state is modified by the method. Transitions. Non-symmetric relations between terms. Generalized module expressions: Modules may be renamed and combined. Modules may be parameterized. Parameterized modules may be instantiated. A powerful module concept is crucial for specifying in the large. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 27/28 A loose module is a named specification with loose semantics. module* ELEM signature [ Elem ] May be used as the type of a parameter in a tight module. module! STACK(E :: ELEM) signature [ Stack ] push : Elem.E Stack -> Stack The parameter may be instantiated by a matching tight module. view NATELEM from ELEM to NAT sort Elem -> Nat module! NATSTACK -- introduces natural number stacks protecting (STACK(E <= NATELEM)) We are now going to present the theory of CafeOBJ-like specifications. Wolfgang Schreiner http://www.risc.uni-linz.ac.at 28/28