-
Notifications
You must be signed in to change notification settings - Fork 7
Use (A×B×C) names for TypedTuples #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shingarov
wants to merge
6
commits into
deduction
Choose a base branch
from
prettyprint-tuple
base: deduction
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
200d2c3
Conceive package Deduction
shingarov 2b3e476
Move packages to `src` subdirectory
janvrany 13f2626
Add new class `TypedTuple`
janvrany 0eb3ff8
[Products] Improve TypedTuple
shingarov 24f0e3a
[Products] Introduce a few TypedTuple tests
shingarov 4b651a7
Use (A×B×C) names for TypedTuples
shingarov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| Class { | ||
| #name : #TypedTupleTest, | ||
| #superclass : #TestCase, | ||
| #category : #'Collections-Homogeneous-Tests' | ||
| } | ||
|
|
||
| { #category : #tests } | ||
| TypedTupleTest >> testEmptyProduct [ | ||
| | t | | ||
| t := TypedTuple empty. | ||
| self assert: t slots size equals: 0. | ||
| self assert: (t⨰Integer) slots size equals: 1 | ||
| ] | ||
|
|
||
| { #category : #tests } | ||
| TypedTupleTest >> testProductComposition [ | ||
| | t | | ||
| t := Int ⨰ String ⨰ Int. | ||
| self assert: t slots size equals: 3 | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Package { #name : #'Collections-Homogeneous-Tests' } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| Extension { #name : #Collection } | ||
|
|
||
| { #category : #'*Collections-Homogeneous' } | ||
| Collection >> isHomogeneous [ | ||
| Collection >> isTyped [ | ||
| ^false | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| Class { | ||
| #name : #TypedArray, | ||
| #superclass : #Array, | ||
| #type : #variable, | ||
| #classVars : [ | ||
| 'Arrays' | ||
| ], | ||
| #classInstVars : [ | ||
| 'elementSpecies' | ||
| ], | ||
| #category : #'Collections-Homogeneous' | ||
| } | ||
|
|
||
| { #category : #accessing } | ||
| TypedArray class >> elementSpecies [ | ||
| ^elementSpecies | ||
| ] | ||
|
|
||
| { #category : #accessing } | ||
| TypedArray class >> elementSpecies: aClass [ | ||
| self assert: aClass isClass. | ||
| self assert: self ~~ TypedArray. | ||
| self assert: elementSpecies isNil. | ||
|
|
||
| elementSpecies := aClass | ||
| ] | ||
|
|
||
| { #category : #initialization } | ||
| TypedArray class >> initialize [ | ||
| Arrays := SystemDictionary new. | ||
| ] | ||
|
|
||
| { #category : #JSON } | ||
| TypedArray class >> neoJsonMapping: mapper [ | ||
| mapper for: self customDo: [ :mapping | | ||
| mapping listOfElementSchema: elementSpecies | ||
| ] | ||
| ] | ||
|
|
||
| { #category : #'instance creation' } | ||
| TypedArray class >> new: size [ | ||
| self == TypedArray ifTrue:[ self shouldNotImplement ]. | ||
| ^super new: size. | ||
|
|
||
| " | ||
| (TypedArray of: String) new:10. | ||
| TypedArray new:10. | ||
| " | ||
|
|
||
|
|
||
| ] | ||
|
|
||
| { #category : #'subclass creation' } | ||
| TypedArray class >> of: aClass [ | ||
| | arrayName arrayClass | | ||
|
|
||
| self assert: aClass isClass. | ||
|
|
||
| arrayName := ('Array of: ', aClass name) asSymbol. | ||
| [ | ||
| arrayClass := self classBuilder | ||
| name: arrayName; | ||
| superclass: self; | ||
| build. | ||
| ] on: InvalidGlobalName do:[:ex| | ||
| ex resumeUnchecked: nil. | ||
| ]. | ||
|
|
||
| arrayClass elementSpecies: aClass. | ||
| ^arrayClass | ||
|
|
||
| " | ||
| Array of: String | ||
| " | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| ] | ||
|
|
||
| { #category : #testing } | ||
| TypedArray >> isTyped [ | ||
| ^true | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| Class { | ||
| #name : #TypedTuple, | ||
| #superclass : #Object, | ||
| #type : #variable, | ||
| #category : #'Collections-Homogeneous' | ||
| } | ||
|
|
||
| { #category : #'instance creation' } | ||
| TypedTuple class >> empty [ | ||
| ^self ofAll: #() | ||
| ] | ||
|
|
||
| { #category : #'instance creation' } | ||
| TypedTuple class >> nameFor: classes [ | ||
| | s | | ||
| s := WriteStream on: String new. | ||
| s nextPut: $(. | ||
| classes | ||
| do: [ :each | s nextPutAll: each name ] | ||
| separatedBy: [ s nextPutAll: '×' ]. | ||
| s nextPut: $). | ||
| ^s contents | ||
| ] | ||
|
|
||
| { #category : #JSON } | ||
| TypedTuple class >> neoJsonMapping: mapper [ | ||
| mapper for: self customDo: [ :mapping | mapping reader: [ :jsonReader | | ||
| | stillLeft acc inst | | ||
|
|
||
| stillLeft := self slots asOrderedCollection. | ||
| acc := OrderedCollection new. | ||
|
|
||
| jsonReader parseListDo: [ acc addLast: (jsonReader nextAs: stillLeft first type). stillLeft removeFirst ]. | ||
| stillLeft isEmpty ifFalse: [self error]. | ||
| inst := self basicNew. | ||
| acc withIndexDo: [ :eachValue :j | inst instVarAt: j put: eachValue ]. | ||
| inst | ||
| ]]. | ||
| ] | ||
|
|
||
| { #category : #'instance creation' } | ||
| TypedTuple class >> new [ | ||
| "Use #ofAll: or #empty." | ||
| self shouldNotImplement | ||
| ] | ||
|
|
||
| { #category : #'instance creation' } | ||
| TypedTuple class >> ofAll: elementNamesAndTypes [ | ||
| | tupleName tupleSlots tupleClass | | ||
| elementNamesAndTypes do:[:nameAndType | | ||
| self assert: (nameAndType isKindOf: Association). | ||
| self assert: nameAndType key isString. | ||
| self assert: nameAndType value isClass. | ||
| ]. | ||
| tupleName := self nameFor: (elementNamesAndTypes collect: #value). | ||
| tupleSlots := elementNamesAndTypes collect: | ||
| [:nameAndType | TypedTupleSlot named: nameAndType key type: nameAndType value]. | ||
| [ | ||
| tupleClass := self classBuilder | ||
| name: tupleName; | ||
| superclass: self; | ||
| slots: tupleSlots; | ||
| build. | ||
| ] on: InvalidGlobalName do:[:ex| | ||
| ex resumeUnchecked: nil. | ||
| ]. | ||
| ^tupleClass | ||
|
|
||
| " | ||
| TypedTuple ofAll:{ #x -> Integer . #y -> Integer } | ||
| " | ||
|
|
||
| ] | ||
|
|
||
| { #category : #accessing } | ||
| TypedTuple class >> slotAssociations [ | ||
| ^self slots collect: [ :eachSlot | eachSlot name -> eachSlot type ] | ||
| ] | ||
|
|
||
| { #category : #semigroup } | ||
| TypedTuple class >> ⨰ [ anotherClass | ||
| anotherClass isClass ifFalse: [ self shouldBeImplemented "A×(B×C) case" ]. | ||
| ^TypedTuple ofAll: | ||
| self slotAssociations, { '_', (self instSize + 1) printString -> anotherClass } | ||
| ] | ||
|
|
||
| { #category : #testing } | ||
| TypedTuple >> isTyped [ | ||
| ^true | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| Class { | ||
| #name : #TypedTupleSlot, | ||
| #superclass : #InstanceVariableSlot, | ||
| #instVars : [ | ||
| 'type' | ||
| ], | ||
| #category : #'Collections-Homogeneous' | ||
| } | ||
|
|
||
| { #category : #'instance creation' } | ||
| TypedTupleSlot class >> named: aString type: aClass [ | ||
| ^self new | ||
| name: aString; | ||
| type: aClass; | ||
| yourself. | ||
| ] | ||
|
|
||
| { #category : #comparing } | ||
| TypedTupleSlot >> = other [ | ||
| ^ super = other and: [type = other type] | ||
| ] | ||
|
|
||
| { #category : #comparing } | ||
| TypedTupleSlot >> hash [ | ||
| ^(super hash + type hash) hashMultiply | ||
| ] | ||
|
|
||
| { #category : #initalize } | ||
| TypedTupleSlot >> initialize: anObject [ | ||
| self write: type new to: anObject | ||
| ] | ||
|
|
||
| { #category : #printing } | ||
| TypedTupleSlot >> printOn: aStream [ | ||
| "Every subclass that adds state must redefine either this method or #definitionString" | ||
| aStream | ||
| nextPutAll: self class name; | ||
| nextPutAll: ' named: '. | ||
| self name storeOn: aStream. | ||
| aStream nextPutAll: ' type: '. | ||
| self type storeOn: aStream | ||
| ] | ||
|
|
||
| { #category : #accessing } | ||
| TypedTupleSlot >> type [ | ||
| ^ type | ||
| ] | ||
|
|
||
| { #category : #accessing } | ||
| TypedTupleSlot >> type: aClass [ | ||
| self assert: aClass isClass. | ||
| type := aClass | ||
| ] | ||
|
|
||
| { #category : #'meta-object-protocol' } | ||
| TypedTupleSlot >> wantsInitialization [ | ||
| "we need to call the initialize to set the default value for the base slot" | ||
| ^"true"false"<-- We need more magic in initialize: to support for example Integer" | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| Extension { #name : #Rule } | ||
|
|
||
| { #category : #'*Deduction-Pharo' } | ||
| Rule >> asTextMorph [ | ||
| | fontWidth premissMorphs premissText premissWidth conclusionMorph conclusionText conclusionWidth maxWidth hl hlWidth | | ||
| fontWidth := 'xxxxxxxxxx' asTextMorph width / 10. | ||
| premissMorphs := premisses collect: [ :eachPremiss | eachPremiss asTextMorph backgroundColor: Color white; yourself ]. | ||
| premissText := Text streamContents: [ :s | premissMorphs | ||
| do: [ :each | each imageForm printOn: s ] | ||
| separatedBy: [ s nextPutAll: ' ' ] ]. | ||
|
|
||
| conclusionMorph := conclusion asTextMorph backgroundColor: Color white; yourself. | ||
| conclusionText := Text streamContents: [ :s | conclusionMorph imageForm printOn: s ]. | ||
|
|
||
| premissWidth := premissText asTextMorph width. | ||
| conclusionWidth := conclusionText asTextMorph width. | ||
| maxWidth := premissWidth max: conclusionWidth. | ||
|
|
||
| hl := Text | ||
| new: (maxWidth / fontWidth + 2.5) ceiling | ||
| withAll: (Character codePoint: 16r23AF). "Horizontal Line Extension" | ||
| hlWidth := hl asTextMorph width. | ||
|
|
||
| ^((Text "pad premiss to center" | ||
| new: (hlWidth - premissWidth / 2 / fontWidth) rounded | ||
| withAll: $ ), | ||
| premissText, | ||
| String cr asText, | ||
| hl, | ||
| ('[', name, ']') asText, | ||
| String cr asText, | ||
| (Text "pad conclusion to center" | ||
| new: (hlWidth - conclusionWidth / 2 / fontWidth) rounded | ||
| withAll: $ ), | ||
| conclusionText) asTextMorph backgroundColor: Color white; yourself | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Package { #name : #'Deduction-Pharo' } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| Class { | ||
| #name : #DeductionRuleTest, | ||
| #superclass : #TestCase, | ||
| #category : #'Deduction-Tests' | ||
| } | ||
|
|
||
| { #category : #examples } | ||
| DeductionRuleTest >> example1 [ | ||
| ^Rule | ||
| premisses: #(A B) | ||
| conclusion: #C | ||
| name: 'Ax' | ||
| ] | ||
|
|
||
| { #category : #examples } | ||
| DeductionRuleTest >> example2 [ | ||
| | d1 d | | ||
| d1 := Rule | ||
| premisses: #(1) | ||
| conclusion: 1234567890 | ||
| name: 'INTRO'. | ||
| d := Rule | ||
| premisses: {d1 . 2} | ||
| conclusion: 3 | ||
| name: 'ELIM'. | ||
| ^d | ||
| ] | ||
|
|
||
| { #category : #examples } | ||
| DeductionRuleTest >> test1 [ | ||
| | r | | ||
| r := self example1. | ||
|
|
||
| ] | ||
|
|
||
| { #category : #examples } | ||
| DeductionRuleTest >> testGT [ | ||
| | r | | ||
| r := self example2. | ||
| r asTextMorph halt | ||
| ] | ||
|
|
||
| { #category : #examples } | ||
| DeductionRuleTest >> testGTXX [ | ||
| | r f | | ||
| f := '/home/boris/boris2.jpg' asFileReference. | ||
| r := self example2. | ||
| r asTextMorph halt | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Package { #name : #'Deduction-Tests' } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.