Algebraic Semantics Abstract Type Whose Values Are Lists of Algebraic Semantics Involves the Algebraic Integers: Specification of Data and Language Constructs
Total Page:16
File Type:pdf, Size:1020Kb
Algebraic Semantics Abstract type whose values are lists of Algebraic semantics involves the algebraic integers: specification of data and language constructs. Sorts = { Integer, Boolean, List }. Function symbols with their signatures: Foundations based on abstract algebras. zero : Integer Basic idea one : Integer • Name the sorts of objects and the operations plus ( _ , _ ) : Integer, Integer ! Integer on the objects. minus ( _ , _ ) : Integer, Integer ! Integer • Use algebraic axioms to describe their characteristic properties. true : Boolean false : Boolean An algebraic specification contains two parts: emptyList : List signature and equations. cons ( _ , _ ) : Integer, List ! List A signature # of an algebraic specification head ( _ ) : List ! Integer is a pair <Sorts, Operations> where tail ( _ ) : List ! List • Sorts is a set containing names of sorts. empty? ( _ ) : List ! Boolean • Operations is a family of function symbols length ( _ ) : List ! Integer indexed by the functionalities of the operations represented by the function symbols. Chapter 12 1 Chapter 12 2 Family of operations decomposes: Module Representation OprBoolean = { true, false } OprInteger,Integer!Integer = { plus, minus } • Decompose definitions into relatively small OprList!Integer = { head, length } components. Equations constrain the operations to indicate • Import the signature and equations of one the appropriate behavior for the operations. module into another. head (cons (m, s)) = m, empty? (emptyList) = true • Define sorts and functions to be either exported or hidden. empty? (cons (m, s)) = false. Each stands for a closed assertion: • Modules can be parameterized to define generic abstract data types. "m:Integer, "s:List [head (cons (m, s)) = m]. empty? (emptyList) = true "m:Integer, "s:List [empty? (cons (m, s))= false]. Chapter 12 3 Chapter 12 4 A Module for Truth Values variables module Booleans b, b1, b2 : Boolean exports equations sorts Boolean [B1] and (true, b) = b operations [B2] and (false, true) = false true : Boolean [B3] and (false, false) = false false : Boolean [B4] not (true)= false errorBoolean : Boolean [B5] not (false) = true not ( _ ) : Boolean Boolean ! [B6] or (b1, b2) = not (and (not (b1), not (b2))) and ( _ , _ ) : [B7] implies (b1, b2) = or (not (b1), b2) Boolean, Boolean Boolean ! [B8] xor (b1, b2) = or ( _ , _ ) : and (or(b1,b2),not(and(b1,b2))) Boolean, Boolean Boolean ! [B9] eq? (b1, b2) = not (xor (b1, b2)) implies ( _ , _ ) : end Booleans Boolean,Boolean ! Boolean eq? ( _ , _ ) : Note module syntax Boolean, Boolean ! Boolean end exports A conditional equation has the form operations lhs=rhs when lhs =rhs , lhs =rhs , ..., lhs =rhs . xor ( _ , _ ) : Boolean, Boolean ! Boolean 1 1 2 2 n n Chapter 12 5 Chapter 12 6 A Module for Natural Numbers variables m, n : Natural module Naturals equations imports Booleans [N1] 1 = succ (0) exports [N2] 10 = succ (succ (succ (succ (succ ( sorts Natural succ (succ (succ (succ (succ (0)))))))))) operations [N3] add (m, 0) = m 0 : Natural [N4] add (m, succ (n)) = succ (add (m, n)) 1 : Natural 10 : Natural [N5] sub (0, succ(n)) = errorNatural errorNatural : Natural [N6] sub (m, 0) = m succ ( _ ) : Natural ! Natural [N7] sub(succ(m),succ(n)) =sub(m,n) add ( _ , _ ) : Natural, Natural ! Natural [N8] mul (m, 0) = 0 when m!errorNatural sub ( _ , _ ) : Natural, Natural ! Natural [N9] mul (m, 1) = m mul ( _ , _ ) : Natural, Natural ! Natural [N10] mul (m, succ(n)) = add (m, mul (m, n)) div ( _ , _ ) : Natural, Natural ! Natural [N11] div (m, 0) = errorNatural eq? ( _ , _ ) : Natural, Natural Boolean ! [N12] div (0, succ (n)) = 0 when n!errorNatural less? ( _ , _ ) : [N13] div (m, succ (n)) = Natural, Natural ! Boolean if ( less? (m, succ (n)), greater?( _ , _ ) : 0, Natural, Natural ! Boolean succ(div(sub(m,succ(n)),succ(n)))) end exports Chapter 12 7 Chapter 12 8 [N14] eq? (0, 0) = true [N15] eq? (0, succ (n)) = false Conditions are Necessary when n!errorNatural [N16] eq? (succ (m), 0) = false Use [N8] and ignore the condition: when m!errorNatural [N17] eq? (succ (m), succ (n)) = eq? (m, n) 0 = mul(succ(errorNatural),0) [N18] less? (0, succ (m)) = true = mul(errorNatural,0) when m!errorNatural = errorNatural. [N19] less? (m, 0) = false when m!errorNatural and [N20] less? (succ (m), succ (n)) = less? (m, n) succ(0) = succ(errorNatural) = errorNatural, [N21] greater? (m, n) = less? (n, m) succ(succ(0)) = end Naturals succ(errorNatural) = errorNatural, and so on. All operations propagate errors succ (errorNatural) = errorNatural, Conditions are needed when variable(s) on the left disappear on the right. sub (div(0,0), succ(0)) = errorNatural, not (errorBoolean) = errorBoolean, and eq? (0, succ (errorNatural)) = errorBoolean. Chapter 12 9 Chapter 12 10 Constructors A Module for Characters • No equations for 0 and succ module Characters • Terms 0, succ(0), succ(succ(0)), ... not equal importsBooleans, Naturals exports • These plus errorNatural can be viewed as characterizing the natural numbers, the sorts Char individuals defined by the module. operations eq? ( _ , _ ) : Char, Char ! Boolean • Initial algebraic semantics letter? ( _ ) : Char ! Boolean digit? ( _ ) : Char ! Boolean • No confusion property ord ( _ ) : Char ! Natural • No junk property char-0 : Char char-1 : Char : : char-9 : Char char-a : Char : : char-z : Char errorChar : Char end exports Chapter 12 11 Chapter 12 12 variables Parameterized Module and Instantiations c, c1, c2 : Char module Lists equations imports Booleans, Naturals [C1] ord (char-0) = 0 [C2] ord (char-1) = succ (ord (char-0)) parameters Items [C3] ord (char-2) = succ (ord (char-1)) sorts Item : : : operations [C11] ord (char-a) = succ (ord (char-9)) errorItem : Item [C12] ord (char-b) = succ (ord (char-a)) eq? : Item, Item ! Boolean : : : variables [C36] ord (char-z) = succ (ord (char-y)) a, b, c : Item equations [C37] eq? (c1, c2) = eq? (ord (c1), ord (c2)) eq? (a,a) = true when a!errorItem [C38] letter? (c) = eq? (a,b) = eq? (b,a) and (not (greater? (ord (char-a), ord (c))), implies(and(eq?(a,b),eq?(b,c)), not (greater? (ord (c), ord (char-z)))) eq?(a,c))=true [C39] digit? (c) = when a!errorItem, and (not (greater? (ord (char-0), ord (c))), b!errorItem, not (greater? (ord (c) ord (char-9)))) c!errorItem end Characters end Items Chapter 12 13 Chapter 12 14 exports equations sorts List [S1] concat (null, s) = s operations [S2] concat(cons(i,s1),s2) = null : List cons(i,concat(s1, s2)) errorList : List [S3] equal? (null, null) = true cons ( _ , _ ) : Item, List ! List concat ( _ , _ ) : List, List ! List [S4] equal? (null, cons (i, s)) = false length ( _ ) : List ! Natural when s!errorList, i!errorItem equal? ( _ , _ ): List, List Boolean ! [S5] equal? (cons (i, s), null) = false mkList ( _ ) : Item List ! when s!errorList, i!errorItem end exports [S6] equal? (cons (i1, s1), cons (i2, s2)) = variables and(eq?(i1, i2), equal?(s1, s2)) i, i1, i2 : Item [S7] length (null) = 0 s, s , s : List 1 2 [S8] length (cons (i, s)) = succ (length (s)) when i!errorItem [S9] mkList (i) = cons (i, null) end Lists Chapter 12 15 Chapter 12 16 Instantiations module Strings imports Booleans, Naturals, Characters, module Files instantiation of Lists importsBooleans, Naturals, bind Items using Char for Item instantiation of Lists using errorChar for errorItem bind Items using eq? for eq? using Natural for Item rename using String for List using errorNatural for errorItem using nullString for null using eq? for eq? using mkString for mkList rename using File for List using strEqual for equal? using emptyFile for null using errorString for errorList using mkFile for mkList using errorFile for errorList exports exports sorts String sorts File operations operations string-to-natural ( _ ) : empty? ( _ ) : File ! Boolean end exports String ! Boolean, Natural variables f : File end exports equations [F1] empty? (f) = equal? (f, emptyFile) end Files Chapter 12 17 Chapter 12 18 variables A Module for Finite Mappings c : Char b : Boolean module Mappings n : Natural s : String imports Booleans equations parameters Entries sorts Domain, Range [Str1] string-to-natural (nullString) = <true,0> operations [Str2] string-to-natural (cons (c, s)) = equals ( _ , _ ) : if ( and (digit? (c), b), Domain,Domain ! Boolean <true, add(mul(sub(ord(c),ord(char-0)), errorDomain : Domain exp(10, length(s))), n)>, errorRange : Range <false, 0>) variables when <b,n> = string-to-natural (s) a,b,c : Domain end Strings equations equals (a,a) = true when a!errorDomain Expression in [Str2]: equals (a,b) = equals (b,a) ((ord(c) – ord(char-0)) • 10length(s)) + n implies (and (equals (a,b), equals (b,c)), equals (a,c)) = true when a, b, and c ! errorDomain end Entries Chapter 12 19 Chapter 12 20 exports sorts Mapping A Store Structure operations emptyMap : Mapping module Stores errorMapping : Mapping imports Strings, Naturals, update( _ , _ , _ ) : Mapping,Domain,Range ! Mapping instantiation of Mappings apply ( _ , _ ) : bind Entries Mapping, Domain ! Range using String for Domain end exports using Natural for Range variables using strEqual for equals m : Mapping using errorString for errorDomain d, d1, d2 : Domain using errorNatural for errorRange r : Range rename using Store for Mapping equations using emptySto for emptyMap [M1] apply (emptyMap, d) = errorRange using updateSto for update [M2] apply (update(m, d1, r), d2) = r using applySto for apply when equals(d1,d2) = true, m!errorMapping end Stores [M3] apply (update(m, d1, r), d2) = apply(m, d2) when equals(d1,d2)=false, r!errorRange end Mappings Chapter 12 21 Chapter 12 22 Mathematical Foundations module Nats imports Bools Simplify modules. exports sorts Natural module Bools operations exports 0 : Natural sorts Boolean succ ( _ ) : Natural ! Natural operations add ( _ , _ ) : Natural, Natural ! Natural true : Boolean end exports false : Boolean variables not ( _ ) : Boolean ! Boolean m, n : Natural end exports equations equations [N1] add (m, 0) = m [B1] not (true) = false [N2] add (m, succ (n)) = succ (add (m, n)) [B2] not (false) = true end Nats end Bools Chapter 12 23 Chapter 12 24 Example: Ground terms of sort Boolean in Ground Terms Bools true, not(true), Function symbols used to construct terms not(not(true)), not(not(not(true))), ..