dhall-haskell icon indicating copy to clipboard operation
dhall-haskell copied to clipboard

Implement "hashOf" keyword

Open KAction opened this issue 3 years ago • 1 comments

Implement "hashOf" keyword, that returns hash of expression as text. I believe this keyword was already discussed previously, and was considered unwanted since it breaks assumption that

f :: forall a. a -> Text

must be implemented as "f = const ", but let me present my use-case.

Consider expression that describes SQL index:

let Index = {
  unique : Bool,
  expressions : List Text
}

and I have list of indexes. I want to generate SQL code that would make sure that table has these and only these indexes. For starters, index must have name, and it is very convenient to auto-generate name based on index properties. Hence the desire to get text hash of arbitrary expression, to be able to write something like following:

let f = \(i: Index) ->
  let name = hashOf i
  let unique = if i.unique then "unique" else ""
  in ''
    create ${unique} index index_${name} on foo.bar(${i.expressions});
  ''

Technically, same result (modulo ergonomics) could be achived by extending Dhall with function of type '(a: Type) -> (x: a) -> Text', but it is not as convenient.

Here is prelimitary patch, that fails several tests. Any chance to get this merged after being polished?

KAction avatar Jun 15 '21 11:06 KAction

@KAction: I think the most likely path forward is what I outlined in https://github.com/dhall-lang/dhall-lang/issues/739#issuecomment-726188100

In general, changes like these have to go through the standardization process

Gabriella439 avatar Jun 16 '21 15:06 Gabriella439