|
1 | | -abbrev SortBool : Type := Int |
2 | | -abbrev SortBytes: Type := ByteArray |
3 | | -abbrev SortId : Type := String |
4 | | -abbrev SortInt : Type := Int |
5 | | -abbrev SortString : Type := String |
| 1 | +/- |
| 2 | +K Prelude in Lean 4 |
| 3 | +
|
| 4 | +Functions with the `hook` attribute need to have a manual implementation in the backends. |
| 5 | +This file contains the Lean 4 definitions of the hooked functions in `domains.md`. |
| 6 | +
|
| 7 | +Currently we translate hooked functions as uninterpreted functions together with axioms asserting their behavior. |
| 8 | +The current definition can be put into three levels: |
| 9 | +
|
| 10 | +1. Signature Level: |
| 11 | +The signature of the hooks, this includes aliases for Sorts and function symbols for hooked functions. |
| 12 | +
|
| 13 | +2. Rule Level: |
| 14 | +The behavior of the uninterpreted symbols can be asserted through axioms or theorems. |
| 15 | +Inconsistencies can arise from them, so it falls under the user to make sure axioms are consistent and/or theorems provable. |
| 16 | +
|
| 17 | +3. Simplification Level: |
| 18 | +With the theory defined through function rules, simplifications can be stated as theorems. |
| 19 | +These theorems should be provable directly from the function rules and the semantics of the Sorts. |
| 20 | + -/ |
| 21 | + |
| 22 | +-- Basic K types |
| 23 | +abbrev SortBool : Type := Int |
| 24 | +abbrev SortBytes : Type := ByteArray |
| 25 | +abbrev SortId : Type := String |
| 26 | +abbrev SortInt : Type := Int |
| 27 | +abbrev SortString : Type := String |
6 | 28 | abbrev SortStringBuffer : Type := String |
7 | 29 |
|
8 | | -abbrev ListHook (E : Type) : Type := List E |
9 | | -abbrev MapHook (K : Type) (V : Type) : Type := List (K × V) |
10 | | -abbrev SetHook (E : Type) : Type := List E |
| 30 | + |
| 31 | +namespace MapHookDef |
| 32 | +/- |
| 33 | +The `Map` sort represents a generalized associative array. |
| 34 | +Each key can be paired with an arbitrary value, and can be used to reference its associated value. |
| 35 | +Multiple bindings for the same key are not allowed. |
| 36 | +Note that both keys and values will always be KItems. |
| 37 | + -/ |
| 38 | + |
| 39 | +-- Signature to be instantiated by map implementations |
| 40 | +structure MapHookSig (K V : Type) where |
| 41 | + map : Type -- Carrier, such as List (KItem × KItem) |
| 42 | + unit : map |
| 43 | + cons : K → V → map → map |
| 44 | + lookup : map → K → V |
| 45 | + lookup? : map → K → V -- lookup with default |
| 46 | + update : K → V → map → map |
| 47 | + delete : map → K → map |
| 48 | + concat : map → map → Option map |
| 49 | + difference : map → map → map |
| 50 | + updateMap : map → map → map |
| 51 | + removeAll : map → List K → map |
| 52 | + keys : map → List K |
| 53 | + in_keys : map → K → Bool |
| 54 | + values : map → List V |
| 55 | + size : map → Nat |
| 56 | + includes : map → map → Bool -- map inclusion |
| 57 | + choice : map → K -- arbitrary key from a map |
| 58 | + nodup : forall al : map, List.Nodup (keys al) |
| 59 | + |
| 60 | +-- We use axioms to have uninterpreted functions |
| 61 | +variable (K V : Type) |
| 62 | +axiom mapCAx : Type -- Map Carrier |
| 63 | +axiom unitAx : mapCAx |
| 64 | +axiom consAx : K → V → mapCAx → mapCAx |
| 65 | +axiom lookupAx : mapCAx → K → V |
| 66 | +axiom lookupAx? : mapCAx → K → V -- lookup with default |
| 67 | +axiom updateAx : K → V → mapCAx → mapCAx |
| 68 | +axiom deleteAx : mapCAx → K → mapCAx |
| 69 | +axiom concatAx : mapCAx → mapCAx → Option mapCAx |
| 70 | +axiom differenceAx : mapCAx → mapCAx → mapCAx |
| 71 | +axiom updateMapAx : mapCAx → mapCAx → mapCAx |
| 72 | +axiom removeAllAx : mapCAx → List K → mapCAx |
| 73 | +axiom keysAx : mapCAx → List K |
| 74 | +axiom in_keysAx : mapCAx → K → Bool |
| 75 | +axiom valuesAx : mapCAx → List V |
| 76 | +axiom sizeAx : mapCAx → Nat |
| 77 | +axiom includesAx : mapCAx → mapCAx → Bool -- map inclusion |
| 78 | +axiom choiceAx : mapCAx → K -- arbitrary key from a map |
| 79 | +axiom nodupAx : forall al : mapCAx, List.Nodup (keysAx K al) |
| 80 | + |
| 81 | +-- Uninterpreted Map implementation |
| 82 | +noncomputable def mapImpl (K V : Type) : MapHookSig K V := |
| 83 | + MapHookSig.mk |
| 84 | + mapCAx |
| 85 | + unitAx |
| 86 | + (consAx K V) |
| 87 | + (lookupAx K V) |
| 88 | + (lookupAx? K V) |
| 89 | + (updateAx K V) |
| 90 | + (deleteAx K) |
| 91 | + concatAx |
| 92 | + differenceAx |
| 93 | + updateMapAx |
| 94 | + (removeAllAx K) |
| 95 | + (keysAx K) |
| 96 | + (in_keysAx K) |
| 97 | + (valuesAx V) |
| 98 | + sizeAx |
| 99 | + includesAx |
| 100 | + (choiceAx K) |
| 101 | + (nodupAx K) |
| 102 | + |
| 103 | +end MapHookDef |
0 commit comments