Skip to content

dreaded monomorphism restriction while implementing dictionary (map) type #641

@mengwong

Description

@mengwong
Image Image
// a simple implementation of a Dictionary type
GIVEN k IS A TYPE
      v IS A TYPE
GIVETH A TYPE
DECLARE Dictionary k v
    HAS contents IS A LIST OF PAIR OF k, v

GIVEN  a IS A TYPE
       b IS A TYPE
       k IS AN a
       v IS A  b 
singleToDict MEANS  Dictionary WITH contents IS LIST (PAIR OF k, v)

GIVEN k IS A TYPE
      v IS A TYPE
      pair IS A PAIR OF k, v
pairToDict MEANS  Dictionary WITH contents IS LIST pair

GIVEN k IS A TYPE
      v IS A TYPE
      pairs IS A LIST OF PAIR OF k, v
listToDict MEANS  Dictionary WITH contents IS pairs

fromList MEANS listToDict

// sample usage:
DECLARE MyDict IS A Dictionary STRING (LIST OF NUMBER)

DECLARE MyPair IS PAIR OF STRING, LIST OF NUMBER
GIVETH MyPair
mypair1 MEANS PAIR OF "one", LIST 1
mypair2 MEANS PAIR OF "two", LIST 2

mydict3 MEANS singleToDict "one" (LIST 1)
mydict4 MEANS listToDict OF LIST PAIR OF "one", LIST 1
                                 PAIR OF "two", LIST 2

#EVAL pairToDict mypair2
#EVAL mydict3
#EVAL mydict4

GIVEN k     IS A TYPE
      v     IS A TYPE
      key   IS A k
      dict  IS A Dictionary k v
dictLookup MEANS lookup key (dict's contents)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions